Javascript 关闭特定行的 eslint 规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27732209/
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
Turning off eslint rule for a specific line
提问by runtimeZero
In order to turn off linting rule for a particular line in JSHint we use the following rule:
为了关闭 JSHint 中特定行的 linting 规则,我们使用以下规则:
/* jshint ignore:start*/
$scope.someVar = ConstructorFunction();
/* jshint ignore:end */
I have been trying to locate the equivalent of the above for eslint.
我一直在尝试为 eslint 找到与上述内容相同的内容。
回答by goofballLogic
You can use the single line syntax now:
您现在可以使用单行语法:
var thing = new Thing(); // eslint-disable-line no-use-before-define
thing.sayHello();
function Thing() {
this.sayHello = function() { console.log("hello"); };
}
Or if you don't want to have a comment on the same line with the actual code, it is possible to disable next line:
或者,如果您不想在实际代码的同一行上添加注释,则可以禁用下一行:
// eslint-disable-next-line no-use-before-define
var thing = new Thing();
Requested docs link: http://eslint.org/docs/user-guide/configuring.html#configuring-rules
请求的文档链接:http: //eslint.org/docs/user-guide/configuring.html#configuring-rules
回答by Nick Tomlin
You can use the following
您可以使用以下
/*eslint-disable */
//suppress all warnings between comments
alert('foo');
/*eslint-enable */
Which is slightly buried the "configuring rules" section of the docs;
这稍微隐藏了文档的“配置规则”部分;
To disable a warning for an entire file, you can include a comment at the top of the file e.g.
要禁用整个文件的警告,您可以在文件顶部添加注释,例如
/*eslint eqeqeq:0*/
Update
更新
ESlint has now been updated with a better way disable a single line, see @goofballLogic's excellent answer.
ESlint 现在已更新,以更好的方式禁用单行,请参阅@goofballLogic 的优秀答案。
回答by a darren
You can also disable a specific rule/rules(rather than all) by specifying them in the enable (open) and disable (close) blocks:
您还可以通过在启用(打开)和禁用(关闭)块中指定它们来禁用特定规则/规则(而不是全部):
/* eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/* eslint-enable no-alert */
via @goofballMagic's link above: http://eslint.org/docs/user-guide/configuring.html#configuring-rules
通过上面@goofballMagic 的链接:http://eslint.org/docs/user-guide/configuring.html#configuring-rules
回答by a darren
From Configuring ESLint - Disabling Rules with Inline Comments:
/* eslint-disable no-alert, no-console */
/* eslint-disable */
alert('foo');
/* eslint-enable */
/* eslint-disable no-alert, no-console */
alert('foo');
console.log('bar');
/* eslint-enable no-alert, no-console */
/* eslint-disable */
alert('foo');
/* eslint-disable no-alert */
alert('foo');
alert('foo'); // eslint-disable-line
// eslint-disable-next-line
alert('foo');
alert('foo'); // eslint-disable-line no-alert
// eslint-disable-next-line no-alert
alert('foo');
alert('foo'); // eslint-disable-line no-alert, quotes, semi
// eslint-disable-next-line no-alert, quotes, semi
alert('foo');
foo(); // eslint-disable-line example/rule-name
回答by Brylie Christopher Oxley
Answer
回答
You can use an inline comment: // eslint-disable-next-line rule-name.
您可以使用内联注释:// eslint-disable-next-line rule-name.
Example
例子
// eslint-disable-next-line no-console
console.log('eslint will ignore the no-console on this line of code');
Reference
参考
ESLint - Disabling Rules with Inline Comments
ESLint -禁用带有内联注释的规则
回答by LeeGee
The general end of line comment, // eslint-disable-line, does not need anything after it: no need to look up a code to specify what you wish ES Lint to ignore.
一般行尾注释 ,// eslint-disable-line后面不需要任何内容:无需查找代码来指定您希望 ES Lint 忽略的内容。
If you need to have any syntax ignored for any reason other than a quick debugging, you have problems: why not update your delint config?
如果您需要出于快速调试以外的任何原因忽略任何语法,那么您会遇到问题:为什么不更新您的 delint 配置?
I enjoy // eslint-disable-lineto allow me to insert consolefor a quick inspection of a service, without my development environment holding me back because of the breach of protocol. (I generally ban console, and use a logging class - which sometimes builds upon console.)
我喜欢// eslint-disable-line允许我插入console以快速检查服务,而我的开发环境不会因为违反协议而阻碍我。(我通常禁止console,并使用日志记录类 - 有时建立在console.)
回答by Raymond Wachaga
Or for multiple ignores on the next line, string the rules using commas
或者对于下一行的多个忽略,使用逗号将规则串起来
// eslint-disable-next-line class-methods-use-this, no-unused-vars
回答by Farhan Salam
To disable a single rule for the rest of the file below:
要为以下文件的其余部分禁用单个规则:
/* eslint no-undef: "off"*/
const uploadData = new FormData();
回答by Debbie Ellis
To disable all rules on a specific line:
要禁用特定行上的所有规则:
alert('foo'); // eslint-disable-line
回答by Sid
single line comment did not work for me inside a react dumb functional component, I have used file level disabling by adding /* eslint-disable insertEslintErrorDefinitionHere */
单行注释在反应愚蠢的功能组件中对我不起作用,我通过添加 /* eslint-disable insertEslintErrorDefinitionHere */ 来使用文件级禁用
(normally if you are using vs code and getting eslint error, you can click on the line which gives error and a bulb would show up in vs code, right click on the light bulb and choose any disable option and vs code will do it for you.)
(通常,如果您正在使用 vs 代码并收到 eslint 错误,您可以单击出现错误的行,vs 代码中会显示一个灯泡,右键单击灯泡并选择任何禁用选项,vs 代码将执行此操作你。)

