-
Notifications
You must be signed in to change notification settings - Fork 290
/
ReactUtils.js
308 lines (274 loc) · 8.56 KB
/
ReactUtils.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
/**
* Copyright 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
module.exports = function(j) {
const REACT_CREATE_CLASS_MEMBER_EXPRESSION = {
type: 'MemberExpression',
object: {
name: 'React',
},
property: {
name: 'createClass',
},
};
// ---------------------------------------------------------------------------
// Checks if the file requires a certain module
const hasModule = (path, module) =>
path
.findVariableDeclarators()
.filter(j.filters.VariableDeclarator.requiresModule(module))
.size() === 1 ||
path
.find(j.ImportDeclaration, {
type: 'ImportDeclaration',
source: {
type: 'Literal',
},
})
.filter(declarator => declarator.value.source.value === module)
.size() === 1;
const hasReact = path => (
hasModule(path, 'React') ||
hasModule(path, 'react') ||
hasModule(path, 'react/addons') ||
hasModule(path, 'react-native')
);
// ---------------------------------------------------------------------------
// Finds all variable declarations that call React.createClass
const findReactCreateClassCallExpression = path =>
j(path).find(j.CallExpression, {
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
});
const findReactCreateClass = path =>
path
.findVariableDeclarators()
.filter(decl => findReactCreateClassCallExpression(decl).size() > 0);
const findReactCreateClassExportDefault = path =>
path.find(j.ExportDeclaration, {
default: true,
declaration: {
type: 'CallExpression',
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
},
});
const findReactCreateClassModuleExports = path =>
path
.find(j.AssignmentExpression, {
left: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'module',
},
property: {
type: 'Identifier',
name: 'exports',
},
},
right: {
type: 'CallExpression',
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
},
});
const getReactCreateClassSpec = classPath => {
const {value} = classPath;
const args = (value.init || value.right || value.declaration).arguments;
if (args && args.length) {
const spec = args[0];
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
return spec;
}
}
return null;
};
// ---------------------------------------------------------------------------
// Finds alias for React.Component if used as named import.
const findReactComponentNameByParent = (path, parentClassName) => {
const reactImportDeclaration = path
.find(j.ImportDeclaration, {
type: 'ImportDeclaration',
source: {
type: 'Literal',
},
})
.filter(importDeclaration => hasReact(path));
const componentImportSpecifier = reactImportDeclaration
.find(j.ImportSpecifier, {
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: parentClassName,
},
}).at(0);
const paths = componentImportSpecifier.paths();
return paths.length
? paths[0].value.local.name
: undefined;
};
const removeUnusedSuperClassImport = (path, file, superClassName) => {
if (path.find(j.Identifier, {
type: 'Identifier',
name: superClassName
}).length === 0) {
file.find(j.ImportSpecifier, {
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: superClassName,
}
}).remove();
}
};
const findReactES6ClassDeclarationByParent = (path, parentClassName) => {
const componentImport = findReactComponentNameByParent(path, parentClassName);
const selector = componentImport
? {
superClass: {
type: 'Identifier',
name: componentImport,
},
}
: {
superClass: {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'React',
},
property: {
type: 'Identifier',
name: parentClassName,
},
},
};
return path
.find(j.ClassDeclaration, selector);
};
// Finds all classes that extend React.Component
const findReactES6ClassDeclaration = path => {
let classDeclarations = findReactES6ClassDeclarationByParent(path, 'Component');
if (classDeclarations.size() === 0) {
classDeclarations = findReactES6ClassDeclarationByParent(path, 'PureComponent');
}
return classDeclarations;
};
// ---------------------------------------------------------------------------
// Checks if the React class has mixins
const isMixinProperty = property => {
const key = property.key;
const value = property.value;
return (
key.name === 'mixins' &&
value.type === 'ArrayExpression' &&
Array.isArray(value.elements) &&
value.elements.length
);
};
const hasMixins = classPath => {
const spec = getReactCreateClassSpec(classPath);
return spec && spec.properties.some(isMixinProperty);
};
// ---------------------------------------------------------------------------
// Others
const getClassExtendReactSpec = classPath => classPath.value.body;
const createCreateReactClassCallExpression = properties =>
j.callExpression(
j.memberExpression(
j.identifier('React'),
j.identifier('createClass'),
false
),
[j.objectExpression(properties)]
);
const getComponentName =
classPath => classPath.node.id && classPath.node.id.name;
// ---------------------------------------------------------------------------
// Direct methods! (see explanation below)
const findAllReactCreateClassCalls = path =>
path.find(j.CallExpression, {
callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION,
});
// Mixin Stuff
const containSameElements = (ls1, ls2) => {
if (ls1.length !== ls2.length) {
return false;
}
return (
ls1.reduce((res, x) => res && ls2.indexOf(x) !== -1, true) &&
ls2.reduce((res, x) => res && ls1.indexOf(x) !== -1, true)
);
};
const keyNameIsMixins = property => property.key.name === 'mixins';
const isSpecificMixinsProperty = (property, mixinIdentifierNames) => {
const key = property.key;
const value = property.value;
return (
key.name === 'mixins' &&
value.type === 'ArrayExpression' &&
Array.isArray(value.elements) &&
value.elements.every(elem => elem.type === 'Identifier') &&
containSameElements(value.elements.map(elem => elem.name), mixinIdentifierNames)
);
};
// These following methods assume that the argument is
// a `React.createClass` call expression. In other words,
// they should only be used with `findAllReactCreateClassCalls`.
const directlyGetCreateClassSpec = classPath => {
if (!classPath || !classPath.value) {
return null;
}
const args = classPath.value.arguments;
if (args && args.length) {
const spec = args[0];
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
return spec;
}
}
return null;
};
const directlyGetComponentName = classPath => {
let result = '';
if (
classPath.parentPath.value &&
classPath.parentPath.value.type === 'VariableDeclarator'
) {
result = classPath.parentPath.value.id.name;
}
return result;
};
const directlyHasMixinsField = classPath => {
const spec = directlyGetCreateClassSpec(classPath);
return spec && spec.properties.some(keyNameIsMixins);
};
const directlyHasSpecificMixins = (classPath, mixinIdentifierNames) => {
const spec = directlyGetCreateClassSpec(classPath);
return spec && spec.properties.some(prop => isSpecificMixinsProperty(prop, mixinIdentifierNames));
};
return {
createCreateReactClassCallExpression,
findReactES6ClassDeclaration,
findReactCreateClass,
findReactCreateClassCallExpression,
findReactCreateClassModuleExports,
findReactCreateClassExportDefault,
getComponentName,
getReactCreateClassSpec,
getClassExtendReactSpec,
hasMixins,
hasModule,
hasReact,
isMixinProperty,
removeUnusedSuperClassImport,
// "direct" methods
findAllReactCreateClassCalls,
directlyGetComponentName,
directlyGetCreateClassSpec,
directlyHasMixinsField,
directlyHasSpecificMixins,
};
};