ESlint: Turning off a specific rule across a project

为君一笑 提交于 2021-01-02 19:16:35

问题


I've read the docs on how to disable rules from within a file, however I'm wondering if there is a way to disable or overwrite rules from .eslintrc without overwriting other previous rules & presets I defined. I'm working in an AngularJS project so I used extends property inside my .eslintrc file.

There are 3 specific rules from the angular ESlint plugin that I would like to disable, without disabling everything else I was using previously.

Without the rules property all of my linting rules work fine (spacing, grammar errors etc), but the two lint errors I don't want show up, naturally. However, with the rules property attached, all of my previous rules no longer get applied (spacing grammar errors etc).

I could just declare a rule to disable the specific rule I don't want applied at the top of my file, but that get's very repetitive (that's what I had done previously).

{
  "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"
  }
}

回答1:


In you .eslintrc you have two rules keys. The second will overwrite the first. Here's the file with the rules merged together:

{
  "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 config file that disables all rules individually.

This is a Eslint config (eslintrc.json) file that has all the rules turned off so that you can change your code on a rule by rule basis rather than changing all the code to fit all the rules?

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



来源:https://stackoverflow.com/questions/37002574/eslint-turning-off-a-specific-rule-across-a-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!