1 | /**
|
2 | * Disallows space after opening square bracket and before closing.
|
3 | * Reports on all on brackets, even on property accessors.
|
4 | * Use [disallowSpacesInsideArrayBrackets](http://jscs.info/rule/disallowSpacesInsideArrayBrackets.html)
|
5 | * to exclude property accessors.
|
6 | *
|
7 | * Types: `Boolean` or `Object`
|
8 | *
|
9 | * Values: `true` for strict mode, or `"allExcept": [ "[", "]" ]`
|
10 | * ignores closing brackets in a row.
|
11 | *
|
12 | * #### Example
|
13 | *
|
14 | * ```js
|
15 | * "disallowSpacesInsideBrackets": true
|
16 | *
|
17 | * // or
|
18 | *
|
19 | * "disallowSpacesInsideBrackets": {
|
20 | * "allExcept": [ "[", "]", "{", "}" ]
|
21 | * }
|
22 | * ```
|
23 | *
|
24 | * ##### Valid for mode `true`
|
25 | *
|
26 | * ```js
|
27 | * var x = [[1]];
|
28 | * var x = a[1];
|
29 | * ```
|
30 | *
|
31 | * ##### Valid for mode `{ allExcept": [ "[", "]", "{", "}" ] }`
|
32 | *
|
33 | * ```js
|
34 | * var x = [ [1] ];
|
35 | * var x = [ { a: 1 } ];
|
36 | * ```
|
37 | *
|
38 | * ##### Invalid
|
39 | *
|
40 | * ```js
|
41 | * var x = [ [ 1 ] ];
|
42 | * ```
|
43 | */
|
44 |
|
45 | var assert = require('assert');
|
46 |
|
47 | module.exports = function() {};
|
48 |
|
49 | module.exports.prototype = {
|
50 |
|
51 | configure: function(value) {
|
52 | var isObject = typeof value === 'object';
|
53 |
|
54 | var error = this.getOptionName() + ' rule requires string value true or object';
|
55 |
|
56 | if (isObject) {
|
57 | assert('allExcept' in value, error);
|
58 | } else {
|
59 | assert(value === true, error);
|
60 | }
|
61 |
|
62 | this._exceptions = {};
|
63 |
|
64 | if (isObject) {
|
65 | (value.allExcept || []).forEach(function(value) {
|
66 | this._exceptions[value] = true;
|
67 | }, this);
|
68 | }
|
69 | },
|
70 |
|
71 | getOptionName: function() {
|
72 | return 'disallowSpacesInsideBrackets';
|
73 | },
|
74 |
|
75 | check: function(file, errors) {
|
76 | var exceptions = this._exceptions;
|
77 |
|
78 | file.iterateTokensByTypeAndValue('Punctuator', '[', function(token) {
|
79 | var nextToken = file.getNextToken(token, { includeComments: true });
|
80 | var value = nextToken.getSourceCode();
|
81 |
|
82 | if (value in exceptions) {
|
83 | return;
|
84 | }
|
85 |
|
86 | // Skip for empty array brackets
|
87 | if (value === ']') {
|
88 | return;
|
89 | }
|
90 |
|
91 | errors.assert.noWhitespaceBetween({
|
92 | token: token,
|
93 | nextToken: nextToken,
|
94 | message: 'Illegal space after opening bracket'
|
95 | });
|
96 | });
|
97 |
|
98 | file.iterateTokensByTypeAndValue('Punctuator', ']', function(token) {
|
99 | var prevToken = file.getPrevToken(token, { includeComments: true });
|
100 | var value = prevToken.getSourceCode();
|
101 |
|
102 | if (value in exceptions) {
|
103 | return;
|
104 | }
|
105 |
|
106 | // Skip for empty array brackets
|
107 | if (value === '[') {
|
108 | return;
|
109 | }
|
110 |
|
111 | errors.assert.noWhitespaceBetween({
|
112 | token: prevToken,
|
113 | nextToken: token,
|
114 | message: 'Illegal space before closing bracket'
|
115 | });
|
116 | });
|
117 | }
|
118 | };
|