diff --git a/lib/rules/disallow-unused-variables.js b/lib/rules/disallow-unused-variables.js index a437330ef..8a95108e9 100644 --- a/lib/rules/disallow-unused-variables.js +++ b/lib/rules/disallow-unused-variables.js @@ -67,7 +67,11 @@ module.exports.prototype = { var grandparentElement = node.parentElement.parentElement; return grandparentElement.type !== 'ExportNamedDeclaration'; - } else { + } else if ( + node.parentElement.type === 'VariableDeclarator' || + node.parentElement.type === 'ObjectProperty' || + node.parentElement.isPattern + ) { return parentCheck(node.parentElement); } } else { diff --git a/test/specs/rules/disallow-unused-variables.js b/test/specs/rules/disallow-unused-variables.js index 4f5f60e5b..198957144 100644 --- a/test/specs/rules/disallow-unused-variables.js +++ b/test/specs/rules/disallow-unused-variables.js @@ -30,6 +30,16 @@ describe('rules/disallow-unused-variables', function() { .to.have.no.errors(); }); + it('should not report unused for function expressions', function() { + expect(checker.checkString('var z = function x() { }; z++;')) + .to.have.no.errors(); + }); + + it('should not report unused for function params', function() { + expect(checker.checkString('var z = function x(f) { }; z++;')) + .to.have.no.errors(); + }); + it('should not report unused variable defined with let within a function declaration', function() { expect(checker.checkString('function x() { let y=1; return y; }')) .to.have.no.errors();