@vureact/compiler-core 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/README.zh.md +4 -0
- package/lib/{chunk-GWLPQNHN.js → chunk-FCLIDEIZ.js} +1104 -809
- package/lib/{chunk-2ESB4I6I.esm.js → chunk-UJZGDNNB.esm.js} +999 -704
- package/lib/cli.esm.js +1 -1
- package/lib/cli.js +8 -8
- package/lib/compiler-core.d.cts +19 -2
- package/lib/compiler-core.d.ts +19 -2
- package/lib/compiler-core.esm.js +1 -1
- package/lib/compiler-core.js +2 -2
- package/package.json +2 -2
|
@@ -26,93 +26,349 @@ function executePlugins(map, result, ctx) {
|
|
|
26
26
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-ctx-provider.ts
|
|
27
27
|
import * as t10 from "@babel/types";
|
|
28
28
|
|
|
29
|
-
// src/consts/
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
Component: "Component",
|
|
35
|
-
Provider: "Provider",
|
|
36
|
-
// 实际是 Vue 的 provide 方法,为了命名统一采用 Provide
|
|
37
|
-
Transition: "Transition",
|
|
38
|
-
TransitionGroup: "TransitionGroup"
|
|
39
|
-
};
|
|
40
|
-
var ADAPTER_UTILS_MAP = {
|
|
41
|
-
dir: "dir",
|
|
42
|
-
dirCls: "dir.cls",
|
|
43
|
-
dirKeyless: "dir.keyless",
|
|
44
|
-
dirOn: "dir.on",
|
|
45
|
-
dirStyle: "dir.style",
|
|
46
|
-
nextTick: "nextTick"
|
|
29
|
+
// src/consts/other.ts
|
|
30
|
+
var PACKAGE_NAME = {
|
|
31
|
+
react: "react",
|
|
32
|
+
runtime: "@vureact/runtime-core",
|
|
33
|
+
router: "@vureact/router"
|
|
47
34
|
};
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
var STYLE_MODULE_NAME = "$style";
|
|
36
|
+
var MACRO_API_NAMES = {
|
|
37
|
+
props: "defineProps",
|
|
38
|
+
emits: "defineEmits",
|
|
39
|
+
slots: "defineSlots",
|
|
40
|
+
options: "defineOptions",
|
|
41
|
+
expose: "defineExpose"
|
|
51
42
|
};
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
43
|
+
var DOLLAR_IDENTIFIERS = [
|
|
44
|
+
"$data",
|
|
45
|
+
"$props",
|
|
46
|
+
"$el",
|
|
47
|
+
"$options",
|
|
48
|
+
"$parent",
|
|
49
|
+
"$root",
|
|
50
|
+
"$slots",
|
|
51
|
+
"$refs",
|
|
52
|
+
"$attrs",
|
|
53
|
+
"$emit",
|
|
54
|
+
"$watch",
|
|
55
|
+
"$forceUpdate",
|
|
56
|
+
"$nextTick",
|
|
57
|
+
"$router",
|
|
58
|
+
"$route",
|
|
59
|
+
"$store",
|
|
60
|
+
"$pinia",
|
|
61
|
+
"$event"
|
|
62
|
+
];
|
|
63
|
+
var VUE_PACKAGES = [
|
|
64
|
+
"vue",
|
|
65
|
+
"vite",
|
|
66
|
+
"pinia",
|
|
67
|
+
"nuxt",
|
|
68
|
+
// 不需要带到 React 项目中
|
|
69
|
+
"@vureact/compiler-core"
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
// src/consts/adapters-map.ts
|
|
73
|
+
var ADAPTER_RULES = {
|
|
74
|
+
// =============== [React] ===============
|
|
75
|
+
react: {
|
|
76
|
+
// =============== Hooks ===============
|
|
77
|
+
useTemplateRef: {
|
|
78
|
+
target: "useRef",
|
|
79
|
+
package: PACKAGE_NAME.react,
|
|
80
|
+
type: "none"
|
|
61
81
|
},
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
readonly: "useReadonly",
|
|
67
|
-
toRef: "useToVRef",
|
|
68
|
-
toRefs: "useToVRefs",
|
|
69
|
-
toRaw: "useToRaw",
|
|
70
|
-
inject: "useInject",
|
|
71
|
-
watch: "useWatch",
|
|
72
|
-
shallowRef: "useShallowRef",
|
|
73
|
-
shallowReactive: "useShallowReactive",
|
|
74
|
-
shallowReadonly: "useShallowReadonly",
|
|
75
|
-
isRef: "isRef",
|
|
76
|
-
isProxy: "isProxy",
|
|
77
|
-
isReactive: "isReactive"
|
|
82
|
+
defineExpose: {
|
|
83
|
+
target: "useImperativeHandle",
|
|
84
|
+
package: PACKAGE_NAME.react,
|
|
85
|
+
type: "none"
|
|
78
86
|
}
|
|
79
87
|
},
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
},
|
|
88
|
-
lifecycle: {
|
|
89
|
-
onBeforeUpdate: "useBeforeUpdate",
|
|
90
|
-
onUpdated: "useUpdated"
|
|
91
|
-
}
|
|
88
|
+
// ============== [VuReact Runtime Core] ===============
|
|
89
|
+
runtime: {
|
|
90
|
+
// =============== Components ===============
|
|
91
|
+
KeepAlive: {
|
|
92
|
+
target: "KeepAlive",
|
|
93
|
+
package: PACKAGE_NAME.runtime,
|
|
94
|
+
type: "rename"
|
|
92
95
|
},
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
Suspense: {
|
|
97
|
+
target: "Suspense",
|
|
98
|
+
package: PACKAGE_NAME.runtime,
|
|
99
|
+
type: "rename"
|
|
100
|
+
},
|
|
101
|
+
Teleport: {
|
|
102
|
+
target: "Teleport",
|
|
103
|
+
package: PACKAGE_NAME.runtime,
|
|
104
|
+
type: "rename"
|
|
105
|
+
},
|
|
106
|
+
Component: {
|
|
107
|
+
target: "Component",
|
|
108
|
+
package: PACKAGE_NAME.runtime,
|
|
109
|
+
type: "rename"
|
|
110
|
+
},
|
|
111
|
+
provide: {
|
|
112
|
+
target: "Provider",
|
|
113
|
+
package: PACKAGE_NAME.runtime,
|
|
114
|
+
type: "rename"
|
|
115
|
+
},
|
|
116
|
+
Transition: {
|
|
117
|
+
target: "Transition",
|
|
118
|
+
package: PACKAGE_NAME.runtime,
|
|
119
|
+
type: "rename"
|
|
120
|
+
},
|
|
121
|
+
TransitionGroup: {
|
|
122
|
+
target: "TransitionGroup",
|
|
123
|
+
package: PACKAGE_NAME.runtime,
|
|
124
|
+
type: "rename"
|
|
125
|
+
},
|
|
126
|
+
// =============== Hooks ===============
|
|
127
|
+
useActived: {
|
|
128
|
+
target: "useActived",
|
|
129
|
+
package: PACKAGE_NAME.runtime,
|
|
130
|
+
type: "rename"
|
|
131
|
+
},
|
|
132
|
+
useDeactivated: {
|
|
133
|
+
target: "useDeactivated",
|
|
134
|
+
package: PACKAGE_NAME.runtime,
|
|
135
|
+
type: "rename"
|
|
136
|
+
},
|
|
137
|
+
onBeforeMount: {
|
|
138
|
+
target: "useBeforeMount",
|
|
139
|
+
package: PACKAGE_NAME.runtime,
|
|
140
|
+
type: "rename"
|
|
141
|
+
},
|
|
142
|
+
onBeforeUnMount: {
|
|
143
|
+
target: "useBeforeUnMount",
|
|
144
|
+
package: PACKAGE_NAME.runtime,
|
|
145
|
+
type: "rename"
|
|
146
|
+
},
|
|
147
|
+
onMounted: {
|
|
148
|
+
target: "useMounted",
|
|
149
|
+
package: PACKAGE_NAME.runtime,
|
|
150
|
+
type: "rename"
|
|
151
|
+
},
|
|
152
|
+
onUnmounted: {
|
|
153
|
+
target: "useUnmounted",
|
|
154
|
+
package: PACKAGE_NAME.runtime,
|
|
155
|
+
type: "rename"
|
|
156
|
+
},
|
|
157
|
+
onBeforeUpdate: {
|
|
158
|
+
target: "useBeforeUpdate",
|
|
159
|
+
package: PACKAGE_NAME.runtime,
|
|
160
|
+
type: "analyzed-deps"
|
|
161
|
+
},
|
|
162
|
+
onUpdated: {
|
|
163
|
+
target: "useUpdated",
|
|
164
|
+
package: PACKAGE_NAME.runtime,
|
|
165
|
+
type: "analyzed-deps"
|
|
166
|
+
},
|
|
167
|
+
ref: {
|
|
168
|
+
target: "useVRef",
|
|
169
|
+
package: PACKAGE_NAME.runtime,
|
|
170
|
+
type: "rename",
|
|
171
|
+
isTrackable: true
|
|
172
|
+
},
|
|
173
|
+
reactive: {
|
|
174
|
+
target: "useReactive",
|
|
175
|
+
package: PACKAGE_NAME.runtime,
|
|
176
|
+
type: "rename",
|
|
177
|
+
isTrackable: true
|
|
178
|
+
},
|
|
179
|
+
computed: {
|
|
180
|
+
target: "useComputed",
|
|
181
|
+
package: PACKAGE_NAME.runtime,
|
|
182
|
+
type: "rename",
|
|
183
|
+
isTrackable: true
|
|
184
|
+
},
|
|
185
|
+
readonly: {
|
|
186
|
+
target: "useReadonly",
|
|
187
|
+
package: PACKAGE_NAME.runtime,
|
|
188
|
+
type: "rename",
|
|
189
|
+
isTrackable: true
|
|
190
|
+
},
|
|
191
|
+
toRef: {
|
|
192
|
+
target: "useToVRef",
|
|
193
|
+
package: PACKAGE_NAME.runtime,
|
|
194
|
+
type: "rename",
|
|
195
|
+
isTrackable: true
|
|
196
|
+
},
|
|
197
|
+
toRefs: {
|
|
198
|
+
target: "useToVRefs",
|
|
199
|
+
package: PACKAGE_NAME.runtime,
|
|
200
|
+
type: "rename",
|
|
201
|
+
isTrackable: true
|
|
202
|
+
},
|
|
203
|
+
toRaw: {
|
|
204
|
+
target: "useToRaw",
|
|
205
|
+
package: PACKAGE_NAME.runtime,
|
|
206
|
+
type: "rename"
|
|
207
|
+
},
|
|
208
|
+
inject: {
|
|
209
|
+
target: "useInject",
|
|
210
|
+
package: PACKAGE_NAME.runtime,
|
|
211
|
+
type: "rename",
|
|
212
|
+
isTrackable: true
|
|
213
|
+
},
|
|
214
|
+
shallowRef: {
|
|
215
|
+
target: "useShallowRef",
|
|
216
|
+
package: PACKAGE_NAME.runtime,
|
|
217
|
+
type: "rename",
|
|
218
|
+
isTrackable: true
|
|
219
|
+
},
|
|
220
|
+
shallowReactive: {
|
|
221
|
+
target: "useShallowReactive",
|
|
222
|
+
package: PACKAGE_NAME.runtime,
|
|
223
|
+
type: "rename",
|
|
224
|
+
isTrackable: true
|
|
225
|
+
},
|
|
226
|
+
shallowReadonly: {
|
|
227
|
+
target: "useShallowReadonly",
|
|
228
|
+
package: PACKAGE_NAME.runtime,
|
|
229
|
+
type: "rename",
|
|
230
|
+
isTrackable: true
|
|
231
|
+
},
|
|
232
|
+
isRef: {
|
|
233
|
+
target: "isRef",
|
|
234
|
+
package: PACKAGE_NAME.runtime,
|
|
235
|
+
type: "rename",
|
|
236
|
+
isTrackable: true
|
|
237
|
+
},
|
|
238
|
+
isProxy: {
|
|
239
|
+
target: "isProxy",
|
|
240
|
+
package: PACKAGE_NAME.runtime,
|
|
241
|
+
type: "rename",
|
|
242
|
+
isTrackable: true
|
|
243
|
+
},
|
|
244
|
+
isReactive: {
|
|
245
|
+
target: "isReactive",
|
|
246
|
+
package: PACKAGE_NAME.runtime,
|
|
247
|
+
type: "rename",
|
|
248
|
+
isTrackable: true
|
|
249
|
+
},
|
|
250
|
+
watch: {
|
|
251
|
+
target: "useWatch",
|
|
252
|
+
package: PACKAGE_NAME.runtime,
|
|
253
|
+
type: "rename"
|
|
254
|
+
},
|
|
255
|
+
watchEffect: {
|
|
256
|
+
target: "useWatchEffect",
|
|
257
|
+
package: PACKAGE_NAME.runtime,
|
|
258
|
+
type: "analyzed-deps"
|
|
259
|
+
},
|
|
260
|
+
watchPostEffect: {
|
|
261
|
+
target: "useWatchPostEffect",
|
|
262
|
+
package: PACKAGE_NAME.runtime,
|
|
263
|
+
type: "analyzed-deps"
|
|
264
|
+
},
|
|
265
|
+
watchSyncEffect: {
|
|
266
|
+
target: "useWatchSyncEffect",
|
|
267
|
+
package: PACKAGE_NAME.runtime,
|
|
268
|
+
type: "analyzed-deps"
|
|
103
269
|
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
270
|
+
// =============== Directive Utils ===============
|
|
271
|
+
dir: {
|
|
272
|
+
target: "dir",
|
|
273
|
+
package: PACKAGE_NAME.runtime,
|
|
274
|
+
type: "rename"
|
|
275
|
+
},
|
|
276
|
+
dirCls: {
|
|
277
|
+
target: "dir.cls",
|
|
278
|
+
package: PACKAGE_NAME.runtime,
|
|
279
|
+
type: "rename"
|
|
280
|
+
},
|
|
281
|
+
dirKeyless: {
|
|
282
|
+
target: "dir.keyless",
|
|
283
|
+
package: PACKAGE_NAME.runtime,
|
|
284
|
+
type: "rename"
|
|
285
|
+
},
|
|
286
|
+
dirOn: {
|
|
287
|
+
target: "dir.On",
|
|
288
|
+
package: PACKAGE_NAME.runtime,
|
|
289
|
+
type: "rename"
|
|
290
|
+
},
|
|
291
|
+
dirStyle: {
|
|
292
|
+
target: "dir.style",
|
|
293
|
+
package: PACKAGE_NAME.runtime,
|
|
294
|
+
type: "rename"
|
|
295
|
+
},
|
|
296
|
+
nextTick: {
|
|
297
|
+
target: "nextTick",
|
|
298
|
+
package: PACKAGE_NAME.runtime,
|
|
299
|
+
type: "rename"
|
|
108
300
|
}
|
|
109
301
|
},
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
302
|
+
// =================== [VuReact Router] ===================
|
|
303
|
+
router: {
|
|
304
|
+
// =============== Components ===============
|
|
305
|
+
RouterLink: {
|
|
306
|
+
target: "RouterLink",
|
|
307
|
+
package: PACKAGE_NAME.router,
|
|
308
|
+
type: "rename"
|
|
309
|
+
},
|
|
310
|
+
RouterView: {
|
|
311
|
+
target: "RouterView",
|
|
312
|
+
package: PACKAGE_NAME.router,
|
|
313
|
+
type: "rename"
|
|
314
|
+
},
|
|
315
|
+
createRouter: {
|
|
316
|
+
target: "createRouter",
|
|
317
|
+
package: PACKAGE_NAME.router,
|
|
318
|
+
type: "rename"
|
|
319
|
+
},
|
|
320
|
+
// =============== Hooks ===============
|
|
321
|
+
useRoute: {
|
|
322
|
+
target: "useRoute",
|
|
323
|
+
package: PACKAGE_NAME.router,
|
|
324
|
+
type: "rename",
|
|
325
|
+
isTrackable: true
|
|
326
|
+
},
|
|
327
|
+
useRouter: {
|
|
328
|
+
target: "useRouter",
|
|
329
|
+
package: PACKAGE_NAME.router,
|
|
330
|
+
type: "rename",
|
|
331
|
+
isTrackable: true
|
|
332
|
+
},
|
|
333
|
+
useLink: {
|
|
334
|
+
target: "useLink",
|
|
335
|
+
package: PACKAGE_NAME.router,
|
|
336
|
+
type: "rename",
|
|
337
|
+
isTrackable: true
|
|
338
|
+
},
|
|
339
|
+
onBeforeRouteLeave: {
|
|
340
|
+
target: "useBeforeRouteLeave",
|
|
341
|
+
package: PACKAGE_NAME.router,
|
|
342
|
+
type: "rename"
|
|
343
|
+
},
|
|
344
|
+
onBeforeRouteUpdate: {
|
|
345
|
+
target: "useBeforeRouteUpdate",
|
|
346
|
+
package: PACKAGE_NAME.router,
|
|
347
|
+
type: "rename"
|
|
348
|
+
},
|
|
349
|
+
onBeforeRouteEnter: {
|
|
350
|
+
target: "useBeforeRouteEnter",
|
|
351
|
+
package: PACKAGE_NAME.router,
|
|
352
|
+
type: "rename"
|
|
353
|
+
}
|
|
113
354
|
}
|
|
114
355
|
};
|
|
115
356
|
|
|
357
|
+
// src/consts/vue-api-map.ts
|
|
358
|
+
var VUE_API_MAP = {
|
|
359
|
+
ref: "ref",
|
|
360
|
+
provide: "provide",
|
|
361
|
+
watch: "watch",
|
|
362
|
+
watchEffect: "watchEffect",
|
|
363
|
+
watchPostEffect: "watchPostEffect",
|
|
364
|
+
watchSyncEffect: "watchSyncEffect",
|
|
365
|
+
useTemplateRef: "useTemplateRef",
|
|
366
|
+
defineAsyncComponent: "defineAsyncComponent",
|
|
367
|
+
DynamicComponent: "Component",
|
|
368
|
+
Transition: "Transition",
|
|
369
|
+
RouterLink: "RouterLink"
|
|
370
|
+
};
|
|
371
|
+
|
|
116
372
|
// src/core/codegen/component/jsx/utils/jsx-element-utils.ts
|
|
117
373
|
import * as t from "@babel/types";
|
|
118
374
|
function createJsxElement(tag, props, children, selfClosing) {
|
|
@@ -705,7 +961,8 @@ function buildCtxProviderNode(nodeIR, ctx, children) {
|
|
|
705
961
|
buildJsxExpressionNode(t10.identifier(value))
|
|
706
962
|
);
|
|
707
963
|
void ctx;
|
|
708
|
-
|
|
964
|
+
const adpater = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
965
|
+
return createJsxElement(adpater.target, [keyProp, valueProp], childNodes);
|
|
709
966
|
}
|
|
710
967
|
|
|
711
968
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-root-jsx.ts
|
|
@@ -783,9 +1040,11 @@ var REACT_API_MAP = {
|
|
|
783
1040
|
lazy: "lazy",
|
|
784
1041
|
memo: "memo",
|
|
785
1042
|
useRef: "useRef",
|
|
786
|
-
useCallback: "useCallback",
|
|
787
1043
|
useMemo: "useMemo",
|
|
788
|
-
ReactNode: "ReactNode"
|
|
1044
|
+
ReactNode: "ReactNode",
|
|
1045
|
+
forwardRef: "forwardRef",
|
|
1046
|
+
useCallback: "useCallback",
|
|
1047
|
+
useImperativeHandle: "useImperativeHandle"
|
|
789
1048
|
};
|
|
790
1049
|
|
|
791
1050
|
// src/utils/camelCase.ts
|
|
@@ -825,10 +1084,13 @@ function buildComponent(scriptIR, ctx, state) {
|
|
|
825
1084
|
const hasProps = getHasProps(propsTSIface.propsTypes) || getHasProps(propsTSIface.emitTypes) || getHasProps(propsTSIface.slotTypes);
|
|
826
1085
|
const shouldMemo = scriptIR !== null || hasProps;
|
|
827
1086
|
const jsxStatement = t12.returnStatement(state.jsx || t12.nullLiteral());
|
|
828
|
-
const component = !shouldMemo ?
|
|
1087
|
+
const component = !shouldMemo ? resolvePureUIComponent(jsxStatement, ctx) : resolveMemoComponent(scriptIR.statement.local, jsxStatement, ctx);
|
|
829
1088
|
state.component = component;
|
|
830
1089
|
}
|
|
831
|
-
function
|
|
1090
|
+
function getHasProps(list) {
|
|
1091
|
+
return list.length > 0;
|
|
1092
|
+
}
|
|
1093
|
+
function resolvePureUIComponent(jsxStmt, ctx) {
|
|
832
1094
|
return t12.variableDeclaration("const", [
|
|
833
1095
|
t12.variableDeclarator(
|
|
834
1096
|
resolveComponentName(ctx),
|
|
@@ -838,12 +1100,14 @@ function resolveComponent(jsxStmt, ctx) {
|
|
|
838
1100
|
}
|
|
839
1101
|
function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
840
1102
|
const name = resolveComponentName(ctx);
|
|
841
|
-
const param =
|
|
842
|
-
const
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
)
|
|
846
|
-
|
|
1103
|
+
const param = resolvePropsParam(ctx);
|
|
1104
|
+
const body = t12.blockStatement(resolveLocalStatements(local, jsxStmt));
|
|
1105
|
+
const component = resolveComponent(param, body, ctx);
|
|
1106
|
+
let compFn = component;
|
|
1107
|
+
if (ctx.scriptData.forwardRef.enabled) {
|
|
1108
|
+
compFn = resolveForwardRef(body, ctx);
|
|
1109
|
+
}
|
|
1110
|
+
const memoCall = t12.callExpression(t12.identifier(REACT_API_MAP.memo), [compFn]);
|
|
847
1111
|
return t12.variableDeclaration("const", [t12.variableDeclarator(name, memoCall)]);
|
|
848
1112
|
}
|
|
849
1113
|
function resolveComponentName(ctx) {
|
|
@@ -860,7 +1124,52 @@ function resolveComponentName(ctx) {
|
|
|
860
1124
|
}
|
|
861
1125
|
return t12.identifier(name);
|
|
862
1126
|
}
|
|
863
|
-
function
|
|
1127
|
+
function resolveLocalStatements(local, jsx) {
|
|
1128
|
+
const stmts = [jsx];
|
|
1129
|
+
if (!local) return stmts;
|
|
1130
|
+
if (typeof local === "object" && "program" in local) {
|
|
1131
|
+
const program3 = local.program;
|
|
1132
|
+
if (program3?.body?.length) {
|
|
1133
|
+
return [...program3.body, jsx];
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
return stmts;
|
|
1137
|
+
}
|
|
1138
|
+
function resolveComponent(param, body, ctx) {
|
|
1139
|
+
const {
|
|
1140
|
+
scriptData: { forwardRef }
|
|
1141
|
+
} = ctx;
|
|
1142
|
+
if (forwardRef.enabled) {
|
|
1143
|
+
return resolveForwardRef(body, ctx);
|
|
1144
|
+
}
|
|
1145
|
+
const baseComponent = t12.arrowFunctionExpression(!param ? [] : [param], body);
|
|
1146
|
+
return baseComponent;
|
|
1147
|
+
}
|
|
1148
|
+
function resolveForwardRef(body, ctx) {
|
|
1149
|
+
const {
|
|
1150
|
+
propField,
|
|
1151
|
+
scriptData: { forwardRef, lang }
|
|
1152
|
+
} = ctx;
|
|
1153
|
+
const params = [t12.identifier(forwardRef.refField)];
|
|
1154
|
+
const propsId = resolvePropsParam(ctx);
|
|
1155
|
+
const callExpr = t12.callExpression(t12.identifier(REACT_API_MAP.forwardRef), [
|
|
1156
|
+
t12.arrowFunctionExpression(params, body)
|
|
1157
|
+
]);
|
|
1158
|
+
if (lang.startsWith("ts")) {
|
|
1159
|
+
const types = [t12.tsAnyKeyword()];
|
|
1160
|
+
if (propsId) {
|
|
1161
|
+
const propsType = propsId?.typeAnnotation?.typeAnnotation;
|
|
1162
|
+
types.push(propsType || t12.tsAnyKeyword());
|
|
1163
|
+
}
|
|
1164
|
+
callExpr.typeParameters = t12.tsTypeParameterInstantiation(types);
|
|
1165
|
+
}
|
|
1166
|
+
if (propsId) {
|
|
1167
|
+
propsId.typeAnnotation = null;
|
|
1168
|
+
params.unshift(propsId);
|
|
1169
|
+
}
|
|
1170
|
+
return callExpr;
|
|
1171
|
+
}
|
|
1172
|
+
function resolvePropsParam(ctx) {
|
|
864
1173
|
const { propField, scriptData } = ctx;
|
|
865
1174
|
const { propsTSIface } = scriptData;
|
|
866
1175
|
const propsIdentifier = t12.identifier(propField);
|
|
@@ -877,20 +1186,6 @@ function resolveParam(ctx) {
|
|
|
877
1186
|
propsIdentifier.typeAnnotation = t12.tsTypeAnnotation(t12.tsTypeReference(typeIdentifier));
|
|
878
1187
|
return propsIdentifier;
|
|
879
1188
|
}
|
|
880
|
-
function resolveStatements(local, jsx) {
|
|
881
|
-
const stmts = [jsx];
|
|
882
|
-
if (!local) return stmts;
|
|
883
|
-
if (typeof local === "object" && "program" in local) {
|
|
884
|
-
const program3 = local.program;
|
|
885
|
-
if (program3?.body?.length) {
|
|
886
|
-
return [...program3.body, jsx];
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
return stmts;
|
|
890
|
-
}
|
|
891
|
-
function getHasProps(list) {
|
|
892
|
-
return list.length > 0;
|
|
893
|
-
}
|
|
894
1189
|
|
|
895
1190
|
// src/core/codegen/component/script/syntax-processor/process/build-program-preamble.ts
|
|
896
1191
|
function buildProgramPreamble(nodeIR, ctx, state) {
|
|
@@ -1234,11 +1529,6 @@ var HTML_TAG_TYPES = {
|
|
|
1234
1529
|
frame: "HTMLFrameElement"
|
|
1235
1530
|
};
|
|
1236
1531
|
|
|
1237
|
-
// src/consts/vue-api-map.ts
|
|
1238
|
-
var VUE_API_MAP = {
|
|
1239
|
-
useTemplateRef: "useTemplateRef"
|
|
1240
|
-
};
|
|
1241
|
-
|
|
1242
1532
|
// src/consts/reactive-type-map.ts
|
|
1243
1533
|
var REACTIVE_TYPE_MAP = {
|
|
1244
1534
|
ref: "ref",
|
|
@@ -1294,7 +1584,7 @@ function collectReactiveBindings(node, ctx) {
|
|
|
1294
1584
|
if (!reactiveStateApis.has(source)) return;
|
|
1295
1585
|
const varName = node.id.name;
|
|
1296
1586
|
const value = init.arguments[0];
|
|
1297
|
-
if (source ===
|
|
1587
|
+
if (source === MACRO_API_NAMES.props) {
|
|
1298
1588
|
ctx.propField = varName;
|
|
1299
1589
|
}
|
|
1300
1590
|
reactiveBindings[varName] = {
|
|
@@ -1311,55 +1601,13 @@ function collectRefBindings(node, ctx) {
|
|
|
1311
1601
|
if (callee.name !== VUE_API_MAP.useTemplateRef) return;
|
|
1312
1602
|
const idName = node.id.name;
|
|
1313
1603
|
const tag = init.arguments[0]?.value;
|
|
1314
|
-
refBindings[idName] = {
|
|
1604
|
+
refBindings.domRefs[idName] = {
|
|
1315
1605
|
tag,
|
|
1316
1606
|
htmlType: HTML_TAG_TYPES[tag] || "HTMLElement",
|
|
1317
1607
|
name: idName
|
|
1318
1608
|
};
|
|
1319
1609
|
}
|
|
1320
1610
|
|
|
1321
|
-
// src/consts/other.ts
|
|
1322
|
-
var PACKAGE_NAME = {
|
|
1323
|
-
react: "react",
|
|
1324
|
-
runtime: "@vureact/runtime-core",
|
|
1325
|
-
router: "@vureact/router"
|
|
1326
|
-
};
|
|
1327
|
-
var STYLE_MODULE_NAME = "$style";
|
|
1328
|
-
var MACRO_API_NAMES = {
|
|
1329
|
-
props: "defineProps",
|
|
1330
|
-
emits: "defineEmits",
|
|
1331
|
-
slots: "defineSlots",
|
|
1332
|
-
options: "defineOptions"
|
|
1333
|
-
};
|
|
1334
|
-
var DOLLAR_IDENTIFIERS = [
|
|
1335
|
-
"$data",
|
|
1336
|
-
"$props",
|
|
1337
|
-
"$el",
|
|
1338
|
-
"$options",
|
|
1339
|
-
"$parent",
|
|
1340
|
-
"$root",
|
|
1341
|
-
"$slots",
|
|
1342
|
-
"$refs",
|
|
1343
|
-
"$attrs",
|
|
1344
|
-
"$emit",
|
|
1345
|
-
"$watch",
|
|
1346
|
-
"$forceUpdate",
|
|
1347
|
-
"$nextTick",
|
|
1348
|
-
"$router",
|
|
1349
|
-
"$route",
|
|
1350
|
-
"$store",
|
|
1351
|
-
"$pinia",
|
|
1352
|
-
"$event"
|
|
1353
|
-
];
|
|
1354
|
-
var VUE_PACKAGES = [
|
|
1355
|
-
"vue",
|
|
1356
|
-
"vite",
|
|
1357
|
-
"pinia",
|
|
1358
|
-
"nuxt",
|
|
1359
|
-
// 不需要带到 React 项目中
|
|
1360
|
-
"@vureact/compiler-core"
|
|
1361
|
-
];
|
|
1362
|
-
|
|
1363
1611
|
// src/plugins/postcss.ts
|
|
1364
1612
|
import postcss from "postcss";
|
|
1365
1613
|
function processScopedWithPostCss(input, hash) {
|
|
@@ -1998,66 +2246,13 @@ function collectLocalStatements(ctx, ast) {
|
|
|
1998
2246
|
scriptIR.statement.local = ast;
|
|
1999
2247
|
}
|
|
2000
2248
|
|
|
2001
|
-
// src/core/transform/sfc/script/syntax-processor/
|
|
2249
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2002
2250
|
import * as t20 from "@babel/types";
|
|
2003
|
-
function
|
|
2004
|
-
if (ctx.inputType !== "sfc") return {};
|
|
2005
|
-
return {
|
|
2006
|
-
CallExpression(path7) {
|
|
2007
|
-
const { node } = path7;
|
|
2008
|
-
if (!isCalleeNamed(node, VUE_API_MAP.useTemplateRef)) return;
|
|
2009
|
-
addTypeParam(ctx, path7);
|
|
2010
|
-
replaceValue(node);
|
|
2011
|
-
replaceCallName(node, REACT_API_MAP.useRef);
|
|
2012
|
-
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
2013
|
-
},
|
|
2014
|
-
MemberExpression(path7) {
|
|
2015
|
-
const { node } = path7;
|
|
2016
|
-
if (node.computed || !t20.isIdentifier(node.property) || node.property.name !== "value") {
|
|
2017
|
-
return;
|
|
2018
|
-
}
|
|
2019
|
-
const rootPath = findRootVariablePath(path7);
|
|
2020
|
-
if (!rootPath?.node || !t20.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
2021
|
-
return;
|
|
2022
|
-
}
|
|
2023
|
-
const rootId = findRootIdentifier(node);
|
|
2024
|
-
if (!t20.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
2025
|
-
return;
|
|
2026
|
-
}
|
|
2027
|
-
node.property.name = "current";
|
|
2028
|
-
}
|
|
2029
|
-
};
|
|
2030
|
-
}
|
|
2031
|
-
function replaceValue(node) {
|
|
2032
|
-
const initVal = node.arguments[0];
|
|
2033
|
-
const newVal = t20.identifier("null");
|
|
2034
|
-
newVal.start = initVal?.start;
|
|
2035
|
-
newVal.end = initVal?.end;
|
|
2036
|
-
newVal.loc = initVal?.loc;
|
|
2037
|
-
node.arguments[0] = newVal;
|
|
2038
|
-
}
|
|
2039
|
-
function addTypeParam(ctx, path7) {
|
|
2040
|
-
const { templateData, scriptData } = ctx;
|
|
2041
|
-
const { node, parent } = path7;
|
|
2042
|
-
if (!scriptData.lang.startsWith("ts") && !t20.isVariableDeclarator(parent)) {
|
|
2043
|
-
return;
|
|
2044
|
-
}
|
|
2045
|
-
const idName = parent.id.name;
|
|
2046
|
-
const bindingMeta = templateData.refBindings[idName];
|
|
2047
|
-
if (!node.typeParameters && bindingMeta) {
|
|
2048
|
-
node.typeParameters = t20.tsTypeParameterInstantiation([
|
|
2049
|
-
t20.tsTypeReference(t20.identifier(bindingMeta.htmlType))
|
|
2050
|
-
]);
|
|
2051
|
-
}
|
|
2052
|
-
}
|
|
2053
|
-
|
|
2054
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-async-component.ts
|
|
2055
|
-
import * as t21 from "@babel/types";
|
|
2056
|
-
function resolveAsyncComponent(ctx) {
|
|
2251
|
+
function resolveDefineAsyncComponent(ctx) {
|
|
2057
2252
|
return {
|
|
2058
2253
|
CallExpression(path7) {
|
|
2059
2254
|
const { node } = path7;
|
|
2060
|
-
if (!isCalleeNamed(node,
|
|
2255
|
+
if (!isCalleeNamed(node, VUE_API_MAP.defineAsyncComponent)) {
|
|
2061
2256
|
return;
|
|
2062
2257
|
}
|
|
2063
2258
|
const [arg] = node.arguments;
|
|
@@ -2068,11 +2263,11 @@ function resolveAsyncComponent(ctx) {
|
|
|
2068
2263
|
};
|
|
2069
2264
|
}
|
|
2070
2265
|
function checkIsUnsupported(ctx, arg) {
|
|
2071
|
-
if (
|
|
2266
|
+
if (t20.isFunction(arg)) {
|
|
2072
2267
|
checkIsDynamicImport(ctx, arg);
|
|
2073
|
-
} else if (
|
|
2268
|
+
} else if (t20.isObjectExpression(arg)) {
|
|
2074
2269
|
const { value } = arg.properties.find(
|
|
2075
|
-
(p) =>
|
|
2270
|
+
(p) => t20.isObjectProperty(p) && t20.isIdentifier(p.key) && p.key.name === "loader"
|
|
2076
2271
|
);
|
|
2077
2272
|
checkIsDynamicImport(ctx, value);
|
|
2078
2273
|
if (arg.properties.length > 1) {
|
|
@@ -2083,7 +2278,7 @@ function checkIsUnsupported(ctx, arg) {
|
|
|
2083
2278
|
function checkIsDynamicImport(ctx, node) {
|
|
2084
2279
|
const { scriptData, filename } = ctx;
|
|
2085
2280
|
const warnIsNotImport = (target) => {
|
|
2086
|
-
if (!target || !
|
|
2281
|
+
if (!target || !t20.isImport(target)) {
|
|
2087
2282
|
logger.error(
|
|
2088
2283
|
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2089
2284
|
{
|
|
@@ -2094,20 +2289,20 @@ function checkIsDynamicImport(ctx, node) {
|
|
|
2094
2289
|
);
|
|
2095
2290
|
}
|
|
2096
2291
|
};
|
|
2097
|
-
if (
|
|
2292
|
+
if (t20.isFunction(node)) {
|
|
2098
2293
|
checkIsDynamicImport(ctx, node.body);
|
|
2099
2294
|
return;
|
|
2100
2295
|
}
|
|
2101
|
-
if (
|
|
2296
|
+
if (t20.isBlockStatement(node)) {
|
|
2102
2297
|
const [returnSmt] = node.body;
|
|
2103
|
-
if (
|
|
2298
|
+
if (t20.isReturnStatement(returnSmt)) {
|
|
2104
2299
|
warnIsNotImport(returnSmt.argument);
|
|
2105
2300
|
}
|
|
2106
2301
|
return;
|
|
2107
2302
|
}
|
|
2108
|
-
if (
|
|
2303
|
+
if (t20.isCallExpression(node)) {
|
|
2109
2304
|
warnIsNotImport(node.callee);
|
|
2110
|
-
if (
|
|
2305
|
+
if (t20.isStringLiteral(node.arguments[0])) {
|
|
2111
2306
|
replaceVueSuffix(ctx, node.arguments[0]);
|
|
2112
2307
|
}
|
|
2113
2308
|
return;
|
|
@@ -2155,61 +2350,64 @@ function pushToGlobalScope(path7, ctx) {
|
|
|
2155
2350
|
scriptIR.statement.global.push(fullNode);
|
|
2156
2351
|
}
|
|
2157
2352
|
|
|
2158
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-
|
|
2353
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2159
2354
|
import * as t22 from "@babel/types";
|
|
2160
|
-
|
|
2355
|
+
|
|
2356
|
+
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2357
|
+
import * as t21 from "@babel/types";
|
|
2358
|
+
function createUseCallback(body, deps) {
|
|
2359
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useCallback), [
|
|
2360
|
+
body,
|
|
2361
|
+
deps ?? t21.arrayExpression([])
|
|
2362
|
+
]);
|
|
2363
|
+
}
|
|
2364
|
+
function createUseMemo(body, deps) {
|
|
2365
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useMemo), [
|
|
2366
|
+
t21.arrowFunctionExpression([], body),
|
|
2367
|
+
deps ?? t21.arrayExpression([])
|
|
2368
|
+
]);
|
|
2369
|
+
}
|
|
2370
|
+
function createUseImperativeHandle(refId, init) {
|
|
2371
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2375
|
+
function resolveDefineExpose(ctx) {
|
|
2376
|
+
if (ctx.inputType !== "sfc") return {};
|
|
2161
2377
|
return {
|
|
2162
2378
|
CallExpression(path7) {
|
|
2163
2379
|
const { node } = path7;
|
|
2164
|
-
const { filename,
|
|
2165
|
-
if (!
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
if (meta) {
|
|
2171
|
-
result = meta.source === MACRO_API_NAMES.emits;
|
|
2172
|
-
}
|
|
2173
|
-
if (!result) {
|
|
2174
|
-
const binding = path7.scope.getBinding(name);
|
|
2175
|
-
if (binding) {
|
|
2176
|
-
const parent = binding.path.node;
|
|
2177
|
-
if (t22.isVariableDeclarator(parent) && t22.isCallExpression(parent.init) && t22.isIdentifier(parent.init.callee)) {
|
|
2178
|
-
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2179
|
-
}
|
|
2180
|
-
}
|
|
2181
|
-
}
|
|
2182
|
-
return result;
|
|
2183
|
-
};
|
|
2184
|
-
if (!checkIfFromDefineEmits()) return;
|
|
2185
|
-
const [callee, ...args] = node.arguments;
|
|
2186
|
-
const eventName = t22.isStringLiteral(callee) ? `on${capitalize(camelCase(callee.value))}` : void 0;
|
|
2187
|
-
if (!eventName) {
|
|
2188
|
-
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2189
|
-
file: filename,
|
|
2190
|
-
source: scriptData.source,
|
|
2191
|
-
loc: callee?.loc
|
|
2192
|
-
});
|
|
2380
|
+
const { filename, scriptData } = ctx;
|
|
2381
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.expose)) {
|
|
2382
|
+
return;
|
|
2383
|
+
}
|
|
2384
|
+
const [expose] = node.arguments;
|
|
2385
|
+
if (!expose) {
|
|
2193
2386
|
path7.remove();
|
|
2194
2387
|
return;
|
|
2195
2388
|
}
|
|
2196
|
-
const
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2389
|
+
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2390
|
+
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2391
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
2392
|
+
if (!t22.isObjectExpression(expose) && !t22.isFunction(expose)) {
|
|
2393
|
+
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2394
|
+
file: filename,
|
|
2395
|
+
loc: expose.loc,
|
|
2396
|
+
source: scriptData.source
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
const init = !t22.isFunction(expose) ? t22.arrowFunctionExpression([], expose) : expose;
|
|
2400
|
+
const { forwardRef } = scriptData;
|
|
2401
|
+
const newNode = createUseImperativeHandle(t22.identifier(forwardRef.refField), init);
|
|
2402
|
+
forwardRef.enabled = true;
|
|
2403
|
+
path7.replaceWith(newNode);
|
|
2206
2404
|
}
|
|
2207
2405
|
};
|
|
2208
2406
|
}
|
|
2209
2407
|
|
|
2210
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-options.ts
|
|
2408
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2211
2409
|
import * as t23 from "@babel/types";
|
|
2212
|
-
function
|
|
2410
|
+
function resolveDefineOptions(ctx) {
|
|
2213
2411
|
return {
|
|
2214
2412
|
CallExpression(path7) {
|
|
2215
2413
|
const { node } = path7;
|
|
@@ -2253,39 +2451,91 @@ function extractName(prop, ctx) {
|
|
|
2253
2451
|
ctx.compName = prop.value.value;
|
|
2254
2452
|
return;
|
|
2255
2453
|
}
|
|
2256
|
-
logger.error(`name must be a string type, but got ${prop.value.type}`, {
|
|
2257
|
-
source: scriptData.source,
|
|
2258
|
-
file: filename,
|
|
2259
|
-
loc: prop.key?.loc
|
|
2260
|
-
});
|
|
2261
|
-
}
|
|
2454
|
+
logger.error(`name must be a string type, but got ${prop.value.type}`, {
|
|
2455
|
+
source: scriptData.source,
|
|
2456
|
+
file: filename,
|
|
2457
|
+
loc: prop.key?.loc
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
|
|
2462
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2463
|
+
import * as t24 from "@babel/types";
|
|
2464
|
+
function resolveEmitCalls(ctx) {
|
|
2465
|
+
return {
|
|
2466
|
+
CallExpression(path7) {
|
|
2467
|
+
const { node } = path7;
|
|
2468
|
+
const { filename, templateData, scriptData } = ctx;
|
|
2469
|
+
if (!t24.isIdentifier(node.callee)) return;
|
|
2470
|
+
const { name } = node.callee;
|
|
2471
|
+
const checkIfFromDefineEmits = () => {
|
|
2472
|
+
let result = false;
|
|
2473
|
+
const meta = templateData.reactiveBindings[name];
|
|
2474
|
+
if (meta) {
|
|
2475
|
+
result = meta.source === MACRO_API_NAMES.emits;
|
|
2476
|
+
}
|
|
2477
|
+
if (!result) {
|
|
2478
|
+
const binding = path7.scope.getBinding(name);
|
|
2479
|
+
if (binding) {
|
|
2480
|
+
const parent = binding.path.node;
|
|
2481
|
+
if (t24.isVariableDeclarator(parent) && t24.isCallExpression(parent.init) && t24.isIdentifier(parent.init.callee)) {
|
|
2482
|
+
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
return result;
|
|
2487
|
+
};
|
|
2488
|
+
if (!checkIfFromDefineEmits()) return;
|
|
2489
|
+
const [callee, ...args] = node.arguments;
|
|
2490
|
+
const eventName = t24.isStringLiteral(callee) ? `on${capitalize(camelCase(callee.value))}` : void 0;
|
|
2491
|
+
if (!eventName) {
|
|
2492
|
+
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2493
|
+
file: filename,
|
|
2494
|
+
source: scriptData.source,
|
|
2495
|
+
loc: callee?.loc
|
|
2496
|
+
});
|
|
2497
|
+
path7.remove();
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
const propCall = t24.callExpression(
|
|
2501
|
+
t24.optionalMemberExpression(
|
|
2502
|
+
t24.identifier(ctx.propField),
|
|
2503
|
+
t24.identifier(eventName),
|
|
2504
|
+
false,
|
|
2505
|
+
true
|
|
2506
|
+
),
|
|
2507
|
+
args
|
|
2508
|
+
);
|
|
2509
|
+
path7.replaceWith(propCall);
|
|
2510
|
+
}
|
|
2511
|
+
};
|
|
2262
2512
|
}
|
|
2263
2513
|
|
|
2264
2514
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2265
|
-
import * as
|
|
2515
|
+
import * as t29 from "@babel/types";
|
|
2266
2516
|
|
|
2267
2517
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2268
|
-
import * as
|
|
2518
|
+
import * as t26 from "@babel/types";
|
|
2269
2519
|
|
|
2270
2520
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2271
|
-
import * as
|
|
2521
|
+
import * as t25 from "@babel/types";
|
|
2272
2522
|
function cloneCallableParams(params) {
|
|
2273
2523
|
const cloneCallableParam = (param, index) => {
|
|
2274
|
-
if (
|
|
2524
|
+
if (t25.isRestElement(param)) {
|
|
2275
2525
|
const arg = param.argument;
|
|
2276
|
-
const name =
|
|
2277
|
-
const rest =
|
|
2278
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
2526
|
+
const name = t25.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
2527
|
+
const rest = t25.restElement(t25.identifier(name));
|
|
2528
|
+
rest.typeAnnotation = param.typeAnnotation || (t25.isIdentifier(arg) ? arg.typeAnnotation : null) || t25.tsTypeAnnotation(t25.tsArrayType(t25.tsAnyKeyword()));
|
|
2279
2529
|
return rest;
|
|
2280
2530
|
}
|
|
2281
|
-
if (
|
|
2282
|
-
const id =
|
|
2531
|
+
if (t25.isIdentifier(param)) {
|
|
2532
|
+
const id = t25.identifier(param.name || `arg${index}`);
|
|
2283
2533
|
id.optional = param.optional;
|
|
2284
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
2534
|
+
id.typeAnnotation = param.typeAnnotation || t25.tsTypeAnnotation(t25.tsAnyKeyword());
|
|
2285
2535
|
return id;
|
|
2286
2536
|
}
|
|
2287
|
-
const fallback =
|
|
2288
|
-
fallback.typeAnnotation =
|
|
2537
|
+
const fallback = t25.identifier(`arg${index}`);
|
|
2538
|
+
fallback.typeAnnotation = t25.tsTypeAnnotation(t25.tsAnyKeyword());
|
|
2289
2539
|
return fallback;
|
|
2290
2540
|
};
|
|
2291
2541
|
return params.map(cloneCallableParam);
|
|
@@ -2295,18 +2545,18 @@ function cloneCallableParams(params) {
|
|
|
2295
2545
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2296
2546
|
return {
|
|
2297
2547
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path7) {
|
|
2298
|
-
if (!
|
|
2548
|
+
if (!t26.isProgram(path7.parent)) return;
|
|
2299
2549
|
const { node } = path7;
|
|
2300
|
-
if (
|
|
2301
|
-
const typeLiteral =
|
|
2550
|
+
if (t26.isTSInterfaceDeclaration(node)) {
|
|
2551
|
+
const typeLiteral = t26.tsTypeLiteral(node.body.body);
|
|
2302
2552
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2303
2553
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2304
|
-
if (resolved &&
|
|
2554
|
+
if (resolved && t26.isTSTypeLiteral(resolved)) {
|
|
2305
2555
|
node.body.body = resolved.members;
|
|
2306
2556
|
}
|
|
2307
2557
|
return;
|
|
2308
2558
|
}
|
|
2309
|
-
if (
|
|
2559
|
+
if (t26.isTSTypeAliasDeclaration(node)) {
|
|
2310
2560
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2311
2561
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2312
2562
|
if (resolved) {
|
|
@@ -2317,47 +2567,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2317
2567
|
};
|
|
2318
2568
|
}
|
|
2319
2569
|
function resolveTopLevelEmitType(tsType) {
|
|
2320
|
-
if (
|
|
2570
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2321
2571
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2322
2572
|
}
|
|
2323
|
-
if (
|
|
2573
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2324
2574
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2325
2575
|
return tsType;
|
|
2326
2576
|
}
|
|
2327
2577
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2328
|
-
return
|
|
2578
|
+
return t26.tsTypeReference(
|
|
2329
2579
|
tsType.typeName,
|
|
2330
|
-
|
|
2580
|
+
t26.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2331
2581
|
);
|
|
2332
2582
|
}
|
|
2333
|
-
if (
|
|
2583
|
+
if (t26.isTSIntersectionType(tsType)) {
|
|
2334
2584
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2335
2585
|
if (!types.length) return null;
|
|
2336
2586
|
if (types.length === 1) return types[0];
|
|
2337
|
-
return
|
|
2587
|
+
return t26.tsIntersectionType(types);
|
|
2338
2588
|
}
|
|
2339
|
-
if (
|
|
2589
|
+
if (t26.isTSUnionType(tsType)) {
|
|
2340
2590
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2341
2591
|
if (!types.length) return null;
|
|
2342
2592
|
if (types.length === 1) return types[0];
|
|
2343
|
-
return
|
|
2593
|
+
return t26.tsUnionType(types);
|
|
2344
2594
|
}
|
|
2345
|
-
if (
|
|
2595
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2346
2596
|
const members = [];
|
|
2347
2597
|
for (const member of tsType.members) {
|
|
2348
|
-
if (
|
|
2598
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2349
2599
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2350
2600
|
continue;
|
|
2351
2601
|
}
|
|
2352
2602
|
members.push(member);
|
|
2353
2603
|
}
|
|
2354
2604
|
if (!members.length) return null;
|
|
2355
|
-
return
|
|
2605
|
+
return t26.tsTypeLiteral(members);
|
|
2356
2606
|
}
|
|
2357
|
-
if (
|
|
2607
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2358
2608
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2359
2609
|
if (!props.length) return null;
|
|
2360
|
-
return
|
|
2610
|
+
return t26.tsTypeLiteral(props);
|
|
2361
2611
|
}
|
|
2362
2612
|
return tsType;
|
|
2363
2613
|
}
|
|
@@ -2378,41 +2628,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2378
2628
|
propsTSIface: { emitTypes }
|
|
2379
2629
|
} = ctx.scriptData;
|
|
2380
2630
|
const members = [];
|
|
2381
|
-
if (
|
|
2631
|
+
if (t26.isArrayExpression(runtimeArg)) {
|
|
2382
2632
|
for (const element of runtimeArg.elements) {
|
|
2383
|
-
if (!element || !
|
|
2633
|
+
if (!element || !t26.isStringLiteral(element)) continue;
|
|
2384
2634
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2385
2635
|
const key = buildKey(handlerName);
|
|
2386
|
-
const fnType =
|
|
2636
|
+
const fnType = t26.tsFunctionType(
|
|
2387
2637
|
null,
|
|
2388
2638
|
[createRestAnyParam("args")],
|
|
2389
|
-
|
|
2639
|
+
t26.tsTypeAnnotation(t26.tsAnyKeyword())
|
|
2390
2640
|
);
|
|
2391
|
-
const prop =
|
|
2641
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2392
2642
|
prop.optional = true;
|
|
2393
2643
|
members.push(prop);
|
|
2394
2644
|
}
|
|
2395
2645
|
if (members.length) {
|
|
2396
|
-
emitTypes.push(
|
|
2646
|
+
emitTypes.push(t26.tsTypeLiteral(members));
|
|
2397
2647
|
}
|
|
2398
2648
|
return;
|
|
2399
2649
|
}
|
|
2400
|
-
if (
|
|
2650
|
+
if (t26.isObjectExpression(runtimeArg)) {
|
|
2401
2651
|
for (const prop of runtimeArg.properties) {
|
|
2402
|
-
if (!
|
|
2403
|
-
if (
|
|
2652
|
+
if (!t26.isObjectProperty(prop)) continue;
|
|
2653
|
+
if (t26.isSpreadElement(prop)) continue;
|
|
2404
2654
|
const rawName = resolvePropName(prop.key);
|
|
2405
2655
|
if (!rawName) continue;
|
|
2406
2656
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2407
2657
|
const key = buildKey(handlerName);
|
|
2408
|
-
const params =
|
|
2409
|
-
const fnType =
|
|
2410
|
-
const propSig =
|
|
2658
|
+
const params = t26.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
2659
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(t26.tsAnyKeyword()));
|
|
2660
|
+
const propSig = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2411
2661
|
propSig.optional = true;
|
|
2412
2662
|
members.push(propSig);
|
|
2413
2663
|
}
|
|
2414
2664
|
if (members.length) {
|
|
2415
|
-
emitTypes.push(
|
|
2665
|
+
emitTypes.push(t26.tsTypeLiteral(members));
|
|
2416
2666
|
}
|
|
2417
2667
|
}
|
|
2418
2668
|
}
|
|
@@ -2433,129 +2683,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2433
2683
|
return `on${name}`;
|
|
2434
2684
|
}
|
|
2435
2685
|
function resolvePropName(key) {
|
|
2436
|
-
if (
|
|
2437
|
-
if (
|
|
2438
|
-
if (
|
|
2686
|
+
if (t26.isIdentifier(key)) return key.name;
|
|
2687
|
+
if (t26.isStringLiteral(key)) return key.value;
|
|
2688
|
+
if (t26.isNumericLiteral(key)) return String(key.value);
|
|
2439
2689
|
return null;
|
|
2440
2690
|
}
|
|
2441
2691
|
function buildKey(name) {
|
|
2442
|
-
return
|
|
2692
|
+
return t26.isValidIdentifier(name) ? t26.identifier(name) : t26.stringLiteral(name);
|
|
2443
2693
|
}
|
|
2444
2694
|
function createRestAnyParam(name) {
|
|
2445
|
-
const id =
|
|
2446
|
-
const rest =
|
|
2447
|
-
rest.typeAnnotation =
|
|
2695
|
+
const id = t26.identifier(name);
|
|
2696
|
+
const rest = t26.restElement(id);
|
|
2697
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(t26.tsArrayType(t26.tsAnyKeyword()));
|
|
2448
2698
|
return rest;
|
|
2449
2699
|
}
|
|
2450
2700
|
function resolveRuntimeTupleParams(value) {
|
|
2451
2701
|
const params = [];
|
|
2452
2702
|
value.elements.forEach((element, index) => {
|
|
2453
2703
|
if (!element) return;
|
|
2454
|
-
if (
|
|
2704
|
+
if (t26.isSpreadElement(element)) {
|
|
2455
2705
|
params.push(createRestAnyParam(`args${index}`));
|
|
2456
2706
|
return;
|
|
2457
2707
|
}
|
|
2458
|
-
if (
|
|
2459
|
-
const id =
|
|
2460
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
2708
|
+
if (t26.isIdentifier(element)) {
|
|
2709
|
+
const id = t26.identifier(element.name);
|
|
2710
|
+
id.typeAnnotation = element.typeAnnotation || t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2461
2711
|
params.push(id);
|
|
2462
2712
|
return;
|
|
2463
2713
|
}
|
|
2464
|
-
if (
|
|
2465
|
-
const id =
|
|
2466
|
-
id.typeAnnotation =
|
|
2714
|
+
if (t26.isTSAsExpression(element)) {
|
|
2715
|
+
const id = t26.identifier(`arg${index}`);
|
|
2716
|
+
id.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2467
2717
|
params.push(id);
|
|
2468
2718
|
return;
|
|
2469
2719
|
}
|
|
2470
|
-
const fallback =
|
|
2471
|
-
fallback.typeAnnotation =
|
|
2720
|
+
const fallback = t26.identifier(`arg${index}`);
|
|
2721
|
+
fallback.typeAnnotation = t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2472
2722
|
params.push(fallback);
|
|
2473
2723
|
});
|
|
2474
2724
|
return params;
|
|
2475
2725
|
}
|
|
2476
2726
|
function resolveExplicitEmitType(tsType) {
|
|
2477
|
-
if (
|
|
2727
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2478
2728
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2479
2729
|
}
|
|
2480
|
-
if (
|
|
2730
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2481
2731
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2482
2732
|
return tsType;
|
|
2483
2733
|
}
|
|
2484
2734
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2485
|
-
return
|
|
2735
|
+
return t26.tsTypeReference(
|
|
2486
2736
|
tsType.typeName,
|
|
2487
|
-
|
|
2737
|
+
t26.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2488
2738
|
);
|
|
2489
2739
|
}
|
|
2490
|
-
if (
|
|
2740
|
+
if (t26.isTSIntersectionType(tsType)) {
|
|
2491
2741
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2492
2742
|
if (!types.length) return null;
|
|
2493
2743
|
if (types.length === 1) return types[0];
|
|
2494
|
-
return
|
|
2744
|
+
return t26.tsIntersectionType(types);
|
|
2495
2745
|
}
|
|
2496
|
-
if (
|
|
2746
|
+
if (t26.isTSUnionType(tsType)) {
|
|
2497
2747
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2498
2748
|
if (!types.length) return null;
|
|
2499
2749
|
if (types.length === 1) return types[0];
|
|
2500
|
-
return
|
|
2750
|
+
return t26.tsUnionType(types);
|
|
2501
2751
|
}
|
|
2502
|
-
if (
|
|
2752
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2503
2753
|
const members = [];
|
|
2504
2754
|
for (const member of tsType.members) {
|
|
2505
|
-
if (
|
|
2755
|
+
if (t26.isTSPropertySignature(member)) {
|
|
2506
2756
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2507
2757
|
if (prop) members.push(prop);
|
|
2508
2758
|
continue;
|
|
2509
2759
|
}
|
|
2510
|
-
if (
|
|
2760
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2511
2761
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2512
2762
|
continue;
|
|
2513
2763
|
}
|
|
2514
2764
|
}
|
|
2515
2765
|
if (!members.length) return null;
|
|
2516
|
-
return
|
|
2766
|
+
return t26.tsTypeLiteral(members);
|
|
2517
2767
|
}
|
|
2518
|
-
if (
|
|
2768
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2519
2769
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2520
2770
|
if (!props.length) return null;
|
|
2521
|
-
return
|
|
2771
|
+
return t26.tsTypeLiteral(props);
|
|
2522
2772
|
}
|
|
2523
2773
|
return tsType;
|
|
2524
2774
|
}
|
|
2525
2775
|
function hasEmitsSignatureInType(tsType) {
|
|
2526
|
-
if (
|
|
2776
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2527
2777
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
2528
2778
|
}
|
|
2529
|
-
if (
|
|
2779
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2530
2780
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2531
2781
|
return false;
|
|
2532
2782
|
}
|
|
2533
2783
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
2534
2784
|
}
|
|
2535
|
-
if (
|
|
2785
|
+
if (t26.isTSIntersectionType(tsType) || t26.isTSUnionType(tsType)) {
|
|
2536
2786
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
2537
2787
|
}
|
|
2538
|
-
if (
|
|
2788
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2539
2789
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
2540
2790
|
}
|
|
2541
|
-
if (
|
|
2791
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2542
2792
|
return isEmitsCallable(tsType.parameters);
|
|
2543
2793
|
}
|
|
2544
2794
|
return false;
|
|
2545
2795
|
}
|
|
2546
2796
|
function hasEmitsSignatureInMember(member) {
|
|
2547
|
-
if (
|
|
2797
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2548
2798
|
return isEmitsCallable(member.parameters);
|
|
2549
2799
|
}
|
|
2550
2800
|
return false;
|
|
2551
2801
|
}
|
|
2552
2802
|
function isEmitsCallable(parameters) {
|
|
2553
2803
|
const [eventParam] = parameters;
|
|
2554
|
-
if (!eventParam || !
|
|
2804
|
+
if (!eventParam || !t26.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2555
2805
|
return false;
|
|
2556
2806
|
}
|
|
2557
2807
|
const { typeAnnotation } = eventParam;
|
|
2558
|
-
if (
|
|
2808
|
+
if (t26.isNoop(typeAnnotation)) return false;
|
|
2559
2809
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
2560
2810
|
}
|
|
2561
2811
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -2565,19 +2815,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
2565
2815
|
const key = buildKey(handlerName);
|
|
2566
2816
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2567
2817
|
let params = [];
|
|
2568
|
-
let returnType =
|
|
2569
|
-
if (typeAnnotation &&
|
|
2818
|
+
let returnType = t26.tsAnyKeyword();
|
|
2819
|
+
if (typeAnnotation && t26.isTSFunctionType(typeAnnotation)) {
|
|
2570
2820
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
2571
2821
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
2572
|
-
} else if (typeAnnotation &&
|
|
2822
|
+
} else if (typeAnnotation && t26.isTSTupleType(typeAnnotation)) {
|
|
2573
2823
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
2574
2824
|
} else if (typeAnnotation) {
|
|
2575
|
-
const id =
|
|
2576
|
-
id.typeAnnotation =
|
|
2825
|
+
const id = t26.identifier("value");
|
|
2826
|
+
id.typeAnnotation = t26.tsTypeAnnotation(typeAnnotation);
|
|
2577
2827
|
params = [id];
|
|
2578
2828
|
}
|
|
2579
|
-
const fnType =
|
|
2580
|
-
const prop =
|
|
2829
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(returnType));
|
|
2830
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2581
2831
|
prop.optional = !!member.optional;
|
|
2582
2832
|
return prop;
|
|
2583
2833
|
}
|
|
@@ -2586,32 +2836,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
2586
2836
|
}
|
|
2587
2837
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
2588
2838
|
const [eventParam, ...restParams] = parameters;
|
|
2589
|
-
if (!eventParam || !
|
|
2839
|
+
if (!eventParam || !t26.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2590
2840
|
return [];
|
|
2591
2841
|
}
|
|
2592
2842
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
2593
|
-
if (
|
|
2843
|
+
if (t26.isNoop(paramTypeAnnotation)) return [];
|
|
2594
2844
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
2595
2845
|
if (!eventNames.length) return [];
|
|
2596
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
2846
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t26.tsAnyKeyword();
|
|
2597
2847
|
return eventNames.map((eventName) => {
|
|
2598
2848
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
2599
2849
|
const key = buildKey(handlerName);
|
|
2600
2850
|
const params = cloneCallableParams(restParams);
|
|
2601
|
-
const fnType =
|
|
2602
|
-
const prop =
|
|
2851
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(returnType));
|
|
2852
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2603
2853
|
prop.optional = true;
|
|
2604
2854
|
return prop;
|
|
2605
2855
|
});
|
|
2606
2856
|
}
|
|
2607
2857
|
function resolveEventNames(type) {
|
|
2608
|
-
if (
|
|
2858
|
+
if (t26.isTSLiteralType(type) && t26.isStringLiteral(type.literal)) {
|
|
2609
2859
|
return [type.literal.value];
|
|
2610
2860
|
}
|
|
2611
|
-
if (
|
|
2861
|
+
if (t26.isTSUnionType(type)) {
|
|
2612
2862
|
return type.types.flatMap(resolveEventNames);
|
|
2613
2863
|
}
|
|
2614
|
-
if (
|
|
2864
|
+
if (t26.isTSParenthesizedType(type)) {
|
|
2615
2865
|
return resolveEventNames(type.typeAnnotation);
|
|
2616
2866
|
}
|
|
2617
2867
|
return [];
|
|
@@ -2624,44 +2874,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
2624
2874
|
return params;
|
|
2625
2875
|
}
|
|
2626
2876
|
function resolveTupleElementParam(element, index) {
|
|
2627
|
-
const isNamedTuple = typeof
|
|
2877
|
+
const isNamedTuple = typeof t26.isTSNamedTupleMember === "function" && t26.isTSNamedTupleMember(element);
|
|
2628
2878
|
if (isNamedTuple) {
|
|
2629
2879
|
const tupleMember = element;
|
|
2630
2880
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
2631
2881
|
let innerType = tupleMember.elementType;
|
|
2632
2882
|
let optional = tupleMember.optional;
|
|
2633
|
-
if (
|
|
2883
|
+
if (t26.isTSOptionalType(innerType)) {
|
|
2634
2884
|
optional = true;
|
|
2635
2885
|
innerType = innerType.typeAnnotation;
|
|
2636
2886
|
}
|
|
2637
|
-
if (
|
|
2638
|
-
const rest =
|
|
2639
|
-
rest.typeAnnotation =
|
|
2887
|
+
if (t26.isTSRestType(innerType)) {
|
|
2888
|
+
const rest = t26.restElement(t26.identifier(name));
|
|
2889
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(innerType.typeAnnotation);
|
|
2640
2890
|
return rest;
|
|
2641
2891
|
}
|
|
2642
|
-
const id2 =
|
|
2892
|
+
const id2 = t26.identifier(name);
|
|
2643
2893
|
id2.optional = optional;
|
|
2644
|
-
id2.typeAnnotation =
|
|
2894
|
+
id2.typeAnnotation = t26.tsTypeAnnotation(innerType);
|
|
2645
2895
|
return id2;
|
|
2646
2896
|
}
|
|
2647
|
-
if (
|
|
2648
|
-
const rest =
|
|
2649
|
-
rest.typeAnnotation =
|
|
2897
|
+
if (t26.isTSRestType(element)) {
|
|
2898
|
+
const rest = t26.restElement(t26.identifier(`args${index}`));
|
|
2899
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2650
2900
|
return rest;
|
|
2651
2901
|
}
|
|
2652
|
-
if (
|
|
2653
|
-
const id2 =
|
|
2902
|
+
if (t26.isTSOptionalType(element)) {
|
|
2903
|
+
const id2 = t26.identifier(`arg${index}`);
|
|
2654
2904
|
id2.optional = true;
|
|
2655
|
-
id2.typeAnnotation =
|
|
2905
|
+
id2.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2656
2906
|
return id2;
|
|
2657
2907
|
}
|
|
2658
|
-
const id =
|
|
2659
|
-
id.typeAnnotation =
|
|
2908
|
+
const id = t26.identifier(`arg${index}`);
|
|
2909
|
+
id.typeAnnotation = t26.tsTypeAnnotation(element);
|
|
2660
2910
|
return id;
|
|
2661
2911
|
}
|
|
2662
2912
|
|
|
2663
2913
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
2664
|
-
import * as
|
|
2914
|
+
import * as t27 from "@babel/types";
|
|
2665
2915
|
function resolveDefinePropsIface(path7, ctx) {
|
|
2666
2916
|
const { node } = path7;
|
|
2667
2917
|
const [runtimeArg] = node.arguments;
|
|
@@ -2681,38 +2931,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2681
2931
|
} = scriptData;
|
|
2682
2932
|
if (!runtimeArg) return;
|
|
2683
2933
|
const members = [];
|
|
2684
|
-
if (
|
|
2934
|
+
if (t27.isArrayExpression(runtimeArg)) {
|
|
2685
2935
|
for (const element of runtimeArg.elements) {
|
|
2686
|
-
if (!element || !
|
|
2687
|
-
const key =
|
|
2688
|
-
const prop =
|
|
2936
|
+
if (!element || !t27.isStringLiteral(element)) continue;
|
|
2937
|
+
const key = t27.isValidIdentifier(element.value) ? t27.identifier(element.value) : t27.stringLiteral(element.value);
|
|
2938
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2689
2939
|
prop.optional = true;
|
|
2690
2940
|
members.push(prop);
|
|
2691
2941
|
}
|
|
2692
2942
|
if (members.length) {
|
|
2693
|
-
propsTypes.push(
|
|
2943
|
+
propsTypes.push(t27.tsTypeLiteral(members));
|
|
2694
2944
|
}
|
|
2695
2945
|
return;
|
|
2696
2946
|
}
|
|
2697
|
-
if (
|
|
2947
|
+
if (t27.isObjectExpression(runtimeArg)) {
|
|
2698
2948
|
for (const prop of runtimeArg.properties) {
|
|
2699
|
-
if (!
|
|
2700
|
-
if (
|
|
2949
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
2950
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2701
2951
|
const key = prop.key;
|
|
2702
2952
|
let propName = null;
|
|
2703
|
-
if (
|
|
2704
|
-
if (
|
|
2705
|
-
if (
|
|
2953
|
+
if (t27.isIdentifier(key)) propName = key.name;
|
|
2954
|
+
if (t27.isStringLiteral(key)) propName = key.value;
|
|
2955
|
+
if (t27.isNumericLiteral(key)) propName = String(key.value);
|
|
2706
2956
|
if (!propName) continue;
|
|
2707
2957
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
2708
|
-
const tsType = type ??
|
|
2709
|
-
const tsKey =
|
|
2710
|
-
const tsProp =
|
|
2958
|
+
const tsType = type ?? t27.tsAnyKeyword();
|
|
2959
|
+
const tsKey = t27.isValidIdentifier(propName) ? t27.identifier(propName) : t27.stringLiteral(propName);
|
|
2960
|
+
const tsProp = t27.tsPropertySignature(tsKey, t27.tsTypeAnnotation(tsType));
|
|
2711
2961
|
tsProp.optional = !required;
|
|
2712
2962
|
members.push(tsProp);
|
|
2713
2963
|
}
|
|
2714
2964
|
if (members.length) {
|
|
2715
|
-
propsTypes.push(
|
|
2965
|
+
propsTypes.push(t27.tsTypeLiteral(members));
|
|
2716
2966
|
}
|
|
2717
2967
|
return;
|
|
2718
2968
|
}
|
|
@@ -2726,42 +2976,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2726
2976
|
);
|
|
2727
2977
|
}
|
|
2728
2978
|
function resolveRuntimePropMeta(value) {
|
|
2729
|
-
if (
|
|
2979
|
+
if (t27.isIdentifier(value)) {
|
|
2730
2980
|
return {
|
|
2731
2981
|
type: mapRuntimeTypeToTSType(value),
|
|
2732
2982
|
required: false
|
|
2733
2983
|
};
|
|
2734
2984
|
}
|
|
2735
|
-
if (
|
|
2985
|
+
if (t27.isArrayExpression(value)) {
|
|
2736
2986
|
return {
|
|
2737
2987
|
type: resolveRuntimeUnionType(value),
|
|
2738
2988
|
required: false
|
|
2739
2989
|
};
|
|
2740
2990
|
}
|
|
2741
|
-
if (!
|
|
2991
|
+
if (!t27.isObjectExpression(value)) {
|
|
2742
2992
|
return { required: false };
|
|
2743
2993
|
}
|
|
2744
2994
|
let type;
|
|
2745
2995
|
let required = false;
|
|
2746
2996
|
for (const prop of value.properties) {
|
|
2747
|
-
if (!
|
|
2748
|
-
if (
|
|
2997
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
2998
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2749
2999
|
const key = prop.key;
|
|
2750
|
-
const propName =
|
|
3000
|
+
const propName = t27.isIdentifier(key) ? key.name : t27.isStringLiteral(key) ? key.value : null;
|
|
2751
3001
|
if (!propName) continue;
|
|
2752
3002
|
if (propName === "type") {
|
|
2753
3003
|
const valueNode = prop.value;
|
|
2754
|
-
if (
|
|
3004
|
+
if (t27.isArrayExpression(valueNode)) {
|
|
2755
3005
|
type = resolveRuntimeUnionType(valueNode);
|
|
2756
3006
|
continue;
|
|
2757
3007
|
}
|
|
2758
|
-
if (
|
|
3008
|
+
if (t27.isIdentifier(valueNode)) {
|
|
2759
3009
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
2760
3010
|
continue;
|
|
2761
3011
|
}
|
|
2762
3012
|
}
|
|
2763
3013
|
if (propName === "required") {
|
|
2764
|
-
if (
|
|
3014
|
+
if (t27.isBooleanLiteral(prop.value)) {
|
|
2765
3015
|
required = prop.value.value;
|
|
2766
3016
|
}
|
|
2767
3017
|
}
|
|
@@ -2771,58 +3021,58 @@ function resolveRuntimePropMeta(value) {
|
|
|
2771
3021
|
function resolveRuntimeUnionType(value) {
|
|
2772
3022
|
const types = [];
|
|
2773
3023
|
for (const element of value.elements) {
|
|
2774
|
-
if (!element || !
|
|
3024
|
+
if (!element || !t27.isIdentifier(element)) continue;
|
|
2775
3025
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
2776
3026
|
if (resolved) types.push(resolved);
|
|
2777
3027
|
}
|
|
2778
|
-
if (!types.length) return
|
|
3028
|
+
if (!types.length) return t27.tsAnyKeyword();
|
|
2779
3029
|
if (types.length === 1) return types[0];
|
|
2780
|
-
return
|
|
3030
|
+
return t27.tsUnionType(types);
|
|
2781
3031
|
}
|
|
2782
3032
|
function mapRuntimeTypeToTSType(value) {
|
|
2783
3033
|
switch (value.name) {
|
|
2784
3034
|
case "String":
|
|
2785
|
-
return
|
|
3035
|
+
return t27.tsStringKeyword();
|
|
2786
3036
|
case "Number":
|
|
2787
|
-
return
|
|
3037
|
+
return t27.tsNumberKeyword();
|
|
2788
3038
|
case "Boolean":
|
|
2789
|
-
return
|
|
3039
|
+
return t27.tsBooleanKeyword();
|
|
2790
3040
|
case "Object":
|
|
2791
|
-
return
|
|
3041
|
+
return t27.tsTypeLiteral([]);
|
|
2792
3042
|
case "Array":
|
|
2793
|
-
return
|
|
3043
|
+
return t27.tsArrayType(t27.tsAnyKeyword());
|
|
2794
3044
|
case "Function":
|
|
2795
|
-
return
|
|
3045
|
+
return t27.tsFunctionType(null, [], t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2796
3046
|
case "Symbol":
|
|
2797
|
-
return
|
|
3047
|
+
return t27.tsSymbolKeyword();
|
|
2798
3048
|
case "BigInt":
|
|
2799
|
-
return
|
|
3049
|
+
return t27.tsBigIntKeyword();
|
|
2800
3050
|
default:
|
|
2801
|
-
return
|
|
3051
|
+
return t27.tsAnyKeyword();
|
|
2802
3052
|
}
|
|
2803
3053
|
}
|
|
2804
3054
|
|
|
2805
3055
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
2806
|
-
import * as
|
|
3056
|
+
import * as t28 from "@babel/types";
|
|
2807
3057
|
var SLOT_DEFAULT_NAME = "default";
|
|
2808
3058
|
var SLOT_CHILDREN_NAME = "children";
|
|
2809
3059
|
var SLOT_FN_PARAM_NAME = "props";
|
|
2810
3060
|
function resolveSlotsTopLevelTypes(ctx) {
|
|
2811
3061
|
return {
|
|
2812
3062
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path7) {
|
|
2813
|
-
if (!
|
|
3063
|
+
if (!t28.isProgram(path7.parent)) return;
|
|
2814
3064
|
const { node } = path7;
|
|
2815
|
-
if (
|
|
2816
|
-
const typeLiteral =
|
|
3065
|
+
if (t28.isTSInterfaceDeclaration(node)) {
|
|
3066
|
+
const typeLiteral = t28.tsTypeLiteral(node.body.body);
|
|
2817
3067
|
if (!hasSlotsSignatureInType(typeLiteral)) return;
|
|
2818
3068
|
const resolved = resolveSlotType(typeLiteral);
|
|
2819
|
-
if (resolved &&
|
|
3069
|
+
if (resolved && t28.isTSTypeLiteral(resolved)) {
|
|
2820
3070
|
node.body.body = resolved.members;
|
|
2821
3071
|
recordReactNode(ctx);
|
|
2822
3072
|
}
|
|
2823
3073
|
return;
|
|
2824
3074
|
}
|
|
2825
|
-
if (
|
|
3075
|
+
if (t28.isTSTypeAliasDeclaration(node)) {
|
|
2826
3076
|
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
2827
3077
|
const resolved = resolveSlotType(node.typeAnnotation);
|
|
2828
3078
|
if (resolved) {
|
|
@@ -2867,7 +3117,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
2867
3117
|
}
|
|
2868
3118
|
if (tsMembers.length) {
|
|
2869
3119
|
recordReactNode(ctx);
|
|
2870
|
-
slotTypes.push(
|
|
3120
|
+
slotTypes.push(t28.tsTypeLiteral(tsMembers));
|
|
2871
3121
|
}
|
|
2872
3122
|
}
|
|
2873
3123
|
function recordReactNode(ctx) {
|
|
@@ -2877,32 +3127,32 @@ function recordReactNode(ctx) {
|
|
|
2877
3127
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
2878
3128
|
}
|
|
2879
3129
|
function resolveSlotType(tsType) {
|
|
2880
|
-
if (
|
|
3130
|
+
if (t28.isTSParenthesizedType(tsType)) {
|
|
2881
3131
|
return resolveSlotType(tsType.typeAnnotation);
|
|
2882
3132
|
}
|
|
2883
|
-
if (
|
|
3133
|
+
if (t28.isTSTypeReference(tsType)) {
|
|
2884
3134
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2885
3135
|
return tsType;
|
|
2886
3136
|
}
|
|
2887
3137
|
const params = tsType.typeParameters.params.map((param) => resolveSlotType(param)).filter(Boolean);
|
|
2888
|
-
return
|
|
3138
|
+
return t28.tsTypeReference(
|
|
2889
3139
|
tsType.typeName,
|
|
2890
|
-
|
|
3140
|
+
t28.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2891
3141
|
);
|
|
2892
3142
|
}
|
|
2893
|
-
if (
|
|
3143
|
+
if (t28.isTSIntersectionType(tsType)) {
|
|
2894
3144
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
2895
3145
|
if (!types.length) return null;
|
|
2896
3146
|
if (types.length === 1) return types[0];
|
|
2897
|
-
return
|
|
3147
|
+
return t28.tsIntersectionType(types);
|
|
2898
3148
|
}
|
|
2899
|
-
if (
|
|
3149
|
+
if (t28.isTSUnionType(tsType)) {
|
|
2900
3150
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
2901
3151
|
if (!types.length) return null;
|
|
2902
3152
|
if (types.length === 1) return types[0];
|
|
2903
|
-
return
|
|
3153
|
+
return t28.tsUnionType(types);
|
|
2904
3154
|
}
|
|
2905
|
-
if (
|
|
3155
|
+
if (t28.isTSTypeLiteral(tsType)) {
|
|
2906
3156
|
const members = [];
|
|
2907
3157
|
for (const member of tsType.members) {
|
|
2908
3158
|
const resolved = resolveSlotPropFromMember(member);
|
|
@@ -2913,56 +3163,56 @@ function resolveSlotType(tsType) {
|
|
|
2913
3163
|
members.push(member);
|
|
2914
3164
|
}
|
|
2915
3165
|
if (!members.length) return null;
|
|
2916
|
-
return
|
|
3166
|
+
return t28.tsTypeLiteral(members);
|
|
2917
3167
|
}
|
|
2918
|
-
if (
|
|
3168
|
+
if (t28.isTSFunctionType(tsType)) {
|
|
2919
3169
|
const props = buildSlotPropSignature(
|
|
2920
3170
|
SLOT_DEFAULT_NAME,
|
|
2921
3171
|
cloneCallableParams(tsType.parameters),
|
|
2922
3172
|
false
|
|
2923
3173
|
);
|
|
2924
|
-
return
|
|
3174
|
+
return t28.tsTypeLiteral([props]);
|
|
2925
3175
|
}
|
|
2926
3176
|
return tsType;
|
|
2927
3177
|
}
|
|
2928
3178
|
function hasSlotsSignatureInType(tsType) {
|
|
2929
|
-
if (
|
|
3179
|
+
if (t28.isTSParenthesizedType(tsType)) {
|
|
2930
3180
|
return hasSlotsSignatureInType(tsType.typeAnnotation);
|
|
2931
3181
|
}
|
|
2932
|
-
if (
|
|
3182
|
+
if (t28.isTSTypeReference(tsType)) {
|
|
2933
3183
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2934
3184
|
return false;
|
|
2935
3185
|
}
|
|
2936
3186
|
return tsType.typeParameters.params.some(hasSlotsSignatureInType);
|
|
2937
3187
|
}
|
|
2938
|
-
if (
|
|
3188
|
+
if (t28.isTSIntersectionType(tsType) || t28.isTSUnionType(tsType)) {
|
|
2939
3189
|
return tsType.types.some(hasSlotsSignatureInType);
|
|
2940
3190
|
}
|
|
2941
|
-
if (
|
|
3191
|
+
if (t28.isTSTypeLiteral(tsType)) {
|
|
2942
3192
|
return tsType.members.some(hasSlotsSignatureInMember);
|
|
2943
3193
|
}
|
|
2944
|
-
if (
|
|
3194
|
+
if (t28.isTSFunctionType(tsType)) {
|
|
2945
3195
|
return true;
|
|
2946
3196
|
}
|
|
2947
3197
|
return false;
|
|
2948
3198
|
}
|
|
2949
3199
|
function hasSlotsSignatureInMember(member) {
|
|
2950
|
-
if (
|
|
2951
|
-
if (
|
|
2952
|
-
if (
|
|
3200
|
+
if (t28.isTSMethodSignature(member)) return true;
|
|
3201
|
+
if (t28.isTSCallSignatureDeclaration(member)) return true;
|
|
3202
|
+
if (t28.isTSPropertySignature(member)) {
|
|
2953
3203
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2954
3204
|
return !!(typeAnnotation && resolveCallableType(typeAnnotation));
|
|
2955
3205
|
}
|
|
2956
3206
|
return false;
|
|
2957
3207
|
}
|
|
2958
3208
|
function resolveSlotPropFromMember(member) {
|
|
2959
|
-
if (
|
|
3209
|
+
if (t28.isTSMethodSignature(member)) {
|
|
2960
3210
|
const rawName = resolvePropName2(member.key);
|
|
2961
3211
|
if (!rawName) return null;
|
|
2962
3212
|
const params = cloneCallableParams(member.parameters);
|
|
2963
3213
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
2964
3214
|
}
|
|
2965
|
-
if (
|
|
3215
|
+
if (t28.isTSPropertySignature(member)) {
|
|
2966
3216
|
const rawName = resolvePropName2(member.key);
|
|
2967
3217
|
if (!rawName) return null;
|
|
2968
3218
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
@@ -2971,52 +3221,52 @@ function resolveSlotPropFromMember(member) {
|
|
|
2971
3221
|
const params = cloneCallableParams(callable.parameters);
|
|
2972
3222
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
2973
3223
|
}
|
|
2974
|
-
if (
|
|
3224
|
+
if (t28.isTSCallSignatureDeclaration(member)) {
|
|
2975
3225
|
const params = cloneCallableParams(member.parameters);
|
|
2976
3226
|
return buildSlotPropSignature(SLOT_DEFAULT_NAME, params, true);
|
|
2977
3227
|
}
|
|
2978
3228
|
return null;
|
|
2979
3229
|
}
|
|
2980
3230
|
function resolveCallableType(tsType) {
|
|
2981
|
-
if (
|
|
2982
|
-
if (
|
|
3231
|
+
if (t28.isTSFunctionType(tsType)) return tsType;
|
|
3232
|
+
if (t28.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
2983
3233
|
return null;
|
|
2984
3234
|
}
|
|
2985
3235
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
2986
3236
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
2987
|
-
const key =
|
|
2988
|
-
const reactNodeType =
|
|
2989
|
-
|
|
3237
|
+
const key = t28.isValidIdentifier(propName) ? t28.identifier(propName) : t28.stringLiteral(propName);
|
|
3238
|
+
const reactNodeType = t28.tsTypeAnnotation(
|
|
3239
|
+
t28.tsTypeReference(t28.identifier(REACT_API_MAP.ReactNode))
|
|
2990
3240
|
);
|
|
2991
3241
|
let typeAnnotation;
|
|
2992
3242
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
2993
3243
|
typeAnnotation = reactNodeType;
|
|
2994
3244
|
} else {
|
|
2995
|
-
const fnType =
|
|
2996
|
-
typeAnnotation =
|
|
3245
|
+
const fnType = t28.tsFunctionType(null, params, reactNodeType);
|
|
3246
|
+
typeAnnotation = t28.tsTypeAnnotation(fnType);
|
|
2997
3247
|
}
|
|
2998
|
-
const prop =
|
|
3248
|
+
const prop = t28.tsPropertySignature(key, typeAnnotation);
|
|
2999
3249
|
prop.optional = optional;
|
|
3000
3250
|
return prop;
|
|
3001
3251
|
}
|
|
3002
3252
|
function createSlotScopeParam(props, ctx) {
|
|
3003
|
-
const paramId =
|
|
3253
|
+
const paramId = t28.identifier(SLOT_FN_PARAM_NAME);
|
|
3004
3254
|
const propsSigns = [];
|
|
3005
3255
|
const { reactiveBindings } = ctx.templateData;
|
|
3006
3256
|
props.forEach(({ prop, tsType }) => {
|
|
3007
3257
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3008
3258
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3009
|
-
const typeAnnotation = foundBindingTypes ?
|
|
3010
|
-
const propSign =
|
|
3259
|
+
const typeAnnotation = foundBindingTypes ? t28.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3260
|
+
const propSign = t28.tsPropertySignature(t28.identifier(prop), typeAnnotation);
|
|
3011
3261
|
propsSigns.push(propSign);
|
|
3012
3262
|
});
|
|
3013
|
-
paramId.typeAnnotation =
|
|
3263
|
+
paramId.typeAnnotation = t28.tsTypeAnnotation(t28.tsTypeLiteral(propsSigns));
|
|
3014
3264
|
return paramId;
|
|
3015
3265
|
}
|
|
3016
3266
|
function resolvePropName2(key) {
|
|
3017
|
-
if (
|
|
3018
|
-
if (
|
|
3019
|
-
if (
|
|
3267
|
+
if (t28.isIdentifier(key)) return key.name;
|
|
3268
|
+
if (t28.isStringLiteral(key)) return key.value;
|
|
3269
|
+
if (t28.isNumericLiteral(key)) return String(key.value);
|
|
3020
3270
|
return null;
|
|
3021
3271
|
}
|
|
3022
3272
|
|
|
@@ -3068,65 +3318,17 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3068
3318
|
}
|
|
3069
3319
|
const n = ctx.compName || "Comp";
|
|
3070
3320
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3071
|
-
const typeNode =
|
|
3072
|
-
const typeAliasDecl =
|
|
3073
|
-
const exportDecl =
|
|
3321
|
+
const typeNode = t29.tsIntersectionType(tsTypes);
|
|
3322
|
+
const typeAliasDecl = t29.tsTypeAliasDeclaration(t29.identifier(ns), null, typeNode);
|
|
3323
|
+
const exportDecl = t29.exportNamedDeclaration(typeAliasDecl);
|
|
3074
3324
|
propsTSIface.name = ns;
|
|
3075
3325
|
const scriptIR = getScriptIR(ctx);
|
|
3076
3326
|
scriptIR.exports.push(exportDecl);
|
|
3077
3327
|
void ast;
|
|
3078
3328
|
}
|
|
3079
3329
|
|
|
3080
|
-
// src/core/transform/sfc/script/syntax-processor/process/
|
|
3081
|
-
import * as
|
|
3082
|
-
function lintRules(ctx, ast) {
|
|
3083
|
-
const inScriptFile = ctx.inputType !== "sfc";
|
|
3084
|
-
return {
|
|
3085
|
-
CallExpression(path7) {
|
|
3086
|
-
const { node, parentPath } = path7;
|
|
3087
|
-
if (!t29.isIdentifier(node.callee)) return;
|
|
3088
|
-
const { name: callName } = node.callee;
|
|
3089
|
-
const addLog = (t40) => {
|
|
3090
|
-
logger.error(t40, {
|
|
3091
|
-
file: ctx.filename,
|
|
3092
|
-
source: ctx.scriptData.source,
|
|
3093
|
-
loc: node.loc
|
|
3094
|
-
});
|
|
3095
|
-
};
|
|
3096
|
-
const lintMacros = () => {
|
|
3097
|
-
const macro = Object.values(MACRO_API_NAMES).find((v) => v === callName);
|
|
3098
|
-
if (!macro) return;
|
|
3099
|
-
if (inScriptFile) {
|
|
3100
|
-
addLog(
|
|
3101
|
-
`The ${macro} can only be used inside Vue SFC <script> blocks, not in separate script files.`
|
|
3102
|
-
);
|
|
3103
|
-
return;
|
|
3104
|
-
}
|
|
3105
|
-
if (!atComponentOrHookRoot(path7, ast.program)) {
|
|
3106
|
-
addLog(
|
|
3107
|
-
`The ${macro} must be defined at the top level of the component, not inside blocks or functions.`
|
|
3108
|
-
);
|
|
3109
|
-
return;
|
|
3110
|
-
}
|
|
3111
|
-
if (!parentPath.isVariableDeclarator()) {
|
|
3112
|
-
addLog(
|
|
3113
|
-
`The ${macro} macro must be assigned to a variable (e.g., const props = defineProps(...)).`
|
|
3114
|
-
);
|
|
3115
|
-
}
|
|
3116
|
-
};
|
|
3117
|
-
const lintHooks = () => {
|
|
3118
|
-
if (!callName.startsWith("use")) return;
|
|
3119
|
-
if (!atComponentOrHookRoot(path7, ast.program, inScriptFile)) {
|
|
3120
|
-
addLog(
|
|
3121
|
-
`The ${callName} hook must be called at the top level, not inside loops, conditions, or nested functions.`
|
|
3122
|
-
);
|
|
3123
|
-
}
|
|
3124
|
-
};
|
|
3125
|
-
lintMacros();
|
|
3126
|
-
lintHooks();
|
|
3127
|
-
}
|
|
3128
|
-
};
|
|
3129
|
-
}
|
|
3330
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3331
|
+
import * as t31 from "@babel/types";
|
|
3130
3332
|
|
|
3131
3333
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3132
3334
|
import { traverse as traverse2 } from "@babel/core";
|
|
@@ -3376,19 +3578,48 @@ function getIsAnalyzed(node) {
|
|
|
3376
3578
|
return getScriptNodeMeta(node)?.is_deps_analyzed;
|
|
3377
3579
|
}
|
|
3378
3580
|
|
|
3379
|
-
// src/core/transform/sfc/script/
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3581
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3582
|
+
function resolveAnalysisOnlyAdapter(ctx) {
|
|
3583
|
+
return {
|
|
3584
|
+
"CallExpression|Identifier"(path7) {
|
|
3585
|
+
const node = path7.node;
|
|
3586
|
+
const apiName = getApiName(node);
|
|
3587
|
+
const adapter = ADAPTER_RULES.runtime[apiName];
|
|
3588
|
+
if (!adapter || adapter.type !== "analyzed-deps") {
|
|
3589
|
+
return;
|
|
3590
|
+
}
|
|
3591
|
+
if (t31.isCallExpression(node)) {
|
|
3592
|
+
resolveCallNode(path7, adapter, ctx);
|
|
3593
|
+
} else {
|
|
3594
|
+
replaceIdName(node, adapter.target);
|
|
3595
|
+
}
|
|
3596
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3597
|
+
}
|
|
3598
|
+
};
|
|
3386
3599
|
}
|
|
3387
|
-
function
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3600
|
+
function getApiName(node) {
|
|
3601
|
+
const isCallNode = t31.isCallExpression(node);
|
|
3602
|
+
let apiName = "";
|
|
3603
|
+
if (t31.isIdentifier(node)) {
|
|
3604
|
+
apiName = node.name;
|
|
3605
|
+
} else if (isCallNode && t31.isIdentifier(node.callee)) {
|
|
3606
|
+
apiName = node.callee.name;
|
|
3607
|
+
}
|
|
3608
|
+
return apiName;
|
|
3609
|
+
}
|
|
3610
|
+
function resolveCallNode(path7, adapter, ctx) {
|
|
3611
|
+
const { node } = path7;
|
|
3612
|
+
const { arguments: args } = node;
|
|
3613
|
+
if (!args.length) return;
|
|
3614
|
+
const fn = args[0];
|
|
3615
|
+
if (!t31.isArrowFunctionExpression(fn) && !t31.isFunctionExpression(fn)) {
|
|
3616
|
+
return;
|
|
3617
|
+
}
|
|
3618
|
+
const fnPath = path7.get("arguments")[0];
|
|
3619
|
+
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
3620
|
+
args.push(deps);
|
|
3621
|
+
replaceCallName(node, adapter.target);
|
|
3622
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3392
3623
|
}
|
|
3393
3624
|
|
|
3394
3625
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-arrow-deps.ts
|
|
@@ -3462,8 +3693,78 @@ function isSkip(path7) {
|
|
|
3462
3693
|
return (isCallback() || isMethod() || !isVariableDecl()) && !isReturnFunc();
|
|
3463
3694
|
}
|
|
3464
3695
|
|
|
3465
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-
|
|
3696
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3466
3697
|
import * as t32 from "@babel/types";
|
|
3698
|
+
function resolveElementRef(ctx) {
|
|
3699
|
+
return {
|
|
3700
|
+
CallExpression(path7) {
|
|
3701
|
+
const {
|
|
3702
|
+
inputType,
|
|
3703
|
+
templateData: { refBindings }
|
|
3704
|
+
} = ctx;
|
|
3705
|
+
if (inputType !== "sfc") return;
|
|
3706
|
+
const { node } = path7;
|
|
3707
|
+
const isUseTemplateRef = isCalleeNamed(node, VUE_API_MAP.useTemplateRef);
|
|
3708
|
+
const isCompRefBindings = Object.keys(refBindings.componentRefs).length > 0 && isCalleeNamed(node, VUE_API_MAP.ref);
|
|
3709
|
+
const shouldProcess = isUseTemplateRef || isCompRefBindings;
|
|
3710
|
+
if (!shouldProcess) {
|
|
3711
|
+
return;
|
|
3712
|
+
}
|
|
3713
|
+
if (isCompRefBindings) {
|
|
3714
|
+
const varDeclaratorPath = getVariableDeclaratorPath(path7)?.node;
|
|
3715
|
+
if (!t32.isIdentifier(varDeclaratorPath?.id)) {
|
|
3716
|
+
return;
|
|
3717
|
+
}
|
|
3718
|
+
const varName = varDeclaratorPath.id.name;
|
|
3719
|
+
const compRef = refBindings.componentRefs[varName];
|
|
3720
|
+
if (!compRef) return;
|
|
3721
|
+
}
|
|
3722
|
+
node.arguments[0] = t32.identifier("null");
|
|
3723
|
+
resolveTypeParameters(ctx, path7);
|
|
3724
|
+
replaceCallName(node, REACT_API_MAP.useRef);
|
|
3725
|
+
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
3726
|
+
},
|
|
3727
|
+
MemberExpression(path7) {
|
|
3728
|
+
resolveRefValueToCurrent(path7);
|
|
3729
|
+
}
|
|
3730
|
+
};
|
|
3731
|
+
}
|
|
3732
|
+
function resolveTypeParameters(ctx, path7) {
|
|
3733
|
+
const {
|
|
3734
|
+
templateData: { refBindings },
|
|
3735
|
+
scriptData
|
|
3736
|
+
} = ctx;
|
|
3737
|
+
const { node } = path7;
|
|
3738
|
+
const varDeclaratorNode = getVariableDeclaratorPath(path7)?.node;
|
|
3739
|
+
if (!scriptData.lang.startsWith("ts") || !varDeclaratorNode) {
|
|
3740
|
+
return;
|
|
3741
|
+
}
|
|
3742
|
+
const idName = varDeclaratorNode.id.name;
|
|
3743
|
+
const domBindingMeta = refBindings.domRefs[idName];
|
|
3744
|
+
const compBindingMeta = refBindings.componentRefs[idName];
|
|
3745
|
+
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
3746
|
+
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
3747
|
+
node.typeParameters = t32.tsTypeParameterInstantiation([t32.tsTypeReference(t32.identifier(type))]);
|
|
3748
|
+
}
|
|
3749
|
+
}
|
|
3750
|
+
function resolveRefValueToCurrent(path7) {
|
|
3751
|
+
const { node } = path7;
|
|
3752
|
+
if (node.computed || !t32.isIdentifier(node.property) || node.property.name !== "value") {
|
|
3753
|
+
return;
|
|
3754
|
+
}
|
|
3755
|
+
const rootPath = findRootVariablePath(path7);
|
|
3756
|
+
if (!rootPath?.node || !t32.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
3757
|
+
return;
|
|
3758
|
+
}
|
|
3759
|
+
const rootId = findRootIdentifier(node);
|
|
3760
|
+
if (!t32.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
3761
|
+
return;
|
|
3762
|
+
}
|
|
3763
|
+
node.property.name = "current";
|
|
3764
|
+
}
|
|
3765
|
+
|
|
3766
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
3767
|
+
import * as t33 from "@babel/types";
|
|
3467
3768
|
function resolveExprMemo(ctx, ast) {
|
|
3468
3769
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
3469
3770
|
return {
|
|
@@ -3475,7 +3776,7 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3475
3776
|
if (!atComponentOrHookRoot(path7, ast.program, isScriptFile)) {
|
|
3476
3777
|
return false;
|
|
3477
3778
|
}
|
|
3478
|
-
if (
|
|
3779
|
+
if (t33.isCallExpression(init) && t33.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
3479
3780
|
return false;
|
|
3480
3781
|
}
|
|
3481
3782
|
return true;
|
|
@@ -3492,20 +3793,74 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3492
3793
|
};
|
|
3493
3794
|
}
|
|
3494
3795
|
|
|
3796
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
3797
|
+
import * as t34 from "@babel/types";
|
|
3798
|
+
function resolveLintRules(ctx, ast) {
|
|
3799
|
+
const inScriptFile = ctx.inputType !== "sfc";
|
|
3800
|
+
return {
|
|
3801
|
+
CallExpression(path7) {
|
|
3802
|
+
const { node, parentPath } = path7;
|
|
3803
|
+
if (!t34.isIdentifier(node.callee)) return;
|
|
3804
|
+
const { name: callName } = node.callee;
|
|
3805
|
+
const addLog = (t41) => {
|
|
3806
|
+
logger.error(t41, {
|
|
3807
|
+
file: ctx.filename,
|
|
3808
|
+
source: ctx.scriptData.source,
|
|
3809
|
+
loc: node.loc
|
|
3810
|
+
});
|
|
3811
|
+
};
|
|
3812
|
+
const lintMacros = () => {
|
|
3813
|
+
const macro = Object.values(MACRO_API_NAMES).find((v) => v === callName);
|
|
3814
|
+
if (!macro) return;
|
|
3815
|
+
if (inScriptFile) {
|
|
3816
|
+
addLog(
|
|
3817
|
+
`The ${macro} can only be used inside Vue SFC <script> blocks, not in separate script files.`
|
|
3818
|
+
);
|
|
3819
|
+
return;
|
|
3820
|
+
}
|
|
3821
|
+
if (!atComponentOrHookRoot(path7, ast.program)) {
|
|
3822
|
+
addLog(
|
|
3823
|
+
`The ${macro} must be defined at the top level of the component, not inside blocks or functions.`
|
|
3824
|
+
);
|
|
3825
|
+
return;
|
|
3826
|
+
}
|
|
3827
|
+
if (!parentPath.isVariableDeclarator()) {
|
|
3828
|
+
if (macro === MACRO_API_NAMES.props || macro === MACRO_API_NAMES.emits) {
|
|
3829
|
+
addLog(
|
|
3830
|
+
`The ${macro} macro must be assigned to a variable (e.g., const props = defineProps(...)).`
|
|
3831
|
+
);
|
|
3832
|
+
}
|
|
3833
|
+
}
|
|
3834
|
+
};
|
|
3835
|
+
const lintHooks = () => {
|
|
3836
|
+
if (!callName.startsWith("use")) return;
|
|
3837
|
+
if (!atComponentOrHookRoot(path7, ast.program, inScriptFile)) {
|
|
3838
|
+
addLog(
|
|
3839
|
+
`The ${callName} hook must be called at the top level, not inside loops, conditions, or nested functions.`
|
|
3840
|
+
);
|
|
3841
|
+
}
|
|
3842
|
+
};
|
|
3843
|
+
lintMacros();
|
|
3844
|
+
lintHooks();
|
|
3845
|
+
}
|
|
3846
|
+
};
|
|
3847
|
+
}
|
|
3848
|
+
|
|
3495
3849
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
3496
3850
|
import { generate as generate3 } from "@babel/generator";
|
|
3497
|
-
import * as
|
|
3851
|
+
import * as t35 from "@babel/types";
|
|
3498
3852
|
function resolveProvide(ctx) {
|
|
3499
3853
|
if (ctx.inputType !== "sfc") return {};
|
|
3500
3854
|
return {
|
|
3501
3855
|
CallExpression(path7) {
|
|
3502
3856
|
const { node } = path7;
|
|
3503
|
-
if (!isCalleeNamed(node,
|
|
3857
|
+
if (!isCalleeNamed(node, VUE_API_MAP.provide)) return;
|
|
3504
3858
|
const { provide } = ctx.scriptData;
|
|
3505
3859
|
const [key, value] = node.arguments;
|
|
3506
3860
|
const target = findOrCreateCtxProvider(provide);
|
|
3861
|
+
const adapter = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
3507
3862
|
assignProviderValue(target, key, value);
|
|
3508
|
-
recordImport(ctx,
|
|
3863
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3509
3864
|
path7.parentPath.remove();
|
|
3510
3865
|
}
|
|
3511
3866
|
};
|
|
@@ -3523,13 +3878,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
3523
3878
|
function assignProviderValue(target, key, value) {
|
|
3524
3879
|
const getRawExp = (exp) => {
|
|
3525
3880
|
if (!exp) return "''";
|
|
3526
|
-
if (
|
|
3881
|
+
if (t35.isStringLiteral(exp)) {
|
|
3527
3882
|
return `'${exp.value}'`;
|
|
3528
3883
|
}
|
|
3529
|
-
if (
|
|
3884
|
+
if (t35.isNumericLiteral(exp)) {
|
|
3530
3885
|
return exp.value.toString();
|
|
3531
3886
|
}
|
|
3532
|
-
if (
|
|
3887
|
+
if (t35.isIdentifier(exp)) {
|
|
3533
3888
|
return exp.name;
|
|
3534
3889
|
}
|
|
3535
3890
|
try {
|
|
@@ -3544,165 +3899,70 @@ function assignProviderValue(target, key, value) {
|
|
|
3544
3899
|
target.provide = {};
|
|
3545
3900
|
}
|
|
3546
3901
|
|
|
3547
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-
|
|
3548
|
-
import * as
|
|
3549
|
-
function
|
|
3550
|
-
return {
|
|
3551
|
-
"Identifier|CallExpression"(path7) {
|
|
3552
|
-
resolveAdapterRules(PACKAGE_NAME.runtime, ADAPTER_HOOKS, path7, ctx);
|
|
3553
|
-
resolveAdapterRules(PACKAGE_NAME.router, ADAPTER_ROUTER_APIS, path7, ctx);
|
|
3554
|
-
}
|
|
3555
|
-
};
|
|
3556
|
-
}
|
|
3557
|
-
function resolveAdapterRules(pkgName, rules, path7, ctx) {
|
|
3558
|
-
const node = path7.node;
|
|
3559
|
-
const isCallExpr = t34.isCallExpression(node);
|
|
3560
|
-
let sourceName = "";
|
|
3561
|
-
if (t34.isIdentifier(node)) {
|
|
3562
|
-
sourceName = node.name;
|
|
3563
|
-
} else if (isCallExpr && t34.isIdentifier(node.callee)) {
|
|
3564
|
-
sourceName = node.callee.name;
|
|
3565
|
-
}
|
|
3566
|
-
const { pure, effectful } = rules.renameOnly;
|
|
3567
|
-
const pureApi = pure[sourceName];
|
|
3568
|
-
const effectfulApi = effectful[sourceName];
|
|
3569
|
-
const adapter = pureApi || effectfulApi;
|
|
3570
|
-
if (!adapter) return;
|
|
3571
|
-
if (effectfulApi) {
|
|
3572
|
-
const reactiveType = getReactiveType(sourceName);
|
|
3573
|
-
const declaratorPath = getVariableDeclaratorPath(path7);
|
|
3574
|
-
setScriptNodeMeta(declaratorPath?.node, {
|
|
3575
|
-
is_reactive: true,
|
|
3576
|
-
reactive_type: reactiveType
|
|
3577
|
-
});
|
|
3578
|
-
}
|
|
3579
|
-
if (isCallExpr) {
|
|
3580
|
-
replaceCallName(node, adapter);
|
|
3581
|
-
} else {
|
|
3582
|
-
replaceIdName(node, adapter);
|
|
3583
|
-
}
|
|
3584
|
-
recordImport(ctx, pkgName, adapter);
|
|
3585
|
-
if (pkgName === PACKAGE_NAME.router && !ctx.route) {
|
|
3586
|
-
ctx.route = true;
|
|
3587
|
-
}
|
|
3588
|
-
}
|
|
3589
|
-
|
|
3590
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-transform-adapter.ts
|
|
3591
|
-
import * as t35 from "@babel/types";
|
|
3592
|
-
function resolveTransformAdapter(ctx) {
|
|
3902
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
3903
|
+
import * as t36 from "@babel/types";
|
|
3904
|
+
function resolveRenameAdapter(ctx) {
|
|
3593
3905
|
return {
|
|
3594
|
-
"Identifier
|
|
3595
|
-
|
|
3596
|
-
|
|
3906
|
+
"CallExpression|Identifier"(path7) {
|
|
3907
|
+
const node = path7.node;
|
|
3908
|
+
const isCallNode = t36.isCallExpression(node);
|
|
3909
|
+
let apiName = "";
|
|
3910
|
+
if (t36.isIdentifier(node)) {
|
|
3911
|
+
apiName = node.name;
|
|
3912
|
+
} else if (isCallNode && t36.isIdentifier(node.callee)) {
|
|
3913
|
+
apiName = node.callee.name;
|
|
3914
|
+
}
|
|
3915
|
+
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
3916
|
+
const routerAdapter = ADAPTER_RULES.router[apiName];
|
|
3917
|
+
const adapter = runtimeAdapter || routerAdapter;
|
|
3918
|
+
if (!adapter || adapter.type !== "rename") {
|
|
3919
|
+
return;
|
|
3920
|
+
}
|
|
3921
|
+
if (adapter.isTrackable) {
|
|
3922
|
+
const reactiveType = getReactiveType(apiName);
|
|
3923
|
+
const declaratorPath = getVariableDeclaratorPath(path7);
|
|
3924
|
+
setScriptNodeMeta(declaratorPath?.node, {
|
|
3925
|
+
is_reactive: true,
|
|
3926
|
+
reactive_type: reactiveType
|
|
3927
|
+
});
|
|
3928
|
+
}
|
|
3929
|
+
if (isCallNode) {
|
|
3930
|
+
replaceCallName(node, adapter.target);
|
|
3931
|
+
} else {
|
|
3932
|
+
replaceIdName(node, adapter.target);
|
|
3933
|
+
}
|
|
3934
|
+
if (adapter.package === PACKAGE_NAME.router && !ctx.route) {
|
|
3935
|
+
ctx.route = true;
|
|
3936
|
+
}
|
|
3937
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3597
3938
|
}
|
|
3598
3939
|
};
|
|
3599
3940
|
}
|
|
3600
|
-
function resolveTransformAdapterRules(pkgName, rules, path7, ctx) {
|
|
3601
|
-
const node = path7.node;
|
|
3602
|
-
const isCallExpr = t35.isCallExpression(node);
|
|
3603
|
-
let sourceName = "";
|
|
3604
|
-
if (t35.isIdentifier(node)) {
|
|
3605
|
-
sourceName = node.name;
|
|
3606
|
-
} else if (isCallExpr && t35.isIdentifier(node.callee)) {
|
|
3607
|
-
sourceName = node.callee.name;
|
|
3608
|
-
}
|
|
3609
|
-
const { pure, effectful } = rules.transform;
|
|
3610
|
-
const watchEffectAdapter = findNestedAdapter(sourceName, pure.watchEffect);
|
|
3611
|
-
if (watchEffectAdapter) {
|
|
3612
|
-
if (isCallExpr) {
|
|
3613
|
-
handleDependencyAnalysisAPI(
|
|
3614
|
-
path7,
|
|
3615
|
-
ctx,
|
|
3616
|
-
watchEffectAdapter,
|
|
3617
|
-
pkgName
|
|
3618
|
-
);
|
|
3619
|
-
} else {
|
|
3620
|
-
replaceIdName(node, watchEffectAdapter);
|
|
3621
|
-
recordImport(ctx, pkgName, watchEffectAdapter);
|
|
3622
|
-
}
|
|
3623
|
-
return;
|
|
3624
|
-
}
|
|
3625
|
-
const lifecycleAdapter = findNestedAdapter(sourceName, pure.lifecycle);
|
|
3626
|
-
if (lifecycleAdapter) {
|
|
3627
|
-
if (isCallExpr) {
|
|
3628
|
-
handleDependencyAnalysisAPI(
|
|
3629
|
-
path7,
|
|
3630
|
-
ctx,
|
|
3631
|
-
lifecycleAdapter,
|
|
3632
|
-
pkgName
|
|
3633
|
-
);
|
|
3634
|
-
} else {
|
|
3635
|
-
replaceIdName(node, lifecycleAdapter);
|
|
3636
|
-
recordImport(ctx, pkgName, lifecycleAdapter);
|
|
3637
|
-
}
|
|
3638
|
-
return;
|
|
3639
|
-
}
|
|
3640
|
-
const pureApi = pure[sourceName];
|
|
3641
|
-
if (pureApi) {
|
|
3642
|
-
if (isCallExpr) {
|
|
3643
|
-
replaceCallName(node, pureApi);
|
|
3644
|
-
} else {
|
|
3645
|
-
replaceIdName(node, pureApi);
|
|
3646
|
-
}
|
|
3647
|
-
recordImport(ctx, pkgName, pureApi);
|
|
3648
|
-
return;
|
|
3649
|
-
}
|
|
3650
|
-
const effectfulApi = effectful[sourceName];
|
|
3651
|
-
if (effectfulApi) {
|
|
3652
|
-
if (isCallExpr) {
|
|
3653
|
-
replaceCallName(node, effectfulApi);
|
|
3654
|
-
} else {
|
|
3655
|
-
replaceIdName(node, effectfulApi);
|
|
3656
|
-
}
|
|
3657
|
-
recordImport(ctx, pkgName, effectfulApi);
|
|
3658
|
-
return;
|
|
3659
|
-
}
|
|
3660
|
-
}
|
|
3661
|
-
function findNestedAdapter(sourceName, nestedMap) {
|
|
3662
|
-
if (!nestedMap) return void 0;
|
|
3663
|
-
if (typeof nestedMap === "string") {
|
|
3664
|
-
return nestedMap;
|
|
3665
|
-
}
|
|
3666
|
-
return nestedMap[sourceName];
|
|
3667
|
-
}
|
|
3668
|
-
function handleDependencyAnalysisAPI(path7, ctx, adapter, pkgName) {
|
|
3669
|
-
const { node } = path7;
|
|
3670
|
-
const { arguments: args } = node;
|
|
3671
|
-
if (args.length === 0) return;
|
|
3672
|
-
const fn = args[0];
|
|
3673
|
-
if (!t35.isArrowFunctionExpression(fn) && !t35.isFunctionExpression(fn)) {
|
|
3674
|
-
return;
|
|
3675
|
-
}
|
|
3676
|
-
const fnPath = path7.get("arguments")[0];
|
|
3677
|
-
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
3678
|
-
args.push(deps);
|
|
3679
|
-
replaceCallName(node, adapter);
|
|
3680
|
-
recordImport(ctx, pkgName, adapter);
|
|
3681
|
-
}
|
|
3682
3941
|
|
|
3683
3942
|
// src/core/transform/sfc/script/syntax-processor/index.ts
|
|
3684
3943
|
function processVueSyntax2(ast, ctx) {
|
|
3685
3944
|
vueSyntaxProcessor2(ast, ctx, {
|
|
3686
3945
|
preprocess: {
|
|
3687
3946
|
applyBabel: [
|
|
3688
|
-
|
|
3947
|
+
resolvePropsIface,
|
|
3689
3948
|
resolveEmitsTopLevelTypes,
|
|
3690
3949
|
resolveSlotsTopLevelTypes,
|
|
3691
|
-
|
|
3692
|
-
|
|
3950
|
+
resolveDefineOptions,
|
|
3951
|
+
resolveDefineExpose,
|
|
3952
|
+
resolveDefineAsyncComponent,
|
|
3693
3953
|
resolveEmitCalls
|
|
3694
3954
|
]
|
|
3695
3955
|
},
|
|
3696
3956
|
process: {
|
|
3697
3957
|
applyBabel: [
|
|
3698
|
-
|
|
3958
|
+
resolveElementRef,
|
|
3959
|
+
resolveRenameAdapter,
|
|
3699
3960
|
resolveArrowFnDeps,
|
|
3700
3961
|
resolveUnanalyzedArrow,
|
|
3701
|
-
|
|
3702
|
-
resolveTemplateRef,
|
|
3962
|
+
resolveAnalysisOnlyAdapter,
|
|
3703
3963
|
resolveProvide,
|
|
3704
3964
|
resolveExprMemo,
|
|
3705
|
-
|
|
3965
|
+
resolveLintRules
|
|
3706
3966
|
],
|
|
3707
3967
|
excludeBabel: [resolveTemplateSlotIface, resolveCompIProps]
|
|
3708
3968
|
},
|
|
@@ -3800,11 +4060,11 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
3800
4060
|
}
|
|
3801
4061
|
|
|
3802
4062
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
3803
|
-
import * as
|
|
4063
|
+
import * as t39 from "@babel/types";
|
|
3804
4064
|
|
|
3805
4065
|
// src/shared/string-code-types.ts
|
|
3806
4066
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
3807
|
-
import * as
|
|
4067
|
+
import * as t37 from "@babel/types";
|
|
3808
4068
|
var strCodeTypes = {
|
|
3809
4069
|
isIdentifier: isIdentifier20,
|
|
3810
4070
|
isSimpleExpression,
|
|
@@ -3817,22 +4077,22 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
3817
4077
|
} catch {
|
|
3818
4078
|
return false;
|
|
3819
4079
|
}
|
|
3820
|
-
if (
|
|
4080
|
+
if (t37.isLiteral(node)) {
|
|
3821
4081
|
return true;
|
|
3822
4082
|
}
|
|
3823
|
-
if (!excludeVar &&
|
|
4083
|
+
if (!excludeVar && t37.isIdentifier(node)) {
|
|
3824
4084
|
return true;
|
|
3825
4085
|
}
|
|
3826
|
-
if (
|
|
3827
|
-
return isSimpleExpression(node.object) &&
|
|
4086
|
+
if (t37.isMemberExpression(node)) {
|
|
4087
|
+
return isSimpleExpression(node.object) && t37.isIdentifier(node.property);
|
|
3828
4088
|
}
|
|
3829
|
-
if (
|
|
4089
|
+
if (t37.isObjectExpression(node) || t37.isArrayExpression(node)) {
|
|
3830
4090
|
return false;
|
|
3831
4091
|
}
|
|
3832
|
-
if (
|
|
4092
|
+
if (t37.isCallExpression(node) || t37.isAssignmentExpression(node)) {
|
|
3833
4093
|
return false;
|
|
3834
4094
|
}
|
|
3835
|
-
if (
|
|
4095
|
+
if (t37.isBinaryExpression(node) || t37.isUnaryExpression(node)) {
|
|
3836
4096
|
return true;
|
|
3837
4097
|
}
|
|
3838
4098
|
return false;
|
|
@@ -3840,7 +4100,7 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
3840
4100
|
function isIdentifier20(code) {
|
|
3841
4101
|
try {
|
|
3842
4102
|
const node = parseExpression3(code);
|
|
3843
|
-
return
|
|
4103
|
+
return t37.isIdentifier(node);
|
|
3844
4104
|
} catch {
|
|
3845
4105
|
return false;
|
|
3846
4106
|
}
|
|
@@ -3848,14 +4108,14 @@ function isIdentifier20(code) {
|
|
|
3848
4108
|
function isStringLiteral12(code) {
|
|
3849
4109
|
try {
|
|
3850
4110
|
const node = parseExpression3(code);
|
|
3851
|
-
return
|
|
4111
|
+
return t37.isStringLiteral(node) || t37.isTemplateLiteral(node);
|
|
3852
4112
|
} catch {
|
|
3853
4113
|
return false;
|
|
3854
4114
|
}
|
|
3855
4115
|
}
|
|
3856
4116
|
|
|
3857
4117
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
3858
|
-
import * as
|
|
4118
|
+
import * as t38 from "@babel/types";
|
|
3859
4119
|
|
|
3860
4120
|
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
3861
4121
|
function resolveSpecialExpressions(input, ctx) {
|
|
@@ -3901,13 +4161,13 @@ function resolveRefVariable(input, ctx) {
|
|
|
3901
4161
|
|
|
3902
4162
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
3903
4163
|
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
3904
|
-
if (toStrLiteral) return
|
|
4164
|
+
if (toStrLiteral) return t38.stringLiteral(input);
|
|
3905
4165
|
const { filename, scriptData } = ctx;
|
|
3906
4166
|
const newContent = resolveSpecialExpressions(input, ctx);
|
|
3907
4167
|
try {
|
|
3908
4168
|
return stringToExpr(newContent, scriptData.lang, filename);
|
|
3909
4169
|
} catch {
|
|
3910
|
-
return
|
|
4170
|
+
return t38.identifier(newContent);
|
|
3911
4171
|
}
|
|
3912
4172
|
}
|
|
3913
4173
|
|
|
@@ -4044,7 +4304,7 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4044
4304
|
const mergedItems = value.merge;
|
|
4045
4305
|
const setNameIdentifier = (target, valueName) => {
|
|
4046
4306
|
target.content = valueName;
|
|
4047
|
-
target.ast =
|
|
4307
|
+
target.ast = t39.jsxIdentifier(valueName);
|
|
4048
4308
|
};
|
|
4049
4309
|
const setValueExpression = (target, content, isStringLiteral13) => {
|
|
4050
4310
|
target.content = content;
|
|
@@ -4058,29 +4318,34 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4058
4318
|
if (setName && nameIdentifier) {
|
|
4059
4319
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
4060
4320
|
}
|
|
4321
|
+
const dir = ADAPTER_RULES.runtime.dir;
|
|
4322
|
+
recordImport(ctx, dir.package, dir.target);
|
|
4061
4323
|
setValueExpression(value.babelExp, expression, isStringLiteral13);
|
|
4062
|
-
recordImport(ctx, PACKAGE_NAME.runtime, ADAPTER_UTILS_MAP.dir);
|
|
4063
4324
|
};
|
|
4064
4325
|
if (ir.isKeyLessVBind) {
|
|
4065
|
-
const
|
|
4326
|
+
const dirKeyless = ADAPTER_RULES.runtime.dirKeyless;
|
|
4327
|
+
const expression = createRuntimeCall(dirKeyless.target, [valueContent]);
|
|
4066
4328
|
applyRuntimeExpression(expression, false);
|
|
4067
4329
|
return;
|
|
4068
4330
|
}
|
|
4069
4331
|
if (isClassAttr(name) && !value.isStringLiteral && !valueContent.startsWith(STYLE_MODULE_NAME)) {
|
|
4332
|
+
const dirCls = ADAPTER_RULES.runtime.dirCls;
|
|
4070
4333
|
const arg = mergedItems?.join(",") || wrapSingleQuotes(valueContent);
|
|
4071
|
-
const expression = createRuntimeCall(
|
|
4334
|
+
const expression = createRuntimeCall(dirCls.target, [arg]);
|
|
4072
4335
|
applyRuntimeExpression(expression, true, name);
|
|
4073
4336
|
return;
|
|
4074
4337
|
}
|
|
4075
4338
|
if (isStyleAttr(name) && (!isSimpleStyle(valueContent) || mergedItems?.some((item) => !isSimpleStyle(item)))) {
|
|
4339
|
+
const dirStyle = ADAPTER_RULES.runtime.dirStyle;
|
|
4076
4340
|
const arg = mergedItems?.join(",") || valueContent;
|
|
4077
|
-
const expression = createRuntimeCall(
|
|
4341
|
+
const expression = createRuntimeCall(dirStyle.target, [arg]);
|
|
4078
4342
|
applyRuntimeExpression(expression, true, name);
|
|
4079
4343
|
return;
|
|
4080
4344
|
}
|
|
4081
4345
|
if (ir.type === 3 /* EVENT */ && ir.modifiers?.length) {
|
|
4346
|
+
const dirOn = ADAPTER_RULES.runtime.dirOn;
|
|
4082
4347
|
const eventName = wrapSingleQuotes(ir.__vOnEvName || name, ir.isStatic);
|
|
4083
|
-
const expression = createRuntimeCall(
|
|
4348
|
+
const expression = createRuntimeCall(dirOn.target, [eventName, valueContent]);
|
|
4084
4349
|
applyRuntimeExpression(expression, true, name);
|
|
4085
4350
|
return;
|
|
4086
4351
|
}
|
|
@@ -4133,12 +4398,12 @@ function resolveElementChildrenRules(children, ctx, parentIR, ir) {
|
|
|
4133
4398
|
}
|
|
4134
4399
|
const nodeIR = child;
|
|
4135
4400
|
if (parentIR?.isBuiltIn) {
|
|
4136
|
-
if (parentIR.tag ==
|
|
4401
|
+
if (parentIR.tag == VUE_API_MAP.Transition) {
|
|
4137
4402
|
resolveTransitionRules(nodeIR, parentIR, ir, ctx);
|
|
4138
4403
|
}
|
|
4139
4404
|
}
|
|
4140
4405
|
if (nodeIR.isRoute) {
|
|
4141
|
-
if (nodeIR.tag ===
|
|
4406
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
4142
4407
|
resolveRouterLinkRules(nodeIR, ctx);
|
|
4143
4408
|
}
|
|
4144
4409
|
}
|
|
@@ -4345,7 +4610,7 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4345
4610
|
}
|
|
4346
4611
|
const propIR = createPropsIR("is", "is", content);
|
|
4347
4612
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4348
|
-
nodeIR.tag =
|
|
4613
|
+
nodeIR.tag = VUE_API_MAP.DynamicComponent;
|
|
4349
4614
|
nodeIR.isComponent = true;
|
|
4350
4615
|
nodeIR.props.push(propIR);
|
|
4351
4616
|
recordImport(ctx, PACKAGE_NAME.runtime, nodeIR.tag);
|
|
@@ -4354,15 +4619,20 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4354
4619
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-ref-prop.ts
|
|
4355
4620
|
import { NodeTypes as NodeTypes5 } from "@vue/compiler-core";
|
|
4356
4621
|
function resolveRefProp(node, ctx, nodeIR) {
|
|
4357
|
-
const {
|
|
4622
|
+
const {
|
|
4623
|
+
templateData: { refBindings }
|
|
4624
|
+
} = ctx;
|
|
4358
4625
|
let propIR;
|
|
4359
4626
|
if (node.type === NodeTypes5.ATTRIBUTE) {
|
|
4360
|
-
const
|
|
4361
|
-
|
|
4362
|
-
|
|
4627
|
+
const tag = node.value?.content;
|
|
4628
|
+
if (!tag) return;
|
|
4629
|
+
collectComponentRef(tag, ctx);
|
|
4630
|
+
const domRefBinding = Object.values(refBindings.domRefs).find((r) => r.tag === tag);
|
|
4631
|
+
const refVar = domRefBinding?.name || refBindings.componentRefs[tag]?.name;
|
|
4632
|
+
propIR = createPropsIR("ref", "ref", refVar || "null");
|
|
4363
4633
|
} else {
|
|
4364
4634
|
const exp = node.exp;
|
|
4365
|
-
for (const name in
|
|
4635
|
+
for (const name in refBindings.domRefs) {
|
|
4366
4636
|
const newName = `${name}.current`;
|
|
4367
4637
|
const regex = new RegExp(`${name}(?!\\.current)`, "g");
|
|
4368
4638
|
exp.content = exp.content.replace(regex, newName);
|
|
@@ -4372,6 +4642,22 @@ function resolveRefProp(node, ctx, nodeIR) {
|
|
|
4372
4642
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4373
4643
|
nodeIR.props.push(propIR);
|
|
4374
4644
|
}
|
|
4645
|
+
function collectComponentRef(tag, ctx) {
|
|
4646
|
+
if (tag in HTML_TAG_TYPES) {
|
|
4647
|
+
return;
|
|
4648
|
+
}
|
|
4649
|
+
const { refBindings, reactiveBindings } = ctx.templateData;
|
|
4650
|
+
refBindings.componentRefs[tag] = {
|
|
4651
|
+
tag,
|
|
4652
|
+
// 对应的标签名(随便填,不影响)
|
|
4653
|
+
htmlType: "",
|
|
4654
|
+
name: tag
|
|
4655
|
+
// 对应的变量名(随便填,不影响)
|
|
4656
|
+
};
|
|
4657
|
+
if (reactiveBindings[tag]) {
|
|
4658
|
+
delete reactiveBindings[tag];
|
|
4659
|
+
}
|
|
4660
|
+
}
|
|
4375
4661
|
|
|
4376
4662
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-dynamic-attribute-prop.ts
|
|
4377
4663
|
function resolveDynamicAttributeProp(node, ir, ctx, nodeIR) {
|
|
@@ -4638,7 +4924,7 @@ function getRootIdName(expr) {
|
|
|
4638
4924
|
}
|
|
4639
4925
|
|
|
4640
4926
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
4641
|
-
import * as
|
|
4927
|
+
import * as t40 from "@babel/types";
|
|
4642
4928
|
function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
4643
4929
|
const arg = node.arg;
|
|
4644
4930
|
const exp = node.exp;
|
|
@@ -4655,7 +4941,7 @@ function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
|
4655
4941
|
originalVueEventName = `${arg.content}.${modifiers.join(".")}`;
|
|
4656
4942
|
} else {
|
|
4657
4943
|
const expr = stringToExpr(handler);
|
|
4658
|
-
if (!
|
|
4944
|
+
if (!t40.isFunctionExpression(expr) && !t40.isIdentifier(expr)) {
|
|
4659
4945
|
handler = `() => {${handler}}`;
|
|
4660
4946
|
}
|
|
4661
4947
|
}
|
|
@@ -4782,7 +5068,7 @@ function resolveDirectiveProp(node, ir, ctx, elementNode, nodeIR, siblingNodesIR
|
|
|
4782
5068
|
return resolveVOn(node, ir, ctx, nodeIR);
|
|
4783
5069
|
}
|
|
4784
5070
|
if (isVSlot(rawName2)) {
|
|
4785
|
-
if (nodeIR.tag ===
|
|
5071
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
4786
5072
|
resolveRouterLinkVSlotProp(node, nodeIR, ctx);
|
|
4787
5073
|
}
|
|
4788
5074
|
return true;
|
|
@@ -4856,14 +5142,16 @@ function resolveElementNode(node, ir, ctx, siblingNodesIR) {
|
|
|
4856
5142
|
isSelfClosing: node.isSelfClosing,
|
|
4857
5143
|
loc: node.loc
|
|
4858
5144
|
});
|
|
4859
|
-
|
|
5145
|
+
const routerAdapter = ADAPTER_RULES.router[tag];
|
|
5146
|
+
const runtimeAdapter = ADAPTER_RULES.runtime[tag];
|
|
5147
|
+
if (runtimeAdapter) {
|
|
4860
5148
|
nodeIR.isBuiltIn = true;
|
|
4861
|
-
recordImport(ctx,
|
|
5149
|
+
recordImport(ctx, runtimeAdapter.package, runtimeAdapter.target);
|
|
4862
5150
|
}
|
|
4863
|
-
if (
|
|
5151
|
+
if (routerAdapter) {
|
|
4864
5152
|
if (!ctx.route) ctx.route = true;
|
|
4865
5153
|
nodeIR.isRoute = true;
|
|
4866
|
-
recordImport(ctx,
|
|
5154
|
+
recordImport(ctx, routerAdapter?.package, routerAdapter.target);
|
|
4867
5155
|
}
|
|
4868
5156
|
resolveProps(node, ir, ctx, nodeIR, siblingNodesIR);
|
|
4869
5157
|
return nodeIR;
|
|
@@ -5556,7 +5844,10 @@ var CompilationContext = class {
|
|
|
5556
5844
|
propField: "$props",
|
|
5557
5845
|
templateData: {
|
|
5558
5846
|
slots: {},
|
|
5559
|
-
refBindings: {
|
|
5847
|
+
refBindings: {
|
|
5848
|
+
domRefs: {},
|
|
5849
|
+
componentRefs: {}
|
|
5850
|
+
},
|
|
5560
5851
|
reactiveBindings: {}
|
|
5561
5852
|
},
|
|
5562
5853
|
scriptData: {
|
|
@@ -5573,6 +5864,10 @@ var CompilationContext = class {
|
|
|
5573
5864
|
emitTypes: [],
|
|
5574
5865
|
slotTypes: []
|
|
5575
5866
|
},
|
|
5867
|
+
forwardRef: {
|
|
5868
|
+
enabled: false,
|
|
5869
|
+
refField: "expose"
|
|
5870
|
+
},
|
|
5576
5871
|
source: ""
|
|
5577
5872
|
},
|
|
5578
5873
|
styleData: {
|