-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (55 loc) · 1.76 KB
/
index.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
// @flow
'use strict';
/*::
type Node = {
type: string,
[key: string]: any,
};
type Path = {
node: Node,
[key: string]: any,
};
*/
const GLOBAL_NAME = 'React';
const MODULE_NAME = 'react';
let getBinding = path => {
return path.scope.getBinding(path.node.name);
};
let isReactComponentMember = path => {
return path.node.name === 'Component' || path.node.name === 'PureComponent';
};
let getSourceFromSpecifier = path => {
return path.parent.source.value;
};
function isReactComponentClass(path /*: Path */) {
if (!path.isClass()) return false;
let superClass = path.get('superClass');
if (!superClass.node) return false;
if (superClass.isMemberExpression()) {
let object = superClass.get('object');
let property = superClass.get('property');
if (!object.isIdentifier()) return false;
let binding = getBinding(object);
if (!binding) {
if (object.node.name !== GLOBAL_NAME) return false;
} else {
if (binding.kind !== 'module') return false;
if (!binding.path.isImportDefaultSpecifier() && !binding.path.isImportNamespaceSpecifier()) return false;
if (getSourceFromSpecifier(binding.path) !== MODULE_NAME) return false;
}
return isReactComponentMember(property);
}
if (superClass.isIdentifier()) {
let binding = getBinding(superClass);
if (!binding) return false;
if (binding.kind !== 'module') return false;
if (!binding.path.isImportSpecifier()) return false;
if (getSourceFromSpecifier(binding.path) !== MODULE_NAME) return false;
if (!isReactComponentMember(binding.path.get('imported'))) return false;
return true;
}
throw superClass.buildCodeFrameError(
`Unexpected super class type: ${superClass.type}`
);
}
exports.isReactComponentClass = isReactComponentClass;