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

Commit cf60723

Browse files
bjdixonmarkelog
authored andcommitted
Fix: Sparse arrays in rules with spacing and commas
Added tests against sparse arrays and fixes where needed. Ref #1909 Closes gh-1937
1 parent 1788d59 commit cf60723

12 files changed

Lines changed: 164 additions & 26 deletions

lib/rules/disallow-space-after-binary-operators.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ module.exports.prototype = {
7070
// Comma
7171
if (operators[',']) {
7272
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
73+
if (file.getNextToken(token).value === ',') {
74+
return;
75+
}
7376
errors.assert.noWhitespaceBetween({
7477
token: token,
7578
nextToken: file.getNextToken(token),

lib/rules/disallow-space-after-comma.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ module.exports.prototype = {
4646
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
4747
var nextToken = file.getNextToken(token);
4848

49+
if (nextToken.value === ',') {
50+
return;
51+
}
4952
errors.assert.noWhitespaceBetween({
5053
token: token,
5154
nextToken: nextToken,

lib/rules/disallow-space-before-binary-operators.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ module.exports.prototype = {
7171
// Comma
7272
if (operators[',']) {
7373
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
74+
if (file.getPrevToken(token).value === ',') {
75+
return;
76+
}
7477
errors.assert.noWhitespaceBetween({
7578
token: file.getPrevToken(token, {includeComments: true}),
7679
nextToken: token,

lib/rules/disallow-space-before-comma.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ module.exports.prototype = {
4646
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
4747
var prevToken = file.getPrevToken(token);
4848

49+
if (prevToken.value === ',') {
50+
return;
51+
}
4952
errors.assert.noWhitespaceBetween({
5053
token: prevToken,
5154
nextToken: token,

test/specs/rules/disallow-space-after-binary-operators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ describe('rules/disallow-space-after-binary-operators', function() {
110110
expect(errors).to.have.one.validation.error.from('disallowSpaceAfterBinaryOperators');
111111
expect(errors.explainError(error)).to.have.string('Operator = should stick to following expression at input');
112112
});
113+
114+
it('should not report error for sparse arrays', function() {
115+
checker.configure({ disallowSpaceAfterBinaryOperators: [','] });
116+
expect(checker.checkString('var x = [1, , ,2] ,y = 32')).to.have.no.errors();
117+
});
118+
113119
});

test/specs/rules/disallow-space-after-comma.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,71 @@ describe('rules/disallow-space-after-comma', function() {
77
beforeEach(function() {
88
checker = new Checker();
99
checker.registerDefaultRules();
10+
checker.configure({ disallowSpaceAfterComma: true });
1011
});
1112

12-
it('does not allow spaces after commas', function() {
13-
checker.configure({ disallowSpaceAfterComma: true });
13+
it('does not allow spaces after commas in var delarations', function() {
14+
expect(checker.checkString('var a, b;')).to.have.one.validation.error.from('disallowSpaceAfterComma');
15+
});
1416

15-
expect(checker.checkString('[a, b]')).to.have.one.validation.error.from('disallowSpaceAfterComma');
17+
it('does not allow tabs after commas in var delarations', function() {
18+
expect(checker.checkString('var a,\tb;')).to.have.one.validation.error.from('disallowSpaceAfterComma');
1619
});
1720

18-
it('does not allow tabs after commas', function() {
19-
checker.configure({ disallowSpaceAfterComma: true });
21+
it('does allow commas with no spaces in var delarations', function() {
22+
expect(checker.checkString('var a,b,c;')).to.have.no.errors();
23+
});
2024

21-
expect(checker.checkString('[a,\tb]')).to.have.one.validation.error.from('disallowSpaceAfterComma');
25+
it('does allow commas with spaces before in var delarations', function() {
26+
expect(checker.checkString('var a ,b ,c;')).to.have.no.errors();
2227
});
2328

24-
it('does allow commas with no spaces', function() {
25-
checker.configure({ disallowSpaceAfterComma: true });
29+
it('does allow commas with newline character after in var delarations', function() {
30+
expect(checker.checkString('var a,\nb,\nc;')).to.have.no.errors();
31+
});
2632

27-
expect(checker.checkString('[a,b,c]')).to.have.no.errors();
33+
it('does not allow spaces after commas in arrays', function() {
34+
expect(checker.checkString('[a, b]')).to.have.one.validation.error.from('disallowSpaceAfterComma');
2835
});
2936

30-
it('does allow commas with spaces before', function() {
31-
checker.configure({ disallowSpaceAfterComma: true });
37+
it('does not allow tabs after commas in arrays', function() {
38+
expect(checker.checkString('[a,\tb]')).to.have.one.validation.error.from('disallowSpaceAfterComma');
39+
});
3240

33-
expect(checker.checkString('[a ,b ,c]')).to.have.no.errors();
41+
it('does allow commas with no spaces in arrays', function() {
42+
expect(checker.checkString('[a,b,c]')).to.have.no.errors();
3443
});
3544

36-
it('does allow commas with newline character after', function() {
37-
checker.configure({ disallowSpaceAfterComma: true });
45+
it('does allow commas with spaces before in arrays', function() {
46+
expect(checker.checkString('[a ,b ,c]')).to.have.no.errors();
47+
});
3848

49+
it('does allow commas with newline character after in arrays', function() {
3950
expect(checker.checkString('[a,\nb,\nc]')).to.have.no.errors();
4051
});
52+
53+
it('does allow sparse arrays', function() {
54+
expect(checker.checkString('[a, , ,b,c]')).to.have.no.errors();
55+
});
56+
57+
it('does not allow spaces after commas in objects', function() {
58+
expect(checker.checkString('var a = {x: 1, y: 2};')).to.have.one.validation.error();
59+
});
60+
61+
it('does not allow tabs after commas in objects', function() {
62+
expect(checker.checkString('var a = {x: 1,\ty: 2};')).to.have.one.validation.error();
63+
});
64+
65+
it('does allow commas with no spaces in objects', function() {
66+
expect(checker.checkString('var a = {x: 1,y: 2};')).to.have.no.errors();
67+
});
68+
69+
it('does allow commas with spaces before in objects', function() {
70+
expect(checker.checkString('var a = {x: 1 ,y: 2};')).to.have.no.errors();
71+
});
72+
73+
it('does allow commas with newline character after in objects', function() {
74+
expect(checker.checkString('var a = {x: 1,\ny: 2,\nz: 3};')).to.have.no.errors();
75+
});
76+
4177
});

test/specs/rules/disallow-space-before-binary-operators.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ describe('rules/disallow-space-before-binary-operators', function() {
101101
expect(checker.checkString('var x = [1, 2]\n , y = 32')).to.have.no.errors();
102102
});
103103

104+
it('should not report error for sparse arrays', function() {
105+
checker.configure({ disallowSpaceBeforeBinaryOperators: [','] });
106+
expect(checker.checkString('var x = [1, , , 2], y = 32')).to.have.no.errors();
107+
});
108+
104109
it('should not report error if a comment is ahead of the comma', function() {
105110
checker.configure({ disallowSpaceBeforeBinaryOperators: [','] });
106111
expect(checker.checkString('var x = [1, 2] /* test*/, y = 32')).to.have.no.errors();

test/specs/rules/disallow-space-before-comma.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,71 @@ describe('rules/disallow-space-before-comma', function() {
77
beforeEach(function() {
88
checker = new Checker();
99
checker.registerDefaultRules();
10-
});
11-
12-
it('does not allow spaces before commas', function() {
1310
checker.configure({ disallowSpaceBeforeComma: true });
11+
});
1412

13+
it('does not allow spaces before commas in var declaration', function() {
1514
expect(checker.checkString('var a ,b;')).to.have.one.validation.error.from('disallowSpaceBeforeComma');
1615
});
1716

18-
it('does not allow tabs before commas', function() {
19-
checker.configure({ disallowSpaceBeforeComma: true });
17+
it('does allow commas with spaces after in var delcaration', function() {
18+
expect(checker.checkString('var a, b, c;')).to.have.no.errors();
19+
});
2020

21+
it('does not allow tabs before commas in var declaration', function() {
2122
expect(checker.checkString('var a\t,b;')).to.have.one.validation.error.from('disallowSpaceBeforeComma');
2223
});
2324

24-
it('does allow commas with no spaces', function() {
25-
checker.configure({ disallowSpaceBeforeComma: true });
26-
25+
it('does allow commas with no spaces in var declaration', function() {
2726
expect(checker.checkString('var a,b;')).to.have.no.errors();
2827
});
2928

30-
it('does allow commas with spaces after', function() {
31-
checker.configure({ disallowSpaceBeforeComma: true });
29+
it('does allow commas with newline character before in var delcaration', function() {
30+
expect(checker.checkString('var a\n,b\n,c;')).to.have.no.errors();
31+
});
32+
33+
it('does not allow spaces before commas in arrays', function() {
34+
expect(checker.checkString('[a ,b]')).to.have.one.validation.error.from('disallowSpaceBeforeComma');
35+
});
3236

37+
it('does allow commas with spaces after in arrays', function() {
3338
expect(checker.checkString('[a, b, c]')).to.have.no.errors();
3439
});
3540

36-
it('does allow commas with newline character before', function() {
37-
checker.configure({ disallowSpaceBeforeComma: true });
41+
it('does not allow tabs before commas in arrays', function() {
42+
expect(checker.checkString('[a\t,b]')).to.have.one.validation.error.from('disallowSpaceBeforeComma');
43+
});
44+
45+
it('does allow commas with no spaces in arrays', function() {
46+
expect(checker.checkString('[a,b]')).to.have.no.errors();
47+
});
3848

49+
it('does allow commas with newline character before in arrays', function() {
3950
expect(checker.checkString('[a\n,b\n,c]')).to.have.no.errors();
4051
});
52+
53+
it('does allow sparse arrays', function() {
54+
expect(checker.checkString('[a, , , b, c]')).to.have.no.errors();
55+
});
56+
57+
it('does not allow spaces before commas in objects', function() {
58+
expect(checker.checkString('var a = {x: 1 ,y: 2};')).to.have.one.validation.error();
59+
});
60+
61+
it('does allow commas with spaces after in objects', function() {
62+
expect(checker.checkString('var a = {x: 1, y: 2};')).to.have.no.errors();
63+
});
64+
65+
it('does not allow tabs before commas in objects', function() {
66+
expect(checker.checkString('var a = {x: 1\t,y: 2};')).to.have.one.validation.error();
67+
});
68+
69+
it('does allow commas with no spaces in objects', function() {
70+
expect(checker.checkString('var a = {x: 1,y: 2};')).to.have.no.errors();
71+
});
72+
73+
it('does allow commas with newline character before in objects', function() {
74+
expect(checker.checkString('var a = {x: 1\n,y: 2\n, z: 3};')).to.have.no.errors();
75+
});
76+
4177
});

test/specs/rules/require-space-after-binary-operators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@ describe('rules/require-space-after-binary-operators', function() {
123123
expect(errors.explainError(error))
124124
.to.have.string('Operator = should not stick to following expression at input');
125125
});
126+
127+
it('should not report error for sparse arrays', function() {
128+
checker.configure({ requireSpaceAfterBinaryOperators: [','] });
129+
expect(checker.checkString('var x = [1, , , 2], y = 32')).to.have.no.errors();
130+
});
131+
126132
});

test/specs/rules/require-space-after-comma.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ describe('rules/require-space-after-comma', function() {
2727
expect(checker.checkString('var a, b;')).to.have.no.errors();
2828
});
2929

30+
it('should allow tab after comma in var declaration', function() {
31+
expect(checker.checkString('var a,\tb;')).to.have.no.errors();
32+
});
33+
3034
it('should allow space after and before comma in var declaration', function() {
3135
expect(checker.checkString('var a , b;')).to.have.no.errors();
3236
});
@@ -47,6 +51,10 @@ describe('rules/require-space-after-comma', function() {
4751
expect(checker.checkString('var a = [1, 2, 3, 4];')).to.have.no.errors();
4852
});
4953

54+
it('should allow tab after comma in arrays', function() {
55+
expect(checker.checkString('var a = [1,\t2,\t3,\t4];')).to.have.no.errors();
56+
});
57+
5058
it('should allow space after and before comma in arrays', function() {
5159
expect(checker.checkString('var a = [1 , 2 , 3 , 4];')).to.have.no.errors();
5260
});
@@ -59,6 +67,10 @@ describe('rules/require-space-after-comma', function() {
5967
expect(checker.checkString('var a = [1,\n2,\n3];')).to.have.no.errors();
6068
});
6169

70+
it('should allow sparse arrays', function() {
71+
expect(checker.checkString('[1, , , 2, 3];')).to.have.no.errors();
72+
});
73+
6274
it('should report errors when no space is given in objects', function() {
6375
expect(checker.checkString('var a = {x:1,y:2,z:3};')).to.have.error.count.equal(2);
6476
});
@@ -75,6 +87,10 @@ describe('rules/require-space-after-comma', function() {
7587
expect(checker.checkString('var a = {x: 1, y: 2, z: 3};')).to.have.no.errors();
7688
});
7789

90+
it('should allow tab after comma in objects', function() {
91+
expect(checker.checkString('var a = {x: 1,\ty: 2,\tz: 3};')).to.have.no.errors();
92+
});
93+
7894
it('should allow space after and before comma in objects', function() {
7995
expect(checker.checkString('var a = {x: 1 , y: 2 , z: 3};')).to.have.no.errors();
8096
});

0 commit comments

Comments
 (0)