diff --git a/lib/linter.js b/lib/linter.js index b9d4b0d5528..4d5a2f22984 100755 --- a/lib/linter.js +++ b/lib/linter.js @@ -823,7 +823,7 @@ module.exports = class Linter { Object.create(BASE_TRAVERSAL_CONTEXT), { getAncestors: () => this.traverser.parents(), - getDeclaredVariables: this.getDeclaredVariables.bind(this), + getDeclaredVariables: node => this.scopeManager && this.scopeManager.getDeclaredVariables(node) || [], getFilename: () => filename, getScope: () => getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion), getSourceCode: () => sourceCode, @@ -1040,29 +1040,6 @@ module.exports = class Linter { return this.rules.getAllLoadedRules(); } - /** - * Gets variables that are declared by a specified node. - * - * The variables are its `defs[].node` or `defs[].parent` is same as the specified node. - * Specifically, below: - * - * - `VariableDeclaration` - variables of its all declarators. - * - `VariableDeclarator` - variables. - * - `FunctionDeclaration`/`FunctionExpression` - its function name and parameters. - * - `ArrowFunctionExpression` - its parameters. - * - `ClassDeclaration`/`ClassExpression` - its class name. - * - `CatchClause` - variables of its exception. - * - `ImportDeclaration` - variables of its all specifiers. - * - `ImportSpecifier`/`ImportDefaultSpecifier`/`ImportNamespaceSpecifier` - a variable. - * - others - always an empty array. - * - * @param {ASTNode} node A node to get. - * @returns {eslint-scope.Variable[]} Variables that are declared by the node. - */ - getDeclaredVariables(node) { - return (this.scopeManager && this.scopeManager.getDeclaredVariables(node)) || []; - } - /** * Performs multiple autofix passes over the text until as many fixes as possible * have been applied. diff --git a/tests/lib/ast-utils.js b/tests/lib/ast-utils.js index 93ad7be48c0..085bd35c0e0 100644 --- a/tests/lib/ast-utils.js +++ b/tests/lib/ast-utils.js @@ -117,9 +117,9 @@ describe("ast-utils", () => { // catch it("should return true if reference is assigned for catch", () => { - linter.defineRule("checker", mustCall(() => ({ + linter.defineRule("checker", mustCall(context => ({ CatchClause: mustCall(node => { - const variables = linter.getDeclaredVariables(node); + const variables = context.getDeclaredVariables(node); assert.lengthOf(astUtils.getModifyingReferences(variables[0].references), 1); }) @@ -130,9 +130,9 @@ describe("ast-utils", () => { // const it("should return true if reference is assigned for const", () => { - linter.defineRule("checker", mustCall(() => ({ + linter.defineRule("checker", mustCall(context => ({ VariableDeclaration: mustCall(node => { - const variables = linter.getDeclaredVariables(node); + const variables = context.getDeclaredVariables(node); assert.lengthOf(astUtils.getModifyingReferences(variables[0].references), 1); }) @@ -142,9 +142,9 @@ describe("ast-utils", () => { }); it("should return false if reference is not assigned for const", () => { - linter.defineRule("checker", mustCall(() => ({ + linter.defineRule("checker", mustCall(context => ({ VariableDeclaration: mustCall(node => { - const variables = linter.getDeclaredVariables(node); + const variables = context.getDeclaredVariables(node); assert.lengthOf(astUtils.getModifyingReferences(variables[0].references), 0); }) @@ -155,9 +155,9 @@ describe("ast-utils", () => { // class it("should return true if reference is assigned for class", () => { - linter.defineRule("checker", mustCall(() => ({ + linter.defineRule("checker", mustCall(context => ({ ClassDeclaration: mustCall(node => { - const variables = linter.getDeclaredVariables(node); + const variables = context.getDeclaredVariables(node); assert.lengthOf(astUtils.getModifyingReferences(variables[0].references), 1); assert.lengthOf(astUtils.getModifyingReferences(variables[1].references), 0); @@ -168,9 +168,9 @@ describe("ast-utils", () => { }); it("should return false if reference is not assigned for class", () => { - linter.defineRule("checker", mustCall(() => ({ + linter.defineRule("checker", mustCall(context => ({ ClassDeclaration: mustCall(node => { - const variables = linter.getDeclaredVariables(node); + const variables = context.getDeclaredVariables(node); assert.lengthOf(astUtils.getModifyingReferences(variables[0].references), 0); }) diff --git a/tests/lib/linter.js b/tests/lib/linter.js index 4a2e124cacd..dd8201c8a91 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -3334,19 +3334,10 @@ describe("Linter", () => { }); }); - describe("getDeclaredVariables(node)", () => { + describe("context.getDeclaredVariables(node)", () => { /** - * Assert `eslint.getDeclaredVariables(node)` is empty. - * @param {ASTNode} node - A node to check. - * @returns {void} - */ - function checkEmpty(node) { - assert.equal(0, linter.getDeclaredVariables(node).length); - } - - /** - * Assert `eslint.getDeclaredVariables(node)` is valid. + * Assert `context.getDeclaredVariables(node)` is valid. * @param {string} code - A code to check. * @param {string} type - A type string of ASTNode. This method checks variables on the node of the type. * @param {Array>} expectedNamesList - An array of expected variable names. The expected variable names is an array of string. @@ -3355,6 +3346,15 @@ describe("Linter", () => { function verify(code, type, expectedNamesList) { linter.defineRules({ test(context) { + + /** + * Assert `context.getDeclaredVariables(node)` is empty. + * @param {ASTNode} node - A node to check. + * @returns {void} + */ + function checkEmpty(node) { + assert.equal(0, context.getDeclaredVariables(node).length); + } const rule = { Program: checkEmpty, EmptyStatement: checkEmpty,