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

Commit c704e9e

Browse files
keptamarkelog
authored andcommitted
requireVarDeclFirst: be aware of the comments
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
1 parent 4f72a95 commit c704e9e

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/rules/require-var-decl-first.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ function getCommentOffsetBetweenNodes(previousNode, currentNode, commentTokens,
180180
break;
181181
}
182182

183+
if (previousNode.range[1] < currentNode.range[0] &&
184+
comment.range[0] > previousNode.range[0] &&
185+
comment.range[1] < previousNode.range[1]) {
186+
// Stop processing comments that are within multiple declarators in a single variable declaration
187+
// of the previous node and the previousNode is not the parent of currentNode
188+
break;
189+
}
190+
183191
if (comment.range[0] > currentNode.range[0] &&
184192
comment.range[1] < currentNode.range[1]) {
185193
// Stop processing comments that are within multiple declarators in a single variable declaration

test/specs/rules/require-var-decl-first.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ describe('rules/require-var-decl-first', function() {
233233
testDeclStatements(checker,
234234
'function tst(node) {\nvar array,\n/* array of class names */\nncn = node.className;\n}', 0);
235235
});
236+
237+
it('should not return errors for multiple var declaration with a comment inside the scope of' +
238+
' first variable declaration', function() {
239+
testDeclStatements(checker, 'var a = function() { var b;\n/*block comment*/\n};\nvar c;', 0);
240+
});
236241
});
237242
});
238243
});

0 commit comments

Comments
 (0)