From 2d98cc71a2acce9153c8e666b6808d0fe16c91ba Mon Sep 17 00:00:00 2001 From: Alexey Yaroshevich Date: Thu, 17 Mar 2016 03:16:24 +0300 Subject: [PATCH] fixup! requireVarDeclFirst: use CST --- lib/rules/require-var-decl-first.js | 29 ++++++++++++++++++---- test/specs/rules/require-var-decl-first.js | 12 ++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/rules/require-var-decl-first.js b/lib/rules/require-var-decl-first.js index d4b1755c0..c2e542cf3 100644 --- a/lib/rules/require-var-decl-first.js +++ b/lib/rules/require-var-decl-first.js @@ -117,12 +117,31 @@ function isScopeElement(elem) { ); } +/** + * Checks for allowed elements before variable declaration + * @param {Object} elem + * @returns {Boolean} + */ +function isRelevantElement(elem) { + // Allow comments and whitespaces. + var itIs = elem.isComment || elem.isWhitespace; + + // Variable declaration itself. + itIs = itIs || elem.type === 'VariableDeclaration'; + + // For '{' in `function() { var a; }`. + itIs = itIs || elem.type === 'Punctuator'; + + // Skip 'use strict' statements at the beginning. + itIs = itIs || elem.type === 'ExpressionStatement' && elem.expression.value === 'use strict'; + + return itIs; +} + function isVarDeclFirst(varDecl) { - var elem = varDecl.previousSibling; - while (elem && (elem.isComment || elem.isWhitespace || - elem.type === 'VariableDeclaration' || - elem.type === 'Punctuator' || - elem.type === 'ExpressionStatement' && elem.expression.value === 'use strict')) { + var elem = varDecl; + // All elements should be relevant. + while (elem && (elem.isComment || elem.isWhitespace || isRelevantElement(elem))) { elem = elem.previousSibling; } diff --git a/test/specs/rules/require-var-decl-first.js b/test/specs/rules/require-var-decl-first.js index 130d24d7d..af38087a8 100644 --- a/test/specs/rules/require-var-decl-first.js +++ b/test/specs/rules/require-var-decl-first.js @@ -103,17 +103,17 @@ describe('rules/require-var-decl-first', function() { }); it('should not return errors for multiple var declaration at top of program scope', function() { - testDeclStatements(checker, 'var a;var b;', 0); + testDeclStatements(checker, 'var a, c;var b;', 0); }); it('should not return errors for multiple var declaration sandwiching a single comment ' + 'at top of program scope', function() { - testDeclStatements(checker, 'var a;/*block comment*/var b;', 0); + testDeclStatements(checker, 'var a, c;/*block comment*/var b;', 0); }); it('should not return errors for multiple var declaration with a single comment before the ' + 'first var decl at top of program scope', function() { - testDeclStatements(checker, 'var a;/*block comment*/var b;/*block comment 2*/var c;', 0); + testDeclStatements(checker, 'var a, d;/*block comment*/var b;/*block comment 2*/var c;', 0); }); it('should not return errors for a single var declaration with comments sandwiched ' + @@ -181,7 +181,7 @@ describe('rules/require-var-decl-first', function() { }); it('should not return errors for multiple var declaration at top of program scope', function() { - testDeclStatements(checker, 'var a; var b;', 0); + testDeclStatements(checker, 'var a, A; var b;', 0); }); it('should not return errors for a single var declaration with comments sandwiched ' + @@ -222,7 +222,7 @@ describe('rules/require-var-decl-first', function() { }); it('should not return errors for multiple var declaration at top of program scope', function() { - testDeclStatements(checker, 'var a;\nvar b;', 0); + testDeclStatements(checker, 'var a, A;\nvar b;', 0); }); it('should not return errors for a single var declaration with comments sandwiched ' + @@ -233,7 +233,7 @@ describe('rules/require-var-decl-first', function() { it('should not return errors for multiple var declaration with a comment inside the scope of' + ' first variable declaration', function() { - testDeclStatements(checker, 'var a = function() { var b;\n/*block comment*/\n};\nvar c;', 0); + testDeclStatements(checker, 'var a = function() { var b;\n/*block comment*/\n}, A;\nvar c;', 0); }); });