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-parenthesized-expression.js
124 lines (107 loc) · 3.41 KB
/
disallow-spaces-inside-parenthesized-expression.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
/**
* Disallows space after opening and before closing grouping parentheses.
*
* Types: `Boolean` or `Object`
*
* Values:
* - `true`: always disallow spaces inside grouping parentheses
* - `Object`:
* - `"allExcept"`: `[ "{", "}", "function" ]` Ignore parenthesized objects and functions
*
* #### Example
*
* ```js
* "disallowSpacesInsideParenthesizedExpression": true
*
* // or
*
* "disallowSpacesInsideParenthesizedExpression": {
* "allExcept": [ "{", "}" ]
* }
* ```
*
* ##### Valid for mode `true`
*
* ```js
* var x = (1 + obj.size) * (2);
* ```
*
* ##### Valid for mode `{ allExcept": [ "{", "}", "function" ] }`
*
* ```js
* var x = (options || { x: true } ).x;
* var global = ( function() { return this; } )();
* ```
*
* ##### Invalid
*
* ```js
* var x = ( 1 + obj.size ) * ( 2 );
* ```
*/
var assert = require('assert');
var TokenCategorizer = require('../token-categorizer');
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 'disallowSpacesInsideParenthesizedExpression';
},
check: function(file, errors) {
var exceptions = this._exceptions;
file.iterateTokensByTypeAndValue('Punctuator', '(', function(token) {
var nextToken = file.getNextToken(token, {includeComments: true});
var value = nextToken.isComment ?
nextToken.type === 'CommentBlock' ? '/*' : '//' :
nextToken.value;
// Skip empty parentheses and explicit exceptions
if (value === ')' || value in exceptions) {
return;
}
// Skip non-expression parentheses
var type = TokenCategorizer.categorizeOpenParen(token);
if (type !== 'ParenthesizedExpression') {
return;
}
errors.assert.noWhitespaceBetween({
token: token,
nextToken: nextToken,
message: 'Illegal space after opening grouping parenthesis'
});
});
file.iterateTokensByTypeAndValue('Punctuator', ')', function(token) {
var prevToken = file.getPrevToken(token, {includeComments: true});
var value = prevToken.isComment ?
prevToken.type === 'CommentBlock' ? '*/' : '' :
prevToken.value;
// Skip empty parentheses and explicit exceptions
if (value === '(' || value in exceptions) {
return;
}
// Skip non-expression parentheses
var type = TokenCategorizer.categorizeCloseParen(token);
if (type !== 'ParenthesizedExpression') {
return;
}
errors.assert.noWhitespaceBetween({
token: prevToken,
nextToken: token,
message: 'Illegal space before closing grouping parenthesis'
});
});
}
};