diff --git a/docs/src/extend/custom-rules.md b/docs/src/extend/custom-rules.md index a7701bb45cf..e5d50b4fb94 100644 --- a/docs/src/extend/custom-rules.md +++ b/docs/src/extend/custom-rules.md @@ -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 `` 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.) @@ -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 `` 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)). diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 44ef3232ede..ff8395d2601 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -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); } }), {} diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 2d2c5ea215e..43a74f2579a 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -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); @@ -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"); @@ -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"); @@ -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); @@ -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"); @@ -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");