Home > Back-end >  eslint/lint for not supported expressions and operators
eslint/lint for not supported expressions and operators

Time:12-29

How would u implement eslint config/rules to only detect JS not supported expressions and operators for a given Node.js version

Example

If the project is using Node.js v10 than should detect this types of errors

  • using optional chaining (?.)
  • using null coalescing operator
  • similar errors

Only js not supported expressions and operators should be detected.

CodePudding user response:

To implement an ESLint configuration that only detects JavaScript syntax and operators that are not supported by a specific version of Node.js, you can use the "node/no-unsupported-features" rule from the eslint-plugin-node package.

To use this rule, you will first need to install the eslint-plugin-node package:

npm install --save-dev eslint-plugin-node

Then, in your ESLint configuration file (e.g. .eslintrc.js), you can add the "node/no-unsupported-features" rule and specify the version of Node.js that you are targeting:

module.exports = {
  plugins: ["node"],
  rules: {
    "node/no-unsupported-features": ["error", { version: "10.0.0" }]
  }
};

This will enable the "node/no-unsupported-features" rule and configure it to only detect syntax and operators that are not supported in Node.js 10.0.0 or higher. If you want to target a different version of Node.js, you can simply update the version option to the desired version.

Note that this rule will not detect syntax and operators that are supported in newer versions of Node.js, but not in the version that you are targeting. For example, if you are targeting Node.js 10.0.0 and the code uses the optional chaining operator (?.), which is supported in Node.js 14.0.0 or higher, the "node/no-unsupported-features" rule will not detect an error. To catch these types of errors, you may want to consider using the "node/no-unsupported-features/es-syntax" rule, which detects syntax and operators that are not supported in any version of Node.js.

  • Related