Skip to content

Commit

Permalink
Fix: api.markVariableAsUsed should check Node.js scope (fixes eslint#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Mar 18, 2015
1 parent bfa7d08 commit 47fdfa6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,21 +994,32 @@ module.exports = (function() {
*/
api.markVariableAsUsed = function(name) {
var scope = this.getScope(),
variables,
i,
len;

do {
variables = scope.variables;
for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
variables[i].eslintUsed = true;
return true;
found = false;

function markVariableAsUsedInScope(currentScope) {
var variables,
i,
len;
do {
variables = currentScope.variables;
for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
variables[i].eslintUsed = true;
return true;
}
}
}
} while ( (scope = scope.upper) );
} while ( (currentScope = currentScope.upper) );

return false;
}

found = markVariableAsUsedInScope(scope);

if (!found && scope.type === "global" && scope.childScopes[0]) {
found = markVariableAsUsedInScope(scope.childScopes[0]);
}

return false;
return found;
};

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ var TEST_CODE = "var answer = 6 * 7;",

function getVariable(scope, name) {
var variable = null;

scope.variables.some(function(v) {
if (v.name === name) {
variable = v;
return true;
}
return false;
});

if (!variable && scope.type === "global" && scope.childScopes[0]) {
variable = getVariable(scope.childScopes[0], name);
}

return variable;
}

Expand Down Expand Up @@ -908,6 +914,23 @@ describe("eslint", function() {

eslint.verify(code, {}, filename, true);
});
it("should mark variables in Node.js environment as used", function() {
var code = "var a = 1, b = 2;";

eslint.reset();
eslint.on("Program:exit", function() {
var scope;

eslint.markVariableAsUsed("a");

scope = eslint.getScope();

assert.isTrue(getVariable(scope, "a").eslintUsed);
assert.notOk(getVariable(scope, "b").eslintUsed);
});

eslint.verify(code, { env: { node: true }}, filename, true);
});
});

describe("when calling report", function() {
Expand Down

0 comments on commit 47fdfa6

Please sign in to comment.