Skip to content

Commit

Permalink
Chore: remove internal Linter#getDeclaredVariables method (refs #9161)
Browse files Browse the repository at this point in the history
This updates `Linter` to remove the `Linter#getDeclaredVariables` method. The `context.getDeclaredVariables` method is still available to rules -- this just removes the version of the method on `Linter`.
  • Loading branch information
not-an-aardvark committed Sep 8, 2017
1 parent 40ae27b commit e2f8f64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
25 changes: 1 addition & 24 deletions lib/linter.js
Expand Up @@ -786,7 +786,7 @@ 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: this.getScope.bind(this),
getSourceCode: () => sourceCode,
Expand Down Expand Up @@ -1047,29 +1047,6 @@ 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.
Expand Down
20 changes: 10 additions & 10 deletions tests/lib/ast-utils.js
Expand Up @@ -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);
})
Expand All @@ -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);
})
Expand All @@ -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);
})
Expand All @@ -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);
Expand All @@ -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);
})
Expand Down
22 changes: 11 additions & 11 deletions tests/lib/linter.js
Expand Up @@ -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<Array<string>>} expectedNamesList - An array of expected variable names. The expected variable names is an array of string.
Expand All @@ -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,
Expand Down

0 comments on commit e2f8f64

Please sign in to comment.