【问题标题】:ESlint: Turning off a specific rule across a projectESlint:关闭项目中的特定规则
【发布时间】:2016-08-28 09:45:35
【问题描述】:

我已经阅读了有关如何从文件中禁用规则的文档,但是我想知道是否有一种方法可以禁用或覆盖来自 .eslintrc 的规则,而不会覆盖我定义的其他以前的规则和预设。我在一个 AngularJS 项目中工作,所以我在 .eslintrc 文件中使用了 extends 属性。

我想禁用 Angular ESlint 插件的 3 个特定规则,而不禁用我之前使用的所有其他规则。

如果没有rules 属性,我所有的 lint 规则都可以正常工作(间距、语法错误等),但我不想自然地出现两个 lint 错误。但是,附加了rules 属性后,我之前的所有规则都不再适用(间距语法错误等)。

我可以只声明一个规则来禁用我不想在文件顶部应用的特定规则,但这会非常重复(这就是我之前所做的)。

{
  "rules": {
    "wrap-iife": [
      2,
      "inside"
    ],
    "max-len": [
      2,
      120
    ],
    "indent": [
      2,
      2
    ],
    "no-with": 2,
    "no-implicit-coercion": [
      2, {
        "string": true
      }
    ],
    "camelcase": [
      2, {
        "properties": "never"
      }
    ],
    "quotes": [
      2,
      "single"
    ],
    "linebreak-style": [
      2,
      "unix"
    ],
    "semi": [
      2,
      "always"
    ]
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  },
  "ecmaFeatures": {
    "modules": true
  },
  "globals": {
    "angular": true
  },
  "extends": "angular",

  //I just want to disable these rules & still use everything else
  //but when I add this, everything else I had previously no longer
  //applies
  "rules": {
    "angular/controller-as-vm": "off",
    "angular/document-service": "off",
    "angular/window-service": "off"
  }
}

【问题讨论】:

    标签: javascript jshint eslint


    【解决方案1】:

    .eslintrc 中有两个 rules 键。第二个将覆盖第一个。这是规则合并在一起的文件:

    {
      "rules": {
        "wrap-iife": [
          2,
          "inside"
        ],
        "max-len": [
          2,
          120
        ],
        "indent": [
          2,
          2
        ],
        "no-with": 2,
        "no-implicit-coercion": [
          2, {
            "string": true
          }
        ],
        "camelcase": [
          2, {
            "properties": "never"
          }
        ],
        "quotes": [
          2,
          "single"
        ],
        "linebreak-style": [
          2,
          "unix"
        ],
        "semi": [
          2,
          "always"
        ],
        "angular/controller-as-vm": "off",
        "angular/document-service": "off",
        "angular/window-service": "off"
      },
      "env": {
        "es6": true,
        "browser": true,
        "node": true
      },
      "ecmaFeatures": {
        "modules": true
      },
      "globals": {
        "angular": true
      },
      "extends": "angular"
    }
    

    【讨论】:

    • 哇——就是这样。谢谢!
    【解决方案2】:

    单独禁用所有规则的 ESLint 配置文件。

    这是一个包含所有规则的 Eslint 配置文件 (eslintrc.json) 关闭,以便您可以按规则更改代码 而不是更改所有代码以适应所有规则?

    https://github.com/ajmaurya99/eslint-rules

    【讨论】:

      猜你喜欢
      • 2015-02-28
      • 2016-04-18
      • 2017-05-10
      • 2020-03-25
      • 2023-01-05
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多