Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
disallowUnusedVariables: adapt to cst 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
markelog committed Apr 13, 2016
1 parent 16d861e commit cca477e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
15 changes: 7 additions & 8 deletions lib/rules/disallow-unused-variables.js
Expand Up @@ -73,25 +73,24 @@ module.exports.prototype = {
} else {
return false;
}
}
};

var useVariable = variable.definitions.some(function checkVariableDefinition(definition) {
var useVariable = variable.getDefinitions().some(function checkVariableDefinition(definition) {
return parentCheck(definition.node);
});


return useVariable;
}

function getVariablesInAllScopes(scope) {
var variableList = [];

var iterateChildScopes = function(scope) {
scope.variables.forEach(function(variable) {
scope.getVariables().forEach(function(variable) {
variableList.push(variable);
});

scope.childScopes.forEach(function(childScope){
scope.childScopes.forEach(function(childScope) {
return iterateChildScopes(childScope);
});
};
Expand All @@ -115,8 +114,8 @@ module.exports.prototype = {

// Check if variables are used.
nodesToCheck.reduce(function checkVariableReferences(acc, variable) {
if (variable.references.length === 1) {
variable.definitions.forEach(function addUnusedVariable(definition) {
if (variable.getReferences().length === 1) {
variable.getDefinitions().forEach(function addUnusedVariable(definition) {
acc.push(definition.node);
});
}
Expand All @@ -130,7 +129,7 @@ module.exports.prototype = {
_fix: function(file, error) {
var node = error.element;

while(node.type !== 'VariableDeclaration') {
while (node.type !== 'VariableDeclaration') {
node = node.parentElement;
}

Expand Down
32 changes: 16 additions & 16 deletions test/specs/rules/disallow-unused-variables.js
Expand Up @@ -117,88 +117,88 @@ describe('rules/disallow-unused-variables', function() {

it('should report unused variable defined with var', function() {
expect(checker.checkString('var x=1'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with let', function() {
expect(checker.checkString('let x=1'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with const', function() {
expect(checker.checkString('const x=1'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with var within a function declaration', function() {
expect(checker.checkString('function x() { var y=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used');
});

it('should report unused variable defined with let within a function declaration', function() {
expect(checker.checkString('function x() { let y=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used');
});

it('should report unused variable defined with const within a function declaration', function() {
expect(checker.checkString('function x() { const y=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used');
});

it('should report unused variable defined with var within a arrow function expression', function() {
expect(checker.checkString('() => { var x=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with let within a arrow function expression', function() {
expect(checker.checkString('() => { let x=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with const within a arrow function expression', function() {
expect(checker.checkString('() => { const x=1; }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with var within a function expression', function() {
expect(checker.checkString('(function() { var x=1; })'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with var within a class', function() {
expect(checker.checkString('class P { test() { var x=1; } }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with let within a class', function() {
expect(checker.checkString('class P { test() { let x=1; } }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with const within a class', function() {
expect(checker.checkString('class P { test() { const x=1; } }'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `x` is not used');
});

it('should report unused variable defined with var for a ObjectPattern', function() {
expect(checker.checkString('var { x, y } = 1;'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.have.error.count.equal(2)
.have.error.count.equal(2);
});

it('should report unused variable defined with let for a ObjectPattern', function() {
expect(checker.checkString('let { x, y } = 1;'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.have.error.count.equal(2)
.have.error.count.equal(2);
});

it('should report unused variable defined with const for a ObjectPattern', function() {
expect(checker.checkString('const { x, y } = 1;'))
.to.contain.error('disallowUnusedVariables: Variable `x` is not used')
.to.contain.error('disallowUnusedVariables: Variable `y` is not used')
.have.error.count.equal(2)
.have.error.count.equal(2);
});

reportAndFix({
Expand Down

0 comments on commit cca477e

Please sign in to comment.