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 16, 2016
1 parent 8e36051 commit 1bc777e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
22 changes: 12 additions & 10 deletions lib/rules/require-var-decl-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,22 @@

var assert = require('assert');

function isScopeElement(elem) {
return elem.type === 'Program' ||
elem.type === 'BlockStatement' && (
elem.parentElement.type === 'FunctionExpression' ||
elem.parentElement.type === 'FunctionDeclaration' ||
elem.parentElement.type === 'ArrowFunctionExpression'
);
}

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')) {
// We should goes up from blocks for var declaration.
if (elem.type === 'Punctuator' &&
elem.parentElement.type === 'BlockStatement' &&
(elem.parentElement.parentElement.type === 'BlockStatement' ||
elem.parentElement.parentElement.type === 'Program')) {
elem = elem.parentElement.previousSibling;
} else {
elem = elem.previousSibling;
}
elem = elem.previousSibling;
}

if (elem === null) {
Expand Down Expand Up @@ -153,7 +154,8 @@ module.exports.prototype = {
return;
}

if (isVarDeclFirst(varDecl)) {
// Checking scope to not allow vars inside block statements.
if (isScopeElement(varDecl.parentElement) && isVarDeclFirst(varDecl)) {
return;
}

Expand Down
14 changes: 7 additions & 7 deletions test/specs/rules/require-var-decl-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,21 @@ describe('rules/require-var-decl-first', function() {
});

describe('statements inside blocks', function() {
it('should not return error for single var declaration inside block', function() {
testDeclStatements(checker, '{var a;}', 0);
it('should report for single var declaration inside block', function() {
testDeclStatements(checker, '{var a;}', 1);
});

it('should not return errors for single var declaration at top of program scope after prologue',
it('should report for single var declaration at top of program scope after prologue',
function() {
testDeclStatements(checker, '{"use strict";\nvar a;}', 0);
testDeclStatements(checker, '{"use strict";\nvar a;}', 1);
});

it('should not return errors for single var declaration after single comment inside a block',
it('should report for single var declaration after single comment inside a block',
function() {
testDeclStatements(checker, '{/*block comment*/\nvar a;}', 0);
testDeclStatements(checker, '{/*block comment*/\nvar a;}', 1);
});

it('should return error for single var decl inside a block after assignment',
it('should report for single var decl inside a block after assignment',
function() {
testDeclStatements(checker, 'x=1;{var a;}', 1);
});
Expand Down

0 comments on commit 1bc777e

Please sign in to comment.