Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: remove internal Linter#getDeclaredVariables method (refs #9161) #9264

Merged
merged 1 commit into from Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 1 addition & 24 deletions lib/linter.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
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