@vureact/compiler-core 1.5.1 → 1.6.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.en.md +9 -12
- package/README.md +6 -10
- package/lib/{chunk-WXMC2IBW.esm.js → chunk-H756PRG7.esm.js} +1841 -1143
- package/lib/{chunk-476FX3TF.js → chunk-ZZVHHUP3.js} +1958 -1260
- package/lib/cli.esm.js +2 -2
- package/lib/cli.js +10 -10
- package/lib/compiler-core.d.cts +16 -1
- package/lib/compiler-core.d.ts +16 -1
- 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.6.0
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -78,6 +78,23 @@ var VUE_PACKAGES = [
|
|
|
78
78
|
// 不需要带到 React 项目中
|
|
79
79
|
"@vureact/compiler-core"
|
|
80
80
|
];
|
|
81
|
+
var SUPPORTED_DIRECTIVES = /* @__PURE__ */ new Set([
|
|
82
|
+
"text",
|
|
83
|
+
"html",
|
|
84
|
+
"show",
|
|
85
|
+
"if",
|
|
86
|
+
"else",
|
|
87
|
+
"else-if",
|
|
88
|
+
"for",
|
|
89
|
+
"on",
|
|
90
|
+
"once",
|
|
91
|
+
"bind",
|
|
92
|
+
"model",
|
|
93
|
+
"cloak",
|
|
94
|
+
"slot",
|
|
95
|
+
"memo",
|
|
96
|
+
"is"
|
|
97
|
+
]);
|
|
81
98
|
|
|
82
99
|
// src/consts/adapters-map.ts
|
|
83
100
|
var ADAPTER_RULES = {
|
|
@@ -392,7 +409,8 @@ var VUE_API_MAP = {
|
|
|
392
409
|
defineAsyncComponent: "defineAsyncComponent",
|
|
393
410
|
DynamicComponent: "Component",
|
|
394
411
|
Transition: "Transition",
|
|
395
|
-
RouterLink: "RouterLink"
|
|
412
|
+
RouterLink: "RouterLink",
|
|
413
|
+
useAttrs: "useAttrs"
|
|
396
414
|
};
|
|
397
415
|
|
|
398
416
|
// src/shared/babel-utils.ts
|
|
@@ -572,34 +590,31 @@ var capitalize = (str) => {
|
|
|
572
590
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
573
591
|
};
|
|
574
592
|
|
|
575
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/special-
|
|
576
|
-
function resolveSpecialExpressions(input, ctx) {
|
|
577
|
-
input = resolveEmitsCalls(input, ctx);
|
|
578
|
-
input = resolveRefVariable(input, ctx);
|
|
579
|
-
return input;
|
|
580
|
-
}
|
|
593
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/resolve-emit-call.ts
|
|
581
594
|
function resolveEmitsCalls(input, ctx) {
|
|
582
|
-
const
|
|
595
|
+
const { reactiveBindings } = ctx.templateData;
|
|
596
|
+
const matchEmitCalls = () => {
|
|
597
|
+
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
598
|
+
if (!macroBinding) return null;
|
|
599
|
+
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
600
|
+
const regex = new RegExp(
|
|
601
|
+
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
602
|
+
// 可选的第二个参数
|
|
603
|
+
"s"
|
|
604
|
+
// s 标志让 . 匹配换行符
|
|
605
|
+
);
|
|
606
|
+
return input.trim().match(regex);
|
|
607
|
+
};
|
|
608
|
+
const result = matchEmitCalls();
|
|
583
609
|
if (!result) return input;
|
|
584
610
|
const [, , eventName, args] = result;
|
|
585
611
|
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
586
612
|
const event = args ? `on${callee}?.(${args})` : `on${callee}?.()`;
|
|
587
|
-
return `${ctx.propField}
|
|
588
|
-
}
|
|
589
|
-
function matchEmitCalls(input, ctx) {
|
|
590
|
-
const { reactiveBindings } = ctx.templateData;
|
|
591
|
-
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
592
|
-
if (!macroBinding) return null;
|
|
593
|
-
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
594
|
-
const regex = new RegExp(
|
|
595
|
-
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
596
|
-
// 可选的第二个参数
|
|
597
|
-
"s"
|
|
598
|
-
// s 标志让 . 匹配换行符
|
|
599
|
-
);
|
|
600
|
-
return input.trim().match(regex);
|
|
613
|
+
return `${ctx.propField}.${event}`;
|
|
601
614
|
}
|
|
602
|
-
|
|
615
|
+
|
|
616
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/resolve-ref-access.ts
|
|
617
|
+
function resolveRefAccess(input, ctx) {
|
|
603
618
|
const { reactiveBindings } = ctx.templateData;
|
|
604
619
|
const addValueProperty = (input2, varName) => {
|
|
605
620
|
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -617,11 +632,20 @@ function resolveRefVariable(input, ctx) {
|
|
|
617
632
|
return input;
|
|
618
633
|
}
|
|
619
634
|
|
|
635
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/index.ts
|
|
636
|
+
function resolveSpecialExpression(input, ctx) {
|
|
637
|
+
const resolver = [resolveEmitsCalls, resolveRefAccess];
|
|
638
|
+
input = resolver.reduce((result, fn) => fn(result, ctx), input);
|
|
639
|
+
return input;
|
|
640
|
+
}
|
|
641
|
+
|
|
620
642
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
621
643
|
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
622
|
-
if (toStrLiteral)
|
|
644
|
+
if (toStrLiteral) {
|
|
645
|
+
return t5.stringLiteral(input);
|
|
646
|
+
}
|
|
623
647
|
const { filename, scriptData } = ctx;
|
|
624
|
-
const newContent =
|
|
648
|
+
const newContent = resolveSpecialExpression(input, ctx);
|
|
625
649
|
try {
|
|
626
650
|
return stringToExpr(newContent, scriptData.lang, filename);
|
|
627
651
|
} catch {
|
|
@@ -1029,7 +1053,7 @@ function buildStandardProp(nodeIR) {
|
|
|
1029
1053
|
babelExp: { ast: keyAST },
|
|
1030
1054
|
value: {
|
|
1031
1055
|
content,
|
|
1032
|
-
isStringLiteral:
|
|
1056
|
+
isStringLiteral: isStringLiteral14,
|
|
1033
1057
|
babelExp: { ast: valueAST }
|
|
1034
1058
|
}
|
|
1035
1059
|
} = nodeIR;
|
|
@@ -1038,18 +1062,34 @@ function buildStandardProp(nodeIR) {
|
|
|
1038
1062
|
}
|
|
1039
1063
|
let value;
|
|
1040
1064
|
if (content !== "true") {
|
|
1041
|
-
value =
|
|
1065
|
+
value = isStringLiteral14 ? t9.stringLiteral(content) : buildJsxExpressionNode(valueAST);
|
|
1042
1066
|
}
|
|
1043
1067
|
return t9.jsxAttribute(keyAST, value);
|
|
1044
1068
|
}
|
|
1045
1069
|
|
|
1046
|
-
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node.ts
|
|
1070
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node/resolve-template-node.ts
|
|
1071
|
+
function resolveTemplateNode(nodeIR, children) {
|
|
1072
|
+
const hasCondition = !!nodeIR.meta?.condition;
|
|
1073
|
+
const hasLoop = !!nodeIR.meta?.loop?.isLoop;
|
|
1074
|
+
const hasSlot = nodeIR.props.some((p) => p.type === 2 /* SLOT */);
|
|
1075
|
+
if (hasCondition || hasLoop || hasSlot) {
|
|
1076
|
+
if (!children.length) {
|
|
1077
|
+
return buildFragmentNode([]);
|
|
1078
|
+
}
|
|
1079
|
+
if (children.length === 1) {
|
|
1080
|
+
return children[0];
|
|
1081
|
+
}
|
|
1082
|
+
return buildFragmentNode(children);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node/index.ts
|
|
1047
1087
|
function buildElementNode(nodeIR, ctx) {
|
|
1048
1088
|
const mutableNodeIR = nodeIR;
|
|
1049
1089
|
if (nodeIR.conditionIsHandled && !mutableNodeIR.__processing) {
|
|
1050
1090
|
return null;
|
|
1051
1091
|
}
|
|
1052
|
-
const meta = nodeIR
|
|
1092
|
+
const { meta } = nodeIR;
|
|
1053
1093
|
if (meta?.condition && !meta.condition.isHandled) {
|
|
1054
1094
|
return buildConditionNode(nodeIR, ctx);
|
|
1055
1095
|
}
|
|
@@ -1061,6 +1101,10 @@ function buildElementNode(nodeIR, ctx) {
|
|
|
1061
1101
|
}
|
|
1062
1102
|
const props = buildProps(nodeIR, ctx);
|
|
1063
1103
|
const children = buildJsxChildren(nodeIR.children, ctx);
|
|
1104
|
+
if (nodeIR.tag === "template") {
|
|
1105
|
+
const jsxChild = resolveTemplateNode(nodeIR, children);
|
|
1106
|
+
if (jsxChild) return jsxChild;
|
|
1107
|
+
}
|
|
1064
1108
|
return createJsxElement(nodeIR.tag, props, children, nodeIR.isSelfClosing);
|
|
1065
1109
|
}
|
|
1066
1110
|
|
|
@@ -1260,17 +1304,16 @@ function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
|
1260
1304
|
return t13.variableDeclaration("const", [t13.variableDeclarator(name, memoCall)]);
|
|
1261
1305
|
}
|
|
1262
1306
|
function resolveComponentName(ctx) {
|
|
1263
|
-
const { filename,
|
|
1264
|
-
let name =
|
|
1307
|
+
const { filename, scriptData } = ctx;
|
|
1308
|
+
let { name } = scriptData.declaredOptions;
|
|
1265
1309
|
if (!name) {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
if (!compName) {
|
|
1270
|
-
logger.warn("Missing component name, it falls back to the filename. " + name, {
|
|
1310
|
+
const defaultName = basename(filename).split(".")[0] || `FC${genHashByXXH(filename)}`;
|
|
1311
|
+
name = capitalize(camelCase(defaultName));
|
|
1312
|
+
logger.warn(`Unnamed component detected. Using file name: <${name}>`, {
|
|
1271
1313
|
file: filename
|
|
1272
1314
|
});
|
|
1273
1315
|
}
|
|
1316
|
+
scriptData.declaredOptions.name = name;
|
|
1274
1317
|
return t13.identifier(name);
|
|
1275
1318
|
}
|
|
1276
1319
|
function resolveLocalStatements(local, jsx) {
|
|
@@ -1320,21 +1363,32 @@ function resolveForwardRef(body, ctx) {
|
|
|
1320
1363
|
}
|
|
1321
1364
|
function resolvePropsParam(ctx) {
|
|
1322
1365
|
const { propField, scriptData } = ctx;
|
|
1323
|
-
const { propsTSIface } = scriptData;
|
|
1366
|
+
const { propsTSIface, hasUseAttrsCall } = scriptData;
|
|
1324
1367
|
const propsIdentifier = t13.identifier(propField);
|
|
1325
1368
|
if (scriptData.lang.startsWith("js")) {
|
|
1326
|
-
if (propsTSIface.hasPropsInJsEnv) {
|
|
1369
|
+
if (propsTSIface.hasPropsInJsEnv || hasUseAttrsCall) {
|
|
1327
1370
|
return propsIdentifier;
|
|
1328
1371
|
}
|
|
1329
1372
|
return;
|
|
1330
1373
|
}
|
|
1331
1374
|
if (!propsTSIface.name) {
|
|
1375
|
+
if (hasUseAttrsCall) {
|
|
1376
|
+
return withPropsTypeAnnotation(propsIdentifier);
|
|
1377
|
+
}
|
|
1332
1378
|
return;
|
|
1333
1379
|
}
|
|
1334
|
-
const
|
|
1335
|
-
propsIdentifier
|
|
1380
|
+
const typeName = resolvePropsTypeName(propsTSIface.name, hasUseAttrsCall);
|
|
1381
|
+
return withPropsTypeAnnotation(propsIdentifier, typeName);
|
|
1382
|
+
}
|
|
1383
|
+
function withPropsTypeAnnotation(propsIdentifier, typeName = "Record<string, unknown>") {
|
|
1384
|
+
const typeRef = t13.tsTypeReference(t13.identifier(typeName));
|
|
1385
|
+
const typeAnnotation = t13.tsTypeAnnotation(typeRef);
|
|
1386
|
+
propsIdentifier.typeAnnotation = typeAnnotation;
|
|
1336
1387
|
return propsIdentifier;
|
|
1337
1388
|
}
|
|
1389
|
+
function resolvePropsTypeName(name, hasUseAttrsCall) {
|
|
1390
|
+
return typeof hasUseAttrsCall !== "undefined" ? `${name} & Record<string, unknown>` : name;
|
|
1391
|
+
}
|
|
1338
1392
|
|
|
1339
1393
|
// src/core/codegen/component/script/syntax-processor/process/build-program-preamble.ts
|
|
1340
1394
|
function buildProgramPreamble(nodeIR, ctx, state) {
|
|
@@ -1454,123 +1508,746 @@ import {
|
|
|
1454
1508
|
parse as parseVueSFC
|
|
1455
1509
|
} from "@vue/compiler-sfc";
|
|
1456
1510
|
|
|
1457
|
-
// src/core/parse/sfc/
|
|
1458
|
-
import {
|
|
1459
|
-
|
|
1511
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/index.ts
|
|
1512
|
+
import { traverse } from "@babel/core";
|
|
1513
|
+
|
|
1514
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-options.ts
|
|
1515
|
+
import * as t16 from "@babel/types";
|
|
1516
|
+
|
|
1517
|
+
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
1460
1518
|
import * as t15 from "@babel/types";
|
|
1461
|
-
function
|
|
1462
|
-
const
|
|
1463
|
-
if (!
|
|
1464
|
-
const
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1519
|
+
function findRootVariablePath(path9) {
|
|
1520
|
+
const rootId = findRootIdentifier(path9.node);
|
|
1521
|
+
if (!rootId?.name) return null;
|
|
1522
|
+
const binding = path9.scope.getBinding(rootId.name);
|
|
1523
|
+
if (!binding) return null;
|
|
1524
|
+
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
1525
|
+
return rootPath;
|
|
1526
|
+
}
|
|
1527
|
+
function findRootIdentifier(node) {
|
|
1528
|
+
let current = node.object;
|
|
1529
|
+
while (t15.isMemberExpression(current) || t15.isOptionalMemberExpression(current)) {
|
|
1530
|
+
current = current.object;
|
|
1531
|
+
}
|
|
1532
|
+
return t15.isIdentifier(current) ? current : null;
|
|
1533
|
+
}
|
|
1534
|
+
function getVariableDeclaratorPath(path9) {
|
|
1535
|
+
if (path9.isVariableDeclarator()) {
|
|
1536
|
+
return path9;
|
|
1537
|
+
}
|
|
1538
|
+
return path9.findParent((p) => p.isVariableDeclarator());
|
|
1539
|
+
}
|
|
1540
|
+
function isVariableDeclTopLevel(path9) {
|
|
1541
|
+
const variableDeclaratorPath = path9;
|
|
1542
|
+
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
1543
|
+
if (!variableDeclarationPath) {
|
|
1544
|
+
return false;
|
|
1545
|
+
}
|
|
1546
|
+
if (variableDeclarationPath.isProgram()) {
|
|
1547
|
+
return true;
|
|
1548
|
+
}
|
|
1549
|
+
const variableDeclarationParentPath = variableDeclarationPath.parentPath;
|
|
1550
|
+
if (variableDeclarationParentPath && variableDeclarationParentPath.isProgram()) {
|
|
1551
|
+
return true;
|
|
1552
|
+
}
|
|
1553
|
+
return false;
|
|
1554
|
+
}
|
|
1555
|
+
function isRealVariableAccess(path9) {
|
|
1556
|
+
return isIdentifierAccess(path9) && !isPropertyName(path9);
|
|
1557
|
+
}
|
|
1558
|
+
function isIdentifierAccess(path9) {
|
|
1559
|
+
if (isIdentifierDeclaration(path9)) {
|
|
1560
|
+
return false;
|
|
1561
|
+
}
|
|
1562
|
+
const binding = path9.scope.getBinding(path9.node.name);
|
|
1563
|
+
if (!binding) {
|
|
1564
|
+
return true;
|
|
1565
|
+
}
|
|
1566
|
+
return binding.identifier !== path9.node;
|
|
1567
|
+
}
|
|
1568
|
+
function isIdentifierDeclaration(path9) {
|
|
1569
|
+
const parent = path9.parentPath;
|
|
1570
|
+
if (!parent) return false;
|
|
1571
|
+
if (parent.isVariableDeclarator() && parent.node.id === path9.node) {
|
|
1572
|
+
return true;
|
|
1573
|
+
}
|
|
1574
|
+
if (parent.isFunctionDeclaration() && parent.node.id === path9.node) {
|
|
1575
|
+
return true;
|
|
1576
|
+
}
|
|
1577
|
+
if (parent.isFunctionExpression() && parent.node.id === path9.node) {
|
|
1578
|
+
return true;
|
|
1579
|
+
}
|
|
1580
|
+
if (parent.isClassDeclaration() && parent.node.id === path9.node) {
|
|
1581
|
+
return true;
|
|
1582
|
+
}
|
|
1583
|
+
if (parent.isImportSpecifier() && parent.node.local === path9.node) {
|
|
1584
|
+
return true;
|
|
1585
|
+
}
|
|
1586
|
+
if (parent.isImportDefaultSpecifier() && parent.node.local === path9.node) {
|
|
1587
|
+
return true;
|
|
1588
|
+
}
|
|
1589
|
+
if (parent.isImportNamespaceSpecifier() && parent.node.local === path9.node) {
|
|
1590
|
+
return true;
|
|
1591
|
+
}
|
|
1592
|
+
if (parent.isFunction() && parent.node.params.includes(path9.node)) {
|
|
1593
|
+
return true;
|
|
1594
|
+
}
|
|
1595
|
+
if (parent.isCatchClause() && parent.node.param === path9.node) {
|
|
1596
|
+
return true;
|
|
1597
|
+
}
|
|
1598
|
+
return false;
|
|
1599
|
+
}
|
|
1600
|
+
function isPropertyName(path9) {
|
|
1601
|
+
const parent = path9.parentPath;
|
|
1602
|
+
if (!parent) return false;
|
|
1603
|
+
if (parent.isObjectProperty() && parent.node.key === path9.node) {
|
|
1604
|
+
return true;
|
|
1605
|
+
}
|
|
1606
|
+
if (parent.isClassProperty() && parent.node.key === path9.node) {
|
|
1607
|
+
return true;
|
|
1608
|
+
}
|
|
1609
|
+
if (parent.isMemberExpression() && parent.node.property === path9.node) {
|
|
1610
|
+
return true;
|
|
1611
|
+
}
|
|
1612
|
+
return false;
|
|
1613
|
+
}
|
|
1614
|
+
function replaceCallName(callExp, identifierName) {
|
|
1615
|
+
const { callee } = callExp;
|
|
1616
|
+
if (!t15.isIdentifier(callee)) return;
|
|
1617
|
+
callee.name = identifierName;
|
|
1618
|
+
if (callee.loc) {
|
|
1619
|
+
callee.loc.identifierName = identifierName;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
function replaceIdName(id, newName) {
|
|
1623
|
+
if (!t15.isIdentifier(id)) return;
|
|
1624
|
+
id.name = newName;
|
|
1625
|
+
if (id.loc) {
|
|
1626
|
+
id.loc.identifierName = newName;
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
1630
|
+
const { filename, scriptData } = ctx;
|
|
1631
|
+
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
1632
|
+
const ts = expressionToTSType(exp);
|
|
1633
|
+
return tsTypeAnnotation7 ? t15.tsTypeAnnotation(ts) : ts;
|
|
1634
|
+
}
|
|
1635
|
+
function expressionToTSType(exp) {
|
|
1636
|
+
if (t15.isStringLiteral(exp)) return t15.tsStringKeyword();
|
|
1637
|
+
if (t15.isNumericLiteral(exp)) return t15.tsNumberKeyword();
|
|
1638
|
+
if (t15.isBooleanLiteral(exp)) return t15.tsBooleanKeyword();
|
|
1639
|
+
if (t15.isArrayExpression(exp)) return t15.tsArrayType(t15.tsAnyKeyword());
|
|
1640
|
+
if (t15.isObjectExpression(exp)) {
|
|
1641
|
+
const members = [];
|
|
1642
|
+
for (const p of exp.properties) {
|
|
1643
|
+
if (!t15.isObjectProperty(p)) continue;
|
|
1644
|
+
let key;
|
|
1645
|
+
if (t15.isIdentifier(p.key)) key = p.key.name;
|
|
1646
|
+
else if (t15.isStringLiteral(p.key)) key = p.key.value;
|
|
1647
|
+
if (!key) continue;
|
|
1648
|
+
if (t15.isExpression(p.value)) {
|
|
1649
|
+
members.push(
|
|
1650
|
+
t15.tsPropertySignature(t15.identifier(key), t15.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
1651
|
+
);
|
|
1652
|
+
} else {
|
|
1653
|
+
members.push(
|
|
1654
|
+
t15.tsPropertySignature(t15.identifier(key), t15.tsTypeAnnotation(t15.tsAnyKeyword()))
|
|
1655
|
+
);
|
|
1656
|
+
}
|
|
1476
1657
|
}
|
|
1477
|
-
|
|
1478
|
-
"Using traditional script (instead of <script setup>) may result in unstable or non-functional React code. It is recommended to use <script setup> instead.",
|
|
1479
|
-
{ file: ctx.filename }
|
|
1480
|
-
);
|
|
1481
|
-
} else {
|
|
1482
|
-
result.ast = babelParse2(result.source.content, options);
|
|
1658
|
+
return t15.tsTypeLiteral(members);
|
|
1483
1659
|
}
|
|
1484
|
-
if (
|
|
1485
|
-
|
|
1486
|
-
|
|
1660
|
+
if (t15.isArrowFunctionExpression(exp) || t15.isFunctionExpression(exp)) {
|
|
1661
|
+
const params = exp.params.map((p, i) => {
|
|
1662
|
+
const id = t15.isIdentifier(p) ? t15.identifier(p.name) : t15.identifier(`arg${i}`);
|
|
1663
|
+
id.typeAnnotation = t15.tsTypeAnnotation(t15.tsAnyKeyword());
|
|
1664
|
+
return id;
|
|
1487
1665
|
});
|
|
1666
|
+
let returnType = t15.tsAnyKeyword();
|
|
1667
|
+
if (t15.isBlockStatement(exp.body)) {
|
|
1668
|
+
for (const stmt of exp.body.body) {
|
|
1669
|
+
if (t15.isReturnStatement(stmt) && stmt.argument) {
|
|
1670
|
+
if (t15.isExpression(stmt.argument)) {
|
|
1671
|
+
returnType = expressionToTSType(stmt.argument);
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
} else if (t15.isExpression(exp.body)) {
|
|
1677
|
+
returnType = expressionToTSType(exp.body);
|
|
1678
|
+
}
|
|
1679
|
+
return t15.tsFunctionType(null, params, t15.tsTypeAnnotation(returnType));
|
|
1488
1680
|
}
|
|
1489
|
-
|
|
1681
|
+
return t15.tsAnyKeyword();
|
|
1490
1682
|
}
|
|
1491
|
-
function
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
content = content.replace(regx, "");
|
|
1497
|
-
return nameMatch?.[1]?.trim() || "";
|
|
1498
|
-
};
|
|
1499
|
-
ctx.compName = resolveVRComment(content);
|
|
1500
|
-
ctx.scriptData.source = content;
|
|
1501
|
-
ctx.scriptData.lang = lang || "js";
|
|
1502
|
-
scriptBlock.content = content;
|
|
1683
|
+
function isCalleeNamed(node, name) {
|
|
1684
|
+
if (!t15.isIdentifier(node.callee)) {
|
|
1685
|
+
return false;
|
|
1686
|
+
}
|
|
1687
|
+
return node.callee.name === name;
|
|
1503
1688
|
}
|
|
1504
|
-
function
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1689
|
+
function isSimpleLiteral(node) {
|
|
1690
|
+
if (!node) return false;
|
|
1691
|
+
if (t15.isStringLiteral(node) || t15.isNumericLiteral(node) || t15.isBooleanLiteral(node) || t15.isNullLiteral(node) || t15.isRegExpLiteral(node) || t15.isBigIntLiteral(node) || t15.isDecimalLiteral(node)) {
|
|
1692
|
+
return true;
|
|
1693
|
+
}
|
|
1694
|
+
return false;
|
|
1695
|
+
}
|
|
1696
|
+
function forkNode(node, deep = true) {
|
|
1697
|
+
const newNode = t15.cloneNode(node, deep);
|
|
1698
|
+
newNode.leadingComments = node.leadingComments;
|
|
1699
|
+
newNode.innerComments = node.innerComments;
|
|
1700
|
+
newNode.trailingComments = null;
|
|
1701
|
+
cleanNodeLoc(node);
|
|
1702
|
+
cleanNodeComments(node);
|
|
1703
|
+
return newNode;
|
|
1704
|
+
}
|
|
1705
|
+
function replaceNode(path9, target, source) {
|
|
1706
|
+
const { start, end, loc, leadingComments, innerComments, trailingComments } = source;
|
|
1707
|
+
target.start = start;
|
|
1708
|
+
target.end = end;
|
|
1709
|
+
target.loc = loc;
|
|
1710
|
+
target.leadingComments = leadingComments;
|
|
1711
|
+
target.innerComments = innerComments;
|
|
1712
|
+
target.trailingComments = trailingComments;
|
|
1713
|
+
cleanNodeLoc(source);
|
|
1714
|
+
cleanNodeComments(source);
|
|
1715
|
+
path9.replaceWith(target);
|
|
1716
|
+
}
|
|
1717
|
+
function cleanNodeLoc(node) {
|
|
1718
|
+
node.start = null;
|
|
1719
|
+
node.end = null;
|
|
1720
|
+
node.loc = null;
|
|
1721
|
+
}
|
|
1722
|
+
function cleanNodeComments(node) {
|
|
1723
|
+
node.leadingComments = null;
|
|
1724
|
+
node.innerComments = null;
|
|
1725
|
+
node.trailingComments = null;
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-options.ts
|
|
1729
|
+
function resolveDeclaredOptions(path9, ctx) {
|
|
1730
|
+
const { node } = path9;
|
|
1731
|
+
const { filename, scriptData } = ctx;
|
|
1732
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
const [options] = node.arguments;
|
|
1736
|
+
if (!options || !t16.isObjectExpression(options)) {
|
|
1737
|
+
logger.warn(
|
|
1738
|
+
`Expected argument to be of a type ["ObjectExpression"] but instead got "${options?.type}".`,
|
|
1739
|
+
{
|
|
1740
|
+
file: filename,
|
|
1741
|
+
source: scriptData.source,
|
|
1742
|
+
loc: options?.loc || node.loc
|
|
1515
1743
|
}
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
if (t15.isObjectProperty(p)) {
|
|
1536
|
-
const key = p.key;
|
|
1537
|
-
return t15.isIdentifier(key) && key.name === "setup" || t15.isStringLiteral(key) && key.value === "setup";
|
|
1538
|
-
}
|
|
1539
|
-
return false;
|
|
1540
|
-
});
|
|
1541
|
-
if (setupProp) {
|
|
1542
|
-
const value = t15.isObjectMethod(setupProp) ? setupProp : t15.isObjectProperty(setupProp) ? setupProp.value : null;
|
|
1543
|
-
if (value && (t15.isFunction(value) || t15.isObjectMethod(value))) {
|
|
1544
|
-
const fnBody = t15.isBlockStatement(value.body) ? value.body.body : [];
|
|
1545
|
-
for (const stmt of fnBody) {
|
|
1546
|
-
if (t15.isReturnStatement(stmt)) continue;
|
|
1547
|
-
setupStatements.push(stmt);
|
|
1548
|
-
}
|
|
1549
|
-
}
|
|
1744
|
+
);
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
const { source, declaredOptions } = scriptData;
|
|
1748
|
+
declaredOptions.inheritAttrs = true;
|
|
1749
|
+
for (const property of options.properties) {
|
|
1750
|
+
if (!t16.isObjectProperty(property) || !t16.isIdentifier(property.key) || property.computed) {
|
|
1751
|
+
continue;
|
|
1752
|
+
}
|
|
1753
|
+
const propKey = property.key;
|
|
1754
|
+
const propValue = property.value;
|
|
1755
|
+
if (propKey.name === "name") {
|
|
1756
|
+
if (!t16.isStringLiteral(propValue)) {
|
|
1757
|
+
logger.warn(
|
|
1758
|
+
`Expected property to be of a type ["StringLiteral"] but instead got "${propValue.type}".`,
|
|
1759
|
+
{
|
|
1760
|
+
source,
|
|
1761
|
+
file: filename,
|
|
1762
|
+
loc: propKey?.loc
|
|
1550
1763
|
}
|
|
1551
|
-
|
|
1764
|
+
);
|
|
1552
1765
|
continue;
|
|
1553
1766
|
}
|
|
1554
|
-
|
|
1767
|
+
if (propValue.value.trim()) {
|
|
1768
|
+
declaredOptions.name = propValue.value.trim();
|
|
1769
|
+
}
|
|
1770
|
+
continue;
|
|
1555
1771
|
}
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1772
|
+
if (propKey.name === "inheritAttrs") {
|
|
1773
|
+
if (!t16.isBooleanLiteral(propValue)) {
|
|
1774
|
+
logger.warn(
|
|
1775
|
+
`Expected property to be of a type ["BooleanLiteral"] but instead got "${propValue.type}".`,
|
|
1776
|
+
{
|
|
1777
|
+
source,
|
|
1778
|
+
file: filename,
|
|
1779
|
+
loc: propKey?.loc
|
|
1780
|
+
}
|
|
1781
|
+
);
|
|
1782
|
+
continue;
|
|
1783
|
+
}
|
|
1784
|
+
declaredOptions.inheritAttrs = propValue.value;
|
|
1560
1785
|
}
|
|
1561
|
-
return { name, ast, code: parts.join("\n\n") };
|
|
1562
|
-
} catch (e) {
|
|
1563
|
-
console.error(e);
|
|
1564
|
-
return { name, code: content };
|
|
1565
1786
|
}
|
|
1566
1787
|
}
|
|
1567
1788
|
|
|
1568
|
-
// src/core/parse/sfc/
|
|
1569
|
-
import
|
|
1570
|
-
import * as t16 from "@babel/types";
|
|
1789
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-emits.ts
|
|
1790
|
+
import * as t18 from "@babel/types";
|
|
1571
1791
|
|
|
1572
|
-
// src/
|
|
1573
|
-
|
|
1792
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/shared.ts
|
|
1793
|
+
import * as t17 from "@babel/types";
|
|
1794
|
+
function isJsLikeLang(lang) {
|
|
1795
|
+
return lang === "js" || lang === "jsx";
|
|
1796
|
+
}
|
|
1797
|
+
function mergeNames(target, names) {
|
|
1798
|
+
for (const name of names) {
|
|
1799
|
+
target.add(name);
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
function resolveTsTypes(typeParams) {
|
|
1803
|
+
if (!typeParams) {
|
|
1804
|
+
return [];
|
|
1805
|
+
}
|
|
1806
|
+
if (t17.isTSTypeParameterInstantiation(typeParams)) {
|
|
1807
|
+
return typeParams.params;
|
|
1808
|
+
}
|
|
1809
|
+
return [];
|
|
1810
|
+
}
|
|
1811
|
+
function resolveObjectOrArrayLiteralNames(value) {
|
|
1812
|
+
const names = /* @__PURE__ */ new Set();
|
|
1813
|
+
if (!value) {
|
|
1814
|
+
return names;
|
|
1815
|
+
}
|
|
1816
|
+
if (t17.isArrayExpression(value)) {
|
|
1817
|
+
for (const element of value.elements) {
|
|
1818
|
+
if (element && t17.isStringLiteral(element)) {
|
|
1819
|
+
names.add(element.value);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
return names;
|
|
1823
|
+
}
|
|
1824
|
+
if (t17.isObjectExpression(value)) {
|
|
1825
|
+
for (const property of value.properties) {
|
|
1826
|
+
if (!t17.isObjectProperty(property) && !t17.isObjectMethod(property)) {
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
const name = resolveStaticName(property.key);
|
|
1830
|
+
if (name) {
|
|
1831
|
+
names.add(name);
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
return names;
|
|
1836
|
+
}
|
|
1837
|
+
function resolveStaticName(key) {
|
|
1838
|
+
if (t17.isIdentifier(key)) {
|
|
1839
|
+
return key.name;
|
|
1840
|
+
}
|
|
1841
|
+
if (t17.isStringLiteral(key)) {
|
|
1842
|
+
return key.value;
|
|
1843
|
+
}
|
|
1844
|
+
if (t17.isNumericLiteral(key)) {
|
|
1845
|
+
return String(key.value);
|
|
1846
|
+
}
|
|
1847
|
+
if (t17.isTemplateLiteral(key) && !key.expressions.length) {
|
|
1848
|
+
return key.quasis.map((q) => q.value.cooked || "").join("");
|
|
1849
|
+
}
|
|
1850
|
+
return null;
|
|
1851
|
+
}
|
|
1852
|
+
function resolveLocalTypeFromReference(path9, typeRef, ctx, macroName, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
1853
|
+
const refName = resolveTypeReferenceName(typeRef.typeName);
|
|
1854
|
+
if (!refName) return null;
|
|
1855
|
+
if (visitedTypeRefs.has(refName)) return null;
|
|
1856
|
+
const binding = path9.scope.getBinding(refName);
|
|
1857
|
+
if (binding) {
|
|
1858
|
+
if (binding.path.isImportSpecifier() || binding.path.isImportDefaultSpecifier() || binding.path.isImportNamespaceSpecifier()) {
|
|
1859
|
+
const key = `${macroName}:${refName}`;
|
|
1860
|
+
if (!warnedImportedTypeRefs.has(key)) {
|
|
1861
|
+
warnedImportedTypeRefs.add(key);
|
|
1862
|
+
logger.warn(
|
|
1863
|
+
"Type reference comes from an external file. Cross-file analysis is not supported, so it was skipped.",
|
|
1864
|
+
{
|
|
1865
|
+
file: ctx.filename,
|
|
1866
|
+
source: ctx.source,
|
|
1867
|
+
loc: typeRef.loc || path9.node.loc
|
|
1868
|
+
}
|
|
1869
|
+
);
|
|
1870
|
+
}
|
|
1871
|
+
return null;
|
|
1872
|
+
}
|
|
1873
|
+
visitedTypeRefs.add(refName);
|
|
1874
|
+
if (binding.path.isTSInterfaceDeclaration()) {
|
|
1875
|
+
return t17.tsTypeLiteral(binding.path.node.body.body);
|
|
1876
|
+
}
|
|
1877
|
+
if (binding.path.isTSTypeAliasDeclaration()) {
|
|
1878
|
+
return binding.path.node.typeAnnotation;
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
const programPath = path9.findParent((p) => p.isProgram());
|
|
1882
|
+
if (!programPath?.isProgram()) {
|
|
1883
|
+
return null;
|
|
1884
|
+
}
|
|
1885
|
+
const declaration = resolveTypeDeclarationInProgram(programPath.node.body, refName);
|
|
1886
|
+
if (declaration) {
|
|
1887
|
+
visitedTypeRefs.add(refName);
|
|
1888
|
+
return declaration;
|
|
1889
|
+
}
|
|
1890
|
+
const importSource = resolveImportedTypeSourceInProgram(programPath.node.body, refName);
|
|
1891
|
+
if (importSource) {
|
|
1892
|
+
const key = `${macroName}:${refName}`;
|
|
1893
|
+
if (!warnedImportedTypeRefs.has(key)) {
|
|
1894
|
+
warnedImportedTypeRefs.add(key);
|
|
1895
|
+
logger.warn(
|
|
1896
|
+
"Type reference comes from an external file. Cross-file analysis is not supported, so it was skipped.",
|
|
1897
|
+
{
|
|
1898
|
+
file: ctx.filename,
|
|
1899
|
+
source: ctx.source,
|
|
1900
|
+
loc: typeRef.loc || path9.node.loc
|
|
1901
|
+
}
|
|
1902
|
+
);
|
|
1903
|
+
}
|
|
1904
|
+
return null;
|
|
1905
|
+
}
|
|
1906
|
+
return null;
|
|
1907
|
+
}
|
|
1908
|
+
function resolveTypeReferenceName(typeName) {
|
|
1909
|
+
if (t17.isIdentifier(typeName)) {
|
|
1910
|
+
return typeName.name;
|
|
1911
|
+
}
|
|
1912
|
+
return null;
|
|
1913
|
+
}
|
|
1914
|
+
function resolveTypeDeclarationInProgram(body, typeName) {
|
|
1915
|
+
for (const statement of body) {
|
|
1916
|
+
if (t17.isTSInterfaceDeclaration(statement) && statement.id.name === typeName) {
|
|
1917
|
+
return t17.tsTypeLiteral(statement.body.body);
|
|
1918
|
+
}
|
|
1919
|
+
if (t17.isTSTypeAliasDeclaration(statement) && statement.id.name === typeName) {
|
|
1920
|
+
return statement.typeAnnotation;
|
|
1921
|
+
}
|
|
1922
|
+
if (!t17.isExportNamedDeclaration(statement) || !statement.declaration) {
|
|
1923
|
+
continue;
|
|
1924
|
+
}
|
|
1925
|
+
const declaration = statement.declaration;
|
|
1926
|
+
if (t17.isTSInterfaceDeclaration(declaration) && declaration.id.name === typeName) {
|
|
1927
|
+
return t17.tsTypeLiteral(declaration.body.body);
|
|
1928
|
+
}
|
|
1929
|
+
if (t17.isTSTypeAliasDeclaration(declaration) && declaration.id.name === typeName) {
|
|
1930
|
+
return declaration.typeAnnotation;
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
return null;
|
|
1934
|
+
}
|
|
1935
|
+
function resolveImportedTypeSourceInProgram(body, typeName) {
|
|
1936
|
+
for (const statement of body) {
|
|
1937
|
+
if (!t17.isImportDeclaration(statement)) {
|
|
1938
|
+
continue;
|
|
1939
|
+
}
|
|
1940
|
+
const imported = statement.specifiers.some((specifier) => {
|
|
1941
|
+
if (!specifier.local || specifier.local.name !== typeName) {
|
|
1942
|
+
return false;
|
|
1943
|
+
}
|
|
1944
|
+
if (t17.isImportSpecifier(specifier)) {
|
|
1945
|
+
return specifier.importKind === "type" || statement.importKind === "type";
|
|
1946
|
+
}
|
|
1947
|
+
return true;
|
|
1948
|
+
});
|
|
1949
|
+
if (imported) {
|
|
1950
|
+
return statement.source.value;
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-emits.ts
|
|
1957
|
+
function resolveDeclaredEmits(path9, ctx) {
|
|
1958
|
+
const { node } = path9;
|
|
1959
|
+
const { templateData, scriptData } = ctx;
|
|
1960
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.emits)) {
|
|
1961
|
+
return;
|
|
1962
|
+
}
|
|
1963
|
+
const [initValue] = node.arguments;
|
|
1964
|
+
const tsTypeDef = node.typeParameters;
|
|
1965
|
+
if (isJsLikeLang(scriptData.lang)) {
|
|
1966
|
+
mergeNames(templateData.declaredEmits, resolveObjectOrArrayLiteralNames(initValue));
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
const namesFromType = resolveDeclaredEmitsFromTypeParams(tsTypeDef, path9, ctx);
|
|
1970
|
+
if (namesFromType.size) {
|
|
1971
|
+
mergeNames(templateData.declaredEmits, namesFromType);
|
|
1972
|
+
return;
|
|
1973
|
+
}
|
|
1974
|
+
mergeNames(templateData.declaredEmits, resolveObjectOrArrayLiteralNames(initValue));
|
|
1975
|
+
}
|
|
1976
|
+
function resolveDeclaredEmitsFromTypeParams(typeParams, path9, ctx) {
|
|
1977
|
+
const names = /* @__PURE__ */ new Set();
|
|
1978
|
+
const visitedTypeRefs = /* @__PURE__ */ new Set();
|
|
1979
|
+
const warnedImportedTypeRefs = /* @__PURE__ */ new Set();
|
|
1980
|
+
for (const tsType of resolveTsTypes(typeParams)) {
|
|
1981
|
+
collectEmitsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
1982
|
+
}
|
|
1983
|
+
return names;
|
|
1984
|
+
}
|
|
1985
|
+
function collectEmitsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
1986
|
+
if (t18.isTSParenthesizedType(tsType)) {
|
|
1987
|
+
collectEmitsFromTsType(
|
|
1988
|
+
tsType.typeAnnotation,
|
|
1989
|
+
names,
|
|
1990
|
+
path9,
|
|
1991
|
+
ctx,
|
|
1992
|
+
visitedTypeRefs,
|
|
1993
|
+
warnedImportedTypeRefs
|
|
1994
|
+
);
|
|
1995
|
+
return;
|
|
1996
|
+
}
|
|
1997
|
+
if (t18.isTSIntersectionType(tsType) || t18.isTSUnionType(tsType)) {
|
|
1998
|
+
for (const type of tsType.types) {
|
|
1999
|
+
collectEmitsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2000
|
+
}
|
|
2001
|
+
return;
|
|
2002
|
+
}
|
|
2003
|
+
if (t18.isTSTypeReference(tsType)) {
|
|
2004
|
+
const innerTypes = tsType.typeParameters?.params || [];
|
|
2005
|
+
for (const type of innerTypes) {
|
|
2006
|
+
collectEmitsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2007
|
+
}
|
|
2008
|
+
const localType = resolveLocalTypeFromReference(
|
|
2009
|
+
path9,
|
|
2010
|
+
tsType,
|
|
2011
|
+
ctx,
|
|
2012
|
+
MACRO_API_NAMES.emits,
|
|
2013
|
+
visitedTypeRefs,
|
|
2014
|
+
warnedImportedTypeRefs
|
|
2015
|
+
);
|
|
2016
|
+
if (localType) {
|
|
2017
|
+
collectEmitsFromTsType(localType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2018
|
+
}
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
if (t18.isTSFunctionType(tsType)) {
|
|
2022
|
+
collectEmitNamesFromCallable(tsType.parameters, names);
|
|
2023
|
+
return;
|
|
2024
|
+
}
|
|
2025
|
+
if (!t18.isTSTypeLiteral(tsType)) {
|
|
2026
|
+
return;
|
|
2027
|
+
}
|
|
2028
|
+
for (const member of tsType.members) {
|
|
2029
|
+
if (t18.isTSPropertySignature(member) || t18.isTSMethodSignature(member)) {
|
|
2030
|
+
const eventName = resolveStaticName(member.key);
|
|
2031
|
+
if (eventName) {
|
|
2032
|
+
names.add(eventName);
|
|
2033
|
+
}
|
|
2034
|
+
continue;
|
|
2035
|
+
}
|
|
2036
|
+
if (t18.isTSCallSignatureDeclaration(member)) {
|
|
2037
|
+
collectEmitNamesFromCallable(member.parameters, names);
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
function collectEmitNamesFromCallable(parameters, names) {
|
|
2042
|
+
const firstParamType = resolveFirstParamType(parameters[0]);
|
|
2043
|
+
if (!firstParamType) {
|
|
2044
|
+
return;
|
|
2045
|
+
}
|
|
2046
|
+
for (const eventName of resolveStringLiteralTypeNames(firstParamType)) {
|
|
2047
|
+
names.add(eventName);
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
function resolveFirstParamType(param) {
|
|
2051
|
+
if (!param) {
|
|
2052
|
+
return null;
|
|
2053
|
+
}
|
|
2054
|
+
if (t18.isIdentifier(param) && param.typeAnnotation && !t18.isNoop(param.typeAnnotation)) {
|
|
2055
|
+
return param.typeAnnotation.typeAnnotation;
|
|
2056
|
+
}
|
|
2057
|
+
if (t18.isAssignmentPattern(param)) {
|
|
2058
|
+
const left = param.left;
|
|
2059
|
+
if (t18.isIdentifier(left) && left.typeAnnotation && !t18.isNoop(left.typeAnnotation)) {
|
|
2060
|
+
return left.typeAnnotation.typeAnnotation;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
if (t18.isRestElement(param) && t18.isIdentifier(param.argument)) {
|
|
2064
|
+
const { typeAnnotation } = param.argument;
|
|
2065
|
+
if (typeAnnotation && !t18.isNoop(typeAnnotation)) {
|
|
2066
|
+
return typeAnnotation.typeAnnotation;
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
return null;
|
|
2070
|
+
}
|
|
2071
|
+
function resolveStringLiteralTypeNames(type) {
|
|
2072
|
+
if (t18.isTSParenthesizedType(type)) {
|
|
2073
|
+
return resolveStringLiteralTypeNames(type.typeAnnotation);
|
|
2074
|
+
}
|
|
2075
|
+
if (t18.isTSUnionType(type)) {
|
|
2076
|
+
return type.types.flatMap(resolveStringLiteralTypeNames);
|
|
2077
|
+
}
|
|
2078
|
+
if (!t18.isTSLiteralType(type)) {
|
|
2079
|
+
return [];
|
|
2080
|
+
}
|
|
2081
|
+
if (t18.isStringLiteral(type.literal)) {
|
|
2082
|
+
return [type.literal.value];
|
|
2083
|
+
}
|
|
2084
|
+
if (t18.isTemplateLiteral(type.literal) && !type.literal.expressions.length) {
|
|
2085
|
+
const cooked = type.literal.quasis.map((q) => q.value.cooked || "").join("");
|
|
2086
|
+
return cooked ? [cooked] : [];
|
|
2087
|
+
}
|
|
2088
|
+
return [];
|
|
2089
|
+
}
|
|
2090
|
+
|
|
2091
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-props.ts
|
|
2092
|
+
import * as t19 from "@babel/types";
|
|
2093
|
+
function resolveDeclaredProps(path9, ctx) {
|
|
2094
|
+
const { node } = path9;
|
|
2095
|
+
const { templateData, scriptData } = ctx;
|
|
2096
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.props)) {
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
const [initValue] = node.arguments;
|
|
2100
|
+
const tsTypeDef = node.typeParameters;
|
|
2101
|
+
if (isJsLikeLang(scriptData.lang)) {
|
|
2102
|
+
mergeNames(templateData.declaredProps, resolveObjectOrArrayLiteralNames(initValue));
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
const namesFromType = resolveDeclaredPropsFromTypeParams(tsTypeDef, path9, ctx);
|
|
2106
|
+
if (namesFromType.size) {
|
|
2107
|
+
mergeNames(templateData.declaredProps, namesFromType);
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
mergeNames(templateData.declaredProps, resolveObjectOrArrayLiteralNames(initValue));
|
|
2111
|
+
}
|
|
2112
|
+
function resolveDeclaredPropsFromTypeParams(typeParams, path9, ctx) {
|
|
2113
|
+
const names = /* @__PURE__ */ new Set();
|
|
2114
|
+
const visitedTypeRefs = /* @__PURE__ */ new Set();
|
|
2115
|
+
const warnedImportedTypeRefs = /* @__PURE__ */ new Set();
|
|
2116
|
+
for (const tsType of resolveTsTypes(typeParams)) {
|
|
2117
|
+
collectPropsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2118
|
+
}
|
|
2119
|
+
return names;
|
|
2120
|
+
}
|
|
2121
|
+
function collectPropsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
2122
|
+
if (t19.isTSParenthesizedType(tsType)) {
|
|
2123
|
+
collectPropsFromTsType(
|
|
2124
|
+
tsType.typeAnnotation,
|
|
2125
|
+
names,
|
|
2126
|
+
path9,
|
|
2127
|
+
ctx,
|
|
2128
|
+
visitedTypeRefs,
|
|
2129
|
+
warnedImportedTypeRefs
|
|
2130
|
+
);
|
|
2131
|
+
return;
|
|
2132
|
+
}
|
|
2133
|
+
if (t19.isTSIntersectionType(tsType) || t19.isTSUnionType(tsType)) {
|
|
2134
|
+
for (const type of tsType.types) {
|
|
2135
|
+
collectPropsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2136
|
+
}
|
|
2137
|
+
return;
|
|
2138
|
+
}
|
|
2139
|
+
if (t19.isTSTypeReference(tsType)) {
|
|
2140
|
+
const innerTypes = tsType.typeParameters?.params || [];
|
|
2141
|
+
for (const type of innerTypes) {
|
|
2142
|
+
collectPropsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2143
|
+
}
|
|
2144
|
+
const localType = resolveLocalTypeFromReference(
|
|
2145
|
+
path9,
|
|
2146
|
+
tsType,
|
|
2147
|
+
ctx,
|
|
2148
|
+
MACRO_API_NAMES.props,
|
|
2149
|
+
visitedTypeRefs,
|
|
2150
|
+
warnedImportedTypeRefs
|
|
2151
|
+
);
|
|
2152
|
+
if (localType) {
|
|
2153
|
+
collectPropsFromTsType(localType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2154
|
+
}
|
|
2155
|
+
return;
|
|
2156
|
+
}
|
|
2157
|
+
if (!t19.isTSTypeLiteral(tsType)) {
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
for (const member of tsType.members) {
|
|
2161
|
+
if (!t19.isTSPropertySignature(member) && !t19.isTSMethodSignature(member)) {
|
|
2162
|
+
continue;
|
|
2163
|
+
}
|
|
2164
|
+
const name = resolveStaticName(member.key);
|
|
2165
|
+
if (name) {
|
|
2166
|
+
names.add(name);
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/index.ts
|
|
2172
|
+
function resolveDeclaredPropsAndEmits(path9, ctx) {
|
|
2173
|
+
resolveDeclaredProps(path9, ctx);
|
|
2174
|
+
resolveDeclaredEmits(path9, ctx);
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-use-attrs-exists.ts
|
|
2178
|
+
import * as t20 from "@babel/types";
|
|
2179
|
+
function resolveUseAttrsExists(path9, ctx) {
|
|
2180
|
+
const { node } = path9;
|
|
2181
|
+
const { templateData, scriptData } = ctx;
|
|
2182
|
+
if (!isCalleeNamed(node, VUE_API_MAP.useAttrs)) {
|
|
2183
|
+
return;
|
|
2184
|
+
}
|
|
2185
|
+
if (scriptData?.hasUseAttrsCall) {
|
|
2186
|
+
return;
|
|
2187
|
+
}
|
|
2188
|
+
scriptData.hasUseAttrsCall = true;
|
|
2189
|
+
const parent = path9.parentPath?.node;
|
|
2190
|
+
if (parent && t20.isVariableDeclarator(parent) && t20.isIdentifier(parent.id)) {
|
|
2191
|
+
templateData.declaredAttrs = parent.id.name;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/index.ts
|
|
2196
|
+
import * as t21 from "@babel/types";
|
|
2197
|
+
|
|
2198
|
+
// src/consts/reactive-type-map.ts
|
|
2199
|
+
var REACTIVE_TYPE_MAP = {
|
|
2200
|
+
ref: "ref",
|
|
2201
|
+
toRef: "ref",
|
|
2202
|
+
toRefs: "ref",
|
|
2203
|
+
customRef: "ref",
|
|
2204
|
+
shallowRef: "ref",
|
|
2205
|
+
reactive: "reactive",
|
|
2206
|
+
shallowReactive: "reactive",
|
|
2207
|
+
computed: "ref",
|
|
2208
|
+
readonly: "reactive",
|
|
2209
|
+
shallowReadonly: "reactive",
|
|
2210
|
+
provide: "reactive",
|
|
2211
|
+
inject: "reactive",
|
|
2212
|
+
defineProps: "reactive",
|
|
2213
|
+
defineEmits: "reactive",
|
|
2214
|
+
useRoute: "reactive",
|
|
2215
|
+
useRouter: "reactive",
|
|
2216
|
+
useLink: "reactive"
|
|
2217
|
+
};
|
|
2218
|
+
|
|
2219
|
+
// src/shared/reactive-utils.ts
|
|
2220
|
+
function getReactiveType(originalName) {
|
|
2221
|
+
return REACTIVE_TYPE_MAP[originalName] || "none";
|
|
2222
|
+
}
|
|
2223
|
+
function getReactiveStateApis() {
|
|
2224
|
+
return new Set(Object.keys(REACTIVE_TYPE_MAP));
|
|
2225
|
+
}
|
|
2226
|
+
|
|
2227
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/resolve-reactive-bindings.ts
|
|
2228
|
+
function resolveReactiveBindings(node, ctx) {
|
|
2229
|
+
const { reactiveBindings } = ctx.templateData;
|
|
2230
|
+
const reactiveStateApis = getReactiveStateApis();
|
|
2231
|
+
const init = node.init;
|
|
2232
|
+
const callName = init.callee.name;
|
|
2233
|
+
if (!reactiveStateApis.has(callName)) {
|
|
2234
|
+
return;
|
|
2235
|
+
}
|
|
2236
|
+
const varName = node.id.name;
|
|
2237
|
+
const initValue = init.arguments[0];
|
|
2238
|
+
reactiveBindings[varName] = {
|
|
2239
|
+
name: varName,
|
|
2240
|
+
value: initValue,
|
|
2241
|
+
source: callName,
|
|
2242
|
+
reactiveType: getReactiveType(callName)
|
|
2243
|
+
};
|
|
2244
|
+
if (callName === MACRO_API_NAMES.props) {
|
|
2245
|
+
ctx.propField = varName;
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
// src/consts/html-tag-types.ts
|
|
2250
|
+
var HTML_TAG_TYPES = {
|
|
1574
2251
|
a: "HTMLAnchorElement",
|
|
1575
2252
|
p: "HTMLParagraphElement",
|
|
1576
2253
|
div: "HTMLDivElement",
|
|
@@ -1637,83 +2314,93 @@ var HTML_TAG_TYPES = {
|
|
|
1637
2314
|
frame: "HTMLFrameElement"
|
|
1638
2315
|
};
|
|
1639
2316
|
|
|
1640
|
-
// src/
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
defineEmits: "reactive",
|
|
1656
|
-
useRoute: "reactive",
|
|
1657
|
-
useRouter: "reactive",
|
|
1658
|
-
useLink: "reactive"
|
|
1659
|
-
};
|
|
1660
|
-
|
|
1661
|
-
// src/shared/reactive-utils.ts
|
|
1662
|
-
function getReactiveType(originalName) {
|
|
1663
|
-
return REACTIVE_TYPE_MAP[originalName] || "none";
|
|
2317
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/resolve-template-ref-bindings.ts
|
|
2318
|
+
function resolveTemplateRefBindings(node, ctx) {
|
|
2319
|
+
const { refBindings } = ctx.templateData;
|
|
2320
|
+
const init = node.init;
|
|
2321
|
+
const callee = init.callee;
|
|
2322
|
+
if (callee.name !== VUE_API_MAP.useTemplateRef) {
|
|
2323
|
+
return;
|
|
2324
|
+
}
|
|
2325
|
+
const varName = node.id.name;
|
|
2326
|
+
const initValue = init.arguments[0]?.value;
|
|
2327
|
+
refBindings.domRefs[varName] = {
|
|
2328
|
+
tag: initValue,
|
|
2329
|
+
name: varName,
|
|
2330
|
+
htmlType: HTML_TAG_TYPES[initValue] || "HTMLElement"
|
|
2331
|
+
};
|
|
1664
2332
|
}
|
|
1665
|
-
|
|
1666
|
-
|
|
2333
|
+
|
|
2334
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/index.ts
|
|
2335
|
+
function resolveVarBindings(node, ctx) {
|
|
2336
|
+
const { init, id } = node;
|
|
2337
|
+
if (!t21.isIdentifier(id) || !init) {
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
const isCallExpr = t21.isCallExpression(init) && t21.isIdentifier(init.callee);
|
|
2341
|
+
if (isCallExpr) {
|
|
2342
|
+
resolveReactiveBindings(node, ctx);
|
|
2343
|
+
resolveTemplateRefBindings(node, ctx);
|
|
2344
|
+
}
|
|
1667
2345
|
}
|
|
1668
2346
|
|
|
1669
|
-
// src/core/parse/sfc/
|
|
2347
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/index.ts
|
|
1670
2348
|
function resolveScriptMeta(result, ctx) {
|
|
1671
|
-
const
|
|
1672
|
-
if (!
|
|
1673
|
-
|
|
2349
|
+
const scriptAst = result.script?.ast;
|
|
2350
|
+
if (ctx.inputType !== "sfc" || !scriptAst) {
|
|
2351
|
+
return;
|
|
2352
|
+
}
|
|
2353
|
+
traverse(scriptAst, {
|
|
1674
2354
|
VariableDeclarator(path9) {
|
|
1675
|
-
|
|
1676
|
-
if (!atComponentOrHookRoot(path9, scriptAST.program) || !t16.isIdentifier(node.id)) {
|
|
2355
|
+
if (!atComponentOrHookRoot(path9, scriptAst.program)) {
|
|
1677
2356
|
return;
|
|
1678
2357
|
}
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
2358
|
+
resolveVarBindings(path9.node, ctx);
|
|
2359
|
+
},
|
|
2360
|
+
CallExpression(path9) {
|
|
2361
|
+
resolveDeclaredOptions(path9, ctx);
|
|
2362
|
+
resolveDeclaredPropsAndEmits(path9, ctx);
|
|
2363
|
+
resolveUseAttrsExists(path9, ctx);
|
|
1683
2364
|
}
|
|
1684
2365
|
});
|
|
1685
2366
|
}
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
if (source === MACRO_API_NAMES.props) {
|
|
1696
|
-
ctx.propField = varName;
|
|
2367
|
+
|
|
2368
|
+
// src/core/parse/sfc/process/resolve-script.ts
|
|
2369
|
+
import { parse as babelParse2 } from "@babel/parser";
|
|
2370
|
+
function resolveScript(descriptor, ctx, parseResult) {
|
|
2371
|
+
if (descriptor.script) {
|
|
2372
|
+
throw new Error(
|
|
2373
|
+
`Traditional Vue <script> syntax is not supported. Please migrate to <script setup>.
|
|
2374
|
+
at <anonymous> (${ctx.filename})`
|
|
2375
|
+
);
|
|
1697
2376
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
2377
|
+
const { scriptSetup } = descriptor;
|
|
2378
|
+
if (!scriptSetup) return null;
|
|
2379
|
+
if (scriptSetup?.warnings) {
|
|
2380
|
+
scriptSetup?.warnings.forEach((msg) => {
|
|
2381
|
+
logger.warn(msg, { file: ctx.filename });
|
|
2382
|
+
});
|
|
2383
|
+
}
|
|
2384
|
+
resolveContext(scriptSetup, ctx);
|
|
2385
|
+
const parseOpts = getBabelParseOptions(scriptSetup.lang, "script", ctx.filename);
|
|
2386
|
+
parseResult.script = {
|
|
2387
|
+
ast: babelParse2(scriptSetup.content, parseOpts),
|
|
2388
|
+
source: scriptSetup
|
|
1703
2389
|
};
|
|
1704
2390
|
}
|
|
1705
|
-
function
|
|
1706
|
-
const {
|
|
1707
|
-
|
|
1708
|
-
const
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
tag,
|
|
1714
|
-
htmlType: HTML_TAG_TYPES[tag] || "HTMLElement",
|
|
1715
|
-
name: idName
|
|
2391
|
+
function resolveContext(scriptSetup, ctx) {
|
|
2392
|
+
const { scriptData } = ctx;
|
|
2393
|
+
let { content, lang } = scriptSetup;
|
|
2394
|
+
const resolveVRComment = (source) => {
|
|
2395
|
+
const regx = /\/\/\s*@vr-name:\s*(\w+)/;
|
|
2396
|
+
const nameMatch = source.match(regx);
|
|
2397
|
+
content = content.replace(regx, "");
|
|
2398
|
+
return nameMatch?.[1]?.trim() || "";
|
|
1716
2399
|
};
|
|
2400
|
+
scriptData.declaredOptions.name = resolveVRComment(content);
|
|
2401
|
+
scriptData.lang = lang || "js";
|
|
2402
|
+
scriptData.source = content;
|
|
2403
|
+
scriptSetup.content = content;
|
|
1717
2404
|
}
|
|
1718
2405
|
|
|
1719
2406
|
// src/plugins/postcss.ts
|
|
@@ -1947,205 +2634,6 @@ import { parse as babelParse3 } from "@babel/parser";
|
|
|
1947
2634
|
// src/core/transform/sfc/script/syntax-processor/index.ts
|
|
1948
2635
|
import { traverse as traverse3 } from "@babel/core";
|
|
1949
2636
|
|
|
1950
|
-
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
1951
|
-
import * as t17 from "@babel/types";
|
|
1952
|
-
function findRootVariablePath(path9) {
|
|
1953
|
-
const rootId = findRootIdentifier(path9.node);
|
|
1954
|
-
if (!rootId?.name) return null;
|
|
1955
|
-
const binding = path9.scope.getBinding(rootId.name);
|
|
1956
|
-
if (!binding) return null;
|
|
1957
|
-
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
1958
|
-
return rootPath;
|
|
1959
|
-
}
|
|
1960
|
-
function findRootIdentifier(node) {
|
|
1961
|
-
let current = node.object;
|
|
1962
|
-
while (t17.isMemberExpression(current) || t17.isOptionalMemberExpression(current)) {
|
|
1963
|
-
current = current.object;
|
|
1964
|
-
}
|
|
1965
|
-
return t17.isIdentifier(current) ? current : null;
|
|
1966
|
-
}
|
|
1967
|
-
function getVariableDeclaratorPath(path9) {
|
|
1968
|
-
if (path9.isVariableDeclarator()) {
|
|
1969
|
-
return path9;
|
|
1970
|
-
}
|
|
1971
|
-
return path9.findParent((p) => p.isVariableDeclarator());
|
|
1972
|
-
}
|
|
1973
|
-
function isVariableDeclTopLevel(path9) {
|
|
1974
|
-
const variableDeclaratorPath = path9;
|
|
1975
|
-
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
1976
|
-
if (!variableDeclarationPath) {
|
|
1977
|
-
return false;
|
|
1978
|
-
}
|
|
1979
|
-
if (variableDeclarationPath.isProgram()) {
|
|
1980
|
-
return true;
|
|
1981
|
-
}
|
|
1982
|
-
const variableDeclarationParentPath = variableDeclarationPath.parentPath;
|
|
1983
|
-
if (variableDeclarationParentPath && variableDeclarationParentPath.isProgram()) {
|
|
1984
|
-
return true;
|
|
1985
|
-
}
|
|
1986
|
-
return false;
|
|
1987
|
-
}
|
|
1988
|
-
function isRealVariableAccess(path9) {
|
|
1989
|
-
return isIdentifierAccess(path9) && !isPropertyName(path9);
|
|
1990
|
-
}
|
|
1991
|
-
function isIdentifierAccess(path9) {
|
|
1992
|
-
if (isIdentifierDeclaration(path9)) {
|
|
1993
|
-
return false;
|
|
1994
|
-
}
|
|
1995
|
-
const binding = path9.scope.getBinding(path9.node.name);
|
|
1996
|
-
if (!binding) {
|
|
1997
|
-
return true;
|
|
1998
|
-
}
|
|
1999
|
-
return binding.identifier !== path9.node;
|
|
2000
|
-
}
|
|
2001
|
-
function isIdentifierDeclaration(path9) {
|
|
2002
|
-
const parent = path9.parentPath;
|
|
2003
|
-
if (!parent) return false;
|
|
2004
|
-
if (parent.isVariableDeclarator() && parent.node.id === path9.node) {
|
|
2005
|
-
return true;
|
|
2006
|
-
}
|
|
2007
|
-
if (parent.isFunctionDeclaration() && parent.node.id === path9.node) {
|
|
2008
|
-
return true;
|
|
2009
|
-
}
|
|
2010
|
-
if (parent.isFunctionExpression() && parent.node.id === path9.node) {
|
|
2011
|
-
return true;
|
|
2012
|
-
}
|
|
2013
|
-
if (parent.isClassDeclaration() && parent.node.id === path9.node) {
|
|
2014
|
-
return true;
|
|
2015
|
-
}
|
|
2016
|
-
if (parent.isImportSpecifier() && parent.node.local === path9.node) {
|
|
2017
|
-
return true;
|
|
2018
|
-
}
|
|
2019
|
-
if (parent.isImportDefaultSpecifier() && parent.node.local === path9.node) {
|
|
2020
|
-
return true;
|
|
2021
|
-
}
|
|
2022
|
-
if (parent.isImportNamespaceSpecifier() && parent.node.local === path9.node) {
|
|
2023
|
-
return true;
|
|
2024
|
-
}
|
|
2025
|
-
if (parent.isFunction() && parent.node.params.includes(path9.node)) {
|
|
2026
|
-
return true;
|
|
2027
|
-
}
|
|
2028
|
-
if (parent.isCatchClause() && parent.node.param === path9.node) {
|
|
2029
|
-
return true;
|
|
2030
|
-
}
|
|
2031
|
-
return false;
|
|
2032
|
-
}
|
|
2033
|
-
function isPropertyName(path9) {
|
|
2034
|
-
const parent = path9.parentPath;
|
|
2035
|
-
if (!parent) return false;
|
|
2036
|
-
if (parent.isObjectProperty() && parent.node.key === path9.node) {
|
|
2037
|
-
return true;
|
|
2038
|
-
}
|
|
2039
|
-
if (parent.isClassProperty() && parent.node.key === path9.node) {
|
|
2040
|
-
return true;
|
|
2041
|
-
}
|
|
2042
|
-
if (parent.isMemberExpression() && parent.node.property === path9.node) {
|
|
2043
|
-
return true;
|
|
2044
|
-
}
|
|
2045
|
-
return false;
|
|
2046
|
-
}
|
|
2047
|
-
function replaceCallName(callExp, identifierName) {
|
|
2048
|
-
const { callee } = callExp;
|
|
2049
|
-
if (!t17.isIdentifier(callee)) return;
|
|
2050
|
-
callee.name = identifierName;
|
|
2051
|
-
if (callee.loc) {
|
|
2052
|
-
callee.loc.identifierName = identifierName;
|
|
2053
|
-
}
|
|
2054
|
-
}
|
|
2055
|
-
function replaceIdName(id, newName) {
|
|
2056
|
-
if (!t17.isIdentifier(id)) return;
|
|
2057
|
-
id.name = newName;
|
|
2058
|
-
if (id.loc) {
|
|
2059
|
-
id.loc.identifierName = newName;
|
|
2060
|
-
}
|
|
2061
|
-
}
|
|
2062
|
-
function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
2063
|
-
const { filename, scriptData } = ctx;
|
|
2064
|
-
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
2065
|
-
const ts = expressionToTSType(exp);
|
|
2066
|
-
return tsTypeAnnotation7 ? t17.tsTypeAnnotation(ts) : ts;
|
|
2067
|
-
}
|
|
2068
|
-
function expressionToTSType(exp) {
|
|
2069
|
-
if (t17.isStringLiteral(exp)) return t17.tsStringKeyword();
|
|
2070
|
-
if (t17.isNumericLiteral(exp)) return t17.tsNumberKeyword();
|
|
2071
|
-
if (t17.isBooleanLiteral(exp)) return t17.tsBooleanKeyword();
|
|
2072
|
-
if (t17.isArrayExpression(exp)) return t17.tsArrayType(t17.tsAnyKeyword());
|
|
2073
|
-
if (t17.isObjectExpression(exp)) {
|
|
2074
|
-
const members = [];
|
|
2075
|
-
for (const p of exp.properties) {
|
|
2076
|
-
if (!t17.isObjectProperty(p)) continue;
|
|
2077
|
-
let key;
|
|
2078
|
-
if (t17.isIdentifier(p.key)) key = p.key.name;
|
|
2079
|
-
else if (t17.isStringLiteral(p.key)) key = p.key.value;
|
|
2080
|
-
if (!key) continue;
|
|
2081
|
-
if (t17.isExpression(p.value)) {
|
|
2082
|
-
members.push(
|
|
2083
|
-
t17.tsPropertySignature(t17.identifier(key), t17.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
2084
|
-
);
|
|
2085
|
-
} else {
|
|
2086
|
-
members.push(
|
|
2087
|
-
t17.tsPropertySignature(t17.identifier(key), t17.tsTypeAnnotation(t17.tsAnyKeyword()))
|
|
2088
|
-
);
|
|
2089
|
-
}
|
|
2090
|
-
}
|
|
2091
|
-
return t17.tsTypeLiteral(members);
|
|
2092
|
-
}
|
|
2093
|
-
if (t17.isArrowFunctionExpression(exp) || t17.isFunctionExpression(exp)) {
|
|
2094
|
-
const params = exp.params.map((p, i) => {
|
|
2095
|
-
const id = t17.isIdentifier(p) ? t17.identifier(p.name) : t17.identifier(`arg${i}`);
|
|
2096
|
-
id.typeAnnotation = t17.tsTypeAnnotation(t17.tsAnyKeyword());
|
|
2097
|
-
return id;
|
|
2098
|
-
});
|
|
2099
|
-
let returnType = t17.tsAnyKeyword();
|
|
2100
|
-
if (t17.isBlockStatement(exp.body)) {
|
|
2101
|
-
for (const stmt of exp.body.body) {
|
|
2102
|
-
if (t17.isReturnStatement(stmt) && stmt.argument) {
|
|
2103
|
-
if (t17.isExpression(stmt.argument)) {
|
|
2104
|
-
returnType = expressionToTSType(stmt.argument);
|
|
2105
|
-
break;
|
|
2106
|
-
}
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
} else if (t17.isExpression(exp.body)) {
|
|
2110
|
-
returnType = expressionToTSType(exp.body);
|
|
2111
|
-
}
|
|
2112
|
-
return t17.tsFunctionType(null, params, t17.tsTypeAnnotation(returnType));
|
|
2113
|
-
}
|
|
2114
|
-
return t17.tsAnyKeyword();
|
|
2115
|
-
}
|
|
2116
|
-
function isCalleeNamed(node, name) {
|
|
2117
|
-
if (!t17.isIdentifier(node.callee)) {
|
|
2118
|
-
return false;
|
|
2119
|
-
}
|
|
2120
|
-
return node.callee.name === name;
|
|
2121
|
-
}
|
|
2122
|
-
function isSimpleLiteral(node) {
|
|
2123
|
-
if (!node) return false;
|
|
2124
|
-
if (t17.isStringLiteral(node) || t17.isNumericLiteral(node) || t17.isBooleanLiteral(node) || t17.isNullLiteral(node) || t17.isRegExpLiteral(node) || t17.isBigIntLiteral(node) || t17.isDecimalLiteral(node)) {
|
|
2125
|
-
return true;
|
|
2126
|
-
}
|
|
2127
|
-
return false;
|
|
2128
|
-
}
|
|
2129
|
-
function forkNode(node, deep = true) {
|
|
2130
|
-
const newNode = t17.cloneNode(node, deep);
|
|
2131
|
-
newNode.leadingComments = node.leadingComments;
|
|
2132
|
-
newNode.innerComments = node.innerComments;
|
|
2133
|
-
newNode.trailingComments = null;
|
|
2134
|
-
cleanNodeLoc(node);
|
|
2135
|
-
cleanNodeComments(node);
|
|
2136
|
-
return newNode;
|
|
2137
|
-
}
|
|
2138
|
-
function cleanNodeLoc(node) {
|
|
2139
|
-
node.start = null;
|
|
2140
|
-
node.end = null;
|
|
2141
|
-
node.loc = null;
|
|
2142
|
-
}
|
|
2143
|
-
function cleanNodeComments(node) {
|
|
2144
|
-
node.leadingComments = null;
|
|
2145
|
-
node.innerComments = null;
|
|
2146
|
-
node.trailingComments = null;
|
|
2147
|
-
}
|
|
2148
|
-
|
|
2149
2637
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-global-type-chunk.ts
|
|
2150
2638
|
function resolveGlobalTypeChunks(path9, ir) {
|
|
2151
2639
|
if (!path9.parentPath?.isProgram()) {
|
|
@@ -2157,12 +2645,12 @@ function resolveGlobalTypeChunks(path9, ir) {
|
|
|
2157
2645
|
}
|
|
2158
2646
|
|
|
2159
2647
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-module-chunk.ts
|
|
2160
|
-
import * as
|
|
2648
|
+
import * as t22 from "@babel/types";
|
|
2161
2649
|
function resolveModuleChunks(path9, ir) {
|
|
2162
2650
|
const forked = forkNode(path9.node);
|
|
2163
|
-
if (
|
|
2651
|
+
if (t22.isImportDeclaration(forked)) {
|
|
2164
2652
|
ir.imports.push(forked);
|
|
2165
|
-
} else if (
|
|
2653
|
+
} else if (t22.isExportDeclaration(forked)) {
|
|
2166
2654
|
ir.exports.push(forked);
|
|
2167
2655
|
}
|
|
2168
2656
|
path9.remove();
|
|
@@ -2220,7 +2708,7 @@ function resolveASTChunks(ctx, ast) {
|
|
|
2220
2708
|
}
|
|
2221
2709
|
|
|
2222
2710
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/index.ts
|
|
2223
|
-
import * as
|
|
2711
|
+
import * as t23 from "@babel/types";
|
|
2224
2712
|
|
|
2225
2713
|
// src/core/transform/shared.ts
|
|
2226
2714
|
function recordImport(ctx, pkg, name, onDemand = true) {
|
|
@@ -2380,7 +2868,7 @@ function resolveRuntimeImports(ctx) {
|
|
|
2380
2868
|
} else if (finalModuleName === PACKAGE_NAME.react) {
|
|
2381
2869
|
path9.insertAfter(importNodes);
|
|
2382
2870
|
} else {
|
|
2383
|
-
|
|
2871
|
+
forkFilePreambleLeadingComments(importNodes[0], node);
|
|
2384
2872
|
path9.insertBefore(importNodes);
|
|
2385
2873
|
}
|
|
2386
2874
|
}
|
|
@@ -2395,7 +2883,7 @@ function resolveRuntimeImports(ctx) {
|
|
|
2395
2883
|
const { node } = path9;
|
|
2396
2884
|
const importNodes = createImportNodes(ctx);
|
|
2397
2885
|
if (!importNodes.length) return;
|
|
2398
|
-
|
|
2886
|
+
forkProgramTopLeadingComments(importNodes[0], node);
|
|
2399
2887
|
path9.unshiftContainer("body", importNodes);
|
|
2400
2888
|
}
|
|
2401
2889
|
}
|
|
@@ -2408,10 +2896,10 @@ function mergeImports(currentNode, ctx, moduleName) {
|
|
|
2408
2896
|
}
|
|
2409
2897
|
const currentImports = /* @__PURE__ */ new Set();
|
|
2410
2898
|
for (const spec of currentNode.specifiers) {
|
|
2411
|
-
if (
|
|
2899
|
+
if (t23.isImportSpecifier(spec) && t23.isIdentifier(spec.imported)) {
|
|
2412
2900
|
currentImports.add(spec.imported.name);
|
|
2413
2901
|
}
|
|
2414
|
-
if (
|
|
2902
|
+
if (t23.isImportDefaultSpecifier(spec) && t23.isIdentifier(spec.local)) {
|
|
2415
2903
|
currentImports.add(spec.local.name);
|
|
2416
2904
|
}
|
|
2417
2905
|
}
|
|
@@ -2419,8 +2907,8 @@ function mergeImports(currentNode, ctx, moduleName) {
|
|
|
2419
2907
|
if (currentImports.has(item.name)) {
|
|
2420
2908
|
continue;
|
|
2421
2909
|
}
|
|
2422
|
-
const local =
|
|
2423
|
-
const newNode = !item.onDemand ?
|
|
2910
|
+
const local = t23.identifier(item.name);
|
|
2911
|
+
const newNode = !item.onDemand ? t23.importDefaultSpecifier(local) : t23.importSpecifier(local, local);
|
|
2424
2912
|
currentNode.specifiers.push(newNode);
|
|
2425
2913
|
}
|
|
2426
2914
|
ctx.imports.delete(moduleName);
|
|
@@ -2429,10 +2917,10 @@ function createImportNodes(ctx) {
|
|
|
2429
2917
|
const result = [];
|
|
2430
2918
|
const importDeclarations = Array.from(ctx.imports).map(([moduleName, items]) => {
|
|
2431
2919
|
const specifiers = items.map((item) => {
|
|
2432
|
-
const local =
|
|
2433
|
-
return item.onDemand ?
|
|
2920
|
+
const local = t23.identifier(item.name);
|
|
2921
|
+
return item.onDemand ? t23.importSpecifier(local, local) : t23.importDefaultSpecifier(local);
|
|
2434
2922
|
});
|
|
2435
|
-
return
|
|
2923
|
+
return t23.importDeclaration(specifiers, t23.stringLiteral(moduleName));
|
|
2436
2924
|
});
|
|
2437
2925
|
for (const decl of importDeclarations) {
|
|
2438
2926
|
const name = decl.source.value;
|
|
@@ -2442,35 +2930,59 @@ function createImportNodes(ctx) {
|
|
|
2442
2930
|
result.push(decl);
|
|
2443
2931
|
}
|
|
2444
2932
|
}
|
|
2445
|
-
return result;
|
|
2446
|
-
}
|
|
2447
|
-
function
|
|
2448
|
-
const
|
|
2449
|
-
if (!
|
|
2933
|
+
return result;
|
|
2934
|
+
}
|
|
2935
|
+
function forkProgramTopLeadingComments(target, program3) {
|
|
2936
|
+
const [firstStatement] = program3.body;
|
|
2937
|
+
if (!firstStatement) {
|
|
2938
|
+
return;
|
|
2939
|
+
}
|
|
2940
|
+
forkFilePreambleLeadingComments(target, firstStatement);
|
|
2941
|
+
}
|
|
2942
|
+
function forkFilePreambleLeadingComments(target, source) {
|
|
2943
|
+
const { leadingComments } = source;
|
|
2944
|
+
if (!leadingComments?.length) {
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
const commentsToMove = [];
|
|
2948
|
+
const remainingComments = [];
|
|
2949
|
+
for (const comment of leadingComments) {
|
|
2950
|
+
if (isFilePreambleComment(comment.value)) {
|
|
2951
|
+
commentsToMove.push(comment);
|
|
2952
|
+
} else {
|
|
2953
|
+
remainingComments.push(comment);
|
|
2954
|
+
}
|
|
2955
|
+
}
|
|
2956
|
+
if (!commentsToMove.length) {
|
|
2450
2957
|
return;
|
|
2451
2958
|
}
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2959
|
+
source.leadingComments = remainingComments.length ? remainingComments : null;
|
|
2960
|
+
target.leadingComments = [...target.leadingComments ?? [], ...commentsToMove];
|
|
2961
|
+
}
|
|
2962
|
+
function isFilePreambleComment(commentValue) {
|
|
2963
|
+
const value = commentValue.trim();
|
|
2964
|
+
return /^(?:@ts-(?:nocheck|check)\b|eslint-(?:disable|enable|disable-next-line|env|global)\b|@jsx(?:ImportSource|Runtime)?\b|!)/.test(
|
|
2965
|
+
value
|
|
2966
|
+
);
|
|
2455
2967
|
}
|
|
2456
2968
|
|
|
2457
2969
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-sfc-css-import.ts
|
|
2458
|
-
import * as
|
|
2970
|
+
import * as t24 from "@babel/types";
|
|
2459
2971
|
function resolveSfcCssImport(ctx) {
|
|
2460
2972
|
if (ctx.inputType !== "sfc") return;
|
|
2461
2973
|
const scriptIR = getScriptIR(ctx);
|
|
2462
2974
|
const { filePath, moduleName } = ctx.styleData;
|
|
2463
2975
|
if (!filePath) return;
|
|
2464
2976
|
const styleFilename = normalizePath(filePath).split("/").pop();
|
|
2465
|
-
const importDecl =
|
|
2466
|
-
!moduleName ? [] : [
|
|
2467
|
-
|
|
2977
|
+
const importDecl = t24.importDeclaration(
|
|
2978
|
+
!moduleName ? [] : [t24.importDefaultSpecifier(t24.identifier(moduleName))],
|
|
2979
|
+
t24.stringLiteral(`./${styleFilename}`)
|
|
2468
2980
|
);
|
|
2469
2981
|
scriptIR.imports.push(importDecl);
|
|
2470
2982
|
}
|
|
2471
2983
|
|
|
2472
2984
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2473
|
-
import * as
|
|
2985
|
+
import * as t25 from "@babel/types";
|
|
2474
2986
|
function resolveDefineAsyncComponent(ctx) {
|
|
2475
2987
|
return {
|
|
2476
2988
|
CallExpression(path9) {
|
|
@@ -2486,11 +2998,11 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
2486
2998
|
};
|
|
2487
2999
|
}
|
|
2488
3000
|
function checkIsUnsupported(ctx, arg) {
|
|
2489
|
-
if (
|
|
3001
|
+
if (t25.isFunction(arg)) {
|
|
2490
3002
|
checkIsDynamicImport(ctx, arg);
|
|
2491
|
-
} else if (
|
|
3003
|
+
} else if (t25.isObjectExpression(arg)) {
|
|
2492
3004
|
const { value } = arg.properties.find(
|
|
2493
|
-
(p) =>
|
|
3005
|
+
(p) => t25.isObjectProperty(p) && t25.isIdentifier(p.key) && p.key.name === "loader"
|
|
2494
3006
|
);
|
|
2495
3007
|
checkIsDynamicImport(ctx, value);
|
|
2496
3008
|
if (arg.properties.length > 1) {
|
|
@@ -2501,7 +3013,7 @@ function checkIsUnsupported(ctx, arg) {
|
|
|
2501
3013
|
function checkIsDynamicImport(ctx, node) {
|
|
2502
3014
|
const { scriptData, filename } = ctx;
|
|
2503
3015
|
const warnIsNotImport = (target) => {
|
|
2504
|
-
if (!target || !
|
|
3016
|
+
if (!target || !t25.isImport(target)) {
|
|
2505
3017
|
logger.error(
|
|
2506
3018
|
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2507
3019
|
{
|
|
@@ -2512,20 +3024,20 @@ function checkIsDynamicImport(ctx, node) {
|
|
|
2512
3024
|
);
|
|
2513
3025
|
}
|
|
2514
3026
|
};
|
|
2515
|
-
if (
|
|
3027
|
+
if (t25.isFunction(node)) {
|
|
2516
3028
|
checkIsDynamicImport(ctx, node.body);
|
|
2517
3029
|
return;
|
|
2518
3030
|
}
|
|
2519
|
-
if (
|
|
3031
|
+
if (t25.isBlockStatement(node)) {
|
|
2520
3032
|
const [returnSmt] = node.body;
|
|
2521
|
-
if (
|
|
3033
|
+
if (t25.isReturnStatement(returnSmt)) {
|
|
2522
3034
|
warnIsNotImport(returnSmt.argument);
|
|
2523
3035
|
}
|
|
2524
3036
|
return;
|
|
2525
3037
|
}
|
|
2526
|
-
if (
|
|
3038
|
+
if (t25.isCallExpression(node)) {
|
|
2527
3039
|
warnIsNotImport(node.callee);
|
|
2528
|
-
if (
|
|
3040
|
+
if (t25.isStringLiteral(node.arguments[0])) {
|
|
2529
3041
|
replaceVueSuffix(node.arguments[0]);
|
|
2530
3042
|
}
|
|
2531
3043
|
return;
|
|
@@ -2574,24 +3086,24 @@ function pushToGlobalScope(path9, ctx) {
|
|
|
2574
3086
|
}
|
|
2575
3087
|
|
|
2576
3088
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2577
|
-
import * as
|
|
3089
|
+
import * as t27 from "@babel/types";
|
|
2578
3090
|
|
|
2579
3091
|
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2580
|
-
import * as
|
|
3092
|
+
import * as t26 from "@babel/types";
|
|
2581
3093
|
function createUseCallback(body, deps) {
|
|
2582
|
-
return
|
|
3094
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useCallback), [
|
|
2583
3095
|
body,
|
|
2584
|
-
deps ??
|
|
3096
|
+
deps ?? t26.arrayExpression([])
|
|
2585
3097
|
]);
|
|
2586
3098
|
}
|
|
2587
3099
|
function createUseMemo(body, deps) {
|
|
2588
|
-
return
|
|
2589
|
-
|
|
2590
|
-
deps ??
|
|
3100
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useMemo), [
|
|
3101
|
+
t26.arrowFunctionExpression([], body),
|
|
3102
|
+
deps ?? t26.arrayExpression([])
|
|
2591
3103
|
]);
|
|
2592
3104
|
}
|
|
2593
3105
|
function createUseImperativeHandle(refId, init) {
|
|
2594
|
-
return
|
|
3106
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2595
3107
|
}
|
|
2596
3108
|
|
|
2597
3109
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
@@ -2612,24 +3124,23 @@ function resolveDefineExpose(ctx) {
|
|
|
2612
3124
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2613
3125
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2614
3126
|
recordImport(ctx, adapter.package, adapter.target);
|
|
2615
|
-
if (!
|
|
3127
|
+
if (!t27.isObjectExpression(expose) && !t27.isFunction(expose)) {
|
|
2616
3128
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2617
3129
|
file: filename,
|
|
2618
3130
|
loc: expose.loc,
|
|
2619
3131
|
source: scriptData.source
|
|
2620
3132
|
});
|
|
2621
3133
|
}
|
|
2622
|
-
const init = !
|
|
3134
|
+
const init = !t27.isFunction(expose) ? t27.arrowFunctionExpression([], expose) : expose;
|
|
2623
3135
|
const { forwardRef } = scriptData;
|
|
2624
|
-
const newNode = createUseImperativeHandle(
|
|
3136
|
+
const newNode = createUseImperativeHandle(t27.identifier(forwardRef.refField), init);
|
|
2625
3137
|
forwardRef.enabled = true;
|
|
2626
|
-
path9
|
|
3138
|
+
replaceNode(path9, newNode, node);
|
|
2627
3139
|
}
|
|
2628
3140
|
};
|
|
2629
3141
|
}
|
|
2630
3142
|
|
|
2631
3143
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2632
|
-
import * as t24 from "@babel/types";
|
|
2633
3144
|
function resolveDefineOptions(ctx) {
|
|
2634
3145
|
return {
|
|
2635
3146
|
CallExpression(path9) {
|
|
@@ -2637,53 +3148,13 @@ function resolveDefineOptions(ctx) {
|
|
|
2637
3148
|
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
2638
3149
|
return;
|
|
2639
3150
|
}
|
|
2640
|
-
const [options] = node.arguments;
|
|
2641
|
-
const { filename, scriptData } = ctx;
|
|
2642
|
-
if (!options) {
|
|
2643
|
-
logger.warn("defineOptions was found, but no options were provided.", {
|
|
2644
|
-
source: scriptData.source,
|
|
2645
|
-
file: filename,
|
|
2646
|
-
loc: node?.loc
|
|
2647
|
-
});
|
|
2648
|
-
path9.remove();
|
|
2649
|
-
return;
|
|
2650
|
-
}
|
|
2651
|
-
if (!t24.isObjectExpression(options)) {
|
|
2652
|
-
logger.warn("Argument for defineOptions must be an object expression.", {
|
|
2653
|
-
source: scriptData.source,
|
|
2654
|
-
file: filename,
|
|
2655
|
-
loc: options?.loc
|
|
2656
|
-
});
|
|
2657
|
-
} else {
|
|
2658
|
-
for (const prop of options.properties) {
|
|
2659
|
-
if (!t24.isObjectProperty(prop) || !t24.isIdentifier(prop.key)) {
|
|
2660
|
-
continue;
|
|
2661
|
-
}
|
|
2662
|
-
extractName(prop, ctx);
|
|
2663
|
-
}
|
|
2664
|
-
}
|
|
2665
3151
|
path9.remove();
|
|
2666
3152
|
}
|
|
2667
3153
|
};
|
|
2668
3154
|
}
|
|
2669
|
-
function extractName(prop, ctx) {
|
|
2670
|
-
if (ctx.compName) return;
|
|
2671
|
-
const { filename, scriptData } = ctx;
|
|
2672
|
-
if (!prop.computed && prop.key.name === "name") {
|
|
2673
|
-
if (t24.isStringLiteral(prop.value)) {
|
|
2674
|
-
ctx.compName = prop.value.value;
|
|
2675
|
-
return;
|
|
2676
|
-
}
|
|
2677
|
-
logger.error(`name must be a string type, but got ${prop.value.type}`, {
|
|
2678
|
-
source: scriptData.source,
|
|
2679
|
-
file: filename,
|
|
2680
|
-
loc: prop.key?.loc
|
|
2681
|
-
});
|
|
2682
|
-
}
|
|
2683
|
-
}
|
|
2684
3155
|
|
|
2685
3156
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2686
|
-
import * as
|
|
3157
|
+
import * as t28 from "@babel/types";
|
|
2687
3158
|
function resolveEmitCalls(ctx) {
|
|
2688
3159
|
const formatEmitEventName = (raw) => {
|
|
2689
3160
|
if (raw.startsWith("update:")) {
|
|
@@ -2697,7 +3168,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2697
3168
|
CallExpression(path9) {
|
|
2698
3169
|
const { node } = path9;
|
|
2699
3170
|
const { filename, templateData, scriptData } = ctx;
|
|
2700
|
-
if (!
|
|
3171
|
+
if (!t28.isIdentifier(node.callee)) return;
|
|
2701
3172
|
const { name } = node.callee;
|
|
2702
3173
|
const checkIfFromDefineEmits = () => {
|
|
2703
3174
|
let result = false;
|
|
@@ -2709,7 +3180,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2709
3180
|
const binding = path9.scope.getBinding(name);
|
|
2710
3181
|
if (binding) {
|
|
2711
3182
|
const parent = binding.path.node;
|
|
2712
|
-
if (
|
|
3183
|
+
if (t28.isVariableDeclarator(parent) && t28.isCallExpression(parent.init) && t28.isIdentifier(parent.init.callee)) {
|
|
2713
3184
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2714
3185
|
}
|
|
2715
3186
|
}
|
|
@@ -2718,7 +3189,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2718
3189
|
};
|
|
2719
3190
|
if (!checkIfFromDefineEmits()) return;
|
|
2720
3191
|
const [callee, ...args] = node.arguments;
|
|
2721
|
-
const eventName =
|
|
3192
|
+
const eventName = t28.isStringLiteral(callee) ? formatEmitEventName(callee.value) : void 0;
|
|
2722
3193
|
if (!eventName) {
|
|
2723
3194
|
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2724
3195
|
file: filename,
|
|
@@ -2728,46 +3199,46 @@ function resolveEmitCalls(ctx) {
|
|
|
2728
3199
|
path9.remove();
|
|
2729
3200
|
return;
|
|
2730
3201
|
}
|
|
2731
|
-
const propCall =
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
3202
|
+
const propCall = t28.optionalCallExpression(
|
|
3203
|
+
t28.optionalMemberExpression(
|
|
3204
|
+
t28.identifier(ctx.propField),
|
|
3205
|
+
t28.identifier(eventName),
|
|
2735
3206
|
false,
|
|
2736
3207
|
true
|
|
2737
3208
|
),
|
|
2738
3209
|
args,
|
|
2739
3210
|
true
|
|
2740
3211
|
);
|
|
2741
|
-
path9
|
|
3212
|
+
replaceNode(path9, propCall, node);
|
|
2742
3213
|
}
|
|
2743
3214
|
};
|
|
2744
3215
|
}
|
|
2745
3216
|
|
|
2746
3217
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2747
|
-
import * as
|
|
3218
|
+
import * as t36 from "@babel/types";
|
|
2748
3219
|
|
|
2749
3220
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2750
|
-
import * as
|
|
3221
|
+
import * as t30 from "@babel/types";
|
|
2751
3222
|
|
|
2752
3223
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2753
|
-
import * as
|
|
3224
|
+
import * as t29 from "@babel/types";
|
|
2754
3225
|
function cloneCallableParams(params) {
|
|
2755
3226
|
const cloneCallableParam = (param, index) => {
|
|
2756
|
-
if (
|
|
3227
|
+
if (t29.isRestElement(param)) {
|
|
2757
3228
|
const arg = param.argument;
|
|
2758
|
-
const name =
|
|
2759
|
-
const rest =
|
|
2760
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
3229
|
+
const name = t29.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
3230
|
+
const rest = t29.restElement(t29.identifier(name));
|
|
3231
|
+
rest.typeAnnotation = param.typeAnnotation || (t29.isIdentifier(arg) ? arg.typeAnnotation : null) || t29.tsTypeAnnotation(t29.tsArrayType(t29.tsAnyKeyword()));
|
|
2761
3232
|
return rest;
|
|
2762
3233
|
}
|
|
2763
|
-
if (
|
|
2764
|
-
const id =
|
|
3234
|
+
if (t29.isIdentifier(param)) {
|
|
3235
|
+
const id = t29.identifier(param.name || `arg${index}`);
|
|
2765
3236
|
id.optional = param.optional;
|
|
2766
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
3237
|
+
id.typeAnnotation = param.typeAnnotation || t29.tsTypeAnnotation(t29.tsAnyKeyword());
|
|
2767
3238
|
return id;
|
|
2768
3239
|
}
|
|
2769
|
-
const fallback =
|
|
2770
|
-
fallback.typeAnnotation =
|
|
3240
|
+
const fallback = t29.identifier(`arg${index}`);
|
|
3241
|
+
fallback.typeAnnotation = t29.tsTypeAnnotation(t29.tsAnyKeyword());
|
|
2771
3242
|
return fallback;
|
|
2772
3243
|
};
|
|
2773
3244
|
return params.map(cloneCallableParam);
|
|
@@ -2777,18 +3248,18 @@ function cloneCallableParams(params) {
|
|
|
2777
3248
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2778
3249
|
return {
|
|
2779
3250
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path9) {
|
|
2780
|
-
if (!
|
|
3251
|
+
if (!t30.isProgram(path9.parent)) return;
|
|
2781
3252
|
const { node } = path9;
|
|
2782
|
-
if (
|
|
2783
|
-
const typeLiteral =
|
|
3253
|
+
if (t30.isTSInterfaceDeclaration(node)) {
|
|
3254
|
+
const typeLiteral = t30.tsTypeLiteral(node.body.body);
|
|
2784
3255
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2785
3256
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2786
|
-
if (resolved &&
|
|
3257
|
+
if (resolved && t30.isTSTypeLiteral(resolved)) {
|
|
2787
3258
|
node.body.body = resolved.members;
|
|
2788
3259
|
}
|
|
2789
3260
|
return;
|
|
2790
3261
|
}
|
|
2791
|
-
if (
|
|
3262
|
+
if (t30.isTSTypeAliasDeclaration(node)) {
|
|
2792
3263
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2793
3264
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2794
3265
|
if (resolved) {
|
|
@@ -2799,47 +3270,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2799
3270
|
};
|
|
2800
3271
|
}
|
|
2801
3272
|
function resolveTopLevelEmitType(tsType) {
|
|
2802
|
-
if (
|
|
3273
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
2803
3274
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2804
3275
|
}
|
|
2805
|
-
if (
|
|
3276
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
2806
3277
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2807
3278
|
return tsType;
|
|
2808
3279
|
}
|
|
2809
3280
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2810
|
-
return
|
|
3281
|
+
return t30.tsTypeReference(
|
|
2811
3282
|
tsType.typeName,
|
|
2812
|
-
|
|
3283
|
+
t30.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2813
3284
|
);
|
|
2814
3285
|
}
|
|
2815
|
-
if (
|
|
3286
|
+
if (t30.isTSIntersectionType(tsType)) {
|
|
2816
3287
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2817
3288
|
if (!types.length) return null;
|
|
2818
3289
|
if (types.length === 1) return types[0];
|
|
2819
|
-
return
|
|
3290
|
+
return t30.tsIntersectionType(types);
|
|
2820
3291
|
}
|
|
2821
|
-
if (
|
|
3292
|
+
if (t30.isTSUnionType(tsType)) {
|
|
2822
3293
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2823
3294
|
if (!types.length) return null;
|
|
2824
3295
|
if (types.length === 1) return types[0];
|
|
2825
|
-
return
|
|
3296
|
+
return t30.tsUnionType(types);
|
|
2826
3297
|
}
|
|
2827
|
-
if (
|
|
3298
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
2828
3299
|
const members = [];
|
|
2829
3300
|
for (const member of tsType.members) {
|
|
2830
|
-
if (
|
|
3301
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
2831
3302
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2832
3303
|
continue;
|
|
2833
3304
|
}
|
|
2834
3305
|
members.push(member);
|
|
2835
3306
|
}
|
|
2836
3307
|
if (!members.length) return null;
|
|
2837
|
-
return
|
|
3308
|
+
return t30.tsTypeLiteral(members);
|
|
2838
3309
|
}
|
|
2839
|
-
if (
|
|
3310
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
2840
3311
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2841
3312
|
if (!props.length) return null;
|
|
2842
|
-
return
|
|
3313
|
+
return t30.tsTypeLiteral(props);
|
|
2843
3314
|
}
|
|
2844
3315
|
return tsType;
|
|
2845
3316
|
}
|
|
@@ -2860,41 +3331,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2860
3331
|
propsTSIface: { emitTypes }
|
|
2861
3332
|
} = ctx.scriptData;
|
|
2862
3333
|
const members = [];
|
|
2863
|
-
if (
|
|
3334
|
+
if (t30.isArrayExpression(runtimeArg)) {
|
|
2864
3335
|
for (const element of runtimeArg.elements) {
|
|
2865
|
-
if (!element || !
|
|
3336
|
+
if (!element || !t30.isStringLiteral(element)) continue;
|
|
2866
3337
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2867
3338
|
const key = buildKey(handlerName);
|
|
2868
|
-
const fnType =
|
|
3339
|
+
const fnType = t30.tsFunctionType(
|
|
2869
3340
|
null,
|
|
2870
3341
|
[createRestAnyParam("args")],
|
|
2871
|
-
|
|
3342
|
+
t30.tsTypeAnnotation(t30.tsAnyKeyword())
|
|
2872
3343
|
);
|
|
2873
|
-
const prop =
|
|
3344
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
2874
3345
|
prop.optional = true;
|
|
2875
3346
|
members.push(prop);
|
|
2876
3347
|
}
|
|
2877
3348
|
if (members.length) {
|
|
2878
|
-
emitTypes.push(
|
|
3349
|
+
emitTypes.push(t30.tsTypeLiteral(members));
|
|
2879
3350
|
}
|
|
2880
3351
|
return;
|
|
2881
3352
|
}
|
|
2882
|
-
if (
|
|
3353
|
+
if (t30.isObjectExpression(runtimeArg)) {
|
|
2883
3354
|
for (const prop of runtimeArg.properties) {
|
|
2884
|
-
if (!
|
|
2885
|
-
if (
|
|
3355
|
+
if (!t30.isObjectProperty(prop)) continue;
|
|
3356
|
+
if (t30.isSpreadElement(prop)) continue;
|
|
2886
3357
|
const rawName = resolvePropName(prop.key);
|
|
2887
3358
|
if (!rawName) continue;
|
|
2888
3359
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2889
3360
|
const key = buildKey(handlerName);
|
|
2890
|
-
const params =
|
|
2891
|
-
const fnType =
|
|
2892
|
-
const propSig =
|
|
3361
|
+
const params = t30.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
3362
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(t30.tsAnyKeyword()));
|
|
3363
|
+
const propSig = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
2893
3364
|
propSig.optional = true;
|
|
2894
3365
|
members.push(propSig);
|
|
2895
3366
|
}
|
|
2896
3367
|
if (members.length) {
|
|
2897
|
-
emitTypes.push(
|
|
3368
|
+
emitTypes.push(t30.tsTypeLiteral(members));
|
|
2898
3369
|
}
|
|
2899
3370
|
}
|
|
2900
3371
|
}
|
|
@@ -2915,129 +3386,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2915
3386
|
return `on${name}`;
|
|
2916
3387
|
}
|
|
2917
3388
|
function resolvePropName(key) {
|
|
2918
|
-
if (
|
|
2919
|
-
if (
|
|
2920
|
-
if (
|
|
3389
|
+
if (t30.isIdentifier(key)) return key.name;
|
|
3390
|
+
if (t30.isStringLiteral(key)) return key.value;
|
|
3391
|
+
if (t30.isNumericLiteral(key)) return String(key.value);
|
|
2921
3392
|
return null;
|
|
2922
3393
|
}
|
|
2923
3394
|
function buildKey(name) {
|
|
2924
|
-
return
|
|
3395
|
+
return t30.isValidIdentifier(name) ? t30.identifier(name) : t30.stringLiteral(name);
|
|
2925
3396
|
}
|
|
2926
3397
|
function createRestAnyParam(name) {
|
|
2927
|
-
const id =
|
|
2928
|
-
const rest =
|
|
2929
|
-
rest.typeAnnotation =
|
|
3398
|
+
const id = t30.identifier(name);
|
|
3399
|
+
const rest = t30.restElement(id);
|
|
3400
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(t30.tsArrayType(t30.tsAnyKeyword()));
|
|
2930
3401
|
return rest;
|
|
2931
3402
|
}
|
|
2932
3403
|
function resolveRuntimeTupleParams(value) {
|
|
2933
3404
|
const params = [];
|
|
2934
3405
|
value.elements.forEach((element, index) => {
|
|
2935
3406
|
if (!element) return;
|
|
2936
|
-
if (
|
|
3407
|
+
if (t30.isSpreadElement(element)) {
|
|
2937
3408
|
params.push(createRestAnyParam(`args${index}`));
|
|
2938
3409
|
return;
|
|
2939
3410
|
}
|
|
2940
|
-
if (
|
|
2941
|
-
const id =
|
|
2942
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
3411
|
+
if (t30.isIdentifier(element)) {
|
|
3412
|
+
const id = t30.identifier(element.name);
|
|
3413
|
+
id.typeAnnotation = element.typeAnnotation || t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
2943
3414
|
params.push(id);
|
|
2944
3415
|
return;
|
|
2945
3416
|
}
|
|
2946
|
-
if (
|
|
2947
|
-
const id =
|
|
2948
|
-
id.typeAnnotation =
|
|
3417
|
+
if (t30.isTSAsExpression(element)) {
|
|
3418
|
+
const id = t30.identifier(`arg${index}`);
|
|
3419
|
+
id.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
2949
3420
|
params.push(id);
|
|
2950
3421
|
return;
|
|
2951
3422
|
}
|
|
2952
|
-
const fallback =
|
|
2953
|
-
fallback.typeAnnotation =
|
|
3423
|
+
const fallback = t30.identifier(`arg${index}`);
|
|
3424
|
+
fallback.typeAnnotation = t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
2954
3425
|
params.push(fallback);
|
|
2955
3426
|
});
|
|
2956
3427
|
return params;
|
|
2957
3428
|
}
|
|
2958
3429
|
function resolveExplicitEmitType(tsType) {
|
|
2959
|
-
if (
|
|
3430
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
2960
3431
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2961
3432
|
}
|
|
2962
|
-
if (
|
|
3433
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
2963
3434
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2964
3435
|
return tsType;
|
|
2965
3436
|
}
|
|
2966
3437
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2967
|
-
return
|
|
3438
|
+
return t30.tsTypeReference(
|
|
2968
3439
|
tsType.typeName,
|
|
2969
|
-
|
|
3440
|
+
t30.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2970
3441
|
);
|
|
2971
3442
|
}
|
|
2972
|
-
if (
|
|
3443
|
+
if (t30.isTSIntersectionType(tsType)) {
|
|
2973
3444
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2974
3445
|
if (!types.length) return null;
|
|
2975
3446
|
if (types.length === 1) return types[0];
|
|
2976
|
-
return
|
|
3447
|
+
return t30.tsIntersectionType(types);
|
|
2977
3448
|
}
|
|
2978
|
-
if (
|
|
3449
|
+
if (t30.isTSUnionType(tsType)) {
|
|
2979
3450
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2980
3451
|
if (!types.length) return null;
|
|
2981
3452
|
if (types.length === 1) return types[0];
|
|
2982
|
-
return
|
|
3453
|
+
return t30.tsUnionType(types);
|
|
2983
3454
|
}
|
|
2984
|
-
if (
|
|
3455
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
2985
3456
|
const members = [];
|
|
2986
3457
|
for (const member of tsType.members) {
|
|
2987
|
-
if (
|
|
3458
|
+
if (t30.isTSPropertySignature(member)) {
|
|
2988
3459
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2989
3460
|
if (prop) members.push(prop);
|
|
2990
3461
|
continue;
|
|
2991
3462
|
}
|
|
2992
|
-
if (
|
|
3463
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
2993
3464
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2994
3465
|
continue;
|
|
2995
3466
|
}
|
|
2996
3467
|
}
|
|
2997
3468
|
if (!members.length) return null;
|
|
2998
|
-
return
|
|
3469
|
+
return t30.tsTypeLiteral(members);
|
|
2999
3470
|
}
|
|
3000
|
-
if (
|
|
3471
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
3001
3472
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
3002
3473
|
if (!props.length) return null;
|
|
3003
|
-
return
|
|
3474
|
+
return t30.tsTypeLiteral(props);
|
|
3004
3475
|
}
|
|
3005
3476
|
return tsType;
|
|
3006
3477
|
}
|
|
3007
3478
|
function hasEmitsSignatureInType(tsType) {
|
|
3008
|
-
if (
|
|
3479
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
3009
3480
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
3010
3481
|
}
|
|
3011
|
-
if (
|
|
3482
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
3012
3483
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3013
3484
|
return false;
|
|
3014
3485
|
}
|
|
3015
3486
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
3016
3487
|
}
|
|
3017
|
-
if (
|
|
3488
|
+
if (t30.isTSIntersectionType(tsType) || t30.isTSUnionType(tsType)) {
|
|
3018
3489
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
3019
3490
|
}
|
|
3020
|
-
if (
|
|
3491
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
3021
3492
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
3022
3493
|
}
|
|
3023
|
-
if (
|
|
3494
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
3024
3495
|
return isEmitsCallable(tsType.parameters);
|
|
3025
3496
|
}
|
|
3026
3497
|
return false;
|
|
3027
3498
|
}
|
|
3028
3499
|
function hasEmitsSignatureInMember(member) {
|
|
3029
|
-
if (
|
|
3500
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
3030
3501
|
return isEmitsCallable(member.parameters);
|
|
3031
3502
|
}
|
|
3032
3503
|
return false;
|
|
3033
3504
|
}
|
|
3034
3505
|
function isEmitsCallable(parameters) {
|
|
3035
3506
|
const [eventParam] = parameters;
|
|
3036
|
-
if (!eventParam || !
|
|
3507
|
+
if (!eventParam || !t30.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
3037
3508
|
return false;
|
|
3038
3509
|
}
|
|
3039
3510
|
const { typeAnnotation } = eventParam;
|
|
3040
|
-
if (
|
|
3511
|
+
if (t30.isNoop(typeAnnotation)) return false;
|
|
3041
3512
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
3042
3513
|
}
|
|
3043
3514
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -3047,19 +3518,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
3047
3518
|
const key = buildKey(handlerName);
|
|
3048
3519
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3049
3520
|
let params = [];
|
|
3050
|
-
let returnType =
|
|
3051
|
-
if (typeAnnotation &&
|
|
3521
|
+
let returnType = t30.tsAnyKeyword();
|
|
3522
|
+
if (typeAnnotation && t30.isTSFunctionType(typeAnnotation)) {
|
|
3052
3523
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
3053
3524
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
3054
|
-
} else if (typeAnnotation &&
|
|
3525
|
+
} else if (typeAnnotation && t30.isTSTupleType(typeAnnotation)) {
|
|
3055
3526
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
3056
3527
|
} else if (typeAnnotation) {
|
|
3057
|
-
const id =
|
|
3058
|
-
id.typeAnnotation =
|
|
3528
|
+
const id = t30.identifier("value");
|
|
3529
|
+
id.typeAnnotation = t30.tsTypeAnnotation(typeAnnotation);
|
|
3059
3530
|
params = [id];
|
|
3060
3531
|
}
|
|
3061
|
-
const fnType =
|
|
3062
|
-
const prop =
|
|
3532
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(returnType));
|
|
3533
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
3063
3534
|
prop.optional = !!member.optional;
|
|
3064
3535
|
return prop;
|
|
3065
3536
|
}
|
|
@@ -3068,32 +3539,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
3068
3539
|
}
|
|
3069
3540
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
3070
3541
|
const [eventParam, ...restParams] = parameters;
|
|
3071
|
-
if (!eventParam || !
|
|
3542
|
+
if (!eventParam || !t30.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
3072
3543
|
return [];
|
|
3073
3544
|
}
|
|
3074
3545
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
3075
|
-
if (
|
|
3546
|
+
if (t30.isNoop(paramTypeAnnotation)) return [];
|
|
3076
3547
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
3077
3548
|
if (!eventNames.length) return [];
|
|
3078
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
3549
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t30.tsAnyKeyword();
|
|
3079
3550
|
return eventNames.map((eventName) => {
|
|
3080
3551
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
3081
3552
|
const key = buildKey(handlerName);
|
|
3082
3553
|
const params = cloneCallableParams(restParams);
|
|
3083
|
-
const fnType =
|
|
3084
|
-
const prop =
|
|
3554
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(returnType));
|
|
3555
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
3085
3556
|
prop.optional = true;
|
|
3086
3557
|
return prop;
|
|
3087
3558
|
});
|
|
3088
3559
|
}
|
|
3089
3560
|
function resolveEventNames(type) {
|
|
3090
|
-
if (
|
|
3561
|
+
if (t30.isTSLiteralType(type) && t30.isStringLiteral(type.literal)) {
|
|
3091
3562
|
return [type.literal.value];
|
|
3092
3563
|
}
|
|
3093
|
-
if (
|
|
3564
|
+
if (t30.isTSUnionType(type)) {
|
|
3094
3565
|
return type.types.flatMap(resolveEventNames);
|
|
3095
3566
|
}
|
|
3096
|
-
if (
|
|
3567
|
+
if (t30.isTSParenthesizedType(type)) {
|
|
3097
3568
|
return resolveEventNames(type.typeAnnotation);
|
|
3098
3569
|
}
|
|
3099
3570
|
return [];
|
|
@@ -3106,44 +3577,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
3106
3577
|
return params;
|
|
3107
3578
|
}
|
|
3108
3579
|
function resolveTupleElementParam(element, index) {
|
|
3109
|
-
const isNamedTuple = typeof
|
|
3580
|
+
const isNamedTuple = typeof t30.isTSNamedTupleMember === "function" && t30.isTSNamedTupleMember(element);
|
|
3110
3581
|
if (isNamedTuple) {
|
|
3111
3582
|
const tupleMember = element;
|
|
3112
3583
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
3113
3584
|
let innerType = tupleMember.elementType;
|
|
3114
3585
|
let optional = tupleMember.optional;
|
|
3115
|
-
if (
|
|
3586
|
+
if (t30.isTSOptionalType(innerType)) {
|
|
3116
3587
|
optional = true;
|
|
3117
3588
|
innerType = innerType.typeAnnotation;
|
|
3118
3589
|
}
|
|
3119
|
-
if (
|
|
3120
|
-
const rest =
|
|
3121
|
-
rest.typeAnnotation =
|
|
3590
|
+
if (t30.isTSRestType(innerType)) {
|
|
3591
|
+
const rest = t30.restElement(t30.identifier(name));
|
|
3592
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(innerType.typeAnnotation);
|
|
3122
3593
|
return rest;
|
|
3123
3594
|
}
|
|
3124
|
-
const id2 =
|
|
3595
|
+
const id2 = t30.identifier(name);
|
|
3125
3596
|
id2.optional = optional;
|
|
3126
|
-
id2.typeAnnotation =
|
|
3597
|
+
id2.typeAnnotation = t30.tsTypeAnnotation(innerType);
|
|
3127
3598
|
return id2;
|
|
3128
3599
|
}
|
|
3129
|
-
if (
|
|
3130
|
-
const rest =
|
|
3131
|
-
rest.typeAnnotation =
|
|
3600
|
+
if (t30.isTSRestType(element)) {
|
|
3601
|
+
const rest = t30.restElement(t30.identifier(`args${index}`));
|
|
3602
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
3132
3603
|
return rest;
|
|
3133
3604
|
}
|
|
3134
|
-
if (
|
|
3135
|
-
const id2 =
|
|
3605
|
+
if (t30.isTSOptionalType(element)) {
|
|
3606
|
+
const id2 = t30.identifier(`arg${index}`);
|
|
3136
3607
|
id2.optional = true;
|
|
3137
|
-
id2.typeAnnotation =
|
|
3608
|
+
id2.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
3138
3609
|
return id2;
|
|
3139
3610
|
}
|
|
3140
|
-
const id =
|
|
3141
|
-
id.typeAnnotation =
|
|
3611
|
+
const id = t30.identifier(`arg${index}`);
|
|
3612
|
+
id.typeAnnotation = t30.tsTypeAnnotation(element);
|
|
3142
3613
|
return id;
|
|
3143
3614
|
}
|
|
3144
3615
|
|
|
3145
3616
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
3146
|
-
import * as
|
|
3617
|
+
import * as t31 from "@babel/types";
|
|
3147
3618
|
function resolveDefinePropsIface(path9, ctx) {
|
|
3148
3619
|
const { node } = path9;
|
|
3149
3620
|
const [runtimeArg] = node.arguments;
|
|
@@ -3163,38 +3634,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3163
3634
|
} = scriptData;
|
|
3164
3635
|
if (!runtimeArg) return;
|
|
3165
3636
|
const members = [];
|
|
3166
|
-
if (
|
|
3637
|
+
if (t31.isArrayExpression(runtimeArg)) {
|
|
3167
3638
|
for (const element of runtimeArg.elements) {
|
|
3168
|
-
if (!element || !
|
|
3169
|
-
const key =
|
|
3170
|
-
const prop =
|
|
3639
|
+
if (!element || !t31.isStringLiteral(element)) continue;
|
|
3640
|
+
const key = t31.isValidIdentifier(element.value) ? t31.identifier(element.value) : t31.stringLiteral(element.value);
|
|
3641
|
+
const prop = t31.tsPropertySignature(key, t31.tsTypeAnnotation(t31.tsAnyKeyword()));
|
|
3171
3642
|
prop.optional = true;
|
|
3172
3643
|
members.push(prop);
|
|
3173
3644
|
}
|
|
3174
3645
|
if (members.length) {
|
|
3175
|
-
propsTypes.push(
|
|
3646
|
+
propsTypes.push(t31.tsTypeLiteral(members));
|
|
3176
3647
|
}
|
|
3177
3648
|
return;
|
|
3178
3649
|
}
|
|
3179
|
-
if (
|
|
3650
|
+
if (t31.isObjectExpression(runtimeArg)) {
|
|
3180
3651
|
for (const prop of runtimeArg.properties) {
|
|
3181
|
-
if (!
|
|
3182
|
-
if (
|
|
3652
|
+
if (!t31.isObjectProperty(prop)) continue;
|
|
3653
|
+
if (t31.isSpreadElement(prop)) continue;
|
|
3183
3654
|
const key = prop.key;
|
|
3184
3655
|
let propName = null;
|
|
3185
|
-
if (
|
|
3186
|
-
if (
|
|
3187
|
-
if (
|
|
3656
|
+
if (t31.isIdentifier(key)) propName = key.name;
|
|
3657
|
+
if (t31.isStringLiteral(key)) propName = key.value;
|
|
3658
|
+
if (t31.isNumericLiteral(key)) propName = String(key.value);
|
|
3188
3659
|
if (!propName) continue;
|
|
3189
3660
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
3190
|
-
const tsType = type ??
|
|
3191
|
-
const tsKey =
|
|
3192
|
-
const tsProp =
|
|
3661
|
+
const tsType = type ?? t31.tsAnyKeyword();
|
|
3662
|
+
const tsKey = t31.isValidIdentifier(propName) ? t31.identifier(propName) : t31.stringLiteral(propName);
|
|
3663
|
+
const tsProp = t31.tsPropertySignature(tsKey, t31.tsTypeAnnotation(tsType));
|
|
3193
3664
|
tsProp.optional = !required;
|
|
3194
3665
|
members.push(tsProp);
|
|
3195
3666
|
}
|
|
3196
3667
|
if (members.length) {
|
|
3197
|
-
propsTypes.push(
|
|
3668
|
+
propsTypes.push(t31.tsTypeLiteral(members));
|
|
3198
3669
|
}
|
|
3199
3670
|
return;
|
|
3200
3671
|
}
|
|
@@ -3208,42 +3679,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3208
3679
|
);
|
|
3209
3680
|
}
|
|
3210
3681
|
function resolveRuntimePropMeta(value) {
|
|
3211
|
-
if (
|
|
3682
|
+
if (t31.isIdentifier(value)) {
|
|
3212
3683
|
return {
|
|
3213
3684
|
type: mapRuntimeTypeToTSType(value),
|
|
3214
3685
|
required: false
|
|
3215
3686
|
};
|
|
3216
3687
|
}
|
|
3217
|
-
if (
|
|
3688
|
+
if (t31.isArrayExpression(value)) {
|
|
3218
3689
|
return {
|
|
3219
3690
|
type: resolveRuntimeUnionType(value),
|
|
3220
3691
|
required: false
|
|
3221
3692
|
};
|
|
3222
3693
|
}
|
|
3223
|
-
if (!
|
|
3694
|
+
if (!t31.isObjectExpression(value)) {
|
|
3224
3695
|
return { required: false };
|
|
3225
3696
|
}
|
|
3226
3697
|
let type;
|
|
3227
3698
|
let required = false;
|
|
3228
3699
|
for (const prop of value.properties) {
|
|
3229
|
-
if (!
|
|
3230
|
-
if (
|
|
3700
|
+
if (!t31.isObjectProperty(prop)) continue;
|
|
3701
|
+
if (t31.isSpreadElement(prop)) continue;
|
|
3231
3702
|
const key = prop.key;
|
|
3232
|
-
const propName =
|
|
3703
|
+
const propName = t31.isIdentifier(key) ? key.name : t31.isStringLiteral(key) ? key.value : null;
|
|
3233
3704
|
if (!propName) continue;
|
|
3234
3705
|
if (propName === "type") {
|
|
3235
3706
|
const valueNode = prop.value;
|
|
3236
|
-
if (
|
|
3707
|
+
if (t31.isArrayExpression(valueNode)) {
|
|
3237
3708
|
type = resolveRuntimeUnionType(valueNode);
|
|
3238
3709
|
continue;
|
|
3239
3710
|
}
|
|
3240
|
-
if (
|
|
3711
|
+
if (t31.isIdentifier(valueNode)) {
|
|
3241
3712
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
3242
3713
|
continue;
|
|
3243
3714
|
}
|
|
3244
3715
|
}
|
|
3245
3716
|
if (propName === "required") {
|
|
3246
|
-
if (
|
|
3717
|
+
if (t31.isBooleanLiteral(prop.value)) {
|
|
3247
3718
|
required = prop.value.value;
|
|
3248
3719
|
}
|
|
3249
3720
|
}
|
|
@@ -3253,257 +3724,403 @@ function resolveRuntimePropMeta(value) {
|
|
|
3253
3724
|
function resolveRuntimeUnionType(value) {
|
|
3254
3725
|
const types = [];
|
|
3255
3726
|
for (const element of value.elements) {
|
|
3256
|
-
if (!element || !
|
|
3727
|
+
if (!element || !t31.isIdentifier(element)) continue;
|
|
3257
3728
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
3258
3729
|
if (resolved) types.push(resolved);
|
|
3259
3730
|
}
|
|
3260
|
-
if (!types.length) return
|
|
3731
|
+
if (!types.length) return t31.tsAnyKeyword();
|
|
3261
3732
|
if (types.length === 1) return types[0];
|
|
3262
|
-
return
|
|
3733
|
+
return t31.tsUnionType(types);
|
|
3263
3734
|
}
|
|
3264
3735
|
function mapRuntimeTypeToTSType(value) {
|
|
3265
3736
|
switch (value.name) {
|
|
3266
3737
|
case "String":
|
|
3267
|
-
return
|
|
3738
|
+
return t31.tsStringKeyword();
|
|
3268
3739
|
case "Number":
|
|
3269
|
-
return
|
|
3740
|
+
return t31.tsNumberKeyword();
|
|
3270
3741
|
case "Boolean":
|
|
3271
|
-
return
|
|
3742
|
+
return t31.tsBooleanKeyword();
|
|
3272
3743
|
case "Object":
|
|
3273
|
-
return
|
|
3744
|
+
return t31.tsTypeLiteral([]);
|
|
3274
3745
|
case "Array":
|
|
3275
|
-
return
|
|
3746
|
+
return t31.tsArrayType(t31.tsAnyKeyword());
|
|
3276
3747
|
case "Function":
|
|
3277
|
-
return
|
|
3748
|
+
return t31.tsFunctionType(null, [], t31.tsTypeAnnotation(t31.tsAnyKeyword()));
|
|
3278
3749
|
case "Symbol":
|
|
3279
|
-
return
|
|
3750
|
+
return t31.tsSymbolKeyword();
|
|
3280
3751
|
case "BigInt":
|
|
3281
|
-
return
|
|
3752
|
+
return t31.tsBigIntKeyword();
|
|
3282
3753
|
default:
|
|
3283
|
-
return
|
|
3754
|
+
return t31.tsAnyKeyword();
|
|
3284
3755
|
}
|
|
3285
3756
|
}
|
|
3286
3757
|
|
|
3287
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
3288
|
-
import * as
|
|
3758
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
3759
|
+
import * as t34 from "@babel/types";
|
|
3760
|
+
|
|
3761
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/slot-builder.ts
|
|
3762
|
+
import * as t32 from "@babel/types";
|
|
3289
3763
|
var SLOT_DEFAULT_NAME = "default";
|
|
3290
3764
|
var SLOT_CHILDREN_NAME = "children";
|
|
3291
3765
|
var SLOT_FN_PARAM_NAME = "props";
|
|
3292
|
-
function
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
if (resolved && t29.isTSTypeLiteral(resolved)) {
|
|
3305
|
-
node.body.body = resolved.members;
|
|
3306
|
-
recordReactNode(ctx);
|
|
3307
|
-
}
|
|
3308
|
-
return;
|
|
3309
|
-
}
|
|
3310
|
-
if (t29.isTSTypeAliasDeclaration(node)) {
|
|
3311
|
-
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
3312
|
-
const resolved = resolveSlotType(node.typeAnnotation);
|
|
3313
|
-
if (resolved) {
|
|
3314
|
-
node.typeAnnotation = resolved;
|
|
3315
|
-
recordReactNode(ctx);
|
|
3316
|
-
}
|
|
3317
|
-
}
|
|
3318
|
-
}
|
|
3319
|
-
};
|
|
3320
|
-
}
|
|
3321
|
-
function resolveDefineSlotsIface(path9, ctx) {
|
|
3322
|
-
const { node } = path9;
|
|
3323
|
-
const tsParams = node.typeParameters?.params;
|
|
3324
|
-
if (!tsParams?.length) return;
|
|
3325
|
-
const {
|
|
3326
|
-
propsTSIface: { slotTypes }
|
|
3327
|
-
} = ctx.scriptData;
|
|
3328
|
-
for (const tsType of tsParams) {
|
|
3329
|
-
const resolved = resolveSlotType(tsType);
|
|
3330
|
-
if (resolved) {
|
|
3331
|
-
slotTypes.push(resolved);
|
|
3332
|
-
}
|
|
3333
|
-
}
|
|
3334
|
-
if (slotTypes.length) {
|
|
3335
|
-
recordReactNode(ctx);
|
|
3766
|
+
function buildSlotPropSignature(rawName, params, optional) {
|
|
3767
|
+
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
3768
|
+
const key = t32.isValidIdentifier(propName) ? t32.identifier(propName) : t32.stringLiteral(propName);
|
|
3769
|
+
const reactNodeType = t32.tsTypeAnnotation(
|
|
3770
|
+
t32.tsTypeReference(t32.identifier(REACT_API_MAP.ReactNode))
|
|
3771
|
+
);
|
|
3772
|
+
let typeAnnotation;
|
|
3773
|
+
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
3774
|
+
typeAnnotation = reactNodeType;
|
|
3775
|
+
} else {
|
|
3776
|
+
const fnType = t32.tsFunctionType(null, params, reactNodeType);
|
|
3777
|
+
typeAnnotation = t32.tsTypeAnnotation(fnType);
|
|
3336
3778
|
}
|
|
3779
|
+
const prop = t32.tsPropertySignature(key, typeAnnotation);
|
|
3780
|
+
prop.optional = optional;
|
|
3781
|
+
return prop;
|
|
3337
3782
|
}
|
|
3338
|
-
function
|
|
3339
|
-
|
|
3340
|
-
const
|
|
3341
|
-
|
|
3342
|
-
}
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
const
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
}
|
|
3353
|
-
if (tsMembers.length) {
|
|
3354
|
-
recordReactNode(ctx);
|
|
3355
|
-
slotTypes.push(t29.tsTypeLiteral(tsMembers));
|
|
3356
|
-
}
|
|
3783
|
+
function createSlotScopeParam(props, ctx) {
|
|
3784
|
+
const paramId = t32.identifier(SLOT_FN_PARAM_NAME);
|
|
3785
|
+
const propsSigns = [];
|
|
3786
|
+
const { reactiveBindings } = ctx.templateData;
|
|
3787
|
+
props.forEach(({ prop, tsType }) => {
|
|
3788
|
+
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3789
|
+
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3790
|
+
const typeAnnotation = foundBindingTypes ? t32.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3791
|
+
const key = t32.isValidIdentifier(prop) ? t32.identifier(prop) : t32.stringLiteral(prop);
|
|
3792
|
+
const propSign = t32.tsPropertySignature(key, typeAnnotation);
|
|
3793
|
+
propsSigns.push(propSign);
|
|
3794
|
+
});
|
|
3795
|
+
paramId.typeAnnotation = t32.tsTypeAnnotation(t32.tsTypeLiteral(propsSigns));
|
|
3796
|
+
return paramId;
|
|
3357
3797
|
}
|
|
3798
|
+
|
|
3799
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/utils.ts
|
|
3800
|
+
import * as t33 from "@babel/types";
|
|
3358
3801
|
function recordReactNode(ctx) {
|
|
3359
3802
|
if (!ctx.scriptData.lang.startsWith("ts")) {
|
|
3360
3803
|
return;
|
|
3361
3804
|
}
|
|
3362
3805
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
3363
3806
|
}
|
|
3364
|
-
function
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3807
|
+
function collectLocalTypeDeclarations(path9) {
|
|
3808
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
3809
|
+
const programPath = path9.findParent(
|
|
3810
|
+
(parentPath) => parentPath.isProgram()
|
|
3811
|
+
);
|
|
3812
|
+
if (!programPath) {
|
|
3813
|
+
return declarations;
|
|
3814
|
+
}
|
|
3815
|
+
for (const statement of programPath.node.body) {
|
|
3816
|
+
if (t33.isTSInterfaceDeclaration(statement)) {
|
|
3817
|
+
declarations.set(statement.id.name, {
|
|
3818
|
+
type: t33.tsTypeLiteral(statement.body.body),
|
|
3819
|
+
// 将接口体转换为类型字面量
|
|
3820
|
+
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
3821
|
+
// 检查是否有泛型参数
|
|
3822
|
+
});
|
|
3823
|
+
continue;
|
|
3371
3824
|
}
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
if (
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3825
|
+
if (t33.isTSTypeAliasDeclaration(statement)) {
|
|
3826
|
+
declarations.set(statement.id.name, {
|
|
3827
|
+
type: statement.typeAnnotation,
|
|
3828
|
+
// 直接使用类型注解
|
|
3829
|
+
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
3830
|
+
// 检查是否有泛型参数
|
|
3831
|
+
});
|
|
3832
|
+
continue;
|
|
3833
|
+
}
|
|
3834
|
+
if (t33.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
3835
|
+
const declaration = statement.declaration;
|
|
3836
|
+
if (t33.isTSInterfaceDeclaration(declaration)) {
|
|
3837
|
+
declarations.set(declaration.id.name, {
|
|
3838
|
+
type: t33.tsTypeLiteral(declaration.body.body),
|
|
3839
|
+
// 将接口体转换为类型字面量
|
|
3840
|
+
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
3841
|
+
// 检查是否有泛型参数
|
|
3842
|
+
});
|
|
3843
|
+
} else if (t33.isTSTypeAliasDeclaration(declaration)) {
|
|
3844
|
+
declarations.set(declaration.id.name, {
|
|
3845
|
+
type: declaration.typeAnnotation,
|
|
3846
|
+
// 直接使用类型注解
|
|
3847
|
+
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
3848
|
+
// 检查是否有泛型参数
|
|
3849
|
+
});
|
|
3397
3850
|
}
|
|
3398
|
-
members.push(member);
|
|
3399
3851
|
}
|
|
3400
|
-
if (!members.length) return null;
|
|
3401
|
-
return t29.tsTypeLiteral(members);
|
|
3402
3852
|
}
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
);
|
|
3409
|
-
return t29.tsTypeLiteral([props]);
|
|
3853
|
+
return declarations;
|
|
3854
|
+
}
|
|
3855
|
+
function resolvePropName2(key) {
|
|
3856
|
+
if (t33.isIdentifier(key)) {
|
|
3857
|
+
return key.name;
|
|
3410
3858
|
}
|
|
3411
|
-
|
|
3859
|
+
if (t33.isStringLiteral(key)) {
|
|
3860
|
+
return key.value;
|
|
3861
|
+
}
|
|
3862
|
+
if (t33.isNumericLiteral(key)) {
|
|
3863
|
+
return String(key.value);
|
|
3864
|
+
}
|
|
3865
|
+
return null;
|
|
3412
3866
|
}
|
|
3413
|
-
function
|
|
3414
|
-
if (
|
|
3415
|
-
return
|
|
3867
|
+
function resolveCallableType(tsType) {
|
|
3868
|
+
if (t33.isTSFunctionType(tsType)) {
|
|
3869
|
+
return tsType;
|
|
3416
3870
|
}
|
|
3417
|
-
if (
|
|
3418
|
-
|
|
3419
|
-
|
|
3871
|
+
if (t33.isTSParenthesizedType(tsType)) {
|
|
3872
|
+
return resolveCallableType(tsType.typeAnnotation);
|
|
3873
|
+
}
|
|
3874
|
+
return null;
|
|
3875
|
+
}
|
|
3876
|
+
|
|
3877
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
3878
|
+
var SLOT_DEFAULT_NAME2 = "default";
|
|
3879
|
+
function resolveSlotType(tsType, options) {
|
|
3880
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
3881
|
+
return resolveSlotType(tsType.typeAnnotation, options);
|
|
3882
|
+
}
|
|
3883
|
+
if (t34.isTSTypeReference(tsType)) {
|
|
3884
|
+
let shouldRecordReactNode = false;
|
|
3885
|
+
if (tsType.typeParameters?.params.length) {
|
|
3886
|
+
const params = [];
|
|
3887
|
+
for (const param of tsType.typeParameters.params) {
|
|
3888
|
+
const resolved2 = resolveSlotType(param, options);
|
|
3889
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved2.shouldRecordReactNode;
|
|
3890
|
+
params.push(resolved2.type || param);
|
|
3891
|
+
}
|
|
3892
|
+
if (!shouldRecordReactNode) {
|
|
3893
|
+
return {
|
|
3894
|
+
type: tsType,
|
|
3895
|
+
shouldRecordReactNode: false
|
|
3896
|
+
};
|
|
3897
|
+
}
|
|
3898
|
+
return {
|
|
3899
|
+
type: t34.tsTypeReference(tsType.typeName, t34.tsTypeParameterInstantiation(params)),
|
|
3900
|
+
shouldRecordReactNode
|
|
3901
|
+
};
|
|
3902
|
+
}
|
|
3903
|
+
if (!t34.isIdentifier(tsType.typeName)) {
|
|
3904
|
+
return {
|
|
3905
|
+
type: tsType,
|
|
3906
|
+
shouldRecordReactNode: false
|
|
3907
|
+
};
|
|
3908
|
+
}
|
|
3909
|
+
const typeName = tsType.typeName.name;
|
|
3910
|
+
const localDeclaration = options.localTypeDeclarations.get(typeName);
|
|
3911
|
+
if (!localDeclaration || localDeclaration.hasTypeParameters) {
|
|
3912
|
+
return {
|
|
3913
|
+
type: tsType,
|
|
3914
|
+
shouldRecordReactNode: false
|
|
3915
|
+
};
|
|
3916
|
+
}
|
|
3917
|
+
if (options.visitedTypeNames.has(typeName)) {
|
|
3918
|
+
return {
|
|
3919
|
+
type: tsType,
|
|
3920
|
+
shouldRecordReactNode: false
|
|
3921
|
+
};
|
|
3922
|
+
}
|
|
3923
|
+
options.visitedTypeNames.add(typeName);
|
|
3924
|
+
const resolved = resolveSlotType(localDeclaration.type, options);
|
|
3925
|
+
options.visitedTypeNames.delete(typeName);
|
|
3926
|
+
if (!resolved.shouldRecordReactNode) {
|
|
3927
|
+
return {
|
|
3928
|
+
type: tsType,
|
|
3929
|
+
shouldRecordReactNode: false
|
|
3930
|
+
};
|
|
3420
3931
|
}
|
|
3421
|
-
return
|
|
3932
|
+
return resolved;
|
|
3422
3933
|
}
|
|
3423
|
-
if (
|
|
3424
|
-
|
|
3934
|
+
if (t34.isTSIntersectionType(tsType)) {
|
|
3935
|
+
const types = [];
|
|
3936
|
+
let shouldRecordReactNode = false;
|
|
3937
|
+
for (const item of tsType.types) {
|
|
3938
|
+
const resolved = resolveSlotType(item, options);
|
|
3939
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3940
|
+
if (resolved.type) {
|
|
3941
|
+
types.push(resolved.type);
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
if (!types.length) {
|
|
3945
|
+
return {
|
|
3946
|
+
type: null,
|
|
3947
|
+
shouldRecordReactNode
|
|
3948
|
+
};
|
|
3949
|
+
}
|
|
3950
|
+
if (types.length === 1) {
|
|
3951
|
+
return {
|
|
3952
|
+
type: types[0],
|
|
3953
|
+
shouldRecordReactNode
|
|
3954
|
+
};
|
|
3955
|
+
}
|
|
3956
|
+
return {
|
|
3957
|
+
type: t34.tsIntersectionType(types),
|
|
3958
|
+
shouldRecordReactNode
|
|
3959
|
+
};
|
|
3425
3960
|
}
|
|
3426
|
-
if (
|
|
3427
|
-
|
|
3961
|
+
if (t34.isTSUnionType(tsType)) {
|
|
3962
|
+
const types = [];
|
|
3963
|
+
let shouldRecordReactNode = false;
|
|
3964
|
+
for (const item of tsType.types) {
|
|
3965
|
+
const resolved = resolveSlotType(item, options);
|
|
3966
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3967
|
+
if (resolved.type) {
|
|
3968
|
+
types.push(resolved.type);
|
|
3969
|
+
}
|
|
3970
|
+
}
|
|
3971
|
+
if (!types.length) {
|
|
3972
|
+
return {
|
|
3973
|
+
type: null,
|
|
3974
|
+
shouldRecordReactNode
|
|
3975
|
+
};
|
|
3976
|
+
}
|
|
3977
|
+
if (types.length === 1) {
|
|
3978
|
+
return {
|
|
3979
|
+
type: types[0],
|
|
3980
|
+
shouldRecordReactNode
|
|
3981
|
+
};
|
|
3982
|
+
}
|
|
3983
|
+
return {
|
|
3984
|
+
type: t34.tsUnionType(types),
|
|
3985
|
+
shouldRecordReactNode
|
|
3986
|
+
};
|
|
3428
3987
|
}
|
|
3429
|
-
if (
|
|
3430
|
-
|
|
3988
|
+
if (t34.isTSTypeLiteral(tsType)) {
|
|
3989
|
+
const members = [];
|
|
3990
|
+
let shouldRecordReactNode = false;
|
|
3991
|
+
for (const item of tsType.members) {
|
|
3992
|
+
const resolved = resolveSlotPropFromMember(item);
|
|
3993
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3994
|
+
members.push(resolved.member || item);
|
|
3995
|
+
}
|
|
3996
|
+
if (!members.length) {
|
|
3997
|
+
return {
|
|
3998
|
+
type: null,
|
|
3999
|
+
shouldRecordReactNode
|
|
4000
|
+
};
|
|
4001
|
+
}
|
|
4002
|
+
return {
|
|
4003
|
+
type: t34.tsTypeLiteral(members),
|
|
4004
|
+
shouldRecordReactNode
|
|
4005
|
+
};
|
|
3431
4006
|
}
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
return
|
|
4007
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
4008
|
+
const props = buildSlotPropSignature(
|
|
4009
|
+
SLOT_DEFAULT_NAME2,
|
|
4010
|
+
cloneCallableParams(tsType.parameters),
|
|
4011
|
+
false
|
|
4012
|
+
// 默认插槽不是可选的
|
|
4013
|
+
);
|
|
4014
|
+
return {
|
|
4015
|
+
type: t34.tsTypeLiteral([props]),
|
|
4016
|
+
shouldRecordReactNode: true
|
|
4017
|
+
// 函数类型总是需要记录 ReactNode
|
|
4018
|
+
};
|
|
3440
4019
|
}
|
|
3441
|
-
return
|
|
4020
|
+
return {
|
|
4021
|
+
type: tsType,
|
|
4022
|
+
shouldRecordReactNode: false
|
|
4023
|
+
};
|
|
3442
4024
|
}
|
|
3443
4025
|
function resolveSlotPropFromMember(member) {
|
|
3444
|
-
if (
|
|
4026
|
+
if (t34.isTSMethodSignature(member)) {
|
|
3445
4027
|
const rawName = resolvePropName2(member.key);
|
|
3446
|
-
if (!rawName)
|
|
4028
|
+
if (!rawName) {
|
|
4029
|
+
return {
|
|
4030
|
+
member: null,
|
|
4031
|
+
shouldRecordReactNode: false
|
|
4032
|
+
};
|
|
4033
|
+
}
|
|
3447
4034
|
const params = cloneCallableParams(member.parameters);
|
|
3448
|
-
return
|
|
4035
|
+
return {
|
|
4036
|
+
member: buildSlotPropSignature(rawName, params, !!member.optional),
|
|
4037
|
+
shouldRecordReactNode: true
|
|
4038
|
+
// 方法签名总是可调用,需要记录 ReactNode
|
|
4039
|
+
};
|
|
3449
4040
|
}
|
|
3450
|
-
if (
|
|
4041
|
+
if (t34.isTSPropertySignature(member)) {
|
|
3451
4042
|
const rawName = resolvePropName2(member.key);
|
|
3452
|
-
if (!rawName)
|
|
4043
|
+
if (!rawName) {
|
|
4044
|
+
return {
|
|
4045
|
+
member: null,
|
|
4046
|
+
shouldRecordReactNode: false
|
|
4047
|
+
};
|
|
4048
|
+
}
|
|
3453
4049
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3454
4050
|
const callable = typeAnnotation ? resolveCallableType(typeAnnotation) : null;
|
|
3455
|
-
if (!callable)
|
|
4051
|
+
if (!callable) {
|
|
4052
|
+
return {
|
|
4053
|
+
member: null,
|
|
4054
|
+
shouldRecordReactNode: false
|
|
4055
|
+
};
|
|
4056
|
+
}
|
|
3456
4057
|
const params = cloneCallableParams(callable.parameters);
|
|
3457
|
-
return
|
|
4058
|
+
return {
|
|
4059
|
+
member: buildSlotPropSignature(rawName, params, !!member.optional),
|
|
4060
|
+
shouldRecordReactNode: true
|
|
4061
|
+
// 可调用属性需要记录 ReactNode
|
|
4062
|
+
};
|
|
3458
4063
|
}
|
|
3459
|
-
if (
|
|
4064
|
+
if (t34.isTSCallSignatureDeclaration(member)) {
|
|
3460
4065
|
const params = cloneCallableParams(member.parameters);
|
|
3461
|
-
return
|
|
4066
|
+
return {
|
|
4067
|
+
member: buildSlotPropSignature(SLOT_DEFAULT_NAME2, params, true),
|
|
4068
|
+
shouldRecordReactNode: true
|
|
4069
|
+
// 调用签名总是可调用,需要记录 ReactNode
|
|
4070
|
+
};
|
|
3462
4071
|
}
|
|
3463
|
-
return
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
if (t29.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
3468
|
-
return null;
|
|
4072
|
+
return {
|
|
4073
|
+
member: null,
|
|
4074
|
+
shouldRecordReactNode: false
|
|
4075
|
+
};
|
|
3469
4076
|
}
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
const
|
|
3474
|
-
|
|
3475
|
-
);
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
4077
|
+
|
|
4078
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/define-slots.ts
|
|
4079
|
+
function resolveDefineSlotsIface(path9, ctx) {
|
|
4080
|
+
const { node } = path9;
|
|
4081
|
+
const tsParams = node.typeParameters?.params;
|
|
4082
|
+
if (!tsParams?.length) return;
|
|
4083
|
+
const {
|
|
4084
|
+
propsTSIface: { slotTypes }
|
|
4085
|
+
} = ctx.scriptData;
|
|
4086
|
+
const localTypeDeclarations = collectLocalTypeDeclarations(path9);
|
|
4087
|
+
let shouldRecordReactNode = false;
|
|
4088
|
+
for (const tsType of tsParams) {
|
|
4089
|
+
const { type: resolved, shouldRecordReactNode: needsReactNode } = resolveSlotType(tsType, {
|
|
4090
|
+
localTypeDeclarations,
|
|
4091
|
+
visitedTypeNames: /* @__PURE__ */ new Set()
|
|
4092
|
+
});
|
|
4093
|
+
if (resolved) {
|
|
4094
|
+
slotTypes.push(resolved);
|
|
4095
|
+
shouldRecordReactNode = shouldRecordReactNode || needsReactNode;
|
|
4096
|
+
}
|
|
4097
|
+
}
|
|
4098
|
+
if (shouldRecordReactNode) {
|
|
4099
|
+
recordReactNode(ctx);
|
|
3482
4100
|
}
|
|
3483
|
-
const prop = t29.tsPropertySignature(key, typeAnnotation);
|
|
3484
|
-
prop.optional = optional;
|
|
3485
|
-
return prop;
|
|
3486
|
-
}
|
|
3487
|
-
function createSlotScopeParam(props, ctx) {
|
|
3488
|
-
const paramId = t29.identifier(SLOT_FN_PARAM_NAME);
|
|
3489
|
-
const propsSigns = [];
|
|
3490
|
-
const { reactiveBindings } = ctx.templateData;
|
|
3491
|
-
props.forEach(({ prop, tsType }) => {
|
|
3492
|
-
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3493
|
-
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3494
|
-
const typeAnnotation = foundBindingTypes ? t29.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3495
|
-
const key = t29.isValidIdentifier(prop) ? t29.identifier(prop) : t29.stringLiteral(prop);
|
|
3496
|
-
const propSign = t29.tsPropertySignature(key, typeAnnotation);
|
|
3497
|
-
propsSigns.push(propSign);
|
|
3498
|
-
});
|
|
3499
|
-
paramId.typeAnnotation = t29.tsTypeAnnotation(t29.tsTypeLiteral(propsSigns));
|
|
3500
|
-
return paramId;
|
|
3501
4101
|
}
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
|
|
3505
|
-
|
|
3506
|
-
return
|
|
4102
|
+
|
|
4103
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/template-slots.ts
|
|
4104
|
+
import * as t35 from "@babel/types";
|
|
4105
|
+
function resolveTemplateSlotIface(ctx) {
|
|
4106
|
+
if (ctx.inputType !== "sfc") return;
|
|
4107
|
+
const {
|
|
4108
|
+
propsTSIface: { slotTypes }
|
|
4109
|
+
} = ctx.scriptData;
|
|
4110
|
+
if (slotTypes.length) return;
|
|
4111
|
+
const { slots } = ctx.templateData;
|
|
4112
|
+
const tsMembers = [];
|
|
4113
|
+
for (const name in slots) {
|
|
4114
|
+
const slotObj = slots[name];
|
|
4115
|
+
if (!slotObj) continue;
|
|
4116
|
+
const params = slotObj.isScope ? [createSlotScopeParam(slotObj.props, ctx)] : [];
|
|
4117
|
+
const tsNode = buildSlotPropSignature(slotObj.name, params, true);
|
|
4118
|
+
tsMembers.push(tsNode);
|
|
4119
|
+
}
|
|
4120
|
+
if (tsMembers.length) {
|
|
4121
|
+
recordReactNode(ctx);
|
|
4122
|
+
slotTypes.push(t35.tsTypeLiteral(tsMembers));
|
|
4123
|
+
}
|
|
3507
4124
|
}
|
|
3508
4125
|
|
|
3509
4126
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
@@ -3546,35 +4163,79 @@ function resolvePropsIface(ctx) {
|
|
|
3546
4163
|
};
|
|
3547
4164
|
}
|
|
3548
4165
|
function resolveCompIProps(ctx, ast) {
|
|
3549
|
-
const { propsTSIface, lang } = ctx.scriptData;
|
|
4166
|
+
const { declaredOptions, propsTSIface, lang } = ctx.scriptData;
|
|
3550
4167
|
const { propsTypes, emitTypes, slotTypes } = propsTSIface;
|
|
3551
4168
|
const tsTypes = [...propsTypes, ...emitTypes, ...slotTypes];
|
|
3552
4169
|
if (ctx.inputType !== "sfc" || !lang.startsWith("ts") || !tsTypes.length) {
|
|
3553
4170
|
return;
|
|
3554
4171
|
}
|
|
3555
|
-
const n =
|
|
4172
|
+
const n = declaredOptions.name || "Comp";
|
|
3556
4173
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3557
|
-
const typeNode =
|
|
3558
|
-
const typeAliasDecl =
|
|
3559
|
-
const exportDecl =
|
|
4174
|
+
const typeNode = t36.tsIntersectionType(tsTypes);
|
|
4175
|
+
const typeAliasDecl = t36.tsTypeAliasDeclaration(t36.identifier(ns), null, typeNode);
|
|
4176
|
+
const exportDecl = t36.exportNamedDeclaration(typeAliasDecl);
|
|
3560
4177
|
propsTSIface.name = ns;
|
|
3561
4178
|
const scriptIR = getScriptIR(ctx);
|
|
3562
4179
|
scriptIR.exports.push(exportDecl);
|
|
3563
4180
|
void ast;
|
|
3564
4181
|
}
|
|
3565
4182
|
|
|
4183
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-use-attrs.ts
|
|
4184
|
+
import * as t37 from "@babel/types";
|
|
4185
|
+
function resolveUseAttrs(ctx) {
|
|
4186
|
+
return {
|
|
4187
|
+
VariableDeclarator(path9) {
|
|
4188
|
+
const { init, id } = path9.node;
|
|
4189
|
+
if (!init) return;
|
|
4190
|
+
const initPath = path9.get("init");
|
|
4191
|
+
const propsIdentifier = t37.identifier(ctx.propField);
|
|
4192
|
+
if (t37.isTSAsExpression(init) && isUseAttrsCall(init.expression)) {
|
|
4193
|
+
const typeAssertion = createPropsTypeAssertion(propsIdentifier, init.typeAnnotation);
|
|
4194
|
+
replaceNode(initPath, typeAssertion, init);
|
|
4195
|
+
return;
|
|
4196
|
+
}
|
|
4197
|
+
if (!isUseAttrsCall(init)) {
|
|
4198
|
+
return;
|
|
4199
|
+
}
|
|
4200
|
+
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
4201
|
+
if (isTS) {
|
|
4202
|
+
let typeAnnotation = null;
|
|
4203
|
+
if (t37.isIdentifier(id) && t37.isTSTypeAnnotation(id.typeAnnotation)) {
|
|
4204
|
+
typeAnnotation = id.typeAnnotation.typeAnnotation;
|
|
4205
|
+
id.typeAnnotation = null;
|
|
4206
|
+
} else {
|
|
4207
|
+
typeAnnotation = t37.tsTypeReference(
|
|
4208
|
+
t37.identifier("Record"),
|
|
4209
|
+
t37.tsTypeParameterInstantiation([t37.tsStringKeyword(), t37.tsUnknownKeyword()])
|
|
4210
|
+
);
|
|
4211
|
+
}
|
|
4212
|
+
const propsTypeAssertion = createPropsTypeAssertion(propsIdentifier, typeAnnotation);
|
|
4213
|
+
replaceNode(initPath, propsTypeAssertion, propsIdentifier);
|
|
4214
|
+
return;
|
|
4215
|
+
}
|
|
4216
|
+
replaceNode(initPath, propsIdentifier, init);
|
|
4217
|
+
}
|
|
4218
|
+
};
|
|
4219
|
+
}
|
|
4220
|
+
function isUseAttrsCall(expr) {
|
|
4221
|
+
return t37.isCallExpression(expr) && isCalleeNamed(expr, VUE_API_MAP.useAttrs);
|
|
4222
|
+
}
|
|
4223
|
+
function createPropsTypeAssertion(propsIdentifier, typeAnnotation) {
|
|
4224
|
+
return t37.tsAsExpression(propsIdentifier, typeAnnotation);
|
|
4225
|
+
}
|
|
4226
|
+
|
|
3566
4227
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3567
|
-
import * as
|
|
4228
|
+
import * as t39 from "@babel/types";
|
|
3568
4229
|
|
|
3569
4230
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3570
4231
|
import { traverse as traverse2 } from "@babel/core";
|
|
3571
|
-
import * as
|
|
4232
|
+
import * as t38 from "@babel/types";
|
|
3572
4233
|
var TRACE_MAX_DEPTH = 20;
|
|
3573
4234
|
function analyzeDeps(node, ctx, parentPath) {
|
|
3574
4235
|
if (!parentPath) {
|
|
3575
|
-
return
|
|
4236
|
+
return t38.arrayExpression([]);
|
|
3576
4237
|
}
|
|
3577
|
-
const isFnExpr =
|
|
4238
|
+
const isFnExpr = t38.isArrowFunctionExpression(node) || t38.isFunctionExpression(node);
|
|
3578
4239
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
3579
4240
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
3580
4241
|
const reactiveStateApis = getReactiveStateApis();
|
|
@@ -3585,13 +4246,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3585
4246
|
}
|
|
3586
4247
|
const analyzeTargetPath = parentPath && parentPath.node === analyzeTarget ? parentPath : null;
|
|
3587
4248
|
if (analyzeTargetPath) {
|
|
3588
|
-
if (
|
|
4249
|
+
if (t38.isMemberExpression(analyzeTarget) || t38.isOptionalMemberExpression(analyzeTarget)) {
|
|
3589
4250
|
const rootId = findRootIdentifier(analyzeTarget);
|
|
3590
4251
|
if (rootId) {
|
|
3591
4252
|
tryAddDependency(analyzeTargetPath, rootId.name, analyzeTargetPath.scope);
|
|
3592
4253
|
processedIdentifiers.add(rootId);
|
|
3593
4254
|
}
|
|
3594
|
-
} else if (
|
|
4255
|
+
} else if (t38.isIdentifier(analyzeTarget)) {
|
|
3595
4256
|
tryAddDependency(analyzeTargetPath, analyzeTarget.name, analyzeTargetPath.scope);
|
|
3596
4257
|
}
|
|
3597
4258
|
}
|
|
@@ -3642,55 +4303,55 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3642
4303
|
}
|
|
3643
4304
|
}
|
|
3644
4305
|
function normalizeDependencyExpr(path9, rootName) {
|
|
3645
|
-
if (
|
|
3646
|
-
return
|
|
4306
|
+
if (t38.isIdentifier(path9.node)) {
|
|
4307
|
+
return t38.identifier(path9.node.name);
|
|
3647
4308
|
}
|
|
3648
|
-
if (
|
|
4309
|
+
if (t38.isMemberExpression(path9.node) || t38.isOptionalMemberExpression(path9.node)) {
|
|
3649
4310
|
const normalizedExp = normalizeMemberForCallSite(path9, path9.node);
|
|
3650
|
-
const safeExp =
|
|
4311
|
+
const safeExp = t38.isMemberExpression(normalizedExp) || t38.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForMemberChain(normalizedExp) : normalizedExp;
|
|
3651
4312
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
3652
|
-
return
|
|
4313
|
+
return t38.cloneNode(safeExp, true);
|
|
3653
4314
|
}
|
|
3654
|
-
return
|
|
4315
|
+
return t38.identifier(rootName);
|
|
3655
4316
|
}
|
|
3656
4317
|
return null;
|
|
3657
4318
|
}
|
|
3658
4319
|
function normalizeSourcedDependency(exp) {
|
|
3659
|
-
if (
|
|
3660
|
-
return
|
|
4320
|
+
if (t38.isIdentifier(exp)) {
|
|
4321
|
+
return t38.identifier(exp.name);
|
|
3661
4322
|
}
|
|
3662
|
-
if (
|
|
4323
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3663
4324
|
const root = findRootIdentifier(exp);
|
|
3664
4325
|
if (!root) return null;
|
|
3665
|
-
const safeExp =
|
|
4326
|
+
const safeExp = t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp) ? ensureOptionalForMemberChain(exp) : exp;
|
|
3666
4327
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
3667
|
-
return
|
|
4328
|
+
return t38.cloneNode(safeExp, true);
|
|
3668
4329
|
}
|
|
3669
|
-
return
|
|
4330
|
+
return t38.identifier(root.name);
|
|
3670
4331
|
}
|
|
3671
4332
|
return null;
|
|
3672
4333
|
}
|
|
3673
4334
|
function isReactValidDependencyExpr(node2) {
|
|
3674
|
-
if (
|
|
4335
|
+
if (t38.isIdentifier(node2)) {
|
|
3675
4336
|
return true;
|
|
3676
4337
|
}
|
|
3677
|
-
if (
|
|
4338
|
+
if (t38.isMemberExpression(node2) || t38.isOptionalMemberExpression(node2)) {
|
|
3678
4339
|
return isStaticMemberChain(node2);
|
|
3679
4340
|
}
|
|
3680
4341
|
return false;
|
|
3681
4342
|
}
|
|
3682
4343
|
function isStaticMemberChain(node2) {
|
|
3683
4344
|
let current = node2;
|
|
3684
|
-
while (
|
|
3685
|
-
if (!current.computed && !
|
|
4345
|
+
while (t38.isMemberExpression(current) || t38.isOptionalMemberExpression(current)) {
|
|
4346
|
+
if (!current.computed && !t38.isIdentifier(current.property)) {
|
|
3686
4347
|
return false;
|
|
3687
4348
|
}
|
|
3688
|
-
if (current.computed && !
|
|
4349
|
+
if (current.computed && !t38.isStringLiteral(current.property) && !t38.isNumericLiteral(current.property)) {
|
|
3689
4350
|
return false;
|
|
3690
4351
|
}
|
|
3691
4352
|
current = current.object;
|
|
3692
4353
|
}
|
|
3693
|
-
return
|
|
4354
|
+
return t38.isIdentifier(current);
|
|
3694
4355
|
}
|
|
3695
4356
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
3696
4357
|
let current = binding.path;
|
|
@@ -3708,7 +4369,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3708
4369
|
if (!isDirectCallee) {
|
|
3709
4370
|
return node2;
|
|
3710
4371
|
}
|
|
3711
|
-
if (!
|
|
4372
|
+
if (!t38.isExpression(node2.object)) {
|
|
3712
4373
|
return node2;
|
|
3713
4374
|
}
|
|
3714
4375
|
return node2.object;
|
|
@@ -3717,15 +4378,15 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3717
4378
|
if (!hasTrailingMemberAccess(node2)) {
|
|
3718
4379
|
return node2;
|
|
3719
4380
|
}
|
|
3720
|
-
if (
|
|
4381
|
+
if (t38.isOptionalMemberExpression(node2) && node2.optional) {
|
|
3721
4382
|
return node2;
|
|
3722
4383
|
}
|
|
3723
|
-
const object =
|
|
3724
|
-
const property =
|
|
3725
|
-
return
|
|
4384
|
+
const object = t38.cloneNode(node2.object, true);
|
|
4385
|
+
const property = t38.cloneNode(node2.property, true);
|
|
4386
|
+
return t38.optionalMemberExpression(object, property, node2.computed, true);
|
|
3726
4387
|
}
|
|
3727
4388
|
function hasTrailingMemberAccess(node2) {
|
|
3728
|
-
return
|
|
4389
|
+
return t38.isMemberExpression(node2.object) || t38.isOptionalMemberExpression(node2.object);
|
|
3729
4390
|
}
|
|
3730
4391
|
function isEligibleBindingSource(binding) {
|
|
3731
4392
|
if (binding.kind === "param") {
|
|
@@ -3735,17 +4396,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3735
4396
|
const declaratorPath = getVariableDeclaratorPath(bindingPath);
|
|
3736
4397
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
3737
4398
|
const nodeInit = declaratorPath?.node.init;
|
|
3738
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
3739
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
3740
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
4399
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t38.isCallExpression(nodeInit) && t38.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
4400
|
+
const isHookCallVarBinding = !!declaratorPath && t38.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
4401
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t38.isArrowFunctionExpression(nodeInit) || t38.isFunctionExpression(nodeInit));
|
|
3741
4402
|
const isReactiveFunctionBinding = isFunctionBinding && (isReactiveBinding(declaratorPath?.node) || isReactiveBinding(bindingPath.node));
|
|
3742
4403
|
return isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isReactiveFunctionBinding;
|
|
3743
4404
|
}
|
|
3744
4405
|
function isHookLikeCallee(callee) {
|
|
3745
|
-
if (
|
|
4406
|
+
if (t38.isIdentifier(callee)) {
|
|
3746
4407
|
return callee.name.startsWith("use");
|
|
3747
4408
|
}
|
|
3748
|
-
if (
|
|
4409
|
+
if (t38.isMemberExpression(callee) && !callee.computed && t38.isIdentifier(callee.property)) {
|
|
3749
4410
|
return callee.property.name.startsWith("use");
|
|
3750
4411
|
}
|
|
3751
4412
|
return false;
|
|
@@ -3761,7 +4422,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3761
4422
|
}
|
|
3762
4423
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
3763
4424
|
if (depth <= 0) return null;
|
|
3764
|
-
if (
|
|
4425
|
+
if (t38.isIdentifier(exp)) {
|
|
3765
4426
|
const sourceBinding = scope.getBinding(exp.name);
|
|
3766
4427
|
if (!sourceBinding) return null;
|
|
3767
4428
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -3769,13 +4430,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3769
4430
|
}
|
|
3770
4431
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3771
4432
|
}
|
|
3772
|
-
if (
|
|
4433
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3773
4434
|
const root = findRootIdentifier(exp);
|
|
3774
4435
|
if (!root) return null;
|
|
3775
4436
|
const sourceBinding = scope.getBinding(root.name);
|
|
3776
4437
|
if (!sourceBinding) return null;
|
|
3777
4438
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
3778
|
-
return
|
|
4439
|
+
return t38.cloneNode(exp);
|
|
3779
4440
|
}
|
|
3780
4441
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3781
4442
|
if (sourcedRoot) {
|
|
@@ -3783,17 +4444,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3783
4444
|
if (rebuilt) {
|
|
3784
4445
|
return rebuilt;
|
|
3785
4446
|
}
|
|
3786
|
-
return
|
|
4447
|
+
return t38.cloneNode(sourcedRoot, true);
|
|
3787
4448
|
}
|
|
3788
4449
|
}
|
|
3789
4450
|
return null;
|
|
3790
4451
|
}
|
|
3791
4452
|
function rebuildMemberWithNewRoot(node2, nextRoot) {
|
|
3792
4453
|
const replacedObject = (() => {
|
|
3793
|
-
if (
|
|
3794
|
-
return
|
|
4454
|
+
if (t38.isIdentifier(node2.object)) {
|
|
4455
|
+
return t38.cloneNode(nextRoot, true);
|
|
3795
4456
|
}
|
|
3796
|
-
if (
|
|
4457
|
+
if (t38.isMemberExpression(node2.object) || t38.isOptionalMemberExpression(node2.object)) {
|
|
3797
4458
|
return rebuildMemberWithNewRoot(node2.object, nextRoot);
|
|
3798
4459
|
}
|
|
3799
4460
|
return null;
|
|
@@ -3801,34 +4462,34 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3801
4462
|
if (!replacedObject) {
|
|
3802
4463
|
return null;
|
|
3803
4464
|
}
|
|
3804
|
-
const property =
|
|
3805
|
-
if (
|
|
3806
|
-
return
|
|
4465
|
+
const property = t38.cloneNode(node2.property, true);
|
|
4466
|
+
if (t38.isMemberExpression(node2)) {
|
|
4467
|
+
return t38.memberExpression(
|
|
3807
4468
|
replacedObject,
|
|
3808
4469
|
property,
|
|
3809
4470
|
node2.computed
|
|
3810
4471
|
);
|
|
3811
4472
|
}
|
|
3812
|
-
return
|
|
4473
|
+
return t38.optionalMemberExpression(
|
|
3813
4474
|
replacedObject,
|
|
3814
4475
|
property,
|
|
3815
4476
|
node2.computed,
|
|
3816
4477
|
node2.optional
|
|
3817
4478
|
);
|
|
3818
4479
|
}
|
|
3819
|
-
return
|
|
4480
|
+
return t38.arrayExpression(Array.from(deps.values()));
|
|
3820
4481
|
}
|
|
3821
4482
|
function getDependencyKey(exp) {
|
|
3822
|
-
if (
|
|
4483
|
+
if (t38.isIdentifier(exp)) {
|
|
3823
4484
|
return exp.name;
|
|
3824
4485
|
}
|
|
3825
|
-
if (
|
|
4486
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3826
4487
|
const objectKey = getDependencyKey(exp.object);
|
|
3827
4488
|
const opt = exp.optional ? "?" : "";
|
|
3828
|
-
if (!exp.computed &&
|
|
4489
|
+
if (!exp.computed && t38.isIdentifier(exp.property)) {
|
|
3829
4490
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
3830
4491
|
}
|
|
3831
|
-
if (
|
|
4492
|
+
if (t38.isStringLiteral(exp.property) || t38.isNumericLiteral(exp.property)) {
|
|
3832
4493
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
3833
4494
|
}
|
|
3834
4495
|
return `${objectKey}${opt}[*]`;
|
|
@@ -3861,7 +4522,7 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3861
4522
|
if (!isVueApiReference(path9, apiName)) {
|
|
3862
4523
|
return;
|
|
3863
4524
|
}
|
|
3864
|
-
if (
|
|
4525
|
+
if (t39.isCallExpression(node)) {
|
|
3865
4526
|
resolveCallNode(path9, adapter, ctx);
|
|
3866
4527
|
} else {
|
|
3867
4528
|
replaceIdName(node, adapter.target);
|
|
@@ -3871,11 +4532,11 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3871
4532
|
};
|
|
3872
4533
|
}
|
|
3873
4534
|
function getApiName(node) {
|
|
3874
|
-
const isCallNode =
|
|
4535
|
+
const isCallNode = t39.isCallExpression(node);
|
|
3875
4536
|
let apiName = "";
|
|
3876
|
-
if (
|
|
4537
|
+
if (t39.isIdentifier(node)) {
|
|
3877
4538
|
apiName = node.name;
|
|
3878
|
-
} else if (isCallNode &&
|
|
4539
|
+
} else if (isCallNode && t39.isIdentifier(node.callee)) {
|
|
3879
4540
|
apiName = node.callee.name;
|
|
3880
4541
|
}
|
|
3881
4542
|
return apiName;
|
|
@@ -3885,7 +4546,7 @@ function resolveCallNode(path9, adapter, ctx) {
|
|
|
3885
4546
|
const { arguments: args } = node;
|
|
3886
4547
|
if (!args.length) return;
|
|
3887
4548
|
const fn = args[0];
|
|
3888
|
-
if (!
|
|
4549
|
+
if (!t39.isArrowFunctionExpression(fn) && !t39.isFunctionExpression(fn)) {
|
|
3889
4550
|
return;
|
|
3890
4551
|
}
|
|
3891
4552
|
const fnPath = path9.get("arguments")[0];
|
|
@@ -3917,7 +4578,7 @@ function isVueImportBinding(binding) {
|
|
|
3917
4578
|
return false;
|
|
3918
4579
|
}
|
|
3919
4580
|
const parent = bindingPath.parentPath?.node;
|
|
3920
|
-
if (!parent || !
|
|
4581
|
+
if (!parent || !t39.isImportDeclaration(parent)) {
|
|
3921
4582
|
return false;
|
|
3922
4583
|
}
|
|
3923
4584
|
const source = parent.source.value.toLowerCase();
|
|
@@ -3944,7 +4605,7 @@ function resolveArrowFnDeps(ctx, ast) {
|
|
|
3944
4605
|
const declaratorPath = getVariableDeclaratorPath(path9);
|
|
3945
4606
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useCallback);
|
|
3946
4607
|
setScriptNodeMeta(declaratorPath?.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3947
|
-
path9
|
|
4608
|
+
replaceNode(path9, newNode, node);
|
|
3948
4609
|
}
|
|
3949
4610
|
};
|
|
3950
4611
|
}
|
|
@@ -3985,7 +4646,7 @@ function isSkip(path9) {
|
|
|
3985
4646
|
}
|
|
3986
4647
|
|
|
3987
4648
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3988
|
-
import * as
|
|
4649
|
+
import * as t40 from "@babel/types";
|
|
3989
4650
|
function resolveElementRef(ctx) {
|
|
3990
4651
|
return {
|
|
3991
4652
|
CallExpression(path9) {
|
|
@@ -4003,14 +4664,14 @@ function resolveElementRef(ctx) {
|
|
|
4003
4664
|
}
|
|
4004
4665
|
if (isCompRefBindings) {
|
|
4005
4666
|
const varDeclaratorPath = getVariableDeclaratorPath(path9)?.node;
|
|
4006
|
-
if (!
|
|
4667
|
+
if (!t40.isIdentifier(varDeclaratorPath?.id)) {
|
|
4007
4668
|
return;
|
|
4008
4669
|
}
|
|
4009
4670
|
const varName = varDeclaratorPath.id.name;
|
|
4010
4671
|
const compRef = refBindings.componentRefs[varName];
|
|
4011
4672
|
if (!compRef) return;
|
|
4012
4673
|
}
|
|
4013
|
-
node.arguments[0] =
|
|
4674
|
+
node.arguments[0] = t40.identifier("null");
|
|
4014
4675
|
resolveTypeParameters(ctx, path9);
|
|
4015
4676
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
4016
4677
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
@@ -4035,27 +4696,27 @@ function resolveTypeParameters(ctx, path9) {
|
|
|
4035
4696
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
4036
4697
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
4037
4698
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
4038
|
-
node.typeParameters =
|
|
4699
|
+
node.typeParameters = t40.tsTypeParameterInstantiation([t40.tsTypeReference(t40.identifier(type))]);
|
|
4039
4700
|
}
|
|
4040
4701
|
}
|
|
4041
4702
|
function resolveRefValueToCurrent(path9) {
|
|
4042
4703
|
const { node } = path9;
|
|
4043
|
-
if (node.computed || !
|
|
4704
|
+
if (node.computed || !t40.isIdentifier(node.property) || node.property.name !== "value") {
|
|
4044
4705
|
return;
|
|
4045
4706
|
}
|
|
4046
4707
|
const rootPath = findRootVariablePath(path9);
|
|
4047
|
-
if (!rootPath?.node || !
|
|
4708
|
+
if (!rootPath?.node || !t40.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
4048
4709
|
return;
|
|
4049
4710
|
}
|
|
4050
4711
|
const rootId = findRootIdentifier(node);
|
|
4051
|
-
if (!
|
|
4712
|
+
if (!t40.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
4052
4713
|
return;
|
|
4053
4714
|
}
|
|
4054
4715
|
node.property.name = "current";
|
|
4055
4716
|
}
|
|
4056
4717
|
|
|
4057
4718
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
4058
|
-
import * as
|
|
4719
|
+
import * as t41 from "@babel/types";
|
|
4059
4720
|
function resolveExprMemo(ctx, ast) {
|
|
4060
4721
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
4061
4722
|
return {
|
|
@@ -4067,11 +4728,11 @@ function resolveExprMemo(ctx, ast) {
|
|
|
4067
4728
|
if (!atComponentOrHookRoot(path9, ast.program, isScriptFile)) {
|
|
4068
4729
|
return false;
|
|
4069
4730
|
}
|
|
4070
|
-
if (!
|
|
4731
|
+
if (!t41.isVariableDeclaration(path9.parent) || path9.parent.kind !== "const") {
|
|
4071
4732
|
return false;
|
|
4072
4733
|
}
|
|
4073
|
-
if (
|
|
4074
|
-
if (
|
|
4734
|
+
if (t41.isFunction(init)) return false;
|
|
4735
|
+
if (t41.isCallExpression(init) && t41.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
4075
4736
|
return false;
|
|
4076
4737
|
}
|
|
4077
4738
|
return true;
|
|
@@ -4089,16 +4750,16 @@ function resolveExprMemo(ctx, ast) {
|
|
|
4089
4750
|
}
|
|
4090
4751
|
|
|
4091
4752
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
4092
|
-
import * as
|
|
4753
|
+
import * as t42 from "@babel/types";
|
|
4093
4754
|
function resolveLintRules(ctx, ast) {
|
|
4094
4755
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
4095
4756
|
return {
|
|
4096
4757
|
CallExpression(path9) {
|
|
4097
4758
|
const { node, parentPath } = path9;
|
|
4098
|
-
if (!
|
|
4759
|
+
if (!t42.isIdentifier(node.callee)) return;
|
|
4099
4760
|
const { name: callName } = node.callee;
|
|
4100
|
-
const addLog = (
|
|
4101
|
-
logger.error(
|
|
4761
|
+
const addLog = (t48) => {
|
|
4762
|
+
logger.error(t48, {
|
|
4102
4763
|
file: ctx.filename,
|
|
4103
4764
|
source: ctx.scriptData.source,
|
|
4104
4765
|
loc: node.loc
|
|
@@ -4142,8 +4803,8 @@ function resolveLintRules(ctx, ast) {
|
|
|
4142
4803
|
}
|
|
4143
4804
|
|
|
4144
4805
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
4145
|
-
import { generate as
|
|
4146
|
-
import * as
|
|
4806
|
+
import { generate as generate2 } from "@babel/generator";
|
|
4807
|
+
import * as t43 from "@babel/types";
|
|
4147
4808
|
function resolveProvide(ctx) {
|
|
4148
4809
|
if (ctx.inputType === "style") return {};
|
|
4149
4810
|
return {
|
|
@@ -4175,17 +4836,17 @@ function findOrCreateCtxProvider(root) {
|
|
|
4175
4836
|
function assignProviderValue(target, key, value) {
|
|
4176
4837
|
const getRawExp = (exp) => {
|
|
4177
4838
|
if (!exp) return "''";
|
|
4178
|
-
if (
|
|
4839
|
+
if (t43.isStringLiteral(exp)) {
|
|
4179
4840
|
return JSON.stringify(exp.value);
|
|
4180
4841
|
}
|
|
4181
|
-
if (
|
|
4842
|
+
if (t43.isNumericLiteral(exp)) {
|
|
4182
4843
|
return exp.value.toString();
|
|
4183
4844
|
}
|
|
4184
|
-
if (
|
|
4845
|
+
if (t43.isIdentifier(exp)) {
|
|
4185
4846
|
return exp.name;
|
|
4186
4847
|
}
|
|
4187
4848
|
try {
|
|
4188
|
-
return
|
|
4849
|
+
return generate2(exp).code;
|
|
4189
4850
|
} catch {
|
|
4190
4851
|
return "null";
|
|
4191
4852
|
}
|
|
@@ -4197,16 +4858,16 @@ function assignProviderValue(target, key, value) {
|
|
|
4197
4858
|
}
|
|
4198
4859
|
|
|
4199
4860
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
4200
|
-
import * as
|
|
4861
|
+
import * as t44 from "@babel/types";
|
|
4201
4862
|
function resolveRenameAdapter(ctx) {
|
|
4202
4863
|
return {
|
|
4203
4864
|
"CallExpression|Identifier"(path9) {
|
|
4204
4865
|
const node = path9.node;
|
|
4205
|
-
const isCallNode =
|
|
4866
|
+
const isCallNode = t44.isCallExpression(node);
|
|
4206
4867
|
let apiName = "";
|
|
4207
|
-
if (
|
|
4868
|
+
if (t44.isIdentifier(node)) {
|
|
4208
4869
|
apiName = node.name;
|
|
4209
|
-
} else if (isCallNode &&
|
|
4870
|
+
} else if (isCallNode && t44.isIdentifier(node.callee)) {
|
|
4210
4871
|
apiName = node.callee.name;
|
|
4211
4872
|
}
|
|
4212
4873
|
if (!apiName) {
|
|
@@ -4264,7 +4925,7 @@ function isVueImportBinding2(binding) {
|
|
|
4264
4925
|
return false;
|
|
4265
4926
|
}
|
|
4266
4927
|
const parent = bindingPath.parentPath?.node;
|
|
4267
|
-
if (!parent || !
|
|
4928
|
+
if (!parent || !t44.isImportDeclaration(parent)) {
|
|
4268
4929
|
return false;
|
|
4269
4930
|
}
|
|
4270
4931
|
const source = parent.source.value.toLowerCase();
|
|
@@ -4284,11 +4945,12 @@ function processVueSyntax2(ast, ctx) {
|
|
|
4284
4945
|
applyBabel: [
|
|
4285
4946
|
resolvePropsIface,
|
|
4286
4947
|
resolveEmitsTopLevelTypes,
|
|
4287
|
-
resolveSlotsTopLevelTypes,
|
|
4288
4948
|
resolveDefineOptions,
|
|
4289
4949
|
resolveDefineExpose,
|
|
4290
4950
|
resolveDefineAsyncComponent,
|
|
4291
|
-
resolveEmitCalls
|
|
4951
|
+
resolveEmitCalls,
|
|
4952
|
+
// feature: https://github.com/vureact-js/core/issues/6
|
|
4953
|
+
resolveUseAttrs
|
|
4292
4954
|
]
|
|
4293
4955
|
},
|
|
4294
4956
|
process: {
|
|
@@ -4400,15 +5062,15 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
4400
5062
|
}
|
|
4401
5063
|
|
|
4402
5064
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
4403
|
-
import * as
|
|
5065
|
+
import * as t46 from "@babel/types";
|
|
4404
5066
|
|
|
4405
5067
|
// src/shared/string-code-types.ts
|
|
4406
5068
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
4407
|
-
import * as
|
|
5069
|
+
import * as t45 from "@babel/types";
|
|
4408
5070
|
var strCodeTypes = {
|
|
4409
|
-
isIdentifier:
|
|
5071
|
+
isIdentifier: isIdentifier24,
|
|
4410
5072
|
isSimpleExpression,
|
|
4411
|
-
isStringLiteral:
|
|
5073
|
+
isStringLiteral: isStringLiteral13
|
|
4412
5074
|
};
|
|
4413
5075
|
function isSimpleExpression(code, excludeVar = false) {
|
|
4414
5076
|
let node;
|
|
@@ -4417,38 +5079,38 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4417
5079
|
} catch {
|
|
4418
5080
|
return false;
|
|
4419
5081
|
}
|
|
4420
|
-
if (
|
|
5082
|
+
if (t45.isLiteral(node)) {
|
|
4421
5083
|
return true;
|
|
4422
5084
|
}
|
|
4423
|
-
if (!excludeVar &&
|
|
5085
|
+
if (!excludeVar && t45.isIdentifier(node)) {
|
|
4424
5086
|
return true;
|
|
4425
5087
|
}
|
|
4426
|
-
if (
|
|
4427
|
-
return isSimpleExpression(node.object) &&
|
|
5088
|
+
if (t45.isMemberExpression(node)) {
|
|
5089
|
+
return isSimpleExpression(node.object) && t45.isIdentifier(node.property);
|
|
4428
5090
|
}
|
|
4429
|
-
if (
|
|
5091
|
+
if (t45.isObjectExpression(node) || t45.isArrayExpression(node)) {
|
|
4430
5092
|
return false;
|
|
4431
5093
|
}
|
|
4432
|
-
if (
|
|
5094
|
+
if (t45.isCallExpression(node) || t45.isAssignmentExpression(node)) {
|
|
4433
5095
|
return false;
|
|
4434
5096
|
}
|
|
4435
|
-
if (
|
|
5097
|
+
if (t45.isBinaryExpression(node) || t45.isUnaryExpression(node)) {
|
|
4436
5098
|
return true;
|
|
4437
5099
|
}
|
|
4438
5100
|
return false;
|
|
4439
5101
|
}
|
|
4440
|
-
function
|
|
5102
|
+
function isIdentifier24(code) {
|
|
4441
5103
|
try {
|
|
4442
5104
|
const node = parseExpression3(code);
|
|
4443
|
-
return
|
|
5105
|
+
return t45.isIdentifier(node);
|
|
4444
5106
|
} catch {
|
|
4445
5107
|
return false;
|
|
4446
5108
|
}
|
|
4447
5109
|
}
|
|
4448
|
-
function
|
|
5110
|
+
function isStringLiteral13(code) {
|
|
4449
5111
|
try {
|
|
4450
5112
|
const node = parseExpression3(code);
|
|
4451
|
-
return
|
|
5113
|
+
return t45.isStringLiteral(node);
|
|
4452
5114
|
} catch {
|
|
4453
5115
|
return false;
|
|
4454
5116
|
}
|
|
@@ -4590,23 +5252,23 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4590
5252
|
const mergedItems = value.merge;
|
|
4591
5253
|
const setNameIdentifier = (target, valueName) => {
|
|
4592
5254
|
target.content = valueName;
|
|
4593
|
-
target.ast =
|
|
5255
|
+
target.ast = t46.jsxIdentifier(valueName);
|
|
4594
5256
|
};
|
|
4595
|
-
const setValueExpression = (target, content,
|
|
5257
|
+
const setValueExpression = (target, content, isStringLiteral14) => {
|
|
4596
5258
|
target.content = content;
|
|
4597
|
-
target.ast = resolveStringExpr(content, ctx,
|
|
5259
|
+
target.ast = resolveStringExpr(content, ctx, isStringLiteral14);
|
|
4598
5260
|
};
|
|
4599
5261
|
const createRuntimeCall = (fnName, args) => {
|
|
4600
5262
|
const fnArgs = args.filter(Boolean).join(",");
|
|
4601
5263
|
return `${fnName}(${fnArgs})`;
|
|
4602
5264
|
};
|
|
4603
|
-
const applyRuntimeExpression = (expression, setName = false, nameIdentifier,
|
|
5265
|
+
const applyRuntimeExpression = (expression, setName = false, nameIdentifier, isStringLiteral14) => {
|
|
4604
5266
|
if (setName && nameIdentifier) {
|
|
4605
5267
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
4606
5268
|
}
|
|
4607
5269
|
const dir = ADAPTER_RULES.runtime.dir;
|
|
4608
5270
|
recordImport(ctx, dir.package, dir.target);
|
|
4609
|
-
setValueExpression(value.babelExp, expression,
|
|
5271
|
+
setValueExpression(value.babelExp, expression, isStringLiteral14);
|
|
4610
5272
|
};
|
|
4611
5273
|
if (ir.isKeyLessVBind) {
|
|
4612
5274
|
const dirKeyless = ADAPTER_RULES.runtime.dirKeyless;
|
|
@@ -4642,7 +5304,7 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4642
5304
|
return;
|
|
4643
5305
|
}
|
|
4644
5306
|
setNameIdentifier(nameExp, name);
|
|
4645
|
-
const normalizedValue =
|
|
5307
|
+
const normalizedValue = resolveSpecialExpression(valueContent, ctx);
|
|
4646
5308
|
setValueExpression(value.babelExp, normalizedValue, value.isStringLiteral);
|
|
4647
5309
|
}
|
|
4648
5310
|
|
|
@@ -4880,22 +5542,22 @@ function warnUnsupportedVueDollarVar(ctx, node) {
|
|
|
4880
5542
|
}
|
|
4881
5543
|
|
|
4882
5544
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-is-prop.ts
|
|
4883
|
-
function resolveStaticIsProp(
|
|
4884
|
-
if (!
|
|
5545
|
+
function resolveStaticIsProp(content, ir, ctx, nodeIR) {
|
|
5546
|
+
if (!content) {
|
|
4885
5547
|
return;
|
|
4886
5548
|
}
|
|
4887
|
-
if (
|
|
4888
|
-
const name =
|
|
5549
|
+
if (content.startsWith("vue:")) {
|
|
5550
|
+
const name = content.split("vue:")[1];
|
|
4889
5551
|
nodeIR.tag = camelCase(name);
|
|
4890
5552
|
return;
|
|
4891
5553
|
}
|
|
4892
|
-
const propIR = createPropsIR("is", "is",
|
|
5554
|
+
const propIR = createPropsIR("is", "is", content);
|
|
4893
5555
|
propIR.value.isStringLiteral = true;
|
|
4894
5556
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4895
5557
|
nodeIR.props.push(propIR);
|
|
4896
5558
|
}
|
|
4897
|
-
function resolveDynamicIsProp(
|
|
4898
|
-
const exp =
|
|
5559
|
+
function resolveDynamicIsProp(directive, ir, ctx, nodeIR) {
|
|
5560
|
+
const exp = directive.exp;
|
|
4899
5561
|
const content = exp.content;
|
|
4900
5562
|
if (strCodeTypes.isStringLiteral(content)) {
|
|
4901
5563
|
resolveStaticIsProp(content, ir, ctx, nodeIR);
|
|
@@ -4911,20 +5573,20 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4911
5573
|
|
|
4912
5574
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-ref-prop.ts
|
|
4913
5575
|
import { NodeTypes as NodeTypes5 } from "@vue/compiler-core";
|
|
4914
|
-
function resolveRefProp(
|
|
5576
|
+
function resolveRefProp(prop, ctx, nodeIR) {
|
|
4915
5577
|
const {
|
|
4916
5578
|
templateData: { refBindings }
|
|
4917
5579
|
} = ctx;
|
|
4918
5580
|
let propIR;
|
|
4919
|
-
if (
|
|
4920
|
-
const tag =
|
|
5581
|
+
if (prop.type === NodeTypes5.ATTRIBUTE) {
|
|
5582
|
+
const tag = prop.value?.content;
|
|
4921
5583
|
if (!tag) return;
|
|
4922
5584
|
collectComponentRef(tag, ctx);
|
|
4923
5585
|
const domRefBinding = Object.values(refBindings.domRefs).find((r) => r.tag === tag);
|
|
4924
5586
|
const refVar = domRefBinding?.name || refBindings.componentRefs[tag]?.name;
|
|
4925
5587
|
propIR = createPropsIR("ref", "ref", refVar || "null");
|
|
4926
5588
|
} else {
|
|
4927
|
-
const exp =
|
|
5589
|
+
const exp = prop.exp;
|
|
4928
5590
|
for (const name in refBindings.domRefs) {
|
|
4929
5591
|
const newName = `${name}.current`;
|
|
4930
5592
|
const regex = new RegExp(`${name}(?!\\.current)`, "g");
|
|
@@ -4952,50 +5614,92 @@ function collectComponentRef(tag, ctx) {
|
|
|
4952
5614
|
}
|
|
4953
5615
|
}
|
|
4954
5616
|
|
|
5617
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-template-key.ts
|
|
5618
|
+
import {
|
|
5619
|
+
ConstantTypes,
|
|
5620
|
+
NodeTypes as NodeTypes6
|
|
5621
|
+
} from "@vue/compiler-core";
|
|
5622
|
+
function resolveTemplateNodeKey(vueNode, keyContent) {
|
|
5623
|
+
const firstNode = vueNode.children[0];
|
|
5624
|
+
if (!firstNode) return;
|
|
5625
|
+
const hasKeyProp = firstNode.props.some(
|
|
5626
|
+
(p) => p.type === NodeTypes6.DIRECTIVE && p.name === "key"
|
|
5627
|
+
);
|
|
5628
|
+
if (hasKeyProp) return;
|
|
5629
|
+
firstNode.props.push(createSimpleVueBind("key", keyContent));
|
|
5630
|
+
}
|
|
5631
|
+
function createSimpleVueBind(name, value) {
|
|
5632
|
+
return {
|
|
5633
|
+
type: NodeTypes6.DIRECTIVE,
|
|
5634
|
+
name: "bind",
|
|
5635
|
+
rawName: `:${name}`,
|
|
5636
|
+
exp: {
|
|
5637
|
+
type: NodeTypes6.SIMPLE_EXPRESSION,
|
|
5638
|
+
content: value,
|
|
5639
|
+
isStatic: false,
|
|
5640
|
+
constType: ConstantTypes.NOT_CONSTANT,
|
|
5641
|
+
loc: {}
|
|
5642
|
+
},
|
|
5643
|
+
arg: {
|
|
5644
|
+
type: NodeTypes6.SIMPLE_EXPRESSION,
|
|
5645
|
+
content: name,
|
|
5646
|
+
isStatic: true,
|
|
5647
|
+
constType: ConstantTypes.CAN_STRINGIFY,
|
|
5648
|
+
loc: {}
|
|
5649
|
+
},
|
|
5650
|
+
modifiers: [],
|
|
5651
|
+
loc: {}
|
|
5652
|
+
};
|
|
5653
|
+
}
|
|
5654
|
+
|
|
4955
5655
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-dynamic-attribute-prop.ts
|
|
4956
|
-
function resolveDynamicAttributeProp(
|
|
4957
|
-
const arg =
|
|
4958
|
-
const exp =
|
|
5656
|
+
function resolveDynamicAttributeProp(directive, ir, ctx, vueNode, nodeIR) {
|
|
5657
|
+
const arg = directive.arg;
|
|
5658
|
+
const exp = directive.exp;
|
|
4959
5659
|
const name = arg?.content ?? "";
|
|
4960
5660
|
const content = exp?.content ?? "true";
|
|
4961
|
-
warnUnsupportedVueDollarVar(ctx,
|
|
5661
|
+
warnUnsupportedVueDollarVar(ctx, directive);
|
|
4962
5662
|
if (name === "is") {
|
|
4963
|
-
resolveDynamicIsProp(
|
|
5663
|
+
resolveDynamicIsProp(directive, ir, ctx, nodeIR);
|
|
4964
5664
|
return;
|
|
4965
5665
|
}
|
|
4966
5666
|
if (name === "ref") {
|
|
4967
|
-
resolveRefProp(
|
|
5667
|
+
resolveRefProp(directive, ctx, nodeIR);
|
|
5668
|
+
return;
|
|
5669
|
+
}
|
|
5670
|
+
if (vueNode.tag === "template" && name === "key") {
|
|
5671
|
+
resolveTemplateNodeKey(vueNode, content);
|
|
4968
5672
|
return;
|
|
4969
5673
|
}
|
|
4970
|
-
const
|
|
4971
|
-
|
|
4972
|
-
checkPropIsDynamicKey(ctx,
|
|
4973
|
-
resolvePropertyIR(
|
|
5674
|
+
const propIR = createPropsIR(directive.rawName, name, content);
|
|
5675
|
+
propIR.isStatic = arg?.isStatic ?? true;
|
|
5676
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
5677
|
+
resolvePropertyIR(propIR, ir, ctx, nodeIR, true);
|
|
4974
5678
|
}
|
|
4975
|
-
function resolvePropertyIR(
|
|
4976
|
-
let content =
|
|
4977
|
-
if (isVBind(
|
|
4978
|
-
|
|
5679
|
+
function resolvePropertyIR(propsIR, ir, ctx, nodeIR, isDynamic = false) {
|
|
5680
|
+
let content = propsIR.value.content;
|
|
5681
|
+
if (isVBind(propsIR.rawName) && !propsIR.name) {
|
|
5682
|
+
propsIR.isKeyLessVBind = true;
|
|
4979
5683
|
}
|
|
4980
|
-
if (isStyleAttr(
|
|
4981
|
-
|
|
4982
|
-
content =
|
|
5684
|
+
if (isStyleAttr(propsIR.name)) {
|
|
5685
|
+
propsIR.value.isStringLiteral = false;
|
|
5686
|
+
content = propsIR.value.content = parseStyleString(content);
|
|
4983
5687
|
}
|
|
4984
5688
|
if (isDynamic) {
|
|
4985
|
-
const
|
|
4986
|
-
if (
|
|
5689
|
+
const isStringLiteral14 = strCodeTypes.isStringLiteral(content);
|
|
5690
|
+
if (isStringLiteral14) {
|
|
4987
5691
|
content = normalizeString(content);
|
|
4988
|
-
|
|
5692
|
+
propsIR.value.content = content;
|
|
4989
5693
|
}
|
|
4990
|
-
|
|
5694
|
+
propsIR.value.isStringLiteral = isStringLiteral14;
|
|
4991
5695
|
}
|
|
4992
|
-
const existing = findSameProp(nodeIR.props,
|
|
5696
|
+
const existing = findSameProp(nodeIR.props, propsIR);
|
|
4993
5697
|
if (existing) {
|
|
4994
|
-
mergePropsIR(ctx, existing,
|
|
5698
|
+
mergePropsIR(ctx, existing, propsIR);
|
|
4995
5699
|
} else {
|
|
4996
|
-
nodeIR.props.push(
|
|
5700
|
+
nodeIR.props.push(propsIR);
|
|
4997
5701
|
}
|
|
4998
|
-
resolvePropAsBabelExp(existing ??
|
|
5702
|
+
resolvePropAsBabelExp(existing ?? propsIR, ctx);
|
|
4999
5703
|
}
|
|
5000
5704
|
function normalizeString(s) {
|
|
5001
5705
|
if (s.startsWith("'") && s.endsWith("'")) {
|
|
@@ -5005,15 +5709,15 @@ function normalizeString(s) {
|
|
|
5005
5709
|
}
|
|
5006
5710
|
|
|
5007
5711
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-attribute-prop.ts
|
|
5008
|
-
function resolveAttributeProp(
|
|
5009
|
-
const name =
|
|
5010
|
-
const content =
|
|
5712
|
+
function resolveAttributeProp(attribute, ir, ctx, nodeIR) {
|
|
5713
|
+
const name = attribute.name;
|
|
5714
|
+
const content = attribute.value?.content ?? "true";
|
|
5011
5715
|
if (name === "is") {
|
|
5012
5716
|
resolveStaticIsProp(content, ir, ctx, nodeIR);
|
|
5013
5717
|
return;
|
|
5014
5718
|
}
|
|
5015
5719
|
if (name === "ref") {
|
|
5016
|
-
resolveRefProp(
|
|
5720
|
+
resolveRefProp(attribute, ctx, nodeIR);
|
|
5017
5721
|
return;
|
|
5018
5722
|
}
|
|
5019
5723
|
const attrIR = createPropsIR(name, name, content);
|
|
@@ -5022,15 +5726,86 @@ function resolveAttributeProp(node, ir, ctx, nodeIR) {
|
|
|
5022
5726
|
resolvePropertyIR(attrIR, ir, ctx, nodeIR);
|
|
5023
5727
|
}
|
|
5024
5728
|
|
|
5729
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-directive-prop.ts
|
|
5730
|
+
function resolveDirectiveProp(directive, ir, ctx, vueNode, nodeIR, siblingNodesIR) {
|
|
5731
|
+
const { name, rawName } = directive;
|
|
5732
|
+
if (!SUPPORTED_DIRECTIVES.has(name)) {
|
|
5733
|
+
warnUnsupportedDirective(ctx, directive.loc, rawName);
|
|
5734
|
+
return;
|
|
5735
|
+
}
|
|
5736
|
+
warnUnsupportedVueDollarVar(ctx, directive);
|
|
5737
|
+
if (isVConditional(rawName)) {
|
|
5738
|
+
return resolveVIf(directive, ir, ctx, nodeIR, siblingNodesIR);
|
|
5739
|
+
}
|
|
5740
|
+
function processExactDirectives() {
|
|
5741
|
+
switch (directive.rawName) {
|
|
5742
|
+
case "v-html":
|
|
5743
|
+
resolveVHtml(directive, ir, ctx, nodeIR);
|
|
5744
|
+
return true;
|
|
5745
|
+
case "v-text":
|
|
5746
|
+
resolveVText(directive, ir, ctx, nodeIR);
|
|
5747
|
+
return true;
|
|
5748
|
+
case "v-once":
|
|
5749
|
+
case "v-memo":
|
|
5750
|
+
return resolveVMemo(directive, ir, ctx, nodeIR);
|
|
5751
|
+
case "v-show":
|
|
5752
|
+
return resolveVShow(directive, ir, ctx, nodeIR);
|
|
5753
|
+
case "v-for":
|
|
5754
|
+
return resolveVFor(directive, ir, ctx, nodeIR);
|
|
5755
|
+
}
|
|
5756
|
+
}
|
|
5757
|
+
function processRangeDirectives() {
|
|
5758
|
+
const { rawName: rawName2 } = directive;
|
|
5759
|
+
if (isVModel(rawName2)) {
|
|
5760
|
+
return resolveVModel(directive, ir, ctx, vueNode, nodeIR);
|
|
5761
|
+
}
|
|
5762
|
+
if (isVBind(rawName2)) {
|
|
5763
|
+
return resolveDynamicAttributeProp(directive, ir, ctx, vueNode, nodeIR);
|
|
5764
|
+
}
|
|
5765
|
+
if (isVOn(rawName2)) {
|
|
5766
|
+
return resolveVOn(directive, ir, ctx, nodeIR);
|
|
5767
|
+
}
|
|
5768
|
+
if (isVSlot(rawName2)) {
|
|
5769
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
5770
|
+
resolveRouterLinkVSlotProp(directive, nodeIR, ctx);
|
|
5771
|
+
}
|
|
5772
|
+
return true;
|
|
5773
|
+
}
|
|
5774
|
+
}
|
|
5775
|
+
return processExactDirectives() || processRangeDirectives();
|
|
5776
|
+
}
|
|
5777
|
+
|
|
5778
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-props.ts
|
|
5779
|
+
import { NodeTypes as NodeTypes7 } from "@vue/compiler-core";
|
|
5780
|
+
function resolveProps(vueNode, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5781
|
+
for (const prop of vueNode.props) {
|
|
5782
|
+
if (prop.type === NodeTypes7.ATTRIBUTE) {
|
|
5783
|
+
resolveAttributeProp(prop, ir, ctx, nodeIR);
|
|
5784
|
+
continue;
|
|
5785
|
+
}
|
|
5786
|
+
if (prop.type === NodeTypes7.DIRECTIVE) {
|
|
5787
|
+
const stop = resolveDirectiveProp(
|
|
5788
|
+
prop,
|
|
5789
|
+
ir,
|
|
5790
|
+
ctx,
|
|
5791
|
+
vueNode,
|
|
5792
|
+
nodeIR,
|
|
5793
|
+
siblingNodesIR
|
|
5794
|
+
);
|
|
5795
|
+
if (stop) break;
|
|
5796
|
+
}
|
|
5797
|
+
}
|
|
5798
|
+
}
|
|
5799
|
+
|
|
5025
5800
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-router-link-v-slot-prop.ts
|
|
5026
|
-
function resolveRouterLinkVSlotProp(
|
|
5027
|
-
const arg =
|
|
5028
|
-
const exp =
|
|
5029
|
-
checkPropIsDynamicKey(ctx,
|
|
5801
|
+
function resolveRouterLinkVSlotProp(directive, nodeIR, ctx) {
|
|
5802
|
+
const arg = directive.arg;
|
|
5803
|
+
const exp = directive.exp;
|
|
5804
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
5030
5805
|
const propIR = {
|
|
5031
5806
|
type: 2 /* SLOT */,
|
|
5032
5807
|
name: "customRender",
|
|
5033
|
-
rawName:
|
|
5808
|
+
rawName: directive.rawName ?? "v-slot",
|
|
5034
5809
|
isStatic: arg?.isStatic ?? true,
|
|
5035
5810
|
isScoped: true,
|
|
5036
5811
|
callback: {
|
|
@@ -5042,11 +5817,11 @@ function resolveRouterLinkVSlotProp(node, nodeIR, ctx) {
|
|
|
5042
5817
|
}
|
|
5043
5818
|
|
|
5044
5819
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-for.ts
|
|
5045
|
-
function resolveVFor(
|
|
5820
|
+
function resolveVFor(directive, ir, ctx, nodeIR) {
|
|
5046
5821
|
nodeIR.meta.loop = {
|
|
5047
5822
|
isLoop: true,
|
|
5048
5823
|
isHandled: false,
|
|
5049
|
-
value: resolveForResult(
|
|
5824
|
+
value: resolveForResult(directive.forParseResult)
|
|
5050
5825
|
};
|
|
5051
5826
|
}
|
|
5052
5827
|
function resolveForResult(forParseResult) {
|
|
@@ -5063,17 +5838,17 @@ function resolveForResult(forParseResult) {
|
|
|
5063
5838
|
}
|
|
5064
5839
|
|
|
5065
5840
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-html.ts
|
|
5066
|
-
function resolveVHtml(
|
|
5067
|
-
const exp =
|
|
5841
|
+
function resolveVHtml(directive, ir, ctx, nodeIR) {
|
|
5842
|
+
const exp = directive.exp;
|
|
5068
5843
|
const propIR = createPropsIR("v-html", "v-html", `{__html: ${exp.content}}`);
|
|
5069
5844
|
resolvePropAsBabelExp(propIR, ctx);
|
|
5070
5845
|
nodeIR.props.push(propIR);
|
|
5071
5846
|
}
|
|
5072
5847
|
|
|
5073
5848
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-if.ts
|
|
5074
|
-
function resolveVIf(
|
|
5075
|
-
const name =
|
|
5076
|
-
const value =
|
|
5849
|
+
function resolveVIf(directive, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5850
|
+
const name = directive.name === "else-if" ? "elseIf" : directive.name;
|
|
5851
|
+
const value = directive.exp?.content ?? "true";
|
|
5077
5852
|
const prevNode = siblingNodesIR[siblingNodesIR.length - 1];
|
|
5078
5853
|
const isElseBranch = name === "else" || name === "elseIf";
|
|
5079
5854
|
let hasError = false;
|
|
@@ -5094,7 +5869,7 @@ function resolveVIf(node, _ir, ctx, nodeIR, siblingNodesIR) {
|
|
|
5094
5869
|
logger.error("v-else/v-else-if has no adjacent v-if or v-else-if.", {
|
|
5095
5870
|
source,
|
|
5096
5871
|
file: filename,
|
|
5097
|
-
loc:
|
|
5872
|
+
loc: directive.loc
|
|
5098
5873
|
});
|
|
5099
5874
|
return hasError;
|
|
5100
5875
|
}
|
|
@@ -5110,8 +5885,8 @@ function resolveVIf(node, _ir, ctx, nodeIR, siblingNodesIR) {
|
|
|
5110
5885
|
}
|
|
5111
5886
|
|
|
5112
5887
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-memo.ts
|
|
5113
|
-
function resolveVMemo(
|
|
5114
|
-
const exp =
|
|
5888
|
+
function resolveVMemo(directive, _ir, ctx, nodeIR) {
|
|
5889
|
+
const exp = directive.exp;
|
|
5115
5890
|
let value = exp?.content;
|
|
5116
5891
|
if (value !== void 0) {
|
|
5117
5892
|
if (!value.trim() || !value.startsWith("[") && !value.endsWith("]")) {
|
|
@@ -5119,7 +5894,7 @@ function resolveVMemo(node, _ir, ctx, nodeIR) {
|
|
|
5119
5894
|
logger.warn(
|
|
5120
5895
|
"The expected value of v-memo is an array; otherwise, memoization will be skipped.",
|
|
5121
5896
|
{
|
|
5122
|
-
loc:
|
|
5897
|
+
loc: directive.loc,
|
|
5123
5898
|
source,
|
|
5124
5899
|
file: filename
|
|
5125
5900
|
}
|
|
@@ -5143,15 +5918,15 @@ function resolveVMemo(node, _ir, ctx, nodeIR) {
|
|
|
5143
5918
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-model.ts
|
|
5144
5919
|
import {
|
|
5145
5920
|
ElementTypes,
|
|
5146
|
-
NodeTypes as
|
|
5921
|
+
NodeTypes as NodeTypes8
|
|
5147
5922
|
} from "@vue/compiler-core";
|
|
5148
|
-
function resolveVModel(
|
|
5149
|
-
const arg =
|
|
5150
|
-
const exp =
|
|
5151
|
-
const modifiers =
|
|
5923
|
+
function resolveVModel(directive, ir, ctx, vueNode, nodeIR) {
|
|
5924
|
+
const arg = directive.arg;
|
|
5925
|
+
const exp = directive.exp;
|
|
5926
|
+
const modifiers = directive.modifiers.map((item) => item.content);
|
|
5152
5927
|
const getterName = exp.content;
|
|
5153
|
-
const isComponent =
|
|
5154
|
-
const inputType = resolveHtmlInput(
|
|
5928
|
+
const isComponent = vueNode.tagType === ElementTypes.COMPONENT;
|
|
5929
|
+
const inputType = resolveHtmlInput(vueNode, isComponent);
|
|
5155
5930
|
const propName = arg?.content ?? resolveModelPropName(inputType, isComponent);
|
|
5156
5931
|
let valuePropIR;
|
|
5157
5932
|
let eventPropIR;
|
|
@@ -5167,7 +5942,7 @@ function resolveVModel(node, _ir, ctx, elementNode, nodeIR) {
|
|
|
5167
5942
|
eventPropIR.type = 3 /* EVENT */;
|
|
5168
5943
|
} else {
|
|
5169
5944
|
if (inputType === "radio") {
|
|
5170
|
-
const radioValue = getRadioValue(
|
|
5945
|
+
const radioValue = getRadioValue(vueNode);
|
|
5171
5946
|
valuePropIR = createPropsIR("v-model", "checked", `${getterName} === ${radioValue}`);
|
|
5172
5947
|
const processedValue = applyValueModifiers(radioValue, modifiers);
|
|
5173
5948
|
const handlerBody = `() => { ${getterName} = ${processedValue} }`;
|
|
@@ -5193,9 +5968,9 @@ function resolveModelPropName(inputType, isComponent = false) {
|
|
|
5193
5968
|
}
|
|
5194
5969
|
return !isComponent ? "value" : "modelValue";
|
|
5195
5970
|
}
|
|
5196
|
-
function getRadioValue(
|
|
5197
|
-
const valueAttr =
|
|
5198
|
-
(prop) => prop.type ===
|
|
5971
|
+
function getRadioValue(vueNode) {
|
|
5972
|
+
const valueAttr = vueNode.props.find(
|
|
5973
|
+
(prop) => prop.type === NodeTypes8.ATTRIBUTE && prop.name === "value"
|
|
5199
5974
|
);
|
|
5200
5975
|
if (!valueAttr?.value?.content) return '""';
|
|
5201
5976
|
const content = valueAttr.value.content;
|
|
@@ -5205,7 +5980,7 @@ function resolveHtmlInput(node, isComponent) {
|
|
|
5205
5980
|
if (isComponent) return;
|
|
5206
5981
|
if (node.tag !== "input") return node.tag;
|
|
5207
5982
|
const typeProp = node.props.find(
|
|
5208
|
-
(prop) => prop.type ===
|
|
5983
|
+
(prop) => prop.type === NodeTypes8.ATTRIBUTE && prop.name === "type"
|
|
5209
5984
|
);
|
|
5210
5985
|
return typeProp?.value?.content?.toLowerCase();
|
|
5211
5986
|
}
|
|
@@ -5221,14 +5996,14 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
5221
5996
|
}
|
|
5222
5997
|
|
|
5223
5998
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
5224
|
-
import * as
|
|
5225
|
-
function resolveVOn(
|
|
5226
|
-
const arg =
|
|
5227
|
-
const exp =
|
|
5228
|
-
const modifiers =
|
|
5999
|
+
import * as t47 from "@babel/types";
|
|
6000
|
+
function resolveVOn(directive, ir, ctx, nodeIR) {
|
|
6001
|
+
const arg = directive.arg;
|
|
6002
|
+
const exp = directive.exp;
|
|
6003
|
+
const modifiers = directive.modifiers.map((item) => item.content);
|
|
5229
6004
|
const captureIndex = modifiers.findIndex((modifier) => modifier === "capture");
|
|
5230
6005
|
let eventName = normalizeVOnEventName(arg.content);
|
|
5231
|
-
let handler =
|
|
6006
|
+
let handler = resolveSpecialExpression(exp.content.trim(), ctx);
|
|
5232
6007
|
if (captureIndex > -1) {
|
|
5233
6008
|
eventName = modifiers[captureIndex] ? `${eventName}Capture` : eventName;
|
|
5234
6009
|
modifiers.splice(captureIndex, 1);
|
|
@@ -5238,11 +6013,11 @@ function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
|
5238
6013
|
originalVueEventName = `${arg.content}.${modifiers.join(".")}`;
|
|
5239
6014
|
} else {
|
|
5240
6015
|
const expr = stringToExpr(handler);
|
|
5241
|
-
if (!
|
|
6016
|
+
if (!t47.isFunctionExpression(expr) && !t47.isArrowFunctionExpression(expr) && !t47.isIdentifier(expr)) {
|
|
5242
6017
|
handler = `() => {${handler}}`;
|
|
5243
6018
|
}
|
|
5244
6019
|
}
|
|
5245
|
-
const eventIR = createPropsIR(
|
|
6020
|
+
const eventIR = createPropsIR(directive.rawName, eventName, handler);
|
|
5246
6021
|
eventIR.type = 3 /* EVENT */;
|
|
5247
6022
|
eventIR.isStatic = arg.isStatic;
|
|
5248
6023
|
eventIR.modifiers = modifiers;
|
|
@@ -5263,8 +6038,8 @@ function normalizeVOnEventName(rawEventName) {
|
|
|
5263
6038
|
}
|
|
5264
6039
|
|
|
5265
6040
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-show.ts
|
|
5266
|
-
function resolveVShow(
|
|
5267
|
-
const exp =
|
|
6041
|
+
function resolveVShow(directive, ir, ctx, nodeIR) {
|
|
6042
|
+
const exp = directive.exp;
|
|
5268
6043
|
const test = exp.content;
|
|
5269
6044
|
const showIR = createPropsIR("v-show", "style", `{display: ${test} ? '' : 'none'}`);
|
|
5270
6045
|
resolvePropAsBabelExp(showIR, ctx);
|
|
@@ -5284,6 +6059,29 @@ function resolveVShow(node, _ir, ctx, nodeIR) {
|
|
|
5284
6059
|
nodeIR.props.push(showIR);
|
|
5285
6060
|
}
|
|
5286
6061
|
|
|
6062
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-slot-prop.ts
|
|
6063
|
+
function resolveVSlotProp(directive, ir, ctx) {
|
|
6064
|
+
const arg = directive.arg;
|
|
6065
|
+
const exp = directive.exp;
|
|
6066
|
+
const isScoped = exp !== void 0;
|
|
6067
|
+
const name = !arg || arg.content === "default" ? "children" : arg.content;
|
|
6068
|
+
const content = !isScoped ? [] : void 0;
|
|
6069
|
+
const callback = isScoped ? {
|
|
6070
|
+
arg: exp?.content?.trim() ?? "",
|
|
6071
|
+
exp: []
|
|
6072
|
+
} : void 0;
|
|
6073
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
6074
|
+
return {
|
|
6075
|
+
type: 2 /* SLOT */,
|
|
6076
|
+
name,
|
|
6077
|
+
rawName: directive.rawName ?? "default",
|
|
6078
|
+
isStatic: arg?.isStatic ?? true,
|
|
6079
|
+
isScoped,
|
|
6080
|
+
content,
|
|
6081
|
+
callback
|
|
6082
|
+
};
|
|
6083
|
+
}
|
|
6084
|
+
|
|
5287
6085
|
// src/core/transform/sfc/template/shared/node-ir-utils.ts
|
|
5288
6086
|
import { isVSlot as isCompilerVSlot } from "@vue/compiler-core";
|
|
5289
6087
|
function createInterpolationNodeIR(content) {
|
|
@@ -5306,126 +6104,13 @@ function isTemplateVSlotNode(node) {
|
|
|
5306
6104
|
}
|
|
5307
6105
|
|
|
5308
6106
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-text.ts
|
|
5309
|
-
function resolveVText(
|
|
5310
|
-
const exp =
|
|
6107
|
+
function resolveVText(directive, ir, ctx, nodeIR) {
|
|
6108
|
+
const exp = directive.exp;
|
|
5311
6109
|
const interpolationIR = createInterpolationNodeIR(exp.content);
|
|
5312
6110
|
interpolationIR.babelExp = resolveStringExpr(exp.content, ctx);
|
|
5313
6111
|
nodeIR.children = [interpolationIR];
|
|
5314
6112
|
}
|
|
5315
6113
|
|
|
5316
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-directive-prop.ts
|
|
5317
|
-
var SUPPORTED_DIRECTIVES = /* @__PURE__ */ new Set([
|
|
5318
|
-
"text",
|
|
5319
|
-
"html",
|
|
5320
|
-
"show",
|
|
5321
|
-
"if",
|
|
5322
|
-
"else",
|
|
5323
|
-
"else-if",
|
|
5324
|
-
"for",
|
|
5325
|
-
"on",
|
|
5326
|
-
"once",
|
|
5327
|
-
"bind",
|
|
5328
|
-
"model",
|
|
5329
|
-
"cloak",
|
|
5330
|
-
"slot",
|
|
5331
|
-
"memo",
|
|
5332
|
-
"is"
|
|
5333
|
-
]);
|
|
5334
|
-
function resolveDirectiveProp(node, ir, ctx, elementNode, nodeIR, siblingNodesIR) {
|
|
5335
|
-
const { name, rawName } = node;
|
|
5336
|
-
if (!SUPPORTED_DIRECTIVES.has(name)) {
|
|
5337
|
-
warnUnsupportedDirective(ctx, node.loc, rawName);
|
|
5338
|
-
return;
|
|
5339
|
-
}
|
|
5340
|
-
warnUnsupportedVueDollarVar(ctx, node);
|
|
5341
|
-
if (isVConditional(rawName)) {
|
|
5342
|
-
return resolveVIf(node, ir, ctx, nodeIR, siblingNodesIR);
|
|
5343
|
-
}
|
|
5344
|
-
function processExactDirectives() {
|
|
5345
|
-
switch (node.rawName) {
|
|
5346
|
-
case "v-html":
|
|
5347
|
-
resolveVHtml(node, ir, ctx, nodeIR);
|
|
5348
|
-
return true;
|
|
5349
|
-
case "v-text":
|
|
5350
|
-
resolveVText(node, ir, ctx, nodeIR);
|
|
5351
|
-
return true;
|
|
5352
|
-
case "v-once":
|
|
5353
|
-
case "v-memo":
|
|
5354
|
-
return resolveVMemo(node, ir, ctx, nodeIR);
|
|
5355
|
-
case "v-show":
|
|
5356
|
-
return resolveVShow(node, ir, ctx, nodeIR);
|
|
5357
|
-
case "v-for":
|
|
5358
|
-
return resolveVFor(node, ir, ctx, nodeIR);
|
|
5359
|
-
}
|
|
5360
|
-
}
|
|
5361
|
-
function processRangeDirectives() {
|
|
5362
|
-
const { rawName: rawName2 } = node;
|
|
5363
|
-
if (isVModel(rawName2)) {
|
|
5364
|
-
return resolveVModel(node, ir, ctx, elementNode, nodeIR);
|
|
5365
|
-
}
|
|
5366
|
-
if (isVBind(rawName2)) {
|
|
5367
|
-
return resolveDynamicAttributeProp(node, ir, ctx, nodeIR);
|
|
5368
|
-
}
|
|
5369
|
-
if (isVOn(rawName2)) {
|
|
5370
|
-
return resolveVOn(node, ir, ctx, nodeIR);
|
|
5371
|
-
}
|
|
5372
|
-
if (isVSlot(rawName2)) {
|
|
5373
|
-
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
5374
|
-
resolveRouterLinkVSlotProp(node, nodeIR, ctx);
|
|
5375
|
-
}
|
|
5376
|
-
return true;
|
|
5377
|
-
}
|
|
5378
|
-
}
|
|
5379
|
-
return processExactDirectives() || processRangeDirectives();
|
|
5380
|
-
}
|
|
5381
|
-
|
|
5382
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-props.ts
|
|
5383
|
-
import { NodeTypes as NodeTypes7 } from "@vue/compiler-core";
|
|
5384
|
-
function resolveProps(node, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5385
|
-
for (const prop of node.props) {
|
|
5386
|
-
if (prop.type === NodeTypes7.ATTRIBUTE) {
|
|
5387
|
-
resolveAttributeProp(prop, ir, ctx, nodeIR);
|
|
5388
|
-
continue;
|
|
5389
|
-
}
|
|
5390
|
-
if (prop.type === NodeTypes7.DIRECTIVE) {
|
|
5391
|
-
const stop = resolveDirectiveProp(
|
|
5392
|
-
prop,
|
|
5393
|
-
ir,
|
|
5394
|
-
ctx,
|
|
5395
|
-
node,
|
|
5396
|
-
nodeIR,
|
|
5397
|
-
siblingNodesIR
|
|
5398
|
-
);
|
|
5399
|
-
if (stop) {
|
|
5400
|
-
break;
|
|
5401
|
-
}
|
|
5402
|
-
}
|
|
5403
|
-
}
|
|
5404
|
-
}
|
|
5405
|
-
|
|
5406
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-slot-prop.ts
|
|
5407
|
-
function resolveVSlotProp(node, _ir, ctx) {
|
|
5408
|
-
const arg = node.arg;
|
|
5409
|
-
const exp = node.exp;
|
|
5410
|
-
const isScoped = exp !== void 0;
|
|
5411
|
-
const name = !arg || arg.content === "default" ? "children" : arg.content;
|
|
5412
|
-
const content = !isScoped ? [] : void 0;
|
|
5413
|
-
const callback = isScoped ? {
|
|
5414
|
-
arg: exp?.content?.trim() ?? "",
|
|
5415
|
-
exp: []
|
|
5416
|
-
} : void 0;
|
|
5417
|
-
checkPropIsDynamicKey(ctx, node);
|
|
5418
|
-
return {
|
|
5419
|
-
type: 2 /* SLOT */,
|
|
5420
|
-
name,
|
|
5421
|
-
rawName: node.rawName ?? "default",
|
|
5422
|
-
isStatic: arg?.isStatic ?? true,
|
|
5423
|
-
isScoped,
|
|
5424
|
-
content,
|
|
5425
|
-
callback
|
|
5426
|
-
};
|
|
5427
|
-
}
|
|
5428
|
-
|
|
5429
6114
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-comment-node.ts
|
|
5430
6115
|
function resolveCommentNode(node, _ir, ctx, childrenIR) {
|
|
5431
6116
|
const nodeIR = createTextNodeIR(node.content, true);
|
|
@@ -5485,7 +6170,7 @@ function resolveInterpolationNode(node, _ir, ctx, childrenIR) {
|
|
|
5485
6170
|
}
|
|
5486
6171
|
|
|
5487
6172
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-slot-outlet-node.ts
|
|
5488
|
-
import { NodeTypes as
|
|
6173
|
+
import { NodeTypes as NodeTypes10 } from "@vue/compiler-core";
|
|
5489
6174
|
|
|
5490
6175
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
5491
6176
|
import {
|
|
@@ -5494,11 +6179,11 @@ import {
|
|
|
5494
6179
|
} from "@vue/compiler-core";
|
|
5495
6180
|
|
|
5496
6181
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-v-slot-node.ts
|
|
5497
|
-
import { NodeTypes as
|
|
6182
|
+
import { NodeTypes as NodeTypes9 } from "@vue/compiler-core";
|
|
5498
6183
|
function resolveTemplateVSlotNode(node, ir, ctx, nodeIR) {
|
|
5499
6184
|
let slotIR = {};
|
|
5500
6185
|
for (const prop of node.props) {
|
|
5501
|
-
if (prop.type ===
|
|
6186
|
+
if (prop.type === NodeTypes9.DIRECTIVE) {
|
|
5502
6187
|
slotIR = resolveVSlotProp(prop, ir, ctx);
|
|
5503
6188
|
}
|
|
5504
6189
|
}
|
|
@@ -5519,7 +6204,11 @@ function resolveTextNode(node, _ir, _ctx, childrenIR) {
|
|
|
5519
6204
|
}
|
|
5520
6205
|
|
|
5521
6206
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
6207
|
+
var isRootMarked = false;
|
|
5522
6208
|
function resolveTemplateChildren(node, nodeIR, ctx) {
|
|
6209
|
+
if (!isRootMarked) {
|
|
6210
|
+
isRootMarked = true;
|
|
6211
|
+
}
|
|
5523
6212
|
resolveChildNodes(node, nodeIR, ctx, null, nodeIR.children);
|
|
5524
6213
|
}
|
|
5525
6214
|
function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
@@ -5536,6 +6225,7 @@ function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
|
5536
6225
|
continue;
|
|
5537
6226
|
}
|
|
5538
6227
|
const elementIR = resolveElementNode(child, nodeIR, ctx, childrenIR);
|
|
6228
|
+
markRootNode(elementIR);
|
|
5539
6229
|
childrenIR.push(elementIR);
|
|
5540
6230
|
if (child.children.length) {
|
|
5541
6231
|
resolveChildNodes(child, nodeIR, ctx, elementIR, elementIR.children);
|
|
@@ -5556,6 +6246,12 @@ function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
|
5556
6246
|
}
|
|
5557
6247
|
return nodeIR;
|
|
5558
6248
|
}
|
|
6249
|
+
function markRootNode(nodeIR) {
|
|
6250
|
+
if (isRootMarked) {
|
|
6251
|
+
nodeIR.isRoot = isRootMarked;
|
|
6252
|
+
isRootMarked = void 0;
|
|
6253
|
+
}
|
|
6254
|
+
}
|
|
5559
6255
|
|
|
5560
6256
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-slot-outlet-node.ts
|
|
5561
6257
|
function resolveSlotOutletNode(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
@@ -5587,7 +6283,7 @@ function resolveSlotProps(node, ctx) {
|
|
|
5587
6283
|
});
|
|
5588
6284
|
};
|
|
5589
6285
|
for (const prop of node.props) {
|
|
5590
|
-
if (prop.type ===
|
|
6286
|
+
if (prop.type === NodeTypes10.ATTRIBUTE) {
|
|
5591
6287
|
const attr = prop.name;
|
|
5592
6288
|
const value = prop.value?.content.trim();
|
|
5593
6289
|
if (attr === "name" && value) {
|
|
@@ -5597,7 +6293,7 @@ function resolveSlotProps(node, ctx) {
|
|
|
5597
6293
|
}
|
|
5598
6294
|
continue;
|
|
5599
6295
|
}
|
|
5600
|
-
if (prop.type ===
|
|
6296
|
+
if (prop.type === NodeTypes10.DIRECTIVE) {
|
|
5601
6297
|
const arg = prop.arg;
|
|
5602
6298
|
const exp = prop.exp;
|
|
5603
6299
|
warnUnsupportedVueDollarVar(ctx, prop);
|
|
@@ -5629,7 +6325,7 @@ function resolveSlotNode(parentIR, node, nodeIR, slotContext, ctx) {
|
|
|
5629
6325
|
}
|
|
5630
6326
|
if (fallback.length === 1) {
|
|
5631
6327
|
const child = fallback[0];
|
|
5632
|
-
if (child.type ===
|
|
6328
|
+
if (child.type === NodeTypes10.TEXT) {
|
|
5633
6329
|
fallbackResolved = true;
|
|
5634
6330
|
expression += ` ?? ${JSON.stringify(child.content.trim())}`;
|
|
5635
6331
|
}
|
|
@@ -5691,7 +6387,7 @@ function transform(ast, ctx, options) {
|
|
|
5691
6387
|
}
|
|
5692
6388
|
|
|
5693
6389
|
// package.json
|
|
5694
|
-
var version = "1.
|
|
6390
|
+
var version = "1.6.0";
|
|
5695
6391
|
var bin = {
|
|
5696
6392
|
vureact: "./bin/vureact.js"
|
|
5697
6393
|
};
|
|
@@ -6308,18 +7004,19 @@ var CompilationContext = class {
|
|
|
6308
7004
|
fileId: "",
|
|
6309
7005
|
source: "",
|
|
6310
7006
|
filename: "",
|
|
6311
|
-
compName: "",
|
|
6312
7007
|
imports: /* @__PURE__ */ new Map(),
|
|
6313
7008
|
cssVars: [],
|
|
6314
7009
|
inputType: "sfc",
|
|
6315
|
-
propField: "
|
|
7010
|
+
propField: "props",
|
|
6316
7011
|
templateData: {
|
|
6317
7012
|
slots: {},
|
|
6318
7013
|
refBindings: {
|
|
6319
7014
|
domRefs: {},
|
|
6320
7015
|
componentRefs: {}
|
|
6321
7016
|
},
|
|
6322
|
-
reactiveBindings: {}
|
|
7017
|
+
reactiveBindings: {},
|
|
7018
|
+
declaredProps: /* @__PURE__ */ new Set(),
|
|
7019
|
+
declaredEmits: /* @__PURE__ */ new Set()
|
|
6323
7020
|
},
|
|
6324
7021
|
scriptData: {
|
|
6325
7022
|
lang: "js",
|
|
@@ -6339,6 +7036,7 @@ var CompilationContext = class {
|
|
|
6339
7036
|
enabled: false,
|
|
6340
7037
|
refField: "expose"
|
|
6341
7038
|
},
|
|
7039
|
+
declaredOptions: {},
|
|
6342
7040
|
source: ""
|
|
6343
7041
|
},
|
|
6344
7042
|
styleData: {
|