This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/
disallow-space-after-object-keys.js
197 lines (179 loc) · 5.66 KB
/
disallow-space-after-object-keys.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Disallows space after object keys.
*
* Types: `Boolean`, `String`, or `Object`
*
* Values:
* - `true`
* - `"ignoreSingleLine"` ignores objects if the object only takes up a single line
* (*deprecated* use `"allExcept": [ "singleline" ]`)
* - `"ignoreMultiLine"` ignores objects if the object takes up multiple lines
* (*deprecated* use `"allExcept": [ "multiline" ]`)
* - `Object`:
* - `"allExcept"`: array of exceptions:
* - `"singleline"` ignores objects if the object only takes up a single line
* - `"multiline"` ignores objects if the object takes up multiple lines
* - `"aligned"` ignores aligned object properties
* - `"method"` ignores method declarations
*
* #### Example
*
* ```js
* "disallowSpaceAfterObjectKeys": true
* ```
*
* ##### Valid for `true`
* ```js
* var x = {a: 1};
* var y = {
* a: 1,
* b: 2
* }
* ```
*
* ##### Valid for `{ allExcept: ['singleline'] }`
* ```js
* var x = {a : 1};
* var y = {
* a: 1,
* b: 2
* }
* ```
*
* ##### Valid for `{ allExcept: ['multiline'] }`
* ```js
* var x = {a: 1};
* var y = {
* a : 1,
* b : 2
* }
* ```
*
* ##### Valid for `{ allExcept: ['aligned'] }`
* ```js
* var y = {
* abc: 1,
* d : 2
* }
* ```
*
* ##### Valid for `{ allExcept: ['method'] }`
* ```js
* var y = {
* fn () {
* return 42;
* }
* }
* ```
*
* ##### Invalid
* ```js
* var x = {a : 1};
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
if (typeof options !== 'object') {
assert(
options === true ||
options === 'ignoreSingleLine' ||
options === 'ignoreMultiLine',
this.getOptionName() +
' option requires a true value, "ignoreSingleLine", "ignoreMultiLine", or an object'
);
var _options = {
allExcept: []
};
if (options === 'ignoreSingleLine') {
_options.allExcept.push('singleline');
}
if (options === 'ignoreMultiLine') {
_options.allExcept.push('multiline');
}
return this.configure(_options);
} else {
assert(
Array.isArray(options.allExcept),
this.getOptionName() +
' option object requires allExcept array property'
);
}
this._exceptSingleline = options.allExcept.indexOf('singleline') > -1;
this._exceptMultiline = options.allExcept.indexOf('multiline') > -1;
this._exceptAligned = options.allExcept.indexOf('aligned') > -1;
this._exceptMethod = options.allExcept.indexOf('method') > -1;
assert(
!this._exceptMultiline || !this._exceptAligned,
this.getOptionName() +
' option allExcept property cannot contain `aligned` and `multiline` at the same time'
);
assert(
!this._exceptMultiline || !this._exceptSingleline,
this.getOptionName() +
' option allExcept property cannot contain `singleline` and `multiline` at the same time'
);
},
getOptionName: function() {
return 'disallowSpaceAfterObjectKeys';
},
check: function(file, errors) {
var exceptSingleline = this._exceptSingleline;
var exceptMultiline = this._exceptMultiline;
var exceptAligned = this._exceptAligned;
var exceptMethod = this._exceptMethod;
file.iterateNodesByType('ObjectExpression', function(node) {
var multiline = node.getNewlineCount() > 0;
if (exceptSingleline && !multiline) {
return;
}
if (exceptMultiline && multiline) {
return;
}
var maxKeyEndPos = 0;
var tokens = [];
node.properties.forEach(function(property) {
if (property.shorthand ||
(exceptMethod && property.method) ||
property.type === 'SpreadProperty') {
return;
}
var keyToken = file.getLastNodeToken(property.key);
if (property.computed === true) {
keyToken = file.getNextToken(keyToken);
}
if (exceptAligned) {
maxKeyEndPos = Math.max(maxKeyEndPos, keyToken.getLoc().end.column);
}
tokens.push(keyToken);
});
var noSpace = true;
if (exceptAligned) {
var withoutSpace = 0;
var alignedOnColon = 0;
tokens.forEach(function(key) {
var colon = file.getNextToken(key);
var spaces = file.getDistanceBetween(key, colon);
if (spaces === 0) {
withoutSpace++;
} else if (spaces === maxKeyEndPos - key.getLoc().end.column) {
alignedOnColon++;
}
});
noSpace = withoutSpace > alignedOnColon;
}
tokens.forEach(function(key) {
var colon = file.getNextToken(key);
var spaces = (exceptAligned && !noSpace) ? maxKeyEndPos - key.getLoc().end.column : 0;
errors.assert.spacesBetween({
token: key,
nextToken: colon,
exactly: spaces,
message: 'Illegal space after key',
disallowNewLine: true
});
});
});
}
};