sort-comp.js
9.61 KB
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
366
367
368
369
370
371
372
373
374
375
376
/**
* @fileoverview Enforce component methods order
* @author Yannick Croissant
*/
'use strict';
var util = require('util');
var Components = require('../util/Components');
/**
* Get the methods order from the default config and the user config
* @param {Object} defaultConfig The default configuration.
* @param {Object} userConfig The user configuration.
* @returns {Array} Methods order
*/
function getMethodsOrder(defaultConfig, userConfig) {
userConfig = userConfig || {};
var groups = util._extend(defaultConfig.groups, userConfig.groups);
var order = userConfig.order || defaultConfig.order;
var config = [];
var entry;
for (var i = 0, j = order.length; i < j; i++) {
entry = order[i];
if (groups.hasOwnProperty(entry)) {
config = config.concat(groups[entry]);
} else {
config.push(entry);
}
}
return config;
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = Components.detect(function(context, components) {
var errors = {};
var MISPOSITION_MESSAGE = '{{propA}} should be placed {{position}} {{propB}}';
var methodsOrder = getMethodsOrder({
order: [
'lifecycle',
'everything-else',
'render'
],
groups: {
lifecycle: [
'displayName',
'propTypes',
'contextTypes',
'childContextTypes',
'mixins',
'statics',
'defaultProps',
'constructor',
'getDefaultProps',
'state',
'getInitialState',
'getChildContext',
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount'
]
}
}, context.options[0]);
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
var regExpRegExp = /\/(.*)\/([g|y|i|m]*)/;
/**
* Get indexes of the matching patterns in methods order configuration
* @param {String} method - Method name.
* @returns {Array} The matching patterns indexes. Return [Infinity] if there is no match.
*/
function getRefPropIndexes(method) {
var isRegExp;
var matching;
var i;
var j;
var indexes = [];
for (i = 0, j = methodsOrder.length; i < j; i++) {
isRegExp = methodsOrder[i].match(regExpRegExp);
if (isRegExp) {
matching = new RegExp(isRegExp[1], isRegExp[2]).test(method);
} else {
matching = methodsOrder[i] === method;
}
if (matching) {
indexes.push(i);
}
}
// No matching pattern, return 'everything-else' index
if (indexes.length === 0) {
for (i = 0, j = methodsOrder.length; i < j; i++) {
if (methodsOrder[i] === 'everything-else') {
indexes.push(i);
}
}
}
// No matching pattern and no 'everything-else' group
if (indexes.length === 0) {
indexes.push(Infinity);
}
return indexes;
}
/**
* Get properties name
* @param {Object} node - Property.
* @returns {String} Property name.
*/
function getPropertyName(node) {
// Special case for class properties
// (babel-eslint does not expose property name so we have to rely on tokens)
if (node.type === 'ClassProperty') {
var tokens = context.getFirstTokens(node, 2);
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
}
return node.key.name;
}
/**
* Store a new error in the error list
* @param {Object} propA - Mispositioned property.
* @param {Object} propB - Reference property.
*/
function storeError(propA, propB) {
// Initialize the error object if needed
if (!errors[propA.index]) {
errors[propA.index] = {
node: propA.node,
score: 0,
closest: {
distance: Infinity,
ref: {
node: null,
index: 0
}
}
};
}
// Increment the prop score
errors[propA.index].score++;
// Stop here if we already have a closer reference
if (Math.abs(propA.index - propB.index) > errors[propA.index].closest.distance) {
return;
}
// Update the closest reference
errors[propA.index].closest.distance = Math.abs(propA.index - propB.index);
errors[propA.index].closest.ref.node = propB.node;
errors[propA.index].closest.ref.index = propB.index;
}
/**
* Dedupe errors, only keep the ones with the highest score and delete the others
*/
function dedupeErrors() {
for (var i in errors) {
if (!errors.hasOwnProperty(i)) {
continue;
}
var index = errors[i].closest.ref.index;
if (!errors[index]) {
continue;
}
if (errors[i].score > errors[index].score) {
delete errors[index];
} else {
delete errors[i];
}
}
}
/**
* Report errors
*/
function reportErrors() {
dedupeErrors();
var nodeA;
var nodeB;
var indexA;
var indexB;
for (var i in errors) {
if (!errors.hasOwnProperty(i)) {
continue;
}
nodeA = errors[i].node;
nodeB = errors[i].closest.ref.node;
indexA = i;
indexB = errors[i].closest.ref.index;
context.report(nodeA, MISPOSITION_MESSAGE, {
propA: getPropertyName(nodeA),
propB: getPropertyName(nodeB),
position: indexA < indexB ? 'before' : 'after'
});
}
}
/**
* Get properties for a given AST node
* @param {ASTNode} node The AST node being checked.
* @returns {Array} Properties array.
*/
function getComponentProperties(node) {
switch (node.type) {
case 'ClassDeclaration':
return node.body.body;
case 'ObjectExpression':
return node.properties;
default:
return [];
}
}
/**
* Compare two properties and find out if they are in the right order
* @param {Array} propertiesNames Array containing all the properties names.
* @param {String} propA First property name.
* @param {String} propB Second property name.
* @returns {Object} Object containing a correct true/false flag and the correct indexes for the two properties.
*/
function comparePropsOrder(propertiesNames, propA, propB) {
var i;
var j;
var k;
var l;
var refIndexA;
var refIndexB;
// Get references indexes (the correct position) for given properties
var refIndexesA = getRefPropIndexes(propA);
var refIndexesB = getRefPropIndexes(propB);
// Get current indexes for given properties
var classIndexA = propertiesNames.indexOf(propA);
var classIndexB = propertiesNames.indexOf(propB);
// Loop around the references indexes for the 1st property
for (i = 0, j = refIndexesA.length; i < j; i++) {
refIndexA = refIndexesA[i];
// Loop around the properties for the 2nd property (for comparison)
for (k = 0, l = refIndexesB.length; k < l; k++) {
refIndexB = refIndexesB[k];
if (
// Comparing the same properties
refIndexA === refIndexB ||
// 1st property is placed before the 2nd one in reference and in current component
refIndexA < refIndexB && classIndexA < classIndexB ||
// 1st property is placed after the 2nd one in reference and in current component
refIndexA > refIndexB && classIndexA > classIndexB
) {
return {
correct: true,
indexA: classIndexA,
indexB: classIndexB
};
}
}
}
// We did not find any correct match between reference and current component
return {
correct: false,
indexA: refIndexA,
indexB: refIndexB
};
}
/**
* Check properties order from a properties list and store the eventual errors
* @param {Array} properties Array containing all the properties.
*/
function checkPropsOrder(properties) {
var propertiesNames = properties.map(getPropertyName);
var i;
var j;
var k;
var l;
var propA;
var propB;
var order;
// Loop around the properties
for (i = 0, j = propertiesNames.length; i < j; i++) {
propA = propertiesNames[i];
// Loop around the properties a second time (for comparison)
for (k = 0, l = propertiesNames.length; k < l; k++) {
propB = propertiesNames[k];
// Compare the properties order
order = comparePropsOrder(propertiesNames, propA, propB);
// Continue to next comparison is order is correct
if (order.correct === true) {
continue;
}
// Store an error if the order is incorrect
storeError({
node: properties[i],
index: order.indexA
}, {
node: properties[k],
index: order.indexB
});
}
}
}
return {
'Program:exit': function() {
var list = components.list();
for (var component in list) {
if (!list.hasOwnProperty(component)) {
continue;
}
var properties = getComponentProperties(list[component].node);
checkPropsOrder(properties);
}
reportErrors();
}
};
});
module.exports.schema = [{
type: 'object',
properties: {
order: {
type: 'array',
items: {
type: 'string'
}
},
groups: {
type: 'object',
patternProperties: {
'^.*$': {
type: 'array',
items: {
type: 'string'
}
}
}
}
},
additionalProperties: false
}];