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

3

66

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?

Haematic answered 14/2, 2017 at 12:38 Comment(4)
a shortcut, not the best answer is using the disabling eslint rules with comments for blocks or lines or files.Neolith
If you have installed the airbnb ruleset, you can surely look it up in the respective configuration file?Zina
Thanks, amir. I know how to tell ESLint "ignore this line" or "ignore this rule for this line": I want to be more ambitious, and teach ESLint that I consider the use of for...of always OK.Haematic
for those like me, who arrive here via google search, here's a good discussion about why the syntax is restricted in the first place: github.com/airbnb/javascript/issues/1271Sketchy
E
1

You can, but only if you use a JS config file.

In the old config system (.eslintrc.js):

const styleConfig = require('eslint-config-airbnb-base/rules/style');
const [_severity, ...restrictedSyntax] = styleConfig.rules['no-restricted-syntax'];

In the new flat config (eslint.config.js):

const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: js.configs.recommended,
});

const airbnb = compat.extends('airbnb');
const [_severity, ...restrictedSyntax] = airbnb.find(c => c.rules?.['no-restricted-syntax']);

And then manipulate the restrictedSyntax variable as desired.

Economics answered 1/12, 2023 at 18:47 Comment(0)
B
87

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.

Bautram answered 14/2, 2017 at 22:48 Comment(4)
Thanks, btmills. The bit of a puzzle I was missing was --print-config, which at least allows me to hand-craft an .eslint rule configuration that makes minimal changes to what I inherited.Haematic
It's a shame there is no way to have .eslintrc modify and existing rule configuration rather then replacing it; but since that's how it is, your answer tells me all there is to know. I will Accept it as soon as I find the right button!Haematic
How can I enable ForInStatement and ForOfStatement?Tonatonal
@Tonatonal same as above except use ["error", "LabeledStatement", "WithStatement"]Lewie
P
2

Add the below lines of code to restrict this error in your application in .eslintrc.js file

module.exports = {
  extends: ['airbnb-base', 'plugin:prettier/recommended'],
  plugins: ['no-only-tests'],
  rules: {
    'no-only-tests/no-only-tests': 2,
    "no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"]
  },
};
Pragmatism answered 2/9, 2021 at 7:24 Comment(0)
E
1

You can, but only if you use a JS config file.

In the old config system (.eslintrc.js):

const styleConfig = require('eslint-config-airbnb-base/rules/style');
const [_severity, ...restrictedSyntax] = styleConfig.rules['no-restricted-syntax'];

In the new flat config (eslint.config.js):

const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: js.configs.recommended,
});

const airbnb = compat.extends('airbnb');
const [_severity, ...restrictedSyntax] = airbnb.find(c => c.rules?.['no-restricted-syntax']);

And then manipulate the restrictedSyntax variable as desired.

Economics answered 1/12, 2023 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.