简体   繁体   中英

What the difference between env.es6=true and parserOptions.ecmaVersion=6 at .eslintrc.js?

I do not understand why do I need to specify the same information in two different parameters.

module.exports = {
  env: {
    commonjs: true,
    es6: true,
    node: true
  },
  extends: [
    'eslint:recommended'
  ],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly'
  },
  parserOptions: {
    ecmaVersion: 6
  },
  rules: {
    indent: ['error', 2],
    quotes: ['error', 'single'],
    semi: ['error', 'always']
  }
};

ecmaVersion option of parserOptions is for syntax. On the otherhand, env option is for global variables.

If you want to use a Promise for example, "ecmaVersion:2020" is not enough. You also have to specify which env to use.

Note that env option automatically enables new syntax. But personally I recommend to set both of them properly.

For more information, see here

For ES6 syntax, use { "parserOptions": { "ecmaVersion": 6 } }; for new ES6 global variables, use { "env": { "es6": true } }. { "env": { "es6": true } } enables ES6 syntax automatically, but { "parserOptions": { "ecmaVersion": 6 } } does not enable ES6 globals automatically

By this https://eslint.org/docs/latest/use/configure/language-options#specifying-environments it is enough to set just env which automatically set correct parserOptions.ecmaVersion . So you should prefer this way to avoid mismatch configuration.

es6 - enable all ECMAScript 6 features except for modules (this automatically sets the ecmaVersion parser option to 6).
es2016 - adds all ECMAScript 2016 globals and automatically sets the ecmaVersion parser option to 7.
es2017 - adds all ECMAScript 2017 globals and automatically sets the ecmaVersion parser option to 8.
es2018 - adds all ECMAScript 2018 globals and automatically sets the ecmaVersion parser option to 9.
es2019 - adds all ECMAScript 2019 globals and automatically sets the ecmaVersion parser option to 10.
es2020 - adds all ECMAScript 2020 globals and automatically sets the ecmaVersion parser option to 11.
es2021 - adds all ECMAScript 2021 globals and automatically sets the ecmaVersion parser option to 12.
es2022 - adds all ECMAScript 2022 globals and automatically sets the ecmaVersion parser option to 13.

UPDATE: There must be an error in ESLint doc because I have tested to add env.es2016: true without parserOptions.ecmaVersion: 2016 and parser is incorrectly fine with async functions which were added later in es2017. So rather set both env and ecmaVersion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM