关闭特定文件的eslint规则

这篇博客讨论了在JavaScript项目中如何针对特定文件关闭eslint规则,避免在整个项目或文件中全局禁用规则。提供了在文件顶部添加注释、使用指令以及创建.eslintignore文件来忽略某些文件的方法。
摘要由CSDN通过智能技术生成

本文翻译自:Turning off eslint rule for a specific file

Is it possible to turn off the eslint rule for the whole file? 是否可以关闭整个文件的eslint规则? Something such as: 像这样的东西:

// 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。)它经常发生在我身上,在某个文件中,我打破了许多地方的特定规则,对于该文件被认为是正常的,但我不想禁用整个项目的规则,我也不想禁用该特定文件的其他规则。


#1楼

参考:https://stackoom.com/question/2LrmJ/关闭特定文件的eslint规则


#2楼

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: http://eslint.org/docs/user-guide/configuring.html#configuring-rules 更多信息: http//eslint.org/docs/user-guide/configuring.html#configuring-rules


#3楼

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-line as mentioned in the question. 与问题中提到的eslint-disable-line类似。 It might be a better method if you don't want to have to restore a complicated rule configuration when re-enabling it. 如果您不希望在重新启用它时还原复杂的规则配置,那么这可能是更好的方法。


#4楼

/*eslint-disable */

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

/*eslint-enable */

it worked for me 它对我有用


#5楼

Just place /* eslint-disable */ at the top of the file 只需将/* eslint-disable */放在文件的顶部即可

Check: https://evanhahn.com/disable-eslint-for-a-file/ 检查: https//evanhahn.com/disable-eslint-for-a-file/


#6楼

You can tell ESLint to ignore specific files and directories by creating an .eslintignore file in your project's root directory: 您可以通过在项目的根目录中创建.eslintignore文件来告诉ESLint 忽略特定的文件和目录:

.eslintignore .eslintignore

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

The ignore patterns behave according to the .gitignore specification. 忽略模式的行为符合.gitignore规范。 (Don't forget to restart your editor.) (别忘了重新启动编辑器。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值