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.
此规则允许你配置要限制使用的语法元素,而不是为你要关闭的每个语言功能创建单独的规则。对于 JavaScript 语言,这些元素由其 ESTree 节点类型表示。例如,函数声明用 FunctionDeclaration
表示,with
语句用 WithStatement
表示。你可以使用 代码资源管理器 来确定代表你的代码的节点。
¥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. For the JavaScript language, 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 use Code Explorer to determine the nodes that represent your code.
你还可以指定 AST 选择器 进行限制,从而更精确地控制语法模式。
¥You can also specify AST selectors to restrict, allowing much more precise control over syntax patterns.
注意:此规则可用于使用 ESLint 进行 lint 的任何语言。要查看其他语言的代码由哪些类型的节点组成,可以使用:
¥Note: This rule can be used with any language you lint using ESLint. To see what type of nodes your code in another language consists of, you can use:
-
如果你将 ESLint 与
typescript-eslint
一起使用,则使用 typescript-eslint 在线运行。¥typescript-eslint Playground if you’re using ESLint with
typescript-eslint
. -
如果你使用 ESLint 来检查 JavaScript、JSON、Markdown 或 CSS,则使用 ESLint 代码浏览器。
¥ESLint Code Explorer if you’re using ESLint to lint JavaScript, JSON, Markdown, or CSS.
规则详情
¥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 中引入。