Eslint配置规则解析

前言

ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误。在许多方面,它和 JSLint、JSHint 相似,除了少数的例外:

  • ESLint 使用 Espree 解析 JavaScript。
  • ESLint 使用 AST 去分析代码中的模式 ESLint是完全插件化的。
  • 每一个规则都是一个插件并且你可以在运行时添加更多的规则。

在项目中使用了ESLint后,有些规则会让人抓破脑袋,但是我们有时会觉得某个规则完全没有意义,就想禁用某些没有意义或者你觉得很扯的规则,听起来有些自暴自弃,但ESLint其实是允许我们自定义规则的。

配置语法

把自定义规则加入到 rules 配置项中即可:

// 配置参数
rules: {
    "规则名1": [规则值, 规则配置],
    "规则名2": [规则值, 规则配置]
}

规则值(决定了是否开启/关闭):

"off"或者0    //关闭规则
"warn"或者1    //在打开的规则作为警告(不影响退出代码)
"error"或者2    //把规则作为一个错误(退出代码触发时为1)

规则名(每个规则对应一个规则名,就像前言提到的空格缩进规则对应 indent 一样):

中文文档

always or never

  • “never” (default) disallows space between the function name and the opening parenthesis.
  • “always” requires space between the function name and the opening parenthesis.
    Further, in “always” mode, a second object option is available that contains a single boolean allowNewlines property.
never

1、Examples of incorrect code for this rule with the default “never” option:

/*eslint func-call-spacing: ["error", "never"]*/

fn ();

fn
();

2、Examples of correct code for this rule with the default “never” option:

/*eslint func-call-spacing: ["error", "never"]*/

fn();
always

1、Examples of incorrect code for this rule with the “always” option:

/*eslint func-call-spacing: ["error", "always"]*/

fn();

fn
();

2、Examples of correct code for this rule with the “always” option:

/*eslint func-call-spacing: ["error", "always"]*/

fn ();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸尘️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值