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

Commit 0ed74ea

Browse files
bjdixonmarkelog
authored andcommitted
disallowSpace(After|Before)Comma: add allExcept: ['sparseArrays']
Fixes #1944 Closes gh-1971
1 parent babc117 commit 0ed74ea

4 files changed

Lines changed: 109 additions & 17 deletions

File tree

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
/**
22
* Disallows spaces after commas
33
*
4-
* Types: `Boolean`
4+
* Types: `Boolean` or `Object`
55
*
6-
* Values: `true` to disallow any spaces after any comma
6+
* Values:
7+
* - `Boolean`: `true` to disallow any spaces after any comma
8+
* - `Object`: `"allExcept"` array of exceptions
9+
* - `"sparseArrays"` to allow spaces in place of absent values in sparse arrays
710
*
811
* #### Example
912
*
1013
* ```js
1114
* "disallowSpaceAfterComma": true
1215
* ```
16+
* ```js
17+
* "disallowSpaceAfterComma" {"allExcept": ["sparseArrays"]}
18+
* ```
1319
*
14-
* ##### Valid
20+
* ##### Valid for mode `true`
1521
*
1622
* ```js
1723
* [a,b,c];
1824
* ```
1925
*
20-
* ##### Invalid
26+
* ##### Invalid for mode `true`
2127
*
2228
* ```js
2329
* [a, b, c];
2430
* ```
31+
* ```js
32+
* [a,b, , ,c];
33+
* ```
34+
*
35+
* ##### Valid for mode `{"allExcept": ["sparseArrays"]}`
36+
*
37+
* ```js
38+
* [a,b, , ,c];
39+
* ```
40+
*
41+
* ##### Invalid for mode `{"allExcept": ["sparseArrays"]}`
42+
*
43+
* ```js
44+
* [a, b, , , c];
45+
* ``
2546
*/
2647

2748
var assert = require('assert');
@@ -31,22 +52,33 @@ module.exports = function() {
3152

3253
module.exports.prototype = {
3354

34-
configure: function(option) {
55+
configure: function(options) {
56+
if (typeof options !== 'object') {
57+
assert(
58+
options === true,
59+
this.getOptionName() + ' option requires true value or an object'
60+
);
61+
var _options = {allExcept: []};
62+
return this.configure(_options);
63+
}
64+
3565
assert(
36-
option === true,
37-
this.getOptionName() + ' option requires true value'
66+
Array.isArray(options.allExcept),
67+
' property `allExcept` in ' + this.getOptionName() + ' should be an array of strings'
3868
);
69+
this._exceptSparseArrays = options.allExcept.indexOf('sparseArrays') >= 0;
3970
},
4071

4172
getOptionName: function() {
4273
return 'disallowSpaceAfterComma';
4374
},
4475

4576
check: function(file, errors) {
77+
var exceptSparseArrays = this._exceptSparseArrays;
4678
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
4779
var nextToken = file.getNextToken(token);
4880

49-
if (nextToken.value === ',') {
81+
if (exceptSparseArrays && nextToken.value === ',') {
5082
return;
5183
}
5284
errors.assert.noWhitespaceBetween({

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
/**
22
* Disallows spaces before commas
33
*
4-
* Types: `Boolean`
4+
* Types: `Boolean` or `Object`
55
*
6-
* Values: `true` to disallow any spaces before any comma
6+
* Values:
7+
* - `Boolean`: `true` to disallow any spaces before any comma
8+
* - `Object`: `"allExcept"` array of exceptions
9+
* - `"sparseArrays"` to allow spaces in place of absent values in sparse arrays
710
*
811
* #### Example
912
*
1013
* ```js
1114
* "disallowSpaceBeforeComma": true
1215
* ```
16+
* ```js
17+
* "disallowSpaceBeforeComma": {"allExcept": ["sparseArrays"]}
18+
* ```
1319
*
14-
* ##### Valid
20+
* ##### Valid for mode `true`
1521
*
1622
* ```js
1723
* var a, b;
1824
* ```
1925
*
20-
* ##### Invalid
26+
* ##### Invalid for mode `true`
2127
*
2228
* ```js
2329
* var a ,b;
2430
* ```
31+
* ```js
32+
* [a, b, , , c]
33+
* ```
34+
* ##### Valid for mode `{"allExcept": ["sparseArrays"]}`
35+
*
36+
* ```js
37+
* [a, b, , , c]
38+
* ```
39+
*
40+
* ##### Invalid for mode `{"allExcept": ["sparseArrays"]}`
41+
*
42+
* ```js
43+
* [a , b , , , c]
44+
* ```
45+
*
2546
*/
2647

2748
var assert = require('assert');
@@ -31,22 +52,33 @@ module.exports = function() {
3152

3253
module.exports.prototype = {
3354

34-
configure: function(option) {
55+
configure: function(options) {
56+
if (typeof options !== 'object') {
57+
assert(
58+
options === true,
59+
this.getOptionName() + ' option requires true value or an object'
60+
);
61+
var _options = {allExcept: []};
62+
return this.configure(_options);
63+
}
64+
3565
assert(
36-
option === true,
37-
this.getOptionName() + ' option requires true value'
66+
Array.isArray(options.allExcept),
67+
' property `allExcept` in ' + this.getOptionName() + ' should be an array of strings'
3868
);
69+
this._exceptSparseArrays = options.allExcept.indexOf('sparseArrays') >= 0;
3970
},
4071

4172
getOptionName: function() {
4273
return 'disallowSpaceBeforeComma';
4374
},
4475

4576
check: function(file, errors) {
77+
var exceptSparseArrays = this._exceptSparseArrays;
4678
file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
4779
var prevToken = file.getPrevToken(token);
4880

49-
if (prevToken.value === ',') {
81+
if (exceptSparseArrays && prevToken.value === ',') {
5082
return;
5183
}
5284
errors.assert.noWhitespaceBetween({

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,23 @@ describe('rules/disallow-space-after-comma', function() {
5151
});
5252

5353
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 in sparse arrays', function() {
58+
expect(checker.checkString('[a, , ,b,c]')).to.have.error.count.equal(2);
59+
});
60+
61+
it('does allow spaces in sparse arrays when excepted', function() {
62+
checker.configure({ disallowSpaceAfterComma: {allExcept: ['sparseArrays']}});
5463
expect(checker.checkString('[a, , ,b,c]')).to.have.no.errors();
5564
});
5665

66+
it('does allow spaces in sparse arrays when excepted but not before values', function() {
67+
checker.configure({ disallowSpaceAfterComma: {allExcept: ['sparseArrays']}});
68+
expect(checker.checkString('[a, , , b, c]')).to.have.error.count.equal(2);
69+
});
70+
5771
it('does not allow spaces after commas in objects', function() {
5872
expect(checker.checkString('var a = {x: 1, y: 2};')).to.have.one.validation.error();
5973
});

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,21 @@ describe('rules/disallow-space-before-comma', function() {
5151
});
5252

5353
it('does allow sparse arrays', function() {
54-
expect(checker.checkString('[a, , , b, c]')).to.have.no.errors();
54+
expect(checker.checkString('[a,,,b,c]')).to.have.no.errors();
55+
});
56+
57+
it('does not allow spaces in sparse arrays', function() {
58+
expect(checker.checkString('[a, , ,b,c]')).to.have.error.count.equal(2);
59+
});
60+
61+
it('does allow spaces in sparse arrays when excepted', function() {
62+
checker.configure({ disallowSpaceBeforeComma: {allExcept: ['sparseArrays']}});
63+
expect(checker.checkString('[a, , ,b,c]')).to.have.no.errors();
64+
});
65+
66+
it('does allow spaces in sparse arrays when excepted but not after values', function() {
67+
checker.configure({ disallowSpaceBeforeComma: {allExcept: ['sparseArrays']}});
68+
expect(checker.checkString('[a , , , b , c]')).to.have.error.count.equal(2);
5569
});
5670

5771
it('does not allow spaces before commas in objects', function() {

0 commit comments

Comments
 (0)