简体   繁体   English

关闭特定行的 eslint 规则

[英]Turning off eslint rule for a specific line

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 找到上面的等价物。

To disable next line:要禁用下一行:

// eslint-disable-next-line no-use-before-define
var thing = new Thing();

Or use the single line syntax:或者使用单行语法:

var thing = new Thing(); // eslint-disable-line no-use-before-define

See the eslint docs查看eslint 文档

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 eg要禁用整个文件的警告,您可以在文件顶部添加注释,例如

/*eslint eqeqeq:0*/

Update更新

ESlint has now been updated with a better way disable a single line, see @goofballLogic's excellent answer . ESlint 现在已更新,以更好的方式禁用单行,请参阅@goofballLogic 的优秀答案

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

From Configuring ESLint - Disabling Rules with Inline Comments :配置 ESLint - 使用内联注释禁用规则

/* 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

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 - 禁用带有内联注释的规则

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-line to allow me to insert console for a quick inspection of a service, without my development environment holding me back because of the breach of protocol.我喜欢// eslint-disable-line允许我插入console以快速检查服务,而我的开发环境不会因为违反协议而阻碍我。 (I generally ban console , and use a logging class - which sometimes builds upon console .) (我通常禁止console ,并使用日志类 - 有时建立在console之上。)

或者对于下一行的多个忽略,使用逗号将规则串起来

// eslint-disable-next-line class-methods-use-this, no-unused-vars

To disable a single rule for the rest of the file below:要为以下文件的其余部分禁用单个规则:

/* eslint no-undef: "off"*/
const uploadData = new FormData();

要禁用特定行上的所有规则:

alert('foo'); // eslint-disable-line

My answer, similar to others given, but shows how you can also add a comment to yourself on the same line.我的回答与其他人给出的答案类似,但显示了您还可以如何在同一行上为自己添加评论。

// eslint-disable-line // THIS WON"T WORK

Use -- if you also need to write a comment on that line (eg. maybe why eslint is disabled)使用--如果您还需要在该行上写评论(例如,为什么 eslint 被禁用)

// eslint-disable-line -- comment to self (This DOES work)

Can be used in conjunction with specific eslint rules to ignore:可以与特定的 eslint 规则结合使用来忽略:

// eslint-disable-line no-console -- comment to self (This Also Works!)

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 代码将执行此操作你。)

To disable all rules on a specific line:要禁用特定行上的所有规则:

// @ts-ignore
$scope.someVar = ConstructorFunction();

您可以将错误的文件添加到项目中的 .eslintignore 文件中。对于所有 .vue 文件,只需添加 /*.vue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM