LoginSignup
12
6

More than 5 years have passed since last update.

ESLintのno-restricted-syntaxでfor-ofだけを許可

Last updated at Posted at 2017-07-25

ESLintでairbnb-base使ってます。 以前まで使ってました (追記参照)。

ES2015のfor-ofを使おうとすると、no-restricted-syntaxとしてエラーになってしまいます。以下の記事がとても参考になりました。
ESLint対応物語 ~no-restricted-syntax~

事情はわかりましたが、それでも僕はfor-ofを使いたい!
しかしfor-inとかは禁止で良いのでno-restricted-syntax自体を無効にはしたくない!

ってことで、for-ofだけを許可する方法です。

まずairbnb-baseの大元の設定を確認。今回はこのファイルでした。

node_modules/eslint-config-airbnb-base/rules/style.js
    // disallow certain syntax forms
    // http://eslint.org/docs/rules/no-restricted-syntax
    'no-restricted-syntax': [
      'error',
      {
        selector: 'ForInStatement',
        message: 'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.',
      },
      {
        selector: 'ForOfStatement',
        message: 'iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations.',
      },
      {
        selector: 'LabeledStatement',
        message: 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.',
      },
      {
        selector: 'WithStatement',
        message: '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.',
      },
    ],

この部分を自分の.eslintrcに転記して、不要な部分を削除すればOK
僕はyamlで書いてるので、分かりやすいようにコメントアウトにしてみました。

.eslintrc.yml
  # for-ofは認める
  no-restricted-syntax:
    - error
    - selector: 'ForInStatement'
      message: 'for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.'
    # - selector: 'ForOfStatement'
    #   message: 'iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations.'
    - selector: 'LabeledStatement'
      message: 'Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand.'
    - selector: 'WithStatement'
      message: '`with` is disallowed in strict mode because it makes code impossible to predict and optimize.'

最初は「大元のno-restricted-syntaxで設定済みのselectorのうちForOfStatementだけを無効にする」のかと思ったんですが、そうではなく「自分でno-restricted-syntaxを書いた場合は大元のno-restricted-syntaxは全部キャンセルされて自分の設定だけが使われる」ということなんですね。

no-restricted-syntaxだけでなく、他のルールでも同様なはず。たぶんESLint自体の仕様として誰かがまとめてるとは思うんだけど、見つからなかったです :sweat_smile:

ではまた。

2017/10/31 追記

airbnb-baseのルールは厳しすぎて、本件以外にも色々と面倒臭い要素が多くて時間をどんどん浪費することに気づきまして、使うのやめました!

今はstandard使ってます。何より「セミコロン書かない」ってところに惚れました!もうね、セミコロン書かないだけで効率が全然上がる(笑)

12
6
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
12
6