是否可以关闭整个文件的eslint规则?例如:

// eslint-disable-file no-use-before-define 

(类似于eslint-disable-line。)我经常遇到这样的情况,在某个文件中,我在许多地方违反了特定的规则,这对该文件来说是OK的,但我不想为整个项目禁用该规则,也不想为该特定文件禁用其他规则。


当前回答

例如,你可以把这个放在文件的顶部:

/* eslint-disable no-console */

其他回答

你可以配置eslint overrides属性来关闭匹配glob模式的文件的特定规则,如下所示。

在这里,我想关闭测试所有文件的no-duplicate-string规则。

overrides: [
  {
    files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
    rules: {
      'sonarjs/no-duplicate-string': 'off'
    }
  }
]

您可以通过将配置放在文件的顶部来关闭/更改文件的特定规则。

/* eslint no-use-before-define: 0 */  // --> OFF

or

/* eslint no-use-before-define: 2 */  // --> ON

更多信息

接受的答案不适合我(可能是eslint的不同版本…?)我使用eslint v3.19.0),但确实找到了一个解决方案与以下…

将以下内容放在文件顶部

/* eslint-disable - no-use-before-define */

这将禁用整个文件的规则

要禁用文件夹内文件的特定规则,您需要使用.eslintrc配置文件的"overrides"键。

例如,如果您想删除以下规则:

no-use-before-define max-lines-per-function

对于以下主目录中的所有文件:

/规范

您可以将此添加到您的.eslintrc文件…

"overrides": [
  {
    "files": ["spec/**/*.js"],
    "rules": {
      "no-use-before-define": ["off"],
      "max-lines-per-function": ["off"]
    }
  }
]

注意,我在spec/**/*.js glob中使用了**,这意味着我正在递归地查找名为spec的文件夹中的所有子文件夹,并选择所有以js结尾的文件,以便从中删除所需的规则。

你可以使用/*eslint [<rule: "off"], >]*/关闭一个文件的特定规则

/* eslint no-console: "off", no-mixed-operators: "off" */

版本:eslint@4.3.0