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