@vureact/compiler-core 1.1.1 → 1.2.1
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-2ESB4I6I.esm.js → chunk-7FIMRVQS.esm.js} +1106 -775
- package/lib/{chunk-GWLPQNHN.js → chunk-SQRJUILR.js} +1217 -886
- package/lib/cli.esm.js +2 -2
- package/lib/cli.js +9 -9
- package/lib/compiler-core.d.cts +19 -2
- package/lib/compiler-core.d.ts +19 -2
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vureact/compiler-core v1.
|
|
2
|
+
* @vureact/compiler-core v1.2.1
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -26,93 +26,415 @@ 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"
|
|
103
131
|
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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"
|
|
269
|
+
},
|
|
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
|
+
// =============== Methods ===============
|
|
316
|
+
createRouter: {
|
|
317
|
+
target: "createRouter",
|
|
318
|
+
package: PACKAGE_NAME.router,
|
|
319
|
+
type: "rename"
|
|
320
|
+
},
|
|
321
|
+
createWebHistory: {
|
|
322
|
+
target: "createWebHistory",
|
|
323
|
+
package: PACKAGE_NAME.router,
|
|
324
|
+
type: "rename"
|
|
325
|
+
},
|
|
326
|
+
createWebHashHistory: {
|
|
327
|
+
target: "createWebHashHistory",
|
|
328
|
+
package: PACKAGE_NAME.router,
|
|
329
|
+
type: "rename"
|
|
330
|
+
},
|
|
331
|
+
createMemoryHistory: {
|
|
332
|
+
target: "createMemoryHistory",
|
|
333
|
+
package: PACKAGE_NAME.router,
|
|
334
|
+
type: "rename"
|
|
335
|
+
},
|
|
336
|
+
// =============== Hooks ===============
|
|
337
|
+
useRoute: {
|
|
338
|
+
target: "useRoute",
|
|
339
|
+
package: PACKAGE_NAME.router,
|
|
340
|
+
type: "rename",
|
|
341
|
+
isTrackable: true
|
|
342
|
+
},
|
|
343
|
+
useRouter: {
|
|
344
|
+
target: "useRouter",
|
|
345
|
+
package: PACKAGE_NAME.router,
|
|
346
|
+
type: "rename",
|
|
347
|
+
isTrackable: true
|
|
348
|
+
},
|
|
349
|
+
useLink: {
|
|
350
|
+
target: "useLink",
|
|
351
|
+
package: PACKAGE_NAME.router,
|
|
352
|
+
type: "rename",
|
|
353
|
+
isTrackable: true
|
|
354
|
+
},
|
|
355
|
+
onBeforeRouteLeave: {
|
|
356
|
+
target: "useBeforeRouteLeave",
|
|
357
|
+
package: PACKAGE_NAME.router,
|
|
358
|
+
type: "rename"
|
|
359
|
+
},
|
|
360
|
+
onBeforeRouteUpdate: {
|
|
361
|
+
target: "useBeforeRouteUpdate",
|
|
362
|
+
package: PACKAGE_NAME.router,
|
|
363
|
+
type: "rename"
|
|
364
|
+
},
|
|
365
|
+
onBeforeRouteEnter: {
|
|
366
|
+
target: "useBeforeRouteEnter",
|
|
367
|
+
package: PACKAGE_NAME.router,
|
|
368
|
+
type: "rename"
|
|
369
|
+
}
|
|
113
370
|
}
|
|
114
371
|
};
|
|
115
372
|
|
|
373
|
+
// src/consts/vue-api-map.ts
|
|
374
|
+
var VUE_API_MAP = {
|
|
375
|
+
ref: "ref",
|
|
376
|
+
provide: "provide",
|
|
377
|
+
watch: "watch",
|
|
378
|
+
watchEffect: "watchEffect",
|
|
379
|
+
watchPostEffect: "watchPostEffect",
|
|
380
|
+
watchSyncEffect: "watchSyncEffect",
|
|
381
|
+
useTemplateRef: "useTemplateRef",
|
|
382
|
+
defineAsyncComponent: "defineAsyncComponent",
|
|
383
|
+
DynamicComponent: "Component",
|
|
384
|
+
Transition: "Transition",
|
|
385
|
+
RouterLink: "RouterLink"
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// src/shared/babel-utils.ts
|
|
389
|
+
import { parseExpression } from "@babel/parser";
|
|
390
|
+
function getBabelParseOptions(lang = "js", context = "script", filename) {
|
|
391
|
+
const baseOptions = {
|
|
392
|
+
sourceType: "module",
|
|
393
|
+
sourceFilename: filename ?? "anonymous",
|
|
394
|
+
errorRecovery: true
|
|
395
|
+
// 容错模式
|
|
396
|
+
};
|
|
397
|
+
const plugins = [];
|
|
398
|
+
if (lang.includes("ts")) {
|
|
399
|
+
plugins.push("typescript");
|
|
400
|
+
}
|
|
401
|
+
if (lang.endsWith("sx")) {
|
|
402
|
+
if (lang.startsWith("t")) plugins.push("typescript");
|
|
403
|
+
plugins.push("jsx");
|
|
404
|
+
}
|
|
405
|
+
if (context === "vueTemplate") {
|
|
406
|
+
plugins.push("decorators-legacy");
|
|
407
|
+
} else if (context === "expression") {
|
|
408
|
+
baseOptions.sourceType = "script";
|
|
409
|
+
baseOptions.allowReturnOutsideFunction = true;
|
|
410
|
+
baseOptions.allowSuperOutsideMethod = true;
|
|
411
|
+
}
|
|
412
|
+
plugins.push("classProperties", "objectRestSpread", "asyncGenerators");
|
|
413
|
+
return {
|
|
414
|
+
...baseOptions,
|
|
415
|
+
plugins: [...new Set(plugins)]
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function stringToExpr(input, lang, filename = "") {
|
|
419
|
+
return parseExpression(input, getBabelParseOptions(lang, "expression", filename));
|
|
420
|
+
}
|
|
421
|
+
function atComponentOrHookRoot(path7, rootScope, inScriptFile = false) {
|
|
422
|
+
const { parentPath, scope } = path7;
|
|
423
|
+
const parentBlock = scope.block;
|
|
424
|
+
if (!parentPath) return !inScriptFile;
|
|
425
|
+
if (parentBlock === rootScope) {
|
|
426
|
+
if (inScriptFile) return false;
|
|
427
|
+
if (parentPath.isBlockStatement() && parentPath.node !== rootScope) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
if (parentPath.isCallExpression() || parentPath.isNewExpression()) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
|
|
116
438
|
// src/core/codegen/component/jsx/utils/jsx-element-utils.ts
|
|
117
439
|
import * as t from "@babel/types";
|
|
118
440
|
function createJsxElement(tag, props, children, selfClosing) {
|
|
@@ -292,7 +614,7 @@ function buildMemoNode(nodeIR, ctx) {
|
|
|
292
614
|
import * as t8 from "@babel/types";
|
|
293
615
|
|
|
294
616
|
// src/core/codegen/component/jsx/syntax-processor/process/build-slot-prop.ts
|
|
295
|
-
import { parseExpression } from "@babel/parser";
|
|
617
|
+
import { parseExpression as parseExpression2 } from "@babel/parser";
|
|
296
618
|
import * as t7 from "@babel/types";
|
|
297
619
|
|
|
298
620
|
// src/shared/logger.ts
|
|
@@ -559,7 +881,7 @@ function buildSlotProp(nodeIR, ctx) {
|
|
|
559
881
|
return slotValue;
|
|
560
882
|
}
|
|
561
883
|
if (!nodeIR.isStatic) {
|
|
562
|
-
const dynamicSlotKey =
|
|
884
|
+
const dynamicSlotKey = parseExpression2(nodeIR.name);
|
|
563
885
|
const spreadObject = t7.objectExpression([
|
|
564
886
|
t7.objectProperty(dynamicSlotKey, convertSlotValueToExpression(slotValue), true)
|
|
565
887
|
]);
|
|
@@ -580,7 +902,7 @@ function buildSlotCallbackParams(nodeIR, ctx) {
|
|
|
580
902
|
}
|
|
581
903
|
const source = rawArg.startsWith("(") ? `${rawArg} => null` : `(${rawArg}) => null`;
|
|
582
904
|
try {
|
|
583
|
-
const expression =
|
|
905
|
+
const expression = parseExpression2(source);
|
|
584
906
|
if (!t7.isArrowFunctionExpression(expression)) {
|
|
585
907
|
return [];
|
|
586
908
|
}
|
|
@@ -696,16 +1018,25 @@ function buildCtxProviderNode(nodeIR, ctx, children) {
|
|
|
696
1018
|
if (nextProvide?.isOccupied) {
|
|
697
1019
|
childNodes = [buildCtxProviderNode(nextProvide, ctx, children)];
|
|
698
1020
|
}
|
|
699
|
-
const
|
|
700
|
-
t10.
|
|
701
|
-
|
|
1021
|
+
const parseProviderExpr = (raw) => {
|
|
1022
|
+
if (!raw) return t10.stringLiteral("");
|
|
1023
|
+
try {
|
|
1024
|
+
return stringToExpr(raw, ctx.scriptData.lang, ctx.filename);
|
|
1025
|
+
} catch {
|
|
1026
|
+
return t10.stringLiteral(raw);
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
const nameProp = t10.jsxAttribute(
|
|
1030
|
+
t10.jsxIdentifier("name"),
|
|
1031
|
+
buildJsxExpressionNode(parseProviderExpr(name))
|
|
702
1032
|
);
|
|
703
1033
|
const valueProp = t10.jsxAttribute(
|
|
704
1034
|
t10.jsxIdentifier("value"),
|
|
705
|
-
buildJsxExpressionNode(
|
|
1035
|
+
buildJsxExpressionNode(parseProviderExpr(value))
|
|
706
1036
|
);
|
|
707
1037
|
void ctx;
|
|
708
|
-
|
|
1038
|
+
const adpater = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
1039
|
+
return createJsxElement(adpater.target, [nameProp, valueProp], childNodes);
|
|
709
1040
|
}
|
|
710
1041
|
|
|
711
1042
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-root-jsx.ts
|
|
@@ -783,9 +1114,11 @@ var REACT_API_MAP = {
|
|
|
783
1114
|
lazy: "lazy",
|
|
784
1115
|
memo: "memo",
|
|
785
1116
|
useRef: "useRef",
|
|
786
|
-
useCallback: "useCallback",
|
|
787
1117
|
useMemo: "useMemo",
|
|
788
|
-
ReactNode: "ReactNode"
|
|
1118
|
+
ReactNode: "ReactNode",
|
|
1119
|
+
forwardRef: "forwardRef",
|
|
1120
|
+
useCallback: "useCallback",
|
|
1121
|
+
useImperativeHandle: "useImperativeHandle"
|
|
789
1122
|
};
|
|
790
1123
|
|
|
791
1124
|
// src/utils/camelCase.ts
|
|
@@ -825,10 +1158,13 @@ function buildComponent(scriptIR, ctx, state) {
|
|
|
825
1158
|
const hasProps = getHasProps(propsTSIface.propsTypes) || getHasProps(propsTSIface.emitTypes) || getHasProps(propsTSIface.slotTypes);
|
|
826
1159
|
const shouldMemo = scriptIR !== null || hasProps;
|
|
827
1160
|
const jsxStatement = t12.returnStatement(state.jsx || t12.nullLiteral());
|
|
828
|
-
const component = !shouldMemo ?
|
|
1161
|
+
const component = !shouldMemo ? resolvePureUIComponent(jsxStatement, ctx) : resolveMemoComponent(scriptIR.statement.local, jsxStatement, ctx);
|
|
829
1162
|
state.component = component;
|
|
830
1163
|
}
|
|
831
|
-
function
|
|
1164
|
+
function getHasProps(list) {
|
|
1165
|
+
return list.length > 0;
|
|
1166
|
+
}
|
|
1167
|
+
function resolvePureUIComponent(jsxStmt, ctx) {
|
|
832
1168
|
return t12.variableDeclaration("const", [
|
|
833
1169
|
t12.variableDeclarator(
|
|
834
1170
|
resolveComponentName(ctx),
|
|
@@ -838,12 +1174,14 @@ function resolveComponent(jsxStmt, ctx) {
|
|
|
838
1174
|
}
|
|
839
1175
|
function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
840
1176
|
const name = resolveComponentName(ctx);
|
|
841
|
-
const param =
|
|
842
|
-
const
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
)
|
|
846
|
-
|
|
1177
|
+
const param = resolvePropsParam(ctx);
|
|
1178
|
+
const body = t12.blockStatement(resolveLocalStatements(local, jsxStmt));
|
|
1179
|
+
const component = resolveComponent(param, body, ctx);
|
|
1180
|
+
let compFn = component;
|
|
1181
|
+
if (ctx.scriptData.forwardRef.enabled) {
|
|
1182
|
+
compFn = resolveForwardRef(body, ctx);
|
|
1183
|
+
}
|
|
1184
|
+
const memoCall = t12.callExpression(t12.identifier(REACT_API_MAP.memo), [compFn]);
|
|
847
1185
|
return t12.variableDeclaration("const", [t12.variableDeclarator(name, memoCall)]);
|
|
848
1186
|
}
|
|
849
1187
|
function resolveComponentName(ctx) {
|
|
@@ -860,7 +1198,52 @@ function resolveComponentName(ctx) {
|
|
|
860
1198
|
}
|
|
861
1199
|
return t12.identifier(name);
|
|
862
1200
|
}
|
|
863
|
-
function
|
|
1201
|
+
function resolveLocalStatements(local, jsx) {
|
|
1202
|
+
const stmts = [jsx];
|
|
1203
|
+
if (!local) return stmts;
|
|
1204
|
+
if (typeof local === "object" && "program" in local) {
|
|
1205
|
+
const program3 = local.program;
|
|
1206
|
+
if (program3?.body?.length) {
|
|
1207
|
+
return [...program3.body, jsx];
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
return stmts;
|
|
1211
|
+
}
|
|
1212
|
+
function resolveComponent(param, body, ctx) {
|
|
1213
|
+
const {
|
|
1214
|
+
scriptData: { forwardRef }
|
|
1215
|
+
} = ctx;
|
|
1216
|
+
if (forwardRef.enabled) {
|
|
1217
|
+
return resolveForwardRef(body, ctx);
|
|
1218
|
+
}
|
|
1219
|
+
const baseComponent = t12.arrowFunctionExpression(!param ? [] : [param], body);
|
|
1220
|
+
return baseComponent;
|
|
1221
|
+
}
|
|
1222
|
+
function resolveForwardRef(body, ctx) {
|
|
1223
|
+
const {
|
|
1224
|
+
propField,
|
|
1225
|
+
scriptData: { forwardRef, lang }
|
|
1226
|
+
} = ctx;
|
|
1227
|
+
const params = [t12.identifier(forwardRef.refField)];
|
|
1228
|
+
const propsId = resolvePropsParam(ctx);
|
|
1229
|
+
const callExpr = t12.callExpression(t12.identifier(REACT_API_MAP.forwardRef), [
|
|
1230
|
+
t12.arrowFunctionExpression(params, body)
|
|
1231
|
+
]);
|
|
1232
|
+
if (lang.startsWith("ts")) {
|
|
1233
|
+
const types = [t12.tsAnyKeyword()];
|
|
1234
|
+
if (propsId) {
|
|
1235
|
+
const propsType = propsId?.typeAnnotation?.typeAnnotation;
|
|
1236
|
+
types.push(propsType || t12.tsAnyKeyword());
|
|
1237
|
+
}
|
|
1238
|
+
callExpr.typeParameters = t12.tsTypeParameterInstantiation(types);
|
|
1239
|
+
}
|
|
1240
|
+
if (propsId) {
|
|
1241
|
+
propsId.typeAnnotation = null;
|
|
1242
|
+
params.unshift(propsId);
|
|
1243
|
+
}
|
|
1244
|
+
return callExpr;
|
|
1245
|
+
}
|
|
1246
|
+
function resolvePropsParam(ctx) {
|
|
864
1247
|
const { propField, scriptData } = ctx;
|
|
865
1248
|
const { propsTSIface } = scriptData;
|
|
866
1249
|
const propsIdentifier = t12.identifier(propField);
|
|
@@ -877,20 +1260,6 @@ function resolveParam(ctx) {
|
|
|
877
1260
|
propsIdentifier.typeAnnotation = t12.tsTypeAnnotation(t12.tsTypeReference(typeIdentifier));
|
|
878
1261
|
return propsIdentifier;
|
|
879
1262
|
}
|
|
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
1263
|
|
|
895
1264
|
// src/core/codegen/component/script/syntax-processor/process/build-program-preamble.ts
|
|
896
1265
|
function buildProgramPreamble(nodeIR, ctx, state) {
|
|
@@ -975,58 +1344,6 @@ function generate(ir, ctx, options) {
|
|
|
975
1344
|
|
|
976
1345
|
// src/core/parse/script-only.ts
|
|
977
1346
|
import { parse as babelParse } from "@babel/parser";
|
|
978
|
-
|
|
979
|
-
// src/shared/babel-utils.ts
|
|
980
|
-
import { parseExpression as parseExpression2 } from "@babel/parser";
|
|
981
|
-
function getBabelParseOptions(lang = "js", context = "script", filename) {
|
|
982
|
-
const baseOptions = {
|
|
983
|
-
sourceType: "module",
|
|
984
|
-
sourceFilename: filename ?? "anonymous",
|
|
985
|
-
errorRecovery: true
|
|
986
|
-
// 容错模式
|
|
987
|
-
};
|
|
988
|
-
const plugins = [];
|
|
989
|
-
if (lang.includes("ts")) {
|
|
990
|
-
plugins.push("typescript");
|
|
991
|
-
}
|
|
992
|
-
if (lang.endsWith("sx")) {
|
|
993
|
-
if (lang.startsWith("t")) plugins.push("typescript");
|
|
994
|
-
plugins.push("jsx");
|
|
995
|
-
}
|
|
996
|
-
if (context === "vueTemplate") {
|
|
997
|
-
plugins.push("decorators-legacy");
|
|
998
|
-
} else if (context === "expression") {
|
|
999
|
-
baseOptions.sourceType = "script";
|
|
1000
|
-
baseOptions.allowReturnOutsideFunction = true;
|
|
1001
|
-
baseOptions.allowSuperOutsideMethod = true;
|
|
1002
|
-
}
|
|
1003
|
-
plugins.push("classProperties", "objectRestSpread", "asyncGenerators");
|
|
1004
|
-
return {
|
|
1005
|
-
...baseOptions,
|
|
1006
|
-
plugins: [...new Set(plugins)]
|
|
1007
|
-
};
|
|
1008
|
-
}
|
|
1009
|
-
function stringToExpr(input, lang, filename = "") {
|
|
1010
|
-
return parseExpression2(input, getBabelParseOptions(lang, "expression", filename));
|
|
1011
|
-
}
|
|
1012
|
-
function atComponentOrHookRoot(path7, rootScope, inScriptFile = false) {
|
|
1013
|
-
const { parentPath, scope } = path7;
|
|
1014
|
-
const parentBlock = scope.block;
|
|
1015
|
-
if (!parentPath) return !inScriptFile;
|
|
1016
|
-
if (parentBlock === rootScope) {
|
|
1017
|
-
if (inScriptFile) return false;
|
|
1018
|
-
if (parentPath.isBlockStatement() && parentPath.node !== rootScope) {
|
|
1019
|
-
return false;
|
|
1020
|
-
}
|
|
1021
|
-
if (parentPath.isCallExpression() || parentPath.isNewExpression()) {
|
|
1022
|
-
return false;
|
|
1023
|
-
}
|
|
1024
|
-
return true;
|
|
1025
|
-
}
|
|
1026
|
-
return false;
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
// src/core/parse/script-only.ts
|
|
1030
1347
|
function parseOnlyScript(source, ctx, options) {
|
|
1031
1348
|
const lang = ctx.inputType.split("-")[1];
|
|
1032
1349
|
const ast = babelParse(source, getBabelParseOptions(lang, "script", ctx.filename));
|
|
@@ -1234,11 +1551,6 @@ var HTML_TAG_TYPES = {
|
|
|
1234
1551
|
frame: "HTMLFrameElement"
|
|
1235
1552
|
};
|
|
1236
1553
|
|
|
1237
|
-
// src/consts/vue-api-map.ts
|
|
1238
|
-
var VUE_API_MAP = {
|
|
1239
|
-
useTemplateRef: "useTemplateRef"
|
|
1240
|
-
};
|
|
1241
|
-
|
|
1242
1554
|
// src/consts/reactive-type-map.ts
|
|
1243
1555
|
var REACTIVE_TYPE_MAP = {
|
|
1244
1556
|
ref: "ref",
|
|
@@ -1294,7 +1606,7 @@ function collectReactiveBindings(node, ctx) {
|
|
|
1294
1606
|
if (!reactiveStateApis.has(source)) return;
|
|
1295
1607
|
const varName = node.id.name;
|
|
1296
1608
|
const value = init.arguments[0];
|
|
1297
|
-
if (source ===
|
|
1609
|
+
if (source === MACRO_API_NAMES.props) {
|
|
1298
1610
|
ctx.propField = varName;
|
|
1299
1611
|
}
|
|
1300
1612
|
reactiveBindings[varName] = {
|
|
@@ -1311,55 +1623,13 @@ function collectRefBindings(node, ctx) {
|
|
|
1311
1623
|
if (callee.name !== VUE_API_MAP.useTemplateRef) return;
|
|
1312
1624
|
const idName = node.id.name;
|
|
1313
1625
|
const tag = init.arguments[0]?.value;
|
|
1314
|
-
refBindings[idName] = {
|
|
1626
|
+
refBindings.domRefs[idName] = {
|
|
1315
1627
|
tag,
|
|
1316
1628
|
htmlType: HTML_TAG_TYPES[tag] || "HTMLElement",
|
|
1317
1629
|
name: idName
|
|
1318
1630
|
};
|
|
1319
1631
|
}
|
|
1320
1632
|
|
|
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
1633
|
// src/plugins/postcss.ts
|
|
1364
1634
|
import postcss from "postcss";
|
|
1365
1635
|
function processScopedWithPostCss(input, hash) {
|
|
@@ -1620,6 +1890,9 @@ import * as t17 from "@babel/types";
|
|
|
1620
1890
|
// src/core/transform/shared.ts
|
|
1621
1891
|
function recordImport(ctx, pkg, name, onDemand = true) {
|
|
1622
1892
|
const { imports } = ctx;
|
|
1893
|
+
if (isTypeOnlyImport(name)) {
|
|
1894
|
+
name = `type ${name}`;
|
|
1895
|
+
}
|
|
1623
1896
|
if (imports.has(pkg)) {
|
|
1624
1897
|
const list = imports.get(pkg);
|
|
1625
1898
|
const foundItem = list.find((item) => item.name === name);
|
|
@@ -1630,6 +1903,10 @@ function recordImport(ctx, pkg, name, onDemand = true) {
|
|
|
1630
1903
|
}
|
|
1631
1904
|
imports.set(pkg, [{ name, onDemand }]);
|
|
1632
1905
|
}
|
|
1906
|
+
function isTypeOnlyImport(name) {
|
|
1907
|
+
const arr = [REACT_API_MAP.ReactNode];
|
|
1908
|
+
return arr.includes(name);
|
|
1909
|
+
}
|
|
1633
1910
|
|
|
1634
1911
|
// src/core/transform/sfc/script/shared/replace-vue-suffix.ts
|
|
1635
1912
|
function replaceVueSuffix(ctx, node) {
|
|
@@ -1998,66 +2275,13 @@ function collectLocalStatements(ctx, ast) {
|
|
|
1998
2275
|
scriptIR.statement.local = ast;
|
|
1999
2276
|
}
|
|
2000
2277
|
|
|
2001
|
-
// src/core/transform/sfc/script/syntax-processor/
|
|
2278
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2002
2279
|
import * as t20 from "@babel/types";
|
|
2003
|
-
function
|
|
2004
|
-
if (ctx.inputType !== "sfc") return {};
|
|
2280
|
+
function resolveDefineAsyncComponent(ctx) {
|
|
2005
2281
|
return {
|
|
2006
2282
|
CallExpression(path7) {
|
|
2007
2283
|
const { node } = path7;
|
|
2008
|
-
if (!isCalleeNamed(node, VUE_API_MAP.
|
|
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) {
|
|
2057
|
-
return {
|
|
2058
|
-
CallExpression(path7) {
|
|
2059
|
-
const { node } = path7;
|
|
2060
|
-
if (!isCalleeNamed(node, "defineAsyncComponent")) {
|
|
2284
|
+
if (!isCalleeNamed(node, VUE_API_MAP.defineAsyncComponent)) {
|
|
2061
2285
|
return;
|
|
2062
2286
|
}
|
|
2063
2287
|
const [arg] = node.arguments;
|
|
@@ -2068,11 +2292,11 @@ function resolveAsyncComponent(ctx) {
|
|
|
2068
2292
|
};
|
|
2069
2293
|
}
|
|
2070
2294
|
function checkIsUnsupported(ctx, arg) {
|
|
2071
|
-
if (
|
|
2295
|
+
if (t20.isFunction(arg)) {
|
|
2072
2296
|
checkIsDynamicImport(ctx, arg);
|
|
2073
|
-
} else if (
|
|
2297
|
+
} else if (t20.isObjectExpression(arg)) {
|
|
2074
2298
|
const { value } = arg.properties.find(
|
|
2075
|
-
(p) =>
|
|
2299
|
+
(p) => t20.isObjectProperty(p) && t20.isIdentifier(p.key) && p.key.name === "loader"
|
|
2076
2300
|
);
|
|
2077
2301
|
checkIsDynamicImport(ctx, value);
|
|
2078
2302
|
if (arg.properties.length > 1) {
|
|
@@ -2083,7 +2307,7 @@ function checkIsUnsupported(ctx, arg) {
|
|
|
2083
2307
|
function checkIsDynamicImport(ctx, node) {
|
|
2084
2308
|
const { scriptData, filename } = ctx;
|
|
2085
2309
|
const warnIsNotImport = (target) => {
|
|
2086
|
-
if (!target || !
|
|
2310
|
+
if (!target || !t20.isImport(target)) {
|
|
2087
2311
|
logger.error(
|
|
2088
2312
|
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2089
2313
|
{
|
|
@@ -2094,20 +2318,20 @@ function checkIsDynamicImport(ctx, node) {
|
|
|
2094
2318
|
);
|
|
2095
2319
|
}
|
|
2096
2320
|
};
|
|
2097
|
-
if (
|
|
2321
|
+
if (t20.isFunction(node)) {
|
|
2098
2322
|
checkIsDynamicImport(ctx, node.body);
|
|
2099
2323
|
return;
|
|
2100
2324
|
}
|
|
2101
|
-
if (
|
|
2325
|
+
if (t20.isBlockStatement(node)) {
|
|
2102
2326
|
const [returnSmt] = node.body;
|
|
2103
|
-
if (
|
|
2327
|
+
if (t20.isReturnStatement(returnSmt)) {
|
|
2104
2328
|
warnIsNotImport(returnSmt.argument);
|
|
2105
2329
|
}
|
|
2106
2330
|
return;
|
|
2107
2331
|
}
|
|
2108
|
-
if (
|
|
2332
|
+
if (t20.isCallExpression(node)) {
|
|
2109
2333
|
warnIsNotImport(node.callee);
|
|
2110
|
-
if (
|
|
2334
|
+
if (t20.isStringLiteral(node.arguments[0])) {
|
|
2111
2335
|
replaceVueSuffix(ctx, node.arguments[0]);
|
|
2112
2336
|
}
|
|
2113
2337
|
return;
|
|
@@ -2155,61 +2379,64 @@ function pushToGlobalScope(path7, ctx) {
|
|
|
2155
2379
|
scriptIR.statement.global.push(fullNode);
|
|
2156
2380
|
}
|
|
2157
2381
|
|
|
2158
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-
|
|
2382
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2159
2383
|
import * as t22 from "@babel/types";
|
|
2160
|
-
|
|
2384
|
+
|
|
2385
|
+
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2386
|
+
import * as t21 from "@babel/types";
|
|
2387
|
+
function createUseCallback(body, deps) {
|
|
2388
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useCallback), [
|
|
2389
|
+
body,
|
|
2390
|
+
deps ?? t21.arrayExpression([])
|
|
2391
|
+
]);
|
|
2392
|
+
}
|
|
2393
|
+
function createUseMemo(body, deps) {
|
|
2394
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useMemo), [
|
|
2395
|
+
t21.arrowFunctionExpression([], body),
|
|
2396
|
+
deps ?? t21.arrayExpression([])
|
|
2397
|
+
]);
|
|
2398
|
+
}
|
|
2399
|
+
function createUseImperativeHandle(refId, init) {
|
|
2400
|
+
return t21.callExpression(t21.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2404
|
+
function resolveDefineExpose(ctx) {
|
|
2405
|
+
if (ctx.inputType !== "sfc") return {};
|
|
2161
2406
|
return {
|
|
2162
2407
|
CallExpression(path7) {
|
|
2163
2408
|
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
|
-
});
|
|
2409
|
+
const { filename, scriptData } = ctx;
|
|
2410
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.expose)) {
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
const [expose] = node.arguments;
|
|
2414
|
+
if (!expose) {
|
|
2193
2415
|
path7.remove();
|
|
2194
2416
|
return;
|
|
2195
2417
|
}
|
|
2196
|
-
const
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2418
|
+
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2419
|
+
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2420
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
2421
|
+
if (!t22.isObjectExpression(expose) && !t22.isFunction(expose)) {
|
|
2422
|
+
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2423
|
+
file: filename,
|
|
2424
|
+
loc: expose.loc,
|
|
2425
|
+
source: scriptData.source
|
|
2426
|
+
});
|
|
2427
|
+
}
|
|
2428
|
+
const init = !t22.isFunction(expose) ? t22.arrowFunctionExpression([], expose) : expose;
|
|
2429
|
+
const { forwardRef } = scriptData;
|
|
2430
|
+
const newNode = createUseImperativeHandle(t22.identifier(forwardRef.refField), init);
|
|
2431
|
+
forwardRef.enabled = true;
|
|
2432
|
+
path7.replaceWith(newNode);
|
|
2206
2433
|
}
|
|
2207
2434
|
};
|
|
2208
2435
|
}
|
|
2209
2436
|
|
|
2210
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-options.ts
|
|
2437
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2211
2438
|
import * as t23 from "@babel/types";
|
|
2212
|
-
function
|
|
2439
|
+
function resolveDefineOptions(ctx) {
|
|
2213
2440
|
return {
|
|
2214
2441
|
CallExpression(path7) {
|
|
2215
2442
|
const { node } = path7;
|
|
@@ -2261,31 +2488,91 @@ function extractName(prop, ctx) {
|
|
|
2261
2488
|
}
|
|
2262
2489
|
}
|
|
2263
2490
|
|
|
2491
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2492
|
+
import * as t24 from "@babel/types";
|
|
2493
|
+
function resolveEmitCalls(ctx) {
|
|
2494
|
+
const formatEmitEventName = (raw) => {
|
|
2495
|
+
if (raw.startsWith("update:")) {
|
|
2496
|
+
const modelKey = raw.slice("update:".length);
|
|
2497
|
+
return `onUpdate${capitalize(camelCase(modelKey))}`;
|
|
2498
|
+
}
|
|
2499
|
+
const normalized = raw.includes(":") ? raw.replace(/:/g, "-") : raw;
|
|
2500
|
+
return `on${capitalize(camelCase(normalized))}`;
|
|
2501
|
+
};
|
|
2502
|
+
return {
|
|
2503
|
+
CallExpression(path7) {
|
|
2504
|
+
const { node } = path7;
|
|
2505
|
+
const { filename, templateData, scriptData } = ctx;
|
|
2506
|
+
if (!t24.isIdentifier(node.callee)) return;
|
|
2507
|
+
const { name } = node.callee;
|
|
2508
|
+
const checkIfFromDefineEmits = () => {
|
|
2509
|
+
let result = false;
|
|
2510
|
+
const meta = templateData.reactiveBindings[name];
|
|
2511
|
+
if (meta) {
|
|
2512
|
+
result = meta.source === MACRO_API_NAMES.emits;
|
|
2513
|
+
}
|
|
2514
|
+
if (!result) {
|
|
2515
|
+
const binding = path7.scope.getBinding(name);
|
|
2516
|
+
if (binding) {
|
|
2517
|
+
const parent = binding.path.node;
|
|
2518
|
+
if (t24.isVariableDeclarator(parent) && t24.isCallExpression(parent.init) && t24.isIdentifier(parent.init.callee)) {
|
|
2519
|
+
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
return result;
|
|
2524
|
+
};
|
|
2525
|
+
if (!checkIfFromDefineEmits()) return;
|
|
2526
|
+
const [callee, ...args] = node.arguments;
|
|
2527
|
+
const eventName = t24.isStringLiteral(callee) ? formatEmitEventName(callee.value) : void 0;
|
|
2528
|
+
if (!eventName) {
|
|
2529
|
+
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2530
|
+
file: filename,
|
|
2531
|
+
source: scriptData.source,
|
|
2532
|
+
loc: callee?.loc
|
|
2533
|
+
});
|
|
2534
|
+
path7.remove();
|
|
2535
|
+
return;
|
|
2536
|
+
}
|
|
2537
|
+
const propCall = t24.callExpression(
|
|
2538
|
+
t24.optionalMemberExpression(
|
|
2539
|
+
t24.identifier(ctx.propField),
|
|
2540
|
+
t24.identifier(eventName),
|
|
2541
|
+
false,
|
|
2542
|
+
true
|
|
2543
|
+
),
|
|
2544
|
+
args
|
|
2545
|
+
);
|
|
2546
|
+
path7.replaceWith(propCall);
|
|
2547
|
+
}
|
|
2548
|
+
};
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2264
2551
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2265
|
-
import * as
|
|
2552
|
+
import * as t29 from "@babel/types";
|
|
2266
2553
|
|
|
2267
2554
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2268
|
-
import * as
|
|
2555
|
+
import * as t26 from "@babel/types";
|
|
2269
2556
|
|
|
2270
2557
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2271
|
-
import * as
|
|
2558
|
+
import * as t25 from "@babel/types";
|
|
2272
2559
|
function cloneCallableParams(params) {
|
|
2273
2560
|
const cloneCallableParam = (param, index) => {
|
|
2274
|
-
if (
|
|
2561
|
+
if (t25.isRestElement(param)) {
|
|
2275
2562
|
const arg = param.argument;
|
|
2276
|
-
const name =
|
|
2277
|
-
const rest =
|
|
2278
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
2563
|
+
const name = t25.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
2564
|
+
const rest = t25.restElement(t25.identifier(name));
|
|
2565
|
+
rest.typeAnnotation = param.typeAnnotation || (t25.isIdentifier(arg) ? arg.typeAnnotation : null) || t25.tsTypeAnnotation(t25.tsArrayType(t25.tsAnyKeyword()));
|
|
2279
2566
|
return rest;
|
|
2280
2567
|
}
|
|
2281
|
-
if (
|
|
2282
|
-
const id =
|
|
2568
|
+
if (t25.isIdentifier(param)) {
|
|
2569
|
+
const id = t25.identifier(param.name || `arg${index}`);
|
|
2283
2570
|
id.optional = param.optional;
|
|
2284
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
2571
|
+
id.typeAnnotation = param.typeAnnotation || t25.tsTypeAnnotation(t25.tsAnyKeyword());
|
|
2285
2572
|
return id;
|
|
2286
2573
|
}
|
|
2287
|
-
const fallback =
|
|
2288
|
-
fallback.typeAnnotation =
|
|
2574
|
+
const fallback = t25.identifier(`arg${index}`);
|
|
2575
|
+
fallback.typeAnnotation = t25.tsTypeAnnotation(t25.tsAnyKeyword());
|
|
2289
2576
|
return fallback;
|
|
2290
2577
|
};
|
|
2291
2578
|
return params.map(cloneCallableParam);
|
|
@@ -2295,18 +2582,18 @@ function cloneCallableParams(params) {
|
|
|
2295
2582
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2296
2583
|
return {
|
|
2297
2584
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path7) {
|
|
2298
|
-
if (!
|
|
2585
|
+
if (!t26.isProgram(path7.parent)) return;
|
|
2299
2586
|
const { node } = path7;
|
|
2300
|
-
if (
|
|
2301
|
-
const typeLiteral =
|
|
2587
|
+
if (t26.isTSInterfaceDeclaration(node)) {
|
|
2588
|
+
const typeLiteral = t26.tsTypeLiteral(node.body.body);
|
|
2302
2589
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2303
2590
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2304
|
-
if (resolved &&
|
|
2591
|
+
if (resolved && t26.isTSTypeLiteral(resolved)) {
|
|
2305
2592
|
node.body.body = resolved.members;
|
|
2306
2593
|
}
|
|
2307
2594
|
return;
|
|
2308
2595
|
}
|
|
2309
|
-
if (
|
|
2596
|
+
if (t26.isTSTypeAliasDeclaration(node)) {
|
|
2310
2597
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2311
2598
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2312
2599
|
if (resolved) {
|
|
@@ -2317,47 +2604,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2317
2604
|
};
|
|
2318
2605
|
}
|
|
2319
2606
|
function resolveTopLevelEmitType(tsType) {
|
|
2320
|
-
if (
|
|
2607
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2321
2608
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2322
2609
|
}
|
|
2323
|
-
if (
|
|
2610
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2324
2611
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2325
2612
|
return tsType;
|
|
2326
2613
|
}
|
|
2327
2614
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2328
|
-
return
|
|
2615
|
+
return t26.tsTypeReference(
|
|
2329
2616
|
tsType.typeName,
|
|
2330
|
-
|
|
2617
|
+
t26.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2331
2618
|
);
|
|
2332
2619
|
}
|
|
2333
|
-
if (
|
|
2620
|
+
if (t26.isTSIntersectionType(tsType)) {
|
|
2334
2621
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2335
2622
|
if (!types.length) return null;
|
|
2336
2623
|
if (types.length === 1) return types[0];
|
|
2337
|
-
return
|
|
2624
|
+
return t26.tsIntersectionType(types);
|
|
2338
2625
|
}
|
|
2339
|
-
if (
|
|
2626
|
+
if (t26.isTSUnionType(tsType)) {
|
|
2340
2627
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2341
2628
|
if (!types.length) return null;
|
|
2342
2629
|
if (types.length === 1) return types[0];
|
|
2343
|
-
return
|
|
2630
|
+
return t26.tsUnionType(types);
|
|
2344
2631
|
}
|
|
2345
|
-
if (
|
|
2632
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2346
2633
|
const members = [];
|
|
2347
2634
|
for (const member of tsType.members) {
|
|
2348
|
-
if (
|
|
2635
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2349
2636
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2350
2637
|
continue;
|
|
2351
2638
|
}
|
|
2352
2639
|
members.push(member);
|
|
2353
2640
|
}
|
|
2354
2641
|
if (!members.length) return null;
|
|
2355
|
-
return
|
|
2642
|
+
return t26.tsTypeLiteral(members);
|
|
2356
2643
|
}
|
|
2357
|
-
if (
|
|
2644
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2358
2645
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2359
2646
|
if (!props.length) return null;
|
|
2360
|
-
return
|
|
2647
|
+
return t26.tsTypeLiteral(props);
|
|
2361
2648
|
}
|
|
2362
2649
|
return tsType;
|
|
2363
2650
|
}
|
|
@@ -2378,41 +2665,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2378
2665
|
propsTSIface: { emitTypes }
|
|
2379
2666
|
} = ctx.scriptData;
|
|
2380
2667
|
const members = [];
|
|
2381
|
-
if (
|
|
2668
|
+
if (t26.isArrayExpression(runtimeArg)) {
|
|
2382
2669
|
for (const element of runtimeArg.elements) {
|
|
2383
|
-
if (!element || !
|
|
2670
|
+
if (!element || !t26.isStringLiteral(element)) continue;
|
|
2384
2671
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2385
2672
|
const key = buildKey(handlerName);
|
|
2386
|
-
const fnType =
|
|
2673
|
+
const fnType = t26.tsFunctionType(
|
|
2387
2674
|
null,
|
|
2388
2675
|
[createRestAnyParam("args")],
|
|
2389
|
-
|
|
2676
|
+
t26.tsTypeAnnotation(t26.tsAnyKeyword())
|
|
2390
2677
|
);
|
|
2391
|
-
const prop =
|
|
2678
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2392
2679
|
prop.optional = true;
|
|
2393
2680
|
members.push(prop);
|
|
2394
2681
|
}
|
|
2395
2682
|
if (members.length) {
|
|
2396
|
-
emitTypes.push(
|
|
2683
|
+
emitTypes.push(t26.tsTypeLiteral(members));
|
|
2397
2684
|
}
|
|
2398
2685
|
return;
|
|
2399
2686
|
}
|
|
2400
|
-
if (
|
|
2687
|
+
if (t26.isObjectExpression(runtimeArg)) {
|
|
2401
2688
|
for (const prop of runtimeArg.properties) {
|
|
2402
|
-
if (!
|
|
2403
|
-
if (
|
|
2689
|
+
if (!t26.isObjectProperty(prop)) continue;
|
|
2690
|
+
if (t26.isSpreadElement(prop)) continue;
|
|
2404
2691
|
const rawName = resolvePropName(prop.key);
|
|
2405
2692
|
if (!rawName) continue;
|
|
2406
2693
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2407
2694
|
const key = buildKey(handlerName);
|
|
2408
|
-
const params =
|
|
2409
|
-
const fnType =
|
|
2410
|
-
const propSig =
|
|
2695
|
+
const params = t26.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
2696
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(t26.tsAnyKeyword()));
|
|
2697
|
+
const propSig = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2411
2698
|
propSig.optional = true;
|
|
2412
2699
|
members.push(propSig);
|
|
2413
2700
|
}
|
|
2414
2701
|
if (members.length) {
|
|
2415
|
-
emitTypes.push(
|
|
2702
|
+
emitTypes.push(t26.tsTypeLiteral(members));
|
|
2416
2703
|
}
|
|
2417
2704
|
}
|
|
2418
2705
|
}
|
|
@@ -2433,129 +2720,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2433
2720
|
return `on${name}`;
|
|
2434
2721
|
}
|
|
2435
2722
|
function resolvePropName(key) {
|
|
2436
|
-
if (
|
|
2437
|
-
if (
|
|
2438
|
-
if (
|
|
2723
|
+
if (t26.isIdentifier(key)) return key.name;
|
|
2724
|
+
if (t26.isStringLiteral(key)) return key.value;
|
|
2725
|
+
if (t26.isNumericLiteral(key)) return String(key.value);
|
|
2439
2726
|
return null;
|
|
2440
2727
|
}
|
|
2441
2728
|
function buildKey(name) {
|
|
2442
|
-
return
|
|
2729
|
+
return t26.isValidIdentifier(name) ? t26.identifier(name) : t26.stringLiteral(name);
|
|
2443
2730
|
}
|
|
2444
2731
|
function createRestAnyParam(name) {
|
|
2445
|
-
const id =
|
|
2446
|
-
const rest =
|
|
2447
|
-
rest.typeAnnotation =
|
|
2732
|
+
const id = t26.identifier(name);
|
|
2733
|
+
const rest = t26.restElement(id);
|
|
2734
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(t26.tsArrayType(t26.tsAnyKeyword()));
|
|
2448
2735
|
return rest;
|
|
2449
2736
|
}
|
|
2450
2737
|
function resolveRuntimeTupleParams(value) {
|
|
2451
2738
|
const params = [];
|
|
2452
2739
|
value.elements.forEach((element, index) => {
|
|
2453
2740
|
if (!element) return;
|
|
2454
|
-
if (
|
|
2741
|
+
if (t26.isSpreadElement(element)) {
|
|
2455
2742
|
params.push(createRestAnyParam(`args${index}`));
|
|
2456
2743
|
return;
|
|
2457
2744
|
}
|
|
2458
|
-
if (
|
|
2459
|
-
const id =
|
|
2460
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
2745
|
+
if (t26.isIdentifier(element)) {
|
|
2746
|
+
const id = t26.identifier(element.name);
|
|
2747
|
+
id.typeAnnotation = element.typeAnnotation || t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2461
2748
|
params.push(id);
|
|
2462
2749
|
return;
|
|
2463
2750
|
}
|
|
2464
|
-
if (
|
|
2465
|
-
const id =
|
|
2466
|
-
id.typeAnnotation =
|
|
2751
|
+
if (t26.isTSAsExpression(element)) {
|
|
2752
|
+
const id = t26.identifier(`arg${index}`);
|
|
2753
|
+
id.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2467
2754
|
params.push(id);
|
|
2468
2755
|
return;
|
|
2469
2756
|
}
|
|
2470
|
-
const fallback =
|
|
2471
|
-
fallback.typeAnnotation =
|
|
2757
|
+
const fallback = t26.identifier(`arg${index}`);
|
|
2758
|
+
fallback.typeAnnotation = t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2472
2759
|
params.push(fallback);
|
|
2473
2760
|
});
|
|
2474
2761
|
return params;
|
|
2475
2762
|
}
|
|
2476
2763
|
function resolveExplicitEmitType(tsType) {
|
|
2477
|
-
if (
|
|
2764
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2478
2765
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2479
2766
|
}
|
|
2480
|
-
if (
|
|
2767
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2481
2768
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2482
2769
|
return tsType;
|
|
2483
2770
|
}
|
|
2484
2771
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2485
|
-
return
|
|
2772
|
+
return t26.tsTypeReference(
|
|
2486
2773
|
tsType.typeName,
|
|
2487
|
-
|
|
2774
|
+
t26.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2488
2775
|
);
|
|
2489
2776
|
}
|
|
2490
|
-
if (
|
|
2777
|
+
if (t26.isTSIntersectionType(tsType)) {
|
|
2491
2778
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2492
2779
|
if (!types.length) return null;
|
|
2493
2780
|
if (types.length === 1) return types[0];
|
|
2494
|
-
return
|
|
2781
|
+
return t26.tsIntersectionType(types);
|
|
2495
2782
|
}
|
|
2496
|
-
if (
|
|
2783
|
+
if (t26.isTSUnionType(tsType)) {
|
|
2497
2784
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2498
2785
|
if (!types.length) return null;
|
|
2499
2786
|
if (types.length === 1) return types[0];
|
|
2500
|
-
return
|
|
2787
|
+
return t26.tsUnionType(types);
|
|
2501
2788
|
}
|
|
2502
|
-
if (
|
|
2789
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2503
2790
|
const members = [];
|
|
2504
2791
|
for (const member of tsType.members) {
|
|
2505
|
-
if (
|
|
2792
|
+
if (t26.isTSPropertySignature(member)) {
|
|
2506
2793
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2507
2794
|
if (prop) members.push(prop);
|
|
2508
2795
|
continue;
|
|
2509
2796
|
}
|
|
2510
|
-
if (
|
|
2797
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2511
2798
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2512
2799
|
continue;
|
|
2513
2800
|
}
|
|
2514
2801
|
}
|
|
2515
2802
|
if (!members.length) return null;
|
|
2516
|
-
return
|
|
2803
|
+
return t26.tsTypeLiteral(members);
|
|
2517
2804
|
}
|
|
2518
|
-
if (
|
|
2805
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2519
2806
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2520
2807
|
if (!props.length) return null;
|
|
2521
|
-
return
|
|
2808
|
+
return t26.tsTypeLiteral(props);
|
|
2522
2809
|
}
|
|
2523
2810
|
return tsType;
|
|
2524
2811
|
}
|
|
2525
2812
|
function hasEmitsSignatureInType(tsType) {
|
|
2526
|
-
if (
|
|
2813
|
+
if (t26.isTSParenthesizedType(tsType)) {
|
|
2527
2814
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
2528
2815
|
}
|
|
2529
|
-
if (
|
|
2816
|
+
if (t26.isTSTypeReference(tsType)) {
|
|
2530
2817
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2531
2818
|
return false;
|
|
2532
2819
|
}
|
|
2533
2820
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
2534
2821
|
}
|
|
2535
|
-
if (
|
|
2822
|
+
if (t26.isTSIntersectionType(tsType) || t26.isTSUnionType(tsType)) {
|
|
2536
2823
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
2537
2824
|
}
|
|
2538
|
-
if (
|
|
2825
|
+
if (t26.isTSTypeLiteral(tsType)) {
|
|
2539
2826
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
2540
2827
|
}
|
|
2541
|
-
if (
|
|
2828
|
+
if (t26.isTSFunctionType(tsType)) {
|
|
2542
2829
|
return isEmitsCallable(tsType.parameters);
|
|
2543
2830
|
}
|
|
2544
2831
|
return false;
|
|
2545
2832
|
}
|
|
2546
2833
|
function hasEmitsSignatureInMember(member) {
|
|
2547
|
-
if (
|
|
2834
|
+
if (t26.isTSCallSignatureDeclaration(member)) {
|
|
2548
2835
|
return isEmitsCallable(member.parameters);
|
|
2549
2836
|
}
|
|
2550
2837
|
return false;
|
|
2551
2838
|
}
|
|
2552
2839
|
function isEmitsCallable(parameters) {
|
|
2553
2840
|
const [eventParam] = parameters;
|
|
2554
|
-
if (!eventParam || !
|
|
2841
|
+
if (!eventParam || !t26.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2555
2842
|
return false;
|
|
2556
2843
|
}
|
|
2557
2844
|
const { typeAnnotation } = eventParam;
|
|
2558
|
-
if (
|
|
2845
|
+
if (t26.isNoop(typeAnnotation)) return false;
|
|
2559
2846
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
2560
2847
|
}
|
|
2561
2848
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -2565,19 +2852,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
2565
2852
|
const key = buildKey(handlerName);
|
|
2566
2853
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2567
2854
|
let params = [];
|
|
2568
|
-
let returnType =
|
|
2569
|
-
if (typeAnnotation &&
|
|
2855
|
+
let returnType = t26.tsAnyKeyword();
|
|
2856
|
+
if (typeAnnotation && t26.isTSFunctionType(typeAnnotation)) {
|
|
2570
2857
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
2571
2858
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
2572
|
-
} else if (typeAnnotation &&
|
|
2859
|
+
} else if (typeAnnotation && t26.isTSTupleType(typeAnnotation)) {
|
|
2573
2860
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
2574
2861
|
} else if (typeAnnotation) {
|
|
2575
|
-
const id =
|
|
2576
|
-
id.typeAnnotation =
|
|
2862
|
+
const id = t26.identifier("value");
|
|
2863
|
+
id.typeAnnotation = t26.tsTypeAnnotation(typeAnnotation);
|
|
2577
2864
|
params = [id];
|
|
2578
2865
|
}
|
|
2579
|
-
const fnType =
|
|
2580
|
-
const prop =
|
|
2866
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(returnType));
|
|
2867
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2581
2868
|
prop.optional = !!member.optional;
|
|
2582
2869
|
return prop;
|
|
2583
2870
|
}
|
|
@@ -2586,32 +2873,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
2586
2873
|
}
|
|
2587
2874
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
2588
2875
|
const [eventParam, ...restParams] = parameters;
|
|
2589
|
-
if (!eventParam || !
|
|
2876
|
+
if (!eventParam || !t26.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2590
2877
|
return [];
|
|
2591
2878
|
}
|
|
2592
2879
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
2593
|
-
if (
|
|
2880
|
+
if (t26.isNoop(paramTypeAnnotation)) return [];
|
|
2594
2881
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
2595
2882
|
if (!eventNames.length) return [];
|
|
2596
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
2883
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t26.tsAnyKeyword();
|
|
2597
2884
|
return eventNames.map((eventName) => {
|
|
2598
2885
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
2599
2886
|
const key = buildKey(handlerName);
|
|
2600
2887
|
const params = cloneCallableParams(restParams);
|
|
2601
|
-
const fnType =
|
|
2602
|
-
const prop =
|
|
2888
|
+
const fnType = t26.tsFunctionType(null, params, t26.tsTypeAnnotation(returnType));
|
|
2889
|
+
const prop = t26.tsPropertySignature(key, t26.tsTypeAnnotation(fnType));
|
|
2603
2890
|
prop.optional = true;
|
|
2604
2891
|
return prop;
|
|
2605
2892
|
});
|
|
2606
2893
|
}
|
|
2607
2894
|
function resolveEventNames(type) {
|
|
2608
|
-
if (
|
|
2895
|
+
if (t26.isTSLiteralType(type) && t26.isStringLiteral(type.literal)) {
|
|
2609
2896
|
return [type.literal.value];
|
|
2610
2897
|
}
|
|
2611
|
-
if (
|
|
2898
|
+
if (t26.isTSUnionType(type)) {
|
|
2612
2899
|
return type.types.flatMap(resolveEventNames);
|
|
2613
2900
|
}
|
|
2614
|
-
if (
|
|
2901
|
+
if (t26.isTSParenthesizedType(type)) {
|
|
2615
2902
|
return resolveEventNames(type.typeAnnotation);
|
|
2616
2903
|
}
|
|
2617
2904
|
return [];
|
|
@@ -2624,44 +2911,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
2624
2911
|
return params;
|
|
2625
2912
|
}
|
|
2626
2913
|
function resolveTupleElementParam(element, index) {
|
|
2627
|
-
const isNamedTuple = typeof
|
|
2914
|
+
const isNamedTuple = typeof t26.isTSNamedTupleMember === "function" && t26.isTSNamedTupleMember(element);
|
|
2628
2915
|
if (isNamedTuple) {
|
|
2629
2916
|
const tupleMember = element;
|
|
2630
2917
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
2631
2918
|
let innerType = tupleMember.elementType;
|
|
2632
2919
|
let optional = tupleMember.optional;
|
|
2633
|
-
if (
|
|
2920
|
+
if (t26.isTSOptionalType(innerType)) {
|
|
2634
2921
|
optional = true;
|
|
2635
2922
|
innerType = innerType.typeAnnotation;
|
|
2636
2923
|
}
|
|
2637
|
-
if (
|
|
2638
|
-
const rest =
|
|
2639
|
-
rest.typeAnnotation =
|
|
2924
|
+
if (t26.isTSRestType(innerType)) {
|
|
2925
|
+
const rest = t26.restElement(t26.identifier(name));
|
|
2926
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(innerType.typeAnnotation);
|
|
2640
2927
|
return rest;
|
|
2641
2928
|
}
|
|
2642
|
-
const id2 =
|
|
2929
|
+
const id2 = t26.identifier(name);
|
|
2643
2930
|
id2.optional = optional;
|
|
2644
|
-
id2.typeAnnotation =
|
|
2931
|
+
id2.typeAnnotation = t26.tsTypeAnnotation(innerType);
|
|
2645
2932
|
return id2;
|
|
2646
2933
|
}
|
|
2647
|
-
if (
|
|
2648
|
-
const rest =
|
|
2649
|
-
rest.typeAnnotation =
|
|
2934
|
+
if (t26.isTSRestType(element)) {
|
|
2935
|
+
const rest = t26.restElement(t26.identifier(`args${index}`));
|
|
2936
|
+
rest.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2650
2937
|
return rest;
|
|
2651
2938
|
}
|
|
2652
|
-
if (
|
|
2653
|
-
const id2 =
|
|
2939
|
+
if (t26.isTSOptionalType(element)) {
|
|
2940
|
+
const id2 = t26.identifier(`arg${index}`);
|
|
2654
2941
|
id2.optional = true;
|
|
2655
|
-
id2.typeAnnotation =
|
|
2942
|
+
id2.typeAnnotation = t26.tsTypeAnnotation(element.typeAnnotation);
|
|
2656
2943
|
return id2;
|
|
2657
2944
|
}
|
|
2658
|
-
const id =
|
|
2659
|
-
id.typeAnnotation =
|
|
2945
|
+
const id = t26.identifier(`arg${index}`);
|
|
2946
|
+
id.typeAnnotation = t26.tsTypeAnnotation(element);
|
|
2660
2947
|
return id;
|
|
2661
2948
|
}
|
|
2662
2949
|
|
|
2663
2950
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
2664
|
-
import * as
|
|
2951
|
+
import * as t27 from "@babel/types";
|
|
2665
2952
|
function resolveDefinePropsIface(path7, ctx) {
|
|
2666
2953
|
const { node } = path7;
|
|
2667
2954
|
const [runtimeArg] = node.arguments;
|
|
@@ -2681,38 +2968,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2681
2968
|
} = scriptData;
|
|
2682
2969
|
if (!runtimeArg) return;
|
|
2683
2970
|
const members = [];
|
|
2684
|
-
if (
|
|
2971
|
+
if (t27.isArrayExpression(runtimeArg)) {
|
|
2685
2972
|
for (const element of runtimeArg.elements) {
|
|
2686
|
-
if (!element || !
|
|
2687
|
-
const key =
|
|
2688
|
-
const prop =
|
|
2973
|
+
if (!element || !t27.isStringLiteral(element)) continue;
|
|
2974
|
+
const key = t27.isValidIdentifier(element.value) ? t27.identifier(element.value) : t27.stringLiteral(element.value);
|
|
2975
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2689
2976
|
prop.optional = true;
|
|
2690
2977
|
members.push(prop);
|
|
2691
2978
|
}
|
|
2692
2979
|
if (members.length) {
|
|
2693
|
-
propsTypes.push(
|
|
2980
|
+
propsTypes.push(t27.tsTypeLiteral(members));
|
|
2694
2981
|
}
|
|
2695
2982
|
return;
|
|
2696
2983
|
}
|
|
2697
|
-
if (
|
|
2984
|
+
if (t27.isObjectExpression(runtimeArg)) {
|
|
2698
2985
|
for (const prop of runtimeArg.properties) {
|
|
2699
|
-
if (!
|
|
2700
|
-
if (
|
|
2986
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
2987
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2701
2988
|
const key = prop.key;
|
|
2702
2989
|
let propName = null;
|
|
2703
|
-
if (
|
|
2704
|
-
if (
|
|
2705
|
-
if (
|
|
2990
|
+
if (t27.isIdentifier(key)) propName = key.name;
|
|
2991
|
+
if (t27.isStringLiteral(key)) propName = key.value;
|
|
2992
|
+
if (t27.isNumericLiteral(key)) propName = String(key.value);
|
|
2706
2993
|
if (!propName) continue;
|
|
2707
2994
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
2708
|
-
const tsType = type ??
|
|
2709
|
-
const tsKey =
|
|
2710
|
-
const tsProp =
|
|
2995
|
+
const tsType = type ?? t27.tsAnyKeyword();
|
|
2996
|
+
const tsKey = t27.isValidIdentifier(propName) ? t27.identifier(propName) : t27.stringLiteral(propName);
|
|
2997
|
+
const tsProp = t27.tsPropertySignature(tsKey, t27.tsTypeAnnotation(tsType));
|
|
2711
2998
|
tsProp.optional = !required;
|
|
2712
2999
|
members.push(tsProp);
|
|
2713
3000
|
}
|
|
2714
3001
|
if (members.length) {
|
|
2715
|
-
propsTypes.push(
|
|
3002
|
+
propsTypes.push(t27.tsTypeLiteral(members));
|
|
2716
3003
|
}
|
|
2717
3004
|
return;
|
|
2718
3005
|
}
|
|
@@ -2726,42 +3013,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2726
3013
|
);
|
|
2727
3014
|
}
|
|
2728
3015
|
function resolveRuntimePropMeta(value) {
|
|
2729
|
-
if (
|
|
3016
|
+
if (t27.isIdentifier(value)) {
|
|
2730
3017
|
return {
|
|
2731
3018
|
type: mapRuntimeTypeToTSType(value),
|
|
2732
3019
|
required: false
|
|
2733
3020
|
};
|
|
2734
3021
|
}
|
|
2735
|
-
if (
|
|
3022
|
+
if (t27.isArrayExpression(value)) {
|
|
2736
3023
|
return {
|
|
2737
3024
|
type: resolveRuntimeUnionType(value),
|
|
2738
3025
|
required: false
|
|
2739
3026
|
};
|
|
2740
3027
|
}
|
|
2741
|
-
if (!
|
|
3028
|
+
if (!t27.isObjectExpression(value)) {
|
|
2742
3029
|
return { required: false };
|
|
2743
3030
|
}
|
|
2744
3031
|
let type;
|
|
2745
3032
|
let required = false;
|
|
2746
3033
|
for (const prop of value.properties) {
|
|
2747
|
-
if (!
|
|
2748
|
-
if (
|
|
3034
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
3035
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2749
3036
|
const key = prop.key;
|
|
2750
|
-
const propName =
|
|
3037
|
+
const propName = t27.isIdentifier(key) ? key.name : t27.isStringLiteral(key) ? key.value : null;
|
|
2751
3038
|
if (!propName) continue;
|
|
2752
3039
|
if (propName === "type") {
|
|
2753
3040
|
const valueNode = prop.value;
|
|
2754
|
-
if (
|
|
3041
|
+
if (t27.isArrayExpression(valueNode)) {
|
|
2755
3042
|
type = resolveRuntimeUnionType(valueNode);
|
|
2756
3043
|
continue;
|
|
2757
3044
|
}
|
|
2758
|
-
if (
|
|
3045
|
+
if (t27.isIdentifier(valueNode)) {
|
|
2759
3046
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
2760
3047
|
continue;
|
|
2761
3048
|
}
|
|
2762
3049
|
}
|
|
2763
3050
|
if (propName === "required") {
|
|
2764
|
-
if (
|
|
3051
|
+
if (t27.isBooleanLiteral(prop.value)) {
|
|
2765
3052
|
required = prop.value.value;
|
|
2766
3053
|
}
|
|
2767
3054
|
}
|
|
@@ -2771,58 +3058,58 @@ function resolveRuntimePropMeta(value) {
|
|
|
2771
3058
|
function resolveRuntimeUnionType(value) {
|
|
2772
3059
|
const types = [];
|
|
2773
3060
|
for (const element of value.elements) {
|
|
2774
|
-
if (!element || !
|
|
3061
|
+
if (!element || !t27.isIdentifier(element)) continue;
|
|
2775
3062
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
2776
3063
|
if (resolved) types.push(resolved);
|
|
2777
3064
|
}
|
|
2778
|
-
if (!types.length) return
|
|
3065
|
+
if (!types.length) return t27.tsAnyKeyword();
|
|
2779
3066
|
if (types.length === 1) return types[0];
|
|
2780
|
-
return
|
|
3067
|
+
return t27.tsUnionType(types);
|
|
2781
3068
|
}
|
|
2782
3069
|
function mapRuntimeTypeToTSType(value) {
|
|
2783
3070
|
switch (value.name) {
|
|
2784
3071
|
case "String":
|
|
2785
|
-
return
|
|
3072
|
+
return t27.tsStringKeyword();
|
|
2786
3073
|
case "Number":
|
|
2787
|
-
return
|
|
3074
|
+
return t27.tsNumberKeyword();
|
|
2788
3075
|
case "Boolean":
|
|
2789
|
-
return
|
|
3076
|
+
return t27.tsBooleanKeyword();
|
|
2790
3077
|
case "Object":
|
|
2791
|
-
return
|
|
3078
|
+
return t27.tsTypeLiteral([]);
|
|
2792
3079
|
case "Array":
|
|
2793
|
-
return
|
|
3080
|
+
return t27.tsArrayType(t27.tsAnyKeyword());
|
|
2794
3081
|
case "Function":
|
|
2795
|
-
return
|
|
3082
|
+
return t27.tsFunctionType(null, [], t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2796
3083
|
case "Symbol":
|
|
2797
|
-
return
|
|
3084
|
+
return t27.tsSymbolKeyword();
|
|
2798
3085
|
case "BigInt":
|
|
2799
|
-
return
|
|
3086
|
+
return t27.tsBigIntKeyword();
|
|
2800
3087
|
default:
|
|
2801
|
-
return
|
|
3088
|
+
return t27.tsAnyKeyword();
|
|
2802
3089
|
}
|
|
2803
3090
|
}
|
|
2804
3091
|
|
|
2805
3092
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
2806
|
-
import * as
|
|
3093
|
+
import * as t28 from "@babel/types";
|
|
2807
3094
|
var SLOT_DEFAULT_NAME = "default";
|
|
2808
3095
|
var SLOT_CHILDREN_NAME = "children";
|
|
2809
3096
|
var SLOT_FN_PARAM_NAME = "props";
|
|
2810
3097
|
function resolveSlotsTopLevelTypes(ctx) {
|
|
2811
3098
|
return {
|
|
2812
3099
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path7) {
|
|
2813
|
-
if (!
|
|
3100
|
+
if (!t28.isProgram(path7.parent)) return;
|
|
2814
3101
|
const { node } = path7;
|
|
2815
|
-
if (
|
|
2816
|
-
const typeLiteral =
|
|
3102
|
+
if (t28.isTSInterfaceDeclaration(node)) {
|
|
3103
|
+
const typeLiteral = t28.tsTypeLiteral(node.body.body);
|
|
2817
3104
|
if (!hasSlotsSignatureInType(typeLiteral)) return;
|
|
2818
3105
|
const resolved = resolveSlotType(typeLiteral);
|
|
2819
|
-
if (resolved &&
|
|
3106
|
+
if (resolved && t28.isTSTypeLiteral(resolved)) {
|
|
2820
3107
|
node.body.body = resolved.members;
|
|
2821
3108
|
recordReactNode(ctx);
|
|
2822
3109
|
}
|
|
2823
3110
|
return;
|
|
2824
3111
|
}
|
|
2825
|
-
if (
|
|
3112
|
+
if (t28.isTSTypeAliasDeclaration(node)) {
|
|
2826
3113
|
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
2827
3114
|
const resolved = resolveSlotType(node.typeAnnotation);
|
|
2828
3115
|
if (resolved) {
|
|
@@ -2867,7 +3154,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
2867
3154
|
}
|
|
2868
3155
|
if (tsMembers.length) {
|
|
2869
3156
|
recordReactNode(ctx);
|
|
2870
|
-
slotTypes.push(
|
|
3157
|
+
slotTypes.push(t28.tsTypeLiteral(tsMembers));
|
|
2871
3158
|
}
|
|
2872
3159
|
}
|
|
2873
3160
|
function recordReactNode(ctx) {
|
|
@@ -2877,32 +3164,32 @@ function recordReactNode(ctx) {
|
|
|
2877
3164
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
2878
3165
|
}
|
|
2879
3166
|
function resolveSlotType(tsType) {
|
|
2880
|
-
if (
|
|
3167
|
+
if (t28.isTSParenthesizedType(tsType)) {
|
|
2881
3168
|
return resolveSlotType(tsType.typeAnnotation);
|
|
2882
3169
|
}
|
|
2883
|
-
if (
|
|
3170
|
+
if (t28.isTSTypeReference(tsType)) {
|
|
2884
3171
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2885
3172
|
return tsType;
|
|
2886
3173
|
}
|
|
2887
3174
|
const params = tsType.typeParameters.params.map((param) => resolveSlotType(param)).filter(Boolean);
|
|
2888
|
-
return
|
|
3175
|
+
return t28.tsTypeReference(
|
|
2889
3176
|
tsType.typeName,
|
|
2890
|
-
|
|
3177
|
+
t28.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2891
3178
|
);
|
|
2892
3179
|
}
|
|
2893
|
-
if (
|
|
3180
|
+
if (t28.isTSIntersectionType(tsType)) {
|
|
2894
3181
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
2895
3182
|
if (!types.length) return null;
|
|
2896
3183
|
if (types.length === 1) return types[0];
|
|
2897
|
-
return
|
|
3184
|
+
return t28.tsIntersectionType(types);
|
|
2898
3185
|
}
|
|
2899
|
-
if (
|
|
3186
|
+
if (t28.isTSUnionType(tsType)) {
|
|
2900
3187
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
2901
3188
|
if (!types.length) return null;
|
|
2902
3189
|
if (types.length === 1) return types[0];
|
|
2903
|
-
return
|
|
3190
|
+
return t28.tsUnionType(types);
|
|
2904
3191
|
}
|
|
2905
|
-
if (
|
|
3192
|
+
if (t28.isTSTypeLiteral(tsType)) {
|
|
2906
3193
|
const members = [];
|
|
2907
3194
|
for (const member of tsType.members) {
|
|
2908
3195
|
const resolved = resolveSlotPropFromMember(member);
|
|
@@ -2913,56 +3200,56 @@ function resolveSlotType(tsType) {
|
|
|
2913
3200
|
members.push(member);
|
|
2914
3201
|
}
|
|
2915
3202
|
if (!members.length) return null;
|
|
2916
|
-
return
|
|
3203
|
+
return t28.tsTypeLiteral(members);
|
|
2917
3204
|
}
|
|
2918
|
-
if (
|
|
3205
|
+
if (t28.isTSFunctionType(tsType)) {
|
|
2919
3206
|
const props = buildSlotPropSignature(
|
|
2920
3207
|
SLOT_DEFAULT_NAME,
|
|
2921
3208
|
cloneCallableParams(tsType.parameters),
|
|
2922
3209
|
false
|
|
2923
3210
|
);
|
|
2924
|
-
return
|
|
3211
|
+
return t28.tsTypeLiteral([props]);
|
|
2925
3212
|
}
|
|
2926
3213
|
return tsType;
|
|
2927
3214
|
}
|
|
2928
3215
|
function hasSlotsSignatureInType(tsType) {
|
|
2929
|
-
if (
|
|
3216
|
+
if (t28.isTSParenthesizedType(tsType)) {
|
|
2930
3217
|
return hasSlotsSignatureInType(tsType.typeAnnotation);
|
|
2931
3218
|
}
|
|
2932
|
-
if (
|
|
3219
|
+
if (t28.isTSTypeReference(tsType)) {
|
|
2933
3220
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2934
3221
|
return false;
|
|
2935
3222
|
}
|
|
2936
3223
|
return tsType.typeParameters.params.some(hasSlotsSignatureInType);
|
|
2937
3224
|
}
|
|
2938
|
-
if (
|
|
3225
|
+
if (t28.isTSIntersectionType(tsType) || t28.isTSUnionType(tsType)) {
|
|
2939
3226
|
return tsType.types.some(hasSlotsSignatureInType);
|
|
2940
3227
|
}
|
|
2941
|
-
if (
|
|
3228
|
+
if (t28.isTSTypeLiteral(tsType)) {
|
|
2942
3229
|
return tsType.members.some(hasSlotsSignatureInMember);
|
|
2943
3230
|
}
|
|
2944
|
-
if (
|
|
3231
|
+
if (t28.isTSFunctionType(tsType)) {
|
|
2945
3232
|
return true;
|
|
2946
3233
|
}
|
|
2947
3234
|
return false;
|
|
2948
3235
|
}
|
|
2949
3236
|
function hasSlotsSignatureInMember(member) {
|
|
2950
|
-
if (
|
|
2951
|
-
if (
|
|
2952
|
-
if (
|
|
3237
|
+
if (t28.isTSMethodSignature(member)) return true;
|
|
3238
|
+
if (t28.isTSCallSignatureDeclaration(member)) return true;
|
|
3239
|
+
if (t28.isTSPropertySignature(member)) {
|
|
2953
3240
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2954
3241
|
return !!(typeAnnotation && resolveCallableType(typeAnnotation));
|
|
2955
3242
|
}
|
|
2956
3243
|
return false;
|
|
2957
3244
|
}
|
|
2958
3245
|
function resolveSlotPropFromMember(member) {
|
|
2959
|
-
if (
|
|
3246
|
+
if (t28.isTSMethodSignature(member)) {
|
|
2960
3247
|
const rawName = resolvePropName2(member.key);
|
|
2961
3248
|
if (!rawName) return null;
|
|
2962
3249
|
const params = cloneCallableParams(member.parameters);
|
|
2963
3250
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
2964
3251
|
}
|
|
2965
|
-
if (
|
|
3252
|
+
if (t28.isTSPropertySignature(member)) {
|
|
2966
3253
|
const rawName = resolvePropName2(member.key);
|
|
2967
3254
|
if (!rawName) return null;
|
|
2968
3255
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
@@ -2971,52 +3258,53 @@ function resolveSlotPropFromMember(member) {
|
|
|
2971
3258
|
const params = cloneCallableParams(callable.parameters);
|
|
2972
3259
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
2973
3260
|
}
|
|
2974
|
-
if (
|
|
3261
|
+
if (t28.isTSCallSignatureDeclaration(member)) {
|
|
2975
3262
|
const params = cloneCallableParams(member.parameters);
|
|
2976
3263
|
return buildSlotPropSignature(SLOT_DEFAULT_NAME, params, true);
|
|
2977
3264
|
}
|
|
2978
3265
|
return null;
|
|
2979
3266
|
}
|
|
2980
3267
|
function resolveCallableType(tsType) {
|
|
2981
|
-
if (
|
|
2982
|
-
if (
|
|
3268
|
+
if (t28.isTSFunctionType(tsType)) return tsType;
|
|
3269
|
+
if (t28.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
2983
3270
|
return null;
|
|
2984
3271
|
}
|
|
2985
3272
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
2986
3273
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
2987
|
-
const key =
|
|
2988
|
-
const reactNodeType =
|
|
2989
|
-
|
|
3274
|
+
const key = t28.isValidIdentifier(propName) ? t28.identifier(propName) : t28.stringLiteral(propName);
|
|
3275
|
+
const reactNodeType = t28.tsTypeAnnotation(
|
|
3276
|
+
t28.tsTypeReference(t28.identifier(REACT_API_MAP.ReactNode))
|
|
2990
3277
|
);
|
|
2991
3278
|
let typeAnnotation;
|
|
2992
3279
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
2993
3280
|
typeAnnotation = reactNodeType;
|
|
2994
3281
|
} else {
|
|
2995
|
-
const fnType =
|
|
2996
|
-
typeAnnotation =
|
|
3282
|
+
const fnType = t28.tsFunctionType(null, params, reactNodeType);
|
|
3283
|
+
typeAnnotation = t28.tsTypeAnnotation(fnType);
|
|
2997
3284
|
}
|
|
2998
|
-
const prop =
|
|
3285
|
+
const prop = t28.tsPropertySignature(key, typeAnnotation);
|
|
2999
3286
|
prop.optional = optional;
|
|
3000
3287
|
return prop;
|
|
3001
3288
|
}
|
|
3002
3289
|
function createSlotScopeParam(props, ctx) {
|
|
3003
|
-
const paramId =
|
|
3290
|
+
const paramId = t28.identifier(SLOT_FN_PARAM_NAME);
|
|
3004
3291
|
const propsSigns = [];
|
|
3005
3292
|
const { reactiveBindings } = ctx.templateData;
|
|
3006
3293
|
props.forEach(({ prop, tsType }) => {
|
|
3007
3294
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3008
3295
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3009
|
-
const typeAnnotation = foundBindingTypes ?
|
|
3010
|
-
const
|
|
3296
|
+
const typeAnnotation = foundBindingTypes ? t28.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3297
|
+
const key = t28.isValidIdentifier(prop) ? t28.identifier(prop) : t28.stringLiteral(prop);
|
|
3298
|
+
const propSign = t28.tsPropertySignature(key, typeAnnotation);
|
|
3011
3299
|
propsSigns.push(propSign);
|
|
3012
3300
|
});
|
|
3013
|
-
paramId.typeAnnotation =
|
|
3301
|
+
paramId.typeAnnotation = t28.tsTypeAnnotation(t28.tsTypeLiteral(propsSigns));
|
|
3014
3302
|
return paramId;
|
|
3015
3303
|
}
|
|
3016
3304
|
function resolvePropName2(key) {
|
|
3017
|
-
if (
|
|
3018
|
-
if (
|
|
3019
|
-
if (
|
|
3305
|
+
if (t28.isIdentifier(key)) return key.name;
|
|
3306
|
+
if (t28.isStringLiteral(key)) return key.value;
|
|
3307
|
+
if (t28.isNumericLiteral(key)) return String(key.value);
|
|
3020
3308
|
return null;
|
|
3021
3309
|
}
|
|
3022
3310
|
|
|
@@ -3068,65 +3356,17 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3068
3356
|
}
|
|
3069
3357
|
const n = ctx.compName || "Comp";
|
|
3070
3358
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3071
|
-
const typeNode =
|
|
3072
|
-
const typeAliasDecl =
|
|
3073
|
-
const exportDecl =
|
|
3359
|
+
const typeNode = t29.tsIntersectionType(tsTypes);
|
|
3360
|
+
const typeAliasDecl = t29.tsTypeAliasDeclaration(t29.identifier(ns), null, typeNode);
|
|
3361
|
+
const exportDecl = t29.exportNamedDeclaration(typeAliasDecl);
|
|
3074
3362
|
propsTSIface.name = ns;
|
|
3075
3363
|
const scriptIR = getScriptIR(ctx);
|
|
3076
3364
|
scriptIR.exports.push(exportDecl);
|
|
3077
3365
|
void ast;
|
|
3078
3366
|
}
|
|
3079
3367
|
|
|
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
|
-
}
|
|
3368
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3369
|
+
import * as t31 from "@babel/types";
|
|
3130
3370
|
|
|
3131
3371
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3132
3372
|
import { traverse as traverse2 } from "@babel/core";
|
|
@@ -3376,19 +3616,48 @@ function getIsAnalyzed(node) {
|
|
|
3376
3616
|
return getScriptNodeMeta(node)?.is_deps_analyzed;
|
|
3377
3617
|
}
|
|
3378
3618
|
|
|
3379
|
-
// src/core/transform/sfc/script/
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3619
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3620
|
+
function resolveAnalysisOnlyAdapter(ctx) {
|
|
3621
|
+
return {
|
|
3622
|
+
"CallExpression|Identifier"(path7) {
|
|
3623
|
+
const node = path7.node;
|
|
3624
|
+
const apiName = getApiName(node);
|
|
3625
|
+
const adapter = ADAPTER_RULES.runtime[apiName];
|
|
3626
|
+
if (!adapter || adapter.type !== "analyzed-deps") {
|
|
3627
|
+
return;
|
|
3628
|
+
}
|
|
3629
|
+
if (t31.isCallExpression(node)) {
|
|
3630
|
+
resolveCallNode(path7, adapter, ctx);
|
|
3631
|
+
} else {
|
|
3632
|
+
replaceIdName(node, adapter.target);
|
|
3633
|
+
}
|
|
3634
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3635
|
+
}
|
|
3636
|
+
};
|
|
3386
3637
|
}
|
|
3387
|
-
function
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3638
|
+
function getApiName(node) {
|
|
3639
|
+
const isCallNode = t31.isCallExpression(node);
|
|
3640
|
+
let apiName = "";
|
|
3641
|
+
if (t31.isIdentifier(node)) {
|
|
3642
|
+
apiName = node.name;
|
|
3643
|
+
} else if (isCallNode && t31.isIdentifier(node.callee)) {
|
|
3644
|
+
apiName = node.callee.name;
|
|
3645
|
+
}
|
|
3646
|
+
return apiName;
|
|
3647
|
+
}
|
|
3648
|
+
function resolveCallNode(path7, adapter, ctx) {
|
|
3649
|
+
const { node } = path7;
|
|
3650
|
+
const { arguments: args } = node;
|
|
3651
|
+
if (!args.length) return;
|
|
3652
|
+
const fn = args[0];
|
|
3653
|
+
if (!t31.isArrowFunctionExpression(fn) && !t31.isFunctionExpression(fn)) {
|
|
3654
|
+
return;
|
|
3655
|
+
}
|
|
3656
|
+
const fnPath = path7.get("arguments")[0];
|
|
3657
|
+
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
3658
|
+
args.push(deps);
|
|
3659
|
+
replaceCallName(node, adapter.target);
|
|
3660
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3392
3661
|
}
|
|
3393
3662
|
|
|
3394
3663
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-arrow-deps.ts
|
|
@@ -3462,8 +3731,78 @@ function isSkip(path7) {
|
|
|
3462
3731
|
return (isCallback() || isMethod() || !isVariableDecl()) && !isReturnFunc();
|
|
3463
3732
|
}
|
|
3464
3733
|
|
|
3465
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-
|
|
3734
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3466
3735
|
import * as t32 from "@babel/types";
|
|
3736
|
+
function resolveElementRef(ctx) {
|
|
3737
|
+
return {
|
|
3738
|
+
CallExpression(path7) {
|
|
3739
|
+
const {
|
|
3740
|
+
inputType,
|
|
3741
|
+
templateData: { refBindings }
|
|
3742
|
+
} = ctx;
|
|
3743
|
+
if (inputType !== "sfc") return;
|
|
3744
|
+
const { node } = path7;
|
|
3745
|
+
const isUseTemplateRef = isCalleeNamed(node, VUE_API_MAP.useTemplateRef);
|
|
3746
|
+
const isCompRefBindings = Object.keys(refBindings.componentRefs).length > 0 && isCalleeNamed(node, VUE_API_MAP.ref);
|
|
3747
|
+
const shouldProcess = isUseTemplateRef || isCompRefBindings;
|
|
3748
|
+
if (!shouldProcess) {
|
|
3749
|
+
return;
|
|
3750
|
+
}
|
|
3751
|
+
if (isCompRefBindings) {
|
|
3752
|
+
const varDeclaratorPath = getVariableDeclaratorPath(path7)?.node;
|
|
3753
|
+
if (!t32.isIdentifier(varDeclaratorPath?.id)) {
|
|
3754
|
+
return;
|
|
3755
|
+
}
|
|
3756
|
+
const varName = varDeclaratorPath.id.name;
|
|
3757
|
+
const compRef = refBindings.componentRefs[varName];
|
|
3758
|
+
if (!compRef) return;
|
|
3759
|
+
}
|
|
3760
|
+
node.arguments[0] = t32.identifier("null");
|
|
3761
|
+
resolveTypeParameters(ctx, path7);
|
|
3762
|
+
replaceCallName(node, REACT_API_MAP.useRef);
|
|
3763
|
+
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
3764
|
+
},
|
|
3765
|
+
MemberExpression(path7) {
|
|
3766
|
+
resolveRefValueToCurrent(path7);
|
|
3767
|
+
}
|
|
3768
|
+
};
|
|
3769
|
+
}
|
|
3770
|
+
function resolveTypeParameters(ctx, path7) {
|
|
3771
|
+
const {
|
|
3772
|
+
templateData: { refBindings },
|
|
3773
|
+
scriptData
|
|
3774
|
+
} = ctx;
|
|
3775
|
+
const { node } = path7;
|
|
3776
|
+
const varDeclaratorNode = getVariableDeclaratorPath(path7)?.node;
|
|
3777
|
+
if (!scriptData.lang.startsWith("ts") || !varDeclaratorNode) {
|
|
3778
|
+
return;
|
|
3779
|
+
}
|
|
3780
|
+
const idName = varDeclaratorNode.id.name;
|
|
3781
|
+
const domBindingMeta = refBindings.domRefs[idName];
|
|
3782
|
+
const compBindingMeta = refBindings.componentRefs[idName];
|
|
3783
|
+
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
3784
|
+
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
3785
|
+
node.typeParameters = t32.tsTypeParameterInstantiation([t32.tsTypeReference(t32.identifier(type))]);
|
|
3786
|
+
}
|
|
3787
|
+
}
|
|
3788
|
+
function resolveRefValueToCurrent(path7) {
|
|
3789
|
+
const { node } = path7;
|
|
3790
|
+
if (node.computed || !t32.isIdentifier(node.property) || node.property.name !== "value") {
|
|
3791
|
+
return;
|
|
3792
|
+
}
|
|
3793
|
+
const rootPath = findRootVariablePath(path7);
|
|
3794
|
+
if (!rootPath?.node || !t32.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
3795
|
+
return;
|
|
3796
|
+
}
|
|
3797
|
+
const rootId = findRootIdentifier(node);
|
|
3798
|
+
if (!t32.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
3799
|
+
return;
|
|
3800
|
+
}
|
|
3801
|
+
node.property.name = "current";
|
|
3802
|
+
}
|
|
3803
|
+
|
|
3804
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
3805
|
+
import * as t33 from "@babel/types";
|
|
3467
3806
|
function resolveExprMemo(ctx, ast) {
|
|
3468
3807
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
3469
3808
|
return {
|
|
@@ -3475,7 +3814,7 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3475
3814
|
if (!atComponentOrHookRoot(path7, ast.program, isScriptFile)) {
|
|
3476
3815
|
return false;
|
|
3477
3816
|
}
|
|
3478
|
-
if (
|
|
3817
|
+
if (t33.isCallExpression(init) && t33.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
3479
3818
|
return false;
|
|
3480
3819
|
}
|
|
3481
3820
|
return true;
|
|
@@ -3492,20 +3831,76 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3492
3831
|
};
|
|
3493
3832
|
}
|
|
3494
3833
|
|
|
3834
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
3835
|
+
import * as t34 from "@babel/types";
|
|
3836
|
+
function resolveLintRules(ctx, ast) {
|
|
3837
|
+
const inScriptFile = ctx.inputType !== "sfc";
|
|
3838
|
+
return {
|
|
3839
|
+
CallExpression(path7) {
|
|
3840
|
+
const { node, parentPath } = path7;
|
|
3841
|
+
if (!t34.isIdentifier(node.callee)) return;
|
|
3842
|
+
const { name: callName } = node.callee;
|
|
3843
|
+
const addLog = (t41) => {
|
|
3844
|
+
logger.error(t41, {
|
|
3845
|
+
file: ctx.filename,
|
|
3846
|
+
source: ctx.scriptData.source,
|
|
3847
|
+
loc: node.loc
|
|
3848
|
+
});
|
|
3849
|
+
};
|
|
3850
|
+
const lintMacros = () => {
|
|
3851
|
+
const macro = Object.values(MACRO_API_NAMES).find((v) => v === callName);
|
|
3852
|
+
if (!macro) return;
|
|
3853
|
+
if (inScriptFile) {
|
|
3854
|
+
addLog(
|
|
3855
|
+
`The ${macro} can only be used inside Vue SFC <script> blocks, not in separate script files.`
|
|
3856
|
+
);
|
|
3857
|
+
return;
|
|
3858
|
+
}
|
|
3859
|
+
if (!atComponentOrHookRoot(path7, ast.program)) {
|
|
3860
|
+
addLog(
|
|
3861
|
+
`The ${macro} must be defined at the top level of the component, not inside blocks or functions.`
|
|
3862
|
+
);
|
|
3863
|
+
return;
|
|
3864
|
+
}
|
|
3865
|
+
if (!parentPath.isVariableDeclarator()) {
|
|
3866
|
+
if (macro === MACRO_API_NAMES.props || macro === MACRO_API_NAMES.emits) {
|
|
3867
|
+
addLog(
|
|
3868
|
+
`The ${macro} macro must be assigned to a variable (e.g., const props = defineProps(...)).`
|
|
3869
|
+
);
|
|
3870
|
+
}
|
|
3871
|
+
}
|
|
3872
|
+
};
|
|
3873
|
+
const lintHooks = () => {
|
|
3874
|
+
if (!callName.startsWith("use")) return;
|
|
3875
|
+
if (!atComponentOrHookRoot(path7, ast.program, inScriptFile)) {
|
|
3876
|
+
addLog(
|
|
3877
|
+
`The ${callName} hook must be called at the top level, not inside loops, conditions, or nested functions.`
|
|
3878
|
+
);
|
|
3879
|
+
}
|
|
3880
|
+
};
|
|
3881
|
+
lintMacros();
|
|
3882
|
+
lintHooks();
|
|
3883
|
+
}
|
|
3884
|
+
};
|
|
3885
|
+
}
|
|
3886
|
+
|
|
3495
3887
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
3496
3888
|
import { generate as generate3 } from "@babel/generator";
|
|
3497
|
-
import * as
|
|
3889
|
+
import * as t35 from "@babel/types";
|
|
3498
3890
|
function resolveProvide(ctx) {
|
|
3499
|
-
if (ctx.inputType
|
|
3891
|
+
if (ctx.inputType === "style") return {};
|
|
3500
3892
|
return {
|
|
3501
3893
|
CallExpression(path7) {
|
|
3502
3894
|
const { node } = path7;
|
|
3503
|
-
|
|
3895
|
+
const providerTarget = ADAPTER_RULES.runtime[VUE_API_MAP.provide]?.target;
|
|
3896
|
+
const isProvideCall = isCalleeNamed(node, VUE_API_MAP.provide) || providerTarget && isCalleeNamed(node, providerTarget);
|
|
3897
|
+
if (!isProvideCall) return;
|
|
3504
3898
|
const { provide } = ctx.scriptData;
|
|
3505
3899
|
const [key, value] = node.arguments;
|
|
3506
3900
|
const target = findOrCreateCtxProvider(provide);
|
|
3901
|
+
const adapter = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
3507
3902
|
assignProviderValue(target, key, value);
|
|
3508
|
-
recordImport(ctx,
|
|
3903
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3509
3904
|
path7.parentPath.remove();
|
|
3510
3905
|
}
|
|
3511
3906
|
};
|
|
@@ -3523,13 +3918,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
3523
3918
|
function assignProviderValue(target, key, value) {
|
|
3524
3919
|
const getRawExp = (exp) => {
|
|
3525
3920
|
if (!exp) return "''";
|
|
3526
|
-
if (
|
|
3527
|
-
return
|
|
3921
|
+
if (t35.isStringLiteral(exp)) {
|
|
3922
|
+
return JSON.stringify(exp.value);
|
|
3528
3923
|
}
|
|
3529
|
-
if (
|
|
3924
|
+
if (t35.isNumericLiteral(exp)) {
|
|
3530
3925
|
return exp.value.toString();
|
|
3531
3926
|
}
|
|
3532
|
-
if (
|
|
3927
|
+
if (t35.isIdentifier(exp)) {
|
|
3533
3928
|
return exp.name;
|
|
3534
3929
|
}
|
|
3535
3930
|
try {
|
|
@@ -3544,165 +3939,71 @@ function assignProviderValue(target, key, value) {
|
|
|
3544
3939
|
target.provide = {};
|
|
3545
3940
|
}
|
|
3546
3941
|
|
|
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) {
|
|
3942
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
3943
|
+
import * as t36 from "@babel/types";
|
|
3944
|
+
function resolveRenameAdapter(ctx) {
|
|
3593
3945
|
return {
|
|
3594
|
-
"Identifier
|
|
3595
|
-
|
|
3596
|
-
|
|
3946
|
+
"CallExpression|Identifier"(path7) {
|
|
3947
|
+
const node = path7.node;
|
|
3948
|
+
const isCallNode = t36.isCallExpression(node);
|
|
3949
|
+
let apiName = "";
|
|
3950
|
+
if (t36.isIdentifier(node)) {
|
|
3951
|
+
apiName = node.name;
|
|
3952
|
+
} else if (isCallNode && t36.isIdentifier(node.callee)) {
|
|
3953
|
+
apiName = node.callee.name;
|
|
3954
|
+
}
|
|
3955
|
+
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
3956
|
+
const routerAdapter = ADAPTER_RULES.router[apiName];
|
|
3957
|
+
const adapter = runtimeAdapter || routerAdapter;
|
|
3958
|
+
if (!adapter || adapter.type !== "rename") {
|
|
3959
|
+
return;
|
|
3960
|
+
}
|
|
3961
|
+
if (adapter.isTrackable) {
|
|
3962
|
+
const reactiveType = getReactiveType(apiName);
|
|
3963
|
+
const declaratorPath = getVariableDeclaratorPath(path7);
|
|
3964
|
+
setScriptNodeMeta(declaratorPath?.node, {
|
|
3965
|
+
is_reactive: true,
|
|
3966
|
+
reactive_type: reactiveType
|
|
3967
|
+
});
|
|
3968
|
+
}
|
|
3969
|
+
if (isCallNode) {
|
|
3970
|
+
replaceCallName(node, adapter.target);
|
|
3971
|
+
} else {
|
|
3972
|
+
replaceIdName(node, adapter.target);
|
|
3973
|
+
}
|
|
3974
|
+
if (adapter.package === PACKAGE_NAME.router && !ctx.route) {
|
|
3975
|
+
ctx.route = true;
|
|
3976
|
+
}
|
|
3977
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
3597
3978
|
}
|
|
3598
3979
|
};
|
|
3599
3980
|
}
|
|
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
3981
|
|
|
3683
3982
|
// src/core/transform/sfc/script/syntax-processor/index.ts
|
|
3684
3983
|
function processVueSyntax2(ast, ctx) {
|
|
3685
3984
|
vueSyntaxProcessor2(ast, ctx, {
|
|
3686
3985
|
preprocess: {
|
|
3687
3986
|
applyBabel: [
|
|
3688
|
-
|
|
3987
|
+
resolvePropsIface,
|
|
3689
3988
|
resolveEmitsTopLevelTypes,
|
|
3690
3989
|
resolveSlotsTopLevelTypes,
|
|
3691
|
-
|
|
3692
|
-
|
|
3990
|
+
resolveDefineOptions,
|
|
3991
|
+
resolveDefineExpose,
|
|
3992
|
+
resolveDefineAsyncComponent,
|
|
3693
3993
|
resolveEmitCalls
|
|
3694
3994
|
]
|
|
3695
3995
|
},
|
|
3696
3996
|
process: {
|
|
3697
3997
|
applyBabel: [
|
|
3698
|
-
|
|
3998
|
+
resolveElementRef,
|
|
3999
|
+
// provide 需要在 rename 之前收集并移除原始调用,避免被重命名后失配
|
|
4000
|
+
resolveProvide,
|
|
4001
|
+
resolveRenameAdapter,
|
|
3699
4002
|
resolveArrowFnDeps,
|
|
3700
4003
|
resolveUnanalyzedArrow,
|
|
3701
|
-
|
|
3702
|
-
resolveTemplateRef,
|
|
3703
|
-
resolveProvide,
|
|
4004
|
+
resolveAnalysisOnlyAdapter,
|
|
3704
4005
|
resolveExprMemo,
|
|
3705
|
-
|
|
4006
|
+
resolveLintRules
|
|
3706
4007
|
],
|
|
3707
4008
|
excludeBabel: [resolveTemplateSlotIface, resolveCompIProps]
|
|
3708
4009
|
},
|
|
@@ -3800,11 +4101,11 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
3800
4101
|
}
|
|
3801
4102
|
|
|
3802
4103
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
3803
|
-
import * as
|
|
4104
|
+
import * as t39 from "@babel/types";
|
|
3804
4105
|
|
|
3805
4106
|
// src/shared/string-code-types.ts
|
|
3806
4107
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
3807
|
-
import * as
|
|
4108
|
+
import * as t37 from "@babel/types";
|
|
3808
4109
|
var strCodeTypes = {
|
|
3809
4110
|
isIdentifier: isIdentifier20,
|
|
3810
4111
|
isSimpleExpression,
|
|
@@ -3817,22 +4118,22 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
3817
4118
|
} catch {
|
|
3818
4119
|
return false;
|
|
3819
4120
|
}
|
|
3820
|
-
if (
|
|
4121
|
+
if (t37.isLiteral(node)) {
|
|
3821
4122
|
return true;
|
|
3822
4123
|
}
|
|
3823
|
-
if (!excludeVar &&
|
|
4124
|
+
if (!excludeVar && t37.isIdentifier(node)) {
|
|
3824
4125
|
return true;
|
|
3825
4126
|
}
|
|
3826
|
-
if (
|
|
3827
|
-
return isSimpleExpression(node.object) &&
|
|
4127
|
+
if (t37.isMemberExpression(node)) {
|
|
4128
|
+
return isSimpleExpression(node.object) && t37.isIdentifier(node.property);
|
|
3828
4129
|
}
|
|
3829
|
-
if (
|
|
4130
|
+
if (t37.isObjectExpression(node) || t37.isArrayExpression(node)) {
|
|
3830
4131
|
return false;
|
|
3831
4132
|
}
|
|
3832
|
-
if (
|
|
4133
|
+
if (t37.isCallExpression(node) || t37.isAssignmentExpression(node)) {
|
|
3833
4134
|
return false;
|
|
3834
4135
|
}
|
|
3835
|
-
if (
|
|
4136
|
+
if (t37.isBinaryExpression(node) || t37.isUnaryExpression(node)) {
|
|
3836
4137
|
return true;
|
|
3837
4138
|
}
|
|
3838
4139
|
return false;
|
|
@@ -3840,7 +4141,7 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
3840
4141
|
function isIdentifier20(code) {
|
|
3841
4142
|
try {
|
|
3842
4143
|
const node = parseExpression3(code);
|
|
3843
|
-
return
|
|
4144
|
+
return t37.isIdentifier(node);
|
|
3844
4145
|
} catch {
|
|
3845
4146
|
return false;
|
|
3846
4147
|
}
|
|
@@ -3848,14 +4149,14 @@ function isIdentifier20(code) {
|
|
|
3848
4149
|
function isStringLiteral12(code) {
|
|
3849
4150
|
try {
|
|
3850
4151
|
const node = parseExpression3(code);
|
|
3851
|
-
return
|
|
4152
|
+
return t37.isStringLiteral(node) || t37.isTemplateLiteral(node);
|
|
3852
4153
|
} catch {
|
|
3853
4154
|
return false;
|
|
3854
4155
|
}
|
|
3855
4156
|
}
|
|
3856
4157
|
|
|
3857
4158
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
3858
|
-
import * as
|
|
4159
|
+
import * as t38 from "@babel/types";
|
|
3859
4160
|
|
|
3860
4161
|
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
3861
4162
|
function resolveSpecialExpressions(input, ctx) {
|
|
@@ -3868,7 +4169,7 @@ function resolveEmitsCalls(input, ctx) {
|
|
|
3868
4169
|
if (!result) return input;
|
|
3869
4170
|
const [, , eventName, args] = result;
|
|
3870
4171
|
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
3871
|
-
const event = args ? `on${callee}(${args})` : `on${callee}()`;
|
|
4172
|
+
const event = args ? `on${callee}?.(${args})` : `on${callee}?.()`;
|
|
3872
4173
|
return `${ctx.propField}?.${event}`;
|
|
3873
4174
|
}
|
|
3874
4175
|
function matchEmitCalls(input, ctx) {
|
|
@@ -3901,13 +4202,13 @@ function resolveRefVariable(input, ctx) {
|
|
|
3901
4202
|
|
|
3902
4203
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
3903
4204
|
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
3904
|
-
if (toStrLiteral) return
|
|
4205
|
+
if (toStrLiteral) return t38.stringLiteral(input);
|
|
3905
4206
|
const { filename, scriptData } = ctx;
|
|
3906
4207
|
const newContent = resolveSpecialExpressions(input, ctx);
|
|
3907
4208
|
try {
|
|
3908
4209
|
return stringToExpr(newContent, scriptData.lang, filename);
|
|
3909
4210
|
} catch {
|
|
3910
|
-
return
|
|
4211
|
+
return t38.identifier(newContent);
|
|
3911
4212
|
}
|
|
3912
4213
|
}
|
|
3913
4214
|
|
|
@@ -4044,7 +4345,7 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4044
4345
|
const mergedItems = value.merge;
|
|
4045
4346
|
const setNameIdentifier = (target, valueName) => {
|
|
4046
4347
|
target.content = valueName;
|
|
4047
|
-
target.ast =
|
|
4348
|
+
target.ast = t39.jsxIdentifier(valueName);
|
|
4048
4349
|
};
|
|
4049
4350
|
const setValueExpression = (target, content, isStringLiteral13) => {
|
|
4050
4351
|
target.content = content;
|
|
@@ -4058,29 +4359,34 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4058
4359
|
if (setName && nameIdentifier) {
|
|
4059
4360
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
4060
4361
|
}
|
|
4362
|
+
const dir = ADAPTER_RULES.runtime.dir;
|
|
4363
|
+
recordImport(ctx, dir.package, dir.target);
|
|
4061
4364
|
setValueExpression(value.babelExp, expression, isStringLiteral13);
|
|
4062
|
-
recordImport(ctx, PACKAGE_NAME.runtime, ADAPTER_UTILS_MAP.dir);
|
|
4063
4365
|
};
|
|
4064
4366
|
if (ir.isKeyLessVBind) {
|
|
4065
|
-
const
|
|
4367
|
+
const dirKeyless = ADAPTER_RULES.runtime.dirKeyless;
|
|
4368
|
+
const expression = createRuntimeCall(dirKeyless.target, [valueContent]);
|
|
4066
4369
|
applyRuntimeExpression(expression, false);
|
|
4067
4370
|
return;
|
|
4068
4371
|
}
|
|
4069
4372
|
if (isClassAttr(name) && !value.isStringLiteral && !valueContent.startsWith(STYLE_MODULE_NAME)) {
|
|
4373
|
+
const dirCls = ADAPTER_RULES.runtime.dirCls;
|
|
4070
4374
|
const arg = mergedItems?.join(",") || wrapSingleQuotes(valueContent);
|
|
4071
|
-
const expression = createRuntimeCall(
|
|
4375
|
+
const expression = createRuntimeCall(dirCls.target, [arg]);
|
|
4072
4376
|
applyRuntimeExpression(expression, true, name);
|
|
4073
4377
|
return;
|
|
4074
4378
|
}
|
|
4075
4379
|
if (isStyleAttr(name) && (!isSimpleStyle(valueContent) || mergedItems?.some((item) => !isSimpleStyle(item)))) {
|
|
4380
|
+
const dirStyle = ADAPTER_RULES.runtime.dirStyle;
|
|
4076
4381
|
const arg = mergedItems?.join(",") || valueContent;
|
|
4077
|
-
const expression = createRuntimeCall(
|
|
4382
|
+
const expression = createRuntimeCall(dirStyle.target, [arg]);
|
|
4078
4383
|
applyRuntimeExpression(expression, true, name);
|
|
4079
4384
|
return;
|
|
4080
4385
|
}
|
|
4081
4386
|
if (ir.type === 3 /* EVENT */ && ir.modifiers?.length) {
|
|
4387
|
+
const dirOn = ADAPTER_RULES.runtime.dirOn;
|
|
4082
4388
|
const eventName = wrapSingleQuotes(ir.__vOnEvName || name, ir.isStatic);
|
|
4083
|
-
const expression = createRuntimeCall(
|
|
4389
|
+
const expression = createRuntimeCall(dirOn.target, [eventName, valueContent]);
|
|
4084
4390
|
applyRuntimeExpression(expression, true, name);
|
|
4085
4391
|
return;
|
|
4086
4392
|
}
|
|
@@ -4133,12 +4439,12 @@ function resolveElementChildrenRules(children, ctx, parentIR, ir) {
|
|
|
4133
4439
|
}
|
|
4134
4440
|
const nodeIR = child;
|
|
4135
4441
|
if (parentIR?.isBuiltIn) {
|
|
4136
|
-
if (parentIR.tag ==
|
|
4442
|
+
if (parentIR.tag == VUE_API_MAP.Transition) {
|
|
4137
4443
|
resolveTransitionRules(nodeIR, parentIR, ir, ctx);
|
|
4138
4444
|
}
|
|
4139
4445
|
}
|
|
4140
4446
|
if (nodeIR.isRoute) {
|
|
4141
|
-
if (nodeIR.tag ===
|
|
4447
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
4142
4448
|
resolveRouterLinkRules(nodeIR, ctx);
|
|
4143
4449
|
}
|
|
4144
4450
|
}
|
|
@@ -4184,6 +4490,8 @@ function resolveDefaultStyleModuleName(node) {
|
|
|
4184
4490
|
// src/core/transform/sfc/template/syntax-processor/preprocess/resolve-style-scope-attribute.ts
|
|
4185
4491
|
import {
|
|
4186
4492
|
ElementTypes,
|
|
4493
|
+
isSlotOutlet,
|
|
4494
|
+
isTemplateNode,
|
|
4187
4495
|
NodeTypes as NodeTypes3
|
|
4188
4496
|
} from "@vue/compiler-core";
|
|
4189
4497
|
function resolveStyleScopeAttribute(node, _ir, ctx) {
|
|
@@ -4205,7 +4513,7 @@ function walkElementNodes2(node, onElement) {
|
|
|
4205
4513
|
}
|
|
4206
4514
|
function injectStyleScopeAttribute(node, ctx) {
|
|
4207
4515
|
const { scopeId } = ctx.styleData;
|
|
4208
|
-
if (!scopeId || isComponentElement(node)) {
|
|
4516
|
+
if (!scopeId || isComponentElement(node) || isSlotOutlet(node) || isTemplateNode(node)) {
|
|
4209
4517
|
return;
|
|
4210
4518
|
}
|
|
4211
4519
|
const hasDynamicIs = node.props.some((prop) => {
|
|
@@ -4345,7 +4653,7 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4345
4653
|
}
|
|
4346
4654
|
const propIR = createPropsIR("is", "is", content);
|
|
4347
4655
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4348
|
-
nodeIR.tag =
|
|
4656
|
+
nodeIR.tag = VUE_API_MAP.DynamicComponent;
|
|
4349
4657
|
nodeIR.isComponent = true;
|
|
4350
4658
|
nodeIR.props.push(propIR);
|
|
4351
4659
|
recordImport(ctx, PACKAGE_NAME.runtime, nodeIR.tag);
|
|
@@ -4354,15 +4662,20 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4354
4662
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-ref-prop.ts
|
|
4355
4663
|
import { NodeTypes as NodeTypes5 } from "@vue/compiler-core";
|
|
4356
4664
|
function resolveRefProp(node, ctx, nodeIR) {
|
|
4357
|
-
const {
|
|
4665
|
+
const {
|
|
4666
|
+
templateData: { refBindings }
|
|
4667
|
+
} = ctx;
|
|
4358
4668
|
let propIR;
|
|
4359
4669
|
if (node.type === NodeTypes5.ATTRIBUTE) {
|
|
4360
|
-
const
|
|
4361
|
-
|
|
4362
|
-
|
|
4670
|
+
const tag = node.value?.content;
|
|
4671
|
+
if (!tag) return;
|
|
4672
|
+
collectComponentRef(tag, ctx);
|
|
4673
|
+
const domRefBinding = Object.values(refBindings.domRefs).find((r) => r.tag === tag);
|
|
4674
|
+
const refVar = domRefBinding?.name || refBindings.componentRefs[tag]?.name;
|
|
4675
|
+
propIR = createPropsIR("ref", "ref", refVar || "null");
|
|
4363
4676
|
} else {
|
|
4364
4677
|
const exp = node.exp;
|
|
4365
|
-
for (const name in
|
|
4678
|
+
for (const name in refBindings.domRefs) {
|
|
4366
4679
|
const newName = `${name}.current`;
|
|
4367
4680
|
const regex = new RegExp(`${name}(?!\\.current)`, "g");
|
|
4368
4681
|
exp.content = exp.content.replace(regex, newName);
|
|
@@ -4372,6 +4685,22 @@ function resolveRefProp(node, ctx, nodeIR) {
|
|
|
4372
4685
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4373
4686
|
nodeIR.props.push(propIR);
|
|
4374
4687
|
}
|
|
4688
|
+
function collectComponentRef(tag, ctx) {
|
|
4689
|
+
if (tag in HTML_TAG_TYPES) {
|
|
4690
|
+
return;
|
|
4691
|
+
}
|
|
4692
|
+
const { refBindings, reactiveBindings } = ctx.templateData;
|
|
4693
|
+
refBindings.componentRefs[tag] = {
|
|
4694
|
+
tag,
|
|
4695
|
+
// 对应的标签名(随便填,不影响)
|
|
4696
|
+
htmlType: "",
|
|
4697
|
+
name: tag
|
|
4698
|
+
// 对应的变量名(随便填,不影响)
|
|
4699
|
+
};
|
|
4700
|
+
if (reactiveBindings[tag]) {
|
|
4701
|
+
delete reactiveBindings[tag];
|
|
4702
|
+
}
|
|
4703
|
+
}
|
|
4375
4704
|
|
|
4376
4705
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-dynamic-attribute-prop.ts
|
|
4377
4706
|
function resolveDynamicAttributeProp(node, ir, ctx, nodeIR) {
|
|
@@ -4567,9 +4896,8 @@ function resolveVModel(node, _ir, ctx, elementNode, nodeIR) {
|
|
|
4567
4896
|
let eventPropIR;
|
|
4568
4897
|
if (isComponent) {
|
|
4569
4898
|
valuePropIR = createPropsIR("v-model", propName, getterName);
|
|
4570
|
-
const
|
|
4571
|
-
const
|
|
4572
|
-
const eventVueName = `update:${getterNamespace}`;
|
|
4899
|
+
const eventReactName = `onUpdate${capitalize(camelCase(propName))}`;
|
|
4900
|
+
const eventVueName = `update:${propName}`;
|
|
4573
4901
|
const isTS = ctx.scriptData?.lang?.startsWith("ts");
|
|
4574
4902
|
const valueArg = isTS ? "value: any" : "value";
|
|
4575
4903
|
const processedValue = applyValueModifiers("value", modifiers);
|
|
@@ -4630,15 +4958,9 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
4630
4958
|
}
|
|
4631
4959
|
return result;
|
|
4632
4960
|
}
|
|
4633
|
-
function getRootIdName(expr) {
|
|
4634
|
-
if (typeof expr !== "string") return;
|
|
4635
|
-
const pattern = /^([a-zA-Z_$][a-zA-Z0-9_$]*)(?:[\.\?\.\[\(].*)?$/;
|
|
4636
|
-
const match = pattern.exec(expr.trim());
|
|
4637
|
-
return match?.[1];
|
|
4638
|
-
}
|
|
4639
4961
|
|
|
4640
4962
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
4641
|
-
import * as
|
|
4963
|
+
import * as t40 from "@babel/types";
|
|
4642
4964
|
function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
4643
4965
|
const arg = node.arg;
|
|
4644
4966
|
const exp = node.exp;
|
|
@@ -4655,7 +4977,7 @@ function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
|
4655
4977
|
originalVueEventName = `${arg.content}.${modifiers.join(".")}`;
|
|
4656
4978
|
} else {
|
|
4657
4979
|
const expr = stringToExpr(handler);
|
|
4658
|
-
if (!
|
|
4980
|
+
if (!t40.isFunctionExpression(expr) && !t40.isIdentifier(expr)) {
|
|
4659
4981
|
handler = `() => {${handler}}`;
|
|
4660
4982
|
}
|
|
4661
4983
|
}
|
|
@@ -4782,7 +5104,7 @@ function resolveDirectiveProp(node, ir, ctx, elementNode, nodeIR, siblingNodesIR
|
|
|
4782
5104
|
return resolveVOn(node, ir, ctx, nodeIR);
|
|
4783
5105
|
}
|
|
4784
5106
|
if (isVSlot(rawName2)) {
|
|
4785
|
-
if (nodeIR.tag ===
|
|
5107
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
4786
5108
|
resolveRouterLinkVSlotProp(node, nodeIR, ctx);
|
|
4787
5109
|
}
|
|
4788
5110
|
return true;
|
|
@@ -4856,14 +5178,16 @@ function resolveElementNode(node, ir, ctx, siblingNodesIR) {
|
|
|
4856
5178
|
isSelfClosing: node.isSelfClosing,
|
|
4857
5179
|
loc: node.loc
|
|
4858
5180
|
});
|
|
4859
|
-
|
|
5181
|
+
const routerAdapter = ADAPTER_RULES.router[tag];
|
|
5182
|
+
const runtimeAdapter = ADAPTER_RULES.runtime[tag];
|
|
5183
|
+
if (runtimeAdapter) {
|
|
4860
5184
|
nodeIR.isBuiltIn = true;
|
|
4861
|
-
recordImport(ctx,
|
|
5185
|
+
recordImport(ctx, runtimeAdapter.package, runtimeAdapter.target);
|
|
4862
5186
|
}
|
|
4863
|
-
if (
|
|
5187
|
+
if (routerAdapter) {
|
|
4864
5188
|
if (!ctx.route) ctx.route = true;
|
|
4865
5189
|
nodeIR.isRoute = true;
|
|
4866
|
-
recordImport(ctx,
|
|
5190
|
+
recordImport(ctx, routerAdapter?.package, routerAdapter.target);
|
|
4867
5191
|
}
|
|
4868
5192
|
resolveProps(node, ir, ctx, nodeIR, siblingNodesIR);
|
|
4869
5193
|
return nodeIR;
|
|
@@ -4899,7 +5223,7 @@ import { NodeTypes as NodeTypes9 } from "@vue/compiler-core";
|
|
|
4899
5223
|
|
|
4900
5224
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
4901
5225
|
import {
|
|
4902
|
-
isSlotOutlet,
|
|
5226
|
+
isSlotOutlet as isSlotOutlet2,
|
|
4903
5227
|
NodeTypes as VueNodeTypes
|
|
4904
5228
|
} from "@vue/compiler-core";
|
|
4905
5229
|
|
|
@@ -4935,7 +5259,7 @@ function resolveTemplateChildren(node, nodeIR, ctx) {
|
|
|
4935
5259
|
function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
4936
5260
|
for (const child of node.children) {
|
|
4937
5261
|
if (child.type === VueNodeTypes.ELEMENT) {
|
|
4938
|
-
if (
|
|
5262
|
+
if (isSlotOutlet2(child)) {
|
|
4939
5263
|
resolveSlotOutletNode(child, nodeIR, ctx, parentIR, childrenIR);
|
|
4940
5264
|
continue;
|
|
4941
5265
|
}
|
|
@@ -5101,7 +5425,7 @@ function transform(ast, ctx, options) {
|
|
|
5101
5425
|
}
|
|
5102
5426
|
|
|
5103
5427
|
// package.json
|
|
5104
|
-
var version = "1.
|
|
5428
|
+
var version = "1.2.1";
|
|
5105
5429
|
var bin = {
|
|
5106
5430
|
vureact: "./bin/vureact.js"
|
|
5107
5431
|
};
|
|
@@ -5556,7 +5880,10 @@ var CompilationContext = class {
|
|
|
5556
5880
|
propField: "$props",
|
|
5557
5881
|
templateData: {
|
|
5558
5882
|
slots: {},
|
|
5559
|
-
refBindings: {
|
|
5883
|
+
refBindings: {
|
|
5884
|
+
domRefs: {},
|
|
5885
|
+
componentRefs: {}
|
|
5886
|
+
},
|
|
5560
5887
|
reactiveBindings: {}
|
|
5561
5888
|
},
|
|
5562
5889
|
scriptData: {
|
|
@@ -5573,6 +5900,10 @@ var CompilationContext = class {
|
|
|
5573
5900
|
emitTypes: [],
|
|
5574
5901
|
slotTypes: []
|
|
5575
5902
|
},
|
|
5903
|
+
forwardRef: {
|
|
5904
|
+
enabled: false,
|
|
5905
|
+
refField: "expose"
|
|
5906
|
+
},
|
|
5576
5907
|
source: ""
|
|
5577
5908
|
},
|
|
5578
5909
|
styleData: {
|