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

Commit

Permalink
requireVarDeclFirst: be aware of the comments
Browse files Browse the repository at this point in the history
In this edge case the comments from previous block scope were being taken and
a false positive error was thrown

Fixes #2022
Closes gh-2061
  • Loading branch information
kepta authored and markelog committed Jan 13, 2016
1 parent 4f72a95 commit c704e9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rules/require-var-decl-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ function getCommentOffsetBetweenNodes(previousNode, currentNode, commentTokens,
break;
}

if (previousNode.range[1] < currentNode.range[0] &&
comment.range[0] > previousNode.range[0] &&
comment.range[1] < previousNode.range[1]) {
// Stop processing comments that are within multiple declarators in a single variable declaration
// of the previous node and the previousNode is not the parent of currentNode
break;
}

if (comment.range[0] > currentNode.range[0] &&
comment.range[1] < currentNode.range[1]) {
// Stop processing comments that are within multiple declarators in a single variable declaration
Expand Down
5 changes: 5 additions & 0 deletions test/specs/rules/require-var-decl-first.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ describe('rules/require-var-decl-first', function() {
testDeclStatements(checker,
'function tst(node) {\nvar array,\n/* array of class names */\nncn = node.className;\n}', 0);
});

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);
});
});
});
});
Expand Down

0 comments on commit c704e9e

Please sign in to comment.