简体   繁体   English

env.es6=true 和 parserOptions.ecmaVersion=6 at.eslintrc.js 有什么区别?

[英]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. parserOptions 的 ecmaVersion 选项用于语法。 On the otherhand, env option is for global variables.另一方面, env 选项用于全局变量。

If you want to use a Promise for example, "ecmaVersion:2020" is not enough.例如,如果您想使用 Promise,“ecmaVersion:2020”是不够的。 You also have to specify which env to use.您还必须指定要使用的环境。

Note that env option automatically enables new syntax.请注意, env 选项会自动启用新语法。 But personally I recommend to set both of them properly.但我个人建议正确设置它们。

For more information, see here有关更多信息,请参见此处

For ES6 syntax, use { "parserOptions": { "ecmaVersion": 6 } };对于 ES6 语法,使用 { "parserOptions": { "ecmaVersion": 6 } }; for new ES6 global variables, use { "env": { "es6": true } }.对于新的 ES6 全局变量,使用 { "env": { "es6": true } }。 { "env": { "es6": true } } enables ES6 syntax automatically, but { "parserOptions": { "ecmaVersion": 6 } } does not enable ES6 globals automatically { "env": { "es6": true } } 自动启用 ES6 语法,但 { "parserOptions": { "ecmaVersion": 6 } } 不会自动启用 ES6 全局变量

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 .通过这个https://eslint.org/docs/latest/use/configure/language-options#specifying-environments设置 env 就足够了,它会自动设置正确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.更新:ESLint 文档中一定有错误,因为我已经测试过添加env.es2016: true without parserOptions.ecmaVersion: 2016并且解析器不正确地使用异步函数,这些函数后来在 es2017 中添加。 So rather set both env and ecmaVersion.所以宁可同时设置 env 和 ecmaVersion。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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