Migrating to v1.0.0

Migrating to 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 被 stric 代替

  • no-comma-dangle 被 comma-dangle 代替

  • no-empty-class 被 no-empty-character-class 代替

  • no-extra-strict 被 strict 代替

  • no-extra-strict 被 strict 代替

  • 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: [] }