Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn off ESLint's no-restricted-syntax rule just for ForOfStatement?

I am using ESLint for my ES6 program, with the AirBNB rule-set. For good and adequate reasons, I am using the for...of construction in my code, but ESLint objects to it, issuing a no-restricted-syntax error.

The documentation at http://eslint.org/docs/rules/no-restricted-syntax explains how I can specify in my .eslint file the set of syntax-tree nodes that it objects to: for example, if all I dislike is the with statement, I can use:

"no-restricted-syntax": ["warn", "WithStatement"]

But I don't want to specify a whole set of unapproved constructions, I just want to say that I consider one such construction OK. Something conceptually similar to

ESlint.rules['no-restricted-syntax'].removeEntry('ForOfStatement');

Is there a way to do this in the ESLint file? Or, failing that, is there at least a way to get it to tell me what its current no-restricted-syntax configuration is, so I can manually remove ForOfStatement from it?

like image 815
Mike Taylor Avatar asked Feb 14 '17 12:02

Mike Taylor


People also ask

How do I turn off rule ESLint?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.

What is no restricted syntax?

Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of.


1 Answers

Check existing config

Based on the current master branch, eslint-config-airbnb currently disables four syntax forms:

  1. ForInStatement
  2. ForOfStatement
  3. LabeledStatement
  4. WithStatement

You can verify this or see if there are any differences by using ESLint's --print-config CLI flag:

$ eslint --print-config file.js 

ESLint will print out the config it would use to lint file.js, and you can find the config for the no-restricted-syntax rule there.

Override no-restricted-syntax

If you want to override Airbnb's preset, you can do so in the rules section of your .eslintrc.json file:

{     "rules": {         "no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"]     } } 

There's no way to configure the rule to use the no-restricted-syntax config inherited from Airbnb's preset excepting only a single syntax form.

like image 85
btmills Avatar answered Sep 22 '22 06:09

btmills