-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform-component-props-withTracker.ts
180 lines (168 loc) · 5.79 KB
/
transform-component-props-withTracker.ts
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
/**
* 1. Find all withTracker calls
* 2. Find all component props
* 3. Check if prop variable was async function
* 4. Find in component's children, all async function props usages
* 4.1. Add async/await to all usages
* 5. Find in component's children, other component
* 5.1 Each component, check if props variable was async function (goto 3)
*/
import { FileInfo, API, Options } from "jscodeshift";
const debug = require("debug")("transform:component-props-withTracker");
const debug2 = require("debug")("transform:print:component-props-withTracker");
import {
findImportNodeByVariableName,
findVariableDeclarator,
getRealImportSource,
} from "./utils";
import { ComponentPropsType, handleComponent } from "./utils-component";
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
debug(
`\n**************************************************
*** ${fileInfo.path}
**************************************************\n`
);
let fileChanged = false;
const rootCollection = j(fileInfo.source);
// work with withTracker functions
const withTrackerCalls = rootCollection
.find(j.CallExpression)
.filter((p) => {
// debug("call expression", p.value.loc?.start);
if (p.value.callee.type === "Identifier") {
if (p.value.callee.name === "withTracker") {
return true;
}
}
return false;
})
.paths();
// debug("withTrackerCalls", withTrackerCalls);
// must use this for loop because we don't want a file being opened at the same time.
for (let i = 0; i < withTrackerCalls.length; i += 1) {
const p = withTrackerCalls[i];
if (p.parentPath.value.type !== "CallExpression") {
continue;
}
const componentName = p.parentPath.value.arguments[0].name;
debug("component name", componentName);
if (!componentName) {
continue;
}
// get the props
// debug(p.value.arguments[0]);
// find in this block, ReturnStatement
const props: ComponentPropsType = {};
j(p.value.arguments)
.find(j.ReturnStatement)
.forEach((rp) => {
// debug("return statement", rp.value.argument);
switch (rp.value.argument?.type) {
case "ObjectExpression":
rp.value.argument.properties.map((item) => {
switch (item.type) {
case "ObjectProperty": {
// debug("property key", item.key);
// debug("property value", item.value);
switch (item.key.type) {
case "Identifier": {
switch (item.value.type) {
case "Identifier": {
// e.g: as1: async1,
// find the variable
const v = findVariableDeclarator(
item.value.name,
p,
j
);
if (v) {
props[item.key.name] = v.value.init;
}
break;
}
case "ArrowFunctionExpression":
case "FunctionExpression":
props[item.key.name] = item.value;
break;
default:
debug(
"Unhandled property value type:",
item.value.type
);
}
break;
}
}
break;
}
case "SpreadElement": {
// e.g: ...aVariable
// debug("SpreadElement", item.argument);
if (item.argument.type === "Identifier") {
// find the variable
// debug("find variable name:", item.argument.name);
const v = findVariableDeclarator(item.argument.name, p, j);
if (v) {
// debug("found spread variable", v.value.init);
switch (v.value.init.type) {
case "ObjectExpression":
v.value.init.properties.map((sp) => {
props[sp.key.name] = sp.value;
});
break;
}
}
}
break;
}
default:
debug("Unhandled argument property type:", item.type);
}
});
break;
default:
debug("Unhandled return statement type:", rp.value.argument?.type);
}
});
debug("props", props);
// find the component declaration
let theComponent = findVariableDeclarator(componentName, p, j);
if (theComponent) {
// debug("_component:", theComponent?.value);
handleComponent({
componentName,
filePath: fileInfo.path,
props,
j,
options,
});
}
if (!theComponent) {
// find the component from import declarations
const { importSource, importSpecType } = findImportNodeByVariableName(
componentName,
rootCollection,
j
);
if (importSource) {
debug(`_import ${importSpecType} from source: ${importSource}`);
const realImportSource = getRealImportSource(
importSource,
fileInfo.path
);
handleComponent({
componentName,
filePath: realImportSource,
props,
j,
options,
importSpecType,
});
}
}
}
if (fileChanged) {
debug2("file changed: ", fileInfo.path);
return rootCollection.toSource();
}
};