如何使用eslint修复单个规则

47

我想使用eslint命令标志--fix来修复一个规则,我已经尝试了以下命令:eslint --fix --config ./.eslintrcsomerules.js .。但它没有生效。如何实现目标?

.eslintrcsomerules.js文件内容如下:

module.exports = {
  'rules': {
    'indent': [2, 2],
  }
};

https://eslint.org/docs/4.0.0/user-guide/configuring - Prasanna
4个回答

21

为了修复由一个规则引起的问题,您需要将--fix --rule--no-eslintrc结合使用。否则,您的规则将与现有配置合并,并且所有可修复的问题都将得到纠正。例如:

$ eslint --no-eslintrc --fix --rule 'indent: [2, 2]'

根据你的设置,你需要将ESLint配置中的强制选项重新添加到命令行中,以使lint运行成功。在我的情况下,我不得不使用

Depending on your setup you'll need to re-add mandatory settings from your ESLint configuration to the command line for the lint run to succeed. In my case I had to use
$ eslint --no-eslintrc --parser babel-eslint --fix --rule 'indent: [2, 2]'

你可能需要添加的更多强制设置可以在这里找到:https://eslint.org/docs/latest/use/command-line-interface - hb20007
在我的情况下,它是eslint --no-eslintrc --parser babel-eslint --plugin react --parser-options ecmaVersion:12 --parser-options sourceType:module --parser-options ecmaFeatures:{jsx:true} --fix --rule 'indent: [2, 2]' - hb20007

19

我也遇到过类似的问题。我正在使用create-react-app,关闭所有规则但保留所有配置来解析ES6/TS等内容非常困难。

这个库正是我需要的!我可以看到每个规则有多少错误,并逐个修复规则。
https://github.com/IanVS/eslint-nibble


11
我是 eslint-nibble 的作者,很高兴您觉得它有用!在幕后,它使用 https://github.com/IanVS/eslint-filtered-fix,如果你只是寻找一个简单的 CLI 工具,那么它也可以直接使用。 - IanVS

13

您可以使用--rule标志并附上您要修复的规则。例如:

eslint --fix --rule 'quotes: [2, double]' .

查看官方文档获取更多信息。

可用规则列表也可能会有帮助。


1
另一个例子: yarn lint --fix --rule '@typescript-eslint/member-delimiter-style: error' - gvlax
1
npm run lint --fix --rule '"arrow-parens": ["warn", "as-needed"]' 不起作用,有什么想法吗,@Lucas Caton - Achy97

1
忽略.eslintrc的建议答案。
eslint --no-eslintrc --fix --rule 'indent: [2, 2]'

可能会失败,如果您的 .eslintrc 文件中包含了预处理器,没有这些预处理器会导致错误,比如:
Parsing error: The keyword 'import' is reserved

我建议使用eslint-interactive,它可以让你选择要修复的规则。
yarn install -D eslint-interactive
eslint-interactive . # This will interactively guide you.
yarn remove -D eslint-interactive

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接