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
+14-15Lines changed: 14 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,9 +132,9 @@ The `context` object has the following properties:
132
132
133
133
Additionally, the `context` object has the following methods:
134
134
135
-
*`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.
136
-
*`getCwd()` - Returns the `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
137
-
*`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.
135
+
*`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.
136
+
*`getCwd()`: Returns the `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
137
+
*`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.
138
138
* If the node is a `VariableDeclaration`, all variables declared in the declaration are returned.
139
139
* If the node is a `VariableDeclarator`, all variables declared in the declarator are returned.
140
140
* If the node is a `FunctionDeclaration` or `FunctionExpression`, the variable for the function name is returned, in addition to variables for the function parameters.
@@ -148,7 +148,7 @@ Additionally, the `context` object has the following methods:
148
148
*`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.
149
149
*`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.
150
150
*`getSourceCode()`: Returns a `SourceCode` object that you can use to work with the source that was passed to ESLint (see [Accessing the Source Code](#accessing-the-source-code)).
151
-
*`markVariableAsUsed(name)` - (**Deprecated:** Use `SourceCode#markVariableAsUsed(name, node)` instead.) 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`.
151
+
*`markVariableAsUsed(name)`: (**Deprecated:** Use `SourceCode#markVariableAsUsed(name, node)` instead.) 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`.
152
152
*`report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).
153
153
154
154
**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.
@@ -257,7 +257,7 @@ var rule = require("../../../lib/rules/avoid-name");
257
257
var RuleTester =require("eslint").RuleTester;
258
258
259
259
var ruleTester =newRuleTester();
260
-
ruleTester.run("my-rule", rule, {
260
+
ruleTester.run("avoid-name", rule, {
261
261
valid: ["bar", "baz"],
262
262
invalid: [
263
263
{
@@ -318,22 +318,21 @@ Best practices for fixes:
318
318
1. Make fixes as small as possible. Fixes that are unnecessarily large could conflict with other fixes, and prevent them from being applied.
319
319
1. Only make one fix per message. This is enforced because you must return the result of the fixer operation from `fix()`.
320
320
1. Since all rules are run again after the initial round of fixes is applied, it's not necessary for a rule to check whether the code style of a fix will cause errors to be reported by another rule.
321
+
* For example, suppose a fixer would like to surround an object key with quotes, but it's not sure whether the user would prefer single or double quotes.
321
322
322
-
* For example, suppose a fixer would like to surround an object key with quotes, but it's not sure whether the user would prefer single or double quotes.
323
+
```js
324
+
({ foo :1 })
323
325
324
-
```js
325
-
({ foo :1 })
326
+
// should get fixed to either
326
327
327
-
// should get fixed to either
328
+
({ 'foo':1 })
328
329
329
-
({ 'foo':1 })
330
+
// or
330
331
331
-
// or
332
+
({ "foo":1 })
333
+
```
332
334
333
-
({ "foo":1 })
334
-
```
335
-
336
-
* This fixer can just select a quote type arbitrarily. If it guesses wrong, the resulting code will be automatically reported and fixed by the [`quotes`](../rules/quotes) rule.
335
+
* This fixer can just select a quote type arbitrarily. If it guesses wrong, the resulting code will be automatically reported and fixed by the [`quotes`](../rules/quotes) rule.
337
336
338
337
Note: Making fixes as small as possible is a best practice, but in some cases it may be correct to extend the range of the fix in order to intentionally prevent other rules from making fixes in a surrounding range in the same pass. For instance, if replacement text declares a newvariable, it can be useful to prevent other changes in the scope of the variable as they might cause name collisions.
0 commit comments