Turning off eslint rule for a specific file

后端 未结 15 1457
后悔当初
后悔当初 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:51

    you can configure eslint overrides property to turn off specific rules on files which matches glob pattern like below.

    Here, I want to turn off the no-duplicate-string rule for tests all files.

    overrides: [
      {
        files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
        rules: {
          'sonarjs/no-duplicate-string': 'off'
        }
      }
    ]
    
    0 讨论(0)
  • 2020-11-30 17:55

    You can tell ESLint to ignore specific files and directories by creating an .eslintignore file in your project’s root directory:

    .eslintignore

    build/*.js
    config/*.js
    bower_components/foo/*.js
    

    The ignore patterns behave according to the .gitignore specification. (Don't forget to restart your editor.)

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

    Based on the number of rules you want to ignore (All, or Some), and the scope of disabling it (Line(s), File(s), Everywhere), we have 2 × 3 = 6 cases.


    1) Disabling "All rules"


    Case 1.1: You want to disable "All Rules" for "One or more Lines"

    Two ways you can do this:

    1. Put /* eslint-disable-line */ at the end of the line(s),
    2. or /* eslint-disable-next-line */ right before the line.

    Case 1.2: You want to disable "All Rules" for "One File"

    • Put the comment of /* eslint-disable */ at the top of the file.

    Case 1.3: You want to disable "All rules" for "Some Files"

    There are 3 ways you can do this:

    1. You can go with 1.2 and add /* eslint-disable */ on top of the files, one by one.
    2. You can put the file name(s) in .eslintignore. This works well especially if you have a path that you want to be ignored. (e.g. apidoc/**)
    3. Alternatively, if you don't want to have a separate .eslintignore file, you can add "eslintIgnore": ["file1.js", "file2.js"] in package.json as instructed here.

    2) Disabling "Some Rules"


    Case 2.1: You want to disable "Some Rules" for "One or more Lines"

    Two ways you can do this:

    1. You can put /* eslint-disable-line quotes */ (replace quotes with your rules) at the end of the line(s),

    2. or /* eslint-disable-next-line no-alert, quotes, semi */ before the line.


    Case 2.2: You want to disable "Some Rules" for "One File"

    • Put the /* eslint-disable no-use-before-define */ comment at the top of the file.

    More examples here.


    Case 2.3: You want to disable "Some Rules" for "Some files"

    • This is less straight-forward. You should put them in "excludedFiles" object of "overrides" section of your .eslintrc as instructed here.
    0 讨论(0)
提交回复
热议问题