no-restricted-syntax
不允许指定的语法
JavaScript 有很多语言特性,并不是每个人都喜欢。因此,一些项目选择完全禁止使用某些语言特性。例如,你可能决定禁止使用 try-catch
或 class
,或者你可能决定禁止使用 in
运算符。
¥JavaScript has a lot of language features, and not everyone likes all of them. As a result, some projects choose to disallow the use of certain language features altogether. For instance, you might decide to disallow the use of try-catch
or class
, or you might decide to disallow the use of the in
operator.
此规则允许你配置要限制使用的语法元素,而不是为你要关闭的每个语言功能创建单独的规则。这些元素由它们的 ESTree 节点类型表示。例如,函数声明用 FunctionDeclaration
表示,with
语句用 WithStatement
表示。你可以找到 AST 节点名称的完整列表,你可以使用 在 GitHub 上 并将 AST Explorer 与 espree 解析器一起使用,以查看你的代码包含哪些类型的节点。
¥Rather than creating separate rules for every language feature you want to turn off, this rule allows you to configure the syntax elements you want to restrict use of. These elements are represented by their ESTree node types. For example, a function declaration is represented by FunctionDeclaration
and the with
statement is represented by WithStatement
. You may find the full list of AST node names you can use on GitHub and use AST Explorer with the espree parser to see what type of nodes your code consists of.
你还可以指定 AST 选择器 进行限制,从而更精确地控制语法模式。
¥You can also specify AST selectors to restrict, allowing much more precise control over syntax patterns.
规则详情
¥Rule Details
此规则不允许指定的(即用户定义的)语法。
¥This rule disallows specified (that is, user-defined) syntax.
选项
¥Options
此规则接受一个字符串列表,其中每个字符串都是一个 AST 选择器:
¥This rule takes a list of strings, where each string is an AST selector:
{
"rules": {
"no-restricted-syntax": ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"]
}
}
或者,该规则还接受对象,其中指定了选择器和可选的自定义消息:
¥Alternatively, the rule also accepts objects, where the selector and an optional custom message are specified:
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "FunctionExpression",
"message": "Function expressions are not allowed."
},
{
"selector": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
"message": "setTimeout must always be invoked with two arguments."
}
]
}
}
如果使用 message
属性指定了自定义消息,ESLint 将在报告出现 selector
属性中指定的语法时使用该消息。
¥If a custom message is specified with the message
property, ESLint will use that message when reporting occurrences of the syntax specified in the selector
property.
字符串和对象格式可以根据需要在配置中自由混合。
¥The string and object formats can be freely mixed in the configuration as needed.
使用 "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
with (me) {
dontMess();
}
var doSomething = function () {};
foo in bar;
使用 "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "FunctionExpression", "WithStatement", BinaryExpression[operator='in']
options:
/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
me.dontMess();
function doSomething() {};
foo instanceof bar;
何时不使用
¥When Not To Use It
如果你不想限制代码使用任何 JavaScript 功能或语法,则不应使用此规则。
¥If you don’t want to restrict your code from using any JavaScript features or syntax, you should not use this rule.
相关规则
版本
此规则是在 ESLint v1.4.0 中引入。