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-parentheses.js
175 lines (144 loc) · 4.46 KB
/
disallow-spaces-inside-parentheses.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Disallows space after opening round bracket and before closing.
*
* Types: `Boolean` or `Object`
*
* Values: Either `true` or Object with `"only"` property as an array of tokens
*
* #### Example
*
* ```js
* "disallowSpacesInsideParentheses": true
* ```
*
* ##### Valid for `true` value
*
* ```js
* var x = (1 + 2) * 3;
* ```
*
* ##### Valid for `only` value
*
* ```js
* "disallowSpacesInsideParentheses": { "only": [ "{", "}", "\"" ] }
* ```
* ```js
* var x = ( 1 + 2 );
* var x = foo({});
* var x = foo("1");
* ```
*
* ##### Invalid
*
* ```js
* var x = foo( {} );
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(option) {
var isObject = typeof option === 'object';
var error = this.getOptionName() + ' option requires' +
' true or object value with "only" properties ';
// backcompat for 1.10: {all: true} #1027
if (isObject && option.all === true) {
option = true;
}
if (typeof option === 'boolean') {
assert(option === true, error);
} else if (isObject) {
assert('only' in option, error);
} else {
assert(false, error);
}
this._onlySingleQuote = false;
this._onlyDoubleQuote = false;
this._only = null;
if (option.only) {
this._only = {};
(option.only).forEach(function(value) {
if (value === '\'') {
this._onlySingleQuote = true;
}
if (value === '"') {
this._onlyDoubleQuote = true;
}
this._only[value] = true;
}, this);
}
},
getOptionName: function() {
return 'disallowSpacesInsideParentheses';
},
check: function(file, errors) {
var only = this._only;
var singleQuote = this._onlySingleQuote;
var doubleQuote = this._onlyDoubleQuote;
var alreadyAdded = [];
file.iterateTokensByTypeAndValue('Punctuator', '(', function(token) {
var nextToken = file.getNextToken(token, {includeComments: true});
var value = nextToken.getSourceCode();
var shouldReturn = true;
if (doubleQuote && nextToken.type === 'String' && value[0] === '"') {
shouldReturn = false;
}
if (singleQuote && nextToken.type === 'String' && value[0] === '\'') {
shouldReturn = false;
}
if (only && value in only) {
shouldReturn = false;
}
if (!only) {
shouldReturn = false;
}
if (shouldReturn) {
return;
}
var isAdded = errors.assert.noWhitespaceBetween({
token: token,
nextToken: nextToken,
message: 'Illegal space after opening round bracket'
});
if (isAdded) {
alreadyAdded.push(token);
}
});
file.iterateTokensByTypeAndValue('Punctuator', ')', function(token) {
var prevToken = file.getPrevToken(token, {includeComments: true});
var value = prevToken.getSourceCode();
var shouldReturn = true;
// Do not check already found errors, like "function ( ) { ..."
if (alreadyAdded.indexOf(prevToken) > -1) {
return;
}
if (doubleQuote && prevToken.type === 'String' && value[value.length - 1] === '"') {
shouldReturn = false;
}
if (singleQuote && prevToken.type === 'String' && value[value.length - 1] === '\'') {
shouldReturn = false;
}
if (only) {
if (value in only) {
shouldReturn = false;
}
if (
value === ']' &&
prevToken.parentElement.type === 'MemberExpression'
) {
shouldReturn = true;
}
} else {
shouldReturn = false;
}
if (shouldReturn) {
return;
}
errors.assert.noWhitespaceBetween({
token: prevToken,
nextToken: token,
message: 'Illegal space before closing round bracket'
});
});
}
};