@vureact/compiler-core 1.8.3 → 1.8.5
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 +65 -61
- package/README.zh-CN.md +193 -0
- package/lib/{chunk-R34TQK4X.js → chunk-6FMNT76F.js} +496 -411
- package/lib/{chunk-QPW3RUZH.esm.js → chunk-FWYPSB43.esm.js} +447 -362
- package/lib/cli.esm.js +2 -2
- package/lib/cli.js +10 -10
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +2 -2
- package/README.en.md +0 -192
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vureact/compiler-core v1.8.
|
|
2
|
+
* @vureact/compiler-core v1.8.5
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -39,7 +39,7 @@ var RUNTIME_PACKAGES = {
|
|
|
39
39
|
},
|
|
40
40
|
runtime: {
|
|
41
41
|
name: PACKAGE_NAME.runtime,
|
|
42
|
-
version: "^1.1.
|
|
42
|
+
version: "^1.1.1"
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
45
|
var STYLE_MODULE_NAME = "$style";
|
|
@@ -311,7 +311,8 @@ var ADAPTER_RULES = {
|
|
|
311
311
|
type: "rename"
|
|
312
312
|
},
|
|
313
313
|
dirOn: {
|
|
314
|
-
|
|
314
|
+
// fix: https://github.com/vureact-js/core/issues/49
|
|
315
|
+
target: "dir.on",
|
|
315
316
|
package: PACKAGE_NAME.runtime,
|
|
316
317
|
type: "rename"
|
|
317
318
|
},
|
|
@@ -568,7 +569,8 @@ function buildJsxChildrenProcessor(nodeIR, ctx, state) {
|
|
|
568
569
|
}
|
|
569
570
|
function buildJsxChildren(children, ctx) {
|
|
570
571
|
const result = [];
|
|
571
|
-
|
|
572
|
+
const normalizedChildren = Array.isArray(children) ? children : [];
|
|
573
|
+
for (const childIR of normalizedChildren) {
|
|
572
574
|
const jsxNode = buildJsxNode(childIR, ctx);
|
|
573
575
|
if (!jsxNode) continue;
|
|
574
576
|
result.push(jsxNode);
|
|
@@ -1178,26 +1180,38 @@ function buildCtxProviderNode(nodeIR, ctx, children) {
|
|
|
1178
1180
|
|
|
1179
1181
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-root-jsx.ts
|
|
1180
1182
|
function buildRootJsxProcessor(nodeIR, ctx, state) {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
+
const { rootChildren } = state;
|
|
1184
|
+
const { provide } = ctx.scriptData;
|
|
1185
|
+
const hasProvide = provide.isOccupied;
|
|
1186
|
+
const hasChildren = rootChildren.length > 0;
|
|
1187
|
+
const setResult = (elem) => {
|
|
1188
|
+
state.result = elem ?? null;
|
|
1189
|
+
};
|
|
1190
|
+
if (hasProvide) {
|
|
1191
|
+
const provider = buildCtxProviderNode(provide, ctx, rootChildren);
|
|
1192
|
+
if (!hasChildren) {
|
|
1193
|
+
setResult(buildFragmentNode([provider]));
|
|
1194
|
+
} else {
|
|
1195
|
+
setResult(provider);
|
|
1196
|
+
}
|
|
1183
1197
|
return;
|
|
1184
1198
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
state.result = buildCtxProviderNode(provide, ctx, state.rootChildren);
|
|
1199
|
+
if (!hasChildren) {
|
|
1200
|
+
setResult();
|
|
1188
1201
|
return;
|
|
1189
1202
|
}
|
|
1190
|
-
|
|
1203
|
+
setResult(buildFragmentNode(rootChildren));
|
|
1191
1204
|
void nodeIR;
|
|
1192
1205
|
}
|
|
1193
1206
|
|
|
1194
1207
|
// src/core/codegen/component/jsx/syntax-processor/index.ts
|
|
1195
1208
|
function buildJSXChild(nodeIR, ctx) {
|
|
1209
|
+
const templateIR = nodeIR || { children: [] };
|
|
1196
1210
|
const state = {
|
|
1197
1211
|
rootChildren: [],
|
|
1198
1212
|
result: null
|
|
1199
1213
|
};
|
|
1200
|
-
jsxBuilder(
|
|
1214
|
+
jsxBuilder(templateIR, ctx, state, {
|
|
1201
1215
|
preprocess: [],
|
|
1202
1216
|
process: [buildJsxChildrenProcessor],
|
|
1203
1217
|
postprocess: [buildRootJsxProcessor]
|
|
@@ -1215,7 +1229,7 @@ function jsxBuilder(nodeIR, ctx, state, options) {
|
|
|
1215
1229
|
|
|
1216
1230
|
// src/core/codegen/component/jsx/index.ts
|
|
1217
1231
|
function buildJSX(nodeIR, ctx) {
|
|
1218
|
-
if (!nodeIR?.children.length) {
|
|
1232
|
+
if (!nodeIR?.children.length && ctx.inputType !== "sfc") {
|
|
1219
1233
|
return null;
|
|
1220
1234
|
}
|
|
1221
1235
|
return buildJSXChild(nodeIR, ctx);
|
|
@@ -3220,8 +3234,51 @@ function resolveSfcCssImport(ctx) {
|
|
|
3220
3234
|
scriptIR.imports.push(importDecl);
|
|
3221
3235
|
}
|
|
3222
3236
|
|
|
3223
|
-
// src/core/transform/sfc/script/syntax-processor/
|
|
3237
|
+
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-vue-type-as-any.ts
|
|
3224
3238
|
import * as t25 from "@babel/types";
|
|
3239
|
+
function resolveVueTypeAsAny(ctx) {
|
|
3240
|
+
return {
|
|
3241
|
+
TSTypeReference(path8) {
|
|
3242
|
+
if (isVueTypeRef(path8)) {
|
|
3243
|
+
replaceNode(path8, t25.tsAnyKeyword(), path8.node);
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
};
|
|
3247
|
+
}
|
|
3248
|
+
function isVueTypeRef(path8) {
|
|
3249
|
+
const id = resolveTypeNameId(path8.node.typeName);
|
|
3250
|
+
return id !== null && isVueImport(id, path8);
|
|
3251
|
+
}
|
|
3252
|
+
function resolveTypeNameId(typeName) {
|
|
3253
|
+
if (t25.isIdentifier(typeName)) {
|
|
3254
|
+
return typeName;
|
|
3255
|
+
}
|
|
3256
|
+
if (t25.isTSQualifiedName(typeName)) {
|
|
3257
|
+
let node = typeName;
|
|
3258
|
+
while (t25.isTSQualifiedName(node)) {
|
|
3259
|
+
node = node.left;
|
|
3260
|
+
}
|
|
3261
|
+
return t25.isIdentifier(node) ? node : null;
|
|
3262
|
+
}
|
|
3263
|
+
return null;
|
|
3264
|
+
}
|
|
3265
|
+
function isVueImport(id, path8) {
|
|
3266
|
+
const binding = path8.scope.getBinding(id.name);
|
|
3267
|
+
if (!binding) return false;
|
|
3268
|
+
const bp = binding.path;
|
|
3269
|
+
if (!bp.isImportSpecifier() && !bp.isImportDefaultSpecifier()) {
|
|
3270
|
+
return false;
|
|
3271
|
+
}
|
|
3272
|
+
const importDecl = bp.parentPath;
|
|
3273
|
+
if (!importDecl.isImportDeclaration()) {
|
|
3274
|
+
return false;
|
|
3275
|
+
}
|
|
3276
|
+
const moduleName = importDecl.node.source.value;
|
|
3277
|
+
return VUE_PACKAGES.some((pkg) => moduleName.includes(pkg));
|
|
3278
|
+
}
|
|
3279
|
+
|
|
3280
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
3281
|
+
import * as t26 from "@babel/types";
|
|
3225
3282
|
function resolveDefineAsyncComponent(ctx) {
|
|
3226
3283
|
return {
|
|
3227
3284
|
CallExpression(path8) {
|
|
@@ -3230,11 +3287,11 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
3230
3287
|
return;
|
|
3231
3288
|
}
|
|
3232
3289
|
const [arg] = node.arguments;
|
|
3233
|
-
if (!
|
|
3290
|
+
if (!t26.isObjectExpression(arg)) {
|
|
3234
3291
|
return;
|
|
3235
3292
|
}
|
|
3236
3293
|
for (const prop of arg.properties) {
|
|
3237
|
-
if (!
|
|
3294
|
+
if (!t26.isObjectProperty(prop) || !t26.isIdentifier(prop.key) || prop.key.name !== "hydrate") {
|
|
3238
3295
|
continue;
|
|
3239
3296
|
}
|
|
3240
3297
|
logger.warn('Unsupported option "hydrate"', {
|
|
@@ -3249,24 +3306,24 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
3249
3306
|
}
|
|
3250
3307
|
|
|
3251
3308
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
3252
|
-
import * as
|
|
3309
|
+
import * as t28 from "@babel/types";
|
|
3253
3310
|
|
|
3254
3311
|
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
3255
|
-
import * as
|
|
3312
|
+
import * as t27 from "@babel/types";
|
|
3256
3313
|
function createUseCallback(body, deps) {
|
|
3257
|
-
return
|
|
3314
|
+
return t27.callExpression(t27.identifier(REACT_API_MAP.useCallback), [
|
|
3258
3315
|
body,
|
|
3259
|
-
deps ??
|
|
3316
|
+
deps ?? t27.arrayExpression([])
|
|
3260
3317
|
]);
|
|
3261
3318
|
}
|
|
3262
3319
|
function createUseMemo(body, deps) {
|
|
3263
|
-
return
|
|
3264
|
-
|
|
3265
|
-
deps ??
|
|
3320
|
+
return t27.callExpression(t27.identifier(REACT_API_MAP.useMemo), [
|
|
3321
|
+
t27.arrowFunctionExpression([], body),
|
|
3322
|
+
deps ?? t27.arrayExpression([])
|
|
3266
3323
|
]);
|
|
3267
3324
|
}
|
|
3268
3325
|
function createUseImperativeHandle(refId, init) {
|
|
3269
|
-
return
|
|
3326
|
+
return t27.callExpression(t27.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
3270
3327
|
}
|
|
3271
3328
|
|
|
3272
3329
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
@@ -3287,16 +3344,16 @@ function resolveDefineExpose(ctx) {
|
|
|
3287
3344
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
3288
3345
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
3289
3346
|
recordImport(ctx, adapter.package, adapter.target);
|
|
3290
|
-
if (!
|
|
3347
|
+
if (!t28.isObjectExpression(expose) && !t28.isFunction(expose)) {
|
|
3291
3348
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
3292
3349
|
file: filename,
|
|
3293
3350
|
loc: expose.loc,
|
|
3294
3351
|
source: scriptData.source
|
|
3295
3352
|
});
|
|
3296
3353
|
}
|
|
3297
|
-
const init = !
|
|
3354
|
+
const init = !t28.isFunction(expose) ? t28.arrowFunctionExpression([], expose) : expose;
|
|
3298
3355
|
const { forwardRef } = scriptData;
|
|
3299
|
-
const newNode = createUseImperativeHandle(
|
|
3356
|
+
const newNode = createUseImperativeHandle(t28.identifier(forwardRef.refField), init);
|
|
3300
3357
|
forwardRef.enabled = true;
|
|
3301
3358
|
replaceNode(path8, newNode, node);
|
|
3302
3359
|
}
|
|
@@ -3317,7 +3374,7 @@ function resolveDefineOptions(ctx) {
|
|
|
3317
3374
|
}
|
|
3318
3375
|
|
|
3319
3376
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
3320
|
-
import * as
|
|
3377
|
+
import * as t29 from "@babel/types";
|
|
3321
3378
|
function resolveEmitCalls(ctx) {
|
|
3322
3379
|
const formatEmitEventName = (raw) => {
|
|
3323
3380
|
if (raw.startsWith("update:")) {
|
|
@@ -3331,7 +3388,7 @@ function resolveEmitCalls(ctx) {
|
|
|
3331
3388
|
CallExpression(path8) {
|
|
3332
3389
|
const { node } = path8;
|
|
3333
3390
|
const { filename, templateData, scriptData } = ctx;
|
|
3334
|
-
if (!
|
|
3391
|
+
if (!t29.isIdentifier(node.callee)) return;
|
|
3335
3392
|
const { name } = node.callee;
|
|
3336
3393
|
const checkIfFromDefineEmits = () => {
|
|
3337
3394
|
let result = false;
|
|
@@ -3343,7 +3400,7 @@ function resolveEmitCalls(ctx) {
|
|
|
3343
3400
|
const binding = path8.scope.getBinding(name);
|
|
3344
3401
|
if (binding) {
|
|
3345
3402
|
const parent = binding.path.node;
|
|
3346
|
-
if (
|
|
3403
|
+
if (t29.isVariableDeclarator(parent) && t29.isCallExpression(parent.init) && t29.isIdentifier(parent.init.callee)) {
|
|
3347
3404
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
3348
3405
|
}
|
|
3349
3406
|
}
|
|
@@ -3353,9 +3410,9 @@ function resolveEmitCalls(ctx) {
|
|
|
3353
3410
|
if (!checkIfFromDefineEmits()) return;
|
|
3354
3411
|
const [callee, ...args] = node.arguments;
|
|
3355
3412
|
let propCall;
|
|
3356
|
-
if (
|
|
3413
|
+
if (t29.isStringLiteral(callee)) {
|
|
3357
3414
|
const eventName = formatEmitEventName(callee.value);
|
|
3358
|
-
propCall = createPropCall(ctx.propField,
|
|
3415
|
+
propCall = createPropCall(ctx.propField, t29.identifier(eventName), args);
|
|
3359
3416
|
} else {
|
|
3360
3417
|
propCall = createPropCall(ctx.propField, callee, args, true);
|
|
3361
3418
|
logger.error(
|
|
@@ -3372,38 +3429,38 @@ function resolveEmitCalls(ctx) {
|
|
|
3372
3429
|
};
|
|
3373
3430
|
}
|
|
3374
3431
|
function createPropCall(rootName, callee, args, computed = false) {
|
|
3375
|
-
return
|
|
3376
|
-
|
|
3432
|
+
return t29.optionalCallExpression(
|
|
3433
|
+
t29.memberExpression(t29.identifier(rootName), callee, computed),
|
|
3377
3434
|
args,
|
|
3378
3435
|
true
|
|
3379
3436
|
);
|
|
3380
3437
|
}
|
|
3381
3438
|
|
|
3382
3439
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
3383
|
-
import * as
|
|
3440
|
+
import * as t37 from "@babel/types";
|
|
3384
3441
|
|
|
3385
3442
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
3386
|
-
import * as
|
|
3443
|
+
import * as t31 from "@babel/types";
|
|
3387
3444
|
|
|
3388
3445
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
3389
|
-
import * as
|
|
3446
|
+
import * as t30 from "@babel/types";
|
|
3390
3447
|
function cloneCallableParams(params) {
|
|
3391
3448
|
const cloneCallableParam = (param, index) => {
|
|
3392
|
-
if (
|
|
3449
|
+
if (t30.isRestElement(param)) {
|
|
3393
3450
|
const arg = param.argument;
|
|
3394
|
-
const name =
|
|
3395
|
-
const rest =
|
|
3396
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
3451
|
+
const name = t30.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
3452
|
+
const rest = t30.restElement(t30.identifier(name));
|
|
3453
|
+
rest.typeAnnotation = param.typeAnnotation || (t30.isIdentifier(arg) ? arg.typeAnnotation : null) || t30.tsTypeAnnotation(t30.tsArrayType(t30.tsAnyKeyword()));
|
|
3397
3454
|
return rest;
|
|
3398
3455
|
}
|
|
3399
|
-
if (
|
|
3400
|
-
const id =
|
|
3456
|
+
if (t30.isIdentifier(param)) {
|
|
3457
|
+
const id = t30.identifier(param.name || `arg${index}`);
|
|
3401
3458
|
id.optional = param.optional;
|
|
3402
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
3459
|
+
id.typeAnnotation = param.typeAnnotation || t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
3403
3460
|
return id;
|
|
3404
3461
|
}
|
|
3405
|
-
const fallback =
|
|
3406
|
-
fallback.typeAnnotation =
|
|
3462
|
+
const fallback = t30.identifier(`arg${index}`);
|
|
3463
|
+
fallback.typeAnnotation = t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
3407
3464
|
return fallback;
|
|
3408
3465
|
};
|
|
3409
3466
|
return params.map(cloneCallableParam);
|
|
@@ -3413,18 +3470,18 @@ function cloneCallableParams(params) {
|
|
|
3413
3470
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
3414
3471
|
return {
|
|
3415
3472
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
3416
|
-
if (!
|
|
3473
|
+
if (!t31.isProgram(path8.parent)) return;
|
|
3417
3474
|
const { node } = path8;
|
|
3418
|
-
if (
|
|
3419
|
-
const typeLiteral =
|
|
3475
|
+
if (t31.isTSInterfaceDeclaration(node)) {
|
|
3476
|
+
const typeLiteral = t31.tsTypeLiteral(node.body.body);
|
|
3420
3477
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
3421
3478
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
3422
|
-
if (resolved &&
|
|
3479
|
+
if (resolved && t31.isTSTypeLiteral(resolved)) {
|
|
3423
3480
|
node.body.body = resolved.members;
|
|
3424
3481
|
}
|
|
3425
3482
|
return;
|
|
3426
3483
|
}
|
|
3427
|
-
if (
|
|
3484
|
+
if (t31.isTSTypeAliasDeclaration(node)) {
|
|
3428
3485
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
3429
3486
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
3430
3487
|
if (resolved) {
|
|
@@ -3435,47 +3492,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
3435
3492
|
};
|
|
3436
3493
|
}
|
|
3437
3494
|
function resolveTopLevelEmitType(tsType) {
|
|
3438
|
-
if (
|
|
3495
|
+
if (t31.isTSParenthesizedType(tsType)) {
|
|
3439
3496
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
3440
3497
|
}
|
|
3441
|
-
if (
|
|
3498
|
+
if (t31.isTSTypeReference(tsType)) {
|
|
3442
3499
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3443
3500
|
return tsType;
|
|
3444
3501
|
}
|
|
3445
3502
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
3446
|
-
return
|
|
3503
|
+
return t31.tsTypeReference(
|
|
3447
3504
|
tsType.typeName,
|
|
3448
|
-
|
|
3505
|
+
t31.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3449
3506
|
);
|
|
3450
3507
|
}
|
|
3451
|
-
if (
|
|
3508
|
+
if (t31.isTSIntersectionType(tsType)) {
|
|
3452
3509
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
3453
3510
|
if (!types.length) return null;
|
|
3454
3511
|
if (types.length === 1) return types[0];
|
|
3455
|
-
return
|
|
3512
|
+
return t31.tsIntersectionType(types);
|
|
3456
3513
|
}
|
|
3457
|
-
if (
|
|
3514
|
+
if (t31.isTSUnionType(tsType)) {
|
|
3458
3515
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
3459
3516
|
if (!types.length) return null;
|
|
3460
3517
|
if (types.length === 1) return types[0];
|
|
3461
|
-
return
|
|
3518
|
+
return t31.tsUnionType(types);
|
|
3462
3519
|
}
|
|
3463
|
-
if (
|
|
3520
|
+
if (t31.isTSTypeLiteral(tsType)) {
|
|
3464
3521
|
const members = [];
|
|
3465
3522
|
for (const member of tsType.members) {
|
|
3466
|
-
if (
|
|
3523
|
+
if (t31.isTSCallSignatureDeclaration(member)) {
|
|
3467
3524
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
3468
3525
|
continue;
|
|
3469
3526
|
}
|
|
3470
3527
|
members.push(member);
|
|
3471
3528
|
}
|
|
3472
3529
|
if (!members.length) return null;
|
|
3473
|
-
return
|
|
3530
|
+
return t31.tsTypeLiteral(members);
|
|
3474
3531
|
}
|
|
3475
|
-
if (
|
|
3532
|
+
if (t31.isTSFunctionType(tsType)) {
|
|
3476
3533
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
3477
3534
|
if (!props.length) return null;
|
|
3478
|
-
return
|
|
3535
|
+
return t31.tsTypeLiteral(props);
|
|
3479
3536
|
}
|
|
3480
3537
|
return tsType;
|
|
3481
3538
|
}
|
|
@@ -3496,41 +3553,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
3496
3553
|
propsTSIface: { emitTypes }
|
|
3497
3554
|
} = ctx.scriptData;
|
|
3498
3555
|
const members = [];
|
|
3499
|
-
if (
|
|
3556
|
+
if (t31.isArrayExpression(runtimeArg)) {
|
|
3500
3557
|
for (const element of runtimeArg.elements) {
|
|
3501
|
-
if (!element || !
|
|
3558
|
+
if (!element || !t31.isStringLiteral(element)) continue;
|
|
3502
3559
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
3503
3560
|
const key = buildKey(handlerName);
|
|
3504
|
-
const fnType =
|
|
3561
|
+
const fnType = t31.tsFunctionType(
|
|
3505
3562
|
null,
|
|
3506
3563
|
[createRestAnyParam("args")],
|
|
3507
|
-
|
|
3564
|
+
t31.tsTypeAnnotation(t31.tsAnyKeyword())
|
|
3508
3565
|
);
|
|
3509
|
-
const prop =
|
|
3566
|
+
const prop = t31.tsPropertySignature(key, t31.tsTypeAnnotation(fnType));
|
|
3510
3567
|
prop.optional = true;
|
|
3511
3568
|
members.push(prop);
|
|
3512
3569
|
}
|
|
3513
3570
|
if (members.length) {
|
|
3514
|
-
emitTypes.push(
|
|
3571
|
+
emitTypes.push(t31.tsTypeLiteral(members));
|
|
3515
3572
|
}
|
|
3516
3573
|
return;
|
|
3517
3574
|
}
|
|
3518
|
-
if (
|
|
3575
|
+
if (t31.isObjectExpression(runtimeArg)) {
|
|
3519
3576
|
for (const prop of runtimeArg.properties) {
|
|
3520
|
-
if (!
|
|
3521
|
-
if (
|
|
3577
|
+
if (!t31.isObjectProperty(prop)) continue;
|
|
3578
|
+
if (t31.isSpreadElement(prop)) continue;
|
|
3522
3579
|
const rawName = resolvePropName(prop.key);
|
|
3523
3580
|
if (!rawName) continue;
|
|
3524
3581
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
3525
3582
|
const key = buildKey(handlerName);
|
|
3526
|
-
const params =
|
|
3527
|
-
const fnType =
|
|
3528
|
-
const propSig =
|
|
3583
|
+
const params = t31.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
3584
|
+
const fnType = t31.tsFunctionType(null, params, t31.tsTypeAnnotation(t31.tsAnyKeyword()));
|
|
3585
|
+
const propSig = t31.tsPropertySignature(key, t31.tsTypeAnnotation(fnType));
|
|
3529
3586
|
propSig.optional = true;
|
|
3530
3587
|
members.push(propSig);
|
|
3531
3588
|
}
|
|
3532
3589
|
if (members.length) {
|
|
3533
|
-
emitTypes.push(
|
|
3590
|
+
emitTypes.push(t31.tsTypeLiteral(members));
|
|
3534
3591
|
}
|
|
3535
3592
|
}
|
|
3536
3593
|
}
|
|
@@ -3551,129 +3608,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
3551
3608
|
return `on${name}`;
|
|
3552
3609
|
}
|
|
3553
3610
|
function resolvePropName(key) {
|
|
3554
|
-
if (
|
|
3555
|
-
if (
|
|
3556
|
-
if (
|
|
3611
|
+
if (t31.isIdentifier(key)) return key.name;
|
|
3612
|
+
if (t31.isStringLiteral(key)) return key.value;
|
|
3613
|
+
if (t31.isNumericLiteral(key)) return String(key.value);
|
|
3557
3614
|
return null;
|
|
3558
3615
|
}
|
|
3559
3616
|
function buildKey(name) {
|
|
3560
|
-
return
|
|
3617
|
+
return t31.isValidIdentifier(name) ? t31.identifier(name) : t31.stringLiteral(name);
|
|
3561
3618
|
}
|
|
3562
3619
|
function createRestAnyParam(name) {
|
|
3563
|
-
const id =
|
|
3564
|
-
const rest =
|
|
3565
|
-
rest.typeAnnotation =
|
|
3620
|
+
const id = t31.identifier(name);
|
|
3621
|
+
const rest = t31.restElement(id);
|
|
3622
|
+
rest.typeAnnotation = t31.tsTypeAnnotation(t31.tsArrayType(t31.tsAnyKeyword()));
|
|
3566
3623
|
return rest;
|
|
3567
3624
|
}
|
|
3568
3625
|
function resolveRuntimeTupleParams(value) {
|
|
3569
3626
|
const params = [];
|
|
3570
3627
|
value.elements.forEach((element, index) => {
|
|
3571
3628
|
if (!element) return;
|
|
3572
|
-
if (
|
|
3629
|
+
if (t31.isSpreadElement(element)) {
|
|
3573
3630
|
params.push(createRestAnyParam(`args${index}`));
|
|
3574
3631
|
return;
|
|
3575
3632
|
}
|
|
3576
|
-
if (
|
|
3577
|
-
const id =
|
|
3578
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
3633
|
+
if (t31.isIdentifier(element)) {
|
|
3634
|
+
const id = t31.identifier(element.name);
|
|
3635
|
+
id.typeAnnotation = element.typeAnnotation || t31.tsTypeAnnotation(t31.tsAnyKeyword());
|
|
3579
3636
|
params.push(id);
|
|
3580
3637
|
return;
|
|
3581
3638
|
}
|
|
3582
|
-
if (
|
|
3583
|
-
const id =
|
|
3584
|
-
id.typeAnnotation =
|
|
3639
|
+
if (t31.isTSAsExpression(element)) {
|
|
3640
|
+
const id = t31.identifier(`arg${index}`);
|
|
3641
|
+
id.typeAnnotation = t31.tsTypeAnnotation(element.typeAnnotation);
|
|
3585
3642
|
params.push(id);
|
|
3586
3643
|
return;
|
|
3587
3644
|
}
|
|
3588
|
-
const fallback =
|
|
3589
|
-
fallback.typeAnnotation =
|
|
3645
|
+
const fallback = t31.identifier(`arg${index}`);
|
|
3646
|
+
fallback.typeAnnotation = t31.tsTypeAnnotation(t31.tsAnyKeyword());
|
|
3590
3647
|
params.push(fallback);
|
|
3591
3648
|
});
|
|
3592
3649
|
return params;
|
|
3593
3650
|
}
|
|
3594
3651
|
function resolveExplicitEmitType(tsType) {
|
|
3595
|
-
if (
|
|
3652
|
+
if (t31.isTSParenthesizedType(tsType)) {
|
|
3596
3653
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
3597
3654
|
}
|
|
3598
|
-
if (
|
|
3655
|
+
if (t31.isTSTypeReference(tsType)) {
|
|
3599
3656
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3600
3657
|
return tsType;
|
|
3601
3658
|
}
|
|
3602
3659
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
3603
|
-
return
|
|
3660
|
+
return t31.tsTypeReference(
|
|
3604
3661
|
tsType.typeName,
|
|
3605
|
-
|
|
3662
|
+
t31.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3606
3663
|
);
|
|
3607
3664
|
}
|
|
3608
|
-
if (
|
|
3665
|
+
if (t31.isTSIntersectionType(tsType)) {
|
|
3609
3666
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
3610
3667
|
if (!types.length) return null;
|
|
3611
3668
|
if (types.length === 1) return types[0];
|
|
3612
|
-
return
|
|
3669
|
+
return t31.tsIntersectionType(types);
|
|
3613
3670
|
}
|
|
3614
|
-
if (
|
|
3671
|
+
if (t31.isTSUnionType(tsType)) {
|
|
3615
3672
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
3616
3673
|
if (!types.length) return null;
|
|
3617
3674
|
if (types.length === 1) return types[0];
|
|
3618
|
-
return
|
|
3675
|
+
return t31.tsUnionType(types);
|
|
3619
3676
|
}
|
|
3620
|
-
if (
|
|
3677
|
+
if (t31.isTSTypeLiteral(tsType)) {
|
|
3621
3678
|
const members = [];
|
|
3622
3679
|
for (const member of tsType.members) {
|
|
3623
|
-
if (
|
|
3680
|
+
if (t31.isTSPropertySignature(member)) {
|
|
3624
3681
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
3625
3682
|
if (prop) members.push(prop);
|
|
3626
3683
|
continue;
|
|
3627
3684
|
}
|
|
3628
|
-
if (
|
|
3685
|
+
if (t31.isTSCallSignatureDeclaration(member)) {
|
|
3629
3686
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
3630
3687
|
continue;
|
|
3631
3688
|
}
|
|
3632
3689
|
}
|
|
3633
3690
|
if (!members.length) return null;
|
|
3634
|
-
return
|
|
3691
|
+
return t31.tsTypeLiteral(members);
|
|
3635
3692
|
}
|
|
3636
|
-
if (
|
|
3693
|
+
if (t31.isTSFunctionType(tsType)) {
|
|
3637
3694
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
3638
3695
|
if (!props.length) return null;
|
|
3639
|
-
return
|
|
3696
|
+
return t31.tsTypeLiteral(props);
|
|
3640
3697
|
}
|
|
3641
3698
|
return tsType;
|
|
3642
3699
|
}
|
|
3643
3700
|
function hasEmitsSignatureInType(tsType) {
|
|
3644
|
-
if (
|
|
3701
|
+
if (t31.isTSParenthesizedType(tsType)) {
|
|
3645
3702
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
3646
3703
|
}
|
|
3647
|
-
if (
|
|
3704
|
+
if (t31.isTSTypeReference(tsType)) {
|
|
3648
3705
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3649
3706
|
return false;
|
|
3650
3707
|
}
|
|
3651
3708
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
3652
3709
|
}
|
|
3653
|
-
if (
|
|
3710
|
+
if (t31.isTSIntersectionType(tsType) || t31.isTSUnionType(tsType)) {
|
|
3654
3711
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
3655
3712
|
}
|
|
3656
|
-
if (
|
|
3713
|
+
if (t31.isTSTypeLiteral(tsType)) {
|
|
3657
3714
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
3658
3715
|
}
|
|
3659
|
-
if (
|
|
3716
|
+
if (t31.isTSFunctionType(tsType)) {
|
|
3660
3717
|
return isEmitsCallable(tsType.parameters);
|
|
3661
3718
|
}
|
|
3662
3719
|
return false;
|
|
3663
3720
|
}
|
|
3664
3721
|
function hasEmitsSignatureInMember(member) {
|
|
3665
|
-
if (
|
|
3722
|
+
if (t31.isTSCallSignatureDeclaration(member)) {
|
|
3666
3723
|
return isEmitsCallable(member.parameters);
|
|
3667
3724
|
}
|
|
3668
3725
|
return false;
|
|
3669
3726
|
}
|
|
3670
3727
|
function isEmitsCallable(parameters) {
|
|
3671
3728
|
const [eventParam] = parameters;
|
|
3672
|
-
if (!eventParam || !
|
|
3729
|
+
if (!eventParam || !t31.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
3673
3730
|
return false;
|
|
3674
3731
|
}
|
|
3675
3732
|
const { typeAnnotation } = eventParam;
|
|
3676
|
-
if (
|
|
3733
|
+
if (t31.isNoop(typeAnnotation)) return false;
|
|
3677
3734
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
3678
3735
|
}
|
|
3679
3736
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -3683,19 +3740,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
3683
3740
|
const key = buildKey(handlerName);
|
|
3684
3741
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3685
3742
|
let params = [];
|
|
3686
|
-
let returnType =
|
|
3687
|
-
if (typeAnnotation &&
|
|
3743
|
+
let returnType = t31.tsAnyKeyword();
|
|
3744
|
+
if (typeAnnotation && t31.isTSFunctionType(typeAnnotation)) {
|
|
3688
3745
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
3689
3746
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
3690
|
-
} else if (typeAnnotation &&
|
|
3747
|
+
} else if (typeAnnotation && t31.isTSTupleType(typeAnnotation)) {
|
|
3691
3748
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
3692
3749
|
} else if (typeAnnotation) {
|
|
3693
|
-
const id =
|
|
3694
|
-
id.typeAnnotation =
|
|
3750
|
+
const id = t31.identifier("value");
|
|
3751
|
+
id.typeAnnotation = t31.tsTypeAnnotation(typeAnnotation);
|
|
3695
3752
|
params = [id];
|
|
3696
3753
|
}
|
|
3697
|
-
const fnType =
|
|
3698
|
-
const prop =
|
|
3754
|
+
const fnType = t31.tsFunctionType(null, params, t31.tsTypeAnnotation(returnType));
|
|
3755
|
+
const prop = t31.tsPropertySignature(key, t31.tsTypeAnnotation(fnType));
|
|
3699
3756
|
prop.optional = !!member.optional;
|
|
3700
3757
|
return prop;
|
|
3701
3758
|
}
|
|
@@ -3704,32 +3761,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
3704
3761
|
}
|
|
3705
3762
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
3706
3763
|
const [eventParam, ...restParams] = parameters;
|
|
3707
|
-
if (!eventParam || !
|
|
3764
|
+
if (!eventParam || !t31.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
3708
3765
|
return [];
|
|
3709
3766
|
}
|
|
3710
3767
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
3711
|
-
if (
|
|
3768
|
+
if (t31.isNoop(paramTypeAnnotation)) return [];
|
|
3712
3769
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
3713
3770
|
if (!eventNames.length) return [];
|
|
3714
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
3771
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t31.tsAnyKeyword();
|
|
3715
3772
|
return eventNames.map((eventName) => {
|
|
3716
3773
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
3717
3774
|
const key = buildKey(handlerName);
|
|
3718
3775
|
const params = cloneCallableParams(restParams);
|
|
3719
|
-
const fnType =
|
|
3720
|
-
const prop =
|
|
3776
|
+
const fnType = t31.tsFunctionType(null, params, t31.tsTypeAnnotation(returnType));
|
|
3777
|
+
const prop = t31.tsPropertySignature(key, t31.tsTypeAnnotation(fnType));
|
|
3721
3778
|
prop.optional = true;
|
|
3722
3779
|
return prop;
|
|
3723
3780
|
});
|
|
3724
3781
|
}
|
|
3725
3782
|
function resolveEventNames(type) {
|
|
3726
|
-
if (
|
|
3783
|
+
if (t31.isTSLiteralType(type) && t31.isStringLiteral(type.literal)) {
|
|
3727
3784
|
return [type.literal.value];
|
|
3728
3785
|
}
|
|
3729
|
-
if (
|
|
3786
|
+
if (t31.isTSUnionType(type)) {
|
|
3730
3787
|
return type.types.flatMap(resolveEventNames);
|
|
3731
3788
|
}
|
|
3732
|
-
if (
|
|
3789
|
+
if (t31.isTSParenthesizedType(type)) {
|
|
3733
3790
|
return resolveEventNames(type.typeAnnotation);
|
|
3734
3791
|
}
|
|
3735
3792
|
return [];
|
|
@@ -3742,44 +3799,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
3742
3799
|
return params;
|
|
3743
3800
|
}
|
|
3744
3801
|
function resolveTupleElementParam(element, index) {
|
|
3745
|
-
const isNamedTuple = typeof
|
|
3802
|
+
const isNamedTuple = typeof t31.isTSNamedTupleMember === "function" && t31.isTSNamedTupleMember(element);
|
|
3746
3803
|
if (isNamedTuple) {
|
|
3747
3804
|
const tupleMember = element;
|
|
3748
3805
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
3749
3806
|
let innerType = tupleMember.elementType;
|
|
3750
3807
|
let optional = tupleMember.optional;
|
|
3751
|
-
if (
|
|
3808
|
+
if (t31.isTSOptionalType(innerType)) {
|
|
3752
3809
|
optional = true;
|
|
3753
3810
|
innerType = innerType.typeAnnotation;
|
|
3754
3811
|
}
|
|
3755
|
-
if (
|
|
3756
|
-
const rest =
|
|
3757
|
-
rest.typeAnnotation =
|
|
3812
|
+
if (t31.isTSRestType(innerType)) {
|
|
3813
|
+
const rest = t31.restElement(t31.identifier(name));
|
|
3814
|
+
rest.typeAnnotation = t31.tsTypeAnnotation(innerType.typeAnnotation);
|
|
3758
3815
|
return rest;
|
|
3759
3816
|
}
|
|
3760
|
-
const id2 =
|
|
3817
|
+
const id2 = t31.identifier(name);
|
|
3761
3818
|
id2.optional = optional;
|
|
3762
|
-
id2.typeAnnotation =
|
|
3819
|
+
id2.typeAnnotation = t31.tsTypeAnnotation(innerType);
|
|
3763
3820
|
return id2;
|
|
3764
3821
|
}
|
|
3765
|
-
if (
|
|
3766
|
-
const rest =
|
|
3767
|
-
rest.typeAnnotation =
|
|
3822
|
+
if (t31.isTSRestType(element)) {
|
|
3823
|
+
const rest = t31.restElement(t31.identifier(`args${index}`));
|
|
3824
|
+
rest.typeAnnotation = t31.tsTypeAnnotation(element.typeAnnotation);
|
|
3768
3825
|
return rest;
|
|
3769
3826
|
}
|
|
3770
|
-
if (
|
|
3771
|
-
const id2 =
|
|
3827
|
+
if (t31.isTSOptionalType(element)) {
|
|
3828
|
+
const id2 = t31.identifier(`arg${index}`);
|
|
3772
3829
|
id2.optional = true;
|
|
3773
|
-
id2.typeAnnotation =
|
|
3830
|
+
id2.typeAnnotation = t31.tsTypeAnnotation(element.typeAnnotation);
|
|
3774
3831
|
return id2;
|
|
3775
3832
|
}
|
|
3776
|
-
const id =
|
|
3777
|
-
id.typeAnnotation =
|
|
3833
|
+
const id = t31.identifier(`arg${index}`);
|
|
3834
|
+
id.typeAnnotation = t31.tsTypeAnnotation(element);
|
|
3778
3835
|
return id;
|
|
3779
3836
|
}
|
|
3780
3837
|
|
|
3781
3838
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
3782
|
-
import * as
|
|
3839
|
+
import * as t32 from "@babel/types";
|
|
3783
3840
|
function resolveDefinePropsIface(path8, ctx) {
|
|
3784
3841
|
const { node } = path8;
|
|
3785
3842
|
const [runtimeArg] = node.arguments;
|
|
@@ -3799,38 +3856,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3799
3856
|
} = scriptData;
|
|
3800
3857
|
if (!runtimeArg) return;
|
|
3801
3858
|
const members = [];
|
|
3802
|
-
if (
|
|
3859
|
+
if (t32.isArrayExpression(runtimeArg)) {
|
|
3803
3860
|
for (const element of runtimeArg.elements) {
|
|
3804
|
-
if (!element || !
|
|
3805
|
-
const key =
|
|
3806
|
-
const prop =
|
|
3861
|
+
if (!element || !t32.isStringLiteral(element)) continue;
|
|
3862
|
+
const key = t32.isValidIdentifier(element.value) ? t32.identifier(element.value) : t32.stringLiteral(element.value);
|
|
3863
|
+
const prop = t32.tsPropertySignature(key, t32.tsTypeAnnotation(t32.tsAnyKeyword()));
|
|
3807
3864
|
prop.optional = true;
|
|
3808
3865
|
members.push(prop);
|
|
3809
3866
|
}
|
|
3810
3867
|
if (members.length) {
|
|
3811
|
-
propsTypes.push(
|
|
3868
|
+
propsTypes.push(t32.tsTypeLiteral(members));
|
|
3812
3869
|
}
|
|
3813
3870
|
return;
|
|
3814
3871
|
}
|
|
3815
|
-
if (
|
|
3872
|
+
if (t32.isObjectExpression(runtimeArg)) {
|
|
3816
3873
|
for (const prop of runtimeArg.properties) {
|
|
3817
|
-
if (!
|
|
3818
|
-
if (
|
|
3874
|
+
if (!t32.isObjectProperty(prop)) continue;
|
|
3875
|
+
if (t32.isSpreadElement(prop)) continue;
|
|
3819
3876
|
const key = prop.key;
|
|
3820
3877
|
let propName = null;
|
|
3821
|
-
if (
|
|
3822
|
-
if (
|
|
3823
|
-
if (
|
|
3878
|
+
if (t32.isIdentifier(key)) propName = key.name;
|
|
3879
|
+
if (t32.isStringLiteral(key)) propName = key.value;
|
|
3880
|
+
if (t32.isNumericLiteral(key)) propName = String(key.value);
|
|
3824
3881
|
if (!propName) continue;
|
|
3825
3882
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
3826
|
-
const tsType = type ??
|
|
3827
|
-
const tsKey =
|
|
3828
|
-
const tsProp =
|
|
3883
|
+
const tsType = type ?? t32.tsAnyKeyword();
|
|
3884
|
+
const tsKey = t32.isValidIdentifier(propName) ? t32.identifier(propName) : t32.stringLiteral(propName);
|
|
3885
|
+
const tsProp = t32.tsPropertySignature(tsKey, t32.tsTypeAnnotation(tsType));
|
|
3829
3886
|
tsProp.optional = !required;
|
|
3830
3887
|
members.push(tsProp);
|
|
3831
3888
|
}
|
|
3832
3889
|
if (members.length) {
|
|
3833
|
-
propsTypes.push(
|
|
3890
|
+
propsTypes.push(t32.tsTypeLiteral(members));
|
|
3834
3891
|
}
|
|
3835
3892
|
return;
|
|
3836
3893
|
}
|
|
@@ -3844,42 +3901,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3844
3901
|
);
|
|
3845
3902
|
}
|
|
3846
3903
|
function resolveRuntimePropMeta(value) {
|
|
3847
|
-
if (
|
|
3904
|
+
if (t32.isIdentifier(value)) {
|
|
3848
3905
|
return {
|
|
3849
3906
|
type: mapRuntimeTypeToTSType(value),
|
|
3850
3907
|
required: false
|
|
3851
3908
|
};
|
|
3852
3909
|
}
|
|
3853
|
-
if (
|
|
3910
|
+
if (t32.isArrayExpression(value)) {
|
|
3854
3911
|
return {
|
|
3855
3912
|
type: resolveRuntimeUnionType(value),
|
|
3856
3913
|
required: false
|
|
3857
3914
|
};
|
|
3858
3915
|
}
|
|
3859
|
-
if (!
|
|
3916
|
+
if (!t32.isObjectExpression(value)) {
|
|
3860
3917
|
return { required: false };
|
|
3861
3918
|
}
|
|
3862
3919
|
let type;
|
|
3863
3920
|
let required = false;
|
|
3864
3921
|
for (const prop of value.properties) {
|
|
3865
|
-
if (!
|
|
3866
|
-
if (
|
|
3922
|
+
if (!t32.isObjectProperty(prop)) continue;
|
|
3923
|
+
if (t32.isSpreadElement(prop)) continue;
|
|
3867
3924
|
const key = prop.key;
|
|
3868
|
-
const propName =
|
|
3925
|
+
const propName = t32.isIdentifier(key) ? key.name : t32.isStringLiteral(key) ? key.value : null;
|
|
3869
3926
|
if (!propName) continue;
|
|
3870
3927
|
if (propName === "type") {
|
|
3871
3928
|
const valueNode = prop.value;
|
|
3872
|
-
if (
|
|
3929
|
+
if (t32.isArrayExpression(valueNode)) {
|
|
3873
3930
|
type = resolveRuntimeUnionType(valueNode);
|
|
3874
3931
|
continue;
|
|
3875
3932
|
}
|
|
3876
|
-
if (
|
|
3933
|
+
if (t32.isIdentifier(valueNode)) {
|
|
3877
3934
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
3878
3935
|
continue;
|
|
3879
3936
|
}
|
|
3880
3937
|
}
|
|
3881
3938
|
if (propName === "required") {
|
|
3882
|
-
if (
|
|
3939
|
+
if (t32.isBooleanLiteral(prop.value)) {
|
|
3883
3940
|
required = prop.value.value;
|
|
3884
3941
|
}
|
|
3885
3942
|
}
|
|
@@ -3889,80 +3946,80 @@ function resolveRuntimePropMeta(value) {
|
|
|
3889
3946
|
function resolveRuntimeUnionType(value) {
|
|
3890
3947
|
const types = [];
|
|
3891
3948
|
for (const element of value.elements) {
|
|
3892
|
-
if (!element || !
|
|
3949
|
+
if (!element || !t32.isIdentifier(element)) continue;
|
|
3893
3950
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
3894
3951
|
if (resolved) types.push(resolved);
|
|
3895
3952
|
}
|
|
3896
|
-
if (!types.length) return
|
|
3953
|
+
if (!types.length) return t32.tsAnyKeyword();
|
|
3897
3954
|
if (types.length === 1) return types[0];
|
|
3898
|
-
return
|
|
3955
|
+
return t32.tsUnionType(types);
|
|
3899
3956
|
}
|
|
3900
3957
|
function mapRuntimeTypeToTSType(value) {
|
|
3901
3958
|
switch (value.name) {
|
|
3902
3959
|
case "String":
|
|
3903
|
-
return
|
|
3960
|
+
return t32.tsStringKeyword();
|
|
3904
3961
|
case "Number":
|
|
3905
|
-
return
|
|
3962
|
+
return t32.tsNumberKeyword();
|
|
3906
3963
|
case "Boolean":
|
|
3907
|
-
return
|
|
3964
|
+
return t32.tsBooleanKeyword();
|
|
3908
3965
|
case "Object":
|
|
3909
|
-
return
|
|
3966
|
+
return t32.tsTypeLiteral([]);
|
|
3910
3967
|
case "Array":
|
|
3911
|
-
return
|
|
3968
|
+
return t32.tsArrayType(t32.tsAnyKeyword());
|
|
3912
3969
|
case "Function":
|
|
3913
|
-
return
|
|
3970
|
+
return t32.tsFunctionType(null, [], t32.tsTypeAnnotation(t32.tsAnyKeyword()));
|
|
3914
3971
|
case "Symbol":
|
|
3915
|
-
return
|
|
3972
|
+
return t32.tsSymbolKeyword();
|
|
3916
3973
|
case "BigInt":
|
|
3917
|
-
return
|
|
3974
|
+
return t32.tsBigIntKeyword();
|
|
3918
3975
|
default:
|
|
3919
|
-
return
|
|
3976
|
+
return t32.tsAnyKeyword();
|
|
3920
3977
|
}
|
|
3921
3978
|
}
|
|
3922
3979
|
|
|
3923
3980
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
3924
|
-
import * as
|
|
3981
|
+
import * as t35 from "@babel/types";
|
|
3925
3982
|
|
|
3926
3983
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/slot-builder.ts
|
|
3927
|
-
import * as
|
|
3984
|
+
import * as t33 from "@babel/types";
|
|
3928
3985
|
var SLOT_DEFAULT_NAME = "default";
|
|
3929
3986
|
var SLOT_CHILDREN_NAME = "children";
|
|
3930
3987
|
var SLOT_FN_PARAM_NAME = "props";
|
|
3931
3988
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
3932
3989
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
3933
|
-
const key =
|
|
3934
|
-
const reactNodeType =
|
|
3935
|
-
|
|
3990
|
+
const key = t33.isValidIdentifier(propName) ? t33.identifier(propName) : t33.stringLiteral(propName);
|
|
3991
|
+
const reactNodeType = t33.tsTypeAnnotation(
|
|
3992
|
+
t33.tsTypeReference(t33.identifier(REACT_API_MAP.ReactNode))
|
|
3936
3993
|
);
|
|
3937
3994
|
let typeAnnotation;
|
|
3938
3995
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
3939
3996
|
typeAnnotation = reactNodeType;
|
|
3940
3997
|
} else {
|
|
3941
|
-
const fnType =
|
|
3942
|
-
typeAnnotation =
|
|
3998
|
+
const fnType = t33.tsFunctionType(null, params, reactNodeType);
|
|
3999
|
+
typeAnnotation = t33.tsTypeAnnotation(fnType);
|
|
3943
4000
|
}
|
|
3944
|
-
const prop =
|
|
4001
|
+
const prop = t33.tsPropertySignature(key, typeAnnotation);
|
|
3945
4002
|
prop.optional = optional;
|
|
3946
4003
|
return prop;
|
|
3947
4004
|
}
|
|
3948
4005
|
function createSlotScopeParam(props, ctx) {
|
|
3949
|
-
const paramId =
|
|
4006
|
+
const paramId = t33.identifier(SLOT_FN_PARAM_NAME);
|
|
3950
4007
|
const propsSigns = [];
|
|
3951
4008
|
const { reactiveBindings } = ctx.templateData;
|
|
3952
4009
|
props.forEach(({ prop, tsType }) => {
|
|
3953
4010
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3954
4011
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3955
|
-
const typeAnnotation = foundBindingTypes ?
|
|
3956
|
-
const key =
|
|
3957
|
-
const propSign =
|
|
4012
|
+
const typeAnnotation = foundBindingTypes ? t33.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
4013
|
+
const key = t33.isValidIdentifier(prop) ? t33.identifier(prop) : t33.stringLiteral(prop);
|
|
4014
|
+
const propSign = t33.tsPropertySignature(key, typeAnnotation);
|
|
3958
4015
|
propsSigns.push(propSign);
|
|
3959
4016
|
});
|
|
3960
|
-
paramId.typeAnnotation =
|
|
4017
|
+
paramId.typeAnnotation = t33.tsTypeAnnotation(t33.tsTypeLiteral(propsSigns));
|
|
3961
4018
|
return paramId;
|
|
3962
4019
|
}
|
|
3963
4020
|
|
|
3964
4021
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/utils.ts
|
|
3965
|
-
import * as
|
|
4022
|
+
import * as t34 from "@babel/types";
|
|
3966
4023
|
function recordReactNode(ctx) {
|
|
3967
4024
|
if (!ctx.scriptData.lang.startsWith("ts")) {
|
|
3968
4025
|
return;
|
|
@@ -3978,16 +4035,16 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
3978
4035
|
return declarations;
|
|
3979
4036
|
}
|
|
3980
4037
|
for (const statement of programPath.node.body) {
|
|
3981
|
-
if (
|
|
4038
|
+
if (t34.isTSInterfaceDeclaration(statement)) {
|
|
3982
4039
|
declarations.set(statement.id.name, {
|
|
3983
|
-
type:
|
|
4040
|
+
type: t34.tsTypeLiteral(statement.body.body),
|
|
3984
4041
|
// 将接口体转换为类型字面量
|
|
3985
4042
|
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
3986
4043
|
// 检查是否有泛型参数
|
|
3987
4044
|
});
|
|
3988
4045
|
continue;
|
|
3989
4046
|
}
|
|
3990
|
-
if (
|
|
4047
|
+
if (t34.isTSTypeAliasDeclaration(statement)) {
|
|
3991
4048
|
declarations.set(statement.id.name, {
|
|
3992
4049
|
type: statement.typeAnnotation,
|
|
3993
4050
|
// 直接使用类型注解
|
|
@@ -3996,16 +4053,16 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
3996
4053
|
});
|
|
3997
4054
|
continue;
|
|
3998
4055
|
}
|
|
3999
|
-
if (
|
|
4056
|
+
if (t34.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
4000
4057
|
const declaration = statement.declaration;
|
|
4001
|
-
if (
|
|
4058
|
+
if (t34.isTSInterfaceDeclaration(declaration)) {
|
|
4002
4059
|
declarations.set(declaration.id.name, {
|
|
4003
|
-
type:
|
|
4060
|
+
type: t34.tsTypeLiteral(declaration.body.body),
|
|
4004
4061
|
// 将接口体转换为类型字面量
|
|
4005
4062
|
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
4006
4063
|
// 检查是否有泛型参数
|
|
4007
4064
|
});
|
|
4008
|
-
} else if (
|
|
4065
|
+
} else if (t34.isTSTypeAliasDeclaration(declaration)) {
|
|
4009
4066
|
declarations.set(declaration.id.name, {
|
|
4010
4067
|
type: declaration.typeAnnotation,
|
|
4011
4068
|
// 直接使用类型注解
|
|
@@ -4018,22 +4075,22 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
4018
4075
|
return declarations;
|
|
4019
4076
|
}
|
|
4020
4077
|
function resolvePropName2(key) {
|
|
4021
|
-
if (
|
|
4078
|
+
if (t34.isIdentifier(key)) {
|
|
4022
4079
|
return key.name;
|
|
4023
4080
|
}
|
|
4024
|
-
if (
|
|
4081
|
+
if (t34.isStringLiteral(key)) {
|
|
4025
4082
|
return key.value;
|
|
4026
4083
|
}
|
|
4027
|
-
if (
|
|
4084
|
+
if (t34.isNumericLiteral(key)) {
|
|
4028
4085
|
return String(key.value);
|
|
4029
4086
|
}
|
|
4030
4087
|
return null;
|
|
4031
4088
|
}
|
|
4032
4089
|
function resolveCallableType(tsType) {
|
|
4033
|
-
if (
|
|
4090
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
4034
4091
|
return tsType;
|
|
4035
4092
|
}
|
|
4036
|
-
if (
|
|
4093
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
4037
4094
|
return resolveCallableType(tsType.typeAnnotation);
|
|
4038
4095
|
}
|
|
4039
4096
|
return null;
|
|
@@ -4042,10 +4099,10 @@ function resolveCallableType(tsType) {
|
|
|
4042
4099
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
4043
4100
|
var SLOT_DEFAULT_NAME2 = "default";
|
|
4044
4101
|
function resolveSlotType(tsType, options) {
|
|
4045
|
-
if (
|
|
4102
|
+
if (t35.isTSParenthesizedType(tsType)) {
|
|
4046
4103
|
return resolveSlotType(tsType.typeAnnotation, options);
|
|
4047
4104
|
}
|
|
4048
|
-
if (
|
|
4105
|
+
if (t35.isTSTypeReference(tsType)) {
|
|
4049
4106
|
let shouldRecordReactNode = false;
|
|
4050
4107
|
if (tsType.typeParameters?.params.length) {
|
|
4051
4108
|
const params = [];
|
|
@@ -4061,11 +4118,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4061
4118
|
};
|
|
4062
4119
|
}
|
|
4063
4120
|
return {
|
|
4064
|
-
type:
|
|
4121
|
+
type: t35.tsTypeReference(tsType.typeName, t35.tsTypeParameterInstantiation(params)),
|
|
4065
4122
|
shouldRecordReactNode
|
|
4066
4123
|
};
|
|
4067
4124
|
}
|
|
4068
|
-
if (!
|
|
4125
|
+
if (!t35.isIdentifier(tsType.typeName)) {
|
|
4069
4126
|
return {
|
|
4070
4127
|
type: tsType,
|
|
4071
4128
|
shouldRecordReactNode: false
|
|
@@ -4096,7 +4153,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4096
4153
|
}
|
|
4097
4154
|
return resolved;
|
|
4098
4155
|
}
|
|
4099
|
-
if (
|
|
4156
|
+
if (t35.isTSIntersectionType(tsType)) {
|
|
4100
4157
|
const types = [];
|
|
4101
4158
|
let shouldRecordReactNode = false;
|
|
4102
4159
|
for (const item of tsType.types) {
|
|
@@ -4119,11 +4176,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4119
4176
|
};
|
|
4120
4177
|
}
|
|
4121
4178
|
return {
|
|
4122
|
-
type:
|
|
4179
|
+
type: t35.tsIntersectionType(types),
|
|
4123
4180
|
shouldRecordReactNode
|
|
4124
4181
|
};
|
|
4125
4182
|
}
|
|
4126
|
-
if (
|
|
4183
|
+
if (t35.isTSUnionType(tsType)) {
|
|
4127
4184
|
const types = [];
|
|
4128
4185
|
let shouldRecordReactNode = false;
|
|
4129
4186
|
for (const item of tsType.types) {
|
|
@@ -4146,11 +4203,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4146
4203
|
};
|
|
4147
4204
|
}
|
|
4148
4205
|
return {
|
|
4149
|
-
type:
|
|
4206
|
+
type: t35.tsUnionType(types),
|
|
4150
4207
|
shouldRecordReactNode
|
|
4151
4208
|
};
|
|
4152
4209
|
}
|
|
4153
|
-
if (
|
|
4210
|
+
if (t35.isTSTypeLiteral(tsType)) {
|
|
4154
4211
|
const members = [];
|
|
4155
4212
|
let shouldRecordReactNode = false;
|
|
4156
4213
|
for (const item of tsType.members) {
|
|
@@ -4165,11 +4222,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4165
4222
|
};
|
|
4166
4223
|
}
|
|
4167
4224
|
return {
|
|
4168
|
-
type:
|
|
4225
|
+
type: t35.tsTypeLiteral(members),
|
|
4169
4226
|
shouldRecordReactNode
|
|
4170
4227
|
};
|
|
4171
4228
|
}
|
|
4172
|
-
if (
|
|
4229
|
+
if (t35.isTSFunctionType(tsType)) {
|
|
4173
4230
|
const props = buildSlotPropSignature(
|
|
4174
4231
|
SLOT_DEFAULT_NAME2,
|
|
4175
4232
|
cloneCallableParams(tsType.parameters),
|
|
@@ -4177,7 +4234,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4177
4234
|
// 默认插槽不是可选的
|
|
4178
4235
|
);
|
|
4179
4236
|
return {
|
|
4180
|
-
type:
|
|
4237
|
+
type: t35.tsTypeLiteral([props]),
|
|
4181
4238
|
shouldRecordReactNode: true
|
|
4182
4239
|
// 函数类型总是需要记录 ReactNode
|
|
4183
4240
|
};
|
|
@@ -4188,7 +4245,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4188
4245
|
};
|
|
4189
4246
|
}
|
|
4190
4247
|
function resolveSlotPropFromMember(member) {
|
|
4191
|
-
if (
|
|
4248
|
+
if (t35.isTSMethodSignature(member)) {
|
|
4192
4249
|
const rawName = resolvePropName2(member.key);
|
|
4193
4250
|
if (!rawName) {
|
|
4194
4251
|
return {
|
|
@@ -4203,7 +4260,7 @@ function resolveSlotPropFromMember(member) {
|
|
|
4203
4260
|
// 方法签名总是可调用,需要记录 ReactNode
|
|
4204
4261
|
};
|
|
4205
4262
|
}
|
|
4206
|
-
if (
|
|
4263
|
+
if (t35.isTSPropertySignature(member)) {
|
|
4207
4264
|
const rawName = resolvePropName2(member.key);
|
|
4208
4265
|
if (!rawName) {
|
|
4209
4266
|
return {
|
|
@@ -4226,7 +4283,7 @@ function resolveSlotPropFromMember(member) {
|
|
|
4226
4283
|
// 可调用属性需要记录 ReactNode
|
|
4227
4284
|
};
|
|
4228
4285
|
}
|
|
4229
|
-
if (
|
|
4286
|
+
if (t35.isTSCallSignatureDeclaration(member)) {
|
|
4230
4287
|
const params = cloneCallableParams(member.parameters);
|
|
4231
4288
|
return {
|
|
4232
4289
|
member: buildSlotPropSignature(SLOT_DEFAULT_NAME2, params, true),
|
|
@@ -4266,7 +4323,7 @@ function resolveDefineSlotsIface(path8, ctx) {
|
|
|
4266
4323
|
}
|
|
4267
4324
|
|
|
4268
4325
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/template-slots.ts
|
|
4269
|
-
import * as
|
|
4326
|
+
import * as t36 from "@babel/types";
|
|
4270
4327
|
function resolveTemplateSlotIface(ctx) {
|
|
4271
4328
|
if (ctx.inputType !== "sfc") return;
|
|
4272
4329
|
const {
|
|
@@ -4284,7 +4341,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
4284
4341
|
}
|
|
4285
4342
|
if (tsMembers.length) {
|
|
4286
4343
|
recordReactNode(ctx);
|
|
4287
|
-
slotTypes.push(
|
|
4344
|
+
slotTypes.push(t36.tsTypeLiteral(tsMembers));
|
|
4288
4345
|
}
|
|
4289
4346
|
}
|
|
4290
4347
|
|
|
@@ -4336,9 +4393,9 @@ function resolveCompIProps(ctx, ast) {
|
|
|
4336
4393
|
}
|
|
4337
4394
|
const n = declaredOptions.name || "Comp";
|
|
4338
4395
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
4339
|
-
const typeNode =
|
|
4340
|
-
const typeAliasDecl =
|
|
4341
|
-
const exportDecl =
|
|
4396
|
+
const typeNode = t37.tsIntersectionType(tsTypes);
|
|
4397
|
+
const typeAliasDecl = t37.tsTypeAliasDeclaration(t37.identifier(ns), null, typeNode);
|
|
4398
|
+
const exportDecl = t37.exportNamedDeclaration(typeAliasDecl);
|
|
4342
4399
|
propsTSIface.name = ns;
|
|
4343
4400
|
const scriptIR = getScriptIR(ctx);
|
|
4344
4401
|
scriptIR.exports.push(exportDecl);
|
|
@@ -4346,15 +4403,15 @@ function resolveCompIProps(ctx, ast) {
|
|
|
4346
4403
|
}
|
|
4347
4404
|
|
|
4348
4405
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-use-attrs.ts
|
|
4349
|
-
import * as
|
|
4406
|
+
import * as t38 from "@babel/types";
|
|
4350
4407
|
function resolveUseAttrs(ctx) {
|
|
4351
4408
|
return {
|
|
4352
4409
|
VariableDeclarator(path8) {
|
|
4353
4410
|
const { init, id } = path8.node;
|
|
4354
4411
|
if (!init) return;
|
|
4355
4412
|
const initPath = path8.get("init");
|
|
4356
|
-
const propsIdentifier =
|
|
4357
|
-
if (
|
|
4413
|
+
const propsIdentifier = t38.identifier(ctx.propField);
|
|
4414
|
+
if (t38.isTSAsExpression(init) && isUseAttrsCall(init.expression)) {
|
|
4358
4415
|
const typeAssertion = createPropsTypeAssertion(propsIdentifier, init.typeAnnotation);
|
|
4359
4416
|
replaceNode(initPath, typeAssertion, init);
|
|
4360
4417
|
return;
|
|
@@ -4365,13 +4422,13 @@ function resolveUseAttrs(ctx) {
|
|
|
4365
4422
|
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
4366
4423
|
if (isTS) {
|
|
4367
4424
|
let typeAnnotation = null;
|
|
4368
|
-
if (
|
|
4425
|
+
if (t38.isIdentifier(id) && t38.isTSTypeAnnotation(id.typeAnnotation)) {
|
|
4369
4426
|
typeAnnotation = id.typeAnnotation.typeAnnotation;
|
|
4370
4427
|
id.typeAnnotation = null;
|
|
4371
4428
|
} else {
|
|
4372
|
-
typeAnnotation =
|
|
4373
|
-
|
|
4374
|
-
|
|
4429
|
+
typeAnnotation = t38.tsTypeReference(
|
|
4430
|
+
t38.identifier("Record"),
|
|
4431
|
+
t38.tsTypeParameterInstantiation([t38.tsStringKeyword(), t38.tsUnknownKeyword()])
|
|
4375
4432
|
);
|
|
4376
4433
|
}
|
|
4377
4434
|
const propsTypeAssertion = createPropsTypeAssertion(propsIdentifier, typeAnnotation);
|
|
@@ -4383,18 +4440,18 @@ function resolveUseAttrs(ctx) {
|
|
|
4383
4440
|
};
|
|
4384
4441
|
}
|
|
4385
4442
|
function isUseAttrsCall(expr) {
|
|
4386
|
-
return
|
|
4443
|
+
return t38.isCallExpression(expr) && isCalleeNamed(expr, VUE_API_MAP.useAttrs);
|
|
4387
4444
|
}
|
|
4388
4445
|
function createPropsTypeAssertion(propsIdentifier, typeAnnotation) {
|
|
4389
|
-
return
|
|
4446
|
+
return t38.tsAsExpression(propsIdentifier, typeAnnotation);
|
|
4390
4447
|
}
|
|
4391
4448
|
|
|
4392
4449
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
4393
|
-
import * as
|
|
4450
|
+
import * as t44 from "@babel/types";
|
|
4394
4451
|
|
|
4395
4452
|
// src/core/transform/sfc/script/shared/dependency-analyzer/index.ts
|
|
4396
4453
|
import { traverse as traverse2 } from "@babel/core";
|
|
4397
|
-
import * as
|
|
4454
|
+
import * as t43 from "@babel/types";
|
|
4398
4455
|
|
|
4399
4456
|
// src/core/transform/sfc/script/shared/dependency-analyzer/binding-utils.ts
|
|
4400
4457
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
@@ -4413,7 +4470,7 @@ function isReactiveBinding(node) {
|
|
|
4413
4470
|
}
|
|
4414
4471
|
|
|
4415
4472
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-checker.ts
|
|
4416
|
-
import * as
|
|
4473
|
+
import * as t39 from "@babel/types";
|
|
4417
4474
|
function isEligibleBindingSource(binding) {
|
|
4418
4475
|
if (binding.kind === "param") {
|
|
4419
4476
|
return false;
|
|
@@ -4423,57 +4480,57 @@ function isEligibleBindingSource(binding) {
|
|
|
4423
4480
|
const declaratorPath = getVariableDeclaratorPath(bindingPath);
|
|
4424
4481
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
4425
4482
|
const nodeInit = declaratorPath?.node.init;
|
|
4426
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
4427
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
4428
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
4483
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t39.isCallExpression(nodeInit) && t39.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
4484
|
+
const isHookCallVarBinding = !!declaratorPath && t39.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
4485
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t39.isArrowFunctionExpression(nodeInit) || t39.isFunctionExpression(nodeInit));
|
|
4429
4486
|
const isReactiveFunctionBinding = isFunctionBinding && (isReactiveBinding(declaratorPath?.node) || isReactiveBinding(bindingPath.node));
|
|
4430
4487
|
return isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isReactiveFunctionBinding;
|
|
4431
4488
|
}
|
|
4432
4489
|
function isReactValidDependencyExpr(node) {
|
|
4433
|
-
if (
|
|
4490
|
+
if (t39.isIdentifier(node)) {
|
|
4434
4491
|
return true;
|
|
4435
4492
|
}
|
|
4436
|
-
if (
|
|
4493
|
+
if (t39.isMemberExpression(node) || t39.isOptionalMemberExpression(node)) {
|
|
4437
4494
|
return isStaticMemberChain(node);
|
|
4438
4495
|
}
|
|
4439
4496
|
return false;
|
|
4440
4497
|
}
|
|
4441
4498
|
function isStaticMemberChain(node) {
|
|
4442
4499
|
let current = node;
|
|
4443
|
-
while (
|
|
4444
|
-
if (!current.computed && !
|
|
4500
|
+
while (t39.isMemberExpression(current) || t39.isOptionalMemberExpression(current)) {
|
|
4501
|
+
if (!current.computed && !t39.isIdentifier(current.property)) {
|
|
4445
4502
|
return false;
|
|
4446
4503
|
}
|
|
4447
|
-
if (current.computed && !
|
|
4504
|
+
if (current.computed && !t39.isStringLiteral(current.property) && !t39.isNumericLiteral(current.property)) {
|
|
4448
4505
|
return false;
|
|
4449
4506
|
}
|
|
4450
4507
|
current = current.object;
|
|
4451
4508
|
}
|
|
4452
|
-
return
|
|
4509
|
+
return t39.isIdentifier(current);
|
|
4453
4510
|
}
|
|
4454
4511
|
function isHookLikeCallee(callee) {
|
|
4455
|
-
if (
|
|
4512
|
+
if (t39.isIdentifier(callee)) {
|
|
4456
4513
|
return callee.name.startsWith("use");
|
|
4457
4514
|
}
|
|
4458
|
-
if (
|
|
4515
|
+
if (t39.isMemberExpression(callee) && !callee.computed && t39.isIdentifier(callee.property)) {
|
|
4459
4516
|
return callee.property.name.startsWith("use");
|
|
4460
4517
|
}
|
|
4461
4518
|
return false;
|
|
4462
4519
|
}
|
|
4463
4520
|
|
|
4464
4521
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-key.ts
|
|
4465
|
-
import * as
|
|
4522
|
+
import * as t40 from "@babel/types";
|
|
4466
4523
|
function getDependencyKey(exp) {
|
|
4467
|
-
if (
|
|
4524
|
+
if (t40.isIdentifier(exp)) {
|
|
4468
4525
|
return exp.name;
|
|
4469
4526
|
}
|
|
4470
|
-
if (
|
|
4527
|
+
if (t40.isMemberExpression(exp) || t40.isOptionalMemberExpression(exp)) {
|
|
4471
4528
|
const objectKey = getDependencyKey(exp.object);
|
|
4472
4529
|
const opt = exp.optional ? "?" : "";
|
|
4473
|
-
if (!exp.computed &&
|
|
4530
|
+
if (!exp.computed && t40.isIdentifier(exp.property)) {
|
|
4474
4531
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
4475
4532
|
}
|
|
4476
|
-
if (
|
|
4533
|
+
if (t40.isStringLiteral(exp.property) || t40.isNumericLiteral(exp.property)) {
|
|
4477
4534
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
4478
4535
|
}
|
|
4479
4536
|
return `${objectKey}${opt}[*]`;
|
|
@@ -4482,40 +4539,40 @@ function getDependencyKey(exp) {
|
|
|
4482
4539
|
}
|
|
4483
4540
|
|
|
4484
4541
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-normalizer.ts
|
|
4485
|
-
import * as
|
|
4542
|
+
import * as t41 from "@babel/types";
|
|
4486
4543
|
function normalizeDependencyExpr(path8, rootName, ctx) {
|
|
4487
|
-
if (
|
|
4488
|
-
return
|
|
4544
|
+
if (t41.isIdentifier(path8.node)) {
|
|
4545
|
+
return t41.identifier(path8.node.name);
|
|
4489
4546
|
}
|
|
4490
|
-
if (
|
|
4547
|
+
if (t41.isMemberExpression(path8.node) || t41.isOptionalMemberExpression(path8.node)) {
|
|
4491
4548
|
if (rootName === ctx.propField) {
|
|
4492
4549
|
const safePropsExp = ensureOptionalForMemberChain(path8.node);
|
|
4493
4550
|
if (isReactValidDependencyExpr(safePropsExp)) {
|
|
4494
|
-
return
|
|
4551
|
+
return t41.cloneNode(safePropsExp, true);
|
|
4495
4552
|
}
|
|
4496
|
-
return
|
|
4553
|
+
return t41.identifier(rootName);
|
|
4497
4554
|
}
|
|
4498
4555
|
const normalizedExp = normalizeMemberForCallSite(path8, path8.node);
|
|
4499
|
-
const safeExp =
|
|
4556
|
+
const safeExp = t41.isMemberExpression(normalizedExp) || t41.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForMemberChain(normalizedExp) : normalizedExp;
|
|
4500
4557
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
4501
|
-
return
|
|
4558
|
+
return t41.cloneNode(safeExp, true);
|
|
4502
4559
|
}
|
|
4503
|
-
return
|
|
4560
|
+
return t41.identifier(rootName);
|
|
4504
4561
|
}
|
|
4505
4562
|
return null;
|
|
4506
4563
|
}
|
|
4507
4564
|
function normalizeSourcedDependency(exp) {
|
|
4508
|
-
if (
|
|
4509
|
-
return
|
|
4565
|
+
if (t41.isIdentifier(exp)) {
|
|
4566
|
+
return t41.identifier(exp.name);
|
|
4510
4567
|
}
|
|
4511
|
-
if (
|
|
4568
|
+
if (t41.isMemberExpression(exp) || t41.isOptionalMemberExpression(exp)) {
|
|
4512
4569
|
const root = findRootIdentifier(exp);
|
|
4513
4570
|
if (!root) return null;
|
|
4514
|
-
const safeExp =
|
|
4571
|
+
const safeExp = t41.isMemberExpression(exp) || t41.isOptionalMemberExpression(exp) ? ensureOptionalForMemberChain(exp) : exp;
|
|
4515
4572
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
4516
|
-
return
|
|
4573
|
+
return t41.cloneNode(safeExp, true);
|
|
4517
4574
|
}
|
|
4518
|
-
return
|
|
4575
|
+
return t41.identifier(root.name);
|
|
4519
4576
|
}
|
|
4520
4577
|
return null;
|
|
4521
4578
|
}
|
|
@@ -4525,7 +4582,7 @@ function normalizeMemberForCallSite(path8, node) {
|
|
|
4525
4582
|
if (!isDirectCallee) {
|
|
4526
4583
|
return node;
|
|
4527
4584
|
}
|
|
4528
|
-
if (!
|
|
4585
|
+
if (!t41.isExpression(node.object)) {
|
|
4529
4586
|
return node;
|
|
4530
4587
|
}
|
|
4531
4588
|
return node.object;
|
|
@@ -4534,15 +4591,23 @@ function ensureOptionalForMemberChain(node) {
|
|
|
4534
4591
|
if (!hasTrailingMemberAccess(node)) {
|
|
4535
4592
|
return node;
|
|
4536
4593
|
}
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4594
|
+
const rebuildWithOptionalChain = (node2) => {
|
|
4595
|
+
const safeObject = ensureOptionalForMemberChain(node2.object);
|
|
4596
|
+
if (safeObject !== node2.object) {
|
|
4597
|
+
const property = t41.cloneNode(node2.property, true);
|
|
4598
|
+
return t41.optionalMemberExpression(safeObject, property, node2.computed, true);
|
|
4599
|
+
}
|
|
4600
|
+
if (t41.isMemberExpression(node2)) {
|
|
4601
|
+
const object = t41.cloneNode(node2.object, true);
|
|
4602
|
+
const property = t41.cloneNode(node2.property, true);
|
|
4603
|
+
return t41.optionalMemberExpression(object, property, node2.computed, true);
|
|
4604
|
+
}
|
|
4605
|
+
return node2;
|
|
4606
|
+
};
|
|
4607
|
+
return rebuildWithOptionalChain(node);
|
|
4543
4608
|
}
|
|
4544
4609
|
function hasTrailingMemberAccess(node) {
|
|
4545
|
-
return
|
|
4610
|
+
return t41.isMemberExpression(node.object) || t41.isOptionalMemberExpression(node.object);
|
|
4546
4611
|
}
|
|
4547
4612
|
|
|
4548
4613
|
// src/core/transform/sfc/script/shared/dependency-analyzer/shared-utils.ts
|
|
@@ -4557,7 +4622,7 @@ function isNestedMemberObject(path8) {
|
|
|
4557
4622
|
}
|
|
4558
4623
|
|
|
4559
4624
|
// src/core/transform/sfc/script/shared/dependency-analyzer/trace-utils.ts
|
|
4560
|
-
import * as
|
|
4625
|
+
import * as t42 from "@babel/types";
|
|
4561
4626
|
function traceBindingSource(binding, seen, depth) {
|
|
4562
4627
|
if (depth <= 0) return null;
|
|
4563
4628
|
const declaratorPath = getVariableDeclaratorPath(binding.path);
|
|
@@ -4569,7 +4634,7 @@ function traceBindingSource(binding, seen, depth) {
|
|
|
4569
4634
|
}
|
|
4570
4635
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
4571
4636
|
if (depth <= 0) return null;
|
|
4572
|
-
if (
|
|
4637
|
+
if (t42.isIdentifier(exp)) {
|
|
4573
4638
|
const sourceBinding = scope.getBinding(exp.name);
|
|
4574
4639
|
if (!sourceBinding) return null;
|
|
4575
4640
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -4577,13 +4642,13 @@ function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
|
4577
4642
|
}
|
|
4578
4643
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
4579
4644
|
}
|
|
4580
|
-
if (
|
|
4645
|
+
if (t42.isMemberExpression(exp) || t42.isOptionalMemberExpression(exp)) {
|
|
4581
4646
|
const root = findRootIdentifier(exp);
|
|
4582
4647
|
if (!root) return null;
|
|
4583
4648
|
const sourceBinding = scope.getBinding(root.name);
|
|
4584
4649
|
if (!sourceBinding) return null;
|
|
4585
4650
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
4586
|
-
return
|
|
4651
|
+
return t42.cloneNode(exp);
|
|
4587
4652
|
}
|
|
4588
4653
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
4589
4654
|
if (sourcedRoot) {
|
|
@@ -4591,17 +4656,17 @@ function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
|
4591
4656
|
if (rebuilt) {
|
|
4592
4657
|
return rebuilt;
|
|
4593
4658
|
}
|
|
4594
|
-
return
|
|
4659
|
+
return t42.cloneNode(sourcedRoot, true);
|
|
4595
4660
|
}
|
|
4596
4661
|
}
|
|
4597
4662
|
return null;
|
|
4598
4663
|
}
|
|
4599
4664
|
function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
4600
4665
|
const replacedObject = (() => {
|
|
4601
|
-
if (
|
|
4602
|
-
return
|
|
4666
|
+
if (t42.isIdentifier(node.object)) {
|
|
4667
|
+
return t42.cloneNode(nextRoot, true);
|
|
4603
4668
|
}
|
|
4604
|
-
if (
|
|
4669
|
+
if (t42.isMemberExpression(node.object) || t42.isOptionalMemberExpression(node.object)) {
|
|
4605
4670
|
return rebuildMemberWithNewRoot(node.object, nextRoot);
|
|
4606
4671
|
}
|
|
4607
4672
|
return null;
|
|
@@ -4609,15 +4674,15 @@ function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
|
4609
4674
|
if (!replacedObject) {
|
|
4610
4675
|
return null;
|
|
4611
4676
|
}
|
|
4612
|
-
const property =
|
|
4613
|
-
if (
|
|
4614
|
-
return
|
|
4677
|
+
const property = t42.cloneNode(node.property, true);
|
|
4678
|
+
if (t42.isMemberExpression(node)) {
|
|
4679
|
+
return t42.memberExpression(
|
|
4615
4680
|
replacedObject,
|
|
4616
4681
|
property,
|
|
4617
4682
|
node.computed
|
|
4618
4683
|
);
|
|
4619
4684
|
}
|
|
4620
|
-
return
|
|
4685
|
+
return t42.optionalMemberExpression(
|
|
4621
4686
|
replacedObject,
|
|
4622
4687
|
property,
|
|
4623
4688
|
node.computed,
|
|
@@ -4628,9 +4693,9 @@ function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
|
4628
4693
|
// src/core/transform/sfc/script/shared/dependency-analyzer/index.ts
|
|
4629
4694
|
function analyzeDeps(node, ctx, parentPath) {
|
|
4630
4695
|
if (!parentPath) {
|
|
4631
|
-
return
|
|
4696
|
+
return t43.arrayExpression([]);
|
|
4632
4697
|
}
|
|
4633
|
-
const isFnExpr =
|
|
4698
|
+
const isFnExpr = t43.isArrowFunctionExpression(node) || t43.isFunctionExpression(node);
|
|
4634
4699
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
4635
4700
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
4636
4701
|
const deps = /* @__PURE__ */ new Map();
|
|
@@ -4640,13 +4705,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
4640
4705
|
}
|
|
4641
4706
|
const analyzeTargetPath = parentPath && parentPath.node === analyzeTarget ? parentPath : null;
|
|
4642
4707
|
if (analyzeTargetPath) {
|
|
4643
|
-
if (
|
|
4708
|
+
if (t43.isMemberExpression(analyzeTarget) || t43.isOptionalMemberExpression(analyzeTarget)) {
|
|
4644
4709
|
const rootId = findRootIdentifier(analyzeTarget);
|
|
4645
4710
|
if (rootId) {
|
|
4646
4711
|
tryAddDependency(analyzeTargetPath, rootId.name, analyzeTargetPath.scope);
|
|
4647
4712
|
processedIdentifiers.add(rootId);
|
|
4648
4713
|
}
|
|
4649
|
-
} else if (
|
|
4714
|
+
} else if (t43.isIdentifier(analyzeTarget)) {
|
|
4650
4715
|
tryAddDependency(analyzeTargetPath, analyzeTarget.name, analyzeTargetPath.scope);
|
|
4651
4716
|
}
|
|
4652
4717
|
}
|
|
@@ -4698,7 +4763,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
4698
4763
|
}
|
|
4699
4764
|
}
|
|
4700
4765
|
}
|
|
4701
|
-
return
|
|
4766
|
+
return t43.arrayExpression(Array.from(deps.values()));
|
|
4702
4767
|
}
|
|
4703
4768
|
|
|
4704
4769
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
@@ -4714,7 +4779,7 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
4714
4779
|
if (!isVueApiReference(path8, apiName)) {
|
|
4715
4780
|
return;
|
|
4716
4781
|
}
|
|
4717
|
-
if (
|
|
4782
|
+
if (t44.isCallExpression(node)) {
|
|
4718
4783
|
resolveCallNode(path8, adapter, ctx);
|
|
4719
4784
|
} else {
|
|
4720
4785
|
replaceIdName(node, adapter.target);
|
|
@@ -4724,11 +4789,11 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
4724
4789
|
};
|
|
4725
4790
|
}
|
|
4726
4791
|
function getApiName(node) {
|
|
4727
|
-
const isCallNode =
|
|
4792
|
+
const isCallNode = t44.isCallExpression(node);
|
|
4728
4793
|
let apiName = "";
|
|
4729
|
-
if (
|
|
4794
|
+
if (t44.isIdentifier(node)) {
|
|
4730
4795
|
apiName = node.name;
|
|
4731
|
-
} else if (isCallNode &&
|
|
4796
|
+
} else if (isCallNode && t44.isIdentifier(node.callee)) {
|
|
4732
4797
|
apiName = node.callee.name;
|
|
4733
4798
|
}
|
|
4734
4799
|
return apiName;
|
|
@@ -4738,7 +4803,7 @@ function resolveCallNode(path8, adapter, ctx) {
|
|
|
4738
4803
|
const { arguments: args } = node;
|
|
4739
4804
|
if (!args.length) return;
|
|
4740
4805
|
const fn = args[0];
|
|
4741
|
-
if (!
|
|
4806
|
+
if (!t44.isArrowFunctionExpression(fn) && !t44.isFunctionExpression(fn)) {
|
|
4742
4807
|
return;
|
|
4743
4808
|
}
|
|
4744
4809
|
const fnPath = path8.get("arguments")[0];
|
|
@@ -4770,7 +4835,7 @@ function isVueImportBinding(binding) {
|
|
|
4770
4835
|
return false;
|
|
4771
4836
|
}
|
|
4772
4837
|
const parent = bindingPath.parentPath?.node;
|
|
4773
|
-
if (!parent || !
|
|
4838
|
+
if (!parent || !t44.isImportDeclaration(parent)) {
|
|
4774
4839
|
return false;
|
|
4775
4840
|
}
|
|
4776
4841
|
const source = parent.source.value.toLowerCase();
|
|
@@ -4838,7 +4903,7 @@ function isSkip(path8) {
|
|
|
4838
4903
|
}
|
|
4839
4904
|
|
|
4840
4905
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
4841
|
-
import * as
|
|
4906
|
+
import * as t45 from "@babel/types";
|
|
4842
4907
|
function resolveElementRef(ctx) {
|
|
4843
4908
|
return {
|
|
4844
4909
|
CallExpression(path8) {
|
|
@@ -4856,14 +4921,14 @@ function resolveElementRef(ctx) {
|
|
|
4856
4921
|
}
|
|
4857
4922
|
if (isCompRefBindings) {
|
|
4858
4923
|
const varDeclaratorPath = getVariableDeclaratorPath(path8)?.node;
|
|
4859
|
-
if (!
|
|
4924
|
+
if (!t45.isIdentifier(varDeclaratorPath?.id)) {
|
|
4860
4925
|
return;
|
|
4861
4926
|
}
|
|
4862
4927
|
const varName = varDeclaratorPath.id.name;
|
|
4863
4928
|
const compRef = refBindings.componentRefs[varName];
|
|
4864
4929
|
if (!compRef) return;
|
|
4865
4930
|
}
|
|
4866
|
-
node.arguments[0] =
|
|
4931
|
+
node.arguments[0] = t45.identifier("null");
|
|
4867
4932
|
resolveTypeParameters(ctx, path8);
|
|
4868
4933
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
4869
4934
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
@@ -4888,27 +4953,27 @@ function resolveTypeParameters(ctx, path8) {
|
|
|
4888
4953
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
4889
4954
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
4890
4955
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
4891
|
-
node.typeParameters =
|
|
4956
|
+
node.typeParameters = t45.tsTypeParameterInstantiation([t45.tsTypeReference(t45.identifier(type))]);
|
|
4892
4957
|
}
|
|
4893
4958
|
}
|
|
4894
4959
|
function resolveRefValueToCurrent(path8) {
|
|
4895
4960
|
const { node } = path8;
|
|
4896
|
-
if (node.computed || !
|
|
4961
|
+
if (node.computed || !t45.isIdentifier(node.property) || node.property.name !== "value") {
|
|
4897
4962
|
return;
|
|
4898
4963
|
}
|
|
4899
4964
|
const rootPath = findRootVariablePath(path8);
|
|
4900
|
-
if (!rootPath?.node || !
|
|
4965
|
+
if (!rootPath?.node || !t45.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
4901
4966
|
return;
|
|
4902
4967
|
}
|
|
4903
4968
|
const rootId = findRootIdentifier(node);
|
|
4904
|
-
if (!
|
|
4969
|
+
if (!t45.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
4905
4970
|
return;
|
|
4906
4971
|
}
|
|
4907
4972
|
node.property.name = "current";
|
|
4908
4973
|
}
|
|
4909
4974
|
|
|
4910
4975
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
4911
|
-
import * as
|
|
4976
|
+
import * as t46 from "@babel/types";
|
|
4912
4977
|
function resolveExprMemo(ctx, ast) {
|
|
4913
4978
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
4914
4979
|
return {
|
|
@@ -4920,11 +4985,11 @@ function resolveExprMemo(ctx, ast) {
|
|
|
4920
4985
|
if (!atComponentOrHookRoot(path8, ast.program, isScriptFile)) {
|
|
4921
4986
|
return false;
|
|
4922
4987
|
}
|
|
4923
|
-
if (!
|
|
4988
|
+
if (!t46.isVariableDeclaration(path8.parent) || path8.parent.kind !== "const") {
|
|
4924
4989
|
return false;
|
|
4925
4990
|
}
|
|
4926
|
-
if (
|
|
4927
|
-
if (
|
|
4991
|
+
if (t46.isFunction(init)) return false;
|
|
4992
|
+
if (t46.isCallExpression(init) && t46.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
4928
4993
|
return false;
|
|
4929
4994
|
}
|
|
4930
4995
|
return true;
|
|
@@ -4943,16 +5008,16 @@ function resolveExprMemo(ctx, ast) {
|
|
|
4943
5008
|
}
|
|
4944
5009
|
|
|
4945
5010
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
4946
|
-
import * as
|
|
5011
|
+
import * as t47 from "@babel/types";
|
|
4947
5012
|
function resolveLintRules(ctx, ast) {
|
|
4948
5013
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
4949
5014
|
return {
|
|
4950
5015
|
CallExpression(path8) {
|
|
4951
5016
|
const { node, parentPath } = path8;
|
|
4952
|
-
if (!
|
|
5017
|
+
if (!t47.isIdentifier(node.callee)) return;
|
|
4953
5018
|
const { name: callName } = node.callee;
|
|
4954
|
-
const addLog = (
|
|
4955
|
-
logger.error(
|
|
5019
|
+
const addLog = (t53) => {
|
|
5020
|
+
logger.error(t53, {
|
|
4956
5021
|
file: ctx.filename,
|
|
4957
5022
|
source: ctx.scriptData.source,
|
|
4958
5023
|
loc: node.loc
|
|
@@ -4997,16 +5062,27 @@ function resolveLintRules(ctx, ast) {
|
|
|
4997
5062
|
|
|
4998
5063
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
4999
5064
|
import { generate as generate2 } from "@babel/generator";
|
|
5000
|
-
import * as
|
|
5065
|
+
import * as t48 from "@babel/types";
|
|
5001
5066
|
function resolveProvide(ctx) {
|
|
5002
|
-
|
|
5067
|
+
const { filename, scriptData, inputType } = ctx;
|
|
5068
|
+
if (inputType !== "sfc") {
|
|
5069
|
+
return {};
|
|
5070
|
+
}
|
|
5003
5071
|
return {
|
|
5004
5072
|
CallExpression(path8) {
|
|
5005
5073
|
const { node } = path8;
|
|
5074
|
+
if (inputType.startsWith("script")) {
|
|
5075
|
+
logger.error("provide() call found outside of SFC <script setup> context.", {
|
|
5076
|
+
file: filename,
|
|
5077
|
+
source: scriptData.source,
|
|
5078
|
+
loc: node.loc
|
|
5079
|
+
});
|
|
5080
|
+
return;
|
|
5081
|
+
}
|
|
5006
5082
|
const providerTarget = ADAPTER_RULES.runtime[VUE_API_MAP.provide]?.target;
|
|
5007
5083
|
const isProvideCall = isCalleeNamed(node, VUE_API_MAP.provide) || providerTarget && isCalleeNamed(node, providerTarget);
|
|
5008
5084
|
if (!isProvideCall) return;
|
|
5009
|
-
const { provide } =
|
|
5085
|
+
const { provide } = scriptData;
|
|
5010
5086
|
const [key, value] = node.arguments;
|
|
5011
5087
|
const target = findOrCreateCtxProvider(provide);
|
|
5012
5088
|
const adapter = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
@@ -5029,13 +5105,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
5029
5105
|
function assignProviderValue(target, key, value) {
|
|
5030
5106
|
const getRawExp = (exp) => {
|
|
5031
5107
|
if (!exp) return "''";
|
|
5032
|
-
if (
|
|
5108
|
+
if (t48.isStringLiteral(exp)) {
|
|
5033
5109
|
return JSON.stringify(exp.value);
|
|
5034
5110
|
}
|
|
5035
|
-
if (
|
|
5111
|
+
if (t48.isNumericLiteral(exp)) {
|
|
5036
5112
|
return exp.value.toString();
|
|
5037
5113
|
}
|
|
5038
|
-
if (
|
|
5114
|
+
if (t48.isIdentifier(exp)) {
|
|
5039
5115
|
return exp.name;
|
|
5040
5116
|
}
|
|
5041
5117
|
try {
|
|
@@ -5051,16 +5127,16 @@ function assignProviderValue(target, key, value) {
|
|
|
5051
5127
|
}
|
|
5052
5128
|
|
|
5053
5129
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
5054
|
-
import * as
|
|
5130
|
+
import * as t49 from "@babel/types";
|
|
5055
5131
|
function resolveRenameAdapter(ctx) {
|
|
5056
5132
|
return {
|
|
5057
5133
|
"CallExpression|Identifier"(path8) {
|
|
5058
5134
|
const node = path8.node;
|
|
5059
|
-
const isCallNode =
|
|
5135
|
+
const isCallNode = t49.isCallExpression(node);
|
|
5060
5136
|
let apiName = "";
|
|
5061
|
-
if (
|
|
5137
|
+
if (t49.isIdentifier(node)) {
|
|
5062
5138
|
apiName = node.name;
|
|
5063
|
-
} else if (isCallNode &&
|
|
5139
|
+
} else if (isCallNode && t49.isIdentifier(node.callee)) {
|
|
5064
5140
|
apiName = node.callee.name;
|
|
5065
5141
|
}
|
|
5066
5142
|
if (!apiName) {
|
|
@@ -5122,7 +5198,7 @@ function isVueImportBinding2(binding) {
|
|
|
5122
5198
|
return false;
|
|
5123
5199
|
}
|
|
5124
5200
|
const parent = bindingPath.parentPath?.node;
|
|
5125
|
-
if (!parent || !
|
|
5201
|
+
if (!parent || !t49.isImportDeclaration(parent)) {
|
|
5126
5202
|
return false;
|
|
5127
5203
|
}
|
|
5128
5204
|
const source = parent.source.value.toLowerCase();
|
|
@@ -5153,11 +5229,10 @@ function processVueSyntax2(ast, ctx) {
|
|
|
5153
5229
|
process: {
|
|
5154
5230
|
applyBabel: [
|
|
5155
5231
|
resolveElementRef,
|
|
5156
|
-
// provide 需要在 rename 之前收集并移除原始调用,避免被重命名后失配
|
|
5157
|
-
resolveProvide,
|
|
5158
5232
|
resolveRenameAdapter,
|
|
5159
|
-
// fix
|
|
5160
|
-
|
|
5233
|
+
// fix: https://github.com/vureact-js/core/issues/46
|
|
5234
|
+
resolveProvide,
|
|
5235
|
+
// fix:在分析函数前分析可优化为 useMemo 的顶层变量声明,使得后续能够被函数依赖分析
|
|
5161
5236
|
resolveExprMemo,
|
|
5162
5237
|
resolveArrowFnDeps,
|
|
5163
5238
|
resolveAnalysisOnlyAdapter,
|
|
@@ -5166,7 +5241,12 @@ function processVueSyntax2(ast, ctx) {
|
|
|
5166
5241
|
excludeBabel: [resolveTemplateSlotIface, resolveCompIProps]
|
|
5167
5242
|
},
|
|
5168
5243
|
postprocess: {
|
|
5169
|
-
applyBabel: [
|
|
5244
|
+
applyBabel: [
|
|
5245
|
+
// 该 resolver 需确保放在所有类型处理之后,移除之前
|
|
5246
|
+
resolveVueTypeAsAny,
|
|
5247
|
+
resolveRuntimeImports,
|
|
5248
|
+
resolveASTChunks
|
|
5249
|
+
],
|
|
5170
5250
|
excludeBabel: [resolveSfcCssImport]
|
|
5171
5251
|
}
|
|
5172
5252
|
});
|
|
@@ -5259,13 +5339,13 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
5259
5339
|
}
|
|
5260
5340
|
|
|
5261
5341
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
5262
|
-
import * as
|
|
5342
|
+
import * as t51 from "@babel/types";
|
|
5263
5343
|
|
|
5264
5344
|
// src/shared/string-code-types.ts
|
|
5265
5345
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
5266
|
-
import * as
|
|
5346
|
+
import * as t50 from "@babel/types";
|
|
5267
5347
|
var strCodeTypes = {
|
|
5268
|
-
isIdentifier:
|
|
5348
|
+
isIdentifier: isIdentifier29,
|
|
5269
5349
|
isSimpleExpression,
|
|
5270
5350
|
isStringLiteral: isStringLiteral13
|
|
5271
5351
|
};
|
|
@@ -5276,30 +5356,30 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
5276
5356
|
} catch {
|
|
5277
5357
|
return false;
|
|
5278
5358
|
}
|
|
5279
|
-
if (
|
|
5359
|
+
if (t50.isLiteral(node)) {
|
|
5280
5360
|
return true;
|
|
5281
5361
|
}
|
|
5282
|
-
if (!excludeVar &&
|
|
5362
|
+
if (!excludeVar && t50.isIdentifier(node)) {
|
|
5283
5363
|
return true;
|
|
5284
5364
|
}
|
|
5285
|
-
if (
|
|
5286
|
-
return isSimpleExpression(node.object) &&
|
|
5365
|
+
if (t50.isMemberExpression(node)) {
|
|
5366
|
+
return isSimpleExpression(node.object) && t50.isIdentifier(node.property);
|
|
5287
5367
|
}
|
|
5288
|
-
if (
|
|
5368
|
+
if (t50.isObjectExpression(node) || t50.isArrayExpression(node)) {
|
|
5289
5369
|
return false;
|
|
5290
5370
|
}
|
|
5291
|
-
if (
|
|
5371
|
+
if (t50.isCallExpression(node) || t50.isAssignmentExpression(node)) {
|
|
5292
5372
|
return false;
|
|
5293
5373
|
}
|
|
5294
|
-
if (
|
|
5374
|
+
if (t50.isBinaryExpression(node) || t50.isUnaryExpression(node)) {
|
|
5295
5375
|
return true;
|
|
5296
5376
|
}
|
|
5297
5377
|
return false;
|
|
5298
5378
|
}
|
|
5299
|
-
function
|
|
5379
|
+
function isIdentifier29(code) {
|
|
5300
5380
|
try {
|
|
5301
5381
|
const node = parseExpression3(code);
|
|
5302
|
-
return
|
|
5382
|
+
return t50.isIdentifier(node);
|
|
5303
5383
|
} catch {
|
|
5304
5384
|
return false;
|
|
5305
5385
|
}
|
|
@@ -5307,7 +5387,7 @@ function isIdentifier28(code) {
|
|
|
5307
5387
|
function isStringLiteral13(code) {
|
|
5308
5388
|
try {
|
|
5309
5389
|
const node = parseExpression3(code);
|
|
5310
|
-
return
|
|
5390
|
+
return t50.isStringLiteral(node);
|
|
5311
5391
|
} catch {
|
|
5312
5392
|
return false;
|
|
5313
5393
|
}
|
|
@@ -5447,9 +5527,10 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
5447
5527
|
const value = ir.value;
|
|
5448
5528
|
const valueContent = value.content;
|
|
5449
5529
|
const mergedItems = value.merge;
|
|
5530
|
+
const rule = ADAPTER_RULES.runtime;
|
|
5450
5531
|
const setNameIdentifier = (target, valueName) => {
|
|
5451
5532
|
target.content = valueName;
|
|
5452
|
-
target.ast =
|
|
5533
|
+
target.ast = t51.jsxIdentifier(valueName);
|
|
5453
5534
|
};
|
|
5454
5535
|
const setValueExpression = (target, content, isStringLiteral14) => {
|
|
5455
5536
|
target.content = content;
|
|
@@ -5457,38 +5538,41 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
5457
5538
|
};
|
|
5458
5539
|
const createRuntimeCall = (fnName, args) => {
|
|
5459
5540
|
const fnArgs = args.filter(Boolean).join(",");
|
|
5460
|
-
|
|
5541
|
+
const valIsUndef = fnName === rule.dirOn.target && args?.[1] === "undefined";
|
|
5542
|
+
const isTs = ctx.scriptData.lang.startsWith("ts");
|
|
5543
|
+
const safeTypeAssertion = isTs ? valIsUndef ? "as never" : "" : "";
|
|
5544
|
+
return `${fnName}(${fnArgs}) ${safeTypeAssertion}`;
|
|
5461
5545
|
};
|
|
5462
5546
|
const applyRuntimeExpression = (expression, setName = false, nameIdentifier, isStringLiteral14) => {
|
|
5463
5547
|
if (setName && nameIdentifier) {
|
|
5464
5548
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
5465
5549
|
}
|
|
5466
|
-
const dir =
|
|
5550
|
+
const dir = rule.dir;
|
|
5467
5551
|
recordImport(ctx, dir.package, dir.target);
|
|
5468
5552
|
setValueExpression(value.babelExp, expression, isStringLiteral14);
|
|
5469
5553
|
};
|
|
5470
5554
|
if (ir.isKeyLessVBind) {
|
|
5471
|
-
const dirKeyless =
|
|
5555
|
+
const dirKeyless = rule.dirKeyless;
|
|
5472
5556
|
const expression = createRuntimeCall(dirKeyless.target, [valueContent]);
|
|
5473
5557
|
applyRuntimeExpression(expression, false);
|
|
5474
5558
|
return;
|
|
5475
5559
|
}
|
|
5476
5560
|
if (isClassAttr(name) && !value.isStringLiteral && !valueContent.startsWith(STYLE_MODULE_NAME)) {
|
|
5477
|
-
const dirCls =
|
|
5561
|
+
const dirCls = rule.dirCls;
|
|
5478
5562
|
const arg = mergedItems?.join(",") || wrapSingleQuotes(valueContent);
|
|
5479
5563
|
const expression = createRuntimeCall(dirCls.target, [arg]);
|
|
5480
5564
|
applyRuntimeExpression(expression, true, name);
|
|
5481
5565
|
return;
|
|
5482
5566
|
}
|
|
5483
5567
|
if (isStyleAttr(name) && (!isSimpleStyle(valueContent) || mergedItems?.some((item) => !isSimpleStyle(item)))) {
|
|
5484
|
-
const dirStyle =
|
|
5568
|
+
const dirStyle = rule.dirStyle;
|
|
5485
5569
|
const arg = mergedItems?.join(",") || valueContent;
|
|
5486
5570
|
const expression = createRuntimeCall(dirStyle.target, [arg]);
|
|
5487
5571
|
applyRuntimeExpression(expression, true, name);
|
|
5488
5572
|
return;
|
|
5489
5573
|
}
|
|
5490
5574
|
if (ir.type === 3 /* EVENT */ && ir.modifiers?.length) {
|
|
5491
|
-
const dirOn =
|
|
5575
|
+
const dirOn = rule.dirOn;
|
|
5492
5576
|
const eventName = wrapSingleQuotes(ir.__vOnEvName || name, ir.isStatic);
|
|
5493
5577
|
const expression = createRuntimeCall(dirOn.target, [eventName, valueContent]);
|
|
5494
5578
|
applyRuntimeExpression(expression, true, name);
|
|
@@ -6196,14 +6280,15 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
6196
6280
|
}
|
|
6197
6281
|
|
|
6198
6282
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
6199
|
-
import * as
|
|
6283
|
+
import * as t52 from "@babel/types";
|
|
6200
6284
|
function resolveVOn(directive, ir, ctx, nodeIR) {
|
|
6201
6285
|
const arg = directive.arg;
|
|
6202
6286
|
const exp = directive.exp;
|
|
6203
6287
|
const modifiers = directive.modifiers.map((item) => item.content);
|
|
6204
6288
|
const captureIndex = resolveCaptureModifier(modifiers);
|
|
6205
6289
|
const eventName = resolveEventName(arg.content, captureIndex);
|
|
6206
|
-
const
|
|
6290
|
+
const handlerContent = exp?.content?.trim() || "undefined";
|
|
6291
|
+
const handler = resolveHandler(handlerContent, ctx, modifiers);
|
|
6207
6292
|
const originalVueEventName = modifiers.length ? `${arg.content}.${modifiers.join(".")}` : "";
|
|
6208
6293
|
const eventIR = createPropsIR(directive.rawName, eventName, handler);
|
|
6209
6294
|
eventIR.type = 3 /* EVENT */;
|
|
@@ -6253,10 +6338,10 @@ function resolveHandler(handlerContent, ctx, modifiers) {
|
|
|
6253
6338
|
return handler;
|
|
6254
6339
|
}
|
|
6255
6340
|
function isConsoleCall(expr) {
|
|
6256
|
-
return
|
|
6341
|
+
return t52.isCallExpression(expr) && t52.isMemberExpression(expr.callee) && t52.isIdentifier(expr.callee.object) && expr.callee.object.name === "console";
|
|
6257
6342
|
}
|
|
6258
6343
|
function isFnReference(expr) {
|
|
6259
|
-
return
|
|
6344
|
+
return t52.isIdentifier(expr) || t52.isMemberExpression(expr) || t52.isFunction(expr);
|
|
6260
6345
|
}
|
|
6261
6346
|
|
|
6262
6347
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-show.ts
|
|
@@ -6488,11 +6573,8 @@ function resolveSlotOutletNode(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
|
6488
6573
|
function resolveSlotProps(node, ctx) {
|
|
6489
6574
|
const result = {
|
|
6490
6575
|
name: "children",
|
|
6491
|
-
// 默认插槽名为'children'
|
|
6492
6576
|
isScope: false,
|
|
6493
|
-
// 默认非作用域插槽
|
|
6494
6577
|
props: []
|
|
6495
|
-
// 作用域参数列表
|
|
6496
6578
|
};
|
|
6497
6579
|
const { source, filename } = ctx;
|
|
6498
6580
|
const addSlotProp = (key, value) => {
|
|
@@ -6516,9 +6598,12 @@ function resolveSlotProps(node, ctx) {
|
|
|
6516
6598
|
continue;
|
|
6517
6599
|
}
|
|
6518
6600
|
if (prop.type === NodeTypes9.DIRECTIVE) {
|
|
6601
|
+
warnUnsupportedVueDollarVar(ctx, prop);
|
|
6602
|
+
if (!prop.arg) {
|
|
6603
|
+
continue;
|
|
6604
|
+
}
|
|
6519
6605
|
const arg = prop.arg;
|
|
6520
6606
|
const exp = prop.exp;
|
|
6521
|
-
warnUnsupportedVueDollarVar(ctx, prop);
|
|
6522
6607
|
if (!arg.isStatic) {
|
|
6523
6608
|
logger.warn("Avoid using dynamic slot names, as they may lead to unexpected behavior.", {
|
|
6524
6609
|
source,
|
|
@@ -6527,7 +6612,7 @@ function resolveSlotProps(node, ctx) {
|
|
|
6527
6612
|
});
|
|
6528
6613
|
}
|
|
6529
6614
|
const key = arg.content;
|
|
6530
|
-
const value = exp
|
|
6615
|
+
const value = exp?.content?.trim() || "undefined";
|
|
6531
6616
|
if (key === "name") {
|
|
6532
6617
|
result.name = camelCase(value);
|
|
6533
6618
|
} else {
|
|
@@ -6609,7 +6694,7 @@ function transform(ast, ctx, options) {
|
|
|
6609
6694
|
}
|
|
6610
6695
|
|
|
6611
6696
|
// package.json
|
|
6612
|
-
var version = "1.8.
|
|
6697
|
+
var version = "1.8.5";
|
|
6613
6698
|
var bin = {
|
|
6614
6699
|
vureact: "bin/vureact.js"
|
|
6615
6700
|
};
|