eslint 迁移 v6 指南 翻译

2019-07-11  本文已影响0人  大鲨鱼麻吉

前言

ESLint v6.0.0是ESLint的主要版本,此版本中做了一些重大更改。

下面的列表大致按每个更改预计会影响的用户数量排序,其中第一个项目预计会影响大多数用户

对用户来说的重大变化

  1. 不再支持Node.js 6
  2. eslint:recommended 已更新
  3. 插件和可共享配置不再受ESLint位置的影响
  4. 默认解析器现在更严格地验证选项
  5. 默认情况下,no-redeclare 规则现在更加严格
  6. 默认情况下,comma-dangle 规则现在更加严格
  7. 默认情况下,no-confusing-arrow 规则现在更宽松
  8. 配置文件中的重写 现在可以匹配dotfiles
  9. 现在,父配置文件可以重写 扩展配置文件中的重写规则
  10. 现在验证全局变量的配置值
  11. 已弃用的experimentalObjectRestSpread选项已被删除
  12. 使用unicode 解析 规则选项 中用户提供的正则表达式

对自定义插件的开发人员的重大变化

  1. 插件作者可能需要更新安装说明
  2. RuleTester 现在验证规则列表 中的无效默认关键字
  3. RuleTester现在需要 parser 选项的绝对路径
  4. 已删除eslintExplicitGlobalComment范围分析属性

对集成开发者的 重大变化

  1. 插件和可共享配置不再受ESLint位置的影响
  2. Linter不再尝试从文件系统加载丢失的解析器

变化详述

1. 不再支持Node.js 6

截至2019年4月,Node.js 6将在EOL,将不再接收安全更新。因此,我们决定在ESLint v6中放弃对它的支持。我们现在支持以下版本的Node.js:

  • Node.js 8 (8.10.0 and above)
  • Node.js 10 (10.13.0 and above)
  • Anything above Node.js 11.10.1

使用ESLint v6时,请确保至少升级到Node.js 8。如果您无法升级,我们建议您继续使用ESLint v5.x,直到您能够升级Node.js.

2. eslint:recommended 已更新

以下规则已添加到eslint:recommended config:

  • no-async-promise-executor不允许使用异步函数作为Promise构造函数的参数,这通常是一个bug。
  • no-misleading-character-class报告正则表达式中可能无法按预期运行的字符类。
  • no-prototype-builtins定义方法调用,如foo.hasOwnProperty(“bar”)(这是bug的常见来源),并建议将它们替换为Object.prototype.hasOwnProperty.call(foo,“bar”)。
  • no-shadow-restricted-names 不允许使用未定义的阴影变量(例如使用let undefined = 5等代码),因为它可能会使读者感到困惑.
  • no-useless-catch 报告catch子句是多余的,可以从代码中删除而不改变其行为.
  • no-with禁止使用with语句,这会使代码难以理解并导致兼容性问题。
  • require-atomic-updates报告在异步函数中重新分配变量时可能发生的竞争条件错误

此外,以下规则已从eslint:recommended中删除

  • no-console 不允许调用console.log之类的函数。虽然此规则在许多情况下很有用(例如,为了避免无意中将调试语句留在生产代码中),但它并不像eslint:recommended中的其他规则那样广泛适用,并且在console.log的情况下它是误报的来源是可以接受的(例如在CLI应用程序中)

最后,在ESLint v5 eslint中:eslint:recommended将明确禁用所有未被视为“recommended”的核心规则。如果eslint:recommended在另一个配置之后被加载,这可能会导致令人困惑,因为eslint:recommended会产生关闭某些规则的效果。在ESLint v6中,eslint:recommended对非推荐规则没有影响。

要模仿eslint:5.x中推荐的行为,您可以在配置文件中显式禁用/启用规则,如下所示:

   {
  "extends": "eslint:recommended",

  "rules": {
    "no-async-promise-executor": "off",
    "no-misleading-character-class": "off",
    "no-prototype-builtins": "off",
    "no-shadow-restricted-names": "off",
    "no-useless-catch": "off",
    "no-with": "off",
    "require-atomic-updates": "off",
    "no-console": "error"
         }
    }

在极少数情况下(如果您依赖以前eslint:recommended禁用核心规则),您可能需要禁用这些规则来恢复以前的行为。

3. 插件和可共享配置不再受ESLint位置的影响

以前,ESLint相对于ESLint包本身的位置加载了插件。因此,我们建议使用全局ESLint安装的用户也应该全局安装插件,而使用本地ESLint安装的用户应该在本地安装插件。但是,由于设计错误,此策略导致ESLint在某些情况下随机无法加载插件和可共享配置,尤其是在使用lerna和Yarn Plug n'Play等包管理工具时。

据经验:使用ESLint v6,即使全局安装了ESLint,也应始终在本地安装插件。更确切地说,ESLint v6默认解析相对于最终用户项目的插件,并且总是相对于导入它们的配置文件的位置解析可共享配置和解析器。

如果您使用ESLint的全局安装(例如,使用npm install eslint --global安装)以及插件,则应在运行ESLint的项目中本地安装这些插件。如果您的配置文件扩展了可共享的配置和/或解析器,则应确保将这些包安装为包含配置文件的项目的依赖项。

如果您使用位于本地项目之外的配置文件(带有--config标志),请考虑将插件安装为该配置文件的依赖项,并将--resolve-plugins-relative-to标志设置为配置文件的location

4. 默认解析器现在更严格地验证选项

espree是ESLint使用的默认解析器,现在会在以下情况下引发错误:

  • ecmaVersion解析器选项设置为数字以外的其他选项,例如字符串“2015”。 (以前,会忽略非数字选项。)
  • The sourceType: "module" parser option is set while ecmaVersion is set to 5 or left unspecified. (Previously, setting sourceType: "module" would implicitly cause ecmaVersion to be set to a minimum of 2015, which could be surprising.)
  • sourceType设置为“script”或“module”以外的任何内容。
  • 如果您的配置将ecmaVersion设置为数字以外的其他内容,则可以通过删除ecmaVersion来恢复以前的行为。 (但是,您可能需要仔细检查您的配置是否实际按预期工作。)如果您的配置设置了parserOptions:{sourceType:“module”}而没有设置parserOptions.ecmaVersion,则应添加parserOptions:{ecmaVersion:2015}恢复以前的行为。

5. 默认情况下,no-redeclare 规则现在更加严格

no-redeclare规则的默认选项已从{builtinGlobals:false}更改为{builtinGlobals:true}。此外,no-redeclare规则现在将报告由/ * global foo * /等注释启用的全局变量的错误,如果这些全局变量已经通过配置启用了。

要恢复规则的先前选项,可以按如下方式进行配置:

      {
      "rules": {
         "no-redeclare": ["error", { "builtinGlobals": false }]
               }
      }

此外,如果您在代码中看到全局注释的新错误,则应删除这些注释。

6. 默认情况下,comma-dangle 规则现在更加严格

以前,除非显式配置为检查函数逗号,否则逗号悬挂规则将忽略尾随函数参数和参数。在ESLint v6中,函数逗号的处理方式与其他类型的尾随逗号相同。

You can restore the previous default behavior of the rule with:

      {
  "rules": {
    "comma-dangle": ["error", {
        "arrays": "never",
        "objects": "never",
        "imports": "never",
        "exports": "never",
        "functions": "ignore"
                }]
            }
      }

要恢复字符串选项的先前行为,例如“always-multiline”,请在上面的示例中将“never”替换为“always-multiline”

7. 默认情况下,no-confusing-arrow 规则现在更宽松

no-confusing-arrow规则的默认选项已从{allowParens:false}更改为{allowParens:true}。
You can restore the previous default behavior of the rule with:

      {
      "rules": {
         "no-confusing-arrow": ["error", { "allowParens": false }]
                }
      }

8. 配置文件中的重写 现在可以匹配dotfiles

  • Due to a bug, the glob patterns in a files list in an overrides section of a config file would never match dotfiles, making it impossible to have overrides apply to files starting with a dot. This bug has been fixed in ESLint v6.
  • If you don’t want dotfiles to be matched by an override, consider adding something like excludedFiles: [".*"] to that overrides section. See the documentation for more details.

9. 现在,父配置文件可以重写 扩展配置文件中的重写规则

由于存在错误,以前的情况是,可共享配置中的覆盖块优先于父配置的顶级。例如,使用以下配置设置,即使在最终用户的配置中明确禁用了the semi rule,也会最终启用the semi rule

      // .eslintrc.js
      module.exports = {
         extends: ["foo"],
         rules: {
            semi: "off"
               }
         };

      // eslint-config-foo/index.js
      module.exports = {
         overrides: {
            files: ["*.js"],
            rules: {
               semi: "error"
            }
         }
     };
  • In ESLint v6.0.0, a parent config always has precedence over extended configs, even with overrides blocks.
  • 我们预计此问题的影响非常低,因为大多数可共享配置不使用overrides 块。但是,如果您使用带有overrides 块的可共享配置,则由于在配置中明确指定但在此之前处于非活动状态的某些内容,您可能会遇到行为更改。如果您希望从可共享配置继承行为,只需从您自己的配置中删除相应的条目。 (在上面的示例中,可以通过从.eslintrc.js中删除semi:“off”来恢复先前的行为。)

10. 现在验证全局变量的配置值

以前,在使用对象配置一组全局变量时,可以使用任何内容作为对象的值。未知值将被视为“可写”。

   // .eslintrc.js
   module.exports = {
   globals: {
      foo: "readonly",
      bar: "writable",
      baz: "hello!" // ???
     }
   };

通过此更改,globals对象中的任何未知值都会导致配置验证错误。
如果您在更新后看到与全局变量相关的配置验证错误,请确保为全局变量配置的所有值都是readonly,writeable或off。 (ESLint还接受一些替代拼写和变体的兼容性。)

11. 已弃用的experimentalObjectRestSpread选项已被删除

以前,在使用默认解析器时,配置可以使用experimentalObjectRestSpread选项来启用对象rest / spread属性的解析支持:

   {
   "parserOptions": {
      "ecmaFeatures": {
         "experimentalObjectRestSpread": true
      }
       }
   }

从ESLint v5开始,ecmaFeatures:{experimentalObjectRestSpread:true}已经等同于ecmaVersion:2018,并且还发出了弃用警告。在ESLint v6中,experimentalObjectRestSpread功能已完全删除且无效。如果您的配置依赖于experimentalObjectRestSpread来启用ES2018解析,您可能会开始看到解析最近语法的错误。

如果您使用experimentalObjectRestSpread选项,则应更改配置以包含此选项

   {
   "parserOptions": {
      "ecmaVersion": 2018
      }
   }

12. 使用unicode 解析 规则选项 中用户提供的正则表达式

max-len之类的规则接受一个字符串选项,该选项被解释为正则表达式。在ESLint v6.0.0中,这些正则表达式使用unicode标志进行解释,当匹配像星体符号这样的字符时,它应该表现出更合理的行为。 Unicode正则表达式还比非unicode正则表达式更严格地验证转义序列。

如果升级后出现规则选项验证错误,请确保规则选项中的任何正则表达式都没有无效的转义序列。

13. 插件作者可能需要更新安装说明

如果您维护插件并提供安装说明,则应确保安装说明是最新的,其中包含有关插件加载方式的用户更改。特别是,如果您的插件是使用generator-eslint包生成的,那么它可能包含有关如何将插件与全局ESLint安装一起使用的过时说明。

14. RuleTester现在验证规则模式中的无效默认关键字

某些情况下,规则架构可以使用default关键字自动指定规则选项的默认值。但是,default关键字仅在某些架构位置有效,并在其他位置被忽略,如果规则错误地预期将默认值作为规则选项提供,则会产生错误风险。但是,default关键字仅在某些架构位置有效,并在其他位置被忽略,如果规则错误地预期将默认值作为规则选项提供,则会产生错误风险。
如果RuleTester开始报告有关无效默认值的错误,则可以删除规则架构中指定位置的默认属性,并且该规则的行为方式相同。 (如果发生这种情况,您可能还希望在该位置未提供选项值时验证规则是否正常运行。)。

15. RuleTester现在需要 parser 选项的绝对路径

To use custom parsers in tests, we could use parser property with a package name or file path. However, if a package name was given, it’s unclear where the tester should load the parser package from because the tester doesn’t know which files are running the tester. In ESLint v6.0.0, RuleTester disallows parser property with a package name.
如果在测试用例中使用包名称的parser属性,请使用require.resolve()函数更新它,以将包名称解析为包的绝对路径。

16. 已删除eslintExplicitGlobalComment范围分析属性

Previously, ESLint would add an eslintExplicitGlobalComment property to Variable objects in scope analysis to indicate that a variable was introduced as a result of a /* global / comment. This property was undocumented, and the ESLint team was unable to find any usage of the property outside of ESLint core. The property has been removed in ESLint v6, and replaced with the eslintExplicitGlobalComments property, which can contain a list of all / global */ comments if a variable was declared with more than one of them.
If you maintain a rule that uses the eslintExplicitGlobalComment property, update it to use the eslintExplicitGlobalComments property as a list instead.

17. Linter不再尝试从文件系统加载丢失的解析器

Previously, when linting code with a parser that had not been previously defined, the Linter API would attempt to load the parser from the filesystem. However, this behavior was confusing because Linter never access the filesystem in any other cases, and it was difficult to ensure that the correct parser would be found when loading the parser from the filesystem.
In ESLint v6, Linter will no longer perform any filesystem operations, including loading parsers.
If you’re using Linter with a custom parser, use Linter#defineParser to explicitly define the parser before linting any code.

上一篇下一篇

猜你喜欢

热点阅读