Javascript 关闭特定文件的 eslint 规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34764287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:49:41  来源:igfitidea点击:

Turning off eslint rule for a specific file

javascriptconfigurationlinteslint

提问by Tomas Kulich

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

是否可以关闭整个文件的 eslint 规则?例如:

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

(Analogous to eslint-disable-line.) It happens to me quite often, that in a certain file, I'm breaking a specific rule on many places which is considered OK for that file, but I don't want to disable the rule for the whole project nor do I want to disable other rules for that specific file.

(类似于 eslint-disable-line。)我经常遇到这样的情况,在某个文件中,我在许多地方违反了对该文件来说可以的特定规则,但我不想禁用整个项目的规则,我也不想禁用该特定文件的其他规则。

采纳答案by Gyandeep

You can turn off/change a particular rule for a file by putting the configurations at the top of the file.

您可以通过将配置放在文件顶部来关闭/更改文件的特定规则。

/* eslint no-use-before-define: 0 */  // --> OFF

or

/* eslint no-use-before-define: 2 */  // --> ON

More info

更多信息

回答by Wahome

Just place /* eslint-disable */at the top of the file.

只需放在/* eslint-disable */文件的顶部。

Or disable a specific rule:

或禁用特定规则:

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

Check this post

检查这个帖子

回答by prograhammer

You can tell ESLint to ignore specific files and directoriesby creating an .eslintignorefile in your project's root directory:

您可以通过在项目的根目录中创建一个文件来告诉 ESLint忽略特定的文件和.eslintignore目录:

.eslintignore

.eslintignore

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

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

忽略模式的行为符合.gitignore规范。(不要忘记重新启动编辑器。)

回答by eppsilon

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-lineas 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.

类似于eslint-disable-line问题中提到的。如果您不想在重新启用时恢复复杂的规则配置,这可能是一种更好的方法。

回答by Aidin

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.

根据您要忽略的规则数量(全部或部分)以及禁用它的范围(行(s)、文件(s)、无处不在),我们有 2 × 3 = 6 种情况。



1) Disabling "All rules"

1)禁用“所有规则”

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

案例 1.1:您想为“一行或多行”禁用“所有规则”

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

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

案例 1.2:您想为“一个文件”禁用“所有规则”

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

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

案例 1.3:您想为“某些文件”禁用“所有规则”

There are 3 ways you can do this:

有 3 种方法可以做到这一点:

  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 paththat you want to be ignored. (e.g. apidoc/**)
  3. Alternatively, if you don't want to have a separate .eslintignorefile, you can add "eslintIgnore": ["file1.js", "file2.js"]in package.jsonas instructed here.
  1. 您可以使用 1.2 并/* eslint-disable */逐个添加文件。
  2. 您可以将文件名放在.eslintignore. 这很有效,特别是如果您有一条想要被忽略的路径。(例如apidoc/**
  3. 另外,如果你不希望有一个单独的.eslintignore文件,你可以添加 "eslintIgnore": ["file1.js", "file2.js"]package.json按照指示在这里


2) Disabling "Some Rules"

2)禁用“某些规则”

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

案例 2.1:您想为“一行或多行”禁用“某些规则”

You can put `/* eslint-disable-line quotes */` (replace `quotes` with your rules) at the end of the line(s), 
or `/* eslint-disable-next-line no-alert, quotes, semi */` before the line.

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

案例 2.2:您想为“一个文件”禁用“某些规则”

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"

## 案例 2.3:您想为“某些文件”禁用“某些规则”

This is less straight-forward. You should put them in "excludedFiles"object of "overrides"section of your .eslintrcas instructed here.

这不太直接。您应该按照此处的说明将它们放在您"excludedFiles""overrides"部分的对象中。.eslintrc

回答by Andriy

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.

最好在 .eslintrc.js 配置文件中添加“覆盖”。例如,如果您不想为所有以 Actions 结尾的 js 文件禁用驼峰规则,请在 .eslintrc.js 中的规则范围之后添加此代码。

"rules": {    
...........    
},
"overrides": [
 {
  "files": ["*Actions.js"],
     "rules": {
        "camelcase": "off"   
     }
 }
]

回答by samuel luswata

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

要暂时禁用文件中的规则警告,请使用以下格式的块注释:

/* eslint-disable */

This will disable ESLint until the

这将禁用 ESLint,直到

/* eslint-enable */

comment is reached.

评论已达成。

You can read more about this topic here.

您可以在此处阅读有关此主题的更多信息。

回答by sfletche

Accepted answer didn't work for me (maybe a different version of eslint...? I'm using eslint v3.19.0), but did find a solution with the following...

接受的答案对我不起作用(也许是不同版本的 eslint ......?我正在使用eslint v3.19.0),但确实找到了以下解决方案......

Place the following on the top of your file

将以下内容放在文件顶部

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

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

This will disable the rule for the entire file

这将禁用整个文件的规则

回答by Shakil

/*eslint-disable */

//suppress all warnings between comments
alert('foo');

/*eslint-enable */

it worked for me

它对我有用

回答by OZZIE

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

例如,您可以将其放在文件顶部:

/* eslint-disable no-console */