ESLint 忽略特定目录的特定规则

ESLint ignore specific rule for a specific directory

ESLint 是否可以忽略整个目录的一个特定规则?

就我而言,我想忽略名为 commonComponents

的目录的 import/prefer-default-export

ESLint configuration (.eslintrc) files 是分层的:

ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.

您可以通过在该目录中创建包含以下内容的 .eslintrc 文件来禁用 commonComponents 目录的 import/prefer-default-export 规则:

{
    "rules": {
        "import/prefer-default-export": "off"
    }
}

如果您要将规则应用到多个目录,则可以为不同的目的创建不同的配置。例如:

  • .eslintrc.json 普通配置
  • .eslintrc-main.json 用于主要 linting 和 运行 eslint -c .eslintrc-main src test
  • .eslintrc-comp.json 组件和 运行 eslint -c .eslintrc-comp commonComponents fooBarComponent

您还可以使用 "overrides" 键为不同的 glob 模式声明规则。

阅读 Configuration Based on Glob Patterns

Sometimes a more fine-controlled configuration is necessary, for example if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the overrides key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., app/**/*.test.js).

我用它来从我的测试文件中删除 no-unused-expressions 规则;

"overrides": [{
  "files": [ "*.spec.js" ],
  "rules": {
    "no-unused-expressions": 0
  }
}]

YAML 版本:

rules:
  no-unused-expressions: true

overrides:
  - files: *-tests.js
    rules:
      no-unused-expressions: false

cretae .eslintignore 文件并将排除的文件夹放入其中。示例:

node_modules/
functions/
dist/