Turning off eslint rule for a specific file

后端 未结 15 1455
后悔当初
后悔当初 2020-11-30 17:26

Is it possible to turn off the eslint rule for the whole file? Something such as:

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

(Analogous t

相关标签:
15条回答
  • 2020-11-30 17:29

    To disable specific rules for file(s) inside folder(s), you need to use the "overrides" key of your .eslintrc config file.

    For example, if you want to remove the following rules:

    1. max-lines-per-function
    2. import/no-unresolved

    For all files inside the following main folder:

    /spec

    You can add this to your .eslintrc file...

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

    Note that I used ** inside the spec/**/*.js regex, which means I am looking recursively for all subfolders inside the folder called spec and selecting all files that ends with js in order to remove the desired rules from them.

    0 讨论(0)
  • 2020-11-30 17:30

    To disable a specific rule for the file:

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

    Note there is a bug in eslint where single line comment will not work -

    // eslint-disable max-classes-per-file
    // This fails!
    

    Check this post

    0 讨论(0)
  • 2020-11-30 17:32

    It's better to add "overrides" in your .eslintrc.js config file. For example if you wont to disable camelcase rule for all js files ending on Actions add this code after rules scope in .eslintrc.js.

    "rules": {    
    ...........    
    },
    "overrides": [
     {
      "files": ["*Actions.js"],
         "rules": {
            "camelcase": "off"   
         }
     }
    ]
    
    0 讨论(0)
  • 2020-11-30 17:35

    You can also disable/enable a rule like this:

    /* eslint-disable no-use-before-define */
    ... code that violates rule ...
    /* eslint-enable no-use-before-define */
    

    Similar to eslint-disable-line as mentioned in the question. It might be a better method if you don't want to have to restore a complicated rule configuration when re-enabling it.

    0 讨论(0)
  • 2020-11-30 17:35

    To temporarily disable rule warnings in your file, use block comments in the following format:

    /* eslint-disable */
    

    This will disable ESLint until the

    /* eslint-enable */
    

    comment is reached.

    You can read more about this topic here.

    0 讨论(0)
  • 2020-11-30 17:35

    You can just put this for example at the top of the file:

    /* eslint-disable no-console */
    
    0 讨论(0)
提交回复
热议问题