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
feat: add sourceCode property to the rule context (#17107)
* feat: add `sourceCode` to the rule context
* docs: mark `context#getSourceCode()` as deprecated
* reafctor: prefer to use context.sourceCode and add tests for getSourceCode()
* docs: add deprecation note for context.getScope()
* docs: update docs/src/extend/custom-rules.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
---------
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Copy file name to clipboardExpand all lines: docs/src/extend/custom-rules.md
+7-4Lines changed: 7 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -128,6 +128,7 @@ The `context` object has the following properties:
128
128
*`physicalFilename`: (`string`) When linting a file, it provides the full path of the file on disk without any code block information. When linting text, it provides the value passed to `—stdin-filename` or `<text>` if not specified.
129
129
*`cwd`: (`string`) 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.
130
130
*`options`: (`array`) An array of the [configured options](../use/configure/rules) for this rule. This array does not include the rule severity (see the [dedicated section](#accessing-options-passed-to-a-rule)).
131
+
*`sourceCode`: (`object`) 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)).
131
132
*`settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
132
133
*`parserPath`: (`string`) The name of the `parser` from the configuration.
133
134
*`parserServices`: (`object`) Contains parser-provided services for rules. The default parser does not provide any services. However, if a rule is intended to be used with a custom parser, it could use `parserServices` to access anything provided by that parser. (For example, a TypeScript parser could provide the ability to get the computed type of a given node.)
@@ -150,7 +151,7 @@ Additionally, the `context` object has the following methods:
150
151
*`getFilename()`: (**Deprecated:** Use `context.filename` instead.) Returns the filename associated with the source.
151
152
*`getPhysicalFilename()`: (**Deprecated:** Use `context.physicalFilename` instead.) 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.
152
153
*`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.
153
-
*`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)).
154
+
*`getSourceCode()`: (**Deprecated:** Use `context.sourceCode` instead.) 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)).
154
155
*`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`.
155
156
*`report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).
156
157
@@ -509,18 +510,20 @@ When using options, make sure that your rule has some logical defaults in case t
509
510
510
511
### Accessing the Source Code
511
512
512
-
The `SourceCode` object is the main object for getting more information about the source code being linted. You can retrieve the `SourceCode` object at any time by using the `context.getSourceCode()` method:
513
+
The `SourceCode` object is the main object for getting more information about the source code being linted. You can retrieve the `SourceCode` object at any time by using the `context.sourceCode` property:
513
514
514
515
```js
515
516
module.exports= {
516
517
create:function(context) {
517
-
var sourceCode =context.getSourceCode();
518
+
var sourceCode =context.sourceCode;
518
519
519
520
// ...
520
521
}
521
522
};
522
523
```
523
524
525
+
**Deprecated:** The `context.getSourceCode()` method is deprecated; make sure to use `context.sourceCode` property instead.
526
+
524
527
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
525
528
526
529
*`getText(node)`: Returns the source code for the given node. Omit `node` to get the whole source (see the [dedicated section](#accessing-the-source-text)).
@@ -712,7 +715,7 @@ To help with this, you can use the `sourceCode.markVariableAsUsed()` method. Thi
0 commit comments