Skip to content

Commit

Permalink
reafctor: prefer to use context.sourceCode and add tests for getSourc…
Browse files Browse the repository at this point in the history
…eCode()
  • Loading branch information
snitin315 committed Apr 30, 2023
1 parent 8c661e9 commit f8a6c80
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/src/extend/custom-rules.md
Expand Up @@ -128,6 +128,7 @@ The `context` object has the following properties:
* `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.
* `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.
* `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)).
* `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)).
* `settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
* `parserPath`: (`string`) The name of the `parser` from the configuration.
* `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.)
Expand All @@ -151,7 +152,6 @@ Additionally, the `context` object has the following methods:
* `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.
* `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.
* `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)).
* `sourceCode`: 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)).
* `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`.
* `report(descriptor)`. Reports a problem in the code (see the [dedicated section](#reporting-problems)).

Expand Down
2 changes: 1 addition & 1 deletion lib/linter/linter.js
Expand Up @@ -902,7 +902,7 @@ const BASE_TRAVERSAL_CONTEXT = Object.freeze(
(contextInfo, methodName) =>
Object.assign(contextInfo, {
[methodName](...args) {
return this.getSourceCode()[DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]](...args);
return this.sourceCode[DEPRECATED_SOURCECODE_PASSTHROUGHS[methodName]](...args);
}
}),
{}
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -116,6 +116,7 @@ describe("Linter", () => {

it("has all the `parent` properties on nodes when the rule listeners are created", () => {
const spy = sinon.spy(context => {
assert.strictEqual(context.getSourceCode(), context.sourceCode);
const ast = context.sourceCode.ast;

assert.strictEqual(ast.body[0].parent, ast);
Expand Down Expand Up @@ -3780,6 +3781,7 @@ var a = "test2";
const sourceCode = context.sourceCode;
const comments = sourceCode.getAllComments();

assert.strictEqual(context.getSourceCode(), sourceCode);
assert.strictEqual(1, comments.length);

const foo = getVariable(scope, "foo");
Expand Down Expand Up @@ -5436,6 +5438,7 @@ var a = "test2";
const sourceCode = context.sourceCode;
const comments = sourceCode.getAllComments();

assert.strictEqual(context.getSourceCode(), sourceCode);
assert.strictEqual(2, comments.length);

const foo = getVariable(scope, "foo");
Expand Down Expand Up @@ -8962,6 +8965,7 @@ describe("Linter with FlatConfigArray", () => {

it("should have all the `parent` properties on nodes when the rule visitors are created", () => {
const spy = sinon.spy(context => {
assert.strictEqual(context.getSourceCode(), context.sourceCode);
const ast = context.sourceCode.ast;

assert.strictEqual(ast.body[0].parent, ast);
Expand Down Expand Up @@ -11617,6 +11621,7 @@ describe("Linter with FlatConfigArray", () => {
const sourceCode = context.sourceCode;
const comments = sourceCode.getAllComments();

assert.strictEqual(context.getSourceCode(), sourceCode);
assert.strictEqual(2, comments.length);

const foo = getVariable(scope, "foo");
Expand Down Expand Up @@ -13899,6 +13904,7 @@ var a = "test2";
const sourceCode = context.sourceCode;
const comments = sourceCode.getAllComments();

assert.strictEqual(context.getSourceCode(), sourceCode);
assert.strictEqual(1, comments.length);

const foo = getVariable(scope, "foo");
Expand Down

0 comments on commit f8a6c80

Please sign in to comment.