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

Commit 706495b

Browse files
bjdixonmarkelog
authored andcommitted
Update: { allExcept: ['trailing'] } for "requireSpaceAfterComma"
New rule value: `{ allExcept: ['trailing'] }` will allow commas to be followed by either `}` or `]` as an alternative to a space Fixes #1915 Closes gh-1920
1 parent 8eb92b6 commit 706495b

2 files changed

Lines changed: 197 additions & 51 deletions

File tree

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
11
/**
22
* Requires space after comma
33
*
4-
* Types: `Boolean`
4+
* Types: `Boolean`, or `Object`
55
*
6-
* Values: `true` to require a space after any comma
6+
* Values:
7+
* - `Boolean`: `true` to require a space after any comma
8+
* - `Object`:
9+
* - `"allExcept"` array of exceptions:
10+
* - `"trailing"` ignore trailing commas
711
*
812
* #### Example
913
*
1014
* ```js
1115
* "requireSpaceAfterComma": true
1216
* ```
17+
* ```
18+
* "requireSpaceAfterComma": {"allExcept": ["trailing"]}
19+
* ```
1320
*
14-
* ##### Valid
21+
* ##### Valid for mode `true`
1522
*
1623
* ```js
1724
* var a, b;
1825
* ```
1926
*
20-
* ##### Invalid
27+
* ##### Invalid for mode `true`
2128
*
2229
* ```js
2330
* var a,b;
2431
* ```
32+
*
33+
*##### Valid for mode `{"allExcept": ["trailing"]}`
34+
*
35+
* ```js
36+
* var a = [1, 2,];
37+
* ```
38+
*
39+
* ##### Invalid for mode `{"allExcept": ["trailing"]}`
40+
*
41+
* ```js
42+
* var a = [a,b,];
43+
* ```
44+
*
2545
*/
2646

2747
var assert = require('assert');
@@ -31,19 +51,33 @@ module.exports = function() {
3151

3252
module.exports.prototype = {
3353

34-
configure: function(option) {
54+
configure: function(options) {
55+
if (typeof options !== 'object') {
56+
assert(
57+
options === true,
58+
this.getOptionName() + ' option requires true value or an object'
59+
);
60+
var _options = {allExcept: []};
61+
return this.configure(_options);
62+
}
63+
3564
assert(
36-
option === true,
37-
this.getOptionName() + ' option requires true value'
65+
Array.isArray(options.allExcept),
66+
' property `allExcept` in ' + this.getOptionName() + ' should be an array of strings'
3867
);
68+
this._exceptTrailingCommas = options.allExcept.indexOf('trailing') >= 0;
3969
},
4070

4171
getOptionName: function() {
4272
return 'requireSpaceAfterComma';
4373
},
4474

4575
check: function(file, errors) {
76+
var exceptTrailingCommas = this._exceptTrailingCommas;
4677
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
78+
if (exceptTrailingCommas && [']', '}'].indexOf(file.getNextToken(token).value) >= 0) {
79+
return;
80+
}
4781
errors.assert.whitespaceBetween({
4882
token: token,
4983
nextToken: file.getNextToken(token),

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

Lines changed: 156 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,179 @@ describe('rules/require-space-after-comma', function() {
77
beforeEach(function() {
88
checker = new Checker();
99
checker.registerDefaultRules();
10-
checker.configure({ requireSpaceAfterComma: true });
1110
});
1211

13-
it('should report error when no space is given', function() {
14-
expect(checker.checkString('var a,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
15-
});
12+
describe('option value true', function() {
1613

17-
it('should report error when space is given before but not after comma', function() {
18-
expect(checker.checkString('var a ,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
19-
});
14+
beforeEach(function() {
15+
checker.configure({ requireSpaceAfterComma: true });
16+
});
2017

21-
it('should allow space after comma in var declaration', function() {
22-
expect(checker.checkString('var a, b;')).to.have.no.errors();
23-
});
18+
it('should report error when no space is given', function() {
19+
expect(checker.checkString('var a,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
20+
});
2421

25-
it('should allow space after and before comma in var declaration', function() {
26-
expect(checker.checkString('var a , b;')).to.have.no.errors();
27-
});
22+
it('should report error when space is given before but not after comma', function() {
23+
expect(checker.checkString('var a ,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
24+
});
2825

29-
it('should allow new line after comma in var declaration', function() {
30-
expect(checker.checkString('var a,\nb,\nc;')).to.have.no.errors();
31-
});
26+
it('should allow space after comma in var declaration', function() {
27+
expect(checker.checkString('var a, b;')).to.have.no.errors();
28+
});
3229

33-
it('should report errors when no space is given in arrays', function() {
34-
expect(checker.checkString('var a = [1,2,3,4];')).to.have.error.count.equal(3);
35-
});
30+
it('should allow space after and before comma in var declaration', function() {
31+
expect(checker.checkString('var a , b;')).to.have.no.errors();
32+
});
3633

37-
it('should report errors when space is given before but not after commas in arrays', function() {
38-
expect(checker.checkString('var a = [1 ,2 ,3 ,4];')).to.have.error.count.equal(3);
39-
});
34+
it('should allow new line after comma in var declaration', function() {
35+
expect(checker.checkString('var a,\nb,\nc;')).to.have.no.errors();
36+
});
4037

41-
it('should allow space after comma in arrays', function() {
42-
expect(checker.checkString('var a = [1, 2, 3, 4];')).to.have.no.errors();
43-
});
38+
it('should report errors when no space is given in arrays', function() {
39+
expect(checker.checkString('var a = [1,2,3,4];')).to.have.error.count.equal(3);
40+
});
4441

45-
it('should allow space after and before comma in arrays', function() {
46-
expect(checker.checkString('var a = [1 , 2 , 3 , 4];')).to.have.no.errors();
47-
});
42+
it('should report errors when space is given before but not after commas in arrays', function() {
43+
expect(checker.checkString('var a = [1 ,2 ,3 ,4];')).to.have.error.count.equal(3);
44+
});
4845

49-
it('should allow new line after comma in arrays', function() {
50-
expect(checker.checkString('var a = [1,\n2,\n3];')).to.have.no.errors();
51-
});
46+
it('should allow space after comma in arrays', function() {
47+
expect(checker.checkString('var a = [1, 2, 3, 4];')).to.have.no.errors();
48+
});
5249

53-
it('should report errors when no space is given in objects', function() {
54-
expect(checker.checkString('var a = {x:1,y:2,z:3};')).to.have.error.count.equal(2);
55-
});
50+
it('should allow space after and before comma in arrays', function() {
51+
expect(checker.checkString('var a = [1 , 2 , 3 , 4];')).to.have.no.errors();
52+
});
5653

57-
it('should report errors when space is given before but not after commas in objects', function() {
58-
expect(checker.checkString('var a = {x:1 ,y:2 ,z:3};')).to.have.error.count.equal(2);
59-
});
54+
it('should report error when no space is given after trailing comma in array', function() {
55+
expect(checker.checkString('var a = [1, 2, 3, 4,];')).to.have.error.count.equal(1);
56+
});
6057

61-
it('should allow space after comma in objects', function() {
62-
expect(checker.checkString('var a = {x: 1, y: 2, z: 3};')).to.have.no.errors();
63-
});
58+
it('should allow new line after comma in arrays', function() {
59+
expect(checker.checkString('var a = [1,\n2,\n3];')).to.have.no.errors();
60+
});
61+
62+
it('should report errors when no space is given in objects', function() {
63+
expect(checker.checkString('var a = {x:1,y:2,z:3};')).to.have.error.count.equal(2);
64+
});
65+
66+
it('should report error when no space is given after trailing comma in object', function() {
67+
expect(checker.checkString('var a = {x:1, y:2, z:3,};')).to.have.error.count.equal(1);
68+
});
69+
70+
it('should report errors when space is given before but not after commas in objects', function() {
71+
expect(checker.checkString('var a = {x:1 ,y:2 ,z:3};')).to.have.error.count.equal(2);
72+
});
73+
74+
it('should allow space after comma in objects', function() {
75+
expect(checker.checkString('var a = {x: 1, y: 2, z: 3};')).to.have.no.errors();
76+
});
77+
78+
it('should allow space after and before comma in objects', function() {
79+
expect(checker.checkString('var a = {x: 1 , y: 2 , z: 3};')).to.have.no.errors();
80+
});
81+
82+
it('should allow new line after comma in objects', function() {
83+
expect(checker.checkString('var a = {x: 1,\ny: 2,\nz: 3};')).to.have.no.errors();
84+
});
85+
86+
it('should report errors when no space is given after trailing comma in object in array', function() {
87+
expect(checker.checkString('var a = [{a:1, b:2,}, {c:3, d:4,},];')).to.have.error.count.equal(3);
88+
});
6489

65-
it('should allow space after and before comma in objects', function() {
66-
expect(checker.checkString('var a = {x: 1 , y: 2 , z: 3};')).to.have.no.errors();
6790
});
6891

69-
it('should allow new line after comma in objects', function() {
70-
expect(checker.checkString('var a = {x: 1,\ny: 2,\nz: 3};')).to.have.no.errors();
92+
describe('option value "exceptTrailingCommas"', function() {
93+
94+
beforeEach(function() {
95+
checker.configure({ requireSpaceAfterComma: { allExcept: ['trailing'] } });
96+
});
97+
98+
it('should report error when no space is given', function() {
99+
expect(checker.checkString('var a,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
100+
});
101+
102+
it('should report error when space is given before but not after comma', function() {
103+
expect(checker.checkString('var a ,b;')).to.have.one.validation.error.from('requireSpaceAfterComma');
104+
});
105+
106+
it('should allow space after comma in var declaration', function() {
107+
expect(checker.checkString('var a, b;')).to.have.no.errors();
108+
});
109+
110+
it('should allow space after and before comma in var declaration', function() {
111+
expect(checker.checkString('var a , b;')).to.have.no.errors();
112+
});
113+
114+
it('should allow new line after comma in var declaration', function() {
115+
expect(checker.checkString('var a,\nb,\nc;')).to.have.no.errors();
116+
});
117+
118+
it('should report errors when no space is given in arrays', function() {
119+
expect(checker.checkString('var a = [1,2,3,4];')).to.have.error.count.equal(3);
120+
});
121+
122+
it('should report errors when space is given before but not after commas in arrays', function() {
123+
expect(checker.checkString('var a = [1 ,2 ,3 ,4];')).to.have.error.count.equal(3);
124+
});
125+
126+
it('should allow space after comma in arrays', function() {
127+
expect(checker.checkString('var a = [1, 2, 3, 4];')).to.have.no.errors();
128+
});
129+
130+
it('should allow space after and before comma in arrays', function() {
131+
expect(checker.checkString('var a = [1 , 2 , 3 , 4];')).to.have.no.errors();
132+
});
133+
134+
it('should allow space after trailing comma in arrays', function() {
135+
expect(checker.checkString('var a = [1, 2, 3, 4, ];')).to.have.no.errors();
136+
});
137+
138+
it('should allow no space after trailing comma in arrays', function() {
139+
expect(checker.checkString('var a = [1, 2, 3, 4,];')).to.have.no.errors();
140+
});
141+
142+
it('should allow new line after comma in arrays', function() {
143+
expect(checker.checkString('var a = [1,\n2,\n3];')).to.have.no.errors();
144+
});
145+
146+
it('should report errors when no space is given in objects', function() {
147+
expect(checker.checkString('var a = {x:1,y:2,z:3};')).to.have.error.count.equal(2);
148+
});
149+
150+
it('should allow when no space is given after trailing comma in object', function() {
151+
expect(checker.checkString('var a = {x:1, y:2, z:3,};')).to.have.no.errors();
152+
});
153+
154+
it('should report errors when space is given before but not after commas in objects', function() {
155+
expect(checker.checkString('var a = {x:1 ,y:2 ,z:3};')).to.have.error.count.equal(2);
156+
});
157+
158+
it('should allow space after comma in objects', function() {
159+
expect(checker.checkString('var a = {x: 1, y: 2, z: 3};')).to.have.no.errors();
160+
});
161+
162+
it('should allow space after and before comma in objects', function() {
163+
expect(checker.checkString('var a = {x: 1 , y: 2 , z: 3};')).to.have.no.errors();
164+
});
165+
166+
it('should allow new line after comma in objects', function() {
167+
expect(checker.checkString('var a = {x: 1,\ny: 2,\nz: 3};')).to.have.no.errors();
168+
});
169+
170+
it('should allow when no space is given after trailing comma in object in array', function() {
171+
expect(checker.checkString('var a = [{a:1, b:2,}, {c:3, d:4,},];')).to.have.no.errors();
172+
});
173+
71174
});
72175

176+
describe('incorrect configuration', function() {
177+
178+
it('should not accept options without a valid key', function() {
179+
expect(function() {
180+
checker.configure({ requireSpaceAfterComma: {} });
181+
}).to.throw('AssertionError');
182+
});
183+
184+
});
73185
});

0 commit comments

Comments
 (0)