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

Commit

Permalink
fixup! requireVarDeclFirst: use CST
Browse files Browse the repository at this point in the history
  • Loading branch information
qfox committed Mar 17, 2016
1 parent 1bc777e commit 2d98cc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
29 changes: 24 additions & 5 deletions lib/rules/require-var-decl-first.js
Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions test/specs/rules/require-var-decl-first.js
Expand Up @@ -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 ' +
Expand Down Expand Up @@ -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 ' +
Expand Down Expand Up @@ -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 ' +
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 2d98cc7

Please sign in to comment.