@vureact/compiler-core 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -4
- package/README.zh-CN.md +2 -3
- package/lib/{chunk-4DEDEJYM.js → chunk-AOWJPHTS.js} +769 -596
- package/lib/{chunk-MBEPCMRP.esm.js → chunk-ZTNVNBKF.esm.js} +681 -508
- package/lib/cli.esm.js +2 -2
- package/lib/cli.js +10 -10
- package/lib/compiler-core.d.cts +18 -3
- package/lib/compiler-core.d.ts +18 -3
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vureact/compiler-core v1.
|
|
2
|
+
* @vureact/compiler-core v1.10.0
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -49,7 +49,8 @@ var MACRO_API_NAMES = {
|
|
|
49
49
|
slots: "defineSlots",
|
|
50
50
|
options: "defineOptions",
|
|
51
51
|
expose: "defineExpose",
|
|
52
|
-
model: "defineModel"
|
|
52
|
+
model: "defineModel",
|
|
53
|
+
defaults: "withDefaults"
|
|
53
54
|
};
|
|
54
55
|
var DOLLAR_IDENTIFIERS = [
|
|
55
56
|
"$data",
|
|
@@ -403,6 +404,11 @@ var ADAPTER_RULES = {
|
|
|
403
404
|
}
|
|
404
405
|
}
|
|
405
406
|
};
|
|
407
|
+
var AUTO_IMPORTED_APIS = new Set(
|
|
408
|
+
Object.entries(ADAPTER_RULES).flatMap(
|
|
409
|
+
([_, rules]) => Object.keys(rules).filter((key) => !key.startsWith("dir"))
|
|
410
|
+
)
|
|
411
|
+
);
|
|
406
412
|
|
|
407
413
|
// src/consts/vue-api-map.ts
|
|
408
414
|
var VUE_API_MAP = {
|
|
@@ -1061,7 +1067,7 @@ function buildStandardProp(nodeIR) {
|
|
|
1061
1067
|
babelExp: { ast: keyAST },
|
|
1062
1068
|
value: {
|
|
1063
1069
|
content,
|
|
1064
|
-
isStringLiteral:
|
|
1070
|
+
isStringLiteral: isStringLiteral16,
|
|
1065
1071
|
babelExp: { ast: valueAST }
|
|
1066
1072
|
}
|
|
1067
1073
|
} = nodeIR;
|
|
@@ -1070,7 +1076,7 @@ function buildStandardProp(nodeIR) {
|
|
|
1070
1076
|
}
|
|
1071
1077
|
let value;
|
|
1072
1078
|
if (content !== "true") {
|
|
1073
|
-
value =
|
|
1079
|
+
value = isStringLiteral16 ? t9.stringLiteral(content) : buildJsxExpressionNode(valueAST);
|
|
1074
1080
|
}
|
|
1075
1081
|
return t9.jsxAttribute(keyAST, value);
|
|
1076
1082
|
}
|
|
@@ -2282,7 +2288,7 @@ function resolveReactiveBindings(node, ctx) {
|
|
|
2282
2288
|
source: callName,
|
|
2283
2289
|
reactiveType: getReactiveType(callName)
|
|
2284
2290
|
};
|
|
2285
|
-
if (callName === MACRO_API_NAMES.props) {
|
|
2291
|
+
if (callName === MACRO_API_NAMES.props || callName === MACRO_API_NAMES.defaults) {
|
|
2286
2292
|
ctx.propField = varName;
|
|
2287
2293
|
}
|
|
2288
2294
|
}
|
|
@@ -3302,8 +3308,236 @@ function isVueImport(id, path8) {
|
|
|
3302
3308
|
return VUE_PACKAGES.some((pkg) => moduleName.includes(pkg));
|
|
3303
3309
|
}
|
|
3304
3310
|
|
|
3305
|
-
// src/core/transform/sfc/script/syntax-processor/
|
|
3311
|
+
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-with-defaults.ts
|
|
3312
|
+
import * as t28 from "@babel/types";
|
|
3313
|
+
|
|
3314
|
+
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
3306
3315
|
import * as t26 from "@babel/types";
|
|
3316
|
+
function createUseCallback(body, deps) {
|
|
3317
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useCallback), [
|
|
3318
|
+
body,
|
|
3319
|
+
deps ?? t26.arrayExpression([])
|
|
3320
|
+
]);
|
|
3321
|
+
}
|
|
3322
|
+
function createUseMemo(body, deps) {
|
|
3323
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useMemo), [
|
|
3324
|
+
t26.arrowFunctionExpression([], body),
|
|
3325
|
+
deps ?? t26.arrayExpression([])
|
|
3326
|
+
]);
|
|
3327
|
+
}
|
|
3328
|
+
function createUseImperativeHandle(refId, init) {
|
|
3329
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
3330
|
+
}
|
|
3331
|
+
function createUseUpdated(body, deps) {
|
|
3332
|
+
const adapter = ADAPTER_RULES.runtime.onUpdated;
|
|
3333
|
+
return t26.callExpression(t26.identifier(adapter.target), [
|
|
3334
|
+
t26.arrowFunctionExpression([], body),
|
|
3335
|
+
deps ?? t26.identifier("undefined")
|
|
3336
|
+
]);
|
|
3337
|
+
}
|
|
3338
|
+
|
|
3339
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-with-defaults.ts
|
|
3340
|
+
import * as t27 from "@babel/types";
|
|
3341
|
+
var WITH_DEFAULTS_PLACEHOLDER = " from withDefaults ";
|
|
3342
|
+
function resolveWithDefaultsOptions(ctx, ast) {
|
|
3343
|
+
if (ctx.inputType !== "sfc") return {};
|
|
3344
|
+
return {
|
|
3345
|
+
CallExpression(path8) {
|
|
3346
|
+
const { node } = path8;
|
|
3347
|
+
const { filename, scriptData } = ctx;
|
|
3348
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.defaults)) {
|
|
3349
|
+
return;
|
|
3350
|
+
}
|
|
3351
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3352
|
+
if (!declaratorPath) {
|
|
3353
|
+
logger.error(
|
|
3354
|
+
`withDefaults() must be assigned to a variable (e.g., const props = withDefaults(...)).`,
|
|
3355
|
+
{ file: filename, source: scriptData.source, loc: node.loc }
|
|
3356
|
+
);
|
|
3357
|
+
return;
|
|
3358
|
+
}
|
|
3359
|
+
const varName = t27.isIdentifier(declaratorPath.node.id) ? declaratorPath.node.id.name : void 0;
|
|
3360
|
+
if (!varName) {
|
|
3361
|
+
logger.error("withDefaults() could not determine the variable name from the declaration.", {
|
|
3362
|
+
file: filename,
|
|
3363
|
+
source: scriptData.source,
|
|
3364
|
+
loc: node.loc
|
|
3365
|
+
});
|
|
3366
|
+
return;
|
|
3367
|
+
}
|
|
3368
|
+
ctx.propField = `vr${capitalize(ctx.propField)}`;
|
|
3369
|
+
const [defineProps, defaults] = node.arguments;
|
|
3370
|
+
if (!node.arguments.length) {
|
|
3371
|
+
logger.error("withDefaults() requires at least one argument (defineProps call).", {
|
|
3372
|
+
file: filename,
|
|
3373
|
+
source: scriptData.source,
|
|
3374
|
+
loc: node.loc
|
|
3375
|
+
});
|
|
3376
|
+
return;
|
|
3377
|
+
}
|
|
3378
|
+
if (!t27.isCallExpression(defineProps)) {
|
|
3379
|
+
logger.error(
|
|
3380
|
+
"withDefaults() first argument must be a call to defineProps (e.g., defineProps({...})).",
|
|
3381
|
+
{ file: filename, source: scriptData.source, loc: defineProps?.loc }
|
|
3382
|
+
);
|
|
3383
|
+
return;
|
|
3384
|
+
}
|
|
3385
|
+
if (defaults && !t27.isObjectExpression(defaults)) {
|
|
3386
|
+
logger.error(
|
|
3387
|
+
'withDefaults() second argument must be an object literal (e.g., { msg: "hello" }).',
|
|
3388
|
+
{ file: filename, source: scriptData.source, loc: defaults?.loc }
|
|
3389
|
+
);
|
|
3390
|
+
return;
|
|
3391
|
+
}
|
|
3392
|
+
recordPropsWithDefaults(ctx, varName, defineProps, defaults, node);
|
|
3393
|
+
declaratorPath.parentPath.insertAfter(createPlaceholder(node));
|
|
3394
|
+
replaceNode(path8, defineProps, node);
|
|
3395
|
+
}
|
|
3396
|
+
};
|
|
3397
|
+
}
|
|
3398
|
+
function recordPropsWithDefaults(ctx, varName, defineProps, defaults, withDefaults) {
|
|
3399
|
+
const { scriptData } = ctx;
|
|
3400
|
+
const { start, end, loc, leadingComments, innerComments, trailingComments } = withDefaults;
|
|
3401
|
+
const values = flattenDefaultValues(defaults);
|
|
3402
|
+
const getTypeParameters = () => {
|
|
3403
|
+
if (!scriptData.lang.startsWith("ts")) {
|
|
3404
|
+
return;
|
|
3405
|
+
}
|
|
3406
|
+
const { propsTypes } = scriptData.propsTSIface;
|
|
3407
|
+
const typeParameters = defineProps.typeParameters || (propsTypes.length ? t27.tSTypeParameterInstantiation(propsTypes) : void 0);
|
|
3408
|
+
if (typeParameters) {
|
|
3409
|
+
return t27.tsTypeParameterInstantiation([
|
|
3410
|
+
t27.tsTypeReference(t27.identifier("Readonly"), typeParameters)
|
|
3411
|
+
]);
|
|
3412
|
+
}
|
|
3413
|
+
};
|
|
3414
|
+
scriptData.propsWithDefaults = {
|
|
3415
|
+
varName,
|
|
3416
|
+
values,
|
|
3417
|
+
start,
|
|
3418
|
+
end,
|
|
3419
|
+
loc,
|
|
3420
|
+
leadingComments,
|
|
3421
|
+
innerComments,
|
|
3422
|
+
trailingComments,
|
|
3423
|
+
typeParameters: getTypeParameters()
|
|
3424
|
+
};
|
|
3425
|
+
}
|
|
3426
|
+
function flattenDefaultValues(expr) {
|
|
3427
|
+
if (!expr) return;
|
|
3428
|
+
if (!t27.isObjectExpression(expr)) {
|
|
3429
|
+
return expr;
|
|
3430
|
+
}
|
|
3431
|
+
const properties = expr.properties.map((prop) => {
|
|
3432
|
+
if (!t27.isObjectProperty(prop)) {
|
|
3433
|
+
return prop;
|
|
3434
|
+
}
|
|
3435
|
+
const value = prop.value;
|
|
3436
|
+
if (t27.isArrowFunctionExpression(value) && t27.isBlockStatement(value.body)) {
|
|
3437
|
+
const returnStmt = value.body.body.find(
|
|
3438
|
+
(stmt) => t27.isReturnStatement(stmt)
|
|
3439
|
+
);
|
|
3440
|
+
if (returnStmt?.argument) {
|
|
3441
|
+
return t27.objectProperty(prop.key, returnStmt.argument, prop.computed);
|
|
3442
|
+
}
|
|
3443
|
+
}
|
|
3444
|
+
if (t27.isArrowFunctionExpression(value) && !t27.isBlockStatement(value.body)) {
|
|
3445
|
+
return t27.objectProperty(prop.key, value.body, prop.computed);
|
|
3446
|
+
}
|
|
3447
|
+
return prop;
|
|
3448
|
+
});
|
|
3449
|
+
return t27.objectExpression(properties);
|
|
3450
|
+
}
|
|
3451
|
+
function createPlaceholder(node) {
|
|
3452
|
+
const placeholder = t27.emptyStatement();
|
|
3453
|
+
const leadingComments = [
|
|
3454
|
+
{
|
|
3455
|
+
type: "CommentBlock",
|
|
3456
|
+
value: WITH_DEFAULTS_PLACEHOLDER,
|
|
3457
|
+
start: node.start,
|
|
3458
|
+
end: node.end
|
|
3459
|
+
}
|
|
3460
|
+
];
|
|
3461
|
+
placeholder.leadingComments = leadingComments;
|
|
3462
|
+
return placeholder;
|
|
3463
|
+
}
|
|
3464
|
+
|
|
3465
|
+
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-with-defaults.ts
|
|
3466
|
+
function resolveWithDefaults(ctx) {
|
|
3467
|
+
const { inputType, scriptData } = ctx;
|
|
3468
|
+
const propsWithDefaults = scriptData.propsWithDefaults;
|
|
3469
|
+
if (inputType !== "sfc" || !propsWithDefaults?.values) return {};
|
|
3470
|
+
return {
|
|
3471
|
+
Program: {
|
|
3472
|
+
exit(programPath) {
|
|
3473
|
+
const { body } = programPath.node;
|
|
3474
|
+
const placeholderIndex = findPlaceholderIndex(body);
|
|
3475
|
+
if (placeholderIndex === -1) return;
|
|
3476
|
+
const placeholder = body[placeholderIndex];
|
|
3477
|
+
const { varName, values, typeParameters } = propsWithDefaults;
|
|
3478
|
+
const varDeclaration = createVarDeclaration(
|
|
3479
|
+
varName,
|
|
3480
|
+
values,
|
|
3481
|
+
typeParameters,
|
|
3482
|
+
ctx.propField,
|
|
3483
|
+
propsWithDefaults
|
|
3484
|
+
);
|
|
3485
|
+
varDeclaration.leadingComments = placeholder.leadingComments;
|
|
3486
|
+
body.splice(placeholderIndex, 0, varDeclaration);
|
|
3487
|
+
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useMemo);
|
|
3488
|
+
}
|
|
3489
|
+
}
|
|
3490
|
+
};
|
|
3491
|
+
}
|
|
3492
|
+
function findPlaceholderIndex(body) {
|
|
3493
|
+
return body.findIndex((stmt) => {
|
|
3494
|
+
if (!t28.isEmptyStatement(stmt) || !stmt.leadingComments) {
|
|
3495
|
+
return false;
|
|
3496
|
+
}
|
|
3497
|
+
return stmt.leadingComments.some(
|
|
3498
|
+
(comment) => comment.type === "CommentBlock" && comment.value === WITH_DEFAULTS_PLACEHOLDER
|
|
3499
|
+
);
|
|
3500
|
+
});
|
|
3501
|
+
}
|
|
3502
|
+
function createVarDeclaration(varName, values, typeParameters, propField, sourceInfo) {
|
|
3503
|
+
const { mergeMembers } = buildMergeLogic(values, propField);
|
|
3504
|
+
const deps = t28.arrayExpression([t28.identifier(propField)]);
|
|
3505
|
+
const useMemoCall = createUseMemo(t28.objectExpression(mergeMembers), deps);
|
|
3506
|
+
if (typeParameters) {
|
|
3507
|
+
useMemoCall.typeParameters = typeParameters;
|
|
3508
|
+
}
|
|
3509
|
+
useMemoCall.start = sourceInfo.start;
|
|
3510
|
+
useMemoCall.end = sourceInfo.end;
|
|
3511
|
+
useMemoCall.loc = sourceInfo.loc;
|
|
3512
|
+
useMemoCall.leadingComments = sourceInfo.leadingComments;
|
|
3513
|
+
useMemoCall.innerComments = sourceInfo.innerComments;
|
|
3514
|
+
useMemoCall.trailingComments = sourceInfo.trailingComments;
|
|
3515
|
+
const varDeclarator = t28.variableDeclarator(t28.identifier(varName), useMemoCall);
|
|
3516
|
+
return t28.variableDeclaration("const", [varDeclarator]);
|
|
3517
|
+
}
|
|
3518
|
+
function buildMergeLogic(values, propField) {
|
|
3519
|
+
const mergeMembers = [];
|
|
3520
|
+
const propFieldIdent = t28.identifier(propField);
|
|
3521
|
+
mergeMembers.push(t28.spreadElement(propFieldIdent));
|
|
3522
|
+
for (const defaultProp of values.properties) {
|
|
3523
|
+
if (!t28.isObjectProperty(defaultProp)) continue;
|
|
3524
|
+
const key = defaultProp.key;
|
|
3525
|
+
if (!t28.isIdentifier(key) && !t28.isStringLiteral(key)) continue;
|
|
3526
|
+
const keyName = t28.isIdentifier(key) ? key.name : key.value;
|
|
3527
|
+
const propAccess = t28.memberExpression(propFieldIdent, t28.identifier(keyName), false);
|
|
3528
|
+
const logicalExpr = t28.logicalExpression("??", propAccess, defaultProp.value);
|
|
3529
|
+
mergeMembers.push(
|
|
3530
|
+
t28.objectProperty(
|
|
3531
|
+
t28.isIdentifier(key) ? t28.identifier(keyName) : t28.stringLiteral(keyName),
|
|
3532
|
+
logicalExpr
|
|
3533
|
+
)
|
|
3534
|
+
);
|
|
3535
|
+
}
|
|
3536
|
+
return { mergeMembers };
|
|
3537
|
+
}
|
|
3538
|
+
|
|
3539
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
3540
|
+
import * as t29 from "@babel/types";
|
|
3307
3541
|
function resolveDefineAsyncComponent(ctx) {
|
|
3308
3542
|
return {
|
|
3309
3543
|
CallExpression(path8) {
|
|
@@ -3312,11 +3546,11 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
3312
3546
|
return;
|
|
3313
3547
|
}
|
|
3314
3548
|
const [arg] = node.arguments;
|
|
3315
|
-
if (!
|
|
3549
|
+
if (!t29.isObjectExpression(arg)) {
|
|
3316
3550
|
return;
|
|
3317
3551
|
}
|
|
3318
3552
|
for (const prop of arg.properties) {
|
|
3319
|
-
if (!
|
|
3553
|
+
if (!t29.isObjectProperty(prop) || !t29.isIdentifier(prop.key) || prop.key.name !== "hydrate") {
|
|
3320
3554
|
continue;
|
|
3321
3555
|
}
|
|
3322
3556
|
logger.warn('Unsupported option "hydrate"', {
|
|
@@ -3331,34 +3565,7 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
3331
3565
|
}
|
|
3332
3566
|
|
|
3333
3567
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
3334
|
-
import * as
|
|
3335
|
-
|
|
3336
|
-
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
3337
|
-
import * as t27 from "@babel/types";
|
|
3338
|
-
function createUseCallback(body, deps) {
|
|
3339
|
-
return t27.callExpression(t27.identifier(REACT_API_MAP.useCallback), [
|
|
3340
|
-
body,
|
|
3341
|
-
deps ?? t27.arrayExpression([])
|
|
3342
|
-
]);
|
|
3343
|
-
}
|
|
3344
|
-
function createUseMemo(body, deps) {
|
|
3345
|
-
return t27.callExpression(t27.identifier(REACT_API_MAP.useMemo), [
|
|
3346
|
-
t27.arrowFunctionExpression([], body),
|
|
3347
|
-
deps ?? t27.arrayExpression([])
|
|
3348
|
-
]);
|
|
3349
|
-
}
|
|
3350
|
-
function createUseImperativeHandle(refId, init) {
|
|
3351
|
-
return t27.callExpression(t27.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
3352
|
-
}
|
|
3353
|
-
function createUseUpdated(body, deps) {
|
|
3354
|
-
const adapter = ADAPTER_RULES.runtime.onUpdated;
|
|
3355
|
-
return t27.callExpression(t27.identifier(adapter.target), [
|
|
3356
|
-
t27.arrowFunctionExpression([], body),
|
|
3357
|
-
deps ?? t27.identifier("undefined")
|
|
3358
|
-
]);
|
|
3359
|
-
}
|
|
3360
|
-
|
|
3361
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
3568
|
+
import * as t30 from "@babel/types";
|
|
3362
3569
|
function resolveDefineExpose(ctx) {
|
|
3363
3570
|
if (ctx.inputType !== "sfc") return {};
|
|
3364
3571
|
return {
|
|
@@ -3376,16 +3583,16 @@ function resolveDefineExpose(ctx) {
|
|
|
3376
3583
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
3377
3584
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
3378
3585
|
recordImport(ctx, adapter.package, adapter.target);
|
|
3379
|
-
if (!
|
|
3586
|
+
if (!t30.isObjectExpression(expose) && !t30.isFunction(expose)) {
|
|
3380
3587
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
3381
3588
|
file: filename,
|
|
3382
3589
|
loc: expose.loc,
|
|
3383
3590
|
source: scriptData.source
|
|
3384
3591
|
});
|
|
3385
3592
|
}
|
|
3386
|
-
const init = !
|
|
3593
|
+
const init = !t30.isFunction(expose) ? t30.arrowFunctionExpression([], expose) : expose;
|
|
3387
3594
|
const { forwardRef } = scriptData;
|
|
3388
|
-
const newNode = createUseImperativeHandle(
|
|
3595
|
+
const newNode = createUseImperativeHandle(t30.identifier(forwardRef.refField), init);
|
|
3389
3596
|
forwardRef.enabled = true;
|
|
3390
3597
|
replaceNode(path8, newNode, node);
|
|
3391
3598
|
}
|
|
@@ -3393,7 +3600,7 @@ function resolveDefineExpose(ctx) {
|
|
|
3393
3600
|
}
|
|
3394
3601
|
|
|
3395
3602
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-model.ts
|
|
3396
|
-
import * as
|
|
3603
|
+
import * as t31 from "@babel/types";
|
|
3397
3604
|
function resolveDefineModel(ctx, ast) {
|
|
3398
3605
|
if (ctx.inputType !== "sfc") return {};
|
|
3399
3606
|
return {
|
|
@@ -3416,7 +3623,7 @@ function validateDefineModelUsage(path8, ctx) {
|
|
|
3416
3623
|
const { node, parent } = path8;
|
|
3417
3624
|
const { filename, scriptData } = ctx;
|
|
3418
3625
|
const [arg1, arg2] = node.arguments;
|
|
3419
|
-
if (!
|
|
3626
|
+
if (!t31.isObjectExpression(arg1) && !t31.isStringLiteral(arg1)) {
|
|
3420
3627
|
logger.error(`Invalid argument type for defineModel. Expected a string.`, {
|
|
3421
3628
|
file: filename,
|
|
3422
3629
|
source: scriptData.source,
|
|
@@ -3425,11 +3632,11 @@ function validateDefineModelUsage(path8, ctx) {
|
|
|
3425
3632
|
return false;
|
|
3426
3633
|
}
|
|
3427
3634
|
const hasUnsupportedOption = (arg) => {
|
|
3428
|
-
if (!
|
|
3635
|
+
if (!t31.isObjectExpression(arg)) {
|
|
3429
3636
|
return false;
|
|
3430
3637
|
}
|
|
3431
3638
|
const result = arg.properties.some((prop) => {
|
|
3432
|
-
if ((
|
|
3639
|
+
if ((t31.isObjectProperty(prop) || t31.isObjectMethod(prop)) && t31.isIdentifier(prop.key)) {
|
|
3433
3640
|
const keyName = prop.key.name;
|
|
3434
3641
|
if (keyName === "get" || keyName === "set" || keyName === "validator") {
|
|
3435
3642
|
logger.error(`defineModel does not support '${keyName}' option.`, {
|
|
@@ -3445,7 +3652,7 @@ function validateDefineModelUsage(path8, ctx) {
|
|
|
3445
3652
|
};
|
|
3446
3653
|
const isValidVariableAssignment = () => {
|
|
3447
3654
|
const varDeclaration = path8.parentPath.parentPath;
|
|
3448
|
-
if (varDeclaration?.isVariableDeclaration() && varDeclaration.node.declarations.length === 1 &&
|
|
3655
|
+
if (varDeclaration?.isVariableDeclaration() && varDeclaration.node.declarations.length === 1 && t31.isArrayPattern(varDeclaration.node.declarations[0]?.id)) {
|
|
3449
3656
|
logger.error(
|
|
3450
3657
|
`defineModel return value cannot be destructured with array pattern. Please use single variable assignment.`,
|
|
3451
3658
|
{
|
|
@@ -3471,7 +3678,7 @@ function extractPropInfo(node, ctx) {
|
|
|
3471
3678
|
};
|
|
3472
3679
|
const findObjectProperty = (objExpr, propName) => {
|
|
3473
3680
|
return objExpr.properties.find(
|
|
3474
|
-
(prop) =>
|
|
3681
|
+
(prop) => t31.isObjectProperty(prop) && t31.isIdentifier(prop.key) && prop.key.name === propName
|
|
3475
3682
|
);
|
|
3476
3683
|
};
|
|
3477
3684
|
const setPropName = (info, value) => {
|
|
@@ -3485,20 +3692,20 @@ function extractPropInfo(node, ctx) {
|
|
|
3485
3692
|
const typeProp = findObjectProperty(objExpr, "type");
|
|
3486
3693
|
const defaultProp = findObjectProperty(objExpr, "default");
|
|
3487
3694
|
const requiredProp = findObjectProperty(objExpr, "required");
|
|
3488
|
-
if (nameProp &&
|
|
3695
|
+
if (nameProp && t31.isStringLiteral(nameProp.value)) {
|
|
3489
3696
|
setPropName(result, nameProp.value.value);
|
|
3490
3697
|
}
|
|
3491
3698
|
if (defaultProp) {
|
|
3492
3699
|
result.default = defaultProp.value;
|
|
3493
3700
|
}
|
|
3494
|
-
if (requiredProp &&
|
|
3701
|
+
if (requiredProp && t31.isBooleanLiteral(requiredProp.value)) {
|
|
3495
3702
|
result.required = requiredProp.value.value;
|
|
3496
3703
|
}
|
|
3497
|
-
if (typeProp &&
|
|
3704
|
+
if (typeProp && t31.isIdentifier(typeProp.value)) {
|
|
3498
3705
|
result.type = mapRuntimeTypeToTSType(typeProp.value);
|
|
3499
3706
|
} else if (node.typeParameters) {
|
|
3500
3707
|
const [typeParam] = node.typeParameters.params;
|
|
3501
|
-
if (
|
|
3708
|
+
if (t31.isTSType(typeParam)) {
|
|
3502
3709
|
result.type = typeParam;
|
|
3503
3710
|
}
|
|
3504
3711
|
} else if (defaultProp) {
|
|
@@ -3525,20 +3732,20 @@ function extractPropInfo(node, ctx) {
|
|
|
3525
3732
|
};
|
|
3526
3733
|
const extractTypeFromTypeParams = (info) => {
|
|
3527
3734
|
if (!node.typeParameters) return;
|
|
3528
|
-
const isDefaultAny = !info.type ||
|
|
3735
|
+
const isDefaultAny = !info.type || t31.isTSAnyKeyword(info.type);
|
|
3529
3736
|
if (!isDefaultAny) return;
|
|
3530
3737
|
const [typeParam] = node.typeParameters.params;
|
|
3531
|
-
if (
|
|
3738
|
+
if (t31.isTSType(typeParam)) {
|
|
3532
3739
|
info.type = typeParam;
|
|
3533
3740
|
}
|
|
3534
3741
|
};
|
|
3535
|
-
if (
|
|
3742
|
+
if (t31.isStringLiteral(arg1)) {
|
|
3536
3743
|
setPropName(propInfo, arg1.value);
|
|
3537
3744
|
extractTypeFromTypeParams(propInfo);
|
|
3538
|
-
} else if (
|
|
3745
|
+
} else if (t31.isObjectExpression(arg1)) {
|
|
3539
3746
|
safeAssign(propInfo, extractPropInfoFromObject(arg1));
|
|
3540
3747
|
}
|
|
3541
|
-
if (!
|
|
3748
|
+
if (!t31.isObjectExpression(arg1) && t31.isObjectExpression(arg2)) {
|
|
3542
3749
|
safeAssign(propInfo, extractPropInfoFromObject(arg2));
|
|
3543
3750
|
}
|
|
3544
3751
|
return propInfo;
|
|
@@ -3548,36 +3755,36 @@ function replaceToUseVRef(node, propInfo, ctx) {
|
|
|
3548
3755
|
replaceCallName(node, refAdapter.target);
|
|
3549
3756
|
recordImport(ctx, refAdapter.package, refAdapter.target);
|
|
3550
3757
|
const defaultValue = propInfo.default;
|
|
3551
|
-
const propRef =
|
|
3758
|
+
const propRef = t31.identifier(`${ctx.propField}.${propInfo.name}`);
|
|
3552
3759
|
if (!defaultValue) {
|
|
3553
3760
|
node.arguments = [propRef];
|
|
3554
3761
|
} else {
|
|
3555
|
-
node.arguments = [
|
|
3762
|
+
node.arguments = [t31.logicalExpression("??", propRef, defaultValue)];
|
|
3556
3763
|
}
|
|
3557
3764
|
if (ctx.scriptData.lang.startsWith("ts") && !node.typeParameters && propInfo.type) {
|
|
3558
|
-
node.typeParameters =
|
|
3765
|
+
node.typeParameters = t31.tsTypeParameterInstantiation([propInfo.type]);
|
|
3559
3766
|
}
|
|
3560
3767
|
}
|
|
3561
3768
|
function resolveAutoUpdate(ast, path8, propInfo, ctx) {
|
|
3562
3769
|
const { parent } = path8;
|
|
3563
|
-
if (!
|
|
3770
|
+
if (!t31.isVariableDeclarator(parent)) {
|
|
3564
3771
|
return;
|
|
3565
3772
|
}
|
|
3566
3773
|
const modelId = parent.id;
|
|
3567
|
-
if (!
|
|
3774
|
+
if (!t31.isIdentifier(modelId)) {
|
|
3568
3775
|
return;
|
|
3569
3776
|
}
|
|
3570
|
-
const memberAccess =
|
|
3571
|
-
|
|
3572
|
-
|
|
3777
|
+
const memberAccess = t31.memberExpression(
|
|
3778
|
+
t31.identifier(ctx.propField),
|
|
3779
|
+
t31.identifier(propInfo.updateEventName)
|
|
3573
3780
|
);
|
|
3574
|
-
const dep =
|
|
3575
|
-
const updateCall =
|
|
3781
|
+
const dep = t31.memberExpression(modelId, t31.identifier("value"));
|
|
3782
|
+
const updateCall = t31.optionalCallExpression(memberAccess, [dep], true);
|
|
3576
3783
|
const callExpr = createUseUpdated(
|
|
3577
|
-
|
|
3578
|
-
|
|
3784
|
+
t31.blockStatement([t31.expressionStatement(updateCall)]),
|
|
3785
|
+
t31.arrayExpression([dep])
|
|
3579
3786
|
);
|
|
3580
|
-
ast.program.body.push(
|
|
3787
|
+
ast.program.body.push(t31.expressionStatement(callExpr));
|
|
3581
3788
|
const adapter = ADAPTER_RULES.runtime.onUpdated;
|
|
3582
3789
|
recordImport(ctx, adapter.package, adapter.target);
|
|
3583
3790
|
}
|
|
@@ -3587,25 +3794,25 @@ function resolveInterface(propInfo, ctx) {
|
|
|
3587
3794
|
if (!lang.startsWith("ts")) {
|
|
3588
3795
|
return;
|
|
3589
3796
|
}
|
|
3590
|
-
const propType = propInfo.type ||
|
|
3591
|
-
const propSignature =
|
|
3592
|
-
|
|
3593
|
-
|
|
3797
|
+
const propType = propInfo.type || t31.tsAnyKeyword();
|
|
3798
|
+
const propSignature = t31.tsPropertySignature(
|
|
3799
|
+
t31.isValidIdentifier(name) ? t31.identifier(name) : t31.stringLiteral(name),
|
|
3800
|
+
t31.tsTypeAnnotation(propType)
|
|
3594
3801
|
);
|
|
3595
3802
|
propSignature.optional = !required;
|
|
3596
|
-
const emitArg =
|
|
3597
|
-
emitArg.typeAnnotation =
|
|
3598
|
-
const emitSignature =
|
|
3599
|
-
|
|
3600
|
-
|
|
3803
|
+
const emitArg = t31.identifier("arg");
|
|
3804
|
+
emitArg.typeAnnotation = t31.tsTypeAnnotation(propType);
|
|
3805
|
+
const emitSignature = t31.tsPropertySignature(
|
|
3806
|
+
t31.isValidIdentifier(updateEventName) ? t31.identifier(updateEventName) : t31.stringLiteral(updateEventName),
|
|
3807
|
+
t31.tsTypeAnnotation(t31.tsFunctionType(null, [emitArg], t31.tsTypeAnnotation(t31.tsVoidKeyword())))
|
|
3601
3808
|
);
|
|
3602
3809
|
emitSignature.optional = true;
|
|
3603
3810
|
const appendToTypeLiteral = (list, member) => {
|
|
3604
|
-
const existing = list.find((item) =>
|
|
3811
|
+
const existing = list.find((item) => t31.isTSTypeLiteral(item));
|
|
3605
3812
|
if (existing) {
|
|
3606
3813
|
existing.members.push(member);
|
|
3607
3814
|
} else {
|
|
3608
|
-
list.push(
|
|
3815
|
+
list.push(t31.tsTypeLiteral([member]));
|
|
3609
3816
|
}
|
|
3610
3817
|
};
|
|
3611
3818
|
appendToTypeLiteral(propsTSIface.propsTypes, propSignature);
|
|
@@ -3626,7 +3833,7 @@ function resolveDefineOptions(ctx) {
|
|
|
3626
3833
|
}
|
|
3627
3834
|
|
|
3628
3835
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
3629
|
-
import * as
|
|
3836
|
+
import * as t32 from "@babel/types";
|
|
3630
3837
|
function resolveEmitCalls(ctx) {
|
|
3631
3838
|
const formatEmitEventName = (raw) => {
|
|
3632
3839
|
if (raw.startsWith("update:")) {
|
|
@@ -3640,7 +3847,7 @@ function resolveEmitCalls(ctx) {
|
|
|
3640
3847
|
CallExpression(path8) {
|
|
3641
3848
|
const { node } = path8;
|
|
3642
3849
|
const { filename, templateData, scriptData } = ctx;
|
|
3643
|
-
if (!
|
|
3850
|
+
if (!t32.isIdentifier(node.callee)) return;
|
|
3644
3851
|
const { name } = node.callee;
|
|
3645
3852
|
const checkIfFromDefineEmits = () => {
|
|
3646
3853
|
let result = false;
|
|
@@ -3652,7 +3859,7 @@ function resolveEmitCalls(ctx) {
|
|
|
3652
3859
|
const binding = path8.scope.getBinding(name);
|
|
3653
3860
|
if (binding) {
|
|
3654
3861
|
const parent = binding.path.node;
|
|
3655
|
-
if (
|
|
3862
|
+
if (t32.isVariableDeclarator(parent) && t32.isCallExpression(parent.init) && t32.isIdentifier(parent.init.callee)) {
|
|
3656
3863
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
3657
3864
|
}
|
|
3658
3865
|
}
|
|
@@ -3662,9 +3869,9 @@ function resolveEmitCalls(ctx) {
|
|
|
3662
3869
|
if (!checkIfFromDefineEmits()) return;
|
|
3663
3870
|
const [callee, ...args] = node.arguments;
|
|
3664
3871
|
let propCall;
|
|
3665
|
-
if (
|
|
3872
|
+
if (t32.isStringLiteral(callee)) {
|
|
3666
3873
|
const eventName = formatEmitEventName(callee.value);
|
|
3667
|
-
propCall = createPropCall(ctx.propField,
|
|
3874
|
+
propCall = createPropCall(ctx.propField, t32.identifier(eventName), args);
|
|
3668
3875
|
} else {
|
|
3669
3876
|
propCall = createPropCall(ctx.propField, callee, args, true);
|
|
3670
3877
|
logger.error(
|
|
@@ -3681,38 +3888,38 @@ function resolveEmitCalls(ctx) {
|
|
|
3681
3888
|
};
|
|
3682
3889
|
}
|
|
3683
3890
|
function createPropCall(rootName, callee, args, computed = false) {
|
|
3684
|
-
return
|
|
3685
|
-
|
|
3891
|
+
return t32.optionalCallExpression(
|
|
3892
|
+
t32.memberExpression(t32.identifier(rootName), callee, computed),
|
|
3686
3893
|
args,
|
|
3687
3894
|
true
|
|
3688
3895
|
);
|
|
3689
3896
|
}
|
|
3690
3897
|
|
|
3691
3898
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
3692
|
-
import * as
|
|
3899
|
+
import * as t40 from "@babel/types";
|
|
3693
3900
|
|
|
3694
3901
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
3695
|
-
import * as
|
|
3902
|
+
import * as t34 from "@babel/types";
|
|
3696
3903
|
|
|
3697
3904
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
3698
|
-
import * as
|
|
3905
|
+
import * as t33 from "@babel/types";
|
|
3699
3906
|
function cloneCallableParams(params) {
|
|
3700
3907
|
const cloneCallableParam = (param, index) => {
|
|
3701
|
-
if (
|
|
3908
|
+
if (t33.isRestElement(param)) {
|
|
3702
3909
|
const arg = param.argument;
|
|
3703
|
-
const name =
|
|
3704
|
-
const rest =
|
|
3705
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
3910
|
+
const name = t33.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
3911
|
+
const rest = t33.restElement(t33.identifier(name));
|
|
3912
|
+
rest.typeAnnotation = param.typeAnnotation || (t33.isIdentifier(arg) ? arg.typeAnnotation : null) || t33.tsTypeAnnotation(t33.tsArrayType(t33.tsAnyKeyword()));
|
|
3706
3913
|
return rest;
|
|
3707
3914
|
}
|
|
3708
|
-
if (
|
|
3709
|
-
const id =
|
|
3915
|
+
if (t33.isIdentifier(param)) {
|
|
3916
|
+
const id = t33.identifier(param.name || `arg${index}`);
|
|
3710
3917
|
id.optional = param.optional;
|
|
3711
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
3918
|
+
id.typeAnnotation = param.typeAnnotation || t33.tsTypeAnnotation(t33.tsAnyKeyword());
|
|
3712
3919
|
return id;
|
|
3713
3920
|
}
|
|
3714
|
-
const fallback =
|
|
3715
|
-
fallback.typeAnnotation =
|
|
3921
|
+
const fallback = t33.identifier(`arg${index}`);
|
|
3922
|
+
fallback.typeAnnotation = t33.tsTypeAnnotation(t33.tsAnyKeyword());
|
|
3716
3923
|
return fallback;
|
|
3717
3924
|
};
|
|
3718
3925
|
return params.map(cloneCallableParam);
|
|
@@ -3722,18 +3929,18 @@ function cloneCallableParams(params) {
|
|
|
3722
3929
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
3723
3930
|
return {
|
|
3724
3931
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
3725
|
-
if (!
|
|
3932
|
+
if (!t34.isProgram(path8.parent)) return;
|
|
3726
3933
|
const { node } = path8;
|
|
3727
|
-
if (
|
|
3728
|
-
const typeLiteral =
|
|
3934
|
+
if (t34.isTSInterfaceDeclaration(node)) {
|
|
3935
|
+
const typeLiteral = t34.tsTypeLiteral(node.body.body);
|
|
3729
3936
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
3730
3937
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
3731
|
-
if (resolved &&
|
|
3938
|
+
if (resolved && t34.isTSTypeLiteral(resolved)) {
|
|
3732
3939
|
node.body.body = resolved.members;
|
|
3733
3940
|
}
|
|
3734
3941
|
return;
|
|
3735
3942
|
}
|
|
3736
|
-
if (
|
|
3943
|
+
if (t34.isTSTypeAliasDeclaration(node)) {
|
|
3737
3944
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
3738
3945
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
3739
3946
|
if (resolved) {
|
|
@@ -3744,47 +3951,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
3744
3951
|
};
|
|
3745
3952
|
}
|
|
3746
3953
|
function resolveTopLevelEmitType(tsType) {
|
|
3747
|
-
if (
|
|
3954
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
3748
3955
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
3749
3956
|
}
|
|
3750
|
-
if (
|
|
3957
|
+
if (t34.isTSTypeReference(tsType)) {
|
|
3751
3958
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3752
3959
|
return tsType;
|
|
3753
3960
|
}
|
|
3754
3961
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
3755
|
-
return
|
|
3962
|
+
return t34.tsTypeReference(
|
|
3756
3963
|
tsType.typeName,
|
|
3757
|
-
|
|
3964
|
+
t34.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3758
3965
|
);
|
|
3759
3966
|
}
|
|
3760
|
-
if (
|
|
3967
|
+
if (t34.isTSIntersectionType(tsType)) {
|
|
3761
3968
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
3762
3969
|
if (!types.length) return null;
|
|
3763
3970
|
if (types.length === 1) return types[0];
|
|
3764
|
-
return
|
|
3971
|
+
return t34.tsIntersectionType(types);
|
|
3765
3972
|
}
|
|
3766
|
-
if (
|
|
3973
|
+
if (t34.isTSUnionType(tsType)) {
|
|
3767
3974
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
3768
3975
|
if (!types.length) return null;
|
|
3769
3976
|
if (types.length === 1) return types[0];
|
|
3770
|
-
return
|
|
3977
|
+
return t34.tsUnionType(types);
|
|
3771
3978
|
}
|
|
3772
|
-
if (
|
|
3979
|
+
if (t34.isTSTypeLiteral(tsType)) {
|
|
3773
3980
|
const members = [];
|
|
3774
3981
|
for (const member of tsType.members) {
|
|
3775
|
-
if (
|
|
3982
|
+
if (t34.isTSCallSignatureDeclaration(member)) {
|
|
3776
3983
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
3777
3984
|
continue;
|
|
3778
3985
|
}
|
|
3779
3986
|
members.push(member);
|
|
3780
3987
|
}
|
|
3781
3988
|
if (!members.length) return null;
|
|
3782
|
-
return
|
|
3989
|
+
return t34.tsTypeLiteral(members);
|
|
3783
3990
|
}
|
|
3784
|
-
if (
|
|
3991
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
3785
3992
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
3786
3993
|
if (!props.length) return null;
|
|
3787
|
-
return
|
|
3994
|
+
return t34.tsTypeLiteral(props);
|
|
3788
3995
|
}
|
|
3789
3996
|
return tsType;
|
|
3790
3997
|
}
|
|
@@ -3805,41 +4012,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
3805
4012
|
propsTSIface: { emitTypes }
|
|
3806
4013
|
} = ctx.scriptData;
|
|
3807
4014
|
const members = [];
|
|
3808
|
-
if (
|
|
4015
|
+
if (t34.isArrayExpression(runtimeArg)) {
|
|
3809
4016
|
for (const element of runtimeArg.elements) {
|
|
3810
|
-
if (!element || !
|
|
4017
|
+
if (!element || !t34.isStringLiteral(element)) continue;
|
|
3811
4018
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
3812
4019
|
const key = buildKey(handlerName);
|
|
3813
|
-
const fnType =
|
|
4020
|
+
const fnType = t34.tsFunctionType(
|
|
3814
4021
|
null,
|
|
3815
4022
|
[createRestAnyParam("args")],
|
|
3816
|
-
|
|
4023
|
+
t34.tsTypeAnnotation(t34.tsAnyKeyword())
|
|
3817
4024
|
);
|
|
3818
|
-
const prop =
|
|
4025
|
+
const prop = t34.tsPropertySignature(key, t34.tsTypeAnnotation(fnType));
|
|
3819
4026
|
prop.optional = true;
|
|
3820
4027
|
members.push(prop);
|
|
3821
4028
|
}
|
|
3822
4029
|
if (members.length) {
|
|
3823
|
-
emitTypes.push(
|
|
4030
|
+
emitTypes.push(t34.tsTypeLiteral(members));
|
|
3824
4031
|
}
|
|
3825
4032
|
return;
|
|
3826
4033
|
}
|
|
3827
|
-
if (
|
|
4034
|
+
if (t34.isObjectExpression(runtimeArg)) {
|
|
3828
4035
|
for (const prop of runtimeArg.properties) {
|
|
3829
|
-
if (!
|
|
3830
|
-
if (
|
|
4036
|
+
if (!t34.isObjectProperty(prop)) continue;
|
|
4037
|
+
if (t34.isSpreadElement(prop)) continue;
|
|
3831
4038
|
const rawName = resolvePropName(prop.key);
|
|
3832
4039
|
if (!rawName) continue;
|
|
3833
4040
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
3834
4041
|
const key = buildKey(handlerName);
|
|
3835
|
-
const params =
|
|
3836
|
-
const fnType =
|
|
3837
|
-
const propSig =
|
|
4042
|
+
const params = t34.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
4043
|
+
const fnType = t34.tsFunctionType(null, params, t34.tsTypeAnnotation(t34.tsAnyKeyword()));
|
|
4044
|
+
const propSig = t34.tsPropertySignature(key, t34.tsTypeAnnotation(fnType));
|
|
3838
4045
|
propSig.optional = true;
|
|
3839
4046
|
members.push(propSig);
|
|
3840
4047
|
}
|
|
3841
4048
|
if (members.length) {
|
|
3842
|
-
emitTypes.push(
|
|
4049
|
+
emitTypes.push(t34.tsTypeLiteral(members));
|
|
3843
4050
|
}
|
|
3844
4051
|
}
|
|
3845
4052
|
}
|
|
@@ -3860,129 +4067,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
3860
4067
|
return `on${name}`;
|
|
3861
4068
|
}
|
|
3862
4069
|
function resolvePropName(key) {
|
|
3863
|
-
if (
|
|
3864
|
-
if (
|
|
3865
|
-
if (
|
|
4070
|
+
if (t34.isIdentifier(key)) return key.name;
|
|
4071
|
+
if (t34.isStringLiteral(key)) return key.value;
|
|
4072
|
+
if (t34.isNumericLiteral(key)) return String(key.value);
|
|
3866
4073
|
return null;
|
|
3867
4074
|
}
|
|
3868
4075
|
function buildKey(name) {
|
|
3869
|
-
return
|
|
4076
|
+
return t34.isValidIdentifier(name) ? t34.identifier(name) : t34.stringLiteral(name);
|
|
3870
4077
|
}
|
|
3871
4078
|
function createRestAnyParam(name) {
|
|
3872
|
-
const id =
|
|
3873
|
-
const rest =
|
|
3874
|
-
rest.typeAnnotation =
|
|
4079
|
+
const id = t34.identifier(name);
|
|
4080
|
+
const rest = t34.restElement(id);
|
|
4081
|
+
rest.typeAnnotation = t34.tsTypeAnnotation(t34.tsArrayType(t34.tsAnyKeyword()));
|
|
3875
4082
|
return rest;
|
|
3876
4083
|
}
|
|
3877
4084
|
function resolveRuntimeTupleParams(value) {
|
|
3878
4085
|
const params = [];
|
|
3879
4086
|
value.elements.forEach((element, index) => {
|
|
3880
4087
|
if (!element) return;
|
|
3881
|
-
if (
|
|
4088
|
+
if (t34.isSpreadElement(element)) {
|
|
3882
4089
|
params.push(createRestAnyParam(`args${index}`));
|
|
3883
4090
|
return;
|
|
3884
4091
|
}
|
|
3885
|
-
if (
|
|
3886
|
-
const id =
|
|
3887
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
4092
|
+
if (t34.isIdentifier(element)) {
|
|
4093
|
+
const id = t34.identifier(element.name);
|
|
4094
|
+
id.typeAnnotation = element.typeAnnotation || t34.tsTypeAnnotation(t34.tsAnyKeyword());
|
|
3888
4095
|
params.push(id);
|
|
3889
4096
|
return;
|
|
3890
4097
|
}
|
|
3891
|
-
if (
|
|
3892
|
-
const id =
|
|
3893
|
-
id.typeAnnotation =
|
|
4098
|
+
if (t34.isTSAsExpression(element)) {
|
|
4099
|
+
const id = t34.identifier(`arg${index}`);
|
|
4100
|
+
id.typeAnnotation = t34.tsTypeAnnotation(element.typeAnnotation);
|
|
3894
4101
|
params.push(id);
|
|
3895
4102
|
return;
|
|
3896
4103
|
}
|
|
3897
|
-
const fallback =
|
|
3898
|
-
fallback.typeAnnotation =
|
|
4104
|
+
const fallback = t34.identifier(`arg${index}`);
|
|
4105
|
+
fallback.typeAnnotation = t34.tsTypeAnnotation(t34.tsAnyKeyword());
|
|
3899
4106
|
params.push(fallback);
|
|
3900
4107
|
});
|
|
3901
4108
|
return params;
|
|
3902
4109
|
}
|
|
3903
4110
|
function resolveExplicitEmitType(tsType) {
|
|
3904
|
-
if (
|
|
4111
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
3905
4112
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
3906
4113
|
}
|
|
3907
|
-
if (
|
|
4114
|
+
if (t34.isTSTypeReference(tsType)) {
|
|
3908
4115
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3909
4116
|
return tsType;
|
|
3910
4117
|
}
|
|
3911
4118
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
3912
|
-
return
|
|
4119
|
+
return t34.tsTypeReference(
|
|
3913
4120
|
tsType.typeName,
|
|
3914
|
-
|
|
4121
|
+
t34.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3915
4122
|
);
|
|
3916
4123
|
}
|
|
3917
|
-
if (
|
|
4124
|
+
if (t34.isTSIntersectionType(tsType)) {
|
|
3918
4125
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
3919
4126
|
if (!types.length) return null;
|
|
3920
4127
|
if (types.length === 1) return types[0];
|
|
3921
|
-
return
|
|
4128
|
+
return t34.tsIntersectionType(types);
|
|
3922
4129
|
}
|
|
3923
|
-
if (
|
|
4130
|
+
if (t34.isTSUnionType(tsType)) {
|
|
3924
4131
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
3925
4132
|
if (!types.length) return null;
|
|
3926
4133
|
if (types.length === 1) return types[0];
|
|
3927
|
-
return
|
|
4134
|
+
return t34.tsUnionType(types);
|
|
3928
4135
|
}
|
|
3929
|
-
if (
|
|
4136
|
+
if (t34.isTSTypeLiteral(tsType)) {
|
|
3930
4137
|
const members = [];
|
|
3931
4138
|
for (const member of tsType.members) {
|
|
3932
|
-
if (
|
|
4139
|
+
if (t34.isTSPropertySignature(member)) {
|
|
3933
4140
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
3934
4141
|
if (prop) members.push(prop);
|
|
3935
4142
|
continue;
|
|
3936
4143
|
}
|
|
3937
|
-
if (
|
|
4144
|
+
if (t34.isTSCallSignatureDeclaration(member)) {
|
|
3938
4145
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
3939
4146
|
continue;
|
|
3940
4147
|
}
|
|
3941
4148
|
}
|
|
3942
4149
|
if (!members.length) return null;
|
|
3943
|
-
return
|
|
4150
|
+
return t34.tsTypeLiteral(members);
|
|
3944
4151
|
}
|
|
3945
|
-
if (
|
|
4152
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
3946
4153
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
3947
4154
|
if (!props.length) return null;
|
|
3948
|
-
return
|
|
4155
|
+
return t34.tsTypeLiteral(props);
|
|
3949
4156
|
}
|
|
3950
4157
|
return tsType;
|
|
3951
4158
|
}
|
|
3952
4159
|
function hasEmitsSignatureInType(tsType) {
|
|
3953
|
-
if (
|
|
4160
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
3954
4161
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
3955
4162
|
}
|
|
3956
|
-
if (
|
|
4163
|
+
if (t34.isTSTypeReference(tsType)) {
|
|
3957
4164
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3958
4165
|
return false;
|
|
3959
4166
|
}
|
|
3960
4167
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
3961
4168
|
}
|
|
3962
|
-
if (
|
|
4169
|
+
if (t34.isTSIntersectionType(tsType) || t34.isTSUnionType(tsType)) {
|
|
3963
4170
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
3964
4171
|
}
|
|
3965
|
-
if (
|
|
4172
|
+
if (t34.isTSTypeLiteral(tsType)) {
|
|
3966
4173
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
3967
4174
|
}
|
|
3968
|
-
if (
|
|
4175
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
3969
4176
|
return isEmitsCallable(tsType.parameters);
|
|
3970
4177
|
}
|
|
3971
4178
|
return false;
|
|
3972
4179
|
}
|
|
3973
4180
|
function hasEmitsSignatureInMember(member) {
|
|
3974
|
-
if (
|
|
4181
|
+
if (t34.isTSCallSignatureDeclaration(member)) {
|
|
3975
4182
|
return isEmitsCallable(member.parameters);
|
|
3976
4183
|
}
|
|
3977
4184
|
return false;
|
|
3978
4185
|
}
|
|
3979
4186
|
function isEmitsCallable(parameters) {
|
|
3980
4187
|
const [eventParam] = parameters;
|
|
3981
|
-
if (!eventParam || !
|
|
4188
|
+
if (!eventParam || !t34.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
3982
4189
|
return false;
|
|
3983
4190
|
}
|
|
3984
4191
|
const { typeAnnotation } = eventParam;
|
|
3985
|
-
if (
|
|
4192
|
+
if (t34.isNoop(typeAnnotation)) return false;
|
|
3986
4193
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
3987
4194
|
}
|
|
3988
4195
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -3992,19 +4199,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
3992
4199
|
const key = buildKey(handlerName);
|
|
3993
4200
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3994
4201
|
let params = [];
|
|
3995
|
-
let returnType =
|
|
3996
|
-
if (typeAnnotation &&
|
|
4202
|
+
let returnType = t34.tsAnyKeyword();
|
|
4203
|
+
if (typeAnnotation && t34.isTSFunctionType(typeAnnotation)) {
|
|
3997
4204
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
3998
4205
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
3999
|
-
} else if (typeAnnotation &&
|
|
4206
|
+
} else if (typeAnnotation && t34.isTSTupleType(typeAnnotation)) {
|
|
4000
4207
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
4001
4208
|
} else if (typeAnnotation) {
|
|
4002
|
-
const id =
|
|
4003
|
-
id.typeAnnotation =
|
|
4209
|
+
const id = t34.identifier("value");
|
|
4210
|
+
id.typeAnnotation = t34.tsTypeAnnotation(typeAnnotation);
|
|
4004
4211
|
params = [id];
|
|
4005
4212
|
}
|
|
4006
|
-
const fnType =
|
|
4007
|
-
const prop =
|
|
4213
|
+
const fnType = t34.tsFunctionType(null, params, t34.tsTypeAnnotation(returnType));
|
|
4214
|
+
const prop = t34.tsPropertySignature(key, t34.tsTypeAnnotation(fnType));
|
|
4008
4215
|
prop.optional = !!member.optional;
|
|
4009
4216
|
return prop;
|
|
4010
4217
|
}
|
|
@@ -4013,32 +4220,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
4013
4220
|
}
|
|
4014
4221
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
4015
4222
|
const [eventParam, ...restParams] = parameters;
|
|
4016
|
-
if (!eventParam || !
|
|
4223
|
+
if (!eventParam || !t34.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
4017
4224
|
return [];
|
|
4018
4225
|
}
|
|
4019
4226
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
4020
|
-
if (
|
|
4227
|
+
if (t34.isNoop(paramTypeAnnotation)) return [];
|
|
4021
4228
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
4022
4229
|
if (!eventNames.length) return [];
|
|
4023
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
4230
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t34.tsAnyKeyword();
|
|
4024
4231
|
return eventNames.map((eventName) => {
|
|
4025
4232
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
4026
4233
|
const key = buildKey(handlerName);
|
|
4027
4234
|
const params = cloneCallableParams(restParams);
|
|
4028
|
-
const fnType =
|
|
4029
|
-
const prop =
|
|
4235
|
+
const fnType = t34.tsFunctionType(null, params, t34.tsTypeAnnotation(returnType));
|
|
4236
|
+
const prop = t34.tsPropertySignature(key, t34.tsTypeAnnotation(fnType));
|
|
4030
4237
|
prop.optional = true;
|
|
4031
4238
|
return prop;
|
|
4032
4239
|
});
|
|
4033
4240
|
}
|
|
4034
4241
|
function resolveEventNames(type) {
|
|
4035
|
-
if (
|
|
4242
|
+
if (t34.isTSLiteralType(type) && t34.isStringLiteral(type.literal)) {
|
|
4036
4243
|
return [type.literal.value];
|
|
4037
4244
|
}
|
|
4038
|
-
if (
|
|
4245
|
+
if (t34.isTSUnionType(type)) {
|
|
4039
4246
|
return type.types.flatMap(resolveEventNames);
|
|
4040
4247
|
}
|
|
4041
|
-
if (
|
|
4248
|
+
if (t34.isTSParenthesizedType(type)) {
|
|
4042
4249
|
return resolveEventNames(type.typeAnnotation);
|
|
4043
4250
|
}
|
|
4044
4251
|
return [];
|
|
@@ -4051,44 +4258,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
4051
4258
|
return params;
|
|
4052
4259
|
}
|
|
4053
4260
|
function resolveTupleElementParam(element, index) {
|
|
4054
|
-
const isNamedTuple = typeof
|
|
4261
|
+
const isNamedTuple = typeof t34.isTSNamedTupleMember === "function" && t34.isTSNamedTupleMember(element);
|
|
4055
4262
|
if (isNamedTuple) {
|
|
4056
4263
|
const tupleMember = element;
|
|
4057
4264
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
4058
4265
|
let innerType = tupleMember.elementType;
|
|
4059
4266
|
let optional = tupleMember.optional;
|
|
4060
|
-
if (
|
|
4267
|
+
if (t34.isTSOptionalType(innerType)) {
|
|
4061
4268
|
optional = true;
|
|
4062
4269
|
innerType = innerType.typeAnnotation;
|
|
4063
4270
|
}
|
|
4064
|
-
if (
|
|
4065
|
-
const rest =
|
|
4066
|
-
rest.typeAnnotation =
|
|
4271
|
+
if (t34.isTSRestType(innerType)) {
|
|
4272
|
+
const rest = t34.restElement(t34.identifier(name));
|
|
4273
|
+
rest.typeAnnotation = t34.tsTypeAnnotation(innerType.typeAnnotation);
|
|
4067
4274
|
return rest;
|
|
4068
4275
|
}
|
|
4069
|
-
const id2 =
|
|
4276
|
+
const id2 = t34.identifier(name);
|
|
4070
4277
|
id2.optional = optional;
|
|
4071
|
-
id2.typeAnnotation =
|
|
4278
|
+
id2.typeAnnotation = t34.tsTypeAnnotation(innerType);
|
|
4072
4279
|
return id2;
|
|
4073
4280
|
}
|
|
4074
|
-
if (
|
|
4075
|
-
const rest =
|
|
4076
|
-
rest.typeAnnotation =
|
|
4281
|
+
if (t34.isTSRestType(element)) {
|
|
4282
|
+
const rest = t34.restElement(t34.identifier(`args${index}`));
|
|
4283
|
+
rest.typeAnnotation = t34.tsTypeAnnotation(element.typeAnnotation);
|
|
4077
4284
|
return rest;
|
|
4078
4285
|
}
|
|
4079
|
-
if (
|
|
4080
|
-
const id2 =
|
|
4286
|
+
if (t34.isTSOptionalType(element)) {
|
|
4287
|
+
const id2 = t34.identifier(`arg${index}`);
|
|
4081
4288
|
id2.optional = true;
|
|
4082
|
-
id2.typeAnnotation =
|
|
4289
|
+
id2.typeAnnotation = t34.tsTypeAnnotation(element.typeAnnotation);
|
|
4083
4290
|
return id2;
|
|
4084
4291
|
}
|
|
4085
|
-
const id =
|
|
4086
|
-
id.typeAnnotation =
|
|
4292
|
+
const id = t34.identifier(`arg${index}`);
|
|
4293
|
+
id.typeAnnotation = t34.tsTypeAnnotation(element);
|
|
4087
4294
|
return id;
|
|
4088
4295
|
}
|
|
4089
4296
|
|
|
4090
4297
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
4091
|
-
import * as
|
|
4298
|
+
import * as t35 from "@babel/types";
|
|
4092
4299
|
function resolveDefinePropsIface(path8, ctx) {
|
|
4093
4300
|
const { node } = path8;
|
|
4094
4301
|
const [runtimeArg] = node.arguments;
|
|
@@ -4108,38 +4315,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
4108
4315
|
} = scriptData;
|
|
4109
4316
|
if (!runtimeArg) return;
|
|
4110
4317
|
const members = [];
|
|
4111
|
-
if (
|
|
4318
|
+
if (t35.isArrayExpression(runtimeArg)) {
|
|
4112
4319
|
for (const element of runtimeArg.elements) {
|
|
4113
|
-
if (!element || !
|
|
4114
|
-
const key =
|
|
4115
|
-
const prop =
|
|
4320
|
+
if (!element || !t35.isStringLiteral(element)) continue;
|
|
4321
|
+
const key = t35.isValidIdentifier(element.value) ? t35.identifier(element.value) : t35.stringLiteral(element.value);
|
|
4322
|
+
const prop = t35.tsPropertySignature(key, t35.tsTypeAnnotation(t35.tsAnyKeyword()));
|
|
4116
4323
|
prop.optional = true;
|
|
4117
4324
|
members.push(prop);
|
|
4118
4325
|
}
|
|
4119
4326
|
if (members.length) {
|
|
4120
|
-
propsTypes.push(
|
|
4327
|
+
propsTypes.push(t35.tsTypeLiteral(members));
|
|
4121
4328
|
}
|
|
4122
4329
|
return;
|
|
4123
4330
|
}
|
|
4124
|
-
if (
|
|
4331
|
+
if (t35.isObjectExpression(runtimeArg)) {
|
|
4125
4332
|
for (const prop of runtimeArg.properties) {
|
|
4126
|
-
if (!
|
|
4127
|
-
if (
|
|
4333
|
+
if (!t35.isObjectProperty(prop)) continue;
|
|
4334
|
+
if (t35.isSpreadElement(prop)) continue;
|
|
4128
4335
|
const key = prop.key;
|
|
4129
4336
|
let propName = null;
|
|
4130
|
-
if (
|
|
4131
|
-
if (
|
|
4132
|
-
if (
|
|
4337
|
+
if (t35.isIdentifier(key)) propName = key.name;
|
|
4338
|
+
if (t35.isStringLiteral(key)) propName = key.value;
|
|
4339
|
+
if (t35.isNumericLiteral(key)) propName = String(key.value);
|
|
4133
4340
|
if (!propName) continue;
|
|
4134
4341
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
4135
|
-
const tsType = type ??
|
|
4136
|
-
const tsKey =
|
|
4137
|
-
const tsProp =
|
|
4342
|
+
const tsType = type ?? t35.tsAnyKeyword();
|
|
4343
|
+
const tsKey = t35.isValidIdentifier(propName) ? t35.identifier(propName) : t35.stringLiteral(propName);
|
|
4344
|
+
const tsProp = t35.tsPropertySignature(tsKey, t35.tsTypeAnnotation(tsType));
|
|
4138
4345
|
tsProp.optional = !required;
|
|
4139
4346
|
members.push(tsProp);
|
|
4140
4347
|
}
|
|
4141
4348
|
if (members.length) {
|
|
4142
|
-
propsTypes.push(
|
|
4349
|
+
propsTypes.push(t35.tsTypeLiteral(members));
|
|
4143
4350
|
}
|
|
4144
4351
|
return;
|
|
4145
4352
|
}
|
|
@@ -4153,42 +4360,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
4153
4360
|
);
|
|
4154
4361
|
}
|
|
4155
4362
|
function resolveRuntimePropMeta(value) {
|
|
4156
|
-
if (
|
|
4363
|
+
if (t35.isIdentifier(value)) {
|
|
4157
4364
|
return {
|
|
4158
4365
|
type: mapRuntimeTypeToTSType(value),
|
|
4159
4366
|
required: false
|
|
4160
4367
|
};
|
|
4161
4368
|
}
|
|
4162
|
-
if (
|
|
4369
|
+
if (t35.isArrayExpression(value)) {
|
|
4163
4370
|
return {
|
|
4164
4371
|
type: resolveRuntimeUnionType(value),
|
|
4165
4372
|
required: false
|
|
4166
4373
|
};
|
|
4167
4374
|
}
|
|
4168
|
-
if (!
|
|
4375
|
+
if (!t35.isObjectExpression(value)) {
|
|
4169
4376
|
return { required: false };
|
|
4170
4377
|
}
|
|
4171
4378
|
let type;
|
|
4172
4379
|
let required = false;
|
|
4173
4380
|
for (const prop of value.properties) {
|
|
4174
|
-
if (!
|
|
4175
|
-
if (
|
|
4381
|
+
if (!t35.isObjectProperty(prop)) continue;
|
|
4382
|
+
if (t35.isSpreadElement(prop)) continue;
|
|
4176
4383
|
const key = prop.key;
|
|
4177
|
-
const propName =
|
|
4384
|
+
const propName = t35.isIdentifier(key) ? key.name : t35.isStringLiteral(key) ? key.value : null;
|
|
4178
4385
|
if (!propName) continue;
|
|
4179
4386
|
if (propName === "type") {
|
|
4180
4387
|
const valueNode = prop.value;
|
|
4181
|
-
if (
|
|
4388
|
+
if (t35.isArrayExpression(valueNode)) {
|
|
4182
4389
|
type = resolveRuntimeUnionType(valueNode);
|
|
4183
4390
|
continue;
|
|
4184
4391
|
}
|
|
4185
|
-
if (
|
|
4392
|
+
if (t35.isIdentifier(valueNode)) {
|
|
4186
4393
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
4187
4394
|
continue;
|
|
4188
4395
|
}
|
|
4189
4396
|
}
|
|
4190
4397
|
if (propName === "required") {
|
|
4191
|
-
if (
|
|
4398
|
+
if (t35.isBooleanLiteral(prop.value)) {
|
|
4192
4399
|
required = prop.value.value;
|
|
4193
4400
|
}
|
|
4194
4401
|
}
|
|
@@ -4198,58 +4405,58 @@ function resolveRuntimePropMeta(value) {
|
|
|
4198
4405
|
function resolveRuntimeUnionType(value) {
|
|
4199
4406
|
const types = [];
|
|
4200
4407
|
for (const element of value.elements) {
|
|
4201
|
-
if (!element || !
|
|
4408
|
+
if (!element || !t35.isIdentifier(element)) continue;
|
|
4202
4409
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
4203
4410
|
if (resolved) types.push(resolved);
|
|
4204
4411
|
}
|
|
4205
|
-
if (!types.length) return
|
|
4412
|
+
if (!types.length) return t35.tsAnyKeyword();
|
|
4206
4413
|
if (types.length === 1) return types[0];
|
|
4207
|
-
return
|
|
4414
|
+
return t35.tsUnionType(types);
|
|
4208
4415
|
}
|
|
4209
4416
|
|
|
4210
4417
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
4211
|
-
import * as
|
|
4418
|
+
import * as t38 from "@babel/types";
|
|
4212
4419
|
|
|
4213
4420
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/slot-builder.ts
|
|
4214
|
-
import * as
|
|
4421
|
+
import * as t36 from "@babel/types";
|
|
4215
4422
|
var SLOT_DEFAULT_NAME = "default";
|
|
4216
4423
|
var SLOT_CHILDREN_NAME = "children";
|
|
4217
4424
|
var SLOT_FN_PARAM_NAME = "props";
|
|
4218
4425
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
4219
4426
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
4220
|
-
const key =
|
|
4221
|
-
const reactNodeType =
|
|
4222
|
-
|
|
4427
|
+
const key = t36.isValidIdentifier(propName) ? t36.identifier(propName) : t36.stringLiteral(propName);
|
|
4428
|
+
const reactNodeType = t36.tsTypeAnnotation(
|
|
4429
|
+
t36.tsTypeReference(t36.identifier(REACT_API_MAP.ReactNode))
|
|
4223
4430
|
);
|
|
4224
4431
|
let typeAnnotation;
|
|
4225
4432
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
4226
4433
|
typeAnnotation = reactNodeType;
|
|
4227
4434
|
} else {
|
|
4228
|
-
const fnType =
|
|
4229
|
-
typeAnnotation =
|
|
4435
|
+
const fnType = t36.tsFunctionType(null, params, reactNodeType);
|
|
4436
|
+
typeAnnotation = t36.tsTypeAnnotation(fnType);
|
|
4230
4437
|
}
|
|
4231
|
-
const prop =
|
|
4438
|
+
const prop = t36.tsPropertySignature(key, typeAnnotation);
|
|
4232
4439
|
prop.optional = optional;
|
|
4233
4440
|
return prop;
|
|
4234
4441
|
}
|
|
4235
4442
|
function createSlotScopeParam(props, ctx) {
|
|
4236
|
-
const paramId =
|
|
4443
|
+
const paramId = t36.identifier(SLOT_FN_PARAM_NAME);
|
|
4237
4444
|
const propsSigns = [];
|
|
4238
4445
|
const { reactiveBindings } = ctx.templateData;
|
|
4239
4446
|
props.forEach(({ prop, tsType }) => {
|
|
4240
4447
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
4241
4448
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
4242
|
-
const typeAnnotation = foundBindingTypes ?
|
|
4243
|
-
const key =
|
|
4244
|
-
const propSign =
|
|
4449
|
+
const typeAnnotation = foundBindingTypes ? t36.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
4450
|
+
const key = t36.isValidIdentifier(prop) ? t36.identifier(prop) : t36.stringLiteral(prop);
|
|
4451
|
+
const propSign = t36.tsPropertySignature(key, typeAnnotation);
|
|
4245
4452
|
propsSigns.push(propSign);
|
|
4246
4453
|
});
|
|
4247
|
-
paramId.typeAnnotation =
|
|
4454
|
+
paramId.typeAnnotation = t36.tsTypeAnnotation(t36.tsTypeLiteral(propsSigns));
|
|
4248
4455
|
return paramId;
|
|
4249
4456
|
}
|
|
4250
4457
|
|
|
4251
4458
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/utils.ts
|
|
4252
|
-
import * as
|
|
4459
|
+
import * as t37 from "@babel/types";
|
|
4253
4460
|
function recordReactNode(ctx) {
|
|
4254
4461
|
if (!ctx.scriptData.lang.startsWith("ts")) {
|
|
4255
4462
|
return;
|
|
@@ -4265,16 +4472,16 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
4265
4472
|
return declarations;
|
|
4266
4473
|
}
|
|
4267
4474
|
for (const statement of programPath.node.body) {
|
|
4268
|
-
if (
|
|
4475
|
+
if (t37.isTSInterfaceDeclaration(statement)) {
|
|
4269
4476
|
declarations.set(statement.id.name, {
|
|
4270
|
-
type:
|
|
4477
|
+
type: t37.tsTypeLiteral(statement.body.body),
|
|
4271
4478
|
// 将接口体转换为类型字面量
|
|
4272
4479
|
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
4273
4480
|
// 检查是否有泛型参数
|
|
4274
4481
|
});
|
|
4275
4482
|
continue;
|
|
4276
4483
|
}
|
|
4277
|
-
if (
|
|
4484
|
+
if (t37.isTSTypeAliasDeclaration(statement)) {
|
|
4278
4485
|
declarations.set(statement.id.name, {
|
|
4279
4486
|
type: statement.typeAnnotation,
|
|
4280
4487
|
// 直接使用类型注解
|
|
@@ -4283,16 +4490,16 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
4283
4490
|
});
|
|
4284
4491
|
continue;
|
|
4285
4492
|
}
|
|
4286
|
-
if (
|
|
4493
|
+
if (t37.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
4287
4494
|
const declaration = statement.declaration;
|
|
4288
|
-
if (
|
|
4495
|
+
if (t37.isTSInterfaceDeclaration(declaration)) {
|
|
4289
4496
|
declarations.set(declaration.id.name, {
|
|
4290
|
-
type:
|
|
4497
|
+
type: t37.tsTypeLiteral(declaration.body.body),
|
|
4291
4498
|
// 将接口体转换为类型字面量
|
|
4292
4499
|
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
4293
4500
|
// 检查是否有泛型参数
|
|
4294
4501
|
});
|
|
4295
|
-
} else if (
|
|
4502
|
+
} else if (t37.isTSTypeAliasDeclaration(declaration)) {
|
|
4296
4503
|
declarations.set(declaration.id.name, {
|
|
4297
4504
|
type: declaration.typeAnnotation,
|
|
4298
4505
|
// 直接使用类型注解
|
|
@@ -4305,22 +4512,22 @@ function collectLocalTypeDeclarations(path8) {
|
|
|
4305
4512
|
return declarations;
|
|
4306
4513
|
}
|
|
4307
4514
|
function resolvePropName2(key) {
|
|
4308
|
-
if (
|
|
4515
|
+
if (t37.isIdentifier(key)) {
|
|
4309
4516
|
return key.name;
|
|
4310
4517
|
}
|
|
4311
|
-
if (
|
|
4518
|
+
if (t37.isStringLiteral(key)) {
|
|
4312
4519
|
return key.value;
|
|
4313
4520
|
}
|
|
4314
|
-
if (
|
|
4521
|
+
if (t37.isNumericLiteral(key)) {
|
|
4315
4522
|
return String(key.value);
|
|
4316
4523
|
}
|
|
4317
4524
|
return null;
|
|
4318
4525
|
}
|
|
4319
4526
|
function resolveCallableType(tsType) {
|
|
4320
|
-
if (
|
|
4527
|
+
if (t37.isTSFunctionType(tsType)) {
|
|
4321
4528
|
return tsType;
|
|
4322
4529
|
}
|
|
4323
|
-
if (
|
|
4530
|
+
if (t37.isTSParenthesizedType(tsType)) {
|
|
4324
4531
|
return resolveCallableType(tsType.typeAnnotation);
|
|
4325
4532
|
}
|
|
4326
4533
|
return null;
|
|
@@ -4329,10 +4536,10 @@ function resolveCallableType(tsType) {
|
|
|
4329
4536
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
4330
4537
|
var SLOT_DEFAULT_NAME2 = "default";
|
|
4331
4538
|
function resolveSlotType(tsType, options) {
|
|
4332
|
-
if (
|
|
4539
|
+
if (t38.isTSParenthesizedType(tsType)) {
|
|
4333
4540
|
return resolveSlotType(tsType.typeAnnotation, options);
|
|
4334
4541
|
}
|
|
4335
|
-
if (
|
|
4542
|
+
if (t38.isTSTypeReference(tsType)) {
|
|
4336
4543
|
let shouldRecordReactNode = false;
|
|
4337
4544
|
if (tsType.typeParameters?.params.length) {
|
|
4338
4545
|
const params = [];
|
|
@@ -4348,11 +4555,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4348
4555
|
};
|
|
4349
4556
|
}
|
|
4350
4557
|
return {
|
|
4351
|
-
type:
|
|
4558
|
+
type: t38.tsTypeReference(tsType.typeName, t38.tsTypeParameterInstantiation(params)),
|
|
4352
4559
|
shouldRecordReactNode
|
|
4353
4560
|
};
|
|
4354
4561
|
}
|
|
4355
|
-
if (!
|
|
4562
|
+
if (!t38.isIdentifier(tsType.typeName)) {
|
|
4356
4563
|
return {
|
|
4357
4564
|
type: tsType,
|
|
4358
4565
|
shouldRecordReactNode: false
|
|
@@ -4383,7 +4590,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4383
4590
|
}
|
|
4384
4591
|
return resolved;
|
|
4385
4592
|
}
|
|
4386
|
-
if (
|
|
4593
|
+
if (t38.isTSIntersectionType(tsType)) {
|
|
4387
4594
|
const types = [];
|
|
4388
4595
|
let shouldRecordReactNode = false;
|
|
4389
4596
|
for (const item of tsType.types) {
|
|
@@ -4406,11 +4613,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4406
4613
|
};
|
|
4407
4614
|
}
|
|
4408
4615
|
return {
|
|
4409
|
-
type:
|
|
4616
|
+
type: t38.tsIntersectionType(types),
|
|
4410
4617
|
shouldRecordReactNode
|
|
4411
4618
|
};
|
|
4412
4619
|
}
|
|
4413
|
-
if (
|
|
4620
|
+
if (t38.isTSUnionType(tsType)) {
|
|
4414
4621
|
const types = [];
|
|
4415
4622
|
let shouldRecordReactNode = false;
|
|
4416
4623
|
for (const item of tsType.types) {
|
|
@@ -4433,11 +4640,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4433
4640
|
};
|
|
4434
4641
|
}
|
|
4435
4642
|
return {
|
|
4436
|
-
type:
|
|
4643
|
+
type: t38.tsUnionType(types),
|
|
4437
4644
|
shouldRecordReactNode
|
|
4438
4645
|
};
|
|
4439
4646
|
}
|
|
4440
|
-
if (
|
|
4647
|
+
if (t38.isTSTypeLiteral(tsType)) {
|
|
4441
4648
|
const members = [];
|
|
4442
4649
|
let shouldRecordReactNode = false;
|
|
4443
4650
|
for (const item of tsType.members) {
|
|
@@ -4452,11 +4659,11 @@ function resolveSlotType(tsType, options) {
|
|
|
4452
4659
|
};
|
|
4453
4660
|
}
|
|
4454
4661
|
return {
|
|
4455
|
-
type:
|
|
4662
|
+
type: t38.tsTypeLiteral(members),
|
|
4456
4663
|
shouldRecordReactNode
|
|
4457
4664
|
};
|
|
4458
4665
|
}
|
|
4459
|
-
if (
|
|
4666
|
+
if (t38.isTSFunctionType(tsType)) {
|
|
4460
4667
|
const props = buildSlotPropSignature(
|
|
4461
4668
|
SLOT_DEFAULT_NAME2,
|
|
4462
4669
|
cloneCallableParams(tsType.parameters),
|
|
@@ -4464,7 +4671,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4464
4671
|
// 默认插槽不是可选的
|
|
4465
4672
|
);
|
|
4466
4673
|
return {
|
|
4467
|
-
type:
|
|
4674
|
+
type: t38.tsTypeLiteral([props]),
|
|
4468
4675
|
shouldRecordReactNode: true
|
|
4469
4676
|
// 函数类型总是需要记录 ReactNode
|
|
4470
4677
|
};
|
|
@@ -4475,7 +4682,7 @@ function resolveSlotType(tsType, options) {
|
|
|
4475
4682
|
};
|
|
4476
4683
|
}
|
|
4477
4684
|
function resolveSlotPropFromMember(member) {
|
|
4478
|
-
if (
|
|
4685
|
+
if (t38.isTSMethodSignature(member)) {
|
|
4479
4686
|
const rawName = resolvePropName2(member.key);
|
|
4480
4687
|
if (!rawName) {
|
|
4481
4688
|
return {
|
|
@@ -4490,7 +4697,7 @@ function resolveSlotPropFromMember(member) {
|
|
|
4490
4697
|
// 方法签名总是可调用,需要记录 ReactNode
|
|
4491
4698
|
};
|
|
4492
4699
|
}
|
|
4493
|
-
if (
|
|
4700
|
+
if (t38.isTSPropertySignature(member)) {
|
|
4494
4701
|
const rawName = resolvePropName2(member.key);
|
|
4495
4702
|
if (!rawName) {
|
|
4496
4703
|
return {
|
|
@@ -4513,7 +4720,7 @@ function resolveSlotPropFromMember(member) {
|
|
|
4513
4720
|
// 可调用属性需要记录 ReactNode
|
|
4514
4721
|
};
|
|
4515
4722
|
}
|
|
4516
|
-
if (
|
|
4723
|
+
if (t38.isTSCallSignatureDeclaration(member)) {
|
|
4517
4724
|
const params = cloneCallableParams(member.parameters);
|
|
4518
4725
|
return {
|
|
4519
4726
|
member: buildSlotPropSignature(SLOT_DEFAULT_NAME2, params, true),
|
|
@@ -4553,7 +4760,7 @@ function resolveDefineSlotsIface(path8, ctx) {
|
|
|
4553
4760
|
}
|
|
4554
4761
|
|
|
4555
4762
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/template-slots.ts
|
|
4556
|
-
import * as
|
|
4763
|
+
import * as t39 from "@babel/types";
|
|
4557
4764
|
function resolveTemplateSlotIface(ctx) {
|
|
4558
4765
|
if (ctx.inputType !== "sfc") return;
|
|
4559
4766
|
const {
|
|
@@ -4571,7 +4778,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
4571
4778
|
}
|
|
4572
4779
|
if (tsMembers.length) {
|
|
4573
4780
|
recordReactNode(ctx);
|
|
4574
|
-
slotTypes.push(
|
|
4781
|
+
slotTypes.push(t39.tsTypeLiteral(tsMembers));
|
|
4575
4782
|
}
|
|
4576
4783
|
}
|
|
4577
4784
|
|
|
@@ -4623,9 +4830,9 @@ function resolveCompIProps(ctx, ast) {
|
|
|
4623
4830
|
}
|
|
4624
4831
|
const n = declaredOptions.name || "Comp";
|
|
4625
4832
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
4626
|
-
const typeNode =
|
|
4627
|
-
const typeAliasDecl =
|
|
4628
|
-
const exportDecl =
|
|
4833
|
+
const typeNode = t40.tsIntersectionType(tsTypes);
|
|
4834
|
+
const typeAliasDecl = t40.tsTypeAliasDeclaration(t40.identifier(ns), null, typeNode);
|
|
4835
|
+
const exportDecl = t40.exportNamedDeclaration(typeAliasDecl);
|
|
4629
4836
|
propsTSIface.name = ns;
|
|
4630
4837
|
const scriptIR = getScriptIR(ctx);
|
|
4631
4838
|
scriptIR.exports.push(exportDecl);
|
|
@@ -4633,15 +4840,15 @@ function resolveCompIProps(ctx, ast) {
|
|
|
4633
4840
|
}
|
|
4634
4841
|
|
|
4635
4842
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-use-attrs.ts
|
|
4636
|
-
import * as
|
|
4843
|
+
import * as t41 from "@babel/types";
|
|
4637
4844
|
function resolveUseAttrs(ctx) {
|
|
4638
4845
|
return {
|
|
4639
4846
|
VariableDeclarator(path8) {
|
|
4640
4847
|
const { init, id } = path8.node;
|
|
4641
4848
|
if (!init) return;
|
|
4642
4849
|
const initPath = path8.get("init");
|
|
4643
|
-
const propsIdentifier =
|
|
4644
|
-
if (
|
|
4850
|
+
const propsIdentifier = t41.identifier(ctx.propField);
|
|
4851
|
+
if (t41.isTSAsExpression(init) && isUseAttrsCall(init.expression)) {
|
|
4645
4852
|
const typeAssertion = createPropsTypeAssertion(propsIdentifier, init.typeAnnotation);
|
|
4646
4853
|
replaceNode(initPath, typeAssertion, init);
|
|
4647
4854
|
return;
|
|
@@ -4652,13 +4859,13 @@ function resolveUseAttrs(ctx) {
|
|
|
4652
4859
|
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
4653
4860
|
if (isTS) {
|
|
4654
4861
|
let typeAnnotation = null;
|
|
4655
|
-
if (
|
|
4862
|
+
if (t41.isIdentifier(id) && t41.isTSTypeAnnotation(id.typeAnnotation)) {
|
|
4656
4863
|
typeAnnotation = id.typeAnnotation.typeAnnotation;
|
|
4657
4864
|
id.typeAnnotation = null;
|
|
4658
4865
|
} else {
|
|
4659
|
-
typeAnnotation =
|
|
4660
|
-
|
|
4661
|
-
|
|
4866
|
+
typeAnnotation = t41.tsTypeReference(
|
|
4867
|
+
t41.identifier("Record"),
|
|
4868
|
+
t41.tsTypeParameterInstantiation([t41.tsStringKeyword(), t41.tsUnknownKeyword()])
|
|
4662
4869
|
);
|
|
4663
4870
|
}
|
|
4664
4871
|
const propsTypeAssertion = createPropsTypeAssertion(propsIdentifier, typeAnnotation);
|
|
@@ -4670,18 +4877,18 @@ function resolveUseAttrs(ctx) {
|
|
|
4670
4877
|
};
|
|
4671
4878
|
}
|
|
4672
4879
|
function isUseAttrsCall(expr) {
|
|
4673
|
-
return
|
|
4880
|
+
return t41.isCallExpression(expr) && isCalleeNamed(expr, VUE_API_MAP.useAttrs);
|
|
4674
4881
|
}
|
|
4675
4882
|
function createPropsTypeAssertion(propsIdentifier, typeAnnotation) {
|
|
4676
|
-
return
|
|
4883
|
+
return t41.tsAsExpression(propsIdentifier, typeAnnotation);
|
|
4677
4884
|
}
|
|
4678
4885
|
|
|
4679
4886
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
4680
|
-
import * as
|
|
4887
|
+
import * as t48 from "@babel/types";
|
|
4681
4888
|
|
|
4682
4889
|
// src/core/transform/sfc/script/shared/dependency-analyzer/index.ts
|
|
4683
4890
|
import { traverse as traverse2 } from "@babel/core";
|
|
4684
|
-
import * as
|
|
4891
|
+
import * as t46 from "@babel/types";
|
|
4685
4892
|
|
|
4686
4893
|
// src/core/transform/sfc/script/shared/dependency-analyzer/binding-utils.ts
|
|
4687
4894
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
@@ -4700,7 +4907,7 @@ function isReactiveBinding(node) {
|
|
|
4700
4907
|
}
|
|
4701
4908
|
|
|
4702
4909
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-checker.ts
|
|
4703
|
-
import * as
|
|
4910
|
+
import * as t42 from "@babel/types";
|
|
4704
4911
|
function isEligibleBindingSource(binding) {
|
|
4705
4912
|
if (binding.kind === "param") {
|
|
4706
4913
|
return false;
|
|
@@ -4710,57 +4917,57 @@ function isEligibleBindingSource(binding) {
|
|
|
4710
4917
|
const declaratorPath = getVariableDeclaratorPath(bindingPath);
|
|
4711
4918
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
4712
4919
|
const nodeInit = declaratorPath?.node.init;
|
|
4713
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
4714
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
4715
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
4920
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t42.isCallExpression(nodeInit) && t42.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
4921
|
+
const isHookCallVarBinding = !!declaratorPath && t42.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
4922
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t42.isArrowFunctionExpression(nodeInit) || t42.isFunctionExpression(nodeInit));
|
|
4716
4923
|
const isReactiveFunctionBinding = isFunctionBinding && (isReactiveBinding(declaratorPath?.node) || isReactiveBinding(bindingPath.node));
|
|
4717
4924
|
return isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isReactiveFunctionBinding;
|
|
4718
4925
|
}
|
|
4719
4926
|
function isReactValidDependencyExpr(node) {
|
|
4720
|
-
if (
|
|
4927
|
+
if (t42.isIdentifier(node)) {
|
|
4721
4928
|
return true;
|
|
4722
4929
|
}
|
|
4723
|
-
if (
|
|
4930
|
+
if (t42.isMemberExpression(node) || t42.isOptionalMemberExpression(node)) {
|
|
4724
4931
|
return isStaticMemberChain(node);
|
|
4725
4932
|
}
|
|
4726
4933
|
return false;
|
|
4727
4934
|
}
|
|
4728
4935
|
function isStaticMemberChain(node) {
|
|
4729
4936
|
let current = node;
|
|
4730
|
-
while (
|
|
4731
|
-
if (!current.computed && !
|
|
4937
|
+
while (t42.isMemberExpression(current) || t42.isOptionalMemberExpression(current)) {
|
|
4938
|
+
if (!current.computed && !t42.isIdentifier(current.property)) {
|
|
4732
4939
|
return false;
|
|
4733
4940
|
}
|
|
4734
|
-
if (current.computed && !
|
|
4941
|
+
if (current.computed && !t42.isStringLiteral(current.property) && !t42.isNumericLiteral(current.property)) {
|
|
4735
4942
|
return false;
|
|
4736
4943
|
}
|
|
4737
4944
|
current = current.object;
|
|
4738
4945
|
}
|
|
4739
|
-
return
|
|
4946
|
+
return t42.isIdentifier(current);
|
|
4740
4947
|
}
|
|
4741
4948
|
function isHookLikeCallee(callee) {
|
|
4742
|
-
if (
|
|
4949
|
+
if (t42.isIdentifier(callee)) {
|
|
4743
4950
|
return callee.name.startsWith("use");
|
|
4744
4951
|
}
|
|
4745
|
-
if (
|
|
4952
|
+
if (t42.isMemberExpression(callee) && !callee.computed && t42.isIdentifier(callee.property)) {
|
|
4746
4953
|
return callee.property.name.startsWith("use");
|
|
4747
4954
|
}
|
|
4748
4955
|
return false;
|
|
4749
4956
|
}
|
|
4750
4957
|
|
|
4751
4958
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-key.ts
|
|
4752
|
-
import * as
|
|
4959
|
+
import * as t43 from "@babel/types";
|
|
4753
4960
|
function getDependencyKey(exp) {
|
|
4754
|
-
if (
|
|
4961
|
+
if (t43.isIdentifier(exp)) {
|
|
4755
4962
|
return exp.name;
|
|
4756
4963
|
}
|
|
4757
|
-
if (
|
|
4964
|
+
if (t43.isMemberExpression(exp) || t43.isOptionalMemberExpression(exp)) {
|
|
4758
4965
|
const objectKey = getDependencyKey(exp.object);
|
|
4759
4966
|
const opt = exp.optional ? "?" : "";
|
|
4760
|
-
if (!exp.computed &&
|
|
4967
|
+
if (!exp.computed && t43.isIdentifier(exp.property)) {
|
|
4761
4968
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
4762
4969
|
}
|
|
4763
|
-
if (
|
|
4970
|
+
if (t43.isStringLiteral(exp.property) || t43.isNumericLiteral(exp.property)) {
|
|
4764
4971
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
4765
4972
|
}
|
|
4766
4973
|
return `${objectKey}${opt}[*]`;
|
|
@@ -4769,40 +4976,40 @@ function getDependencyKey(exp) {
|
|
|
4769
4976
|
}
|
|
4770
4977
|
|
|
4771
4978
|
// src/core/transform/sfc/script/shared/dependency-analyzer/dep-normalizer.ts
|
|
4772
|
-
import * as
|
|
4979
|
+
import * as t44 from "@babel/types";
|
|
4773
4980
|
function normalizeDependencyExpr(path8, rootName, ctx) {
|
|
4774
|
-
if (
|
|
4775
|
-
return
|
|
4981
|
+
if (t44.isIdentifier(path8.node)) {
|
|
4982
|
+
return t44.identifier(path8.node.name);
|
|
4776
4983
|
}
|
|
4777
|
-
if (
|
|
4984
|
+
if (t44.isMemberExpression(path8.node) || t44.isOptionalMemberExpression(path8.node)) {
|
|
4778
4985
|
if (rootName === ctx.propField) {
|
|
4779
4986
|
const safePropsExp = ensureOptionalForMemberChain(path8.node);
|
|
4780
4987
|
if (isReactValidDependencyExpr(safePropsExp)) {
|
|
4781
|
-
return
|
|
4988
|
+
return t44.cloneNode(safePropsExp, true);
|
|
4782
4989
|
}
|
|
4783
|
-
return
|
|
4990
|
+
return t44.identifier(rootName);
|
|
4784
4991
|
}
|
|
4785
4992
|
const normalizedExp = normalizeMemberForCallSite(path8, path8.node);
|
|
4786
|
-
const safeExp =
|
|
4993
|
+
const safeExp = t44.isMemberExpression(normalizedExp) || t44.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForMemberChain(normalizedExp) : normalizedExp;
|
|
4787
4994
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
4788
|
-
return
|
|
4995
|
+
return t44.cloneNode(safeExp, true);
|
|
4789
4996
|
}
|
|
4790
|
-
return
|
|
4997
|
+
return t44.identifier(rootName);
|
|
4791
4998
|
}
|
|
4792
4999
|
return null;
|
|
4793
5000
|
}
|
|
4794
5001
|
function normalizeSourcedDependency(exp) {
|
|
4795
|
-
if (
|
|
4796
|
-
return
|
|
5002
|
+
if (t44.isIdentifier(exp)) {
|
|
5003
|
+
return t44.identifier(exp.name);
|
|
4797
5004
|
}
|
|
4798
|
-
if (
|
|
5005
|
+
if (t44.isMemberExpression(exp) || t44.isOptionalMemberExpression(exp)) {
|
|
4799
5006
|
const root = findRootIdentifier(exp);
|
|
4800
5007
|
if (!root) return null;
|
|
4801
|
-
const safeExp =
|
|
5008
|
+
const safeExp = t44.isMemberExpression(exp) || t44.isOptionalMemberExpression(exp) ? ensureOptionalForMemberChain(exp) : exp;
|
|
4802
5009
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
4803
|
-
return
|
|
5010
|
+
return t44.cloneNode(safeExp, true);
|
|
4804
5011
|
}
|
|
4805
|
-
return
|
|
5012
|
+
return t44.identifier(root.name);
|
|
4806
5013
|
}
|
|
4807
5014
|
return null;
|
|
4808
5015
|
}
|
|
@@ -4812,7 +5019,7 @@ function normalizeMemberForCallSite(path8, node) {
|
|
|
4812
5019
|
if (!isDirectCallee) {
|
|
4813
5020
|
return node;
|
|
4814
5021
|
}
|
|
4815
|
-
if (!
|
|
5022
|
+
if (!t44.isExpression(node.object)) {
|
|
4816
5023
|
return node;
|
|
4817
5024
|
}
|
|
4818
5025
|
return node.object;
|
|
@@ -4824,20 +5031,20 @@ function ensureOptionalForMemberChain(node) {
|
|
|
4824
5031
|
const rebuildWithOptionalChain = (node2) => {
|
|
4825
5032
|
const safeObject = ensureOptionalForMemberChain(node2.object);
|
|
4826
5033
|
if (safeObject !== node2.object) {
|
|
4827
|
-
const property =
|
|
4828
|
-
return
|
|
5034
|
+
const property = t44.cloneNode(node2.property, true);
|
|
5035
|
+
return t44.optionalMemberExpression(safeObject, property, node2.computed, true);
|
|
4829
5036
|
}
|
|
4830
|
-
if (
|
|
4831
|
-
const object =
|
|
4832
|
-
const property =
|
|
4833
|
-
return
|
|
5037
|
+
if (t44.isMemberExpression(node2)) {
|
|
5038
|
+
const object = t44.cloneNode(node2.object, true);
|
|
5039
|
+
const property = t44.cloneNode(node2.property, true);
|
|
5040
|
+
return t44.optionalMemberExpression(object, property, node2.computed, true);
|
|
4834
5041
|
}
|
|
4835
5042
|
return node2;
|
|
4836
5043
|
};
|
|
4837
5044
|
return rebuildWithOptionalChain(node);
|
|
4838
5045
|
}
|
|
4839
5046
|
function hasTrailingMemberAccess(node) {
|
|
4840
|
-
return
|
|
5047
|
+
return t44.isMemberExpression(node.object) || t44.isOptionalMemberExpression(node.object);
|
|
4841
5048
|
}
|
|
4842
5049
|
|
|
4843
5050
|
// src/core/transform/sfc/script/shared/dependency-analyzer/shared-utils.ts
|
|
@@ -4852,7 +5059,7 @@ function isNestedMemberObject(path8) {
|
|
|
4852
5059
|
}
|
|
4853
5060
|
|
|
4854
5061
|
// src/core/transform/sfc/script/shared/dependency-analyzer/trace-utils.ts
|
|
4855
|
-
import * as
|
|
5062
|
+
import * as t45 from "@babel/types";
|
|
4856
5063
|
function traceBindingSource(binding, seen, depth) {
|
|
4857
5064
|
if (depth <= 0) return null;
|
|
4858
5065
|
const declaratorPath = getVariableDeclaratorPath(binding.path);
|
|
@@ -4864,7 +5071,7 @@ function traceBindingSource(binding, seen, depth) {
|
|
|
4864
5071
|
}
|
|
4865
5072
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
4866
5073
|
if (depth <= 0) return null;
|
|
4867
|
-
if (
|
|
5074
|
+
if (t45.isIdentifier(exp)) {
|
|
4868
5075
|
const sourceBinding = scope.getBinding(exp.name);
|
|
4869
5076
|
if (!sourceBinding) return null;
|
|
4870
5077
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -4872,13 +5079,13 @@ function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
|
4872
5079
|
}
|
|
4873
5080
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
4874
5081
|
}
|
|
4875
|
-
if (
|
|
5082
|
+
if (t45.isMemberExpression(exp) || t45.isOptionalMemberExpression(exp)) {
|
|
4876
5083
|
const root = findRootIdentifier(exp);
|
|
4877
5084
|
if (!root) return null;
|
|
4878
5085
|
const sourceBinding = scope.getBinding(root.name);
|
|
4879
5086
|
if (!sourceBinding) return null;
|
|
4880
5087
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
4881
|
-
return
|
|
5088
|
+
return t45.cloneNode(exp);
|
|
4882
5089
|
}
|
|
4883
5090
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
4884
5091
|
if (sourcedRoot) {
|
|
@@ -4886,17 +5093,17 @@ function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
|
4886
5093
|
if (rebuilt) {
|
|
4887
5094
|
return rebuilt;
|
|
4888
5095
|
}
|
|
4889
|
-
return
|
|
5096
|
+
return t45.cloneNode(sourcedRoot, true);
|
|
4890
5097
|
}
|
|
4891
5098
|
}
|
|
4892
5099
|
return null;
|
|
4893
5100
|
}
|
|
4894
5101
|
function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
4895
5102
|
const replacedObject = (() => {
|
|
4896
|
-
if (
|
|
4897
|
-
return
|
|
5103
|
+
if (t45.isIdentifier(node.object)) {
|
|
5104
|
+
return t45.cloneNode(nextRoot, true);
|
|
4898
5105
|
}
|
|
4899
|
-
if (
|
|
5106
|
+
if (t45.isMemberExpression(node.object) || t45.isOptionalMemberExpression(node.object)) {
|
|
4900
5107
|
return rebuildMemberWithNewRoot(node.object, nextRoot);
|
|
4901
5108
|
}
|
|
4902
5109
|
return null;
|
|
@@ -4904,15 +5111,15 @@ function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
|
4904
5111
|
if (!replacedObject) {
|
|
4905
5112
|
return null;
|
|
4906
5113
|
}
|
|
4907
|
-
const property =
|
|
4908
|
-
if (
|
|
4909
|
-
return
|
|
5114
|
+
const property = t45.cloneNode(node.property, true);
|
|
5115
|
+
if (t45.isMemberExpression(node)) {
|
|
5116
|
+
return t45.memberExpression(
|
|
4910
5117
|
replacedObject,
|
|
4911
5118
|
property,
|
|
4912
5119
|
node.computed
|
|
4913
5120
|
);
|
|
4914
5121
|
}
|
|
4915
|
-
return
|
|
5122
|
+
return t45.optionalMemberExpression(
|
|
4916
5123
|
replacedObject,
|
|
4917
5124
|
property,
|
|
4918
5125
|
node.computed,
|
|
@@ -4923,9 +5130,9 @@ function rebuildMemberWithNewRoot(node, nextRoot) {
|
|
|
4923
5130
|
// src/core/transform/sfc/script/shared/dependency-analyzer/index.ts
|
|
4924
5131
|
function analyzeDeps(node, ctx, parentPath) {
|
|
4925
5132
|
if (!parentPath) {
|
|
4926
|
-
return
|
|
5133
|
+
return t46.arrayExpression([]);
|
|
4927
5134
|
}
|
|
4928
|
-
const isFnExpr =
|
|
5135
|
+
const isFnExpr = t46.isArrowFunctionExpression(node) || t46.isFunctionExpression(node);
|
|
4929
5136
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
4930
5137
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
4931
5138
|
const deps = /* @__PURE__ */ new Map();
|
|
@@ -4935,13 +5142,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
4935
5142
|
}
|
|
4936
5143
|
const analyzeTargetPath = parentPath && parentPath.node === analyzeTarget ? parentPath : null;
|
|
4937
5144
|
if (analyzeTargetPath) {
|
|
4938
|
-
if (
|
|
5145
|
+
if (t46.isMemberExpression(analyzeTarget) || t46.isOptionalMemberExpression(analyzeTarget)) {
|
|
4939
5146
|
const rootId = findRootIdentifier(analyzeTarget);
|
|
4940
5147
|
if (rootId) {
|
|
4941
5148
|
tryAddDependency(analyzeTargetPath, rootId.name, analyzeTargetPath.scope);
|
|
4942
5149
|
processedIdentifiers.add(rootId);
|
|
4943
5150
|
}
|
|
4944
|
-
} else if (
|
|
5151
|
+
} else if (t46.isIdentifier(analyzeTarget)) {
|
|
4945
5152
|
tryAddDependency(analyzeTargetPath, analyzeTarget.name, analyzeTargetPath.scope);
|
|
4946
5153
|
}
|
|
4947
5154
|
}
|
|
@@ -4993,55 +5200,54 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
4993
5200
|
}
|
|
4994
5201
|
}
|
|
4995
5202
|
}
|
|
4996
|
-
return
|
|
5203
|
+
return t46.arrayExpression(Array.from(deps.values()));
|
|
4997
5204
|
}
|
|
4998
5205
|
|
|
4999
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-
|
|
5000
|
-
|
|
5206
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
5207
|
+
import * as t47 from "@babel/types";
|
|
5208
|
+
function resolveRenameAdapter(ctx) {
|
|
5001
5209
|
return {
|
|
5002
5210
|
"CallExpression|Identifier"(path8) {
|
|
5003
5211
|
const node = path8.node;
|
|
5004
|
-
const
|
|
5005
|
-
|
|
5006
|
-
if (
|
|
5212
|
+
const isCallNode = t47.isCallExpression(node);
|
|
5213
|
+
let apiName = "";
|
|
5214
|
+
if (t47.isIdentifier(node)) {
|
|
5215
|
+
apiName = node.name;
|
|
5216
|
+
} else if (isCallNode && t47.isIdentifier(node.callee)) {
|
|
5217
|
+
apiName = node.callee.name;
|
|
5218
|
+
}
|
|
5219
|
+
if (!apiName) {
|
|
5220
|
+
return;
|
|
5221
|
+
}
|
|
5222
|
+
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
5223
|
+
const routerAdapter = ADAPTER_RULES.router[apiName];
|
|
5224
|
+
const adapter = runtimeAdapter || routerAdapter;
|
|
5225
|
+
if (!adapter || adapter.type !== "rename") {
|
|
5007
5226
|
return;
|
|
5008
5227
|
}
|
|
5009
5228
|
if (!isVueApiReference(path8, apiName)) {
|
|
5010
5229
|
return;
|
|
5011
5230
|
}
|
|
5012
|
-
if (
|
|
5013
|
-
|
|
5231
|
+
if (adapter.isTrackable) {
|
|
5232
|
+
const reactiveType = getReactiveType(apiName);
|
|
5233
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
5234
|
+
setScriptNodeMeta(declaratorPath?.node, {
|
|
5235
|
+
is_reactive: true,
|
|
5236
|
+
reactive_type: reactiveType
|
|
5237
|
+
});
|
|
5238
|
+
}
|
|
5239
|
+
if (isCallNode) {
|
|
5240
|
+
replaceCallName(node, adapter.target);
|
|
5014
5241
|
} else {
|
|
5015
5242
|
replaceIdName(node, adapter.target);
|
|
5016
5243
|
}
|
|
5244
|
+
if (adapter.package === PACKAGE_NAME.router && !ctx.route) {
|
|
5245
|
+
ctx.route = true;
|
|
5246
|
+
}
|
|
5017
5247
|
recordImport(ctx, adapter.package, adapter.target);
|
|
5018
5248
|
}
|
|
5019
5249
|
};
|
|
5020
5250
|
}
|
|
5021
|
-
function getApiName(node) {
|
|
5022
|
-
const isCallNode = t45.isCallExpression(node);
|
|
5023
|
-
let apiName = "";
|
|
5024
|
-
if (t45.isIdentifier(node)) {
|
|
5025
|
-
apiName = node.name;
|
|
5026
|
-
} else if (isCallNode && t45.isIdentifier(node.callee)) {
|
|
5027
|
-
apiName = node.callee.name;
|
|
5028
|
-
}
|
|
5029
|
-
return apiName;
|
|
5030
|
-
}
|
|
5031
|
-
function resolveCallNode(path8, adapter, ctx) {
|
|
5032
|
-
const { node } = path8;
|
|
5033
|
-
const { arguments: args } = node;
|
|
5034
|
-
if (!args.length) return;
|
|
5035
|
-
const fn = args[0];
|
|
5036
|
-
if (!t45.isArrowFunctionExpression(fn) && !t45.isFunctionExpression(fn)) {
|
|
5037
|
-
return;
|
|
5038
|
-
}
|
|
5039
|
-
const fnPath = path8.get("arguments")[0];
|
|
5040
|
-
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
5041
|
-
args.push(deps);
|
|
5042
|
-
replaceCallName(node, adapter.target);
|
|
5043
|
-
recordImport(ctx, adapter.package, adapter.target);
|
|
5044
|
-
}
|
|
5045
5251
|
function isVueApiReference(path8, apiName) {
|
|
5046
5252
|
if (path8.isIdentifier()) {
|
|
5047
5253
|
if (path8.parentPath.isCallExpression() && path8.parentPath.node.callee === path8.node) {
|
|
@@ -5051,12 +5257,11 @@ function isVueApiReference(path8, apiName) {
|
|
|
5051
5257
|
return false;
|
|
5052
5258
|
}
|
|
5053
5259
|
}
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
return isVueImportBinding(callee.scope.getBinding(apiName));
|
|
5260
|
+
const binding = path8.isCallExpression() ? path8.get("callee").scope.getBinding(apiName) : path8.scope.getBinding(apiName);
|
|
5261
|
+
if (binding) {
|
|
5262
|
+
return isVueImportBinding(binding);
|
|
5058
5263
|
}
|
|
5059
|
-
return
|
|
5264
|
+
return AUTO_IMPORTED_APIS.has(apiName);
|
|
5060
5265
|
}
|
|
5061
5266
|
function isVueImportBinding(binding) {
|
|
5062
5267
|
if (!binding) return false;
|
|
@@ -5065,7 +5270,7 @@ function isVueImportBinding(binding) {
|
|
|
5065
5270
|
return false;
|
|
5066
5271
|
}
|
|
5067
5272
|
const parent = bindingPath.parentPath?.node;
|
|
5068
|
-
if (!parent || !
|
|
5273
|
+
if (!parent || !t47.isImportDeclaration(parent)) {
|
|
5069
5274
|
return false;
|
|
5070
5275
|
}
|
|
5071
5276
|
const source = parent.source.value.toLowerCase();
|
|
@@ -5078,6 +5283,53 @@ function isVueImportBinding(binding) {
|
|
|
5078
5283
|
return VUE_PACKAGES.some((name) => source === name || source.startsWith(`${name}/`));
|
|
5079
5284
|
}
|
|
5080
5285
|
|
|
5286
|
+
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
5287
|
+
function resolveAnalysisOnlyAdapter(ctx) {
|
|
5288
|
+
return {
|
|
5289
|
+
"CallExpression|Identifier"(path8) {
|
|
5290
|
+
const node = path8.node;
|
|
5291
|
+
const apiName = getApiName(node);
|
|
5292
|
+
const adapter = ADAPTER_RULES.runtime[apiName];
|
|
5293
|
+
if (!adapter || adapter.type !== "analyzed-deps") {
|
|
5294
|
+
return;
|
|
5295
|
+
}
|
|
5296
|
+
if (!isVueApiReference(path8, apiName)) {
|
|
5297
|
+
return;
|
|
5298
|
+
}
|
|
5299
|
+
if (t48.isCallExpression(node)) {
|
|
5300
|
+
resolveCallNode(path8, adapter, ctx);
|
|
5301
|
+
} else {
|
|
5302
|
+
replaceIdName(node, adapter.target);
|
|
5303
|
+
}
|
|
5304
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
5305
|
+
}
|
|
5306
|
+
};
|
|
5307
|
+
}
|
|
5308
|
+
function getApiName(node) {
|
|
5309
|
+
const isCallNode = t48.isCallExpression(node);
|
|
5310
|
+
let apiName = "";
|
|
5311
|
+
if (t48.isIdentifier(node)) {
|
|
5312
|
+
apiName = node.name;
|
|
5313
|
+
} else if (isCallNode && t48.isIdentifier(node.callee)) {
|
|
5314
|
+
apiName = node.callee.name;
|
|
5315
|
+
}
|
|
5316
|
+
return apiName;
|
|
5317
|
+
}
|
|
5318
|
+
function resolveCallNode(path8, adapter, ctx) {
|
|
5319
|
+
const { node } = path8;
|
|
5320
|
+
const { arguments: args } = node;
|
|
5321
|
+
if (!args.length) return;
|
|
5322
|
+
const fn = args[0];
|
|
5323
|
+
if (!t48.isArrowFunctionExpression(fn) && !t48.isFunctionExpression(fn)) {
|
|
5324
|
+
return;
|
|
5325
|
+
}
|
|
5326
|
+
const fnPath = path8.get("arguments")[0];
|
|
5327
|
+
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
5328
|
+
args.push(deps);
|
|
5329
|
+
replaceCallName(node, adapter.target);
|
|
5330
|
+
recordImport(ctx, adapter.package, adapter.target);
|
|
5331
|
+
}
|
|
5332
|
+
|
|
5081
5333
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-arrow-deps.ts
|
|
5082
5334
|
function resolveArrowFnDeps(ctx, ast) {
|
|
5083
5335
|
return {
|
|
@@ -5133,7 +5385,7 @@ function isSkip(path8) {
|
|
|
5133
5385
|
}
|
|
5134
5386
|
|
|
5135
5387
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
5136
|
-
import * as
|
|
5388
|
+
import * as t49 from "@babel/types";
|
|
5137
5389
|
function resolveElementRef(ctx) {
|
|
5138
5390
|
return {
|
|
5139
5391
|
CallExpression(path8) {
|
|
@@ -5151,14 +5403,14 @@ function resolveElementRef(ctx) {
|
|
|
5151
5403
|
}
|
|
5152
5404
|
if (isCompRefBindings) {
|
|
5153
5405
|
const varDeclaratorPath = getVariableDeclaratorPath(path8)?.node;
|
|
5154
|
-
if (!
|
|
5406
|
+
if (!t49.isIdentifier(varDeclaratorPath?.id)) {
|
|
5155
5407
|
return;
|
|
5156
5408
|
}
|
|
5157
5409
|
const varName = varDeclaratorPath.id.name;
|
|
5158
5410
|
const compRef = refBindings.componentRefs[varName];
|
|
5159
5411
|
if (!compRef) return;
|
|
5160
5412
|
}
|
|
5161
|
-
node.arguments[0] =
|
|
5413
|
+
node.arguments[0] = t49.identifier("null");
|
|
5162
5414
|
resolveTypeParameters(ctx, path8);
|
|
5163
5415
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
5164
5416
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
@@ -5183,27 +5435,27 @@ function resolveTypeParameters(ctx, path8) {
|
|
|
5183
5435
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
5184
5436
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
5185
5437
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
5186
|
-
node.typeParameters =
|
|
5438
|
+
node.typeParameters = t49.tsTypeParameterInstantiation([t49.tsTypeReference(t49.identifier(type))]);
|
|
5187
5439
|
}
|
|
5188
5440
|
}
|
|
5189
5441
|
function resolveRefValueToCurrent(path8) {
|
|
5190
5442
|
const { node } = path8;
|
|
5191
|
-
if (node.computed || !
|
|
5443
|
+
if (node.computed || !t49.isIdentifier(node.property) || node.property.name !== "value") {
|
|
5192
5444
|
return;
|
|
5193
5445
|
}
|
|
5194
5446
|
const rootPath = findRootVariablePath(path8);
|
|
5195
|
-
if (!rootPath?.node || !
|
|
5447
|
+
if (!rootPath?.node || !t49.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
5196
5448
|
return;
|
|
5197
5449
|
}
|
|
5198
5450
|
const rootId = findRootIdentifier(node);
|
|
5199
|
-
if (!
|
|
5451
|
+
if (!t49.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
5200
5452
|
return;
|
|
5201
5453
|
}
|
|
5202
5454
|
node.property.name = "current";
|
|
5203
5455
|
}
|
|
5204
5456
|
|
|
5205
5457
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
5206
|
-
import * as
|
|
5458
|
+
import * as t50 from "@babel/types";
|
|
5207
5459
|
function resolveExprMemo(ctx, ast) {
|
|
5208
5460
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
5209
5461
|
return {
|
|
@@ -5215,11 +5467,11 @@ function resolveExprMemo(ctx, ast) {
|
|
|
5215
5467
|
if (!atComponentOrHookRoot(path8, ast.program, isScriptFile)) {
|
|
5216
5468
|
return false;
|
|
5217
5469
|
}
|
|
5218
|
-
if (!
|
|
5470
|
+
if (!t50.isVariableDeclaration(path8.parent) || path8.parent.kind !== "const") {
|
|
5219
5471
|
return false;
|
|
5220
5472
|
}
|
|
5221
|
-
if (
|
|
5222
|
-
if (
|
|
5473
|
+
if (t50.isFunction(init)) return false;
|
|
5474
|
+
if (t50.isCallExpression(init) && t50.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
5223
5475
|
return false;
|
|
5224
5476
|
}
|
|
5225
5477
|
return true;
|
|
@@ -5238,16 +5490,16 @@ function resolveExprMemo(ctx, ast) {
|
|
|
5238
5490
|
}
|
|
5239
5491
|
|
|
5240
5492
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
5241
|
-
import * as
|
|
5493
|
+
import * as t51 from "@babel/types";
|
|
5242
5494
|
function resolveLintRules(ctx, ast) {
|
|
5243
5495
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
5244
5496
|
return {
|
|
5245
5497
|
CallExpression(path8) {
|
|
5246
5498
|
const { node, parentPath } = path8;
|
|
5247
|
-
if (!
|
|
5499
|
+
if (!t51.isIdentifier(node.callee)) return;
|
|
5248
5500
|
const { name: callName } = node.callee;
|
|
5249
|
-
const addLog = (
|
|
5250
|
-
logger.error(
|
|
5501
|
+
const addLog = (t56) => {
|
|
5502
|
+
logger.error(t56, {
|
|
5251
5503
|
file: ctx.filename,
|
|
5252
5504
|
source: ctx.scriptData.source,
|
|
5253
5505
|
loc: node.loc
|
|
@@ -5292,7 +5544,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
5292
5544
|
|
|
5293
5545
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
5294
5546
|
import { generate as generate2 } from "@babel/generator";
|
|
5295
|
-
import * as
|
|
5547
|
+
import * as t52 from "@babel/types";
|
|
5296
5548
|
function resolveProvide(ctx) {
|
|
5297
5549
|
const { filename, scriptData, inputType } = ctx;
|
|
5298
5550
|
if (inputType !== "sfc") {
|
|
@@ -5335,13 +5587,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
5335
5587
|
function assignProviderValue(target, key, value) {
|
|
5336
5588
|
const getRawExp = (exp) => {
|
|
5337
5589
|
if (!exp) return "''";
|
|
5338
|
-
if (
|
|
5590
|
+
if (t52.isStringLiteral(exp)) {
|
|
5339
5591
|
return JSON.stringify(exp.value);
|
|
5340
5592
|
}
|
|
5341
|
-
if (
|
|
5593
|
+
if (t52.isNumericLiteral(exp)) {
|
|
5342
5594
|
return exp.value.toString();
|
|
5343
5595
|
}
|
|
5344
|
-
if (
|
|
5596
|
+
if (t52.isIdentifier(exp)) {
|
|
5345
5597
|
return exp.name;
|
|
5346
5598
|
}
|
|
5347
5599
|
try {
|
|
@@ -5356,96 +5608,13 @@ function assignProviderValue(target, key, value) {
|
|
|
5356
5608
|
target.provide = {};
|
|
5357
5609
|
}
|
|
5358
5610
|
|
|
5359
|
-
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
5360
|
-
import * as t50 from "@babel/types";
|
|
5361
|
-
function resolveRenameAdapter(ctx) {
|
|
5362
|
-
return {
|
|
5363
|
-
"CallExpression|Identifier"(path8) {
|
|
5364
|
-
const node = path8.node;
|
|
5365
|
-
const isCallNode = t50.isCallExpression(node);
|
|
5366
|
-
let apiName = "";
|
|
5367
|
-
if (t50.isIdentifier(node)) {
|
|
5368
|
-
apiName = node.name;
|
|
5369
|
-
} else if (isCallNode && t50.isIdentifier(node.callee)) {
|
|
5370
|
-
apiName = node.callee.name;
|
|
5371
|
-
}
|
|
5372
|
-
if (!apiName) {
|
|
5373
|
-
return;
|
|
5374
|
-
}
|
|
5375
|
-
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
5376
|
-
const routerAdapter = ADAPTER_RULES.router[apiName];
|
|
5377
|
-
const adapter = runtimeAdapter || routerAdapter;
|
|
5378
|
-
if (!adapter || adapter.type !== "rename") {
|
|
5379
|
-
return;
|
|
5380
|
-
}
|
|
5381
|
-
if (!isVueApiReference2(path8, apiName)) {
|
|
5382
|
-
return;
|
|
5383
|
-
}
|
|
5384
|
-
if (adapter.isTrackable) {
|
|
5385
|
-
const reactiveType = getReactiveType(apiName);
|
|
5386
|
-
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
5387
|
-
setScriptNodeMeta(declaratorPath?.node, {
|
|
5388
|
-
is_reactive: true,
|
|
5389
|
-
reactive_type: reactiveType
|
|
5390
|
-
});
|
|
5391
|
-
}
|
|
5392
|
-
if (isCallNode) {
|
|
5393
|
-
replaceCallName(node, adapter.target);
|
|
5394
|
-
} else {
|
|
5395
|
-
replaceIdName(node, adapter.target);
|
|
5396
|
-
}
|
|
5397
|
-
if (adapter.package === PACKAGE_NAME.router && !ctx.route) {
|
|
5398
|
-
ctx.route = true;
|
|
5399
|
-
}
|
|
5400
|
-
recordImport(ctx, adapter.package, adapter.target);
|
|
5401
|
-
}
|
|
5402
|
-
};
|
|
5403
|
-
}
|
|
5404
|
-
function isVueApiReference2(path8, apiName) {
|
|
5405
|
-
const whitelist = [VUE_API_MAP.defineAsyncComponent];
|
|
5406
|
-
if (whitelist.includes(apiName)) {
|
|
5407
|
-
return true;
|
|
5408
|
-
}
|
|
5409
|
-
if (path8.isIdentifier()) {
|
|
5410
|
-
if (path8.parentPath.isCallExpression() && path8.parentPath.node.callee === path8.node) {
|
|
5411
|
-
return false;
|
|
5412
|
-
}
|
|
5413
|
-
if (!path8.isReferencedIdentifier()) {
|
|
5414
|
-
return false;
|
|
5415
|
-
}
|
|
5416
|
-
}
|
|
5417
|
-
if (path8.isCallExpression()) {
|
|
5418
|
-
const callee = path8.get("callee");
|
|
5419
|
-
if (!callee.isIdentifier()) return false;
|
|
5420
|
-
return isVueImportBinding2(callee.scope.getBinding(apiName));
|
|
5421
|
-
}
|
|
5422
|
-
return isVueImportBinding2(path8.scope.getBinding(apiName));
|
|
5423
|
-
}
|
|
5424
|
-
function isVueImportBinding2(binding) {
|
|
5425
|
-
if (!binding) return false;
|
|
5426
|
-
const bindingPath = binding.path;
|
|
5427
|
-
if (!bindingPath.isImportSpecifier() && !bindingPath.isImportDefaultSpecifier() && !bindingPath.isImportNamespaceSpecifier()) {
|
|
5428
|
-
return false;
|
|
5429
|
-
}
|
|
5430
|
-
const parent = bindingPath.parentPath?.node;
|
|
5431
|
-
if (!parent || !t50.isImportDeclaration(parent)) {
|
|
5432
|
-
return false;
|
|
5433
|
-
}
|
|
5434
|
-
const source = parent.source.value.toLowerCase();
|
|
5435
|
-
if (source.startsWith("@vue/")) {
|
|
5436
|
-
return true;
|
|
5437
|
-
}
|
|
5438
|
-
if (source === "vue-router" || source.startsWith("vue-router/")) {
|
|
5439
|
-
return true;
|
|
5440
|
-
}
|
|
5441
|
-
return VUE_PACKAGES.some((name) => source === name || source.startsWith(`${name}/`));
|
|
5442
|
-
}
|
|
5443
|
-
|
|
5444
5611
|
// src/core/transform/sfc/script/syntax-processor/index.ts
|
|
5445
5612
|
function processVueSyntax2(ast, ctx) {
|
|
5446
5613
|
vueSyntaxProcessor2(ast, ctx, {
|
|
5447
5614
|
preprocess: {
|
|
5448
5615
|
applyBabel: [
|
|
5616
|
+
// feature: https://github.com/vureact-js/core/issues/63
|
|
5617
|
+
resolveWithDefaultsOptions,
|
|
5449
5618
|
resolvePropsIface,
|
|
5450
5619
|
resolveEmitsTopLevelTypes,
|
|
5451
5620
|
resolveDefineOptions,
|
|
@@ -5474,6 +5643,8 @@ function processVueSyntax2(ast, ctx) {
|
|
|
5474
5643
|
},
|
|
5475
5644
|
postprocess: {
|
|
5476
5645
|
applyBabel: [
|
|
5646
|
+
// feature: https://github.com/vureact-js/core/issues/63
|
|
5647
|
+
resolveWithDefaults,
|
|
5477
5648
|
// 该 resolver 需确保放在所有类型处理之后,移除之前
|
|
5478
5649
|
resolveVueTypeAsAny,
|
|
5479
5650
|
resolveRuntimeImports,
|
|
@@ -5570,15 +5741,15 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
5570
5741
|
}
|
|
5571
5742
|
|
|
5572
5743
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
5573
|
-
import * as
|
|
5744
|
+
import * as t54 from "@babel/types";
|
|
5574
5745
|
|
|
5575
5746
|
// src/shared/string-code-types.ts
|
|
5576
5747
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
5577
|
-
import * as
|
|
5748
|
+
import * as t53 from "@babel/types";
|
|
5578
5749
|
var strCodeTypes = {
|
|
5579
|
-
isIdentifier:
|
|
5750
|
+
isIdentifier: isIdentifier32,
|
|
5580
5751
|
isSimpleExpression,
|
|
5581
|
-
isStringLiteral:
|
|
5752
|
+
isStringLiteral: isStringLiteral15
|
|
5582
5753
|
};
|
|
5583
5754
|
function isSimpleExpression(code, excludeVar = false) {
|
|
5584
5755
|
let node;
|
|
@@ -5587,38 +5758,38 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
5587
5758
|
} catch {
|
|
5588
5759
|
return false;
|
|
5589
5760
|
}
|
|
5590
|
-
if (
|
|
5761
|
+
if (t53.isLiteral(node)) {
|
|
5591
5762
|
return true;
|
|
5592
5763
|
}
|
|
5593
|
-
if (!excludeVar &&
|
|
5764
|
+
if (!excludeVar && t53.isIdentifier(node)) {
|
|
5594
5765
|
return true;
|
|
5595
5766
|
}
|
|
5596
|
-
if (
|
|
5597
|
-
return isSimpleExpression(node.object) &&
|
|
5767
|
+
if (t53.isMemberExpression(node)) {
|
|
5768
|
+
return isSimpleExpression(node.object) && t53.isIdentifier(node.property);
|
|
5598
5769
|
}
|
|
5599
|
-
if (
|
|
5770
|
+
if (t53.isObjectExpression(node) || t53.isArrayExpression(node)) {
|
|
5600
5771
|
return false;
|
|
5601
5772
|
}
|
|
5602
|
-
if (
|
|
5773
|
+
if (t53.isCallExpression(node) || t53.isAssignmentExpression(node)) {
|
|
5603
5774
|
return false;
|
|
5604
5775
|
}
|
|
5605
|
-
if (
|
|
5776
|
+
if (t53.isBinaryExpression(node) || t53.isUnaryExpression(node)) {
|
|
5606
5777
|
return true;
|
|
5607
5778
|
}
|
|
5608
5779
|
return false;
|
|
5609
5780
|
}
|
|
5610
|
-
function
|
|
5781
|
+
function isIdentifier32(code) {
|
|
5611
5782
|
try {
|
|
5612
5783
|
const node = parseExpression3(code);
|
|
5613
|
-
return
|
|
5784
|
+
return t53.isIdentifier(node);
|
|
5614
5785
|
} catch {
|
|
5615
5786
|
return false;
|
|
5616
5787
|
}
|
|
5617
5788
|
}
|
|
5618
|
-
function
|
|
5789
|
+
function isStringLiteral15(code) {
|
|
5619
5790
|
try {
|
|
5620
5791
|
const node = parseExpression3(code);
|
|
5621
|
-
return
|
|
5792
|
+
return t53.isStringLiteral(node);
|
|
5622
5793
|
} catch {
|
|
5623
5794
|
return false;
|
|
5624
5795
|
}
|
|
@@ -5761,11 +5932,11 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
5761
5932
|
const rule = ADAPTER_RULES.runtime;
|
|
5762
5933
|
const setNameIdentifier = (target, valueName) => {
|
|
5763
5934
|
target.content = valueName;
|
|
5764
|
-
target.ast =
|
|
5935
|
+
target.ast = t54.jsxIdentifier(valueName);
|
|
5765
5936
|
};
|
|
5766
|
-
const setValueExpression = (target, content,
|
|
5937
|
+
const setValueExpression = (target, content, isStringLiteral16) => {
|
|
5767
5938
|
target.content = content;
|
|
5768
|
-
target.ast = resolveStringExpr(content, ctx,
|
|
5939
|
+
target.ast = resolveStringExpr(content, ctx, isStringLiteral16);
|
|
5769
5940
|
};
|
|
5770
5941
|
const createRuntimeCall = (fnName, args) => {
|
|
5771
5942
|
const fnArgs = args.filter(Boolean).join(",");
|
|
@@ -5774,13 +5945,13 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
5774
5945
|
const safeTypeAssertion = isTs ? valIsUndef ? "as never" : "" : "";
|
|
5775
5946
|
return `${fnName}(${fnArgs}) ${safeTypeAssertion}`;
|
|
5776
5947
|
};
|
|
5777
|
-
const applyRuntimeExpression = (expression, setName = false, nameIdentifier,
|
|
5948
|
+
const applyRuntimeExpression = (expression, setName = false, nameIdentifier, isStringLiteral16) => {
|
|
5778
5949
|
if (setName && nameIdentifier) {
|
|
5779
5950
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
5780
5951
|
}
|
|
5781
5952
|
const dir = rule.dir;
|
|
5782
5953
|
recordImport(ctx, dir.package, dir.target);
|
|
5783
|
-
setValueExpression(value.babelExp, expression,
|
|
5954
|
+
setValueExpression(value.babelExp, expression, isStringLiteral16);
|
|
5784
5955
|
};
|
|
5785
5956
|
if (ir.isKeyLessVBind) {
|
|
5786
5957
|
const dirKeyless = rule.dirKeyless;
|
|
@@ -6201,12 +6372,12 @@ function resolvePropertyIR(propsIR, ir, ctx, nodeIR, isDynamic = false) {
|
|
|
6201
6372
|
content = propsIR.value.content = parseStyleString(content);
|
|
6202
6373
|
}
|
|
6203
6374
|
if (isDynamic) {
|
|
6204
|
-
const
|
|
6205
|
-
if (
|
|
6375
|
+
const isStringLiteral16 = strCodeTypes.isStringLiteral(content);
|
|
6376
|
+
if (isStringLiteral16) {
|
|
6206
6377
|
content = normalizeString(content);
|
|
6207
6378
|
propsIR.value.content = content;
|
|
6208
6379
|
}
|
|
6209
|
-
propsIR.value.isStringLiteral =
|
|
6380
|
+
propsIR.value.isStringLiteral = isStringLiteral16;
|
|
6210
6381
|
}
|
|
6211
6382
|
const existing = findSameProp(nodeIR.props, propsIR);
|
|
6212
6383
|
if (existing) {
|
|
@@ -6511,7 +6682,7 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
6511
6682
|
}
|
|
6512
6683
|
|
|
6513
6684
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
6514
|
-
import * as
|
|
6685
|
+
import * as t55 from "@babel/types";
|
|
6515
6686
|
function resolveVOn(directive, ir, ctx, nodeIR) {
|
|
6516
6687
|
const arg = directive.arg;
|
|
6517
6688
|
const exp = directive.exp;
|
|
@@ -6569,10 +6740,10 @@ function resolveHandler(handlerContent, ctx, modifiers) {
|
|
|
6569
6740
|
return handler;
|
|
6570
6741
|
}
|
|
6571
6742
|
function isConsoleCall(expr) {
|
|
6572
|
-
return
|
|
6743
|
+
return t55.isCallExpression(expr) && t55.isMemberExpression(expr.callee) && t55.isIdentifier(expr.callee.object) && expr.callee.object.name === "console";
|
|
6573
6744
|
}
|
|
6574
6745
|
function isFnReference(expr) {
|
|
6575
|
-
return
|
|
6746
|
+
return t55.isIdentifier(expr) || t55.isMemberExpression(expr) || t55.isFunction(expr);
|
|
6576
6747
|
}
|
|
6577
6748
|
|
|
6578
6749
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-show.ts
|
|
@@ -6925,7 +7096,7 @@ function transform(ast, ctx, options) {
|
|
|
6925
7096
|
}
|
|
6926
7097
|
|
|
6927
7098
|
// package.json
|
|
6928
|
-
var version = "1.
|
|
7099
|
+
var version = "1.10.0";
|
|
6929
7100
|
var bin = {
|
|
6930
7101
|
vureact: "bin/vureact.js"
|
|
6931
7102
|
};
|
|
@@ -8608,9 +8779,11 @@ var FileCompiler = class extends BaseCompiler {
|
|
|
8608
8779
|
startTime = performance.now();
|
|
8609
8780
|
this.updateSpinner("Scanning files...");
|
|
8610
8781
|
const scanFiles = fileProcessor.scanFiles();
|
|
8611
|
-
this.updateSpinner("Compiling
|
|
8782
|
+
this.updateSpinner("Compiling components...");
|
|
8612
8783
|
const sfcCount = await pipelineManager.runSFC(scanFiles.vue, cacheMap);
|
|
8784
|
+
this.updateSpinner("Compiling scripts...");
|
|
8613
8785
|
const scriptCount = await pipelineManager.runScript(scanFiles.script, cacheMap);
|
|
8786
|
+
this.updateSpinner("Compiling styles...");
|
|
8614
8787
|
const styleCount = await pipelineManager.runStyle(scanFiles.style, cacheMap);
|
|
8615
8788
|
this.updateSpinner("Copying assets...");
|
|
8616
8789
|
const assetCount = await assetManager.runAsset(scanFiles.assets, cacheMap);
|
|
@@ -8668,7 +8841,7 @@ var FileCompiler = class extends BaseCompiler {
|
|
|
8668
8841
|
return await this.manager.cleanupManager.removeOutputPath(targetPath, type);
|
|
8669
8842
|
}
|
|
8670
8843
|
showCompileStats(endTime, sfcCount, scriptCount, styleCount, assetCount) {
|
|
8671
|
-
this.spinner.succeed(`
|
|
8844
|
+
this.spinner.succeed(`Vue compiled to React in ${endTime}`);
|
|
8672
8845
|
if (sfcCount || scriptCount || styleCount || assetCount) {
|
|
8673
8846
|
const stats = [];
|
|
8674
8847
|
if (sfcCount) stats.push(`${sfcCount} ${kleur8.dim("SFC(s)")}`);
|