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
2748var assert = require ( 'assert' ) ;
@@ -31,22 +52,33 @@ module.exports = function() {
3152
3253module . 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 ( {
0 commit comments