You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/extend/custom-rules.md
+54-52Lines changed: 54 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,9 +117,9 @@ The `context` object contains additional functionality that is helpful for rules
117
117
118
118
Additionally, the `context` object has the following methods:
119
119
120
-
*`getAncestors()` - returns an array of the ancestors of the currently-traversed node, starting at the root of the AST and continuing through the direct parent of the current node. This array does not include the currently-traversed node itself.
120
+
*`getAncestors()` - (**Deprecated:** Use `SourceCode#getAncestors(node)` instead.) returns an array of the ancestors of the currently-traversed node, starting at the root of the AST and continuing through the direct parent of the current node. This array does not include the currently-traversed node itself.
121
121
*`getCwd()` - returns the `cwd` passed to [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered as the current working directory.
122
-
*`getDeclaredVariables(node)` - returns a list of [variables](./scope-manager-interface#variable-interface) declared by the given node. This information can be used to track references to variables.
122
+
*`getDeclaredVariables(node)` - (**Deprecated:** Use `SourceCode#getDeclaredVariables(node)` instead.) returns a list of [variables](./scope-manager-interface#variable-interface) declared by the given node. This information can be used to track references to variables.
123
123
* If the node is a `VariableDeclaration`, all variables declared in the declaration are returned.
124
124
* If the node is a `VariableDeclarator`, all variables declared in the declarator are returned.
125
125
* If the node is a `FunctionDeclaration` or `FunctionExpression`, the variable for the function name is returned, in addition to variables for the function parameters.
@@ -131,62 +131,13 @@ Additionally, the `context` object has the following methods:
131
131
* Otherwise, if the node does not declare any variables, an empty array is returned.
132
132
*`getFilename()` - returns the filename associated with the source.
133
133
*`getPhysicalFilename()` - when linting a file, it returns the full path of the file on disk without any code block information. When linting text, it returns the value passed to `—stdin-filename` or `<text>` if not specified.
134
-
*`getScope()` - returns the [scope](./scope-manager-interface#scope-interface) of the currently-traversed node. This information can be used to track references to variables.
134
+
*`getScope()` - (**Deprecated:** Use `SourceCode#getScope(node)` instead.) returns the [scope](./scope-manager-interface#scope-interface) of the currently-traversed node. This information can be used to track references to variables.
135
135
*`getSourceCode()` - returns a [`SourceCode`](#contextgetsourcecode) object that you can use to work with the source that was passed to ESLint.
136
136
*`markVariableAsUsed(name)` - marks a variable with the given name in the current scope as used. This affects the [no-unused-vars](../rules/no-unused-vars) rule. Returns `true` if a variable with the given name was found and marked as used, otherwise `false`.
137
137
*`report(descriptor)` - reports a problem in the code (see the [dedicated section](#contextreport)).
138
138
139
139
**Note:** Earlier versions of ESLint supported additional methods on the `context` object. Those methods were removed in the new format and should not be relied upon.
140
140
141
-
### context.getScope()
142
-
143
-
This method returns the scope of the current node. It is a useful method for finding information about the variables in a given scope, and how they are used in other scopes.
144
-
145
-
#### Scope types
146
-
147
-
The following table contains a list of AST node types and the scope type that they correspond to. For more information about the scope types, refer to the [`Scope` object documentation](./scope-manager-interface#scope-interface).
148
-
149
-
| AST Node Type | Scope Type |
150
-
|:--------------------------|:-----------|
151
-
|`Program`|`global`|
152
-
|`FunctionDeclaration`|`function`|
153
-
|`FunctionExpression`|`function`|
154
-
|`ArrowFunctionExpression`|`function`|
155
-
|`ClassDeclaration`|`class`|
156
-
|`ClassExpression`|`class`|
157
-
|`BlockStatement` ※1 |`block`|
158
-
|`SwitchStatement` ※1 |`switch`|
159
-
|`ForStatement` ※2 |`for`|
160
-
|`ForInStatement` ※2 |`for`|
161
-
|`ForOfStatement` ※2 |`for`|
162
-
|`WithStatement`|`with`|
163
-
|`CatchClause`|`catch`|
164
-
| others | ※3 |
165
-
166
-
**※1** Only if the configured parser provided the block-scope feature. The default parser provides the block-scope feature if `parserOptions.ecmaVersion` is not less than `6`.<br>
167
-
**※2** Only if the `for` statement defines the iteration variable as a block-scoped variable (E.g., `for (let i = 0;;) {}`).<br>
168
-
**※3** The scope of the closest ancestor node which has own scope. If the closest ancestor node has multiple scopes then it chooses the innermost scope (E.g., the `Program` node has a `global` scope and a `module` scope if `Program#sourceType` is `"module"`. The innermost scope is the `module` scope.).
169
-
170
-
#### Scope Variables
171
-
172
-
The `Scope#variables` property contains an array of [`Variable` objects](./scope-manager-interface#variable-interface). These are the variables declared in current scope. You can use these `Variable` objects to track references to a variable throughout the entire module.
173
-
174
-
Inside of each `Variable`, the `Variable#references` property contains an array of [`Reference` objects](./scope-manager-interface#reference-interface). The `Reference` array contains all the locations where the variable is referenced in the module's source code.
175
-
176
-
Also inside of each `Variable`, the `Variable#defs` property contains an array of [`Definition` objects](./scope-manager-interface#definition-interface). You can use the `Definitions` to find where the variable was defined.
177
-
178
-
Global variables have the following additional properties:
179
-
180
-
*`Variable#writeable` (`boolean | undefined`) ... If `true`, this global variable can be assigned arbitrary value. If `false`, this global variable is read-only.
181
-
*`Variable#eslintExplicitGlobal` (`boolean | undefined`) ... If `true`, this global variable was defined by a `/* globals */` directive comment in the source code file.
182
-
*`Variable#eslintExplicitGlobalComments` (`Comment[] | undefined`) ... The array of `/* globals */` directive comments which defined this global variable in the source code file. This property is `undefined` if there are no `/* globals */` directive comments.
183
-
*`Variable#eslintImplicitGlobalSetting` (`"readonly" | "writable" | undefined`) ... The configured value in config files. This can be different from `variable.writeable` if there are `/* globals */` directive comments.
184
-
185
-
For examples of using `context.getScope()` to track variables, refer to the source code for the following built-in rules:
186
-
187
-
*[no-shadow](https://github.com/eslint/eslint/blob/main/lib/rules/no-shadow.js): Calls `context.getScopes()` at the global scope and parses all child scopes to make sure a variable name is not reused at a lower scope. ([no-shadow](../rules/no-shadow) documentation)
188
-
*[no-redeclare](https://github.com/eslint/eslint/blob/main/lib/rules/no-redeclare.js): Calls `context.getScope()` at each scope to make sure that a variable is not declared twice at that scope. ([no-redeclare](../rules/no-redeclare) documentation)
189
-
190
141
### context.report()
191
142
192
143
The main method you'll use is `context.report()`, which publishes a warning or error (depending on the configuration being used). This method accepts a single argument, which is an object containing the following properties:
@@ -684,6 +635,57 @@ Finally, comments can be accessed through many of `sourceCode`'s methods using t
684
635
685
636
Shebangs are represented by tokens of type `"Shebang"`. They are treated as comments and can be accessed by the methods outlined above.
686
637
638
+
### Accessing Variable Scopes
639
+
640
+
The `SourceCode#getScope(node)` method returns the scope of the given node. It is a useful method for finding information about the variables in a given scope and how they are used in other scopes.
641
+
642
+
**Deprecated:** The `context.getScope()` is deprecated; make sure to use `SourceCode#getScope(node)` instead.
643
+
644
+
#### Scope types
645
+
646
+
The following table contains a list of AST node types and the scope type that they correspond to. For more information about the scope types, refer to the [`Scope` object documentation](./scope-manager-interface#scope-interface).
647
+
648
+
| AST Node Type | Scope Type |
649
+
|:--------------------------|:-----------|
650
+
|`Program`|`global`|
651
+
|`FunctionDeclaration`|`function`|
652
+
|`FunctionExpression`|`function`|
653
+
|`ArrowFunctionExpression`|`function`|
654
+
|`ClassDeclaration`|`class`|
655
+
|`ClassExpression`|`class`|
656
+
|`BlockStatement` ※1 |`block`|
657
+
|`SwitchStatement` ※1 |`switch`|
658
+
|`ForStatement` ※2 |`for`|
659
+
|`ForInStatement` ※2 |`for`|
660
+
|`ForOfStatement` ※2 |`for`|
661
+
|`WithStatement`|`with`|
662
+
|`CatchClause`|`catch`|
663
+
| others | ※3 |
664
+
665
+
**※1** Only if the configured parser provided the block-scope feature. The default parser provides the block-scope feature if `parserOptions.ecmaVersion` is not less than `6`.<br>
666
+
**※2** Only if the `for` statement defines the iteration variable as a block-scoped variable (E.g., `for (let i = 0;;) {}`).<br>
667
+
**※3** The scope of the closest ancestor node which has own scope. If the closest ancestor node has multiple scopes then it chooses the innermost scope (E.g., the `Program` node has a `global` scope and a `module` scope if `Program#sourceType` is `"module"`. The innermost scope is the `module` scope.).
668
+
669
+
#### Scope Variables
670
+
671
+
The `Scope#variables` property contains an array of [`Variable` objects](./scope-manager-interface#variable-interface). These are the variables declared in current scope. You can use these `Variable` objects to track references to a variable throughout the entire module.
672
+
673
+
Inside of each `Variable`, the `Variable#references` property contains an array of [`Reference` objects](./scope-manager-interface#reference-interface). The `Reference` array contains all the locations where the variable is referenced in the module's source code.
674
+
675
+
Also inside of each `Variable`, the `Variable#defs` property contains an array of [`Definition` objects](./scope-manager-interface#definition-interface). You can use the `Definitions` to find where the variable was defined.
676
+
677
+
Global variables have the following additional properties:
678
+
679
+
*`Variable#writeable` (`boolean | undefined`) ... If `true`, this global variable can be assigned arbitrary value. If `false`, this global variable is read-only.
680
+
*`Variable#eslintExplicitGlobal` (`boolean | undefined`) ... If `true`, this global variable was defined by a `/* globals */` directive comment in the source code file.
681
+
*`Variable#eslintExplicitGlobalComments` (`Comment[] | undefined`) ... The array of `/* globals */` directive comments which defined this global variable in the source code file. This property is `undefined` if there are no `/* globals */` directive comments.
682
+
*`Variable#eslintImplicitGlobalSetting` (`"readonly" | "writable" | undefined`) ... The configured value in config files. This can be different from `variable.writeable` if there are `/* globals */` directive comments.
683
+
684
+
For examples of using `SourceCode#getScope()` to track variables, refer to the source code for the following built-in rules:
685
+
686
+
*[no-shadow](https://github.com/eslint/eslint/blob/main/lib/rules/no-shadow.js): Calls `sourceCode.getScope()` at the `Program` node and inspects all child scopes to make sure a variable name is not reused at a lower scope. ([no-shadow](../rules/no-shadow) documentation)
687
+
*[no-redeclare](https://github.com/eslint/eslint/blob/main/lib/rules/no-redeclare.js): Calls `sourceCode.getScope()` at each scope to make sure that a variable is not declared twice in the same scope. ([no-redeclare](../rules/no-redeclare) documentation)
0 commit comments