/
index.js
112 lines (90 loc) 路 2.93 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
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
import { declare } from "@babel/helper-plugin-utils";
import path from "path";
import { types as t } from "@babel/core";
export default declare(api => {
api.assertVersion(7);
function addDisplayName(id, call) {
const props = call.arguments[0].properties;
let safe = true;
for (let i = 0; i < props.length; i++) {
const prop = props[i];
const key = t.toComputedKey(prop);
if (t.isLiteral(key, { value: "displayName" })) {
safe = false;
break;
}
}
if (safe) {
props.unshift(
t.objectProperty(t.identifier("displayName"), t.stringLiteral(id)),
);
}
}
const isCreateClassCallExpression = t.buildMatchMemberExpression(
"React.createClass",
);
const isCreateClassAddon = callee => callee.name === "createReactClass";
function isCreateClass(node) {
if (!node || !t.isCallExpression(node)) return false;
// not createReactClass nor React.createClass call member object
if (
!isCreateClassCallExpression(node.callee) &&
!isCreateClassAddon(node.callee)
) {
return false;
}
// no call arguments
const args = node.arguments;
if (args.length !== 1) return false;
// first node arg is not an object
const first = args[0];
if (!t.isObjectExpression(first)) return false;
return true;
}
return {
name: "transform-react-display-name",
visitor: {
ExportDefaultDeclaration({ node }, state) {
if (isCreateClass(node.declaration)) {
const filename = state.filename || "unknown";
let displayName = path.basename(filename, path.extname(filename));
// ./{module name}/index.js
if (displayName === "index") {
displayName = path.basename(path.dirname(filename));
}
addDisplayName(displayName, node.declaration);
}
},
CallExpression(path) {
const { node } = path;
if (!isCreateClass(node)) return;
let id;
// crawl up the ancestry looking for possible candidates for displayName inference
path.find(function(path) {
if (path.isAssignmentExpression()) {
id = path.node.left;
} else if (path.isObjectProperty()) {
id = path.node.key;
} else if (path.isVariableDeclarator()) {
id = path.node.id;
} else if (path.isStatement()) {
// we've hit a statement, we should stop crawling up
return true;
}
// we've got an id! no need to continue
if (id) return true;
});
// ensure that we have an identifier we can inherit from
if (!id) return;
// foo.bar -> bar
if (t.isMemberExpression(id)) {
id = id.property;
}
// identifiers are the only thing we can reliably get a name from
if (t.isIdentifier(id)) {
addDisplayName(id.name, node);
}
},
},
};
});