简体   繁体   English

如何仅针对 ForOfStatement 关闭 ESLint 的无限制语法规则?

[英]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.我将 ESLint 用于我的 ES6 程序,并带有 AirBNB 规则集。 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.出于充分和充分的原因,我在我的代码中使用了for...of构造,但 ESLint 反对它,发出no-restricted-syntax错误。

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: http://eslint.org/docs/rules/no-restricted-syntax 上的文档解释了我如何在我的.eslint文件中指定它反对的语法树节点集:例如,如果我不喜欢的是with语句,我可以使用:

"no-restricted-syntax": ["warn", "WithStatement"] “无限制语法”:[“警告”,“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'); ESlint.rules['no-restricted-syntax'].removeEntry('ForOfStatement');

Is there a way to do this in the ESLint file?有没有办法在 ESLint 文件中做到这一点? 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?或者,如果失败了,是否至少有一种方法可以让它告诉我它当前no-restricted-syntax配置是什么,以便我可以从中手动删除 ForOfStatement ?

Check existing config 检查现有配置

Based on the current master branch, eslint-config-airbnb currently disables four syntax forms : 基于当前的主分支, eslint-config-airbnb目前禁用四种语法形式

  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 CLI标志验证这一点或查看是否存在任何差异:

$ 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. ESLint将打印出它将使用皮棉的配置file.js ,你可以找到的的配置no-restricted-syntax规则存在。

Override no-restricted-syntax 覆盖no-restricted-syntax

If you want to override Airbnb's preset, you can do so in the rules section of your .eslintrc.json file: 如果要覆盖Airbnb的预设,可以在.eslintrc.json文件的rules部分中.eslintrc.json操作:

{
    "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. 除了单一语法形式之外, no-restricted-syntax将规则配置为使用从Airbnb预设继承no-restricted-syntax配置。

Add the below lines of code to restrict this error in your application in .eslintrc.js file在 .eslintrc.js 文件中添加以下代码行以限制应用程序中的此错误

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"]
  },
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中解析eslinter的无限制语法? - How do I resolve the no-restricted-syntax from eslinter in JavaScript ? 在两个无限制语法违规之间捕获 - Caught between two no-restricted-syntax violations 如果我根据数组特定索引中的对象分配两个变量,如何传递 eslint 的“优先解构”规则? - How do I pass eslint's "prefer-destructuring" rule if I'm assigning two variables based off an object in a specific index of an array? 有没有办法在babelify中关闭“超级之前不允许这样的规则”? - Is there a way to turn off the “this is not allowed before super” rule in babelify? 如何将推荐的ESLint传递给package.json? - How can I pass ESLint recommended to package.json? 导入的ESLint规则错误 - ESLint rule error for import 多行函数调用的eslint规则 - eslint rule for multiline function calls 如何避免eslint规则不必要的三元违规:“对默认赋值不必要使用条件表达式” - How to avoid eslint rule no-unneeded-ternary violation: 'Unnecessary use of conditional expression for default assignment' 如何修复类方法将使用的“警告预期”“这个”“eslint”错误? - How can I fix 'warning Expected 'this' to be used by class method ' eslint error? 在尝试使用箭头函数返回三元运算符时,如何修复ES6 Eslint错误? - How can I fix ES6 Eslint errors while trying to return a ternary operator in arrow function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM