关闭特定行的 eslint 规则

问题描述 投票:0回答:13

为了关闭 JSHint 中特定行的 linting 规则,我们使用以下规则:

/* jshint ignore:start*/
$scope.someVar = ConstructorFunction();
/* jshint ignore:end */

我一直在尝试为 eslint 找到上面的等价物。

javascript jshint eslint
13个回答
2848
投票

要禁用下一行:

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

或者使用单行语法:

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

查看 eslint 文档


感谢KhalilRavanna评论

如果您不关心特异性,您可以执行 //eslint-disable-line 并且它似乎禁用给定行的所有规则

var thing = new Thing() // eslint-disable-line

648
投票

更新

ESlint 现已更新为禁用单行的更好方法,请参阅@goofballLogic 的优秀答案

旧答案:

您可以使用以下内容

/*eslint-disable */

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

/*eslint-enable */

略微隐藏在 docs 的“配置规则”部分;


禁用

specific warning
例如
eqeqeq
对于整个文件,您可以在文件顶部添加注释

/*eslint eqeqeq:0*/

当要忽略多条规则时,将它们列在 1 行上,用空格分隔

/* eslint jsx-a11y/alt-text:0 react/jsx-no-duplicate-props:0 */

264
投票

您还可以通过在启用(打开)和禁用(关闭)块中指定它们来禁用特定规则(而不是全部):

/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert */

通过@goofballMagic 上面的链接:http://eslint.org/docs/user-guide/configuring.html#configuring-rules


129
投票

来自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

86
投票

回答

您可以使用内联评论:

// eslint-disable-next-line rule-name
.

例子

// eslint-disable-next-line no-console
console.log('eslint will ignore the no-console on this line of code');

参考

ESLint - 使用内联评论禁用规则


33
投票

一般的行尾注释,

// eslint-disable-line
,后面不需要任何东西:不需要查找代码来指定您希望 ES Lint 忽略的内容。

如果您出于除快速调试之外的任何原因需要忽略任何语法,您会遇到问题:为什么不更新您的 delint 配置?

我喜欢

// eslint-disable-line
允许我插入
console
以快速检查服务,而我的开发环境不会因为违反协议而阻碍我。 (我通常禁止
console
,并使用日志记录类 - 有时建立在
console
之上。)


20
投票

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

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

13
投票

要为以下文件的其余部分禁用单个规则:

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

13
投票

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

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

10
投票

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

如果您还需要在该行上写评论(例如,为什么 eslint 被禁用),请使用

--

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

可以结合特定的eslint规则来忽略:

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

1
投票

单行注释在反应哑功能组件中对我不起作用,我通过添加禁用文件级别 /* eslint-disable insertEslintErrorDefinitionHere */

(通常,如果您使用 vs code 并遇到 eslint 错误,您可以单击出现错误的行,然后 vs code 中会出现一个灯泡,右键单击灯泡并选择任何禁用选项,vs code 将执行此操作给你。)


0
投票

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

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

-8
投票

您可以在项目中添加给 .eslintignore 文件带来错误的文件。就像所有 .vue 文件一样,只需添加 /*.vue

© www.soinside.com 2019 - 2024. All rights reserved.