Skip to content

Commit e52b98b

Browse files
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>
1 parent e980bf3 commit e52b98b

File tree

222 files changed

+272
-260
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

222 files changed

+272
-260
lines changed

docs/src/extend/code-path-analysis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Please use a map of information instead.
259259
```js
260260
function hasCb(node, context) {
261261
if (node.type.indexOf("Function") !== -1) {
262-
const sourceCode = context.getSourceCode();
262+
const sourceCode = context.sourceCode;
263263
return sourceCode.getDeclaredVariables(node).some(function(v) {
264264
return v.type === "Parameter" && v.name === "cb";
265265
});

docs/src/extend/custom-rules.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ The `context` object has the following properties:
128128
* `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.
129129
* `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.
130130
* `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)).
131132
* `settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
132133
* `parserPath`: (`string`) The name of the `parser` from the configuration.
133134
* `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:
150151
* `getFilename()`: (**Deprecated:** Use `context.filename` instead.) Returns the filename associated with the source.
151152
* `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.
152153
* `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)).
154155
* `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`.
155156
* `report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).
156157

@@ -509,18 +510,20 @@ When using options, make sure that your rule has some logical defaults in case t
509510

510511
### Accessing the Source Code
511512

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:
513514

514515
```js
515516
module.exports = {
516517
create: function(context) {
517-
var sourceCode = context.getSourceCode();
518+
var sourceCode = context.sourceCode;
518519

519520
// ...
520521
}
521522
};
522523
```
523524

525+
**Deprecated:** The `context.getSourceCode()` method is deprecated; make sure to use `context.sourceCode` property instead.
526+
524527
Once you have an instance of `SourceCode`, you can use the following methods on it to work with the code:
525528

526529
* `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
712715
```js
713716
module.exports = {
714717
create: function(context) {
715-
var sourceCode = context.getSourceCode();
718+
var sourceCode = context.sourceCode;
716719

717720
return {
718721
ReturnStatement(node) {

lib/linter/linter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ const BASE_TRAVERSAL_CONTEXT = Object.freeze(
902902
(contextInfo, methodName) =>
903903
Object.assign(contextInfo, {
904904
[methodName](...args) {
905-
return this.getSourceCode()[DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]](...args);
905+
return this.sourceCode[DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]](...args);
906906
}
907907
}),
908908
{}
@@ -958,6 +958,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO
958958
physicalFilename: physicalFilename || filename,
959959
getScope: () => sourceCode.getScope(currentNode),
960960
getSourceCode: () => sourceCode,
961+
sourceCode,
961962
markVariableAsUsed: name => sourceCode.markVariableAsUsed(name, currentNode),
962963
parserOptions: {
963964
...languageOptions.parserOptions

lib/rules/accessor-pairs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ module.exports = {
178178
const checkGetWithoutSet = config.getWithoutSet === true;
179179
const checkSetWithoutGet = config.setWithoutGet !== false;
180180
const enforceForClassMembers = config.enforceForClassMembers !== false;
181-
const sourceCode = context.getSourceCode();
181+
const sourceCode = context.sourceCode;
182182

183183
/**
184184
* Reports the given node.

lib/rules/array-bracket-newline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656
},
5757

5858
create(context) {
59-
const sourceCode = context.getSourceCode();
59+
const sourceCode = context.sourceCode;
6060

6161

6262
//----------------------------------------------------------------------

lib/rules/array-bracket-spacing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = {
5353
},
5454
create(context) {
5555
const spaced = context.options[0] === "always",
56-
sourceCode = context.getSourceCode();
56+
sourceCode = context.sourceCode;
5757

5858
/**
5959
* Determines whether an option is set, relative to the spacing option.

lib/rules/array-callback-return.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ module.exports = {
172172
create(context) {
173173

174174
const options = context.options[0] || { allowImplicit: false, checkForEach: false };
175-
const sourceCode = context.getSourceCode();
175+
const sourceCode = context.sourceCode;
176176

177177
let funcInfo = {
178178
arrayMethodName: null,

lib/rules/array-element-newline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module.exports = {
7979
},
8080

8181
create(context) {
82-
const sourceCode = context.getSourceCode();
82+
const sourceCode = context.sourceCode;
8383

8484
//----------------------------------------------------------------------
8585
// Helpers

lib/rules/arrow-body-style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
const asNeeded = !options[0] || options[0] === "as-needed";
7575
const never = options[0] === "never";
7676
const requireReturnForObjectLiteral = options[1] && options[1].requireReturnForObjectLiteral;
77-
const sourceCode = context.getSourceCode();
77+
const sourceCode = context.sourceCode;
7878
let funcInfo = null;
7979

8080
/**

lib/rules/arrow-parens.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = {
6969
const asNeeded = context.options[0] === "as-needed";
7070
const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true;
7171

72-
const sourceCode = context.getSourceCode();
72+
const sourceCode = context.sourceCode;
7373

7474
/**
7575
* Finds opening paren of parameters for the given arrow function, if it exists.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy