-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetDisplayName.js
78 lines (71 loc) · 2.11 KB
/
setDisplayName.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
import { concat, flow, uniq } from "lodash/fp";
import {
createImportNode,
findImport,
findImportSpecifier
} from "./importUtils";
const addImportSetDisplayName = (j, root) => {
const importRecompose = findImport(j, root, "recompose");
const recomposeSpecifiers = findImportSpecifier(j, importRecompose);
importRecompose.replaceWith(() => {
const newMethods = flow(concat("setDisplayName"), uniq)(
recomposeSpecifiers
);
return createImportNode(j, newMethods, "recompose");
});
};
const findComponentName = (j, root, node) => {
if (node.parentPath.value.type === "VariableDeclarator") {
const enhanceName = node.parentPath.value.id.name;
const enhancer = root.find(j.CallExpression, {
callee: {
name: enhanceName
}
});
if (enhancer.length < 1) return null;
return enhancer.get().value.arguments[0].name;
}
if (node.parentPath.value.type === "CallExpression") {
return node.parentPath.value.arguments[0].name;
}
return null;
};
export default (file, api) => {
const j = api.jscodeshift;
const root = j(file.source);
const useCompose = root.find(j.CallExpression, {
callee: {
name: "compose"
}
});
if (useCompose.length < 1) return file.source;
useCompose.replaceWith(node => {
const args = node.node.arguments;
const componentName = findComponentName(j, root, node);
if (componentName === null) {
return j.callExpression(j.identifier("compose"), args);
}
if (
args[0].type === "CallExpression" &&
args[0].callee.name === "setStatic"
) {
return j.callExpression(j.identifier("compose"), args);
}
if (
args[0].type === "CallExpression" &&
args[0].callee.name === "setDisplayName"
) {
return j.callExpression(j.identifier("compose"), args);
}
const newArgs = [
j.callExpression(j.identifier("setDisplayName"), [
j.literal(componentName)
]),
...args
];
addImportSetDisplayName(j, root);
return j.callExpression(j.identifier("compose"), newArgs);
});
// return file.source;
return root.toSource({ quote: "single" });
};