简体   繁体   English

为什么 ESLint 没有检测到更高版本 ES 的全局变量?

[英]Why ESLint doesn't detect a global variable of a later ES release?

index.js:索引.js:

var mySet = new Set();

Command:命令:

npx eslint --no-eslintrc index.js

Problem: ESLint doesn't detect that Set doesn't exist in ES5.问题: ESLint 没有检测到 ES5 中不存在Set

However, ESLint explains ( https://eslint.org/docs/latest/user-guide/configuring/language-options#specifying-parser-options ):但是,ESLint 解释了 ( https://eslint.org/docs/latest/user-guide/configuring/language-options#specifying-parser-options ):

By default, ESLint expects ECMAScript 5 syntax.默认情况下,ESLint 需要 ECMAScript 5 语法。

And for the env property, that provides global variables ( https://eslint.org/docs/latest/user-guide/configuring/language-options#specifying-environments ):对于env属性,它提供了全局变量( https://eslint.org/docs/latest/user-guide/configuring/language-options#specifying-environments ):

es6 - enable all ECMAScript 6 features except for modules (this automatically sets the ecmaVersion parser option to 6). es6 - 启用除模块之外的所有 ECMAScript 6 功能(这会自动将 ecmaVersion 解析器选项设置为 6)。

for new ES6 global variables, use { "env": { "es6": true } }对于新的 ES6 全局变量,使用 { "env": { "es6": true } }

Do you know why ESLint doesn't trigger any error for new Set() ?你知道为什么 ESLint 不会为new Set()触发任何错误吗? The problem remains the same if I use an eslintrc with env and parserOptions empty, or "ecmaVersion": 5 for parserOptions .如果我使用 eslintrc 且envparserOptions空,或者使用"ecmaVersion": 5 for parserOptions ,问题仍然存在。

The problem was that, as Bergi said, ESLint did not complain about undefined variables .问题是,正如 Bergi 所说, ESLint 没有抱怨 undefined variables

Example:例子:

var s = new Set();
var f = new Foo(); // (Foo does not exist)

-> ESLint does not complain. -> ESLint 不会抱怨。

However, if I add "extends": "eslint:recommended" to my eslintrc, then both lines fail, because of https://eslint.org/docs/latest/rules/no-undef .但是,如果我将"extends": "eslint:recommended"添加到我的 eslintrc,那么这两行都会失败,因为https://eslint.org/docs/latest/rules/no-undef

Indeed, if I add:确实,如果我添加:

{ "env": { "es6": true } }

to my eslintrc, then var f = new Foo();到我的 eslintrc,然后var f = new Foo(); fails, but var s = new Set();失败,但是var s = new Set(); does not.才不是。

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

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