LoginSignup
0
0

More than 5 years have passed since last update.

ES2015 で Hubot のスクリプトを書くと `using deprecated documentation syntax` って怒られる件について

Last updated at Posted at 2016-08-29

追記あり

前置き

最近、 Hubot 使って ChatOps なコトをしようと思っており、色々ググりながら環境作ったりしてました。

んで、概ね巧く動くところまでは出来たんだけど、いざ Hubot 起動すると以下のように怒られる。

$ bin/hubot
hubot_project> [Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO /path/to/hubot_project/scripts/test.js is using deprecated documentation syntax
[Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

下の2行は Redis についての単なる情報だと思うので無視するとして、問題は1行目。

警告的なメッセージが出るのは気持ち悪いので、色々調べてみた。

んで、一応、解決出来た (?) ので綴ってみる。

Documentation

先ず、Hubot Script に関する説明がないコトが根本的な原因だったので、公式のドキュメントに従い、 Commands: を追加してやった。

test.js
// Commands:
//  ぬるぽ - ガッ!
//
module.exports = (robot) => {
  robot.hear(
    /^ぬるぽ$/,
    (message) => {
      message.send("ガッ!");
    }
  );
};

あ、勿論 .babelrc"comments": true の追記を忘れずに。
これが無いと、トランスパイル時にコメント落とされちゃうので。

.babelrc
{
  "presets": ["es2015"],
  "comments": true
}

さぁ、どうかな!?

$ bin/hubot
hubot_project> [Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO /path/to/hubot_project/scripts/test.js is using deprecated documentation syntax
[Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

………でも、相変わらず怒られ続ける。

'use strict';

なんでかなー?と思ってトランスパイル後の JS ファイルを見てみたところ、こんな感じになってた。

test.js(transpiled)
'use strict';

// Commands:
//  ぬるぽ - ガッ!
//
module.exports = function (robot) {
  robot.hear(/^ぬるぽ$/, function (message) {
    message.send("ガッ!");
  });
};

babel-presets-es2015 を使う場合、1行目に 'use strict'; が出力されるので、 Hubot 的な規約としての

Hubot scripts can be documented with comments at the top of their file

ってのとぶつかってしまうコトが原因。

解決策

じゃあ、どうするか?

Hubot 側にプルリク送るというのも手だけど、ちょっと今そんな時間ないよ!っていう人はトランスパイラ側を変更しちゃえば良いんじゃないかと。

具体的には、 babel-presets-es2015 ではなく babel-presets-es2015-without-strict という、そのものズバリなパッケージがありました。

$ npm i -D babel-presets-es2015-without-strict
.babelrc
{
  "presets": ["es2015-without-strict"],
  "comments": true
}

いざ!

$ bin/hubot
hubot_project> [Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Mon Aug 29 2016 12:30:53 GMT+0900 (JST)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

てってれー!

これで、怒られることがなくなりました!

勿論、 strict ではなくなるので、100点満点の解決策ではないですが、まぁ、及第点…かな?

追記: 2016/08/29 20:40

@mohayonao さんからコメントを頂戴しました。

test.js
// Commands:
//  ぬるぽ - ガッ!
//

"use strict";

module.exports = (robot) => {
  robot.hear(
    /^ぬるぽ$/,
    (message) => {
      message.send("ガッ!");
    }
  );
};

と言った感じに、 ES2015 な JS 側に自分で use strict を書いてあげれば babel-presets-es2015 側で自動追記されることがなくなるので、この方がスマートですね!

「毎回書くのが…」みたいなケースも、IDE のコードテンプレートとか使えば、さほど不便はないし。

@mohayonao さん、コメントありがとうございました!

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0