ESLint recently released the RC version of V8.0.0, which means an official release is imminent. V8.0.0 introduces a number of major updates that give you a head start on the features of ESLint v8.0.0.

The original link: eslint.docschina.org/docs/8.0.0/…

ESLint V8.0.0 is a major release of ESLint, with which we introduced some major updates. This guide is intended to help you understand these updates.

Most of the changes are listed in the following list, each ranked in order of expected user impact, with the first item having the most impact on users.

directory

Updates that affect users

  • Node.js v10, v13 and v15 are no longer supported
  • Remove core codecodeframetableFormatter for
  • comma-dangleThe rule pattern is more stringent
  • Fix unused disable directive
  • Updated theeslint:recommended

Affect plugin developers’ updates

  • Node.js v10, v13 and v15 are no longer supported
  • Set this parameter before providing modification suggestionsmeta.hasSuggestions
  • You need to set this parameter before providing the repair modemeta.fixable
  • Continue to useSourceCode#getComments()Will causeRuleTesterOn failure
  • Adjust the AST format of property abbreviations

Affect tooling integration developer updates

  • Node.js v10, v13 and v15 are no longer supported
  • removeCLIEngine
  • removelinterobject
  • remove/libThe entrance of the

Node.js v10, v13 and v15 are no longer supported

Node.js v10, V13, and V15 all ceased maintenance in 2020 or early 2021. ESLint officially drops support for these versions of Node.js starting with V8.0.0. ESLint now supports the following versions of Node.js:

  • Node.js 12.22 or later
  • Node.js version 14 or later
  • Node.js version 16 or later

Solution: Make sure you upgrade node.js to at least 12.22.0 when using ESLint V8.0.0. Another point to note is that you need to check the version of Node.js your editor is using when using ESLint through the editor. If the Node.js version cannot be upgraded for any reason, we recommend that you continue to use ESLint 7 before upgrading the Node.js version.

Related issue: #14023

removedcodeframetableFormatter for

ESLint V8.0.0 has removed codeFrame and table formatters from the core code. These formatters rely on packages that are not used elsewhere in ESLint, and removing them can reduce the size of ESLint and make it faster to install.

Solution: If you are using codeFrame or table formatters, you will need to install the ESlint-Formatter-CodeFrame or ESlint-Formatter-Table NPM packages after upgrading to V8.0.0 so that they work properly.

Related issue: #14277, #14316

comma-dangleThe rule pattern is more stringent

In ESLint V7.0.0, comma-Dangle can be configured without error as follows:

{
    "rules": {
        "comma-dangle": ["error"."never", { "arrays": "always"}}}]Copy the code

As configured, the rule reads only the second element, and the third element in the array is ignored. In ESLint V8.0.0, this configuration causes ESLint to throw an error.

Solution: Adjust your rule configuration so that the array has only two elements. The second element can be a string or an object, for example:

{
    "comma-dangle": ["error", "never"],
    // or
    "comma-dangle": ["error", {
        "arrays": "never",
        "objects": "never",
        "imports": "never",
        "exports": "never",
        "functions": "never"
    }]
}
Copy the code

Related issue: #13739

Fix unused disable directive

In ESLint V7.0.0, the use of –report-unused-disable-directives and –fix will only repair the rule on the command line, but retain the unused disable directive. In ESLint V8.0.0, this combination of command options causes unused disable directives to be deleted.

Solution: If both –report-unused-disable-directives and –fix are used on the command line and do not want the unused disable directive to be deleted, Please add the command line options – fix – type problem, the suggestion and layout.

Related issue: #11815

Updated theeslint:recommended

Four new rules are enabled in esLint :recommended preset.

Solution: Fix the bugs or disable these rules.

Related issue: #14673

Set this parameter before providing modification suggestionsmeta.hasSuggestions

In ESLint V7.0.0, rules provide advice without telling ESLint. In V8.0.0, rule suggestions require meta.hasSuggestions to be set to true. This tells ESLint that the rule provides advice. Without this property, the task’s suggestion behavior is reported as an error.

Workaround: If your custom rules provide suggestions, set meta-. hasSuggestions to true, for example:

module.exports = {
    meta: {
        hasSuggestions: true
    },
    create(context) {
        // your rule}};Copy the code

Using the eslint-plugin/require-meta-has-suggestions rule can automatically fix this problem and force the meta. HasSuggestions to be set correctly in your rules.

Related issue: #14312

You need to set this parameter before providing the repair modemeta.fixable

In ESLint V7.0.0, rules exported as functions can provide fixes. In ESLint V8.0.0, only rules exported as objects are allowed to provide fixes, and the meta.fixable property must be set to “code” or “whitespace”.

Solution: If your rule provides a fix and you write a function, for example:

module.exports = function(context) {
    // Your rules
};
Copy the code

Please rewrite your rules as follows:

module.exports = {
    meta: {
        fixable: "code" / / or "whitespace"
    },
    create(context) {
        // Your rules}};Copy the code

The eslint-plugin/require-meta-fixable rule can fix this problem automatically and force the correct meta. Fixable setting in your rules.

The eslint-plugin/prefer-object-rule rule can automatically fix the problem of exporting rules as functions by forcing deprecated function formats to be changed to object formats.

For more details on writing rules, see the rules documentation.

Related issue: #13349

Continue to useSourceCode#getComments()Will causeRuleTesterOn failure

In ESLint v4.0.0, we did away with SourceCode#getComments() and forgot to delete it. But instead of immediately removing it from v8.0.0, we took a transitional approach and updated RuleTester, which failed when SourceCode#getComments() was used in the rule. Thus, existing rules work fine, but when developers test the rules, the tests fail.

The SourceCode#getComments() method will be removed completely in v9.0.0.

Solution: If your rule uses SourceCode#getComments(), use SourceCode#getCommentsBefore(), SourceCode#getCommentsAfter() or SourceCode#getCommentsInside() instead.

Related issue: #14744

Adjust the AST format of property abbreviations

ESLint V8.0.0 also upgraded Espree to V8.0.0 to support the new syntax. The update to Espree, in turn, involved the Acorn V8.0.0 update, and the Acron update changed the way properties are abbreviated in the AST, as shown in the following example:

const version = 8;
const x = {
    version
};
Copy the code

This code generates an attribute node of the following form:

{
    "type": "Property"."method": false."shorthand": true."computed": false."key": {
        "type": "Identifier"."name": "version"
    },
    "kind": "init"."value": {
        "type": "Identifier"."name": "version"}}Copy the code

Note that both the key and value attributes contain the same information, and before Acorn V8.0.0 (or ESLint v8.0.0), these two nodes would point to the same object, so you can use === to determine if they represent the same node, for example:

// True in ESLint v7.x, false in ESLint v8.0.0
if (propertyNode.key === propertyNode.value) {
    // do something
}
Copy the code

In ESLint V8.0.0 (based on Acorn V8.0.0), key and value now refer to two separate objects and are therefore no longer equal.

Solution: If your rule needs to determine whether the key and value of object shorthand property literals are on the same node, you need to rewrite your code in either of the following ways:

  1. usepropertyNode.shorthandTo determine whether the property is a shorthand property.
  2. Use each node’srangeProperty to determine whether key and value occupy the same location.

Related issue: #14591

removeCLIEngine

The CLIEngine class has been removed and replaced by the ESLint class.

Solution: If you use CLIEngine in your code, update your code to use the new ESLint class. The following table shows how the existing CLIEngine methods correspond to ESLint methods:

CLIEngine ESLint
executeOnFiles(patterns) lintFiles(patterns)
executeOnText(text, filePath, warnIgnored) lintText(text, options)
getFormatter(name) loadFormatter(name)
getConfigForFile(filePath) calculateConfigForFile(filePath)
isPathIgnored(filePath) isPathIgnored(filePath)
static outputFixes(results) static outputFixes(results)
static getErrorResults(results) static getErrorResults(results)
static getFormatter(name) (removed does 1)
addPlugin(pluginId, definition) the plugins constructor option
getRules() (removed does 2)
resolveFileGlobPatterns() (removed does 3)
  • Does 1engine.getFormatter()Method currently returns the loaded package object as is, which makes it difficult to add new functionality to formatter for compatibility reasons. The neweslint.loadFormatter()Method returns an adapter object containing the loaded package object to simplify the process of adding new features. In addition, adapter objects can be accessedESLintExample to calculate default data (for example, using loaded plug-in rulesrulesMeta). As a result,ESLintClass only implementsloadFormatter()Method instance version.
  • Does 2 due toCLIEngine#getRules()The method had side effects and has been removed. If you need to useCLIEngine#getRules()To retrieve meta information based on linting result rules, use theESLint#getRulesMetaForResults()Instead. If you need to useCLIEngine#getRules()To retrieve all built-in rules fromeslint/use-at-your-own-riskThe introduction ofbuiltinRulesThis is an unsupported API, but you can access internal rules through it.
  • ※3 Since ESLint v6.0.0, ESLint uses a different version ofresolveFileGlobPatterns()Method to iterate over the file, eventually making the method obsolete.

Related issue: RFC80, #14716, #13654

removelinterobject

In V8.0.0, deprecated Linter objects have been removed from the ESLint package.

Solution: If you use linter objects, for example:

const { linter } = require("eslint");
Copy the code

Please change the code to:

const { Linter } = require("eslint");
const linter = new Linter();
Copy the code

Related issue: RFC80, #14716, #13654

remove/libThe entrance of the

Since V8.0.0, ESLint has strictly defined access to its public API. Previously, you could access individual files such as require(“eslint/lib/rules/semi”), this will no longer be allowed. For compatibility reasons, there are a few apis that can now be accessed through the /use-at-your-own-risk entry. But these apis aren’t officially supported, and things can get confusing.

Solution: If you access a rule directly through the /lib entry, for example:

const rule = require("eslint/lib/rules/semi");
Copy the code

Please change the code to:

const { builtinRules } = require("eslint/use-at-your-own-risk");
const rule = builtinRules.get("semi");
Copy the code

If you access FileEnumerator directly through the /lib entry, for example:

const { FileEnumerator } = require("eslint/lib/cli-engine/file-enumerator");
Copy the code

Please change the code to:

const { FileEnumerator } = require("eslint/use-at-your-own-risk");
Copy the code

Related issue: RFC80, #14716, #13654