首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止使用JavaScript在IE11中使用不支持的ESLint特性?

防止使用JavaScript在IE11中使用不支持的ESLint特性?
EN

Stack Overflow用户
提问于 2018-11-05 19:09:35
回答 1查看 1.7K关注 0票数 15

我有一个现有的ESLint配置,将"ecmaVersion“设置为"5”,我想要修改该配置,以允许使用letconst (这是ES6特性)。Internet 11中主要支持*,但是,我想拒绝使用在ES6中不支持的任何特性,例如类。我怎样才能用ESLint做到这一点呢?

我确实找到了Elint-plugin-IE11插件,但它只包含了一些不受支持的特性。

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

EN

回答 1

Stack Overflow用户

发布于 2021-11-01 22:15:06

您可以添加eslint规则来禁止使用no-restricted-syntax规则的任何语言特性。

来自埃斯林特博士

此规则允许您配置要限制使用 ..。 您可以找到完整的AST节点名称列表,您可以使用论GitHub,并在espree解析器中使用AST探索者来查看代码所包含的节点类型。 ..。 您还可以指定AST选择器进行限制,允许对语法模式进行更精确的控制。 ..。 此规则接受字符串列表,其中每个字符串都是AST选择器.或对象,其中指定了选择器和可选的自定义消息

因此,例如,如果您想阻止箭头函数、模板文本和let/const声明在for.infor.of循环左侧的使用,

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

代码语言:javascript
复制
"no-restricted-syntax": ["error",
    {
        "selector": "*:matches(ForOfStatement, ForInStatement) > VariableDeclaration.left:matches([kind=let], [kind=const])",
        "message": "let/const not allowed in lhs of for..in / for..of loops."
    },
    {
        "selector": "TemplateLiteral",
        "message": "template literal strings not allowed"
    },
    "ArrowFunctionExpression"
]

然后,在这个文件上运行eslint

代码语言:javascript
复制
for(const x of foo) {}
for(let x of foo) {}
for(const x in foo) {}
for(let x in foo) {}
console.log(`hello world`);
const bar = x => x + 1;

给出以下错误:

代码语言:javascript
复制
  1:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax
  2:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax
  3:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax
  4:5   error  let/const not allowed in lhs of for..in / for..of loops  no-restricted-syntax
  5:13  error  template literal strings not allowed                     no-restricted-syntax
  6:13  error  Using 'ArrowFunctionExpression' is not allowed           no-restricted-syntax

✖ 6 problems (6 errors, 0 warnings)

并在这个文件上运行eslint

代码语言:javascript
复制
let a = 1;
const b = 2;
for(var x of foo) {}
for(var x in foo) {}
console.log('hello world');
function bar(x) { return x + 1; }

不出差错

代码语言:javascript
复制

对所有es6特性这样做或多或少都是一样的,只是有更多的选择器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53160669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档