阻止使用ESLint在IE11中使用不受支持的JavaScript功能?

Mau*_*cio 8 javascript ecmascript-6 internet-explorer-11 eslint

我有一个现有的ESLint配置,其中“ ecmaVersion”设置为“ 5”,我想对其进行修改以允许使用作为ES6功能的letconst。Internet Explorer 11主要支持*。但是,我想拒绝使用IE11不支持的任何ES6功能,例如类。如何使用ESLint做到这一点?

我确实找到了eslint-plugin-ie11插件,但是它仅涵盖了一些不受支持的功能。

*我也想阻止使用let in循环,这在IE11中不受支持。

amg*_*mgg 0

您可以添加 eslint 规则来禁止几乎任何您想要使用该no-restricted-syntax规则的语言功能。

\n

来自eslint 文档

\n
\n

此规则允许您配置要限制使用的语法元素
\n... \n您可以在 GitHub 上
找到可以使用的 AST 节点名称的完整列表,并使用AST Explorer和 espree 解析器来查看节点类型你的代码包括。\n... \n您还可以指定要限制的AST 选择器,从而可以更精确地控制语法模式。\n... \n此规则采用字符串列表,其中每个字符串都是 AST 选择器\n...\n[, 或] 对象,其中指定选择器和可选的自定义消息



\n
\n

因此,例如,如果您想阻止在...和...循环左侧使用箭头函数、模板文字和let/声明,constforinforof

\n

您可以将其添加到 eslintrc 的规则部分:

\n
"no-restricted-syntax": ["error",\n    {\n        "selector": "*:matches(ForOfStatement, ForInStatement) > VariableDeclaration.left:matches([kind=let], [kind=const])",\n        "message": "let/const not allowed in lhs of for..in / for..of loops."\n    },\n    {\n        "selector": "TemplateLiteral",\n        "message": "template literal strings not allowed"\n    },\n    "ArrowFunctionExpression"\n]\n
Run Code Online (Sandbox Code Playgroud)\n

然后,在此文件上运行 eslint

\n
for(const x of foo) {}\nfor(let x of foo) {}\nfor(const x in foo) {}\nfor(let x in foo) {}\nconsole.log(`hello world`);\nconst bar = x => x + 1;\n
Run Code Online (Sandbox Code Playgroud)\n

给出这些错误:

\n
  1:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax\n  2:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax\n  3:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax\n  4:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax\n  5:13  error  template literal strings not allowed                     no-restricted-syntax\n  6:13  error  Using \'ArrowFunctionExpression\' is not allowed           no-restricted-syntax\n\n\xe2\x9c\x96 6 problems (6 errors, 0 warnings)\n
Run Code Online (Sandbox Code Playgroud)\n

并在此文件上运行 eslint

\n
let a = 1;\nconst b = 2;\nfor(var x of foo) {}\nfor(var x in foo) {}\nconsole.log(\'hello world\');\nfunction bar(x) { return x + 1; }\n
Run Code Online (Sandbox Code Playgroud)\n

没有给出错误

\n
Run Code Online (Sandbox Code Playgroud)\n

所有es6 功能执行此操作或多或少是相同的,只是需要更多的选择器

\n