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

Commit e7ca7a0

Browse files
committed
Improve requireMultipleVarDecl rule
Do not treat var and const statements as same entity Fixes #462
1 parent fd36cc9 commit e7ca7a0

2 files changed

Lines changed: 54 additions & 9 deletions

File tree

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ function consecutive(file, errors) {
55
var pos = node.parentCollection.indexOf(node);
66
if (pos < node.parentCollection.length - 1) {
77
var sibling = node.parentCollection[pos + 1];
8-
if (sibling.type === 'VariableDeclaration') {
8+
if (sibling.type === 'VariableDeclaration' && sibling.kind === node.kind) {
99
errors.add(
10-
'Var declarations should be joined',
10+
node.kind[0].toUpperCase() + node.kind.slice(1) + ' declarations should be joined',
1111
sibling.loc.start
1212
);
1313
}
@@ -18,10 +18,12 @@ function consecutive(file, errors) {
1818
function onevar(file, errors) {
1919
file.iterateNodesByType(['Program', 'FunctionDeclaration', 'FunctionExpression'], function(node) {
2020
var firstVar = true;
21+
var firstConst = true;
2122
var firstParent = true;
2223

2324
file.iterate(function(node) {
2425
var type = node && node.type;
26+
var kind = node && node.kind;
2527

2628
// Don't go in nested scopes
2729
if (!firstParent && type && ['FunctionDeclaration', 'FunctionExpression'].indexOf(type) > -1) {
@@ -33,13 +35,26 @@ function onevar(file, errors) {
3335
}
3436

3537
if (type === 'VariableDeclaration') {
36-
if (!firstVar) {
37-
errors.add(
38-
'Var declarations should be joined',
39-
node.loc.start
40-
);
41-
} else {
42-
firstVar = false;
38+
if (kind === 'var') {
39+
if (!firstVar) {
40+
errors.add(
41+
'Var declarations should be joined',
42+
node.loc.start
43+
);
44+
} else {
45+
firstVar = false;
46+
}
47+
}
48+
49+
if (kind === 'const') {
50+
if (!firstConst) {
51+
errors.add(
52+
'Const declarations should be joined',
53+
node.loc.start
54+
);
55+
} else {
56+
firstConst = false;
57+
}
4358
}
4459
}
4560
}, node);

test/rules/require-multiple-var-decl.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('rules/require-multiple-var-decl', function() {
1414
checker.registerDefaultRules();
1515
checker.configure({ requireMultipleVarDecl: true });
1616
});
17+
it('should not report const and var decls as one entity (#462)', function() {
18+
assert(checker.checkString('const a = 1; var b = 2;').isEmpty());
19+
});
1720
it('should report consecutive var decl', function() {
1821
assert(checker.checkString('var x; var y;').getErrorCount() === 1);
1922
});
@@ -32,6 +35,9 @@ describe('rules/require-multiple-var-decl', function() {
3235
checker.configure({ requireMultipleVarDecl: 'onevar' });
3336
});
3437

38+
it('should not report const and var decls as one entity (#462)', function() {
39+
assert(checker.checkString('const a = 1; var b = 2;').isEmpty());
40+
});
3541
it('should report consecutive var decl', function() {
3642
assert(checker.checkString('var x; var y;').getErrorCount() === 1);
3743
});
@@ -51,6 +57,30 @@ describe('rules/require-multiple-var-decl', function() {
5157
}
5258
assert(checker.checkString(test.toString()).getErrorCount() === 1);
5359
});
60+
it('should report multiple const in function', function() {
61+
/* jshint esnext: true */
62+
function test() {
63+
const first = true;
64+
65+
if (true) {
66+
const second = 2;
67+
}
68+
}
69+
assert(checker.checkString(test.toString()).getErrorCount() === 1);
70+
});
71+
it('should report multiple const and vars in function', function() {
72+
/* jshint esnext: true */
73+
function test() {
74+
const firstConst = true;
75+
var firstVar = true;
76+
77+
if (true) {
78+
const secondConst = 2;
79+
var secondVar = 2;
80+
}
81+
}
82+
assert(checker.checkString(test.toString()).getErrorCount() === 2);
83+
});
5484
it('should not confuse two separate functions', function() {
5585
function testFunc() {
5686
function foo() {

0 commit comments

Comments
 (0)