我不明白为什么我需要在两个不同的参数中指定相同的信息,
env: { es6: true }
和 parserOptions: { ecmaVersion: 6 }
。
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'eslint:recommended'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 6
},
};
ecmaVersion
的parserOptions
选项用于语法。 env
选项适用于全局变量。
例如,如果您想使用 Promise,则
ecmaVersion:latest
是不够的。 env
)。
请注意,
env
选项会自动启用新语法。 来自配置语言选项:
对于 ES6 语法,使用
;对于新的 ES6 全局变量,请使用{ "parserOptions": { "ecmaVersion": 6 } }
。 设置{ "env": { "es6": true } }
自动启用 ES6 语法,但{ "env": { "es6": true } }
不会自动启用 ES6 全局变量。{ "parserOptions": { "ecmaVersion": 6 } }
欲了解更多信息,请参阅此处。
通过这个https://eslint.org/docs/latest/use/configure/language-options#specifying-environments只需设置 env 就足够了,它会自动设置正确的
parserOptions.ecmaVersion
。所以你应该更喜欢这种方式以避免配置不匹配。
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.
更新:ESLint 文档中一定有错误,因为我已经测试过在没有
env.es2016 : true
的情况下添加 parserOptions.ecmaVersion : 2016
,并且解析器对于 es2017 后来添加的异步函数不正确。因此,最好同时设置 env 和 ecmaVersion。