Search code examples
ecmascript-6eslintfor-of-loop

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?


Solution

  • 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.