迁移到 v1.0.0
ESLint v1.0.0 是第一个主要版本发布。因此,ESLint 在 0.x 版本中的工作方式与未来版本的工作方式之间存在一些重大变化。这些变化是直接来自于 ESLint 用户社区的反馈,并且在充分考虑了升级路径后才做出的。我们相信这些变化使 ESLint 变得更好,虽然升级需要做一些工作,但我们希望这次升级的痛苦足够小,以至于您会看到升级的好处。
所有规则默认关闭
v1.0.0 中最重要的区别是所有规则默认都是关闭的。在收到许多允许从配置文件中关闭默认规则的请求后,我们做出了这一更改。虽然在技术上不可行,但让所有规则默认关闭,然后使用 extends
在配置文件中重新启用规则是可行的。因此,我们已将 --reset
行为设为默认行为,并移除了此命令行选项。
当使用 --init
时,您的配置文件将自动包含以下行
{
"extends": "eslint:recommended"
}
此设置模仿了 0.x 版本中的一些默认行为,但并非全部。如果您不想使用任何推荐的规则,您可以删除此行。
需要处理: 如果您当前正在使用 --reset
,那么您应该停止在命令行上传递 --reset
;不需要其他更改。如果您没有使用 --reset
,那么您应该检查您的配置,以确定哪些规则应该默认开启。您可以通过在配置文件中添加以下内容来部分恢复一些默认行为
"eslint:recommended"
配置包含许多与 0.x 版本相同的默认规则设置,但并非全部。这些规则不再是默认开启的,因此您应该检查您的设置以确保它们仍然符合您的预期
- no-alert
- no-array-constructor
- no-caller
- no-catch-shadow
- no-empty-label
- no-eval
- no-extend-native
- no-extra-bind
- no-extra-strict
- no-implied-eval
- no-iterator
- no-label-var
- no-labels
- no-lone-blocks
- no-loop-func
- no-multi-spaces
- no-multi-str
- no-native-reassign
- no-new
- no-new-func
- no-new-object
- no-new-wrappers
- no-octal-escape
- no-process-exit
- no-proto
- no-return-assign
- no-script-url
- no-sequences
- no-shadow
- no-shadow-restricted-names
- no-spaced-func
- no-trailing-spaces
- no-undef-init
- no-underscore-dangle
- no-unused-expressions
- no-use-before-define
- no-with
- no-wrap-func
- camelcase
- comma-spacing
- consistent-return
- curly
- dot-notation
- eol-last
- eqeqeq
- key-spacing
- new-cap
- new-parens
- quotes
- semi
- semi-spacing
- space-infix-ops
- space-return-throw-case
- space-unary-ops
- strict
- yoda
另请参阅:完整差异对比,其中默认值已更改。
这是一个配置文件的示例,它最接近旧的默认设置
{
"extends": "eslint:recommended",
"rules": {
"no-alert": 2,
"no-array-constructor": 2,
"no-caller": 2,
"no-catch-shadow": 2,
"no-empty-label": 2,
"no-eval": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-implied-eval": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-new-wrappers": 2,
"no-octal-escape": 2,
"no-process-exit": 2,
"no-proto": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-sequences": 2,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-trailing-spaces": 2,
"no-undef-init": 2,
"no-underscore-dangle": 2,
"no-unused-expressions": 2,
"no-use-before-define": 2,
"no-with": 2,
"camelcase": 2,
"comma-spacing": 2,
"consistent-return": 2,
"curly": [2, "all"],
"dot-notation": [2, { "allowKeywords": true }],
"eol-last": 2,
"no-extra-parens": [2, "functions"],
"eqeqeq": 2,
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"new-cap": 2,
"new-parens": 2,
"quotes": [2, "double"],
"semi": 2,
"semi-spacing": [2, {"before": false, "after": true}],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"strict": [2, "function"],
"yoda": [2, "never"]
}
}
已移除的规则
在过去的几个版本中,我们一直在弃用规则并引入新规则来替代它们。以下是已移除规则及其替换规则的列表
- generator-star 已被 generator-star-spacing 替换
- global-strict 已被 strict 替换
- no-comma-dangle 已被 comma-dangle 替换
- no-empty-class 已被 no-empty-character-class 替换
- no-extra-strict 已被 strict 替换
- no-reserved-keys 已被 quote-props 替换
- no-space-before-semi 已被 semi-spacing 替换
- no-wrap-func 已被 no-extra-parens 替换
- space-after-function-name 已被 space-before-function-paren 替换
- space-before-function-parentheses 已被 space-before-function-paren 替换
- space-in-brackets 已被object-curly-spacing 和 array-bracket-spacing 替换
- space-unary-word-ops 已被 space-unary-ops 替换
- spaced-line-comment 已被 spaced-comment 替换
需要处理: 您需要更新您的规则配置以使用新规则。当您使用已被移除的规则时,ESLint v1.0.0 也会发出警告,并会建议替换规则。希望这将在升级过程中减少意外情况。
列号从 1 开始
从一开始,ESLint 就使用从 0 开始的列号报告错误,因为这是 Esprima,以及后来的 Espree 所报告的。然而,大多数工具和编辑器都使用从 1 开始的列号,这使得与 ESLint 的集成变得有些棘手。在 v1.0.0 中,我们已切换为使用从 1 开始的列号报告错误,以与开发人员日常使用的工具保持一致。
需要处理: 如果您创建了编辑器集成,或者一个必须更正列号的工具,您将需要更新为仅传递来自 ESLint 的列号。否则,无需更改。
不再导出 cli
在 0.x 版本中,导出了 cli
对象供外部工具使用。后来它被弃用,转而使用 CLIEngine
。在 v1.0.0 中,我们不再导出 cli
,因为它不应被外部工具使用。这将破坏现有使用它的工具。
需要处理: 如果您正在使用导出的 cli
对象,请切换为使用 CLIEngine
。
弃用 eslint-tester
eslint-tester
模块长期以来一直是 ESLint 规则的主要测试工具,现在已移至 eslint
模块中。这是这两个模块之间关系困难的结果,它们之间产生了循环依赖,并在规则测试中造成了很多问题。将测试工具移至 eslint
模块中解决了许多这些问题。
eslint-tester
的替代品称为 RuleTester
。它是 ESLintTester
的简化版本,旨在与任何测试框架一起使用。此对象由包公开。
需要处理: 将您的所有规则测试转换为使用 RuleTester
。如果您有使用 ESLintTester
的测试,如下所示
var eslint = require("../../../lib/eslint"),
ESLintTester = require("eslint-tester");
var eslintTester = new ESLintTester(eslint);
eslintTester.addRuleTest("lib/rules/your-rule", {
valid: [],
invalid: []
});
那么您可以更改为
var rule = require("../../../lib/rules/your-rule"),
RuleTester = require("eslint").RuleTester;
var ruleTester = new RuleTester();
ruleTester.run("your-rule", rule, {
valid: [],
invalid: []
});