BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News ESLint 6.7.0 Released

ESLint 6.7.0 Released

This item in japanese

Bookmarks

ESLint, a powerful JavaScript linting tool, recently released version 6.7.0. This release provides developers with a new Suggestions API to apply manual linting suggestions and a new ‘ignorePatterns’ configuration property to better control the files ESLint ignores.

In ESLint 6.7.0, if the ‘--fix’ command changes the runtime behaviour of the code or if there are multiple valid ways to fix a rule, then these cases can get addressed by providing an alternative ‘suggest’ option on ‘context.report()’. This option allows other tools such as IDEs and code editors to expose helpers for developers to manually apply a suggestion.

Developers can then embed suggestions with the 'suggest' key in the report argument with an array of suggestion objects. The suggestion objects represent individual suggestions that could get applied and require either a ‘desc’ key string that describes what applying the suggestion would do or a ‘messageId’ key, and a ‘fix’ key that is a function defining the suggestion result.

context.report({
    node: node,
    message: "Unnecessary escape character: \\{{character}}.",
    data: { character },
    suggest: [
        {
            desc: "Remove the `\\`. This maintains the current functionality.",
            fix: function(fixer) {
                return fixer.removeRange(range);
            }
        },
        {
            desc: "Replace the `\\` with `\\\\` to include the actual backslash character.",
            fix: function(fixer) {
                return fixer.insertTextBeforeRange(range, "\\");
            }
        }
    ]
});

Developers can now tell ESLint to ignore specific files and directories by adding the ‘ignorePatterns’ option in their configuration. Before the introduction of the ‘ignorePatterns’ property, the only way to tell ESLint to ignore specific files and directories was by creating an .eslintignore file in the project's root directory. The ‘ignorePatterns’ property affects only the directory that contains the configuration file. If provided, the .eslintignore file overrides the ‘ignorePatterns’ property of configuration files.

{
    "ignorePatterns": ["temp.js", "node_modules/"],
    "rules": {
        //...
    }
}

In recent versions of ESLint, developers observed that while returning a value from a setter does not produce an error, the returned value gets ignored. Therefore, returning a value from a setter is either unnecessary or a possible error, since the returned value cannot be used.

The ‘no-setter-return’ rule introduced in ESLint 6.7.0 disallows returning such values from setters and reports return statements in setter functions. This rule checks setters in object literals, class declarations and class expressions.

In ESLint, the rule ‘no-implicit-globals’  disallows declarations in the global scope. Global variables created from a script can produce name collisions with global variables created from another script, which will usually lead to runtime errors or unexpected behavior. This rule gets enhanced in ESLint 6.7.0 to set "lexicalBindings" to true if developers want this rule to check `const`, `let` and `class` declarations as well.

Examples of incorrect code for this rule with "lexicalBindings" option set to true:

/*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/

const foo = 1;

let baz;

class Bar {}

Example of correct code for this rule with the "lexicalBindings" option set to true:

/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

{
    const foo = 1;
    let baz;
    class Bar {}
}

(function() {
    const foo = 1;
    let baz;
    class Bar {}
}());

Introduced in ES2016, the infix exponentiation operator ** is an alternative for the standard `Math.pow` function. In ESLint 6.7.0, the rule ‘prefer-exponentiation-operator’ disallows the use of `Math.pow` in favor of the ** operator. This rule disallows calls to `Math.pow` and suggests using the ** operator instead.

Incorrect code with this rule:

/*eslint prefer-exponentiation-operator: "error"*/

const foo = Math.pow(2, 8);

const bar = Math.pow(a, b);

let baz = Math.pow(a + b, c + d);

let quux = Math.pow(-1, n);

Correct code with the exponentiation operator rule:

/*eslint prefer-exponentiation-operator: "error"*/

const foo = 2 ** 8;

const bar = a ** b;

let baz = (a + b) ** (c + d);

let quux = (-1) ** n;

Beyond adding new rules and enhancements to previous rules, the ESLint 6.7.0 release fixes several bugs found in the previous releases.To learn more about ESLint 6.7.0, refer to the ESLint 6.7.0 release.

ESLint is an OpenJS Foundation project and is available as open source software under the MIT license. Contributions are welcome via the ESLint GitHub repository. Contributors should follow the ESLint contribution guidelines.

Rate this Article

Adoption
Style

BT