1 | /**
|
2 | * Disallows spaces after commas
|
3 | *
|
4 | * Types: `Boolean` or `Object`
|
5 | *
|
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
|
10 | *
|
11 | * #### Example
|
12 | *
|
13 | * ```js
|
14 | * "disallowSpaceAfterComma": true
|
15 | * ```
|
16 | * ```js
|
17 | * "disallowSpaceAfterComma" {"allExcept": ["sparseArrays"]}
|
18 | * ```
|
19 | *
|
20 | * ##### Valid for mode `true`
|
21 | *
|
22 | * ```js
|
23 | * [a,b,c];
|
24 | * ```
|
25 | *
|
26 | * ##### Invalid for mode `true`
|
27 | *
|
28 | * ```js
|
29 | * [a, b, c];
|
30 | * ```
|
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 | * ``
|
46 | */
|
47 |
|
48 | var assert = require('assert');
|
49 |
|
50 | module.exports = function() {
|
51 | };
|
52 |
|
53 | module.exports.prototype = {
|
54 |
|
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 |
|
65 | assert(
|
66 | Array.isArray(options.allExcept),
|
67 | ' property `allExcept` in ' + this.getOptionName() + ' should be an array of strings'
|
68 | );
|
69 | this._exceptSparseArrays = options.allExcept.indexOf('sparseArrays') >= 0;
|
70 | },
|
71 |
|
72 | getOptionName: function() {
|
73 | return 'disallowSpaceAfterComma';
|
74 | },
|
75 |
|
76 | check: function(file, errors) {
|
77 | var exceptSparseArrays = this._exceptSparseArrays;
|
78 | file.iterateTokensByTypeAndValue('Punctuator', ',', function(token) {
|
79 | var nextToken = file.getNextToken(token);
|
80 |
|
81 | if (exceptSparseArrays && nextToken.value === ',') {
|
82 | return;
|
83 | }
|
84 | errors.assert.noWhitespaceBetween({
|
85 | token: token,
|
86 | nextToken: nextToken,
|
87 | message: 'Illegal space after comma'
|
88 | });
|
89 | });
|
90 | }
|
91 |
|
92 | };
|