/
index.js
65 lines (57 loc) · 1.72 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
import isStatelessComponent from './is-stateless-component';
export default function() {
function isReactComponent(superClass) {
return superClass.matchesPattern('React.Component') || superClass.matchesPattern('Component');
}
return {
visitor: {
ObjectProperty(path) {
const {node} = path;
if (node.computed || node.key.name !== 'propTypes') {
return;
}
const parent = path.findParent((parent) => {
if (parent.type !== 'CallExpression') {
return false;
}
return parent.get('callee').matchesPattern('React.createClass');
});
if (parent) {
path.remove();
}
},
ClassProperty(path) {
const {node, scope} = path
if (node.key.name !== 'propTypes') {
return;
}
const className = scope.block.id.name;
const binding = scope.getBinding(className);
const superClass = binding.path.get('superClass');
if (isReactComponent(superClass)) {
path.remove();
}
},
AssignmentExpression(path) {
const {node, scope} = path;
const {left} = node;
if (left.computed || !left.property || left.property.name !== 'propTypes') {
return;
}
const className = left.object.name;
const binding = scope.getBinding(className);
if (!binding) {
return;
}
if (binding.path.isClassDeclaration()) {
const superClass = binding.path.get('superClass');
if (isReactComponent(superClass)) {
path.remove();
}
} else if (isStatelessComponent(binding.path)) {
path.remove();
}
}
}
};
}