我有一个现有的ESLint配置,将"ecmaVersion“设置为"5”,我想要修改该配置,以允许使用let和const (这是ES6特性)。Internet 11中主要支持*,但是,我想拒绝使用在ES6中不支持的任何特性,例如类。我怎样才能用ESLint做到这一点呢?
我确实找到了Elint-plugin-IE11插件,但它只包含了一些不受支持的特性。
*我还想阻止let in循环的使用,这在IE11中是不支持的。
发布于 2021-11-01 22:15:06
您可以添加eslint规则来禁止使用no-restricted-syntax规则的任何语言特性。
来自埃斯林特博士
此规则允许您配置要限制使用 ..。 您可以找到完整的AST节点名称列表,您可以使用论GitHub,并在espree解析器中使用AST探索者来查看代码所包含的节点类型。 ..。 您还可以指定AST选择器进行限制,允许对语法模式进行更精确的控制。 ..。 此规则接受字符串列表,其中每个字符串都是AST选择器.或对象,其中指定了选择器和可选的自定义消息
因此,例如,如果您想阻止箭头函数、模板文本和let/const声明在for.in和for.of循环左侧的使用,
您可以将其添加到eslintrc的规则部分:
"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
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;给出以下错误:
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
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; }不出差错
对所有es6特性这样做或多或少都是一样的,只是有更多的选择器。
https://stackoverflow.com/questions/53160669
复制相似问题