簡體   English   中英

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

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

我不明白為什么我需要在兩個不同的參數中指定相同的信息。

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']
  }
};

parserOptions 的 ecmaVersion 選項用於語法。 另一方面, env 選項用於全局變量。

例如,如果您想使用 Promise,“ecmaVersion:2020”是不夠的。 您還必須指定要使用的環境。

請注意, env 選項會自動啟用新語法。 但我個人建議正確設置它們。

有關更多信息,請參見此處

對於 ES6 語法,使用 { "parserOptions": { "ecmaVersion": 6 } }; 對於新的 ES6 全局變量,使用 { "env": { "es6": true } }。 { "env": { "es6": true } } 自動啟用 ES6 語法,但 { "parserOptions": { "ecmaVersion": 6 } } 不會自動啟用 ES6 全局變量

通過這個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 without parserOptions.ecmaVersion: 2016並且解析器不正確地使用異步函數,這些函數后來在 es2017 中添加。 所以寧可同時設置 env 和 ecmaVersion。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM