-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
property-order.js
365 lines (308 loc) · 10.2 KB
/
property-order.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
const ember = require('./ember');
const utils = require('./utils');
const types = require('./types');
const decoratorUtils = require('../utils/decorators');
module.exports = {
determinePropertyType,
reportUnorderedProperties,
addBackwardsPosition,
};
const NAMES = {
actions: 'actions hash',
activate: 'lifecycle hook',
afterModel: 'lifecycle hook',
attribute: 'attribute',
beforeModel: 'lifecycle hook',
controller: 'controller injection',
deactivate: 'lifecycle hook',
didDestroyElement: 'lifecycle hook',
didInsertElement: 'lifecycle hook',
didReceiveAttrs: 'lifecycle hook',
didRender: 'lifecycle hook',
didUpdate: 'lifecycle hook',
didUpdateAttrs: 'lifecycle hook',
'empty-method': 'empty method',
init: 'lifecycle hook',
'lifecycle-hook': 'lifecycle hook',
method: 'method',
model: '"model" hook',
observer: 'observer',
property: 'property',
'single-line-function': 'single-line function',
'multi-line-function': 'multi-line function',
'query-params': 'property',
redirect: 'lifecycle hook',
relationship: 'relationship',
renderTemplate: 'lifecycle hook',
resetController: 'lifecycle hook',
serialize: 'lifecycle hook',
service: 'service injection',
setupController: 'lifecycle hook',
spread: 'spread property',
unknown: 'unknown property type',
willClearRender: 'lifecycle hook',
willDestroyElement: 'lifecycle hook',
willInsertElement: 'lifecycle hook',
willRender: 'lifecycle hook',
willUpdate: 'lifecycle hook',
};
// eslint-disable-next-line complexity
function determinePropertyType(
node,
parentType,
ORDER,
importedEmberName,
importedInjectName,
importedObserverName,
importedControllerName
) {
if (ember.isInjectedServiceProp(node, importedEmberName, importedInjectName)) {
return 'service';
}
if (ember.isInjectedControllerProp(node, importedEmberName, importedControllerName)) {
return 'controller';
}
if (types.isIdentifier(node.key) && node.key.name === 'init') {
return 'init';
}
if (parentType === 'component') {
if (types.isIdentifier(node.key) && ember.isComponentLifecycleHook(node)) {
return node.key.name;
}
}
if (parentType === 'controller') {
if (types.isIdentifier(node.key) && node.key.name === 'queryParams') {
return 'query-params';
} else if (ember.isControllerDefaultProp(node)) {
return 'inherited-property';
}
}
if (parentType === 'model') {
if (
(node.value && ember.isModule(node.value, 'attr', 'DS')) ||
decoratorUtils.isClassPropertyOrPropertyDefinitionWithDecorator(node, 'attr')
) {
return 'attribute';
} else if (ember.isRelation(node)) {
return 'relationship';
}
}
if (parentType === 'route') {
if (ember.isRouteDefaultProp(node)) {
return 'inherited-property';
} else if (ember.isRouteLifecycleHook(node)) {
return node.key.name;
}
}
if (ember.isObserverProp(node, importedEmberName, importedObserverName)) {
return 'observer';
}
if (parentType !== 'model' && ember.isActionsProp(node)) {
return 'actions';
}
if (ember.isSingleLineFn(node, importedEmberName, importedObserverName)) {
return 'single-line-function';
}
if (ember.isMultiLineFn(node, importedEmberName, importedObserverName)) {
return 'multi-line-function';
}
const propName = getNodeKeyName(node);
const possibleOrderName = `custom:${propName}`;
if (ORDER.includes(possibleOrderName)) {
return possibleOrderName;
}
if (ember.isCustomProp(node)) {
return 'property';
}
if (node.value && ember.isFunctionExpression(node.value)) {
if (utils.isEmptyMethod(node)) {
return 'empty-method';
}
return 'method';
}
// Handle both babel-eslint and @babel/eslint-parser
if (node.type === 'ExperimentalSpreadProperty' || node.type === 'SpreadElement') {
return 'spread';
}
return 'unknown';
}
function getOrder(ORDER, type) {
for (let i = 0, len = ORDER.length; i < len; i++) {
const value = ORDER[i];
if (typeof value === 'string') {
if (value === type) {
return i;
}
} else {
const index = value.indexOf(type);
if (index !== -1) {
return i;
}
}
}
return ORDER.length;
}
function getNodeKeyName(node) {
if (node.key) {
if (node.key.type === 'Identifier') {
return node.key.name;
} else if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
return node.key.value;
}
}
return 'unknown';
}
function getName(type, node) {
let prefix;
if (!node.computed && type !== 'actions' && type !== 'model' && type !== 'spread') {
prefix = getNodeKeyName(node);
}
const typeDescription = NAMES[type];
if (type === 'inherited-property') {
return prefix ? `inherited "${prefix}" property` : 'inherited property';
}
if (type.startsWith('custom:')) {
return prefix ? `"${prefix}" custom property` : 'custom property';
}
return prefix ? `"${prefix}" ${typeDescription}` : typeDescription;
}
function reportUnorderedProperties(
node,
context,
parentType,
ORDER,
importedEmberName,
importedInjectName,
importedObserverName,
importedControllerName,
scopeManager
) {
let maxOrder = -1;
const firstPropertyOfType = {};
let firstPropertyOfNextType;
const properties = types.isClassDeclaration(node)
? node.body.body
: ember.getModuleProperties(node, scopeManager);
for (const property of properties) {
const type = determinePropertyType(
property,
parentType,
ORDER,
importedEmberName,
importedInjectName,
importedObserverName,
importedControllerName
);
const order = getOrder(ORDER, type);
const info = {
node: property,
type,
};
// check if this property should be moved further upwards
if (order < maxOrder) {
// look for correct position to insert this property
for (let i = order + 1; i <= maxOrder; i++) {
firstPropertyOfNextType = firstPropertyOfType[i];
if (firstPropertyOfNextType) {
break;
}
}
const typeName = getName(info.type, info.node);
const nextTypeName = getName(firstPropertyOfNextType.type, firstPropertyOfNextType.node);
const nextSourceLine = firstPropertyOfNextType.node.loc.start.line;
context.report({
node: property,
message: `The ${typeName} should be above the ${nextTypeName} on line ${nextSourceLine}`,
fix: (fixer) => {
// for capturing the moved property and EOL character to insert ',' in between
const propertyWithEOL = /(.+)(\s+)$/;
const sourceCode = context.getSourceCode();
const foundProperty = firstPropertyOfNextType.node;
let nextToken = property;
let optionalComma = '';
let isLastProperty = false;
// including EOL character(s)
do {
const previousToken = nextToken;
nextToken = sourceCode.getTokenAfter(nextToken, {
includeComments: true,
});
if (!nextToken) {
nextToken = previousToken;
}
// adding a trailing comma when it's the last property defined
if (nextToken.value === '}') {
isLastProperty = true;
if (previousToken.value !== ',') {
optionalComma = ',';
}
}
} while (nextToken.value === ',');
// additional whitespace is needed only when it's the last property
const whitespaceCount = isLastProperty ? property.loc.start.column : 0;
const propertyOffset = getCommentOffsetBefore(property, sourceCode);
const foundPropertyOffset = getCommentOffsetBefore(foundProperty, sourceCode);
const offset = nextToken.range[0] - property.range[1];
const textBetween =
property.range[0] - propertyOffset - foundProperty.range[1] - whitespaceCount;
const replaceTextRange = [
foundProperty.range[0] - foundPropertyOffset,
nextToken.range[0],
];
const movedProperty = sourceCode.getText(property, propertyOffset, offset);
const restText = sourceCode.getText(foundProperty, foundPropertyOffset, textBetween);
// adding the optional comma between the property and newline
const replaceWithComma = `$1${optionalComma}$2`;
const movedPropertyWithComma = movedProperty.replace(propertyWithEOL, replaceWithComma);
const optionalWhitespace = Array.from({ length: whitespaceCount + 1 }).join(' ');
const outputText = movedPropertyWithComma + optionalWhitespace + restText;
return fixer.replaceTextRange(replaceTextRange, outputText);
},
});
} else {
maxOrder = order;
if (!(order in firstPropertyOfType)) {
firstPropertyOfType[order] = info;
}
}
}
}
function getCommentOffsetBefore(property, sourceCode) {
const commentBlockRegExp = /^\/\*(.|\s)*\*\/$/m;
const commentLineRegExp = /^\/\/.*$/;
const previousToken = sourceCode.getTokenBefore(property, {
includeComments: true,
});
const previousTokenText = sourceCode.getText(previousToken);
// including comments above the moved property
const isLineComment = previousToken.type === 'Line' && commentLineRegExp.test(previousTokenText);
const isBlockComment =
previousToken.type === 'Block' && commentBlockRegExp.test(previousTokenText);
if (isLineComment || isBlockComment) {
return property.range[0] - previousToken.range[0];
}
return 0;
}
function addBackwardsPosition(order, newPosition, targetPosition) {
const positionOrder = [...order];
const containsPosition = positionOrder.some((position) => {
if (Array.isArray(position)) {
return position.includes(newPosition);
}
return position === newPosition;
});
if (!containsPosition) {
const targetIndex = positionOrder.indexOf(targetPosition);
if (targetIndex > -1) {
positionOrder[targetIndex] = [targetPosition, newPosition];
} else {
for (const position of positionOrder) {
if (Array.isArray(position) && position.includes(targetPosition)) {
position.push(newPosition);
}
}
}
}
return positionOrder;
}