]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-restricted-syntax.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-restricted-syntax.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-restricted-syntax
8f9d1d4d
DC
3rule_type: suggestion
4related_rules:
5- no-alert
6- no-console
7- no-debugger
8- no-restricted-properties
9---
10
eb39fafa
DC
11
12JavaScript 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.
13
8f9d1d4d 14Rather 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](https://github.com/estree/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](https://github.com/eslint/eslint-visitor-keys/blob/main/lib/visitor-keys.js) and use [AST Explorer](https://astexplorer.net/) with the espree parser to see what type of nodes your code consists of.
eb39fafa 15
f2a92ac6 16You can also specify [AST selectors](../extend/selectors) to restrict, allowing much more precise control over syntax patterns.
eb39fafa
DC
17
18## Rule Details
19
20This rule disallows specified (that is, user-defined) syntax.
21
22## Options
23
24This rule takes a list of strings, where each string is an AST selector:
25
26```json
27{
28 "rules": {
29 "no-restricted-syntax": ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"]
30 }
31}
32```
33
34Alternatively, the rule also accepts objects, where the selector and an optional custom message are specified:
35
36```json
37{
38 "rules": {
39 "no-restricted-syntax": [
40 "error",
41 {
42 "selector": "FunctionExpression",
43 "message": "Function expressions are not allowed."
44 },
45 {
46 "selector": "CallExpression[callee.name='setTimeout'][arguments.length!=2]",
47 "message": "setTimeout must always be invoked with two arguments."
48 }
49 ]
50 }
51}
52```
53
54If 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.
55
56The string and object formats can be freely mixed in the configuration as needed.
57
58Examples of **incorrect** code for this rule with the `"FunctionExpression", "WithStatement", BinaryExpression[operator='in']` options:
59
8f9d1d4d
DC
60::: incorrect
61
eb39fafa
DC
62```js
63/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
64
65with (me) {
66 dontMess();
67}
68
69var doSomething = function () {};
70
71foo in bar;
72```
73
8f9d1d4d
DC
74:::
75
eb39fafa
DC
76Examples of **correct** code for this rule with the `"FunctionExpression", "WithStatement", BinaryExpression[operator='in']` options:
77
8f9d1d4d
DC
78::: correct
79
eb39fafa
DC
80```js
81/* eslint no-restricted-syntax: ["error", "FunctionExpression", "WithStatement", "BinaryExpression[operator='in']"] */
82
83me.dontMess();
84
85function doSomething() {};
86
87foo instanceof bar;
88```
89
8f9d1d4d
DC
90:::
91
eb39fafa
DC
92## When Not To Use It
93
94If you don't want to restrict your code from using any JavaScript features or syntax, you should not use this rule.