@vureact/compiler-core 1.5.2 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +9 -12
- package/README.md +6 -10
- package/lib/{chunk-UYTNAOAJ.esm.js → chunk-NXCZJT5W.esm.js} +1962 -1257
- package/lib/{chunk-37GGS4UW.js → chunk-ZOTZLL4U.js} +2058 -1353
- 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.1
|
|
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 = {
|
|
@@ -307,6 +324,11 @@ var ADAPTER_RULES = {
|
|
|
307
324
|
target: "nextTick",
|
|
308
325
|
package: PACKAGE_NAME.runtime,
|
|
309
326
|
type: "rename"
|
|
327
|
+
},
|
|
328
|
+
defineAsyncComponent: {
|
|
329
|
+
target: "defineAsyncComponent",
|
|
330
|
+
package: PACKAGE_NAME.runtime,
|
|
331
|
+
type: "rename"
|
|
310
332
|
}
|
|
311
333
|
},
|
|
312
334
|
// =================== [VuReact Router] ===================
|
|
@@ -392,7 +414,8 @@ var VUE_API_MAP = {
|
|
|
392
414
|
defineAsyncComponent: "defineAsyncComponent",
|
|
393
415
|
DynamicComponent: "Component",
|
|
394
416
|
Transition: "Transition",
|
|
395
|
-
RouterLink: "RouterLink"
|
|
417
|
+
RouterLink: "RouterLink",
|
|
418
|
+
useAttrs: "useAttrs"
|
|
396
419
|
};
|
|
397
420
|
|
|
398
421
|
// src/shared/babel-utils.ts
|
|
@@ -572,34 +595,31 @@ var capitalize = (str) => {
|
|
|
572
595
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
573
596
|
};
|
|
574
597
|
|
|
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
|
-
}
|
|
598
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/resolve-emit-call.ts
|
|
581
599
|
function resolveEmitsCalls(input, ctx) {
|
|
582
|
-
const
|
|
600
|
+
const { reactiveBindings } = ctx.templateData;
|
|
601
|
+
const matchEmitCalls = () => {
|
|
602
|
+
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
603
|
+
if (!macroBinding) return null;
|
|
604
|
+
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
605
|
+
const regex = new RegExp(
|
|
606
|
+
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
607
|
+
// 可选的第二个参数
|
|
608
|
+
"s"
|
|
609
|
+
// s 标志让 . 匹配换行符
|
|
610
|
+
);
|
|
611
|
+
return input.trim().match(regex);
|
|
612
|
+
};
|
|
613
|
+
const result = matchEmitCalls();
|
|
583
614
|
if (!result) return input;
|
|
584
615
|
const [, , eventName, args] = result;
|
|
585
616
|
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
586
617
|
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);
|
|
618
|
+
return `${ctx.propField}.${event}`;
|
|
601
619
|
}
|
|
602
|
-
|
|
620
|
+
|
|
621
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/resolve-ref-access.ts
|
|
622
|
+
function resolveRefAccess(input, ctx) {
|
|
603
623
|
const { reactiveBindings } = ctx.templateData;
|
|
604
624
|
const addValueProperty = (input2, varName) => {
|
|
605
625
|
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -617,11 +637,20 @@ function resolveRefVariable(input, ctx) {
|
|
|
617
637
|
return input;
|
|
618
638
|
}
|
|
619
639
|
|
|
640
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/resolve-special-expression/index.ts
|
|
641
|
+
function resolveSpecialExpression(input, ctx) {
|
|
642
|
+
const resolver = [resolveEmitsCalls, resolveRefAccess];
|
|
643
|
+
input = resolver.reduce((result, fn) => fn(result, ctx), input);
|
|
644
|
+
return input;
|
|
645
|
+
}
|
|
646
|
+
|
|
620
647
|
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
621
648
|
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
622
|
-
if (toStrLiteral)
|
|
649
|
+
if (toStrLiteral) {
|
|
650
|
+
return t5.stringLiteral(input);
|
|
651
|
+
}
|
|
623
652
|
const { filename, scriptData } = ctx;
|
|
624
|
-
const newContent =
|
|
653
|
+
const newContent = resolveSpecialExpression(input, ctx);
|
|
625
654
|
try {
|
|
626
655
|
return stringToExpr(newContent, scriptData.lang, filename);
|
|
627
656
|
} catch {
|
|
@@ -1029,7 +1058,7 @@ function buildStandardProp(nodeIR) {
|
|
|
1029
1058
|
babelExp: { ast: keyAST },
|
|
1030
1059
|
value: {
|
|
1031
1060
|
content,
|
|
1032
|
-
isStringLiteral:
|
|
1061
|
+
isStringLiteral: isStringLiteral13,
|
|
1033
1062
|
babelExp: { ast: valueAST }
|
|
1034
1063
|
}
|
|
1035
1064
|
} = nodeIR;
|
|
@@ -1038,18 +1067,34 @@ function buildStandardProp(nodeIR) {
|
|
|
1038
1067
|
}
|
|
1039
1068
|
let value;
|
|
1040
1069
|
if (content !== "true") {
|
|
1041
|
-
value =
|
|
1070
|
+
value = isStringLiteral13 ? t9.stringLiteral(content) : buildJsxExpressionNode(valueAST);
|
|
1042
1071
|
}
|
|
1043
1072
|
return t9.jsxAttribute(keyAST, value);
|
|
1044
1073
|
}
|
|
1045
1074
|
|
|
1046
|
-
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node.ts
|
|
1075
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node/resolve-template-node.ts
|
|
1076
|
+
function resolveTemplateNode(nodeIR, children) {
|
|
1077
|
+
const hasCondition = !!nodeIR.meta?.condition;
|
|
1078
|
+
const hasLoop = !!nodeIR.meta?.loop?.isLoop;
|
|
1079
|
+
const hasSlot = nodeIR.props.some((p) => p.type === 2 /* SLOT */);
|
|
1080
|
+
if (hasCondition || hasLoop || hasSlot) {
|
|
1081
|
+
if (!children.length) {
|
|
1082
|
+
return buildFragmentNode([]);
|
|
1083
|
+
}
|
|
1084
|
+
if (children.length === 1) {
|
|
1085
|
+
return children[0];
|
|
1086
|
+
}
|
|
1087
|
+
return buildFragmentNode(children);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node/index.ts
|
|
1047
1092
|
function buildElementNode(nodeIR, ctx) {
|
|
1048
1093
|
const mutableNodeIR = nodeIR;
|
|
1049
1094
|
if (nodeIR.conditionIsHandled && !mutableNodeIR.__processing) {
|
|
1050
1095
|
return null;
|
|
1051
1096
|
}
|
|
1052
|
-
const meta = nodeIR
|
|
1097
|
+
const { meta } = nodeIR;
|
|
1053
1098
|
if (meta?.condition && !meta.condition.isHandled) {
|
|
1054
1099
|
return buildConditionNode(nodeIR, ctx);
|
|
1055
1100
|
}
|
|
@@ -1061,6 +1106,10 @@ function buildElementNode(nodeIR, ctx) {
|
|
|
1061
1106
|
}
|
|
1062
1107
|
const props = buildProps(nodeIR, ctx);
|
|
1063
1108
|
const children = buildJsxChildren(nodeIR.children, ctx);
|
|
1109
|
+
if (nodeIR.tag === "template") {
|
|
1110
|
+
const jsxChild = resolveTemplateNode(nodeIR, children);
|
|
1111
|
+
if (jsxChild) return jsxChild;
|
|
1112
|
+
}
|
|
1064
1113
|
return createJsxElement(nodeIR.tag, props, children, nodeIR.isSelfClosing);
|
|
1065
1114
|
}
|
|
1066
1115
|
|
|
@@ -1260,17 +1309,16 @@ function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
|
1260
1309
|
return t13.variableDeclaration("const", [t13.variableDeclarator(name, memoCall)]);
|
|
1261
1310
|
}
|
|
1262
1311
|
function resolveComponentName(ctx) {
|
|
1263
|
-
const { filename,
|
|
1264
|
-
let name =
|
|
1312
|
+
const { filename, scriptData } = ctx;
|
|
1313
|
+
let { name } = scriptData.declaredOptions;
|
|
1265
1314
|
if (!name) {
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
if (!compName) {
|
|
1270
|
-
logger.warn("Missing component name, it falls back to the filename. " + name, {
|
|
1315
|
+
const defaultName = basename(filename).split(".")[0] || `FC${genHashByXXH(filename)}`;
|
|
1316
|
+
name = capitalize(camelCase(defaultName));
|
|
1317
|
+
logger.warn(`Unnamed component detected. Using file name: <${name}>`, {
|
|
1271
1318
|
file: filename
|
|
1272
1319
|
});
|
|
1273
1320
|
}
|
|
1321
|
+
scriptData.declaredOptions.name = name;
|
|
1274
1322
|
return t13.identifier(name);
|
|
1275
1323
|
}
|
|
1276
1324
|
function resolveLocalStatements(local, jsx) {
|
|
@@ -1320,21 +1368,32 @@ function resolveForwardRef(body, ctx) {
|
|
|
1320
1368
|
}
|
|
1321
1369
|
function resolvePropsParam(ctx) {
|
|
1322
1370
|
const { propField, scriptData } = ctx;
|
|
1323
|
-
const { propsTSIface } = scriptData;
|
|
1371
|
+
const { propsTSIface, hasUseAttrsCall } = scriptData;
|
|
1324
1372
|
const propsIdentifier = t13.identifier(propField);
|
|
1325
1373
|
if (scriptData.lang.startsWith("js")) {
|
|
1326
|
-
if (propsTSIface.hasPropsInJsEnv) {
|
|
1374
|
+
if (propsTSIface.hasPropsInJsEnv || hasUseAttrsCall) {
|
|
1327
1375
|
return propsIdentifier;
|
|
1328
1376
|
}
|
|
1329
1377
|
return;
|
|
1330
1378
|
}
|
|
1331
1379
|
if (!propsTSIface.name) {
|
|
1380
|
+
if (hasUseAttrsCall) {
|
|
1381
|
+
return withPropsTypeAnnotation(propsIdentifier);
|
|
1382
|
+
}
|
|
1332
1383
|
return;
|
|
1333
1384
|
}
|
|
1334
|
-
const
|
|
1335
|
-
propsIdentifier
|
|
1385
|
+
const typeName = resolvePropsTypeName(propsTSIface.name, hasUseAttrsCall);
|
|
1386
|
+
return withPropsTypeAnnotation(propsIdentifier, typeName);
|
|
1387
|
+
}
|
|
1388
|
+
function withPropsTypeAnnotation(propsIdentifier, typeName = "Record<string, unknown>") {
|
|
1389
|
+
const typeRef = t13.tsTypeReference(t13.identifier(typeName));
|
|
1390
|
+
const typeAnnotation = t13.tsTypeAnnotation(typeRef);
|
|
1391
|
+
propsIdentifier.typeAnnotation = typeAnnotation;
|
|
1336
1392
|
return propsIdentifier;
|
|
1337
1393
|
}
|
|
1394
|
+
function resolvePropsTypeName(name, hasUseAttrsCall) {
|
|
1395
|
+
return typeof hasUseAttrsCall !== "undefined" ? `${name} & Record<string, unknown>` : name;
|
|
1396
|
+
}
|
|
1338
1397
|
|
|
1339
1398
|
// src/core/codegen/component/script/syntax-processor/process/build-program-preamble.ts
|
|
1340
1399
|
function buildProgramPreamble(nodeIR, ctx, state) {
|
|
@@ -1454,217 +1513,924 @@ import {
|
|
|
1454
1513
|
parse as parseVueSFC
|
|
1455
1514
|
} from "@vue/compiler-sfc";
|
|
1456
1515
|
|
|
1457
|
-
// src/core/parse/sfc/
|
|
1458
|
-
import { parse as babelParse2 } from "@babel/parser";
|
|
1459
|
-
function resolveScript(descriptor, ctx, parseResult) {
|
|
1460
|
-
if (descriptor.script) {
|
|
1461
|
-
throw new Error(
|
|
1462
|
-
`Traditional Vue <script> syntax is not supported. Please migrate to <script setup>.
|
|
1463
|
-
at <anonymous> (${ctx.filename})`
|
|
1464
|
-
);
|
|
1465
|
-
}
|
|
1466
|
-
const { scriptSetup } = descriptor;
|
|
1467
|
-
if (!scriptSetup) return null;
|
|
1468
|
-
if (scriptSetup?.warnings) {
|
|
1469
|
-
scriptSetup?.warnings.forEach((msg) => {
|
|
1470
|
-
logger.warn(msg, { file: ctx.filename });
|
|
1471
|
-
});
|
|
1472
|
-
}
|
|
1473
|
-
resolveContext(scriptSetup, ctx);
|
|
1474
|
-
const parseOpts = getBabelParseOptions(scriptSetup.lang, "script", ctx.filename);
|
|
1475
|
-
parseResult.script = {
|
|
1476
|
-
ast: babelParse2(scriptSetup.content, parseOpts),
|
|
1477
|
-
source: scriptSetup
|
|
1478
|
-
};
|
|
1479
|
-
}
|
|
1480
|
-
function resolveContext(scriptSetup, ctx) {
|
|
1481
|
-
let { content, lang } = scriptSetup;
|
|
1482
|
-
const resolveVRComment = (source) => {
|
|
1483
|
-
const regx = /\/\/\s*@vr-name:\s*(\w+)/;
|
|
1484
|
-
const nameMatch = source.match(regx);
|
|
1485
|
-
content = content.replace(regx, "");
|
|
1486
|
-
return nameMatch?.[1]?.trim() || "";
|
|
1487
|
-
};
|
|
1488
|
-
ctx.compName = resolveVRComment(content);
|
|
1489
|
-
ctx.scriptData.source = content;
|
|
1490
|
-
ctx.scriptData.lang = lang || "js";
|
|
1491
|
-
scriptSetup.content = content;
|
|
1492
|
-
}
|
|
1493
|
-
|
|
1494
|
-
// src/core/parse/sfc/process/resolve-script-meta.ts
|
|
1516
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/index.ts
|
|
1495
1517
|
import { traverse } from "@babel/core";
|
|
1496
|
-
import * as t15 from "@babel/types";
|
|
1497
|
-
|
|
1498
|
-
// src/consts/html-tag-types.ts
|
|
1499
|
-
var HTML_TAG_TYPES = {
|
|
1500
|
-
a: "HTMLAnchorElement",
|
|
1501
|
-
p: "HTMLParagraphElement",
|
|
1502
|
-
div: "HTMLDivElement",
|
|
1503
|
-
span: "HTMLSpanElement",
|
|
1504
|
-
h1: "HTMLHeadingElement",
|
|
1505
|
-
h2: "HTMLHeadingElement",
|
|
1506
|
-
h3: "HTMLHeadingElement",
|
|
1507
|
-
h4: "HTMLHeadingElement",
|
|
1508
|
-
h5: "HTMLHeadingElement",
|
|
1509
|
-
h6: "HTMLHeadingElement",
|
|
1510
|
-
button: "HTMLButtonElement",
|
|
1511
|
-
input: "HTMLInputElement",
|
|
1512
|
-
form: "HTMLFormElement",
|
|
1513
|
-
textarea: "HTMLTextAreaElement",
|
|
1514
|
-
select: "HTMLSelectElement",
|
|
1515
|
-
option: "HTMLOptionElement",
|
|
1516
|
-
label: "HTMLLabelElement",
|
|
1517
|
-
fieldset: "HTMLFieldSetElement",
|
|
1518
|
-
legend: "HTMLLegendElement",
|
|
1519
|
-
img: "HTMLImageElement",
|
|
1520
|
-
video: "HTMLVideoElement",
|
|
1521
|
-
audio: "HTMLAudioElement",
|
|
1522
|
-
canvas: "HTMLCanvasElement",
|
|
1523
|
-
picture: "HTMLPictureElement",
|
|
1524
|
-
source: "HTMLSourceElement",
|
|
1525
|
-
track: "HTMLTrackElement",
|
|
1526
|
-
ul: "HTMLUListElement",
|
|
1527
|
-
ol: "HTMLOListElement",
|
|
1528
|
-
li: "HTMLLIElement",
|
|
1529
|
-
dl: "HTMLDListElement",
|
|
1530
|
-
table: "HTMLTableElement",
|
|
1531
|
-
thead: "HTMLTableSectionElement",
|
|
1532
|
-
tbody: "HTMLTableSectionElement",
|
|
1533
|
-
tfoot: "HTMLTableSectionElement",
|
|
1534
|
-
tr: "HTMLTableRowElement",
|
|
1535
|
-
td: "HTMLTableCellElement",
|
|
1536
|
-
th: "HTMLTableCellElement",
|
|
1537
|
-
caption: "HTMLTableCaptionElement",
|
|
1538
|
-
col: "HTMLTableColElement",
|
|
1539
|
-
colgroup: "HTMLTableColElement",
|
|
1540
|
-
html: "HTMLHtmlElement",
|
|
1541
|
-
head: "HTMLHeadElement",
|
|
1542
|
-
body: "HTMLBodyElement",
|
|
1543
|
-
title: "HTMLTitleElement",
|
|
1544
|
-
meta: "HTMLMetaElement",
|
|
1545
|
-
link: "HTMLLinkElement",
|
|
1546
|
-
style: "HTMLStyleElement",
|
|
1547
|
-
script: "HTMLScriptElement",
|
|
1548
|
-
base: "HTMLBaseElement",
|
|
1549
|
-
br: "HTMLBRElement",
|
|
1550
|
-
hr: "HTMLHRElement",
|
|
1551
|
-
del: "HTMLModElement",
|
|
1552
|
-
ins: "HTMLModElement",
|
|
1553
|
-
iframe: "HTMLIFrameElement",
|
|
1554
|
-
embed: "HTMLEmbedElement",
|
|
1555
|
-
object: "HTMLObjectElement",
|
|
1556
|
-
param: "HTMLParamElement",
|
|
1557
|
-
details: "HTMLDetailsElement",
|
|
1558
|
-
dialog: "HTMLDialogElement",
|
|
1559
|
-
menu: "HTMLMenuElement",
|
|
1560
|
-
progress: "HTMLProgressElement",
|
|
1561
|
-
meter: "HTMLMeterElement",
|
|
1562
|
-
output: "HTMLOutputElement",
|
|
1563
|
-
frame: "HTMLFrameElement"
|
|
1564
|
-
};
|
|
1565
1518
|
|
|
1566
|
-
// src/
|
|
1567
|
-
|
|
1568
|
-
ref: "ref",
|
|
1569
|
-
toRef: "ref",
|
|
1570
|
-
toRefs: "ref",
|
|
1571
|
-
customRef: "ref",
|
|
1572
|
-
shallowRef: "ref",
|
|
1573
|
-
reactive: "reactive",
|
|
1574
|
-
shallowReactive: "reactive",
|
|
1575
|
-
computed: "ref",
|
|
1576
|
-
readonly: "reactive",
|
|
1577
|
-
shallowReadonly: "reactive",
|
|
1578
|
-
provide: "reactive",
|
|
1579
|
-
inject: "reactive",
|
|
1580
|
-
defineProps: "reactive",
|
|
1581
|
-
defineEmits: "reactive",
|
|
1582
|
-
useRoute: "reactive",
|
|
1583
|
-
useRouter: "reactive",
|
|
1584
|
-
useLink: "reactive"
|
|
1585
|
-
};
|
|
1519
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-options.ts
|
|
1520
|
+
import * as t16 from "@babel/types";
|
|
1586
1521
|
|
|
1587
|
-
// src/shared/
|
|
1588
|
-
|
|
1589
|
-
|
|
1522
|
+
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
1523
|
+
import * as t15 from "@babel/types";
|
|
1524
|
+
function findRootVariablePath(path9) {
|
|
1525
|
+
const rootId = findRootIdentifier(path9.node);
|
|
1526
|
+
if (!rootId?.name) return null;
|
|
1527
|
+
const binding = path9.scope.getBinding(rootId.name);
|
|
1528
|
+
if (!binding) return null;
|
|
1529
|
+
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
1530
|
+
return rootPath;
|
|
1590
1531
|
}
|
|
1591
|
-
function
|
|
1592
|
-
|
|
1532
|
+
function findRootIdentifier(node) {
|
|
1533
|
+
let current = node.object;
|
|
1534
|
+
while (t15.isMemberExpression(current) || t15.isOptionalMemberExpression(current)) {
|
|
1535
|
+
current = current.object;
|
|
1536
|
+
}
|
|
1537
|
+
return t15.isIdentifier(current) ? current : null;
|
|
1593
1538
|
}
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
traverse(scriptAST, {
|
|
1600
|
-
VariableDeclarator(path9) {
|
|
1601
|
-
const { node } = path9;
|
|
1602
|
-
if (!atComponentOrHookRoot(path9, scriptAST.program) || !t15.isIdentifier(node.id)) {
|
|
1603
|
-
return;
|
|
1604
|
-
}
|
|
1605
|
-
if (node.init && t15.isCallExpression(node.init) && t15.isIdentifier(node.init.callee)) {
|
|
1606
|
-
collectReactiveBindings(node, ctx);
|
|
1607
|
-
collectRefBindings(node, ctx);
|
|
1608
|
-
}
|
|
1609
|
-
}
|
|
1610
|
-
});
|
|
1539
|
+
function getVariableDeclaratorPath(path9) {
|
|
1540
|
+
if (path9.isVariableDeclarator()) {
|
|
1541
|
+
return path9;
|
|
1542
|
+
}
|
|
1543
|
+
return path9.findParent((p) => p.isVariableDeclarator());
|
|
1611
1544
|
}
|
|
1612
|
-
function
|
|
1613
|
-
const
|
|
1614
|
-
const
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
const source = callee.name;
|
|
1618
|
-
if (!reactiveStateApis.has(source)) return;
|
|
1619
|
-
const varName = node.id.name;
|
|
1620
|
-
const value = init.arguments[0];
|
|
1621
|
-
if (source === MACRO_API_NAMES.props) {
|
|
1622
|
-
ctx.propField = varName;
|
|
1545
|
+
function isVariableDeclTopLevel(path9) {
|
|
1546
|
+
const variableDeclaratorPath = path9;
|
|
1547
|
+
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
1548
|
+
if (!variableDeclarationPath) {
|
|
1549
|
+
return false;
|
|
1623
1550
|
}
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1551
|
+
if (variableDeclarationPath.isProgram()) {
|
|
1552
|
+
return true;
|
|
1553
|
+
}
|
|
1554
|
+
const variableDeclarationParentPath = variableDeclarationPath.parentPath;
|
|
1555
|
+
if (variableDeclarationParentPath && variableDeclarationParentPath.isProgram()) {
|
|
1556
|
+
return true;
|
|
1557
|
+
}
|
|
1558
|
+
return false;
|
|
1630
1559
|
}
|
|
1631
|
-
function
|
|
1632
|
-
|
|
1633
|
-
const init = node.init;
|
|
1634
|
-
const callee = init.callee;
|
|
1635
|
-
if (callee.name !== VUE_API_MAP.useTemplateRef) return;
|
|
1636
|
-
const idName = node.id.name;
|
|
1637
|
-
const tag = init.arguments[0]?.value;
|
|
1638
|
-
refBindings.domRefs[idName] = {
|
|
1639
|
-
tag,
|
|
1640
|
-
htmlType: HTML_TAG_TYPES[tag] || "HTMLElement",
|
|
1641
|
-
name: idName
|
|
1642
|
-
};
|
|
1560
|
+
function isRealVariableAccess(path9) {
|
|
1561
|
+
return isIdentifierAccess(path9) && !isPropertyName(path9);
|
|
1643
1562
|
}
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
const
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1563
|
+
function isIdentifierAccess(path9) {
|
|
1564
|
+
if (isIdentifierDeclaration(path9)) {
|
|
1565
|
+
return false;
|
|
1566
|
+
}
|
|
1567
|
+
const binding = path9.scope.getBinding(path9.node.name);
|
|
1568
|
+
if (!binding) {
|
|
1569
|
+
return true;
|
|
1570
|
+
}
|
|
1571
|
+
return binding.identifier !== path9.node;
|
|
1572
|
+
}
|
|
1573
|
+
function isIdentifierDeclaration(path9) {
|
|
1574
|
+
const parent = path9.parentPath;
|
|
1575
|
+
if (!parent) return false;
|
|
1576
|
+
if (parent.isVariableDeclarator() && parent.node.id === path9.node) {
|
|
1577
|
+
return true;
|
|
1578
|
+
}
|
|
1579
|
+
if (parent.isFunctionDeclaration() && parent.node.id === path9.node) {
|
|
1580
|
+
return true;
|
|
1581
|
+
}
|
|
1582
|
+
if (parent.isFunctionExpression() && parent.node.id === path9.node) {
|
|
1583
|
+
return true;
|
|
1584
|
+
}
|
|
1585
|
+
if (parent.isClassDeclaration() && parent.node.id === path9.node) {
|
|
1586
|
+
return true;
|
|
1587
|
+
}
|
|
1588
|
+
if (parent.isImportSpecifier() && parent.node.local === path9.node) {
|
|
1589
|
+
return true;
|
|
1590
|
+
}
|
|
1591
|
+
if (parent.isImportDefaultSpecifier() && parent.node.local === path9.node) {
|
|
1592
|
+
return true;
|
|
1593
|
+
}
|
|
1594
|
+
if (parent.isImportNamespaceSpecifier() && parent.node.local === path9.node) {
|
|
1595
|
+
return true;
|
|
1596
|
+
}
|
|
1597
|
+
if (parent.isFunction() && parent.node.params.includes(path9.node)) {
|
|
1598
|
+
return true;
|
|
1599
|
+
}
|
|
1600
|
+
if (parent.isCatchClause() && parent.node.param === path9.node) {
|
|
1601
|
+
return true;
|
|
1602
|
+
}
|
|
1603
|
+
return false;
|
|
1604
|
+
}
|
|
1605
|
+
function isPropertyName(path9) {
|
|
1606
|
+
const parent = path9.parentPath;
|
|
1607
|
+
if (!parent) return false;
|
|
1608
|
+
if (parent.isObjectProperty() && parent.node.key === path9.node) {
|
|
1609
|
+
return true;
|
|
1610
|
+
}
|
|
1611
|
+
if (parent.isClassProperty() && parent.node.key === path9.node) {
|
|
1612
|
+
return true;
|
|
1613
|
+
}
|
|
1614
|
+
if (parent.isMemberExpression() && parent.node.property === path9.node) {
|
|
1615
|
+
return true;
|
|
1616
|
+
}
|
|
1617
|
+
return false;
|
|
1618
|
+
}
|
|
1619
|
+
function replaceCallName(callExp, identifierName) {
|
|
1620
|
+
const { callee } = callExp;
|
|
1621
|
+
if (!t15.isIdentifier(callee)) return;
|
|
1622
|
+
callee.name = identifierName;
|
|
1623
|
+
if (callee.loc) {
|
|
1624
|
+
callee.loc.identifierName = identifierName;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
function replaceIdName(id, newName) {
|
|
1628
|
+
if (!t15.isIdentifier(id)) return;
|
|
1629
|
+
id.name = newName;
|
|
1630
|
+
if (id.loc) {
|
|
1631
|
+
id.loc.identifierName = newName;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
1635
|
+
const { filename, scriptData } = ctx;
|
|
1636
|
+
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
1637
|
+
const ts = expressionToTSType(exp);
|
|
1638
|
+
return tsTypeAnnotation7 ? t15.tsTypeAnnotation(ts) : ts;
|
|
1639
|
+
}
|
|
1640
|
+
function expressionToTSType(exp) {
|
|
1641
|
+
if (t15.isStringLiteral(exp)) return t15.tsStringKeyword();
|
|
1642
|
+
if (t15.isNumericLiteral(exp)) return t15.tsNumberKeyword();
|
|
1643
|
+
if (t15.isBooleanLiteral(exp)) return t15.tsBooleanKeyword();
|
|
1644
|
+
if (t15.isArrayExpression(exp)) return t15.tsArrayType(t15.tsAnyKeyword());
|
|
1645
|
+
if (t15.isObjectExpression(exp)) {
|
|
1646
|
+
const members = [];
|
|
1647
|
+
for (const p of exp.properties) {
|
|
1648
|
+
if (!t15.isObjectProperty(p)) continue;
|
|
1649
|
+
let key;
|
|
1650
|
+
if (t15.isIdentifier(p.key)) key = p.key.name;
|
|
1651
|
+
else if (t15.isStringLiteral(p.key)) key = p.key.value;
|
|
1652
|
+
if (!key) continue;
|
|
1653
|
+
if (t15.isExpression(p.value)) {
|
|
1654
|
+
members.push(
|
|
1655
|
+
t15.tsPropertySignature(t15.identifier(key), t15.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
1656
|
+
);
|
|
1657
|
+
} else {
|
|
1658
|
+
members.push(
|
|
1659
|
+
t15.tsPropertySignature(t15.identifier(key), t15.tsTypeAnnotation(t15.tsAnyKeyword()))
|
|
1660
|
+
);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
return t15.tsTypeLiteral(members);
|
|
1664
|
+
}
|
|
1665
|
+
if (t15.isArrowFunctionExpression(exp) || t15.isFunctionExpression(exp)) {
|
|
1666
|
+
const params = exp.params.map((p, i) => {
|
|
1667
|
+
const id = t15.isIdentifier(p) ? t15.identifier(p.name) : t15.identifier(`arg${i}`);
|
|
1668
|
+
id.typeAnnotation = t15.tsTypeAnnotation(t15.tsAnyKeyword());
|
|
1669
|
+
return id;
|
|
1670
|
+
});
|
|
1671
|
+
let returnType = t15.tsAnyKeyword();
|
|
1672
|
+
if (t15.isBlockStatement(exp.body)) {
|
|
1673
|
+
for (const stmt of exp.body.body) {
|
|
1674
|
+
if (t15.isReturnStatement(stmt) && stmt.argument) {
|
|
1675
|
+
if (t15.isExpression(stmt.argument)) {
|
|
1676
|
+
returnType = expressionToTSType(stmt.argument);
|
|
1677
|
+
break;
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
} else if (t15.isExpression(exp.body)) {
|
|
1682
|
+
returnType = expressionToTSType(exp.body);
|
|
1683
|
+
}
|
|
1684
|
+
return t15.tsFunctionType(null, params, t15.tsTypeAnnotation(returnType));
|
|
1685
|
+
}
|
|
1686
|
+
return t15.tsAnyKeyword();
|
|
1687
|
+
}
|
|
1688
|
+
function isCalleeNamed(node, name) {
|
|
1689
|
+
if (!t15.isIdentifier(node.callee)) {
|
|
1690
|
+
return false;
|
|
1691
|
+
}
|
|
1692
|
+
return node.callee.name === name;
|
|
1693
|
+
}
|
|
1694
|
+
function isSimpleLiteral(node) {
|
|
1695
|
+
if (!node) return false;
|
|
1696
|
+
if (t15.isStringLiteral(node) || t15.isNumericLiteral(node) || t15.isBooleanLiteral(node) || t15.isNullLiteral(node) || t15.isRegExpLiteral(node) || t15.isBigIntLiteral(node) || t15.isDecimalLiteral(node)) {
|
|
1697
|
+
return true;
|
|
1698
|
+
}
|
|
1699
|
+
return false;
|
|
1700
|
+
}
|
|
1701
|
+
function forkNode(node, deep = true) {
|
|
1702
|
+
const newNode = t15.cloneNode(node, deep);
|
|
1703
|
+
newNode.leadingComments = node.leadingComments;
|
|
1704
|
+
newNode.innerComments = node.innerComments;
|
|
1705
|
+
newNode.trailingComments = null;
|
|
1706
|
+
cleanNodeLoc(node);
|
|
1707
|
+
cleanNodeComments(node);
|
|
1708
|
+
return newNode;
|
|
1709
|
+
}
|
|
1710
|
+
function replaceNode(path9, target, source) {
|
|
1711
|
+
const { start, end, loc, leadingComments, innerComments, trailingComments } = source;
|
|
1712
|
+
target.start = start;
|
|
1713
|
+
target.end = end;
|
|
1714
|
+
target.loc = loc;
|
|
1715
|
+
target.leadingComments = leadingComments;
|
|
1716
|
+
target.innerComments = innerComments;
|
|
1717
|
+
target.trailingComments = trailingComments;
|
|
1718
|
+
cleanNodeLoc(source);
|
|
1719
|
+
cleanNodeComments(source);
|
|
1720
|
+
path9.replaceWith(target);
|
|
1721
|
+
}
|
|
1722
|
+
function cleanNodeLoc(node) {
|
|
1723
|
+
node.start = null;
|
|
1724
|
+
node.end = null;
|
|
1725
|
+
node.loc = null;
|
|
1726
|
+
}
|
|
1727
|
+
function cleanNodeComments(node) {
|
|
1728
|
+
node.leadingComments = null;
|
|
1729
|
+
node.innerComments = null;
|
|
1730
|
+
node.trailingComments = null;
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-options.ts
|
|
1734
|
+
function resolveDeclaredOptions(path9, ctx) {
|
|
1735
|
+
const { node } = path9;
|
|
1736
|
+
const { filename, scriptData } = ctx;
|
|
1737
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1740
|
+
const [options] = node.arguments;
|
|
1741
|
+
if (!options || !t16.isObjectExpression(options)) {
|
|
1742
|
+
logger.warn(
|
|
1743
|
+
`Expected argument to be of a type ["ObjectExpression"] but instead got "${options?.type}".`,
|
|
1744
|
+
{
|
|
1745
|
+
file: filename,
|
|
1746
|
+
source: scriptData.source,
|
|
1747
|
+
loc: options?.loc || node.loc
|
|
1748
|
+
}
|
|
1749
|
+
);
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1752
|
+
const { source, declaredOptions } = scriptData;
|
|
1753
|
+
declaredOptions.inheritAttrs = true;
|
|
1754
|
+
for (const property of options.properties) {
|
|
1755
|
+
if (!t16.isObjectProperty(property) || !t16.isIdentifier(property.key) || property.computed) {
|
|
1756
|
+
continue;
|
|
1757
|
+
}
|
|
1758
|
+
const propKey = property.key;
|
|
1759
|
+
const propValue = property.value;
|
|
1760
|
+
if (propKey.name === "name") {
|
|
1761
|
+
if (!t16.isStringLiteral(propValue)) {
|
|
1762
|
+
logger.warn(
|
|
1763
|
+
`Expected property to be of a type ["StringLiteral"] but instead got "${propValue.type}".`,
|
|
1764
|
+
{
|
|
1765
|
+
source,
|
|
1766
|
+
file: filename,
|
|
1767
|
+
loc: propKey?.loc
|
|
1768
|
+
}
|
|
1769
|
+
);
|
|
1770
|
+
continue;
|
|
1771
|
+
}
|
|
1772
|
+
if (propValue.value.trim()) {
|
|
1773
|
+
declaredOptions.name = propValue.value.trim();
|
|
1774
|
+
}
|
|
1775
|
+
continue;
|
|
1776
|
+
}
|
|
1777
|
+
if (propKey.name === "inheritAttrs") {
|
|
1778
|
+
if (!t16.isBooleanLiteral(propValue)) {
|
|
1779
|
+
logger.warn(
|
|
1780
|
+
`Expected property to be of a type ["BooleanLiteral"] but instead got "${propValue.type}".`,
|
|
1781
|
+
{
|
|
1782
|
+
source,
|
|
1783
|
+
file: filename,
|
|
1784
|
+
loc: propKey?.loc
|
|
1785
|
+
}
|
|
1786
|
+
);
|
|
1787
|
+
continue;
|
|
1788
|
+
}
|
|
1789
|
+
declaredOptions.inheritAttrs = propValue.value;
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-emits.ts
|
|
1795
|
+
import * as t18 from "@babel/types";
|
|
1796
|
+
|
|
1797
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/shared.ts
|
|
1798
|
+
import * as t17 from "@babel/types";
|
|
1799
|
+
function isJsLikeLang(lang) {
|
|
1800
|
+
return lang === "js" || lang === "jsx";
|
|
1801
|
+
}
|
|
1802
|
+
function mergeNames(target, names) {
|
|
1803
|
+
for (const name of names) {
|
|
1804
|
+
target.add(name);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
function resolveTsTypes(typeParams) {
|
|
1808
|
+
if (!typeParams) {
|
|
1809
|
+
return [];
|
|
1810
|
+
}
|
|
1811
|
+
if (t17.isTSTypeParameterInstantiation(typeParams)) {
|
|
1812
|
+
return typeParams.params;
|
|
1813
|
+
}
|
|
1814
|
+
return [];
|
|
1815
|
+
}
|
|
1816
|
+
function resolveObjectOrArrayLiteralNames(value) {
|
|
1817
|
+
const names = /* @__PURE__ */ new Set();
|
|
1818
|
+
if (!value) {
|
|
1819
|
+
return names;
|
|
1820
|
+
}
|
|
1821
|
+
if (t17.isArrayExpression(value)) {
|
|
1822
|
+
for (const element of value.elements) {
|
|
1823
|
+
if (element && t17.isStringLiteral(element)) {
|
|
1824
|
+
names.add(element.value);
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
return names;
|
|
1828
|
+
}
|
|
1829
|
+
if (t17.isObjectExpression(value)) {
|
|
1830
|
+
for (const property of value.properties) {
|
|
1831
|
+
if (!t17.isObjectProperty(property) && !t17.isObjectMethod(property)) {
|
|
1832
|
+
continue;
|
|
1833
|
+
}
|
|
1834
|
+
const name = resolveStaticName(property.key);
|
|
1835
|
+
if (name) {
|
|
1836
|
+
names.add(name);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
return names;
|
|
1841
|
+
}
|
|
1842
|
+
function resolveStaticName(key) {
|
|
1843
|
+
if (t17.isIdentifier(key)) {
|
|
1844
|
+
return key.name;
|
|
1845
|
+
}
|
|
1846
|
+
if (t17.isStringLiteral(key)) {
|
|
1847
|
+
return key.value;
|
|
1848
|
+
}
|
|
1849
|
+
if (t17.isNumericLiteral(key)) {
|
|
1850
|
+
return String(key.value);
|
|
1851
|
+
}
|
|
1852
|
+
if (t17.isTemplateLiteral(key) && !key.expressions.length) {
|
|
1853
|
+
return key.quasis.map((q) => q.value.cooked || "").join("");
|
|
1854
|
+
}
|
|
1855
|
+
return null;
|
|
1856
|
+
}
|
|
1857
|
+
function resolveLocalTypeFromReference(path9, typeRef, ctx, macroName, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
1858
|
+
const refName = resolveTypeReferenceName(typeRef.typeName);
|
|
1859
|
+
if (!refName) return null;
|
|
1860
|
+
if (visitedTypeRefs.has(refName)) return null;
|
|
1861
|
+
const binding = path9.scope.getBinding(refName);
|
|
1862
|
+
if (binding) {
|
|
1863
|
+
if (binding.path.isImportSpecifier() || binding.path.isImportDefaultSpecifier() || binding.path.isImportNamespaceSpecifier()) {
|
|
1864
|
+
const key = `${macroName}:${refName}`;
|
|
1865
|
+
if (!warnedImportedTypeRefs.has(key)) {
|
|
1866
|
+
warnedImportedTypeRefs.add(key);
|
|
1867
|
+
logger.warn(
|
|
1868
|
+
"Type reference comes from an external file. Cross-file analysis is not supported, so it was skipped.",
|
|
1869
|
+
{
|
|
1870
|
+
file: ctx.filename,
|
|
1871
|
+
source: ctx.source,
|
|
1872
|
+
loc: typeRef.loc || path9.node.loc
|
|
1873
|
+
}
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
return null;
|
|
1877
|
+
}
|
|
1878
|
+
visitedTypeRefs.add(refName);
|
|
1879
|
+
if (binding.path.isTSInterfaceDeclaration()) {
|
|
1880
|
+
return t17.tsTypeLiteral(binding.path.node.body.body);
|
|
1881
|
+
}
|
|
1882
|
+
if (binding.path.isTSTypeAliasDeclaration()) {
|
|
1883
|
+
return binding.path.node.typeAnnotation;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
const programPath = path9.findParent((p) => p.isProgram());
|
|
1887
|
+
if (!programPath?.isProgram()) {
|
|
1888
|
+
return null;
|
|
1889
|
+
}
|
|
1890
|
+
const declaration = resolveTypeDeclarationInProgram(programPath.node.body, refName);
|
|
1891
|
+
if (declaration) {
|
|
1892
|
+
visitedTypeRefs.add(refName);
|
|
1893
|
+
return declaration;
|
|
1894
|
+
}
|
|
1895
|
+
const importSource = resolveImportedTypeSourceInProgram(programPath.node.body, refName);
|
|
1896
|
+
if (importSource) {
|
|
1897
|
+
const key = `${macroName}:${refName}`;
|
|
1898
|
+
if (!warnedImportedTypeRefs.has(key)) {
|
|
1899
|
+
warnedImportedTypeRefs.add(key);
|
|
1900
|
+
logger.warn(
|
|
1901
|
+
"Type reference comes from an external file. Cross-file analysis is not supported, so it was skipped.",
|
|
1902
|
+
{
|
|
1903
|
+
file: ctx.filename,
|
|
1904
|
+
source: ctx.source,
|
|
1905
|
+
loc: typeRef.loc || path9.node.loc
|
|
1906
|
+
}
|
|
1907
|
+
);
|
|
1908
|
+
}
|
|
1909
|
+
return null;
|
|
1910
|
+
}
|
|
1911
|
+
return null;
|
|
1912
|
+
}
|
|
1913
|
+
function resolveTypeReferenceName(typeName) {
|
|
1914
|
+
if (t17.isIdentifier(typeName)) {
|
|
1915
|
+
return typeName.name;
|
|
1916
|
+
}
|
|
1917
|
+
return null;
|
|
1918
|
+
}
|
|
1919
|
+
function resolveTypeDeclarationInProgram(body, typeName) {
|
|
1920
|
+
for (const statement of body) {
|
|
1921
|
+
if (t17.isTSInterfaceDeclaration(statement) && statement.id.name === typeName) {
|
|
1922
|
+
return t17.tsTypeLiteral(statement.body.body);
|
|
1923
|
+
}
|
|
1924
|
+
if (t17.isTSTypeAliasDeclaration(statement) && statement.id.name === typeName) {
|
|
1925
|
+
return statement.typeAnnotation;
|
|
1926
|
+
}
|
|
1927
|
+
if (!t17.isExportNamedDeclaration(statement) || !statement.declaration) {
|
|
1928
|
+
continue;
|
|
1929
|
+
}
|
|
1930
|
+
const declaration = statement.declaration;
|
|
1931
|
+
if (t17.isTSInterfaceDeclaration(declaration) && declaration.id.name === typeName) {
|
|
1932
|
+
return t17.tsTypeLiteral(declaration.body.body);
|
|
1933
|
+
}
|
|
1934
|
+
if (t17.isTSTypeAliasDeclaration(declaration) && declaration.id.name === typeName) {
|
|
1935
|
+
return declaration.typeAnnotation;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
return null;
|
|
1939
|
+
}
|
|
1940
|
+
function resolveImportedTypeSourceInProgram(body, typeName) {
|
|
1941
|
+
for (const statement of body) {
|
|
1942
|
+
if (!t17.isImportDeclaration(statement)) {
|
|
1943
|
+
continue;
|
|
1944
|
+
}
|
|
1945
|
+
const imported = statement.specifiers.some((specifier) => {
|
|
1946
|
+
if (!specifier.local || specifier.local.name !== typeName) {
|
|
1947
|
+
return false;
|
|
1948
|
+
}
|
|
1949
|
+
if (t17.isImportSpecifier(specifier)) {
|
|
1950
|
+
return specifier.importKind === "type" || statement.importKind === "type";
|
|
1951
|
+
}
|
|
1952
|
+
return true;
|
|
1953
|
+
});
|
|
1954
|
+
if (imported) {
|
|
1955
|
+
return statement.source.value;
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
return null;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-emits.ts
|
|
1962
|
+
function resolveDeclaredEmits(path9, ctx) {
|
|
1963
|
+
const { node } = path9;
|
|
1964
|
+
const { templateData, scriptData } = ctx;
|
|
1965
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.emits)) {
|
|
1966
|
+
return;
|
|
1967
|
+
}
|
|
1968
|
+
const [initValue] = node.arguments;
|
|
1969
|
+
const tsTypeDef = node.typeParameters;
|
|
1970
|
+
if (isJsLikeLang(scriptData.lang)) {
|
|
1971
|
+
mergeNames(templateData.declaredEmits, resolveObjectOrArrayLiteralNames(initValue));
|
|
1972
|
+
return;
|
|
1973
|
+
}
|
|
1974
|
+
const namesFromType = resolveDeclaredEmitsFromTypeParams(tsTypeDef, path9, ctx);
|
|
1975
|
+
if (namesFromType.size) {
|
|
1976
|
+
mergeNames(templateData.declaredEmits, namesFromType);
|
|
1977
|
+
return;
|
|
1978
|
+
}
|
|
1979
|
+
mergeNames(templateData.declaredEmits, resolveObjectOrArrayLiteralNames(initValue));
|
|
1980
|
+
}
|
|
1981
|
+
function resolveDeclaredEmitsFromTypeParams(typeParams, path9, ctx) {
|
|
1982
|
+
const names = /* @__PURE__ */ new Set();
|
|
1983
|
+
const visitedTypeRefs = /* @__PURE__ */ new Set();
|
|
1984
|
+
const warnedImportedTypeRefs = /* @__PURE__ */ new Set();
|
|
1985
|
+
for (const tsType of resolveTsTypes(typeParams)) {
|
|
1986
|
+
collectEmitsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
1987
|
+
}
|
|
1988
|
+
return names;
|
|
1989
|
+
}
|
|
1990
|
+
function collectEmitsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
1991
|
+
if (t18.isTSParenthesizedType(tsType)) {
|
|
1992
|
+
collectEmitsFromTsType(
|
|
1993
|
+
tsType.typeAnnotation,
|
|
1994
|
+
names,
|
|
1995
|
+
path9,
|
|
1996
|
+
ctx,
|
|
1997
|
+
visitedTypeRefs,
|
|
1998
|
+
warnedImportedTypeRefs
|
|
1999
|
+
);
|
|
2000
|
+
return;
|
|
2001
|
+
}
|
|
2002
|
+
if (t18.isTSIntersectionType(tsType) || t18.isTSUnionType(tsType)) {
|
|
2003
|
+
for (const type of tsType.types) {
|
|
2004
|
+
collectEmitsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2005
|
+
}
|
|
2006
|
+
return;
|
|
2007
|
+
}
|
|
2008
|
+
if (t18.isTSTypeReference(tsType)) {
|
|
2009
|
+
const innerTypes = tsType.typeParameters?.params || [];
|
|
2010
|
+
for (const type of innerTypes) {
|
|
2011
|
+
collectEmitsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2012
|
+
}
|
|
2013
|
+
const localType = resolveLocalTypeFromReference(
|
|
2014
|
+
path9,
|
|
2015
|
+
tsType,
|
|
2016
|
+
ctx,
|
|
2017
|
+
MACRO_API_NAMES.emits,
|
|
2018
|
+
visitedTypeRefs,
|
|
2019
|
+
warnedImportedTypeRefs
|
|
2020
|
+
);
|
|
2021
|
+
if (localType) {
|
|
2022
|
+
collectEmitsFromTsType(localType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2023
|
+
}
|
|
2024
|
+
return;
|
|
2025
|
+
}
|
|
2026
|
+
if (t18.isTSFunctionType(tsType)) {
|
|
2027
|
+
collectEmitNamesFromCallable(tsType.parameters, names);
|
|
2028
|
+
return;
|
|
2029
|
+
}
|
|
2030
|
+
if (!t18.isTSTypeLiteral(tsType)) {
|
|
2031
|
+
return;
|
|
2032
|
+
}
|
|
2033
|
+
for (const member of tsType.members) {
|
|
2034
|
+
if (t18.isTSPropertySignature(member) || t18.isTSMethodSignature(member)) {
|
|
2035
|
+
const eventName = resolveStaticName(member.key);
|
|
2036
|
+
if (eventName) {
|
|
2037
|
+
names.add(eventName);
|
|
2038
|
+
}
|
|
2039
|
+
continue;
|
|
2040
|
+
}
|
|
2041
|
+
if (t18.isTSCallSignatureDeclaration(member)) {
|
|
2042
|
+
collectEmitNamesFromCallable(member.parameters, names);
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
function collectEmitNamesFromCallable(parameters, names) {
|
|
2047
|
+
const firstParamType = resolveFirstParamType(parameters[0]);
|
|
2048
|
+
if (!firstParamType) {
|
|
2049
|
+
return;
|
|
2050
|
+
}
|
|
2051
|
+
for (const eventName of resolveStringLiteralTypeNames(firstParamType)) {
|
|
2052
|
+
names.add(eventName);
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
function resolveFirstParamType(param) {
|
|
2056
|
+
if (!param) {
|
|
2057
|
+
return null;
|
|
2058
|
+
}
|
|
2059
|
+
if (t18.isIdentifier(param) && param.typeAnnotation && !t18.isNoop(param.typeAnnotation)) {
|
|
2060
|
+
return param.typeAnnotation.typeAnnotation;
|
|
2061
|
+
}
|
|
2062
|
+
if (t18.isAssignmentPattern(param)) {
|
|
2063
|
+
const left = param.left;
|
|
2064
|
+
if (t18.isIdentifier(left) && left.typeAnnotation && !t18.isNoop(left.typeAnnotation)) {
|
|
2065
|
+
return left.typeAnnotation.typeAnnotation;
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
if (t18.isRestElement(param) && t18.isIdentifier(param.argument)) {
|
|
2069
|
+
const { typeAnnotation } = param.argument;
|
|
2070
|
+
if (typeAnnotation && !t18.isNoop(typeAnnotation)) {
|
|
2071
|
+
return typeAnnotation.typeAnnotation;
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
return null;
|
|
2075
|
+
}
|
|
2076
|
+
function resolveStringLiteralTypeNames(type) {
|
|
2077
|
+
if (t18.isTSParenthesizedType(type)) {
|
|
2078
|
+
return resolveStringLiteralTypeNames(type.typeAnnotation);
|
|
2079
|
+
}
|
|
2080
|
+
if (t18.isTSUnionType(type)) {
|
|
2081
|
+
return type.types.flatMap(resolveStringLiteralTypeNames);
|
|
2082
|
+
}
|
|
2083
|
+
if (!t18.isTSLiteralType(type)) {
|
|
2084
|
+
return [];
|
|
2085
|
+
}
|
|
2086
|
+
if (t18.isStringLiteral(type.literal)) {
|
|
2087
|
+
return [type.literal.value];
|
|
2088
|
+
}
|
|
2089
|
+
if (t18.isTemplateLiteral(type.literal) && !type.literal.expressions.length) {
|
|
2090
|
+
const cooked = type.literal.quasis.map((q) => q.value.cooked || "").join("");
|
|
2091
|
+
return cooked ? [cooked] : [];
|
|
2092
|
+
}
|
|
2093
|
+
return [];
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/resolve-declared-props.ts
|
|
2097
|
+
import * as t19 from "@babel/types";
|
|
2098
|
+
function resolveDeclaredProps(path9, ctx) {
|
|
2099
|
+
const { node } = path9;
|
|
2100
|
+
const { templateData, scriptData } = ctx;
|
|
2101
|
+
if (!isCalleeNamed(node, MACRO_API_NAMES.props)) {
|
|
2102
|
+
return;
|
|
2103
|
+
}
|
|
2104
|
+
const [initValue] = node.arguments;
|
|
2105
|
+
const tsTypeDef = node.typeParameters;
|
|
2106
|
+
if (isJsLikeLang(scriptData.lang)) {
|
|
2107
|
+
mergeNames(templateData.declaredProps, resolveObjectOrArrayLiteralNames(initValue));
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
const namesFromType = resolveDeclaredPropsFromTypeParams(tsTypeDef, path9, ctx);
|
|
2111
|
+
if (namesFromType.size) {
|
|
2112
|
+
mergeNames(templateData.declaredProps, namesFromType);
|
|
2113
|
+
return;
|
|
2114
|
+
}
|
|
2115
|
+
mergeNames(templateData.declaredProps, resolveObjectOrArrayLiteralNames(initValue));
|
|
2116
|
+
}
|
|
2117
|
+
function resolveDeclaredPropsFromTypeParams(typeParams, path9, ctx) {
|
|
2118
|
+
const names = /* @__PURE__ */ new Set();
|
|
2119
|
+
const visitedTypeRefs = /* @__PURE__ */ new Set();
|
|
2120
|
+
const warnedImportedTypeRefs = /* @__PURE__ */ new Set();
|
|
2121
|
+
for (const tsType of resolveTsTypes(typeParams)) {
|
|
2122
|
+
collectPropsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2123
|
+
}
|
|
2124
|
+
return names;
|
|
2125
|
+
}
|
|
2126
|
+
function collectPropsFromTsType(tsType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs) {
|
|
2127
|
+
if (t19.isTSParenthesizedType(tsType)) {
|
|
2128
|
+
collectPropsFromTsType(
|
|
2129
|
+
tsType.typeAnnotation,
|
|
2130
|
+
names,
|
|
2131
|
+
path9,
|
|
2132
|
+
ctx,
|
|
2133
|
+
visitedTypeRefs,
|
|
2134
|
+
warnedImportedTypeRefs
|
|
2135
|
+
);
|
|
2136
|
+
return;
|
|
2137
|
+
}
|
|
2138
|
+
if (t19.isTSIntersectionType(tsType) || t19.isTSUnionType(tsType)) {
|
|
2139
|
+
for (const type of tsType.types) {
|
|
2140
|
+
collectPropsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2141
|
+
}
|
|
2142
|
+
return;
|
|
2143
|
+
}
|
|
2144
|
+
if (t19.isTSTypeReference(tsType)) {
|
|
2145
|
+
const innerTypes = tsType.typeParameters?.params || [];
|
|
2146
|
+
for (const type of innerTypes) {
|
|
2147
|
+
collectPropsFromTsType(type, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2148
|
+
}
|
|
2149
|
+
const localType = resolveLocalTypeFromReference(
|
|
2150
|
+
path9,
|
|
2151
|
+
tsType,
|
|
2152
|
+
ctx,
|
|
2153
|
+
MACRO_API_NAMES.props,
|
|
2154
|
+
visitedTypeRefs,
|
|
2155
|
+
warnedImportedTypeRefs
|
|
2156
|
+
);
|
|
2157
|
+
if (localType) {
|
|
2158
|
+
collectPropsFromTsType(localType, names, path9, ctx, visitedTypeRefs, warnedImportedTypeRefs);
|
|
2159
|
+
}
|
|
2160
|
+
return;
|
|
2161
|
+
}
|
|
2162
|
+
if (!t19.isTSTypeLiteral(tsType)) {
|
|
2163
|
+
return;
|
|
2164
|
+
}
|
|
2165
|
+
for (const member of tsType.members) {
|
|
2166
|
+
if (!t19.isTSPropertySignature(member) && !t19.isTSMethodSignature(member)) {
|
|
2167
|
+
continue;
|
|
2168
|
+
}
|
|
2169
|
+
const name = resolveStaticName(member.key);
|
|
2170
|
+
if (name) {
|
|
2171
|
+
names.add(name);
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
|
|
2176
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-declared-props-emits/index.ts
|
|
2177
|
+
function resolveDeclaredPropsAndEmits(path9, ctx) {
|
|
2178
|
+
resolveDeclaredProps(path9, ctx);
|
|
2179
|
+
resolveDeclaredEmits(path9, ctx);
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-use-attrs-exists.ts
|
|
2183
|
+
import * as t20 from "@babel/types";
|
|
2184
|
+
function resolveUseAttrsExists(path9, ctx) {
|
|
2185
|
+
const { node } = path9;
|
|
2186
|
+
const { templateData, scriptData } = ctx;
|
|
2187
|
+
if (!isCalleeNamed(node, VUE_API_MAP.useAttrs)) {
|
|
2188
|
+
return;
|
|
2189
|
+
}
|
|
2190
|
+
if (scriptData?.hasUseAttrsCall) {
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
scriptData.hasUseAttrsCall = true;
|
|
2194
|
+
const parent = path9.parentPath?.node;
|
|
2195
|
+
if (parent && t20.isVariableDeclarator(parent) && t20.isIdentifier(parent.id)) {
|
|
2196
|
+
templateData.declaredAttrs = parent.id.name;
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/index.ts
|
|
2201
|
+
import * as t21 from "@babel/types";
|
|
2202
|
+
|
|
2203
|
+
// src/consts/reactive-type-map.ts
|
|
2204
|
+
var REACTIVE_TYPE_MAP = {
|
|
2205
|
+
ref: "ref",
|
|
2206
|
+
toRef: "ref",
|
|
2207
|
+
toRefs: "ref",
|
|
2208
|
+
customRef: "ref",
|
|
2209
|
+
shallowRef: "ref",
|
|
2210
|
+
reactive: "reactive",
|
|
2211
|
+
shallowReactive: "reactive",
|
|
2212
|
+
computed: "ref",
|
|
2213
|
+
readonly: "reactive",
|
|
2214
|
+
shallowReadonly: "reactive",
|
|
2215
|
+
provide: "reactive",
|
|
2216
|
+
inject: "reactive",
|
|
2217
|
+
defineProps: "reactive",
|
|
2218
|
+
defineEmits: "reactive",
|
|
2219
|
+
useRoute: "reactive",
|
|
2220
|
+
useRouter: "reactive",
|
|
2221
|
+
useLink: "reactive"
|
|
2222
|
+
};
|
|
2223
|
+
|
|
2224
|
+
// src/shared/reactive-utils.ts
|
|
2225
|
+
function getReactiveType(originalName) {
|
|
2226
|
+
return REACTIVE_TYPE_MAP[originalName] || "none";
|
|
2227
|
+
}
|
|
2228
|
+
function getReactiveStateApis() {
|
|
2229
|
+
return new Set(Object.keys(REACTIVE_TYPE_MAP));
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/resolve-reactive-bindings.ts
|
|
2233
|
+
function resolveReactiveBindings(node, ctx) {
|
|
2234
|
+
const { reactiveBindings } = ctx.templateData;
|
|
2235
|
+
const reactiveStateApis = getReactiveStateApis();
|
|
2236
|
+
const init = node.init;
|
|
2237
|
+
const callName = init.callee.name;
|
|
2238
|
+
if (!reactiveStateApis.has(callName)) {
|
|
2239
|
+
return;
|
|
2240
|
+
}
|
|
2241
|
+
const varName = node.id.name;
|
|
2242
|
+
const initValue = init.arguments[0];
|
|
2243
|
+
reactiveBindings[varName] = {
|
|
2244
|
+
name: varName,
|
|
2245
|
+
value: initValue,
|
|
2246
|
+
source: callName,
|
|
2247
|
+
reactiveType: getReactiveType(callName)
|
|
2248
|
+
};
|
|
2249
|
+
if (callName === MACRO_API_NAMES.props) {
|
|
2250
|
+
ctx.propField = varName;
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
// src/consts/html-tag-types.ts
|
|
2255
|
+
var HTML_TAG_TYPES = {
|
|
2256
|
+
a: "HTMLAnchorElement",
|
|
2257
|
+
p: "HTMLParagraphElement",
|
|
2258
|
+
div: "HTMLDivElement",
|
|
2259
|
+
span: "HTMLSpanElement",
|
|
2260
|
+
h1: "HTMLHeadingElement",
|
|
2261
|
+
h2: "HTMLHeadingElement",
|
|
2262
|
+
h3: "HTMLHeadingElement",
|
|
2263
|
+
h4: "HTMLHeadingElement",
|
|
2264
|
+
h5: "HTMLHeadingElement",
|
|
2265
|
+
h6: "HTMLHeadingElement",
|
|
2266
|
+
button: "HTMLButtonElement",
|
|
2267
|
+
input: "HTMLInputElement",
|
|
2268
|
+
form: "HTMLFormElement",
|
|
2269
|
+
textarea: "HTMLTextAreaElement",
|
|
2270
|
+
select: "HTMLSelectElement",
|
|
2271
|
+
option: "HTMLOptionElement",
|
|
2272
|
+
label: "HTMLLabelElement",
|
|
2273
|
+
fieldset: "HTMLFieldSetElement",
|
|
2274
|
+
legend: "HTMLLegendElement",
|
|
2275
|
+
img: "HTMLImageElement",
|
|
2276
|
+
video: "HTMLVideoElement",
|
|
2277
|
+
audio: "HTMLAudioElement",
|
|
2278
|
+
canvas: "HTMLCanvasElement",
|
|
2279
|
+
picture: "HTMLPictureElement",
|
|
2280
|
+
source: "HTMLSourceElement",
|
|
2281
|
+
track: "HTMLTrackElement",
|
|
2282
|
+
ul: "HTMLUListElement",
|
|
2283
|
+
ol: "HTMLOListElement",
|
|
2284
|
+
li: "HTMLLIElement",
|
|
2285
|
+
dl: "HTMLDListElement",
|
|
2286
|
+
table: "HTMLTableElement",
|
|
2287
|
+
thead: "HTMLTableSectionElement",
|
|
2288
|
+
tbody: "HTMLTableSectionElement",
|
|
2289
|
+
tfoot: "HTMLTableSectionElement",
|
|
2290
|
+
tr: "HTMLTableRowElement",
|
|
2291
|
+
td: "HTMLTableCellElement",
|
|
2292
|
+
th: "HTMLTableCellElement",
|
|
2293
|
+
caption: "HTMLTableCaptionElement",
|
|
2294
|
+
col: "HTMLTableColElement",
|
|
2295
|
+
colgroup: "HTMLTableColElement",
|
|
2296
|
+
html: "HTMLHtmlElement",
|
|
2297
|
+
head: "HTMLHeadElement",
|
|
2298
|
+
body: "HTMLBodyElement",
|
|
2299
|
+
title: "HTMLTitleElement",
|
|
2300
|
+
meta: "HTMLMetaElement",
|
|
2301
|
+
link: "HTMLLinkElement",
|
|
2302
|
+
style: "HTMLStyleElement",
|
|
2303
|
+
script: "HTMLScriptElement",
|
|
2304
|
+
base: "HTMLBaseElement",
|
|
2305
|
+
br: "HTMLBRElement",
|
|
2306
|
+
hr: "HTMLHRElement",
|
|
2307
|
+
del: "HTMLModElement",
|
|
2308
|
+
ins: "HTMLModElement",
|
|
2309
|
+
iframe: "HTMLIFrameElement",
|
|
2310
|
+
embed: "HTMLEmbedElement",
|
|
2311
|
+
object: "HTMLObjectElement",
|
|
2312
|
+
param: "HTMLParamElement",
|
|
2313
|
+
details: "HTMLDetailsElement",
|
|
2314
|
+
dialog: "HTMLDialogElement",
|
|
2315
|
+
menu: "HTMLMenuElement",
|
|
2316
|
+
progress: "HTMLProgressElement",
|
|
2317
|
+
meter: "HTMLMeterElement",
|
|
2318
|
+
output: "HTMLOutputElement",
|
|
2319
|
+
frame: "HTMLFrameElement"
|
|
2320
|
+
};
|
|
2321
|
+
|
|
2322
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/resolve-template-ref-bindings.ts
|
|
2323
|
+
function resolveTemplateRefBindings(node, ctx) {
|
|
2324
|
+
const { refBindings } = ctx.templateData;
|
|
2325
|
+
const init = node.init;
|
|
2326
|
+
const callee = init.callee;
|
|
2327
|
+
if (callee.name !== VUE_API_MAP.useTemplateRef) {
|
|
2328
|
+
return;
|
|
2329
|
+
}
|
|
2330
|
+
const varName = node.id.name;
|
|
2331
|
+
const initValue = init.arguments[0]?.value;
|
|
2332
|
+
refBindings.domRefs[varName] = {
|
|
2333
|
+
tag: initValue,
|
|
2334
|
+
name: varName,
|
|
2335
|
+
htmlType: HTML_TAG_TYPES[initValue] || "HTMLElement"
|
|
2336
|
+
};
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/resolve-var-bindings/index.ts
|
|
2340
|
+
function resolveVarBindings(node, ctx) {
|
|
2341
|
+
const { init, id } = node;
|
|
2342
|
+
if (!t21.isIdentifier(id) || !init) {
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
2345
|
+
const isCallExpr = t21.isCallExpression(init) && t21.isIdentifier(init.callee);
|
|
2346
|
+
if (isCallExpr) {
|
|
2347
|
+
resolveReactiveBindings(node, ctx);
|
|
2348
|
+
resolveTemplateRefBindings(node, ctx);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
// src/core/parse/sfc/postprocess/resolve-script-metadata/index.ts
|
|
2353
|
+
function resolveScriptMeta(result, ctx) {
|
|
2354
|
+
const scriptAst = result.script?.ast;
|
|
2355
|
+
if (ctx.inputType !== "sfc" || !scriptAst) {
|
|
2356
|
+
return;
|
|
2357
|
+
}
|
|
2358
|
+
traverse(scriptAst, {
|
|
2359
|
+
VariableDeclarator(path9) {
|
|
2360
|
+
if (!atComponentOrHookRoot(path9, scriptAst.program)) {
|
|
2361
|
+
return;
|
|
2362
|
+
}
|
|
2363
|
+
resolveVarBindings(path9.node, ctx);
|
|
2364
|
+
},
|
|
2365
|
+
CallExpression(path9) {
|
|
2366
|
+
resolveDeclaredOptions(path9, ctx);
|
|
2367
|
+
resolveDeclaredPropsAndEmits(path9, ctx);
|
|
2368
|
+
resolveUseAttrsExists(path9, ctx);
|
|
2369
|
+
}
|
|
2370
|
+
});
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
// src/core/parse/sfc/process/resolve-script.ts
|
|
2374
|
+
import { parse as babelParse2 } from "@babel/parser";
|
|
2375
|
+
function resolveScript(descriptor, ctx, parseResult) {
|
|
2376
|
+
if (descriptor.script) {
|
|
2377
|
+
throw new Error(
|
|
2378
|
+
`Traditional Vue <script> syntax is not supported. Please migrate to <script setup>.
|
|
2379
|
+
at <anonymous> (${ctx.filename})`
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2382
|
+
const { scriptSetup } = descriptor;
|
|
2383
|
+
if (!scriptSetup) return null;
|
|
2384
|
+
if (scriptSetup?.warnings) {
|
|
2385
|
+
scriptSetup?.warnings.forEach((msg) => {
|
|
2386
|
+
logger.warn(msg, { file: ctx.filename });
|
|
2387
|
+
});
|
|
2388
|
+
}
|
|
2389
|
+
resolveContext(scriptSetup, ctx);
|
|
2390
|
+
const parseOpts = getBabelParseOptions(scriptSetup.lang, "script", ctx.filename);
|
|
2391
|
+
parseResult.script = {
|
|
2392
|
+
ast: babelParse2(scriptSetup.content, parseOpts),
|
|
2393
|
+
source: scriptSetup
|
|
2394
|
+
};
|
|
2395
|
+
}
|
|
2396
|
+
function resolveContext(scriptSetup, ctx) {
|
|
2397
|
+
const { scriptData } = ctx;
|
|
2398
|
+
let { content, lang } = scriptSetup;
|
|
2399
|
+
const resolveVRComment = (source) => {
|
|
2400
|
+
const regx = /\/\/\s*@vr-name:\s*(\w+)/;
|
|
2401
|
+
const nameMatch = source.match(regx);
|
|
2402
|
+
content = content.replace(regx, "");
|
|
2403
|
+
return nameMatch?.[1]?.trim() || "";
|
|
2404
|
+
};
|
|
2405
|
+
scriptData.declaredOptions.name = resolveVRComment(content);
|
|
2406
|
+
scriptData.lang = lang || "js";
|
|
2407
|
+
scriptData.source = content;
|
|
2408
|
+
scriptSetup.content = content;
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
// src/plugins/postcss.ts
|
|
2412
|
+
import postcss from "postcss";
|
|
2413
|
+
function processScopedWithPostCss(input, hash) {
|
|
2414
|
+
const scopeId = `data-css-${hash}`;
|
|
2415
|
+
const result = postcss([
|
|
2416
|
+
{
|
|
2417
|
+
postcssPlugin: "postcss-scoped-simple",
|
|
2418
|
+
Rule(rule) {
|
|
2419
|
+
if (rule.parent && rule.parent.type === "atrule" && rule.parent.name === "keyframes") {
|
|
2420
|
+
return;
|
|
2421
|
+
}
|
|
2422
|
+
rule.selectors = rule.selectors.map((selector) => {
|
|
2423
|
+
if (selector.includes(":global(")) {
|
|
2424
|
+
return selector.replace(/:global\((.*)\)/, "$1");
|
|
2425
|
+
}
|
|
2426
|
+
if (selector.includes(":deep(")) {
|
|
2427
|
+
return selector.replace(/:deep\((.*)\)/, "$1");
|
|
2428
|
+
}
|
|
2429
|
+
const pseudoElementRegex = /(::[a-zA-Z-]+)$/;
|
|
2430
|
+
const match = selector.match(pseudoElementRegex);
|
|
2431
|
+
if (match) {
|
|
2432
|
+
const base = selector.replace(pseudoElementRegex, "");
|
|
2433
|
+
const pseudo = match[1];
|
|
1668
2434
|
return `${base}[${scopeId}]${pseudo}`;
|
|
1669
2435
|
}
|
|
1670
2436
|
return `${selector}[${scopeId}]`;
|
|
@@ -1873,205 +2639,6 @@ import { parse as babelParse3 } from "@babel/parser";
|
|
|
1873
2639
|
// src/core/transform/sfc/script/syntax-processor/index.ts
|
|
1874
2640
|
import { traverse as traverse3 } from "@babel/core";
|
|
1875
2641
|
|
|
1876
|
-
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
1877
|
-
import * as t16 from "@babel/types";
|
|
1878
|
-
function findRootVariablePath(path9) {
|
|
1879
|
-
const rootId = findRootIdentifier(path9.node);
|
|
1880
|
-
if (!rootId?.name) return null;
|
|
1881
|
-
const binding = path9.scope.getBinding(rootId.name);
|
|
1882
|
-
if (!binding) return null;
|
|
1883
|
-
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
1884
|
-
return rootPath;
|
|
1885
|
-
}
|
|
1886
|
-
function findRootIdentifier(node) {
|
|
1887
|
-
let current = node.object;
|
|
1888
|
-
while (t16.isMemberExpression(current) || t16.isOptionalMemberExpression(current)) {
|
|
1889
|
-
current = current.object;
|
|
1890
|
-
}
|
|
1891
|
-
return t16.isIdentifier(current) ? current : null;
|
|
1892
|
-
}
|
|
1893
|
-
function getVariableDeclaratorPath(path9) {
|
|
1894
|
-
if (path9.isVariableDeclarator()) {
|
|
1895
|
-
return path9;
|
|
1896
|
-
}
|
|
1897
|
-
return path9.findParent((p) => p.isVariableDeclarator());
|
|
1898
|
-
}
|
|
1899
|
-
function isVariableDeclTopLevel(path9) {
|
|
1900
|
-
const variableDeclaratorPath = path9;
|
|
1901
|
-
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
1902
|
-
if (!variableDeclarationPath) {
|
|
1903
|
-
return false;
|
|
1904
|
-
}
|
|
1905
|
-
if (variableDeclarationPath.isProgram()) {
|
|
1906
|
-
return true;
|
|
1907
|
-
}
|
|
1908
|
-
const variableDeclarationParentPath = variableDeclarationPath.parentPath;
|
|
1909
|
-
if (variableDeclarationParentPath && variableDeclarationParentPath.isProgram()) {
|
|
1910
|
-
return true;
|
|
1911
|
-
}
|
|
1912
|
-
return false;
|
|
1913
|
-
}
|
|
1914
|
-
function isRealVariableAccess(path9) {
|
|
1915
|
-
return isIdentifierAccess(path9) && !isPropertyName(path9);
|
|
1916
|
-
}
|
|
1917
|
-
function isIdentifierAccess(path9) {
|
|
1918
|
-
if (isIdentifierDeclaration(path9)) {
|
|
1919
|
-
return false;
|
|
1920
|
-
}
|
|
1921
|
-
const binding = path9.scope.getBinding(path9.node.name);
|
|
1922
|
-
if (!binding) {
|
|
1923
|
-
return true;
|
|
1924
|
-
}
|
|
1925
|
-
return binding.identifier !== path9.node;
|
|
1926
|
-
}
|
|
1927
|
-
function isIdentifierDeclaration(path9) {
|
|
1928
|
-
const parent = path9.parentPath;
|
|
1929
|
-
if (!parent) return false;
|
|
1930
|
-
if (parent.isVariableDeclarator() && parent.node.id === path9.node) {
|
|
1931
|
-
return true;
|
|
1932
|
-
}
|
|
1933
|
-
if (parent.isFunctionDeclaration() && parent.node.id === path9.node) {
|
|
1934
|
-
return true;
|
|
1935
|
-
}
|
|
1936
|
-
if (parent.isFunctionExpression() && parent.node.id === path9.node) {
|
|
1937
|
-
return true;
|
|
1938
|
-
}
|
|
1939
|
-
if (parent.isClassDeclaration() && parent.node.id === path9.node) {
|
|
1940
|
-
return true;
|
|
1941
|
-
}
|
|
1942
|
-
if (parent.isImportSpecifier() && parent.node.local === path9.node) {
|
|
1943
|
-
return true;
|
|
1944
|
-
}
|
|
1945
|
-
if (parent.isImportDefaultSpecifier() && parent.node.local === path9.node) {
|
|
1946
|
-
return true;
|
|
1947
|
-
}
|
|
1948
|
-
if (parent.isImportNamespaceSpecifier() && parent.node.local === path9.node) {
|
|
1949
|
-
return true;
|
|
1950
|
-
}
|
|
1951
|
-
if (parent.isFunction() && parent.node.params.includes(path9.node)) {
|
|
1952
|
-
return true;
|
|
1953
|
-
}
|
|
1954
|
-
if (parent.isCatchClause() && parent.node.param === path9.node) {
|
|
1955
|
-
return true;
|
|
1956
|
-
}
|
|
1957
|
-
return false;
|
|
1958
|
-
}
|
|
1959
|
-
function isPropertyName(path9) {
|
|
1960
|
-
const parent = path9.parentPath;
|
|
1961
|
-
if (!parent) return false;
|
|
1962
|
-
if (parent.isObjectProperty() && parent.node.key === path9.node) {
|
|
1963
|
-
return true;
|
|
1964
|
-
}
|
|
1965
|
-
if (parent.isClassProperty() && parent.node.key === path9.node) {
|
|
1966
|
-
return true;
|
|
1967
|
-
}
|
|
1968
|
-
if (parent.isMemberExpression() && parent.node.property === path9.node) {
|
|
1969
|
-
return true;
|
|
1970
|
-
}
|
|
1971
|
-
return false;
|
|
1972
|
-
}
|
|
1973
|
-
function replaceCallName(callExp, identifierName) {
|
|
1974
|
-
const { callee } = callExp;
|
|
1975
|
-
if (!t16.isIdentifier(callee)) return;
|
|
1976
|
-
callee.name = identifierName;
|
|
1977
|
-
if (callee.loc) {
|
|
1978
|
-
callee.loc.identifierName = identifierName;
|
|
1979
|
-
}
|
|
1980
|
-
}
|
|
1981
|
-
function replaceIdName(id, newName) {
|
|
1982
|
-
if (!t16.isIdentifier(id)) return;
|
|
1983
|
-
id.name = newName;
|
|
1984
|
-
if (id.loc) {
|
|
1985
|
-
id.loc.identifierName = newName;
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
1989
|
-
const { filename, scriptData } = ctx;
|
|
1990
|
-
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
1991
|
-
const ts = expressionToTSType(exp);
|
|
1992
|
-
return tsTypeAnnotation7 ? t16.tsTypeAnnotation(ts) : ts;
|
|
1993
|
-
}
|
|
1994
|
-
function expressionToTSType(exp) {
|
|
1995
|
-
if (t16.isStringLiteral(exp)) return t16.tsStringKeyword();
|
|
1996
|
-
if (t16.isNumericLiteral(exp)) return t16.tsNumberKeyword();
|
|
1997
|
-
if (t16.isBooleanLiteral(exp)) return t16.tsBooleanKeyword();
|
|
1998
|
-
if (t16.isArrayExpression(exp)) return t16.tsArrayType(t16.tsAnyKeyword());
|
|
1999
|
-
if (t16.isObjectExpression(exp)) {
|
|
2000
|
-
const members = [];
|
|
2001
|
-
for (const p of exp.properties) {
|
|
2002
|
-
if (!t16.isObjectProperty(p)) continue;
|
|
2003
|
-
let key;
|
|
2004
|
-
if (t16.isIdentifier(p.key)) key = p.key.name;
|
|
2005
|
-
else if (t16.isStringLiteral(p.key)) key = p.key.value;
|
|
2006
|
-
if (!key) continue;
|
|
2007
|
-
if (t16.isExpression(p.value)) {
|
|
2008
|
-
members.push(
|
|
2009
|
-
t16.tsPropertySignature(t16.identifier(key), t16.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
2010
|
-
);
|
|
2011
|
-
} else {
|
|
2012
|
-
members.push(
|
|
2013
|
-
t16.tsPropertySignature(t16.identifier(key), t16.tsTypeAnnotation(t16.tsAnyKeyword()))
|
|
2014
|
-
);
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
2017
|
-
return t16.tsTypeLiteral(members);
|
|
2018
|
-
}
|
|
2019
|
-
if (t16.isArrowFunctionExpression(exp) || t16.isFunctionExpression(exp)) {
|
|
2020
|
-
const params = exp.params.map((p, i) => {
|
|
2021
|
-
const id = t16.isIdentifier(p) ? t16.identifier(p.name) : t16.identifier(`arg${i}`);
|
|
2022
|
-
id.typeAnnotation = t16.tsTypeAnnotation(t16.tsAnyKeyword());
|
|
2023
|
-
return id;
|
|
2024
|
-
});
|
|
2025
|
-
let returnType = t16.tsAnyKeyword();
|
|
2026
|
-
if (t16.isBlockStatement(exp.body)) {
|
|
2027
|
-
for (const stmt of exp.body.body) {
|
|
2028
|
-
if (t16.isReturnStatement(stmt) && stmt.argument) {
|
|
2029
|
-
if (t16.isExpression(stmt.argument)) {
|
|
2030
|
-
returnType = expressionToTSType(stmt.argument);
|
|
2031
|
-
break;
|
|
2032
|
-
}
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
} else if (t16.isExpression(exp.body)) {
|
|
2036
|
-
returnType = expressionToTSType(exp.body);
|
|
2037
|
-
}
|
|
2038
|
-
return t16.tsFunctionType(null, params, t16.tsTypeAnnotation(returnType));
|
|
2039
|
-
}
|
|
2040
|
-
return t16.tsAnyKeyword();
|
|
2041
|
-
}
|
|
2042
|
-
function isCalleeNamed(node, name) {
|
|
2043
|
-
if (!t16.isIdentifier(node.callee)) {
|
|
2044
|
-
return false;
|
|
2045
|
-
}
|
|
2046
|
-
return node.callee.name === name;
|
|
2047
|
-
}
|
|
2048
|
-
function isSimpleLiteral(node) {
|
|
2049
|
-
if (!node) return false;
|
|
2050
|
-
if (t16.isStringLiteral(node) || t16.isNumericLiteral(node) || t16.isBooleanLiteral(node) || t16.isNullLiteral(node) || t16.isRegExpLiteral(node) || t16.isBigIntLiteral(node) || t16.isDecimalLiteral(node)) {
|
|
2051
|
-
return true;
|
|
2052
|
-
}
|
|
2053
|
-
return false;
|
|
2054
|
-
}
|
|
2055
|
-
function forkNode(node, deep = true) {
|
|
2056
|
-
const newNode = t16.cloneNode(node, deep);
|
|
2057
|
-
newNode.leadingComments = node.leadingComments;
|
|
2058
|
-
newNode.innerComments = node.innerComments;
|
|
2059
|
-
newNode.trailingComments = null;
|
|
2060
|
-
cleanNodeLoc(node);
|
|
2061
|
-
cleanNodeComments(node);
|
|
2062
|
-
return newNode;
|
|
2063
|
-
}
|
|
2064
|
-
function cleanNodeLoc(node) {
|
|
2065
|
-
node.start = null;
|
|
2066
|
-
node.end = null;
|
|
2067
|
-
node.loc = null;
|
|
2068
|
-
}
|
|
2069
|
-
function cleanNodeComments(node) {
|
|
2070
|
-
node.leadingComments = null;
|
|
2071
|
-
node.innerComments = null;
|
|
2072
|
-
node.trailingComments = null;
|
|
2073
|
-
}
|
|
2074
|
-
|
|
2075
2642
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-global-type-chunk.ts
|
|
2076
2643
|
function resolveGlobalTypeChunks(path9, ir) {
|
|
2077
2644
|
if (!path9.parentPath?.isProgram()) {
|
|
@@ -2083,12 +2650,12 @@ function resolveGlobalTypeChunks(path9, ir) {
|
|
|
2083
2650
|
}
|
|
2084
2651
|
|
|
2085
2652
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-module-chunk.ts
|
|
2086
|
-
import * as
|
|
2653
|
+
import * as t22 from "@babel/types";
|
|
2087
2654
|
function resolveModuleChunks(path9, ir) {
|
|
2088
2655
|
const forked = forkNode(path9.node);
|
|
2089
|
-
if (
|
|
2656
|
+
if (t22.isImportDeclaration(forked)) {
|
|
2090
2657
|
ir.imports.push(forked);
|
|
2091
|
-
} else if (
|
|
2658
|
+
} else if (t22.isExportDeclaration(forked)) {
|
|
2092
2659
|
ir.exports.push(forked);
|
|
2093
2660
|
}
|
|
2094
2661
|
path9.remove();
|
|
@@ -2146,7 +2713,7 @@ function resolveASTChunks(ctx, ast) {
|
|
|
2146
2713
|
}
|
|
2147
2714
|
|
|
2148
2715
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/index.ts
|
|
2149
|
-
import * as
|
|
2716
|
+
import * as t23 from "@babel/types";
|
|
2150
2717
|
|
|
2151
2718
|
// src/core/transform/shared.ts
|
|
2152
2719
|
function recordImport(ctx, pkg, name, onDemand = true) {
|
|
@@ -2306,7 +2873,7 @@ function resolveRuntimeImports(ctx) {
|
|
|
2306
2873
|
} else if (finalModuleName === PACKAGE_NAME.react) {
|
|
2307
2874
|
path9.insertAfter(importNodes);
|
|
2308
2875
|
} else {
|
|
2309
|
-
|
|
2876
|
+
forkFilePreambleLeadingComments(importNodes[0], node);
|
|
2310
2877
|
path9.insertBefore(importNodes);
|
|
2311
2878
|
}
|
|
2312
2879
|
}
|
|
@@ -2321,7 +2888,7 @@ function resolveRuntimeImports(ctx) {
|
|
|
2321
2888
|
const { node } = path9;
|
|
2322
2889
|
const importNodes = createImportNodes(ctx);
|
|
2323
2890
|
if (!importNodes.length) return;
|
|
2324
|
-
|
|
2891
|
+
forkProgramTopLeadingComments(importNodes[0], node);
|
|
2325
2892
|
path9.unshiftContainer("body", importNodes);
|
|
2326
2893
|
}
|
|
2327
2894
|
}
|
|
@@ -2334,10 +2901,10 @@ function mergeImports(currentNode, ctx, moduleName) {
|
|
|
2334
2901
|
}
|
|
2335
2902
|
const currentImports = /* @__PURE__ */ new Set();
|
|
2336
2903
|
for (const spec of currentNode.specifiers) {
|
|
2337
|
-
if (
|
|
2904
|
+
if (t23.isImportSpecifier(spec) && t23.isIdentifier(spec.imported)) {
|
|
2338
2905
|
currentImports.add(spec.imported.name);
|
|
2339
2906
|
}
|
|
2340
|
-
if (
|
|
2907
|
+
if (t23.isImportDefaultSpecifier(spec) && t23.isIdentifier(spec.local)) {
|
|
2341
2908
|
currentImports.add(spec.local.name);
|
|
2342
2909
|
}
|
|
2343
2910
|
}
|
|
@@ -2345,8 +2912,8 @@ function mergeImports(currentNode, ctx, moduleName) {
|
|
|
2345
2912
|
if (currentImports.has(item.name)) {
|
|
2346
2913
|
continue;
|
|
2347
2914
|
}
|
|
2348
|
-
const local =
|
|
2349
|
-
const newNode = !item.onDemand ?
|
|
2915
|
+
const local = t23.identifier(item.name);
|
|
2916
|
+
const newNode = !item.onDemand ? t23.importDefaultSpecifier(local) : t23.importSpecifier(local, local);
|
|
2350
2917
|
currentNode.specifiers.push(newNode);
|
|
2351
2918
|
}
|
|
2352
2919
|
ctx.imports.delete(moduleName);
|
|
@@ -2355,10 +2922,10 @@ function createImportNodes(ctx) {
|
|
|
2355
2922
|
const result = [];
|
|
2356
2923
|
const importDeclarations = Array.from(ctx.imports).map(([moduleName, items]) => {
|
|
2357
2924
|
const specifiers = items.map((item) => {
|
|
2358
|
-
const local =
|
|
2359
|
-
return item.onDemand ?
|
|
2925
|
+
const local = t23.identifier(item.name);
|
|
2926
|
+
return item.onDemand ? t23.importSpecifier(local, local) : t23.importDefaultSpecifier(local);
|
|
2360
2927
|
});
|
|
2361
|
-
return
|
|
2928
|
+
return t23.importDeclaration(specifiers, t23.stringLiteral(moduleName));
|
|
2362
2929
|
});
|
|
2363
2930
|
for (const decl of importDeclarations) {
|
|
2364
2931
|
const name = decl.source.value;
|
|
@@ -2370,33 +2937,57 @@ function createImportNodes(ctx) {
|
|
|
2370
2937
|
}
|
|
2371
2938
|
return result;
|
|
2372
2939
|
}
|
|
2373
|
-
function
|
|
2940
|
+
function forkProgramTopLeadingComments(target, program3) {
|
|
2941
|
+
const [firstStatement] = program3.body;
|
|
2942
|
+
if (!firstStatement) {
|
|
2943
|
+
return;
|
|
2944
|
+
}
|
|
2945
|
+
forkFilePreambleLeadingComments(target, firstStatement);
|
|
2946
|
+
}
|
|
2947
|
+
function forkFilePreambleLeadingComments(target, source) {
|
|
2374
2948
|
const { leadingComments } = source;
|
|
2375
2949
|
if (!leadingComments?.length) {
|
|
2376
2950
|
return;
|
|
2377
2951
|
}
|
|
2378
|
-
const
|
|
2379
|
-
|
|
2380
|
-
|
|
2952
|
+
const commentsToMove = [];
|
|
2953
|
+
const remainingComments = [];
|
|
2954
|
+
for (const comment of leadingComments) {
|
|
2955
|
+
if (isFilePreambleComment(comment.value)) {
|
|
2956
|
+
commentsToMove.push(comment);
|
|
2957
|
+
} else {
|
|
2958
|
+
remainingComments.push(comment);
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2961
|
+
if (!commentsToMove.length) {
|
|
2962
|
+
return;
|
|
2963
|
+
}
|
|
2964
|
+
source.leadingComments = remainingComments.length ? remainingComments : null;
|
|
2965
|
+
target.leadingComments = [...target.leadingComments ?? [], ...commentsToMove];
|
|
2966
|
+
}
|
|
2967
|
+
function isFilePreambleComment(commentValue) {
|
|
2968
|
+
const value = commentValue.trim();
|
|
2969
|
+
return /^(?:@ts-(?:nocheck|check)\b|eslint-(?:disable|enable|disable-next-line|env|global)\b|@jsx(?:ImportSource|Runtime)?\b|!)/.test(
|
|
2970
|
+
value
|
|
2971
|
+
);
|
|
2381
2972
|
}
|
|
2382
2973
|
|
|
2383
2974
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-sfc-css-import.ts
|
|
2384
|
-
import * as
|
|
2975
|
+
import * as t24 from "@babel/types";
|
|
2385
2976
|
function resolveSfcCssImport(ctx) {
|
|
2386
2977
|
if (ctx.inputType !== "sfc") return;
|
|
2387
2978
|
const scriptIR = getScriptIR(ctx);
|
|
2388
2979
|
const { filePath, moduleName } = ctx.styleData;
|
|
2389
2980
|
if (!filePath) return;
|
|
2390
2981
|
const styleFilename = normalizePath(filePath).split("/").pop();
|
|
2391
|
-
const importDecl =
|
|
2392
|
-
!moduleName ? [] : [
|
|
2393
|
-
|
|
2982
|
+
const importDecl = t24.importDeclaration(
|
|
2983
|
+
!moduleName ? [] : [t24.importDefaultSpecifier(t24.identifier(moduleName))],
|
|
2984
|
+
t24.stringLiteral(`./${styleFilename}`)
|
|
2394
2985
|
);
|
|
2395
2986
|
scriptIR.imports.push(importDecl);
|
|
2396
2987
|
}
|
|
2397
2988
|
|
|
2398
2989
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2399
|
-
import * as
|
|
2990
|
+
import * as t25 from "@babel/types";
|
|
2400
2991
|
function resolveDefineAsyncComponent(ctx) {
|
|
2401
2992
|
return {
|
|
2402
2993
|
CallExpression(path9) {
|
|
@@ -2405,119 +2996,43 @@ function resolveDefineAsyncComponent(ctx) {
|
|
|
2405
2996
|
return;
|
|
2406
2997
|
}
|
|
2407
2998
|
const [arg] = node.arguments;
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
function checkIsUnsupported(ctx, arg) {
|
|
2415
|
-
if (t20.isFunction(arg)) {
|
|
2416
|
-
checkIsDynamicImport(ctx, arg);
|
|
2417
|
-
} else if (t20.isObjectExpression(arg)) {
|
|
2418
|
-
const { value } = arg.properties.find(
|
|
2419
|
-
(p) => t20.isObjectProperty(p) && t20.isIdentifier(p.key) && p.key.name === "loader"
|
|
2420
|
-
);
|
|
2421
|
-
checkIsDynamicImport(ctx, value);
|
|
2422
|
-
if (arg.properties.length > 1) {
|
|
2423
|
-
warnMultipleOptionsUsed(ctx, arg);
|
|
2424
|
-
}
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
|
-
function checkIsDynamicImport(ctx, node) {
|
|
2428
|
-
const { scriptData, filename } = ctx;
|
|
2429
|
-
const warnIsNotImport = (target) => {
|
|
2430
|
-
if (!target || !t20.isImport(target)) {
|
|
2431
|
-
logger.error(
|
|
2432
|
-
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2433
|
-
{
|
|
2434
|
-
source: scriptData.source,
|
|
2435
|
-
file: filename,
|
|
2436
|
-
loc: target?.loc || {}
|
|
2999
|
+
if (!t25.isObjectExpression(arg)) {
|
|
3000
|
+
return;
|
|
3001
|
+
}
|
|
3002
|
+
for (const prop of arg.properties) {
|
|
3003
|
+
if (!t25.isObjectProperty(prop) || !t25.isIdentifier(prop.key) || prop.key.name !== "hydrate") {
|
|
3004
|
+
continue;
|
|
2437
3005
|
}
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
if (t20.isBlockStatement(node)) {
|
|
2446
|
-
const [returnSmt] = node.body;
|
|
2447
|
-
if (t20.isReturnStatement(returnSmt)) {
|
|
2448
|
-
warnIsNotImport(returnSmt.argument);
|
|
2449
|
-
}
|
|
2450
|
-
return;
|
|
2451
|
-
}
|
|
2452
|
-
if (t20.isCallExpression(node)) {
|
|
2453
|
-
warnIsNotImport(node.callee);
|
|
2454
|
-
if (t20.isStringLiteral(node.arguments[0])) {
|
|
2455
|
-
replaceVueSuffix(node.arguments[0]);
|
|
2456
|
-
}
|
|
2457
|
-
return;
|
|
2458
|
-
}
|
|
2459
|
-
warnIsNotImport(node);
|
|
2460
|
-
}
|
|
2461
|
-
function warnMultipleOptionsUsed(ctx, node) {
|
|
2462
|
-
const { scriptData, filename } = ctx;
|
|
2463
|
-
logger.warn(
|
|
2464
|
-
"Only the loader option is supported. Other options may be implemented manually based on your needs.",
|
|
2465
|
-
{
|
|
2466
|
-
source: scriptData.source,
|
|
2467
|
-
file: filename,
|
|
2468
|
-
loc: node.loc
|
|
3006
|
+
logger.warn('Unsupported option "hydrate"', {
|
|
3007
|
+
file: ctx.filename,
|
|
3008
|
+
source: ctx.scriptData.source,
|
|
3009
|
+
loc: prop.key.loc
|
|
3010
|
+
});
|
|
3011
|
+
break;
|
|
3012
|
+
}
|
|
2469
3013
|
}
|
|
2470
|
-
|
|
2471
|
-
}
|
|
2472
|
-
function pushToGlobalScope(path9, ctx) {
|
|
2473
|
-
const { node } = path9;
|
|
2474
|
-
const callee = node.callee;
|
|
2475
|
-
callee.name = REACT_API_MAP.lazy;
|
|
2476
|
-
callee.loc.identifierName = REACT_API_MAP.lazy;
|
|
2477
|
-
if (node.typeParameters) {
|
|
2478
|
-
node.typeParameters = void 0;
|
|
2479
|
-
}
|
|
2480
|
-
let declarationPath = path9.parentPath;
|
|
2481
|
-
while (declarationPath) {
|
|
2482
|
-
if (declarationPath.isVariableDeclaration()) {
|
|
2483
|
-
break;
|
|
2484
|
-
}
|
|
2485
|
-
declarationPath = declarationPath.parentPath;
|
|
2486
|
-
}
|
|
2487
|
-
let fullNode;
|
|
2488
|
-
if (declarationPath?.isVariableDeclaration()) {
|
|
2489
|
-
fullNode = declarationPath.node;
|
|
2490
|
-
declarationPath.remove();
|
|
2491
|
-
} else if (path9.parentPath.isVariableDeclarator()) {
|
|
2492
|
-
fullNode = path9.parent;
|
|
2493
|
-
path9.parentPath.remove();
|
|
2494
|
-
} else {
|
|
2495
|
-
fullNode = path9.node;
|
|
2496
|
-
path9.remove();
|
|
2497
|
-
}
|
|
2498
|
-
const scriptIR = getScriptIR(ctx);
|
|
2499
|
-
scriptIR.statement.global.push(fullNode);
|
|
3014
|
+
};
|
|
2500
3015
|
}
|
|
2501
3016
|
|
|
2502
3017
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2503
|
-
import * as
|
|
3018
|
+
import * as t27 from "@babel/types";
|
|
2504
3019
|
|
|
2505
3020
|
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2506
|
-
import * as
|
|
3021
|
+
import * as t26 from "@babel/types";
|
|
2507
3022
|
function createUseCallback(body, deps) {
|
|
2508
|
-
return
|
|
3023
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useCallback), [
|
|
2509
3024
|
body,
|
|
2510
|
-
deps ??
|
|
3025
|
+
deps ?? t26.arrayExpression([])
|
|
2511
3026
|
]);
|
|
2512
3027
|
}
|
|
2513
3028
|
function createUseMemo(body, deps) {
|
|
2514
|
-
return
|
|
2515
|
-
|
|
2516
|
-
deps ??
|
|
3029
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useMemo), [
|
|
3030
|
+
t26.arrowFunctionExpression([], body),
|
|
3031
|
+
deps ?? t26.arrayExpression([])
|
|
2517
3032
|
]);
|
|
2518
3033
|
}
|
|
2519
3034
|
function createUseImperativeHandle(refId, init) {
|
|
2520
|
-
return
|
|
3035
|
+
return t26.callExpression(t26.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2521
3036
|
}
|
|
2522
3037
|
|
|
2523
3038
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
@@ -2538,24 +3053,23 @@ function resolveDefineExpose(ctx) {
|
|
|
2538
3053
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2539
3054
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2540
3055
|
recordImport(ctx, adapter.package, adapter.target);
|
|
2541
|
-
if (!
|
|
3056
|
+
if (!t27.isObjectExpression(expose) && !t27.isFunction(expose)) {
|
|
2542
3057
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2543
3058
|
file: filename,
|
|
2544
3059
|
loc: expose.loc,
|
|
2545
3060
|
source: scriptData.source
|
|
2546
3061
|
});
|
|
2547
3062
|
}
|
|
2548
|
-
const init = !
|
|
3063
|
+
const init = !t27.isFunction(expose) ? t27.arrowFunctionExpression([], expose) : expose;
|
|
2549
3064
|
const { forwardRef } = scriptData;
|
|
2550
|
-
const newNode = createUseImperativeHandle(
|
|
3065
|
+
const newNode = createUseImperativeHandle(t27.identifier(forwardRef.refField), init);
|
|
2551
3066
|
forwardRef.enabled = true;
|
|
2552
|
-
path9
|
|
3067
|
+
replaceNode(path9, newNode, node);
|
|
2553
3068
|
}
|
|
2554
3069
|
};
|
|
2555
3070
|
}
|
|
2556
3071
|
|
|
2557
3072
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2558
|
-
import * as t23 from "@babel/types";
|
|
2559
3073
|
function resolveDefineOptions(ctx) {
|
|
2560
3074
|
return {
|
|
2561
3075
|
CallExpression(path9) {
|
|
@@ -2563,53 +3077,13 @@ function resolveDefineOptions(ctx) {
|
|
|
2563
3077
|
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
2564
3078
|
return;
|
|
2565
3079
|
}
|
|
2566
|
-
const [options] = node.arguments;
|
|
2567
|
-
const { filename, scriptData } = ctx;
|
|
2568
|
-
if (!options) {
|
|
2569
|
-
logger.warn("defineOptions was found, but no options were provided.", {
|
|
2570
|
-
source: scriptData.source,
|
|
2571
|
-
file: filename,
|
|
2572
|
-
loc: node?.loc
|
|
2573
|
-
});
|
|
2574
|
-
path9.remove();
|
|
2575
|
-
return;
|
|
2576
|
-
}
|
|
2577
|
-
if (!t23.isObjectExpression(options)) {
|
|
2578
|
-
logger.warn("Argument for defineOptions must be an object expression.", {
|
|
2579
|
-
source: scriptData.source,
|
|
2580
|
-
file: filename,
|
|
2581
|
-
loc: options?.loc
|
|
2582
|
-
});
|
|
2583
|
-
} else {
|
|
2584
|
-
for (const prop of options.properties) {
|
|
2585
|
-
if (!t23.isObjectProperty(prop) || !t23.isIdentifier(prop.key)) {
|
|
2586
|
-
continue;
|
|
2587
|
-
}
|
|
2588
|
-
extractName(prop, ctx);
|
|
2589
|
-
}
|
|
2590
|
-
}
|
|
2591
3080
|
path9.remove();
|
|
2592
3081
|
}
|
|
2593
3082
|
};
|
|
2594
3083
|
}
|
|
2595
|
-
function extractName(prop, ctx) {
|
|
2596
|
-
if (ctx.compName) return;
|
|
2597
|
-
const { filename, scriptData } = ctx;
|
|
2598
|
-
if (!prop.computed && prop.key.name === "name") {
|
|
2599
|
-
if (t23.isStringLiteral(prop.value)) {
|
|
2600
|
-
ctx.compName = prop.value.value;
|
|
2601
|
-
return;
|
|
2602
|
-
}
|
|
2603
|
-
logger.error(`name must be a string type, but got ${prop.value.type}`, {
|
|
2604
|
-
source: scriptData.source,
|
|
2605
|
-
file: filename,
|
|
2606
|
-
loc: prop.key?.loc
|
|
2607
|
-
});
|
|
2608
|
-
}
|
|
2609
|
-
}
|
|
2610
3084
|
|
|
2611
3085
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2612
|
-
import * as
|
|
3086
|
+
import * as t28 from "@babel/types";
|
|
2613
3087
|
function resolveEmitCalls(ctx) {
|
|
2614
3088
|
const formatEmitEventName = (raw) => {
|
|
2615
3089
|
if (raw.startsWith("update:")) {
|
|
@@ -2623,7 +3097,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2623
3097
|
CallExpression(path9) {
|
|
2624
3098
|
const { node } = path9;
|
|
2625
3099
|
const { filename, templateData, scriptData } = ctx;
|
|
2626
|
-
if (!
|
|
3100
|
+
if (!t28.isIdentifier(node.callee)) return;
|
|
2627
3101
|
const { name } = node.callee;
|
|
2628
3102
|
const checkIfFromDefineEmits = () => {
|
|
2629
3103
|
let result = false;
|
|
@@ -2635,7 +3109,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2635
3109
|
const binding = path9.scope.getBinding(name);
|
|
2636
3110
|
if (binding) {
|
|
2637
3111
|
const parent = binding.path.node;
|
|
2638
|
-
if (
|
|
3112
|
+
if (t28.isVariableDeclarator(parent) && t28.isCallExpression(parent.init) && t28.isIdentifier(parent.init.callee)) {
|
|
2639
3113
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2640
3114
|
}
|
|
2641
3115
|
}
|
|
@@ -2644,7 +3118,7 @@ function resolveEmitCalls(ctx) {
|
|
|
2644
3118
|
};
|
|
2645
3119
|
if (!checkIfFromDefineEmits()) return;
|
|
2646
3120
|
const [callee, ...args] = node.arguments;
|
|
2647
|
-
const eventName =
|
|
3121
|
+
const eventName = t28.isStringLiteral(callee) ? formatEmitEventName(callee.value) : void 0;
|
|
2648
3122
|
if (!eventName) {
|
|
2649
3123
|
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2650
3124
|
file: filename,
|
|
@@ -2654,46 +3128,46 @@ function resolveEmitCalls(ctx) {
|
|
|
2654
3128
|
path9.remove();
|
|
2655
3129
|
return;
|
|
2656
3130
|
}
|
|
2657
|
-
const propCall =
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
3131
|
+
const propCall = t28.optionalCallExpression(
|
|
3132
|
+
t28.optionalMemberExpression(
|
|
3133
|
+
t28.identifier(ctx.propField),
|
|
3134
|
+
t28.identifier(eventName),
|
|
2661
3135
|
false,
|
|
2662
3136
|
true
|
|
2663
3137
|
),
|
|
2664
3138
|
args,
|
|
2665
3139
|
true
|
|
2666
3140
|
);
|
|
2667
|
-
path9
|
|
3141
|
+
replaceNode(path9, propCall, node);
|
|
2668
3142
|
}
|
|
2669
3143
|
};
|
|
2670
3144
|
}
|
|
2671
3145
|
|
|
2672
3146
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2673
|
-
import * as
|
|
3147
|
+
import * as t36 from "@babel/types";
|
|
2674
3148
|
|
|
2675
3149
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2676
|
-
import * as
|
|
3150
|
+
import * as t30 from "@babel/types";
|
|
2677
3151
|
|
|
2678
3152
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2679
|
-
import * as
|
|
3153
|
+
import * as t29 from "@babel/types";
|
|
2680
3154
|
function cloneCallableParams(params) {
|
|
2681
3155
|
const cloneCallableParam = (param, index) => {
|
|
2682
|
-
if (
|
|
3156
|
+
if (t29.isRestElement(param)) {
|
|
2683
3157
|
const arg = param.argument;
|
|
2684
|
-
const name =
|
|
2685
|
-
const rest =
|
|
2686
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
3158
|
+
const name = t29.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
3159
|
+
const rest = t29.restElement(t29.identifier(name));
|
|
3160
|
+
rest.typeAnnotation = param.typeAnnotation || (t29.isIdentifier(arg) ? arg.typeAnnotation : null) || t29.tsTypeAnnotation(t29.tsArrayType(t29.tsAnyKeyword()));
|
|
2687
3161
|
return rest;
|
|
2688
3162
|
}
|
|
2689
|
-
if (
|
|
2690
|
-
const id =
|
|
3163
|
+
if (t29.isIdentifier(param)) {
|
|
3164
|
+
const id = t29.identifier(param.name || `arg${index}`);
|
|
2691
3165
|
id.optional = param.optional;
|
|
2692
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
3166
|
+
id.typeAnnotation = param.typeAnnotation || t29.tsTypeAnnotation(t29.tsAnyKeyword());
|
|
2693
3167
|
return id;
|
|
2694
3168
|
}
|
|
2695
|
-
const fallback =
|
|
2696
|
-
fallback.typeAnnotation =
|
|
3169
|
+
const fallback = t29.identifier(`arg${index}`);
|
|
3170
|
+
fallback.typeAnnotation = t29.tsTypeAnnotation(t29.tsAnyKeyword());
|
|
2697
3171
|
return fallback;
|
|
2698
3172
|
};
|
|
2699
3173
|
return params.map(cloneCallableParam);
|
|
@@ -2703,18 +3177,18 @@ function cloneCallableParams(params) {
|
|
|
2703
3177
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2704
3178
|
return {
|
|
2705
3179
|
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path9) {
|
|
2706
|
-
if (!
|
|
3180
|
+
if (!t30.isProgram(path9.parent)) return;
|
|
2707
3181
|
const { node } = path9;
|
|
2708
|
-
if (
|
|
2709
|
-
const typeLiteral =
|
|
3182
|
+
if (t30.isTSInterfaceDeclaration(node)) {
|
|
3183
|
+
const typeLiteral = t30.tsTypeLiteral(node.body.body);
|
|
2710
3184
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2711
3185
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2712
|
-
if (resolved &&
|
|
3186
|
+
if (resolved && t30.isTSTypeLiteral(resolved)) {
|
|
2713
3187
|
node.body.body = resolved.members;
|
|
2714
3188
|
}
|
|
2715
3189
|
return;
|
|
2716
3190
|
}
|
|
2717
|
-
if (
|
|
3191
|
+
if (t30.isTSTypeAliasDeclaration(node)) {
|
|
2718
3192
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2719
3193
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2720
3194
|
if (resolved) {
|
|
@@ -2725,47 +3199,47 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2725
3199
|
};
|
|
2726
3200
|
}
|
|
2727
3201
|
function resolveTopLevelEmitType(tsType) {
|
|
2728
|
-
if (
|
|
3202
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
2729
3203
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2730
3204
|
}
|
|
2731
|
-
if (
|
|
3205
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
2732
3206
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2733
3207
|
return tsType;
|
|
2734
3208
|
}
|
|
2735
3209
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2736
|
-
return
|
|
3210
|
+
return t30.tsTypeReference(
|
|
2737
3211
|
tsType.typeName,
|
|
2738
|
-
|
|
3212
|
+
t30.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2739
3213
|
);
|
|
2740
3214
|
}
|
|
2741
|
-
if (
|
|
3215
|
+
if (t30.isTSIntersectionType(tsType)) {
|
|
2742
3216
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2743
3217
|
if (!types.length) return null;
|
|
2744
3218
|
if (types.length === 1) return types[0];
|
|
2745
|
-
return
|
|
3219
|
+
return t30.tsIntersectionType(types);
|
|
2746
3220
|
}
|
|
2747
|
-
if (
|
|
3221
|
+
if (t30.isTSUnionType(tsType)) {
|
|
2748
3222
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2749
3223
|
if (!types.length) return null;
|
|
2750
3224
|
if (types.length === 1) return types[0];
|
|
2751
|
-
return
|
|
3225
|
+
return t30.tsUnionType(types);
|
|
2752
3226
|
}
|
|
2753
|
-
if (
|
|
3227
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
2754
3228
|
const members = [];
|
|
2755
3229
|
for (const member of tsType.members) {
|
|
2756
|
-
if (
|
|
3230
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
2757
3231
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2758
3232
|
continue;
|
|
2759
3233
|
}
|
|
2760
3234
|
members.push(member);
|
|
2761
3235
|
}
|
|
2762
3236
|
if (!members.length) return null;
|
|
2763
|
-
return
|
|
3237
|
+
return t30.tsTypeLiteral(members);
|
|
2764
3238
|
}
|
|
2765
|
-
if (
|
|
3239
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
2766
3240
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2767
3241
|
if (!props.length) return null;
|
|
2768
|
-
return
|
|
3242
|
+
return t30.tsTypeLiteral(props);
|
|
2769
3243
|
}
|
|
2770
3244
|
return tsType;
|
|
2771
3245
|
}
|
|
@@ -2786,41 +3260,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2786
3260
|
propsTSIface: { emitTypes }
|
|
2787
3261
|
} = ctx.scriptData;
|
|
2788
3262
|
const members = [];
|
|
2789
|
-
if (
|
|
3263
|
+
if (t30.isArrayExpression(runtimeArg)) {
|
|
2790
3264
|
for (const element of runtimeArg.elements) {
|
|
2791
|
-
if (!element || !
|
|
3265
|
+
if (!element || !t30.isStringLiteral(element)) continue;
|
|
2792
3266
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2793
3267
|
const key = buildKey(handlerName);
|
|
2794
|
-
const fnType =
|
|
3268
|
+
const fnType = t30.tsFunctionType(
|
|
2795
3269
|
null,
|
|
2796
3270
|
[createRestAnyParam("args")],
|
|
2797
|
-
|
|
3271
|
+
t30.tsTypeAnnotation(t30.tsAnyKeyword())
|
|
2798
3272
|
);
|
|
2799
|
-
const prop =
|
|
3273
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
2800
3274
|
prop.optional = true;
|
|
2801
3275
|
members.push(prop);
|
|
2802
3276
|
}
|
|
2803
3277
|
if (members.length) {
|
|
2804
|
-
emitTypes.push(
|
|
3278
|
+
emitTypes.push(t30.tsTypeLiteral(members));
|
|
2805
3279
|
}
|
|
2806
3280
|
return;
|
|
2807
3281
|
}
|
|
2808
|
-
if (
|
|
3282
|
+
if (t30.isObjectExpression(runtimeArg)) {
|
|
2809
3283
|
for (const prop of runtimeArg.properties) {
|
|
2810
|
-
if (!
|
|
2811
|
-
if (
|
|
3284
|
+
if (!t30.isObjectProperty(prop)) continue;
|
|
3285
|
+
if (t30.isSpreadElement(prop)) continue;
|
|
2812
3286
|
const rawName = resolvePropName(prop.key);
|
|
2813
3287
|
if (!rawName) continue;
|
|
2814
3288
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2815
3289
|
const key = buildKey(handlerName);
|
|
2816
|
-
const params =
|
|
2817
|
-
const fnType =
|
|
2818
|
-
const propSig =
|
|
3290
|
+
const params = t30.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
3291
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(t30.tsAnyKeyword()));
|
|
3292
|
+
const propSig = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
2819
3293
|
propSig.optional = true;
|
|
2820
3294
|
members.push(propSig);
|
|
2821
3295
|
}
|
|
2822
3296
|
if (members.length) {
|
|
2823
|
-
emitTypes.push(
|
|
3297
|
+
emitTypes.push(t30.tsTypeLiteral(members));
|
|
2824
3298
|
}
|
|
2825
3299
|
}
|
|
2826
3300
|
}
|
|
@@ -2841,129 +3315,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2841
3315
|
return `on${name}`;
|
|
2842
3316
|
}
|
|
2843
3317
|
function resolvePropName(key) {
|
|
2844
|
-
if (
|
|
2845
|
-
if (
|
|
2846
|
-
if (
|
|
3318
|
+
if (t30.isIdentifier(key)) return key.name;
|
|
3319
|
+
if (t30.isStringLiteral(key)) return key.value;
|
|
3320
|
+
if (t30.isNumericLiteral(key)) return String(key.value);
|
|
2847
3321
|
return null;
|
|
2848
3322
|
}
|
|
2849
3323
|
function buildKey(name) {
|
|
2850
|
-
return
|
|
3324
|
+
return t30.isValidIdentifier(name) ? t30.identifier(name) : t30.stringLiteral(name);
|
|
2851
3325
|
}
|
|
2852
3326
|
function createRestAnyParam(name) {
|
|
2853
|
-
const id =
|
|
2854
|
-
const rest =
|
|
2855
|
-
rest.typeAnnotation =
|
|
3327
|
+
const id = t30.identifier(name);
|
|
3328
|
+
const rest = t30.restElement(id);
|
|
3329
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(t30.tsArrayType(t30.tsAnyKeyword()));
|
|
2856
3330
|
return rest;
|
|
2857
3331
|
}
|
|
2858
3332
|
function resolveRuntimeTupleParams(value) {
|
|
2859
3333
|
const params = [];
|
|
2860
3334
|
value.elements.forEach((element, index) => {
|
|
2861
3335
|
if (!element) return;
|
|
2862
|
-
if (
|
|
3336
|
+
if (t30.isSpreadElement(element)) {
|
|
2863
3337
|
params.push(createRestAnyParam(`args${index}`));
|
|
2864
3338
|
return;
|
|
2865
3339
|
}
|
|
2866
|
-
if (
|
|
2867
|
-
const id =
|
|
2868
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
3340
|
+
if (t30.isIdentifier(element)) {
|
|
3341
|
+
const id = t30.identifier(element.name);
|
|
3342
|
+
id.typeAnnotation = element.typeAnnotation || t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
2869
3343
|
params.push(id);
|
|
2870
3344
|
return;
|
|
2871
3345
|
}
|
|
2872
|
-
if (
|
|
2873
|
-
const id =
|
|
2874
|
-
id.typeAnnotation =
|
|
3346
|
+
if (t30.isTSAsExpression(element)) {
|
|
3347
|
+
const id = t30.identifier(`arg${index}`);
|
|
3348
|
+
id.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
2875
3349
|
params.push(id);
|
|
2876
3350
|
return;
|
|
2877
3351
|
}
|
|
2878
|
-
const fallback =
|
|
2879
|
-
fallback.typeAnnotation =
|
|
3352
|
+
const fallback = t30.identifier(`arg${index}`);
|
|
3353
|
+
fallback.typeAnnotation = t30.tsTypeAnnotation(t30.tsAnyKeyword());
|
|
2880
3354
|
params.push(fallback);
|
|
2881
3355
|
});
|
|
2882
3356
|
return params;
|
|
2883
3357
|
}
|
|
2884
3358
|
function resolveExplicitEmitType(tsType) {
|
|
2885
|
-
if (
|
|
3359
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
2886
3360
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2887
3361
|
}
|
|
2888
|
-
if (
|
|
3362
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
2889
3363
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2890
3364
|
return tsType;
|
|
2891
3365
|
}
|
|
2892
3366
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2893
|
-
return
|
|
3367
|
+
return t30.tsTypeReference(
|
|
2894
3368
|
tsType.typeName,
|
|
2895
|
-
|
|
3369
|
+
t30.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2896
3370
|
);
|
|
2897
3371
|
}
|
|
2898
|
-
if (
|
|
3372
|
+
if (t30.isTSIntersectionType(tsType)) {
|
|
2899
3373
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2900
3374
|
if (!types.length) return null;
|
|
2901
3375
|
if (types.length === 1) return types[0];
|
|
2902
|
-
return
|
|
3376
|
+
return t30.tsIntersectionType(types);
|
|
2903
3377
|
}
|
|
2904
|
-
if (
|
|
3378
|
+
if (t30.isTSUnionType(tsType)) {
|
|
2905
3379
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2906
3380
|
if (!types.length) return null;
|
|
2907
3381
|
if (types.length === 1) return types[0];
|
|
2908
|
-
return
|
|
3382
|
+
return t30.tsUnionType(types);
|
|
2909
3383
|
}
|
|
2910
|
-
if (
|
|
3384
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
2911
3385
|
const members = [];
|
|
2912
3386
|
for (const member of tsType.members) {
|
|
2913
|
-
if (
|
|
3387
|
+
if (t30.isTSPropertySignature(member)) {
|
|
2914
3388
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2915
3389
|
if (prop) members.push(prop);
|
|
2916
3390
|
continue;
|
|
2917
3391
|
}
|
|
2918
|
-
if (
|
|
3392
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
2919
3393
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2920
3394
|
continue;
|
|
2921
3395
|
}
|
|
2922
3396
|
}
|
|
2923
3397
|
if (!members.length) return null;
|
|
2924
|
-
return
|
|
3398
|
+
return t30.tsTypeLiteral(members);
|
|
2925
3399
|
}
|
|
2926
|
-
if (
|
|
3400
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
2927
3401
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2928
3402
|
if (!props.length) return null;
|
|
2929
|
-
return
|
|
3403
|
+
return t30.tsTypeLiteral(props);
|
|
2930
3404
|
}
|
|
2931
3405
|
return tsType;
|
|
2932
3406
|
}
|
|
2933
3407
|
function hasEmitsSignatureInType(tsType) {
|
|
2934
|
-
if (
|
|
3408
|
+
if (t30.isTSParenthesizedType(tsType)) {
|
|
2935
3409
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
2936
3410
|
}
|
|
2937
|
-
if (
|
|
3411
|
+
if (t30.isTSTypeReference(tsType)) {
|
|
2938
3412
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2939
3413
|
return false;
|
|
2940
3414
|
}
|
|
2941
3415
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
2942
3416
|
}
|
|
2943
|
-
if (
|
|
3417
|
+
if (t30.isTSIntersectionType(tsType) || t30.isTSUnionType(tsType)) {
|
|
2944
3418
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
2945
3419
|
}
|
|
2946
|
-
if (
|
|
3420
|
+
if (t30.isTSTypeLiteral(tsType)) {
|
|
2947
3421
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
2948
3422
|
}
|
|
2949
|
-
if (
|
|
3423
|
+
if (t30.isTSFunctionType(tsType)) {
|
|
2950
3424
|
return isEmitsCallable(tsType.parameters);
|
|
2951
3425
|
}
|
|
2952
3426
|
return false;
|
|
2953
3427
|
}
|
|
2954
3428
|
function hasEmitsSignatureInMember(member) {
|
|
2955
|
-
if (
|
|
3429
|
+
if (t30.isTSCallSignatureDeclaration(member)) {
|
|
2956
3430
|
return isEmitsCallable(member.parameters);
|
|
2957
3431
|
}
|
|
2958
3432
|
return false;
|
|
2959
3433
|
}
|
|
2960
3434
|
function isEmitsCallable(parameters) {
|
|
2961
3435
|
const [eventParam] = parameters;
|
|
2962
|
-
if (!eventParam || !
|
|
3436
|
+
if (!eventParam || !t30.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2963
3437
|
return false;
|
|
2964
3438
|
}
|
|
2965
3439
|
const { typeAnnotation } = eventParam;
|
|
2966
|
-
if (
|
|
3440
|
+
if (t30.isNoop(typeAnnotation)) return false;
|
|
2967
3441
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
2968
3442
|
}
|
|
2969
3443
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -2973,19 +3447,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
2973
3447
|
const key = buildKey(handlerName);
|
|
2974
3448
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2975
3449
|
let params = [];
|
|
2976
|
-
let returnType =
|
|
2977
|
-
if (typeAnnotation &&
|
|
3450
|
+
let returnType = t30.tsAnyKeyword();
|
|
3451
|
+
if (typeAnnotation && t30.isTSFunctionType(typeAnnotation)) {
|
|
2978
3452
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
2979
3453
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
2980
|
-
} else if (typeAnnotation &&
|
|
3454
|
+
} else if (typeAnnotation && t30.isTSTupleType(typeAnnotation)) {
|
|
2981
3455
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
2982
3456
|
} else if (typeAnnotation) {
|
|
2983
|
-
const id =
|
|
2984
|
-
id.typeAnnotation =
|
|
3457
|
+
const id = t30.identifier("value");
|
|
3458
|
+
id.typeAnnotation = t30.tsTypeAnnotation(typeAnnotation);
|
|
2985
3459
|
params = [id];
|
|
2986
3460
|
}
|
|
2987
|
-
const fnType =
|
|
2988
|
-
const prop =
|
|
3461
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(returnType));
|
|
3462
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
2989
3463
|
prop.optional = !!member.optional;
|
|
2990
3464
|
return prop;
|
|
2991
3465
|
}
|
|
@@ -2994,32 +3468,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
2994
3468
|
}
|
|
2995
3469
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
2996
3470
|
const [eventParam, ...restParams] = parameters;
|
|
2997
|
-
if (!eventParam || !
|
|
3471
|
+
if (!eventParam || !t30.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2998
3472
|
return [];
|
|
2999
3473
|
}
|
|
3000
3474
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
3001
|
-
if (
|
|
3475
|
+
if (t30.isNoop(paramTypeAnnotation)) return [];
|
|
3002
3476
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
3003
3477
|
if (!eventNames.length) return [];
|
|
3004
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
3478
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t30.tsAnyKeyword();
|
|
3005
3479
|
return eventNames.map((eventName) => {
|
|
3006
3480
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
3007
3481
|
const key = buildKey(handlerName);
|
|
3008
3482
|
const params = cloneCallableParams(restParams);
|
|
3009
|
-
const fnType =
|
|
3010
|
-
const prop =
|
|
3483
|
+
const fnType = t30.tsFunctionType(null, params, t30.tsTypeAnnotation(returnType));
|
|
3484
|
+
const prop = t30.tsPropertySignature(key, t30.tsTypeAnnotation(fnType));
|
|
3011
3485
|
prop.optional = true;
|
|
3012
3486
|
return prop;
|
|
3013
3487
|
});
|
|
3014
3488
|
}
|
|
3015
3489
|
function resolveEventNames(type) {
|
|
3016
|
-
if (
|
|
3490
|
+
if (t30.isTSLiteralType(type) && t30.isStringLiteral(type.literal)) {
|
|
3017
3491
|
return [type.literal.value];
|
|
3018
3492
|
}
|
|
3019
|
-
if (
|
|
3493
|
+
if (t30.isTSUnionType(type)) {
|
|
3020
3494
|
return type.types.flatMap(resolveEventNames);
|
|
3021
3495
|
}
|
|
3022
|
-
if (
|
|
3496
|
+
if (t30.isTSParenthesizedType(type)) {
|
|
3023
3497
|
return resolveEventNames(type.typeAnnotation);
|
|
3024
3498
|
}
|
|
3025
3499
|
return [];
|
|
@@ -3032,44 +3506,44 @@ function resolveTupleTypeParams(tuple) {
|
|
|
3032
3506
|
return params;
|
|
3033
3507
|
}
|
|
3034
3508
|
function resolveTupleElementParam(element, index) {
|
|
3035
|
-
const isNamedTuple = typeof
|
|
3509
|
+
const isNamedTuple = typeof t30.isTSNamedTupleMember === "function" && t30.isTSNamedTupleMember(element);
|
|
3036
3510
|
if (isNamedTuple) {
|
|
3037
3511
|
const tupleMember = element;
|
|
3038
3512
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
3039
3513
|
let innerType = tupleMember.elementType;
|
|
3040
3514
|
let optional = tupleMember.optional;
|
|
3041
|
-
if (
|
|
3515
|
+
if (t30.isTSOptionalType(innerType)) {
|
|
3042
3516
|
optional = true;
|
|
3043
3517
|
innerType = innerType.typeAnnotation;
|
|
3044
3518
|
}
|
|
3045
|
-
if (
|
|
3046
|
-
const rest =
|
|
3047
|
-
rest.typeAnnotation =
|
|
3519
|
+
if (t30.isTSRestType(innerType)) {
|
|
3520
|
+
const rest = t30.restElement(t30.identifier(name));
|
|
3521
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(innerType.typeAnnotation);
|
|
3048
3522
|
return rest;
|
|
3049
3523
|
}
|
|
3050
|
-
const id2 =
|
|
3524
|
+
const id2 = t30.identifier(name);
|
|
3051
3525
|
id2.optional = optional;
|
|
3052
|
-
id2.typeAnnotation =
|
|
3526
|
+
id2.typeAnnotation = t30.tsTypeAnnotation(innerType);
|
|
3053
3527
|
return id2;
|
|
3054
3528
|
}
|
|
3055
|
-
if (
|
|
3056
|
-
const rest =
|
|
3057
|
-
rest.typeAnnotation =
|
|
3529
|
+
if (t30.isTSRestType(element)) {
|
|
3530
|
+
const rest = t30.restElement(t30.identifier(`args${index}`));
|
|
3531
|
+
rest.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
3058
3532
|
return rest;
|
|
3059
3533
|
}
|
|
3060
|
-
if (
|
|
3061
|
-
const id2 =
|
|
3534
|
+
if (t30.isTSOptionalType(element)) {
|
|
3535
|
+
const id2 = t30.identifier(`arg${index}`);
|
|
3062
3536
|
id2.optional = true;
|
|
3063
|
-
id2.typeAnnotation =
|
|
3537
|
+
id2.typeAnnotation = t30.tsTypeAnnotation(element.typeAnnotation);
|
|
3064
3538
|
return id2;
|
|
3065
3539
|
}
|
|
3066
|
-
const id =
|
|
3067
|
-
id.typeAnnotation =
|
|
3540
|
+
const id = t30.identifier(`arg${index}`);
|
|
3541
|
+
id.typeAnnotation = t30.tsTypeAnnotation(element);
|
|
3068
3542
|
return id;
|
|
3069
3543
|
}
|
|
3070
3544
|
|
|
3071
3545
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
3072
|
-
import * as
|
|
3546
|
+
import * as t31 from "@babel/types";
|
|
3073
3547
|
function resolveDefinePropsIface(path9, ctx) {
|
|
3074
3548
|
const { node } = path9;
|
|
3075
3549
|
const [runtimeArg] = node.arguments;
|
|
@@ -3089,38 +3563,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3089
3563
|
} = scriptData;
|
|
3090
3564
|
if (!runtimeArg) return;
|
|
3091
3565
|
const members = [];
|
|
3092
|
-
if (
|
|
3566
|
+
if (t31.isArrayExpression(runtimeArg)) {
|
|
3093
3567
|
for (const element of runtimeArg.elements) {
|
|
3094
|
-
if (!element || !
|
|
3095
|
-
const key =
|
|
3096
|
-
const prop =
|
|
3568
|
+
if (!element || !t31.isStringLiteral(element)) continue;
|
|
3569
|
+
const key = t31.isValidIdentifier(element.value) ? t31.identifier(element.value) : t31.stringLiteral(element.value);
|
|
3570
|
+
const prop = t31.tsPropertySignature(key, t31.tsTypeAnnotation(t31.tsAnyKeyword()));
|
|
3097
3571
|
prop.optional = true;
|
|
3098
3572
|
members.push(prop);
|
|
3099
3573
|
}
|
|
3100
3574
|
if (members.length) {
|
|
3101
|
-
propsTypes.push(
|
|
3575
|
+
propsTypes.push(t31.tsTypeLiteral(members));
|
|
3102
3576
|
}
|
|
3103
3577
|
return;
|
|
3104
3578
|
}
|
|
3105
|
-
if (
|
|
3579
|
+
if (t31.isObjectExpression(runtimeArg)) {
|
|
3106
3580
|
for (const prop of runtimeArg.properties) {
|
|
3107
|
-
if (!
|
|
3108
|
-
if (
|
|
3581
|
+
if (!t31.isObjectProperty(prop)) continue;
|
|
3582
|
+
if (t31.isSpreadElement(prop)) continue;
|
|
3109
3583
|
const key = prop.key;
|
|
3110
3584
|
let propName = null;
|
|
3111
|
-
if (
|
|
3112
|
-
if (
|
|
3113
|
-
if (
|
|
3585
|
+
if (t31.isIdentifier(key)) propName = key.name;
|
|
3586
|
+
if (t31.isStringLiteral(key)) propName = key.value;
|
|
3587
|
+
if (t31.isNumericLiteral(key)) propName = String(key.value);
|
|
3114
3588
|
if (!propName) continue;
|
|
3115
3589
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
3116
|
-
const tsType = type ??
|
|
3117
|
-
const tsKey =
|
|
3118
|
-
const tsProp =
|
|
3590
|
+
const tsType = type ?? t31.tsAnyKeyword();
|
|
3591
|
+
const tsKey = t31.isValidIdentifier(propName) ? t31.identifier(propName) : t31.stringLiteral(propName);
|
|
3592
|
+
const tsProp = t31.tsPropertySignature(tsKey, t31.tsTypeAnnotation(tsType));
|
|
3119
3593
|
tsProp.optional = !required;
|
|
3120
3594
|
members.push(tsProp);
|
|
3121
3595
|
}
|
|
3122
3596
|
if (members.length) {
|
|
3123
|
-
propsTypes.push(
|
|
3597
|
+
propsTypes.push(t31.tsTypeLiteral(members));
|
|
3124
3598
|
}
|
|
3125
3599
|
return;
|
|
3126
3600
|
}
|
|
@@ -3134,42 +3608,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3134
3608
|
);
|
|
3135
3609
|
}
|
|
3136
3610
|
function resolveRuntimePropMeta(value) {
|
|
3137
|
-
if (
|
|
3611
|
+
if (t31.isIdentifier(value)) {
|
|
3138
3612
|
return {
|
|
3139
3613
|
type: mapRuntimeTypeToTSType(value),
|
|
3140
3614
|
required: false
|
|
3141
3615
|
};
|
|
3142
3616
|
}
|
|
3143
|
-
if (
|
|
3617
|
+
if (t31.isArrayExpression(value)) {
|
|
3144
3618
|
return {
|
|
3145
3619
|
type: resolveRuntimeUnionType(value),
|
|
3146
3620
|
required: false
|
|
3147
3621
|
};
|
|
3148
3622
|
}
|
|
3149
|
-
if (!
|
|
3623
|
+
if (!t31.isObjectExpression(value)) {
|
|
3150
3624
|
return { required: false };
|
|
3151
3625
|
}
|
|
3152
3626
|
let type;
|
|
3153
3627
|
let required = false;
|
|
3154
3628
|
for (const prop of value.properties) {
|
|
3155
|
-
if (!
|
|
3156
|
-
if (
|
|
3629
|
+
if (!t31.isObjectProperty(prop)) continue;
|
|
3630
|
+
if (t31.isSpreadElement(prop)) continue;
|
|
3157
3631
|
const key = prop.key;
|
|
3158
|
-
const propName =
|
|
3632
|
+
const propName = t31.isIdentifier(key) ? key.name : t31.isStringLiteral(key) ? key.value : null;
|
|
3159
3633
|
if (!propName) continue;
|
|
3160
3634
|
if (propName === "type") {
|
|
3161
3635
|
const valueNode = prop.value;
|
|
3162
|
-
if (
|
|
3636
|
+
if (t31.isArrayExpression(valueNode)) {
|
|
3163
3637
|
type = resolveRuntimeUnionType(valueNode);
|
|
3164
3638
|
continue;
|
|
3165
3639
|
}
|
|
3166
|
-
if (
|
|
3640
|
+
if (t31.isIdentifier(valueNode)) {
|
|
3167
3641
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
3168
3642
|
continue;
|
|
3169
3643
|
}
|
|
3170
3644
|
}
|
|
3171
3645
|
if (propName === "required") {
|
|
3172
|
-
if (
|
|
3646
|
+
if (t31.isBooleanLiteral(prop.value)) {
|
|
3173
3647
|
required = prop.value.value;
|
|
3174
3648
|
}
|
|
3175
3649
|
}
|
|
@@ -3179,257 +3653,403 @@ function resolveRuntimePropMeta(value) {
|
|
|
3179
3653
|
function resolveRuntimeUnionType(value) {
|
|
3180
3654
|
const types = [];
|
|
3181
3655
|
for (const element of value.elements) {
|
|
3182
|
-
if (!element || !
|
|
3656
|
+
if (!element || !t31.isIdentifier(element)) continue;
|
|
3183
3657
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
3184
3658
|
if (resolved) types.push(resolved);
|
|
3185
3659
|
}
|
|
3186
|
-
if (!types.length) return
|
|
3660
|
+
if (!types.length) return t31.tsAnyKeyword();
|
|
3187
3661
|
if (types.length === 1) return types[0];
|
|
3188
|
-
return
|
|
3662
|
+
return t31.tsUnionType(types);
|
|
3189
3663
|
}
|
|
3190
3664
|
function mapRuntimeTypeToTSType(value) {
|
|
3191
3665
|
switch (value.name) {
|
|
3192
3666
|
case "String":
|
|
3193
|
-
return
|
|
3667
|
+
return t31.tsStringKeyword();
|
|
3194
3668
|
case "Number":
|
|
3195
|
-
return
|
|
3669
|
+
return t31.tsNumberKeyword();
|
|
3196
3670
|
case "Boolean":
|
|
3197
|
-
return
|
|
3671
|
+
return t31.tsBooleanKeyword();
|
|
3198
3672
|
case "Object":
|
|
3199
|
-
return
|
|
3673
|
+
return t31.tsTypeLiteral([]);
|
|
3200
3674
|
case "Array":
|
|
3201
|
-
return
|
|
3675
|
+
return t31.tsArrayType(t31.tsAnyKeyword());
|
|
3202
3676
|
case "Function":
|
|
3203
|
-
return
|
|
3677
|
+
return t31.tsFunctionType(null, [], t31.tsTypeAnnotation(t31.tsAnyKeyword()));
|
|
3204
3678
|
case "Symbol":
|
|
3205
|
-
return
|
|
3679
|
+
return t31.tsSymbolKeyword();
|
|
3206
3680
|
case "BigInt":
|
|
3207
|
-
return
|
|
3681
|
+
return t31.tsBigIntKeyword();
|
|
3208
3682
|
default:
|
|
3209
|
-
return
|
|
3683
|
+
return t31.tsAnyKeyword();
|
|
3210
3684
|
}
|
|
3211
3685
|
}
|
|
3212
3686
|
|
|
3213
|
-
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
3214
|
-
import * as
|
|
3687
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
3688
|
+
import * as t34 from "@babel/types";
|
|
3689
|
+
|
|
3690
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/slot-builder.ts
|
|
3691
|
+
import * as t32 from "@babel/types";
|
|
3215
3692
|
var SLOT_DEFAULT_NAME = "default";
|
|
3216
3693
|
var SLOT_CHILDREN_NAME = "children";
|
|
3217
3694
|
var SLOT_FN_PARAM_NAME = "props";
|
|
3218
|
-
function
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
if (resolved && t28.isTSTypeLiteral(resolved)) {
|
|
3231
|
-
node.body.body = resolved.members;
|
|
3232
|
-
recordReactNode(ctx);
|
|
3233
|
-
}
|
|
3234
|
-
return;
|
|
3235
|
-
}
|
|
3236
|
-
if (t28.isTSTypeAliasDeclaration(node)) {
|
|
3237
|
-
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
3238
|
-
const resolved = resolveSlotType(node.typeAnnotation);
|
|
3239
|
-
if (resolved) {
|
|
3240
|
-
node.typeAnnotation = resolved;
|
|
3241
|
-
recordReactNode(ctx);
|
|
3242
|
-
}
|
|
3243
|
-
}
|
|
3244
|
-
}
|
|
3245
|
-
};
|
|
3246
|
-
}
|
|
3247
|
-
function resolveDefineSlotsIface(path9, ctx) {
|
|
3248
|
-
const { node } = path9;
|
|
3249
|
-
const tsParams = node.typeParameters?.params;
|
|
3250
|
-
if (!tsParams?.length) return;
|
|
3251
|
-
const {
|
|
3252
|
-
propsTSIface: { slotTypes }
|
|
3253
|
-
} = ctx.scriptData;
|
|
3254
|
-
for (const tsType of tsParams) {
|
|
3255
|
-
const resolved = resolveSlotType(tsType);
|
|
3256
|
-
if (resolved) {
|
|
3257
|
-
slotTypes.push(resolved);
|
|
3258
|
-
}
|
|
3259
|
-
}
|
|
3260
|
-
if (slotTypes.length) {
|
|
3261
|
-
recordReactNode(ctx);
|
|
3695
|
+
function buildSlotPropSignature(rawName, params, optional) {
|
|
3696
|
+
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
3697
|
+
const key = t32.isValidIdentifier(propName) ? t32.identifier(propName) : t32.stringLiteral(propName);
|
|
3698
|
+
const reactNodeType = t32.tsTypeAnnotation(
|
|
3699
|
+
t32.tsTypeReference(t32.identifier(REACT_API_MAP.ReactNode))
|
|
3700
|
+
);
|
|
3701
|
+
let typeAnnotation;
|
|
3702
|
+
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
3703
|
+
typeAnnotation = reactNodeType;
|
|
3704
|
+
} else {
|
|
3705
|
+
const fnType = t32.tsFunctionType(null, params, reactNodeType);
|
|
3706
|
+
typeAnnotation = t32.tsTypeAnnotation(fnType);
|
|
3262
3707
|
}
|
|
3708
|
+
const prop = t32.tsPropertySignature(key, typeAnnotation);
|
|
3709
|
+
prop.optional = optional;
|
|
3710
|
+
return prop;
|
|
3263
3711
|
}
|
|
3264
|
-
function
|
|
3265
|
-
|
|
3266
|
-
const
|
|
3267
|
-
|
|
3268
|
-
}
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
const
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
}
|
|
3279
|
-
if (tsMembers.length) {
|
|
3280
|
-
recordReactNode(ctx);
|
|
3281
|
-
slotTypes.push(t28.tsTypeLiteral(tsMembers));
|
|
3282
|
-
}
|
|
3712
|
+
function createSlotScopeParam(props, ctx) {
|
|
3713
|
+
const paramId = t32.identifier(SLOT_FN_PARAM_NAME);
|
|
3714
|
+
const propsSigns = [];
|
|
3715
|
+
const { reactiveBindings } = ctx.templateData;
|
|
3716
|
+
props.forEach(({ prop, tsType }) => {
|
|
3717
|
+
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3718
|
+
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3719
|
+
const typeAnnotation = foundBindingTypes ? t32.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3720
|
+
const key = t32.isValidIdentifier(prop) ? t32.identifier(prop) : t32.stringLiteral(prop);
|
|
3721
|
+
const propSign = t32.tsPropertySignature(key, typeAnnotation);
|
|
3722
|
+
propsSigns.push(propSign);
|
|
3723
|
+
});
|
|
3724
|
+
paramId.typeAnnotation = t32.tsTypeAnnotation(t32.tsTypeLiteral(propsSigns));
|
|
3725
|
+
return paramId;
|
|
3283
3726
|
}
|
|
3727
|
+
|
|
3728
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/utils.ts
|
|
3729
|
+
import * as t33 from "@babel/types";
|
|
3284
3730
|
function recordReactNode(ctx) {
|
|
3285
3731
|
if (!ctx.scriptData.lang.startsWith("ts")) {
|
|
3286
3732
|
return;
|
|
3287
3733
|
}
|
|
3288
3734
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
3289
3735
|
}
|
|
3290
|
-
function
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3736
|
+
function collectLocalTypeDeclarations(path9) {
|
|
3737
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
3738
|
+
const programPath = path9.findParent(
|
|
3739
|
+
(parentPath) => parentPath.isProgram()
|
|
3740
|
+
);
|
|
3741
|
+
if (!programPath) {
|
|
3742
|
+
return declarations;
|
|
3743
|
+
}
|
|
3744
|
+
for (const statement of programPath.node.body) {
|
|
3745
|
+
if (t33.isTSInterfaceDeclaration(statement)) {
|
|
3746
|
+
declarations.set(statement.id.name, {
|
|
3747
|
+
type: t33.tsTypeLiteral(statement.body.body),
|
|
3748
|
+
// 将接口体转换为类型字面量
|
|
3749
|
+
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
3750
|
+
// 检查是否有泛型参数
|
|
3751
|
+
});
|
|
3752
|
+
continue;
|
|
3297
3753
|
}
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
if (
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3754
|
+
if (t33.isTSTypeAliasDeclaration(statement)) {
|
|
3755
|
+
declarations.set(statement.id.name, {
|
|
3756
|
+
type: statement.typeAnnotation,
|
|
3757
|
+
// 直接使用类型注解
|
|
3758
|
+
hasTypeParameters: !!statement.typeParameters?.params.length
|
|
3759
|
+
// 检查是否有泛型参数
|
|
3760
|
+
});
|
|
3761
|
+
continue;
|
|
3762
|
+
}
|
|
3763
|
+
if (t33.isExportNamedDeclaration(statement) && statement.declaration) {
|
|
3764
|
+
const declaration = statement.declaration;
|
|
3765
|
+
if (t33.isTSInterfaceDeclaration(declaration)) {
|
|
3766
|
+
declarations.set(declaration.id.name, {
|
|
3767
|
+
type: t33.tsTypeLiteral(declaration.body.body),
|
|
3768
|
+
// 将接口体转换为类型字面量
|
|
3769
|
+
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
3770
|
+
// 检查是否有泛型参数
|
|
3771
|
+
});
|
|
3772
|
+
} else if (t33.isTSTypeAliasDeclaration(declaration)) {
|
|
3773
|
+
declarations.set(declaration.id.name, {
|
|
3774
|
+
type: declaration.typeAnnotation,
|
|
3775
|
+
// 直接使用类型注解
|
|
3776
|
+
hasTypeParameters: !!declaration.typeParameters?.params.length
|
|
3777
|
+
// 检查是否有泛型参数
|
|
3778
|
+
});
|
|
3323
3779
|
}
|
|
3324
|
-
members.push(member);
|
|
3325
3780
|
}
|
|
3326
|
-
if (!members.length) return null;
|
|
3327
|
-
return t28.tsTypeLiteral(members);
|
|
3328
3781
|
}
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
);
|
|
3335
|
-
return t28.tsTypeLiteral([props]);
|
|
3782
|
+
return declarations;
|
|
3783
|
+
}
|
|
3784
|
+
function resolvePropName2(key) {
|
|
3785
|
+
if (t33.isIdentifier(key)) {
|
|
3786
|
+
return key.name;
|
|
3336
3787
|
}
|
|
3337
|
-
|
|
3788
|
+
if (t33.isStringLiteral(key)) {
|
|
3789
|
+
return key.value;
|
|
3790
|
+
}
|
|
3791
|
+
if (t33.isNumericLiteral(key)) {
|
|
3792
|
+
return String(key.value);
|
|
3793
|
+
}
|
|
3794
|
+
return null;
|
|
3338
3795
|
}
|
|
3339
|
-
function
|
|
3340
|
-
if (
|
|
3341
|
-
return
|
|
3796
|
+
function resolveCallableType(tsType) {
|
|
3797
|
+
if (t33.isTSFunctionType(tsType)) {
|
|
3798
|
+
return tsType;
|
|
3342
3799
|
}
|
|
3343
|
-
if (
|
|
3344
|
-
|
|
3345
|
-
|
|
3800
|
+
if (t33.isTSParenthesizedType(tsType)) {
|
|
3801
|
+
return resolveCallableType(tsType.typeAnnotation);
|
|
3802
|
+
}
|
|
3803
|
+
return null;
|
|
3804
|
+
}
|
|
3805
|
+
|
|
3806
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/type-resolver.ts
|
|
3807
|
+
var SLOT_DEFAULT_NAME2 = "default";
|
|
3808
|
+
function resolveSlotType(tsType, options) {
|
|
3809
|
+
if (t34.isTSParenthesizedType(tsType)) {
|
|
3810
|
+
return resolveSlotType(tsType.typeAnnotation, options);
|
|
3811
|
+
}
|
|
3812
|
+
if (t34.isTSTypeReference(tsType)) {
|
|
3813
|
+
let shouldRecordReactNode = false;
|
|
3814
|
+
if (tsType.typeParameters?.params.length) {
|
|
3815
|
+
const params = [];
|
|
3816
|
+
for (const param of tsType.typeParameters.params) {
|
|
3817
|
+
const resolved2 = resolveSlotType(param, options);
|
|
3818
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved2.shouldRecordReactNode;
|
|
3819
|
+
params.push(resolved2.type || param);
|
|
3820
|
+
}
|
|
3821
|
+
if (!shouldRecordReactNode) {
|
|
3822
|
+
return {
|
|
3823
|
+
type: tsType,
|
|
3824
|
+
shouldRecordReactNode: false
|
|
3825
|
+
};
|
|
3826
|
+
}
|
|
3827
|
+
return {
|
|
3828
|
+
type: t34.tsTypeReference(tsType.typeName, t34.tsTypeParameterInstantiation(params)),
|
|
3829
|
+
shouldRecordReactNode
|
|
3830
|
+
};
|
|
3831
|
+
}
|
|
3832
|
+
if (!t34.isIdentifier(tsType.typeName)) {
|
|
3833
|
+
return {
|
|
3834
|
+
type: tsType,
|
|
3835
|
+
shouldRecordReactNode: false
|
|
3836
|
+
};
|
|
3837
|
+
}
|
|
3838
|
+
const typeName = tsType.typeName.name;
|
|
3839
|
+
const localDeclaration = options.localTypeDeclarations.get(typeName);
|
|
3840
|
+
if (!localDeclaration || localDeclaration.hasTypeParameters) {
|
|
3841
|
+
return {
|
|
3842
|
+
type: tsType,
|
|
3843
|
+
shouldRecordReactNode: false
|
|
3844
|
+
};
|
|
3845
|
+
}
|
|
3846
|
+
if (options.visitedTypeNames.has(typeName)) {
|
|
3847
|
+
return {
|
|
3848
|
+
type: tsType,
|
|
3849
|
+
shouldRecordReactNode: false
|
|
3850
|
+
};
|
|
3346
3851
|
}
|
|
3347
|
-
|
|
3852
|
+
options.visitedTypeNames.add(typeName);
|
|
3853
|
+
const resolved = resolveSlotType(localDeclaration.type, options);
|
|
3854
|
+
options.visitedTypeNames.delete(typeName);
|
|
3855
|
+
if (!resolved.shouldRecordReactNode) {
|
|
3856
|
+
return {
|
|
3857
|
+
type: tsType,
|
|
3858
|
+
shouldRecordReactNode: false
|
|
3859
|
+
};
|
|
3860
|
+
}
|
|
3861
|
+
return resolved;
|
|
3348
3862
|
}
|
|
3349
|
-
if (
|
|
3350
|
-
|
|
3863
|
+
if (t34.isTSIntersectionType(tsType)) {
|
|
3864
|
+
const types = [];
|
|
3865
|
+
let shouldRecordReactNode = false;
|
|
3866
|
+
for (const item of tsType.types) {
|
|
3867
|
+
const resolved = resolveSlotType(item, options);
|
|
3868
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3869
|
+
if (resolved.type) {
|
|
3870
|
+
types.push(resolved.type);
|
|
3871
|
+
}
|
|
3872
|
+
}
|
|
3873
|
+
if (!types.length) {
|
|
3874
|
+
return {
|
|
3875
|
+
type: null,
|
|
3876
|
+
shouldRecordReactNode
|
|
3877
|
+
};
|
|
3878
|
+
}
|
|
3879
|
+
if (types.length === 1) {
|
|
3880
|
+
return {
|
|
3881
|
+
type: types[0],
|
|
3882
|
+
shouldRecordReactNode
|
|
3883
|
+
};
|
|
3884
|
+
}
|
|
3885
|
+
return {
|
|
3886
|
+
type: t34.tsIntersectionType(types),
|
|
3887
|
+
shouldRecordReactNode
|
|
3888
|
+
};
|
|
3351
3889
|
}
|
|
3352
|
-
if (
|
|
3353
|
-
|
|
3890
|
+
if (t34.isTSUnionType(tsType)) {
|
|
3891
|
+
const types = [];
|
|
3892
|
+
let shouldRecordReactNode = false;
|
|
3893
|
+
for (const item of tsType.types) {
|
|
3894
|
+
const resolved = resolveSlotType(item, options);
|
|
3895
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3896
|
+
if (resolved.type) {
|
|
3897
|
+
types.push(resolved.type);
|
|
3898
|
+
}
|
|
3899
|
+
}
|
|
3900
|
+
if (!types.length) {
|
|
3901
|
+
return {
|
|
3902
|
+
type: null,
|
|
3903
|
+
shouldRecordReactNode
|
|
3904
|
+
};
|
|
3905
|
+
}
|
|
3906
|
+
if (types.length === 1) {
|
|
3907
|
+
return {
|
|
3908
|
+
type: types[0],
|
|
3909
|
+
shouldRecordReactNode
|
|
3910
|
+
};
|
|
3911
|
+
}
|
|
3912
|
+
return {
|
|
3913
|
+
type: t34.tsUnionType(types),
|
|
3914
|
+
shouldRecordReactNode
|
|
3915
|
+
};
|
|
3354
3916
|
}
|
|
3355
|
-
if (
|
|
3356
|
-
|
|
3917
|
+
if (t34.isTSTypeLiteral(tsType)) {
|
|
3918
|
+
const members = [];
|
|
3919
|
+
let shouldRecordReactNode = false;
|
|
3920
|
+
for (const item of tsType.members) {
|
|
3921
|
+
const resolved = resolveSlotPropFromMember(item);
|
|
3922
|
+
shouldRecordReactNode = shouldRecordReactNode || resolved.shouldRecordReactNode;
|
|
3923
|
+
members.push(resolved.member || item);
|
|
3924
|
+
}
|
|
3925
|
+
if (!members.length) {
|
|
3926
|
+
return {
|
|
3927
|
+
type: null,
|
|
3928
|
+
shouldRecordReactNode
|
|
3929
|
+
};
|
|
3930
|
+
}
|
|
3931
|
+
return {
|
|
3932
|
+
type: t34.tsTypeLiteral(members),
|
|
3933
|
+
shouldRecordReactNode
|
|
3934
|
+
};
|
|
3357
3935
|
}
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
return
|
|
3936
|
+
if (t34.isTSFunctionType(tsType)) {
|
|
3937
|
+
const props = buildSlotPropSignature(
|
|
3938
|
+
SLOT_DEFAULT_NAME2,
|
|
3939
|
+
cloneCallableParams(tsType.parameters),
|
|
3940
|
+
false
|
|
3941
|
+
// 默认插槽不是可选的
|
|
3942
|
+
);
|
|
3943
|
+
return {
|
|
3944
|
+
type: t34.tsTypeLiteral([props]),
|
|
3945
|
+
shouldRecordReactNode: true
|
|
3946
|
+
// 函数类型总是需要记录 ReactNode
|
|
3947
|
+
};
|
|
3366
3948
|
}
|
|
3367
|
-
return
|
|
3949
|
+
return {
|
|
3950
|
+
type: tsType,
|
|
3951
|
+
shouldRecordReactNode: false
|
|
3952
|
+
};
|
|
3368
3953
|
}
|
|
3369
3954
|
function resolveSlotPropFromMember(member) {
|
|
3370
|
-
if (
|
|
3955
|
+
if (t34.isTSMethodSignature(member)) {
|
|
3371
3956
|
const rawName = resolvePropName2(member.key);
|
|
3372
|
-
if (!rawName)
|
|
3957
|
+
if (!rawName) {
|
|
3958
|
+
return {
|
|
3959
|
+
member: null,
|
|
3960
|
+
shouldRecordReactNode: false
|
|
3961
|
+
};
|
|
3962
|
+
}
|
|
3373
3963
|
const params = cloneCallableParams(member.parameters);
|
|
3374
|
-
return
|
|
3964
|
+
return {
|
|
3965
|
+
member: buildSlotPropSignature(rawName, params, !!member.optional),
|
|
3966
|
+
shouldRecordReactNode: true
|
|
3967
|
+
// 方法签名总是可调用,需要记录 ReactNode
|
|
3968
|
+
};
|
|
3375
3969
|
}
|
|
3376
|
-
if (
|
|
3970
|
+
if (t34.isTSPropertySignature(member)) {
|
|
3377
3971
|
const rawName = resolvePropName2(member.key);
|
|
3378
|
-
if (!rawName)
|
|
3972
|
+
if (!rawName) {
|
|
3973
|
+
return {
|
|
3974
|
+
member: null,
|
|
3975
|
+
shouldRecordReactNode: false
|
|
3976
|
+
};
|
|
3977
|
+
}
|
|
3379
3978
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3380
3979
|
const callable = typeAnnotation ? resolveCallableType(typeAnnotation) : null;
|
|
3381
|
-
if (!callable)
|
|
3980
|
+
if (!callable) {
|
|
3981
|
+
return {
|
|
3982
|
+
member: null,
|
|
3983
|
+
shouldRecordReactNode: false
|
|
3984
|
+
};
|
|
3985
|
+
}
|
|
3382
3986
|
const params = cloneCallableParams(callable.parameters);
|
|
3383
|
-
return
|
|
3987
|
+
return {
|
|
3988
|
+
member: buildSlotPropSignature(rawName, params, !!member.optional),
|
|
3989
|
+
shouldRecordReactNode: true
|
|
3990
|
+
// 可调用属性需要记录 ReactNode
|
|
3991
|
+
};
|
|
3384
3992
|
}
|
|
3385
|
-
if (
|
|
3993
|
+
if (t34.isTSCallSignatureDeclaration(member)) {
|
|
3386
3994
|
const params = cloneCallableParams(member.parameters);
|
|
3387
|
-
return
|
|
3995
|
+
return {
|
|
3996
|
+
member: buildSlotPropSignature(SLOT_DEFAULT_NAME2, params, true),
|
|
3997
|
+
shouldRecordReactNode: true
|
|
3998
|
+
// 调用签名总是可调用,需要记录 ReactNode
|
|
3999
|
+
};
|
|
3388
4000
|
}
|
|
3389
|
-
return
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
if (t28.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
3394
|
-
return null;
|
|
4001
|
+
return {
|
|
4002
|
+
member: null,
|
|
4003
|
+
shouldRecordReactNode: false
|
|
4004
|
+
};
|
|
3395
4005
|
}
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
const
|
|
3400
|
-
|
|
3401
|
-
);
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
4006
|
+
|
|
4007
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/define-slots.ts
|
|
4008
|
+
function resolveDefineSlotsIface(path9, ctx) {
|
|
4009
|
+
const { node } = path9;
|
|
4010
|
+
const tsParams = node.typeParameters?.params;
|
|
4011
|
+
if (!tsParams?.length) return;
|
|
4012
|
+
const {
|
|
4013
|
+
propsTSIface: { slotTypes }
|
|
4014
|
+
} = ctx.scriptData;
|
|
4015
|
+
const localTypeDeclarations = collectLocalTypeDeclarations(path9);
|
|
4016
|
+
let shouldRecordReactNode = false;
|
|
4017
|
+
for (const tsType of tsParams) {
|
|
4018
|
+
const { type: resolved, shouldRecordReactNode: needsReactNode } = resolveSlotType(tsType, {
|
|
4019
|
+
localTypeDeclarations,
|
|
4020
|
+
visitedTypeNames: /* @__PURE__ */ new Set()
|
|
4021
|
+
});
|
|
4022
|
+
if (resolved) {
|
|
4023
|
+
slotTypes.push(resolved);
|
|
4024
|
+
shouldRecordReactNode = shouldRecordReactNode || needsReactNode;
|
|
4025
|
+
}
|
|
4026
|
+
}
|
|
4027
|
+
if (shouldRecordReactNode) {
|
|
4028
|
+
recordReactNode(ctx);
|
|
3408
4029
|
}
|
|
3409
|
-
const prop = t28.tsPropertySignature(key, typeAnnotation);
|
|
3410
|
-
prop.optional = optional;
|
|
3411
|
-
return prop;
|
|
3412
|
-
}
|
|
3413
|
-
function createSlotScopeParam(props, ctx) {
|
|
3414
|
-
const paramId = t28.identifier(SLOT_FN_PARAM_NAME);
|
|
3415
|
-
const propsSigns = [];
|
|
3416
|
-
const { reactiveBindings } = ctx.templateData;
|
|
3417
|
-
props.forEach(({ prop, tsType }) => {
|
|
3418
|
-
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3419
|
-
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3420
|
-
const typeAnnotation = foundBindingTypes ? t28.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3421
|
-
const key = t28.isValidIdentifier(prop) ? t28.identifier(prop) : t28.stringLiteral(prop);
|
|
3422
|
-
const propSign = t28.tsPropertySignature(key, typeAnnotation);
|
|
3423
|
-
propsSigns.push(propSign);
|
|
3424
|
-
});
|
|
3425
|
-
paramId.typeAnnotation = t28.tsTypeAnnotation(t28.tsTypeLiteral(propsSigns));
|
|
3426
|
-
return paramId;
|
|
3427
4030
|
}
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
return
|
|
4031
|
+
|
|
4032
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot/template-slots.ts
|
|
4033
|
+
import * as t35 from "@babel/types";
|
|
4034
|
+
function resolveTemplateSlotIface(ctx) {
|
|
4035
|
+
if (ctx.inputType !== "sfc") return;
|
|
4036
|
+
const {
|
|
4037
|
+
propsTSIface: { slotTypes }
|
|
4038
|
+
} = ctx.scriptData;
|
|
4039
|
+
if (slotTypes.length) return;
|
|
4040
|
+
const { slots } = ctx.templateData;
|
|
4041
|
+
const tsMembers = [];
|
|
4042
|
+
for (const name in slots) {
|
|
4043
|
+
const slotObj = slots[name];
|
|
4044
|
+
if (!slotObj) continue;
|
|
4045
|
+
const params = slotObj.isScope ? [createSlotScopeParam(slotObj.props, ctx)] : [];
|
|
4046
|
+
const tsNode = buildSlotPropSignature(slotObj.name, params, true);
|
|
4047
|
+
tsMembers.push(tsNode);
|
|
4048
|
+
}
|
|
4049
|
+
if (tsMembers.length) {
|
|
4050
|
+
recordReactNode(ctx);
|
|
4051
|
+
slotTypes.push(t35.tsTypeLiteral(tsMembers));
|
|
4052
|
+
}
|
|
3433
4053
|
}
|
|
3434
4054
|
|
|
3435
4055
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
@@ -3472,35 +4092,79 @@ function resolvePropsIface(ctx) {
|
|
|
3472
4092
|
};
|
|
3473
4093
|
}
|
|
3474
4094
|
function resolveCompIProps(ctx, ast) {
|
|
3475
|
-
const { propsTSIface, lang } = ctx.scriptData;
|
|
4095
|
+
const { declaredOptions, propsTSIface, lang } = ctx.scriptData;
|
|
3476
4096
|
const { propsTypes, emitTypes, slotTypes } = propsTSIface;
|
|
3477
4097
|
const tsTypes = [...propsTypes, ...emitTypes, ...slotTypes];
|
|
3478
4098
|
if (ctx.inputType !== "sfc" || !lang.startsWith("ts") || !tsTypes.length) {
|
|
3479
4099
|
return;
|
|
3480
4100
|
}
|
|
3481
|
-
const n =
|
|
4101
|
+
const n = declaredOptions.name || "Comp";
|
|
3482
4102
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3483
|
-
const typeNode =
|
|
3484
|
-
const typeAliasDecl =
|
|
3485
|
-
const exportDecl =
|
|
4103
|
+
const typeNode = t36.tsIntersectionType(tsTypes);
|
|
4104
|
+
const typeAliasDecl = t36.tsTypeAliasDeclaration(t36.identifier(ns), null, typeNode);
|
|
4105
|
+
const exportDecl = t36.exportNamedDeclaration(typeAliasDecl);
|
|
3486
4106
|
propsTSIface.name = ns;
|
|
3487
4107
|
const scriptIR = getScriptIR(ctx);
|
|
3488
4108
|
scriptIR.exports.push(exportDecl);
|
|
3489
4109
|
void ast;
|
|
3490
4110
|
}
|
|
3491
4111
|
|
|
4112
|
+
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-use-attrs.ts
|
|
4113
|
+
import * as t37 from "@babel/types";
|
|
4114
|
+
function resolveUseAttrs(ctx) {
|
|
4115
|
+
return {
|
|
4116
|
+
VariableDeclarator(path9) {
|
|
4117
|
+
const { init, id } = path9.node;
|
|
4118
|
+
if (!init) return;
|
|
4119
|
+
const initPath = path9.get("init");
|
|
4120
|
+
const propsIdentifier = t37.identifier(ctx.propField);
|
|
4121
|
+
if (t37.isTSAsExpression(init) && isUseAttrsCall(init.expression)) {
|
|
4122
|
+
const typeAssertion = createPropsTypeAssertion(propsIdentifier, init.typeAnnotation);
|
|
4123
|
+
replaceNode(initPath, typeAssertion, init);
|
|
4124
|
+
return;
|
|
4125
|
+
}
|
|
4126
|
+
if (!isUseAttrsCall(init)) {
|
|
4127
|
+
return;
|
|
4128
|
+
}
|
|
4129
|
+
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
4130
|
+
if (isTS) {
|
|
4131
|
+
let typeAnnotation = null;
|
|
4132
|
+
if (t37.isIdentifier(id) && t37.isTSTypeAnnotation(id.typeAnnotation)) {
|
|
4133
|
+
typeAnnotation = id.typeAnnotation.typeAnnotation;
|
|
4134
|
+
id.typeAnnotation = null;
|
|
4135
|
+
} else {
|
|
4136
|
+
typeAnnotation = t37.tsTypeReference(
|
|
4137
|
+
t37.identifier("Record"),
|
|
4138
|
+
t37.tsTypeParameterInstantiation([t37.tsStringKeyword(), t37.tsUnknownKeyword()])
|
|
4139
|
+
);
|
|
4140
|
+
}
|
|
4141
|
+
const propsTypeAssertion = createPropsTypeAssertion(propsIdentifier, typeAnnotation);
|
|
4142
|
+
replaceNode(initPath, propsTypeAssertion, propsIdentifier);
|
|
4143
|
+
return;
|
|
4144
|
+
}
|
|
4145
|
+
replaceNode(initPath, propsIdentifier, init);
|
|
4146
|
+
}
|
|
4147
|
+
};
|
|
4148
|
+
}
|
|
4149
|
+
function isUseAttrsCall(expr) {
|
|
4150
|
+
return t37.isCallExpression(expr) && isCalleeNamed(expr, VUE_API_MAP.useAttrs);
|
|
4151
|
+
}
|
|
4152
|
+
function createPropsTypeAssertion(propsIdentifier, typeAnnotation) {
|
|
4153
|
+
return t37.tsAsExpression(propsIdentifier, typeAnnotation);
|
|
4154
|
+
}
|
|
4155
|
+
|
|
3492
4156
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3493
|
-
import * as
|
|
4157
|
+
import * as t39 from "@babel/types";
|
|
3494
4158
|
|
|
3495
4159
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3496
4160
|
import { traverse as traverse2 } from "@babel/core";
|
|
3497
|
-
import * as
|
|
4161
|
+
import * as t38 from "@babel/types";
|
|
3498
4162
|
var TRACE_MAX_DEPTH = 20;
|
|
3499
4163
|
function analyzeDeps(node, ctx, parentPath) {
|
|
3500
4164
|
if (!parentPath) {
|
|
3501
|
-
return
|
|
4165
|
+
return t38.arrayExpression([]);
|
|
3502
4166
|
}
|
|
3503
|
-
const isFnExpr =
|
|
4167
|
+
const isFnExpr = t38.isArrowFunctionExpression(node) || t38.isFunctionExpression(node);
|
|
3504
4168
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
3505
4169
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
3506
4170
|
const reactiveStateApis = getReactiveStateApis();
|
|
@@ -3511,13 +4175,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3511
4175
|
}
|
|
3512
4176
|
const analyzeTargetPath = parentPath && parentPath.node === analyzeTarget ? parentPath : null;
|
|
3513
4177
|
if (analyzeTargetPath) {
|
|
3514
|
-
if (
|
|
4178
|
+
if (t38.isMemberExpression(analyzeTarget) || t38.isOptionalMemberExpression(analyzeTarget)) {
|
|
3515
4179
|
const rootId = findRootIdentifier(analyzeTarget);
|
|
3516
4180
|
if (rootId) {
|
|
3517
4181
|
tryAddDependency(analyzeTargetPath, rootId.name, analyzeTargetPath.scope);
|
|
3518
4182
|
processedIdentifiers.add(rootId);
|
|
3519
4183
|
}
|
|
3520
|
-
} else if (
|
|
4184
|
+
} else if (t38.isIdentifier(analyzeTarget)) {
|
|
3521
4185
|
tryAddDependency(analyzeTargetPath, analyzeTarget.name, analyzeTargetPath.scope);
|
|
3522
4186
|
}
|
|
3523
4187
|
}
|
|
@@ -3568,55 +4232,55 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3568
4232
|
}
|
|
3569
4233
|
}
|
|
3570
4234
|
function normalizeDependencyExpr(path9, rootName) {
|
|
3571
|
-
if (
|
|
3572
|
-
return
|
|
4235
|
+
if (t38.isIdentifier(path9.node)) {
|
|
4236
|
+
return t38.identifier(path9.node.name);
|
|
3573
4237
|
}
|
|
3574
|
-
if (
|
|
4238
|
+
if (t38.isMemberExpression(path9.node) || t38.isOptionalMemberExpression(path9.node)) {
|
|
3575
4239
|
const normalizedExp = normalizeMemberForCallSite(path9, path9.node);
|
|
3576
|
-
const safeExp =
|
|
4240
|
+
const safeExp = t38.isMemberExpression(normalizedExp) || t38.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForMemberChain(normalizedExp) : normalizedExp;
|
|
3577
4241
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
3578
|
-
return
|
|
4242
|
+
return t38.cloneNode(safeExp, true);
|
|
3579
4243
|
}
|
|
3580
|
-
return
|
|
4244
|
+
return t38.identifier(rootName);
|
|
3581
4245
|
}
|
|
3582
4246
|
return null;
|
|
3583
4247
|
}
|
|
3584
4248
|
function normalizeSourcedDependency(exp) {
|
|
3585
|
-
if (
|
|
3586
|
-
return
|
|
4249
|
+
if (t38.isIdentifier(exp)) {
|
|
4250
|
+
return t38.identifier(exp.name);
|
|
3587
4251
|
}
|
|
3588
|
-
if (
|
|
4252
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3589
4253
|
const root = findRootIdentifier(exp);
|
|
3590
4254
|
if (!root) return null;
|
|
3591
|
-
const safeExp =
|
|
4255
|
+
const safeExp = t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp) ? ensureOptionalForMemberChain(exp) : exp;
|
|
3592
4256
|
if (isReactValidDependencyExpr(safeExp)) {
|
|
3593
|
-
return
|
|
4257
|
+
return t38.cloneNode(safeExp, true);
|
|
3594
4258
|
}
|
|
3595
|
-
return
|
|
4259
|
+
return t38.identifier(root.name);
|
|
3596
4260
|
}
|
|
3597
4261
|
return null;
|
|
3598
4262
|
}
|
|
3599
4263
|
function isReactValidDependencyExpr(node2) {
|
|
3600
|
-
if (
|
|
4264
|
+
if (t38.isIdentifier(node2)) {
|
|
3601
4265
|
return true;
|
|
3602
4266
|
}
|
|
3603
|
-
if (
|
|
4267
|
+
if (t38.isMemberExpression(node2) || t38.isOptionalMemberExpression(node2)) {
|
|
3604
4268
|
return isStaticMemberChain(node2);
|
|
3605
4269
|
}
|
|
3606
4270
|
return false;
|
|
3607
4271
|
}
|
|
3608
4272
|
function isStaticMemberChain(node2) {
|
|
3609
4273
|
let current = node2;
|
|
3610
|
-
while (
|
|
3611
|
-
if (!current.computed && !
|
|
4274
|
+
while (t38.isMemberExpression(current) || t38.isOptionalMemberExpression(current)) {
|
|
4275
|
+
if (!current.computed && !t38.isIdentifier(current.property)) {
|
|
3612
4276
|
return false;
|
|
3613
4277
|
}
|
|
3614
|
-
if (current.computed && !
|
|
4278
|
+
if (current.computed && !t38.isStringLiteral(current.property) && !t38.isNumericLiteral(current.property)) {
|
|
3615
4279
|
return false;
|
|
3616
4280
|
}
|
|
3617
4281
|
current = current.object;
|
|
3618
4282
|
}
|
|
3619
|
-
return
|
|
4283
|
+
return t38.isIdentifier(current);
|
|
3620
4284
|
}
|
|
3621
4285
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
3622
4286
|
let current = binding.path;
|
|
@@ -3634,7 +4298,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3634
4298
|
if (!isDirectCallee) {
|
|
3635
4299
|
return node2;
|
|
3636
4300
|
}
|
|
3637
|
-
if (!
|
|
4301
|
+
if (!t38.isExpression(node2.object)) {
|
|
3638
4302
|
return node2;
|
|
3639
4303
|
}
|
|
3640
4304
|
return node2.object;
|
|
@@ -3643,15 +4307,15 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3643
4307
|
if (!hasTrailingMemberAccess(node2)) {
|
|
3644
4308
|
return node2;
|
|
3645
4309
|
}
|
|
3646
|
-
if (
|
|
4310
|
+
if (t38.isOptionalMemberExpression(node2) && node2.optional) {
|
|
3647
4311
|
return node2;
|
|
3648
4312
|
}
|
|
3649
|
-
const object =
|
|
3650
|
-
const property =
|
|
3651
|
-
return
|
|
4313
|
+
const object = t38.cloneNode(node2.object, true);
|
|
4314
|
+
const property = t38.cloneNode(node2.property, true);
|
|
4315
|
+
return t38.optionalMemberExpression(object, property, node2.computed, true);
|
|
3652
4316
|
}
|
|
3653
4317
|
function hasTrailingMemberAccess(node2) {
|
|
3654
|
-
return
|
|
4318
|
+
return t38.isMemberExpression(node2.object) || t38.isOptionalMemberExpression(node2.object);
|
|
3655
4319
|
}
|
|
3656
4320
|
function isEligibleBindingSource(binding) {
|
|
3657
4321
|
if (binding.kind === "param") {
|
|
@@ -3661,17 +4325,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3661
4325
|
const declaratorPath = getVariableDeclaratorPath(bindingPath);
|
|
3662
4326
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
3663
4327
|
const nodeInit = declaratorPath?.node.init;
|
|
3664
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
3665
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
3666
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
4328
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t38.isCallExpression(nodeInit) && t38.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
4329
|
+
const isHookCallVarBinding = !!declaratorPath && t38.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
4330
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t38.isArrowFunctionExpression(nodeInit) || t38.isFunctionExpression(nodeInit));
|
|
3667
4331
|
const isReactiveFunctionBinding = isFunctionBinding && (isReactiveBinding(declaratorPath?.node) || isReactiveBinding(bindingPath.node));
|
|
3668
4332
|
return isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isReactiveFunctionBinding;
|
|
3669
4333
|
}
|
|
3670
4334
|
function isHookLikeCallee(callee) {
|
|
3671
|
-
if (
|
|
4335
|
+
if (t38.isIdentifier(callee)) {
|
|
3672
4336
|
return callee.name.startsWith("use");
|
|
3673
4337
|
}
|
|
3674
|
-
if (
|
|
4338
|
+
if (t38.isMemberExpression(callee) && !callee.computed && t38.isIdentifier(callee.property)) {
|
|
3675
4339
|
return callee.property.name.startsWith("use");
|
|
3676
4340
|
}
|
|
3677
4341
|
return false;
|
|
@@ -3687,7 +4351,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3687
4351
|
}
|
|
3688
4352
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
3689
4353
|
if (depth <= 0) return null;
|
|
3690
|
-
if (
|
|
4354
|
+
if (t38.isIdentifier(exp)) {
|
|
3691
4355
|
const sourceBinding = scope.getBinding(exp.name);
|
|
3692
4356
|
if (!sourceBinding) return null;
|
|
3693
4357
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -3695,13 +4359,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3695
4359
|
}
|
|
3696
4360
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3697
4361
|
}
|
|
3698
|
-
if (
|
|
4362
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3699
4363
|
const root = findRootIdentifier(exp);
|
|
3700
4364
|
if (!root) return null;
|
|
3701
4365
|
const sourceBinding = scope.getBinding(root.name);
|
|
3702
4366
|
if (!sourceBinding) return null;
|
|
3703
4367
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
3704
|
-
return
|
|
4368
|
+
return t38.cloneNode(exp);
|
|
3705
4369
|
}
|
|
3706
4370
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3707
4371
|
if (sourcedRoot) {
|
|
@@ -3709,17 +4373,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3709
4373
|
if (rebuilt) {
|
|
3710
4374
|
return rebuilt;
|
|
3711
4375
|
}
|
|
3712
|
-
return
|
|
4376
|
+
return t38.cloneNode(sourcedRoot, true);
|
|
3713
4377
|
}
|
|
3714
4378
|
}
|
|
3715
4379
|
return null;
|
|
3716
4380
|
}
|
|
3717
4381
|
function rebuildMemberWithNewRoot(node2, nextRoot) {
|
|
3718
4382
|
const replacedObject = (() => {
|
|
3719
|
-
if (
|
|
3720
|
-
return
|
|
4383
|
+
if (t38.isIdentifier(node2.object)) {
|
|
4384
|
+
return t38.cloneNode(nextRoot, true);
|
|
3721
4385
|
}
|
|
3722
|
-
if (
|
|
4386
|
+
if (t38.isMemberExpression(node2.object) || t38.isOptionalMemberExpression(node2.object)) {
|
|
3723
4387
|
return rebuildMemberWithNewRoot(node2.object, nextRoot);
|
|
3724
4388
|
}
|
|
3725
4389
|
return null;
|
|
@@ -3727,34 +4391,34 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3727
4391
|
if (!replacedObject) {
|
|
3728
4392
|
return null;
|
|
3729
4393
|
}
|
|
3730
|
-
const property =
|
|
3731
|
-
if (
|
|
3732
|
-
return
|
|
4394
|
+
const property = t38.cloneNode(node2.property, true);
|
|
4395
|
+
if (t38.isMemberExpression(node2)) {
|
|
4396
|
+
return t38.memberExpression(
|
|
3733
4397
|
replacedObject,
|
|
3734
4398
|
property,
|
|
3735
4399
|
node2.computed
|
|
3736
4400
|
);
|
|
3737
4401
|
}
|
|
3738
|
-
return
|
|
4402
|
+
return t38.optionalMemberExpression(
|
|
3739
4403
|
replacedObject,
|
|
3740
4404
|
property,
|
|
3741
4405
|
node2.computed,
|
|
3742
4406
|
node2.optional
|
|
3743
4407
|
);
|
|
3744
4408
|
}
|
|
3745
|
-
return
|
|
4409
|
+
return t38.arrayExpression(Array.from(deps.values()));
|
|
3746
4410
|
}
|
|
3747
4411
|
function getDependencyKey(exp) {
|
|
3748
|
-
if (
|
|
4412
|
+
if (t38.isIdentifier(exp)) {
|
|
3749
4413
|
return exp.name;
|
|
3750
4414
|
}
|
|
3751
|
-
if (
|
|
4415
|
+
if (t38.isMemberExpression(exp) || t38.isOptionalMemberExpression(exp)) {
|
|
3752
4416
|
const objectKey = getDependencyKey(exp.object);
|
|
3753
4417
|
const opt = exp.optional ? "?" : "";
|
|
3754
|
-
if (!exp.computed &&
|
|
4418
|
+
if (!exp.computed && t38.isIdentifier(exp.property)) {
|
|
3755
4419
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
3756
4420
|
}
|
|
3757
|
-
if (
|
|
4421
|
+
if (t38.isStringLiteral(exp.property) || t38.isNumericLiteral(exp.property)) {
|
|
3758
4422
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
3759
4423
|
}
|
|
3760
4424
|
return `${objectKey}${opt}[*]`;
|
|
@@ -3787,7 +4451,7 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3787
4451
|
if (!isVueApiReference(path9, apiName)) {
|
|
3788
4452
|
return;
|
|
3789
4453
|
}
|
|
3790
|
-
if (
|
|
4454
|
+
if (t39.isCallExpression(node)) {
|
|
3791
4455
|
resolveCallNode(path9, adapter, ctx);
|
|
3792
4456
|
} else {
|
|
3793
4457
|
replaceIdName(node, adapter.target);
|
|
@@ -3797,11 +4461,11 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3797
4461
|
};
|
|
3798
4462
|
}
|
|
3799
4463
|
function getApiName(node) {
|
|
3800
|
-
const isCallNode =
|
|
4464
|
+
const isCallNode = t39.isCallExpression(node);
|
|
3801
4465
|
let apiName = "";
|
|
3802
|
-
if (
|
|
4466
|
+
if (t39.isIdentifier(node)) {
|
|
3803
4467
|
apiName = node.name;
|
|
3804
|
-
} else if (isCallNode &&
|
|
4468
|
+
} else if (isCallNode && t39.isIdentifier(node.callee)) {
|
|
3805
4469
|
apiName = node.callee.name;
|
|
3806
4470
|
}
|
|
3807
4471
|
return apiName;
|
|
@@ -3811,7 +4475,7 @@ function resolveCallNode(path9, adapter, ctx) {
|
|
|
3811
4475
|
const { arguments: args } = node;
|
|
3812
4476
|
if (!args.length) return;
|
|
3813
4477
|
const fn = args[0];
|
|
3814
|
-
if (!
|
|
4478
|
+
if (!t39.isArrowFunctionExpression(fn) && !t39.isFunctionExpression(fn)) {
|
|
3815
4479
|
return;
|
|
3816
4480
|
}
|
|
3817
4481
|
const fnPath = path9.get("arguments")[0];
|
|
@@ -3843,7 +4507,7 @@ function isVueImportBinding(binding) {
|
|
|
3843
4507
|
return false;
|
|
3844
4508
|
}
|
|
3845
4509
|
const parent = bindingPath.parentPath?.node;
|
|
3846
|
-
if (!parent || !
|
|
4510
|
+
if (!parent || !t39.isImportDeclaration(parent)) {
|
|
3847
4511
|
return false;
|
|
3848
4512
|
}
|
|
3849
4513
|
const source = parent.source.value.toLowerCase();
|
|
@@ -3870,7 +4534,7 @@ function resolveArrowFnDeps(ctx, ast) {
|
|
|
3870
4534
|
const declaratorPath = getVariableDeclaratorPath(path9);
|
|
3871
4535
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useCallback);
|
|
3872
4536
|
setScriptNodeMeta(declaratorPath?.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3873
|
-
path9
|
|
4537
|
+
replaceNode(path9, newNode, node);
|
|
3874
4538
|
}
|
|
3875
4539
|
};
|
|
3876
4540
|
}
|
|
@@ -3911,7 +4575,7 @@ function isSkip(path9) {
|
|
|
3911
4575
|
}
|
|
3912
4576
|
|
|
3913
4577
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3914
|
-
import * as
|
|
4578
|
+
import * as t40 from "@babel/types";
|
|
3915
4579
|
function resolveElementRef(ctx) {
|
|
3916
4580
|
return {
|
|
3917
4581
|
CallExpression(path9) {
|
|
@@ -3929,14 +4593,14 @@ function resolveElementRef(ctx) {
|
|
|
3929
4593
|
}
|
|
3930
4594
|
if (isCompRefBindings) {
|
|
3931
4595
|
const varDeclaratorPath = getVariableDeclaratorPath(path9)?.node;
|
|
3932
|
-
if (!
|
|
4596
|
+
if (!t40.isIdentifier(varDeclaratorPath?.id)) {
|
|
3933
4597
|
return;
|
|
3934
4598
|
}
|
|
3935
4599
|
const varName = varDeclaratorPath.id.name;
|
|
3936
4600
|
const compRef = refBindings.componentRefs[varName];
|
|
3937
4601
|
if (!compRef) return;
|
|
3938
4602
|
}
|
|
3939
|
-
node.arguments[0] =
|
|
4603
|
+
node.arguments[0] = t40.identifier("null");
|
|
3940
4604
|
resolveTypeParameters(ctx, path9);
|
|
3941
4605
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
3942
4606
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
@@ -3961,27 +4625,27 @@ function resolveTypeParameters(ctx, path9) {
|
|
|
3961
4625
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
3962
4626
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
3963
4627
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
3964
|
-
node.typeParameters =
|
|
4628
|
+
node.typeParameters = t40.tsTypeParameterInstantiation([t40.tsTypeReference(t40.identifier(type))]);
|
|
3965
4629
|
}
|
|
3966
4630
|
}
|
|
3967
4631
|
function resolveRefValueToCurrent(path9) {
|
|
3968
4632
|
const { node } = path9;
|
|
3969
|
-
if (node.computed || !
|
|
4633
|
+
if (node.computed || !t40.isIdentifier(node.property) || node.property.name !== "value") {
|
|
3970
4634
|
return;
|
|
3971
4635
|
}
|
|
3972
4636
|
const rootPath = findRootVariablePath(path9);
|
|
3973
|
-
if (!rootPath?.node || !
|
|
4637
|
+
if (!rootPath?.node || !t40.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
3974
4638
|
return;
|
|
3975
4639
|
}
|
|
3976
4640
|
const rootId = findRootIdentifier(node);
|
|
3977
|
-
if (!
|
|
4641
|
+
if (!t40.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
3978
4642
|
return;
|
|
3979
4643
|
}
|
|
3980
4644
|
node.property.name = "current";
|
|
3981
4645
|
}
|
|
3982
4646
|
|
|
3983
4647
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
3984
|
-
import * as
|
|
4648
|
+
import * as t41 from "@babel/types";
|
|
3985
4649
|
function resolveExprMemo(ctx, ast) {
|
|
3986
4650
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
3987
4651
|
return {
|
|
@@ -3993,11 +4657,11 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3993
4657
|
if (!atComponentOrHookRoot(path9, ast.program, isScriptFile)) {
|
|
3994
4658
|
return false;
|
|
3995
4659
|
}
|
|
3996
|
-
if (!
|
|
4660
|
+
if (!t41.isVariableDeclaration(path9.parent) || path9.parent.kind !== "const") {
|
|
3997
4661
|
return false;
|
|
3998
4662
|
}
|
|
3999
|
-
if (
|
|
4000
|
-
if (
|
|
4663
|
+
if (t41.isFunction(init)) return false;
|
|
4664
|
+
if (t41.isCallExpression(init) && t41.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
4001
4665
|
return false;
|
|
4002
4666
|
}
|
|
4003
4667
|
return true;
|
|
@@ -4015,16 +4679,16 @@ function resolveExprMemo(ctx, ast) {
|
|
|
4015
4679
|
}
|
|
4016
4680
|
|
|
4017
4681
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
4018
|
-
import * as
|
|
4682
|
+
import * as t42 from "@babel/types";
|
|
4019
4683
|
function resolveLintRules(ctx, ast) {
|
|
4020
4684
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
4021
4685
|
return {
|
|
4022
4686
|
CallExpression(path9) {
|
|
4023
4687
|
const { node, parentPath } = path9;
|
|
4024
|
-
if (!
|
|
4688
|
+
if (!t42.isIdentifier(node.callee)) return;
|
|
4025
4689
|
const { name: callName } = node.callee;
|
|
4026
|
-
const addLog = (
|
|
4027
|
-
logger.error(
|
|
4690
|
+
const addLog = (t48) => {
|
|
4691
|
+
logger.error(t48, {
|
|
4028
4692
|
file: ctx.filename,
|
|
4029
4693
|
source: ctx.scriptData.source,
|
|
4030
4694
|
loc: node.loc
|
|
@@ -4069,7 +4733,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
4069
4733
|
|
|
4070
4734
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
4071
4735
|
import { generate as generate2 } from "@babel/generator";
|
|
4072
|
-
import * as
|
|
4736
|
+
import * as t43 from "@babel/types";
|
|
4073
4737
|
function resolveProvide(ctx) {
|
|
4074
4738
|
if (ctx.inputType === "style") return {};
|
|
4075
4739
|
return {
|
|
@@ -4101,13 +4765,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
4101
4765
|
function assignProviderValue(target, key, value) {
|
|
4102
4766
|
const getRawExp = (exp) => {
|
|
4103
4767
|
if (!exp) return "''";
|
|
4104
|
-
if (
|
|
4768
|
+
if (t43.isStringLiteral(exp)) {
|
|
4105
4769
|
return JSON.stringify(exp.value);
|
|
4106
4770
|
}
|
|
4107
|
-
if (
|
|
4771
|
+
if (t43.isNumericLiteral(exp)) {
|
|
4108
4772
|
return exp.value.toString();
|
|
4109
4773
|
}
|
|
4110
|
-
if (
|
|
4774
|
+
if (t43.isIdentifier(exp)) {
|
|
4111
4775
|
return exp.name;
|
|
4112
4776
|
}
|
|
4113
4777
|
try {
|
|
@@ -4123,16 +4787,16 @@ function assignProviderValue(target, key, value) {
|
|
|
4123
4787
|
}
|
|
4124
4788
|
|
|
4125
4789
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
4126
|
-
import * as
|
|
4790
|
+
import * as t44 from "@babel/types";
|
|
4127
4791
|
function resolveRenameAdapter(ctx) {
|
|
4128
4792
|
return {
|
|
4129
4793
|
"CallExpression|Identifier"(path9) {
|
|
4130
4794
|
const node = path9.node;
|
|
4131
|
-
const isCallNode =
|
|
4795
|
+
const isCallNode = t44.isCallExpression(node);
|
|
4132
4796
|
let apiName = "";
|
|
4133
|
-
if (
|
|
4797
|
+
if (t44.isIdentifier(node)) {
|
|
4134
4798
|
apiName = node.name;
|
|
4135
|
-
} else if (isCallNode &&
|
|
4799
|
+
} else if (isCallNode && t44.isIdentifier(node.callee)) {
|
|
4136
4800
|
apiName = node.callee.name;
|
|
4137
4801
|
}
|
|
4138
4802
|
if (!apiName) {
|
|
@@ -4168,6 +4832,10 @@ function resolveRenameAdapter(ctx) {
|
|
|
4168
4832
|
};
|
|
4169
4833
|
}
|
|
4170
4834
|
function isVueApiReference2(path9, apiName) {
|
|
4835
|
+
const whitelist = [VUE_API_MAP.defineAsyncComponent];
|
|
4836
|
+
if (whitelist.includes(apiName)) {
|
|
4837
|
+
return true;
|
|
4838
|
+
}
|
|
4171
4839
|
if (path9.isIdentifier()) {
|
|
4172
4840
|
if (path9.parentPath.isCallExpression() && path9.parentPath.node.callee === path9.node) {
|
|
4173
4841
|
return false;
|
|
@@ -4190,7 +4858,7 @@ function isVueImportBinding2(binding) {
|
|
|
4190
4858
|
return false;
|
|
4191
4859
|
}
|
|
4192
4860
|
const parent = bindingPath.parentPath?.node;
|
|
4193
|
-
if (!parent || !
|
|
4861
|
+
if (!parent || !t44.isImportDeclaration(parent)) {
|
|
4194
4862
|
return false;
|
|
4195
4863
|
}
|
|
4196
4864
|
const source = parent.source.value.toLowerCase();
|
|
@@ -4210,11 +4878,12 @@ function processVueSyntax2(ast, ctx) {
|
|
|
4210
4878
|
applyBabel: [
|
|
4211
4879
|
resolvePropsIface,
|
|
4212
4880
|
resolveEmitsTopLevelTypes,
|
|
4213
|
-
resolveSlotsTopLevelTypes,
|
|
4214
4881
|
resolveDefineOptions,
|
|
4215
4882
|
resolveDefineExpose,
|
|
4216
4883
|
resolveDefineAsyncComponent,
|
|
4217
|
-
resolveEmitCalls
|
|
4884
|
+
resolveEmitCalls,
|
|
4885
|
+
// feature: https://github.com/vureact-js/core/issues/6
|
|
4886
|
+
resolveUseAttrs
|
|
4218
4887
|
]
|
|
4219
4888
|
},
|
|
4220
4889
|
process: {
|
|
@@ -4326,15 +4995,15 @@ function isRouterLinkBooleanCustomProp(prop) {
|
|
|
4326
4995
|
}
|
|
4327
4996
|
|
|
4328
4997
|
// src/core/transform/sfc/template/shared/prop-ir-utils.ts
|
|
4329
|
-
import * as
|
|
4998
|
+
import * as t46 from "@babel/types";
|
|
4330
4999
|
|
|
4331
5000
|
// src/shared/string-code-types.ts
|
|
4332
5001
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
4333
|
-
import * as
|
|
5002
|
+
import * as t45 from "@babel/types";
|
|
4334
5003
|
var strCodeTypes = {
|
|
4335
|
-
isIdentifier:
|
|
5004
|
+
isIdentifier: isIdentifier24,
|
|
4336
5005
|
isSimpleExpression,
|
|
4337
|
-
isStringLiteral:
|
|
5006
|
+
isStringLiteral: isStringLiteral12
|
|
4338
5007
|
};
|
|
4339
5008
|
function isSimpleExpression(code, excludeVar = false) {
|
|
4340
5009
|
let node;
|
|
@@ -4343,38 +5012,38 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4343
5012
|
} catch {
|
|
4344
5013
|
return false;
|
|
4345
5014
|
}
|
|
4346
|
-
if (
|
|
5015
|
+
if (t45.isLiteral(node)) {
|
|
4347
5016
|
return true;
|
|
4348
5017
|
}
|
|
4349
|
-
if (!excludeVar &&
|
|
5018
|
+
if (!excludeVar && t45.isIdentifier(node)) {
|
|
4350
5019
|
return true;
|
|
4351
5020
|
}
|
|
4352
|
-
if (
|
|
4353
|
-
return isSimpleExpression(node.object) &&
|
|
5021
|
+
if (t45.isMemberExpression(node)) {
|
|
5022
|
+
return isSimpleExpression(node.object) && t45.isIdentifier(node.property);
|
|
4354
5023
|
}
|
|
4355
|
-
if (
|
|
5024
|
+
if (t45.isObjectExpression(node) || t45.isArrayExpression(node)) {
|
|
4356
5025
|
return false;
|
|
4357
5026
|
}
|
|
4358
|
-
if (
|
|
5027
|
+
if (t45.isCallExpression(node) || t45.isAssignmentExpression(node)) {
|
|
4359
5028
|
return false;
|
|
4360
5029
|
}
|
|
4361
|
-
if (
|
|
5030
|
+
if (t45.isBinaryExpression(node) || t45.isUnaryExpression(node)) {
|
|
4362
5031
|
return true;
|
|
4363
5032
|
}
|
|
4364
5033
|
return false;
|
|
4365
5034
|
}
|
|
4366
|
-
function
|
|
5035
|
+
function isIdentifier24(code) {
|
|
4367
5036
|
try {
|
|
4368
5037
|
const node = parseExpression3(code);
|
|
4369
|
-
return
|
|
5038
|
+
return t45.isIdentifier(node);
|
|
4370
5039
|
} catch {
|
|
4371
5040
|
return false;
|
|
4372
5041
|
}
|
|
4373
5042
|
}
|
|
4374
|
-
function
|
|
5043
|
+
function isStringLiteral12(code) {
|
|
4375
5044
|
try {
|
|
4376
5045
|
const node = parseExpression3(code);
|
|
4377
|
-
return
|
|
5046
|
+
return t45.isStringLiteral(node);
|
|
4378
5047
|
} catch {
|
|
4379
5048
|
return false;
|
|
4380
5049
|
}
|
|
@@ -4516,23 +5185,23 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4516
5185
|
const mergedItems = value.merge;
|
|
4517
5186
|
const setNameIdentifier = (target, valueName) => {
|
|
4518
5187
|
target.content = valueName;
|
|
4519
|
-
target.ast =
|
|
5188
|
+
target.ast = t46.jsxIdentifier(valueName);
|
|
4520
5189
|
};
|
|
4521
|
-
const setValueExpression = (target, content,
|
|
5190
|
+
const setValueExpression = (target, content, isStringLiteral13) => {
|
|
4522
5191
|
target.content = content;
|
|
4523
|
-
target.ast = resolveStringExpr(content, ctx,
|
|
5192
|
+
target.ast = resolveStringExpr(content, ctx, isStringLiteral13);
|
|
4524
5193
|
};
|
|
4525
5194
|
const createRuntimeCall = (fnName, args) => {
|
|
4526
5195
|
const fnArgs = args.filter(Boolean).join(",");
|
|
4527
5196
|
return `${fnName}(${fnArgs})`;
|
|
4528
5197
|
};
|
|
4529
|
-
const applyRuntimeExpression = (expression, setName = false, nameIdentifier,
|
|
5198
|
+
const applyRuntimeExpression = (expression, setName = false, nameIdentifier, isStringLiteral13) => {
|
|
4530
5199
|
if (setName && nameIdentifier) {
|
|
4531
5200
|
setNameIdentifier(nameExp, nameIdentifier);
|
|
4532
5201
|
}
|
|
4533
5202
|
const dir = ADAPTER_RULES.runtime.dir;
|
|
4534
5203
|
recordImport(ctx, dir.package, dir.target);
|
|
4535
|
-
setValueExpression(value.babelExp, expression,
|
|
5204
|
+
setValueExpression(value.babelExp, expression, isStringLiteral13);
|
|
4536
5205
|
};
|
|
4537
5206
|
if (ir.isKeyLessVBind) {
|
|
4538
5207
|
const dirKeyless = ADAPTER_RULES.runtime.dirKeyless;
|
|
@@ -4568,7 +5237,7 @@ function resolvePropAsBabelExp(ir, ctx) {
|
|
|
4568
5237
|
return;
|
|
4569
5238
|
}
|
|
4570
5239
|
setNameIdentifier(nameExp, name);
|
|
4571
|
-
const normalizedValue =
|
|
5240
|
+
const normalizedValue = resolveSpecialExpression(valueContent, ctx);
|
|
4572
5241
|
setValueExpression(value.babelExp, normalizedValue, value.isStringLiteral);
|
|
4573
5242
|
}
|
|
4574
5243
|
|
|
@@ -4806,22 +5475,22 @@ function warnUnsupportedVueDollarVar(ctx, node) {
|
|
|
4806
5475
|
}
|
|
4807
5476
|
|
|
4808
5477
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-is-prop.ts
|
|
4809
|
-
function resolveStaticIsProp(
|
|
4810
|
-
if (!
|
|
5478
|
+
function resolveStaticIsProp(content, ir, ctx, nodeIR) {
|
|
5479
|
+
if (!content) {
|
|
4811
5480
|
return;
|
|
4812
5481
|
}
|
|
4813
|
-
if (
|
|
4814
|
-
const name =
|
|
5482
|
+
if (content.startsWith("vue:")) {
|
|
5483
|
+
const name = content.split("vue:")[1];
|
|
4815
5484
|
nodeIR.tag = camelCase(name);
|
|
4816
5485
|
return;
|
|
4817
5486
|
}
|
|
4818
|
-
const propIR = createPropsIR("is", "is",
|
|
5487
|
+
const propIR = createPropsIR("is", "is", content);
|
|
4819
5488
|
propIR.value.isStringLiteral = true;
|
|
4820
5489
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4821
5490
|
nodeIR.props.push(propIR);
|
|
4822
5491
|
}
|
|
4823
|
-
function resolveDynamicIsProp(
|
|
4824
|
-
const exp =
|
|
5492
|
+
function resolveDynamicIsProp(directive, ir, ctx, nodeIR) {
|
|
5493
|
+
const exp = directive.exp;
|
|
4825
5494
|
const content = exp.content;
|
|
4826
5495
|
if (strCodeTypes.isStringLiteral(content)) {
|
|
4827
5496
|
resolveStaticIsProp(content, ir, ctx, nodeIR);
|
|
@@ -4837,20 +5506,20 @@ function resolveDynamicIsProp(node, ir, ctx, nodeIR) {
|
|
|
4837
5506
|
|
|
4838
5507
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-ref-prop.ts
|
|
4839
5508
|
import { NodeTypes as NodeTypes5 } from "@vue/compiler-core";
|
|
4840
|
-
function resolveRefProp(
|
|
5509
|
+
function resolveRefProp(prop, ctx, nodeIR) {
|
|
4841
5510
|
const {
|
|
4842
5511
|
templateData: { refBindings }
|
|
4843
5512
|
} = ctx;
|
|
4844
5513
|
let propIR;
|
|
4845
|
-
if (
|
|
4846
|
-
const tag =
|
|
5514
|
+
if (prop.type === NodeTypes5.ATTRIBUTE) {
|
|
5515
|
+
const tag = prop.value?.content;
|
|
4847
5516
|
if (!tag) return;
|
|
4848
5517
|
collectComponentRef(tag, ctx);
|
|
4849
5518
|
const domRefBinding = Object.values(refBindings.domRefs).find((r) => r.tag === tag);
|
|
4850
5519
|
const refVar = domRefBinding?.name || refBindings.componentRefs[tag]?.name;
|
|
4851
5520
|
propIR = createPropsIR("ref", "ref", refVar || "null");
|
|
4852
5521
|
} else {
|
|
4853
|
-
const exp =
|
|
5522
|
+
const exp = prop.exp;
|
|
4854
5523
|
for (const name in refBindings.domRefs) {
|
|
4855
5524
|
const newName = `${name}.current`;
|
|
4856
5525
|
const regex = new RegExp(`${name}(?!\\.current)`, "g");
|
|
@@ -4878,50 +5547,92 @@ function collectComponentRef(tag, ctx) {
|
|
|
4878
5547
|
}
|
|
4879
5548
|
}
|
|
4880
5549
|
|
|
5550
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-template-key.ts
|
|
5551
|
+
import {
|
|
5552
|
+
ConstantTypes,
|
|
5553
|
+
NodeTypes as NodeTypes6
|
|
5554
|
+
} from "@vue/compiler-core";
|
|
5555
|
+
function resolveTemplateNodeKey(vueNode, keyContent) {
|
|
5556
|
+
const firstNode = vueNode.children[0];
|
|
5557
|
+
if (!firstNode) return;
|
|
5558
|
+
const hasKeyProp = firstNode.props.some(
|
|
5559
|
+
(p) => p.type === NodeTypes6.DIRECTIVE && p.name === "key"
|
|
5560
|
+
);
|
|
5561
|
+
if (hasKeyProp) return;
|
|
5562
|
+
firstNode.props.push(createSimpleVueBind("key", keyContent));
|
|
5563
|
+
}
|
|
5564
|
+
function createSimpleVueBind(name, value) {
|
|
5565
|
+
return {
|
|
5566
|
+
type: NodeTypes6.DIRECTIVE,
|
|
5567
|
+
name: "bind",
|
|
5568
|
+
rawName: `:${name}`,
|
|
5569
|
+
exp: {
|
|
5570
|
+
type: NodeTypes6.SIMPLE_EXPRESSION,
|
|
5571
|
+
content: value,
|
|
5572
|
+
isStatic: false,
|
|
5573
|
+
constType: ConstantTypes.NOT_CONSTANT,
|
|
5574
|
+
loc: {}
|
|
5575
|
+
},
|
|
5576
|
+
arg: {
|
|
5577
|
+
type: NodeTypes6.SIMPLE_EXPRESSION,
|
|
5578
|
+
content: name,
|
|
5579
|
+
isStatic: true,
|
|
5580
|
+
constType: ConstantTypes.CAN_STRINGIFY,
|
|
5581
|
+
loc: {}
|
|
5582
|
+
},
|
|
5583
|
+
modifiers: [],
|
|
5584
|
+
loc: {}
|
|
5585
|
+
};
|
|
5586
|
+
}
|
|
5587
|
+
|
|
4881
5588
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-dynamic-attribute-prop.ts
|
|
4882
|
-
function resolveDynamicAttributeProp(
|
|
4883
|
-
const arg =
|
|
4884
|
-
const exp =
|
|
5589
|
+
function resolveDynamicAttributeProp(directive, ir, ctx, vueNode, nodeIR) {
|
|
5590
|
+
const arg = directive.arg;
|
|
5591
|
+
const exp = directive.exp;
|
|
4885
5592
|
const name = arg?.content ?? "";
|
|
4886
5593
|
const content = exp?.content ?? "true";
|
|
4887
|
-
warnUnsupportedVueDollarVar(ctx,
|
|
5594
|
+
warnUnsupportedVueDollarVar(ctx, directive);
|
|
4888
5595
|
if (name === "is") {
|
|
4889
|
-
resolveDynamicIsProp(
|
|
5596
|
+
resolveDynamicIsProp(directive, ir, ctx, nodeIR);
|
|
4890
5597
|
return;
|
|
4891
5598
|
}
|
|
4892
5599
|
if (name === "ref") {
|
|
4893
|
-
resolveRefProp(
|
|
5600
|
+
resolveRefProp(directive, ctx, nodeIR);
|
|
5601
|
+
return;
|
|
5602
|
+
}
|
|
5603
|
+
if (vueNode.tag === "template" && name === "key") {
|
|
5604
|
+
resolveTemplateNodeKey(vueNode, content);
|
|
4894
5605
|
return;
|
|
4895
5606
|
}
|
|
4896
|
-
const
|
|
4897
|
-
|
|
4898
|
-
checkPropIsDynamicKey(ctx,
|
|
4899
|
-
resolvePropertyIR(
|
|
5607
|
+
const propIR = createPropsIR(directive.rawName, name, content);
|
|
5608
|
+
propIR.isStatic = arg?.isStatic ?? true;
|
|
5609
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
5610
|
+
resolvePropertyIR(propIR, ir, ctx, nodeIR, true);
|
|
4900
5611
|
}
|
|
4901
|
-
function resolvePropertyIR(
|
|
4902
|
-
let content =
|
|
4903
|
-
if (isVBind(
|
|
4904
|
-
|
|
5612
|
+
function resolvePropertyIR(propsIR, ir, ctx, nodeIR, isDynamic = false) {
|
|
5613
|
+
let content = propsIR.value.content;
|
|
5614
|
+
if (isVBind(propsIR.rawName) && !propsIR.name) {
|
|
5615
|
+
propsIR.isKeyLessVBind = true;
|
|
4905
5616
|
}
|
|
4906
|
-
if (isStyleAttr(
|
|
4907
|
-
|
|
4908
|
-
content =
|
|
5617
|
+
if (isStyleAttr(propsIR.name)) {
|
|
5618
|
+
propsIR.value.isStringLiteral = false;
|
|
5619
|
+
content = propsIR.value.content = parseStyleString(content);
|
|
4909
5620
|
}
|
|
4910
5621
|
if (isDynamic) {
|
|
4911
|
-
const
|
|
4912
|
-
if (
|
|
5622
|
+
const isStringLiteral13 = strCodeTypes.isStringLiteral(content);
|
|
5623
|
+
if (isStringLiteral13) {
|
|
4913
5624
|
content = normalizeString(content);
|
|
4914
|
-
|
|
5625
|
+
propsIR.value.content = content;
|
|
4915
5626
|
}
|
|
4916
|
-
|
|
5627
|
+
propsIR.value.isStringLiteral = isStringLiteral13;
|
|
4917
5628
|
}
|
|
4918
|
-
const existing = findSameProp(nodeIR.props,
|
|
5629
|
+
const existing = findSameProp(nodeIR.props, propsIR);
|
|
4919
5630
|
if (existing) {
|
|
4920
|
-
mergePropsIR(ctx, existing,
|
|
5631
|
+
mergePropsIR(ctx, existing, propsIR);
|
|
4921
5632
|
} else {
|
|
4922
|
-
nodeIR.props.push(
|
|
5633
|
+
nodeIR.props.push(propsIR);
|
|
4923
5634
|
}
|
|
4924
|
-
resolvePropAsBabelExp(existing ??
|
|
5635
|
+
resolvePropAsBabelExp(existing ?? propsIR, ctx);
|
|
4925
5636
|
}
|
|
4926
5637
|
function normalizeString(s) {
|
|
4927
5638
|
if (s.startsWith("'") && s.endsWith("'")) {
|
|
@@ -4931,15 +5642,15 @@ function normalizeString(s) {
|
|
|
4931
5642
|
}
|
|
4932
5643
|
|
|
4933
5644
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-attribute-prop.ts
|
|
4934
|
-
function resolveAttributeProp(
|
|
4935
|
-
const name =
|
|
4936
|
-
const content =
|
|
5645
|
+
function resolveAttributeProp(attribute, ir, ctx, nodeIR) {
|
|
5646
|
+
const name = attribute.name;
|
|
5647
|
+
const content = attribute.value?.content ?? "true";
|
|
4937
5648
|
if (name === "is") {
|
|
4938
5649
|
resolveStaticIsProp(content, ir, ctx, nodeIR);
|
|
4939
5650
|
return;
|
|
4940
5651
|
}
|
|
4941
5652
|
if (name === "ref") {
|
|
4942
|
-
resolveRefProp(
|
|
5653
|
+
resolveRefProp(attribute, ctx, nodeIR);
|
|
4943
5654
|
return;
|
|
4944
5655
|
}
|
|
4945
5656
|
const attrIR = createPropsIR(name, name, content);
|
|
@@ -4948,15 +5659,86 @@ function resolveAttributeProp(node, ir, ctx, nodeIR) {
|
|
|
4948
5659
|
resolvePropertyIR(attrIR, ir, ctx, nodeIR);
|
|
4949
5660
|
}
|
|
4950
5661
|
|
|
5662
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-directive-prop.ts
|
|
5663
|
+
function resolveDirectiveProp(directive, ir, ctx, vueNode, nodeIR, siblingNodesIR) {
|
|
5664
|
+
const { name, rawName } = directive;
|
|
5665
|
+
if (!SUPPORTED_DIRECTIVES.has(name)) {
|
|
5666
|
+
warnUnsupportedDirective(ctx, directive.loc, rawName);
|
|
5667
|
+
return;
|
|
5668
|
+
}
|
|
5669
|
+
warnUnsupportedVueDollarVar(ctx, directive);
|
|
5670
|
+
if (isVConditional(rawName)) {
|
|
5671
|
+
return resolveVIf(directive, ir, ctx, nodeIR, siblingNodesIR);
|
|
5672
|
+
}
|
|
5673
|
+
function processExactDirectives() {
|
|
5674
|
+
switch (directive.rawName) {
|
|
5675
|
+
case "v-html":
|
|
5676
|
+
resolveVHtml(directive, ir, ctx, nodeIR);
|
|
5677
|
+
return true;
|
|
5678
|
+
case "v-text":
|
|
5679
|
+
resolveVText(directive, ir, ctx, nodeIR);
|
|
5680
|
+
return true;
|
|
5681
|
+
case "v-once":
|
|
5682
|
+
case "v-memo":
|
|
5683
|
+
return resolveVMemo(directive, ir, ctx, nodeIR);
|
|
5684
|
+
case "v-show":
|
|
5685
|
+
return resolveVShow(directive, ir, ctx, nodeIR);
|
|
5686
|
+
case "v-for":
|
|
5687
|
+
return resolveVFor(directive, ir, ctx, nodeIR);
|
|
5688
|
+
}
|
|
5689
|
+
}
|
|
5690
|
+
function processRangeDirectives() {
|
|
5691
|
+
const { rawName: rawName2 } = directive;
|
|
5692
|
+
if (isVModel(rawName2)) {
|
|
5693
|
+
return resolveVModel(directive, ir, ctx, vueNode, nodeIR);
|
|
5694
|
+
}
|
|
5695
|
+
if (isVBind(rawName2)) {
|
|
5696
|
+
return resolveDynamicAttributeProp(directive, ir, ctx, vueNode, nodeIR);
|
|
5697
|
+
}
|
|
5698
|
+
if (isVOn(rawName2)) {
|
|
5699
|
+
return resolveVOn(directive, ir, ctx, nodeIR);
|
|
5700
|
+
}
|
|
5701
|
+
if (isVSlot(rawName2)) {
|
|
5702
|
+
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
5703
|
+
resolveRouterLinkVSlotProp(directive, nodeIR, ctx);
|
|
5704
|
+
}
|
|
5705
|
+
return true;
|
|
5706
|
+
}
|
|
5707
|
+
}
|
|
5708
|
+
return processExactDirectives() || processRangeDirectives();
|
|
5709
|
+
}
|
|
5710
|
+
|
|
5711
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-props.ts
|
|
5712
|
+
import { NodeTypes as NodeTypes7 } from "@vue/compiler-core";
|
|
5713
|
+
function resolveProps(vueNode, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5714
|
+
for (const prop of vueNode.props) {
|
|
5715
|
+
if (prop.type === NodeTypes7.ATTRIBUTE) {
|
|
5716
|
+
resolveAttributeProp(prop, ir, ctx, nodeIR);
|
|
5717
|
+
continue;
|
|
5718
|
+
}
|
|
5719
|
+
if (prop.type === NodeTypes7.DIRECTIVE) {
|
|
5720
|
+
const stop = resolveDirectiveProp(
|
|
5721
|
+
prop,
|
|
5722
|
+
ir,
|
|
5723
|
+
ctx,
|
|
5724
|
+
vueNode,
|
|
5725
|
+
nodeIR,
|
|
5726
|
+
siblingNodesIR
|
|
5727
|
+
);
|
|
5728
|
+
if (stop) break;
|
|
5729
|
+
}
|
|
5730
|
+
}
|
|
5731
|
+
}
|
|
5732
|
+
|
|
4951
5733
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-router-link-v-slot-prop.ts
|
|
4952
|
-
function resolveRouterLinkVSlotProp(
|
|
4953
|
-
const arg =
|
|
4954
|
-
const exp =
|
|
4955
|
-
checkPropIsDynamicKey(ctx,
|
|
5734
|
+
function resolveRouterLinkVSlotProp(directive, nodeIR, ctx) {
|
|
5735
|
+
const arg = directive.arg;
|
|
5736
|
+
const exp = directive.exp;
|
|
5737
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
4956
5738
|
const propIR = {
|
|
4957
5739
|
type: 2 /* SLOT */,
|
|
4958
5740
|
name: "customRender",
|
|
4959
|
-
rawName:
|
|
5741
|
+
rawName: directive.rawName ?? "v-slot",
|
|
4960
5742
|
isStatic: arg?.isStatic ?? true,
|
|
4961
5743
|
isScoped: true,
|
|
4962
5744
|
callback: {
|
|
@@ -4968,11 +5750,11 @@ function resolveRouterLinkVSlotProp(node, nodeIR, ctx) {
|
|
|
4968
5750
|
}
|
|
4969
5751
|
|
|
4970
5752
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-for.ts
|
|
4971
|
-
function resolveVFor(
|
|
5753
|
+
function resolveVFor(directive, ir, ctx, nodeIR) {
|
|
4972
5754
|
nodeIR.meta.loop = {
|
|
4973
5755
|
isLoop: true,
|
|
4974
5756
|
isHandled: false,
|
|
4975
|
-
value: resolveForResult(
|
|
5757
|
+
value: resolveForResult(directive.forParseResult)
|
|
4976
5758
|
};
|
|
4977
5759
|
}
|
|
4978
5760
|
function resolveForResult(forParseResult) {
|
|
@@ -4989,17 +5771,17 @@ function resolveForResult(forParseResult) {
|
|
|
4989
5771
|
}
|
|
4990
5772
|
|
|
4991
5773
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-html.ts
|
|
4992
|
-
function resolveVHtml(
|
|
4993
|
-
const exp =
|
|
5774
|
+
function resolveVHtml(directive, ir, ctx, nodeIR) {
|
|
5775
|
+
const exp = directive.exp;
|
|
4994
5776
|
const propIR = createPropsIR("v-html", "v-html", `{__html: ${exp.content}}`);
|
|
4995
5777
|
resolvePropAsBabelExp(propIR, ctx);
|
|
4996
5778
|
nodeIR.props.push(propIR);
|
|
4997
5779
|
}
|
|
4998
5780
|
|
|
4999
5781
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-if.ts
|
|
5000
|
-
function resolveVIf(
|
|
5001
|
-
const name =
|
|
5002
|
-
const value =
|
|
5782
|
+
function resolveVIf(directive, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5783
|
+
const name = directive.name === "else-if" ? "elseIf" : directive.name;
|
|
5784
|
+
const value = directive.exp?.content ?? "true";
|
|
5003
5785
|
const prevNode = siblingNodesIR[siblingNodesIR.length - 1];
|
|
5004
5786
|
const isElseBranch = name === "else" || name === "elseIf";
|
|
5005
5787
|
let hasError = false;
|
|
@@ -5020,7 +5802,7 @@ function resolveVIf(node, _ir, ctx, nodeIR, siblingNodesIR) {
|
|
|
5020
5802
|
logger.error("v-else/v-else-if has no adjacent v-if or v-else-if.", {
|
|
5021
5803
|
source,
|
|
5022
5804
|
file: filename,
|
|
5023
|
-
loc:
|
|
5805
|
+
loc: directive.loc
|
|
5024
5806
|
});
|
|
5025
5807
|
return hasError;
|
|
5026
5808
|
}
|
|
@@ -5036,8 +5818,8 @@ function resolveVIf(node, _ir, ctx, nodeIR, siblingNodesIR) {
|
|
|
5036
5818
|
}
|
|
5037
5819
|
|
|
5038
5820
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-memo.ts
|
|
5039
|
-
function resolveVMemo(
|
|
5040
|
-
const exp =
|
|
5821
|
+
function resolveVMemo(directive, _ir, ctx, nodeIR) {
|
|
5822
|
+
const exp = directive.exp;
|
|
5041
5823
|
let value = exp?.content;
|
|
5042
5824
|
if (value !== void 0) {
|
|
5043
5825
|
if (!value.trim() || !value.startsWith("[") && !value.endsWith("]")) {
|
|
@@ -5045,7 +5827,7 @@ function resolveVMemo(node, _ir, ctx, nodeIR) {
|
|
|
5045
5827
|
logger.warn(
|
|
5046
5828
|
"The expected value of v-memo is an array; otherwise, memoization will be skipped.",
|
|
5047
5829
|
{
|
|
5048
|
-
loc:
|
|
5830
|
+
loc: directive.loc,
|
|
5049
5831
|
source,
|
|
5050
5832
|
file: filename
|
|
5051
5833
|
}
|
|
@@ -5069,15 +5851,15 @@ function resolveVMemo(node, _ir, ctx, nodeIR) {
|
|
|
5069
5851
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-model.ts
|
|
5070
5852
|
import {
|
|
5071
5853
|
ElementTypes,
|
|
5072
|
-
NodeTypes as
|
|
5854
|
+
NodeTypes as NodeTypes8
|
|
5073
5855
|
} from "@vue/compiler-core";
|
|
5074
|
-
function resolveVModel(
|
|
5075
|
-
const arg =
|
|
5076
|
-
const exp =
|
|
5077
|
-
const modifiers =
|
|
5856
|
+
function resolveVModel(directive, ir, ctx, vueNode, nodeIR) {
|
|
5857
|
+
const arg = directive.arg;
|
|
5858
|
+
const exp = directive.exp;
|
|
5859
|
+
const modifiers = directive.modifiers.map((item) => item.content);
|
|
5078
5860
|
const getterName = exp.content;
|
|
5079
|
-
const isComponent =
|
|
5080
|
-
const inputType = resolveHtmlInput(
|
|
5861
|
+
const isComponent = vueNode.tagType === ElementTypes.COMPONENT;
|
|
5862
|
+
const inputType = resolveHtmlInput(vueNode, isComponent);
|
|
5081
5863
|
const propName = arg?.content ?? resolveModelPropName(inputType, isComponent);
|
|
5082
5864
|
let valuePropIR;
|
|
5083
5865
|
let eventPropIR;
|
|
@@ -5093,7 +5875,7 @@ function resolveVModel(node, _ir, ctx, elementNode, nodeIR) {
|
|
|
5093
5875
|
eventPropIR.type = 3 /* EVENT */;
|
|
5094
5876
|
} else {
|
|
5095
5877
|
if (inputType === "radio") {
|
|
5096
|
-
const radioValue = getRadioValue(
|
|
5878
|
+
const radioValue = getRadioValue(vueNode);
|
|
5097
5879
|
valuePropIR = createPropsIR("v-model", "checked", `${getterName} === ${radioValue}`);
|
|
5098
5880
|
const processedValue = applyValueModifiers(radioValue, modifiers);
|
|
5099
5881
|
const handlerBody = `() => { ${getterName} = ${processedValue} }`;
|
|
@@ -5119,9 +5901,9 @@ function resolveModelPropName(inputType, isComponent = false) {
|
|
|
5119
5901
|
}
|
|
5120
5902
|
return !isComponent ? "value" : "modelValue";
|
|
5121
5903
|
}
|
|
5122
|
-
function getRadioValue(
|
|
5123
|
-
const valueAttr =
|
|
5124
|
-
(prop) => prop.type ===
|
|
5904
|
+
function getRadioValue(vueNode) {
|
|
5905
|
+
const valueAttr = vueNode.props.find(
|
|
5906
|
+
(prop) => prop.type === NodeTypes8.ATTRIBUTE && prop.name === "value"
|
|
5125
5907
|
);
|
|
5126
5908
|
if (!valueAttr?.value?.content) return '""';
|
|
5127
5909
|
const content = valueAttr.value.content;
|
|
@@ -5131,7 +5913,7 @@ function resolveHtmlInput(node, isComponent) {
|
|
|
5131
5913
|
if (isComponent) return;
|
|
5132
5914
|
if (node.tag !== "input") return node.tag;
|
|
5133
5915
|
const typeProp = node.props.find(
|
|
5134
|
-
(prop) => prop.type ===
|
|
5916
|
+
(prop) => prop.type === NodeTypes8.ATTRIBUTE && prop.name === "type"
|
|
5135
5917
|
);
|
|
5136
5918
|
return typeProp?.value?.content?.toLowerCase();
|
|
5137
5919
|
}
|
|
@@ -5147,14 +5929,14 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
5147
5929
|
}
|
|
5148
5930
|
|
|
5149
5931
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
5150
|
-
import * as
|
|
5151
|
-
function resolveVOn(
|
|
5152
|
-
const arg =
|
|
5153
|
-
const exp =
|
|
5154
|
-
const modifiers =
|
|
5932
|
+
import * as t47 from "@babel/types";
|
|
5933
|
+
function resolveVOn(directive, ir, ctx, nodeIR) {
|
|
5934
|
+
const arg = directive.arg;
|
|
5935
|
+
const exp = directive.exp;
|
|
5936
|
+
const modifiers = directive.modifiers.map((item) => item.content);
|
|
5155
5937
|
const captureIndex = modifiers.findIndex((modifier) => modifier === "capture");
|
|
5156
5938
|
let eventName = normalizeVOnEventName(arg.content);
|
|
5157
|
-
let handler =
|
|
5939
|
+
let handler = resolveSpecialExpression(exp.content.trim(), ctx);
|
|
5158
5940
|
if (captureIndex > -1) {
|
|
5159
5941
|
eventName = modifiers[captureIndex] ? `${eventName}Capture` : eventName;
|
|
5160
5942
|
modifiers.splice(captureIndex, 1);
|
|
@@ -5164,11 +5946,11 @@ function resolveVOn(node, _ir, ctx, nodeIR) {
|
|
|
5164
5946
|
originalVueEventName = `${arg.content}.${modifiers.join(".")}`;
|
|
5165
5947
|
} else {
|
|
5166
5948
|
const expr = stringToExpr(handler);
|
|
5167
|
-
if (!
|
|
5949
|
+
if (!t47.isFunctionExpression(expr) && !t47.isArrowFunctionExpression(expr) && !t47.isIdentifier(expr)) {
|
|
5168
5950
|
handler = `() => {${handler}}`;
|
|
5169
5951
|
}
|
|
5170
5952
|
}
|
|
5171
|
-
const eventIR = createPropsIR(
|
|
5953
|
+
const eventIR = createPropsIR(directive.rawName, eventName, handler);
|
|
5172
5954
|
eventIR.type = 3 /* EVENT */;
|
|
5173
5955
|
eventIR.isStatic = arg.isStatic;
|
|
5174
5956
|
eventIR.modifiers = modifiers;
|
|
@@ -5189,8 +5971,8 @@ function normalizeVOnEventName(rawEventName) {
|
|
|
5189
5971
|
}
|
|
5190
5972
|
|
|
5191
5973
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-show.ts
|
|
5192
|
-
function resolveVShow(
|
|
5193
|
-
const exp =
|
|
5974
|
+
function resolveVShow(directive, ir, ctx, nodeIR) {
|
|
5975
|
+
const exp = directive.exp;
|
|
5194
5976
|
const test = exp.content;
|
|
5195
5977
|
const showIR = createPropsIR("v-show", "style", `{display: ${test} ? '' : 'none'}`);
|
|
5196
5978
|
resolvePropAsBabelExp(showIR, ctx);
|
|
@@ -5210,6 +5992,29 @@ function resolveVShow(node, _ir, ctx, nodeIR) {
|
|
|
5210
5992
|
nodeIR.props.push(showIR);
|
|
5211
5993
|
}
|
|
5212
5994
|
|
|
5995
|
+
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-slot-prop.ts
|
|
5996
|
+
function resolveVSlotProp(directive, ir, ctx) {
|
|
5997
|
+
const arg = directive.arg;
|
|
5998
|
+
const exp = directive.exp;
|
|
5999
|
+
const isScoped = exp !== void 0;
|
|
6000
|
+
const name = !arg || arg.content === "default" ? "children" : arg.content;
|
|
6001
|
+
const content = !isScoped ? [] : void 0;
|
|
6002
|
+
const callback = isScoped ? {
|
|
6003
|
+
arg: exp?.content?.trim() ?? "",
|
|
6004
|
+
exp: []
|
|
6005
|
+
} : void 0;
|
|
6006
|
+
checkPropIsDynamicKey(ctx, directive);
|
|
6007
|
+
return {
|
|
6008
|
+
type: 2 /* SLOT */,
|
|
6009
|
+
name,
|
|
6010
|
+
rawName: directive.rawName ?? "default",
|
|
6011
|
+
isStatic: arg?.isStatic ?? true,
|
|
6012
|
+
isScoped,
|
|
6013
|
+
content,
|
|
6014
|
+
callback
|
|
6015
|
+
};
|
|
6016
|
+
}
|
|
6017
|
+
|
|
5213
6018
|
// src/core/transform/sfc/template/shared/node-ir-utils.ts
|
|
5214
6019
|
import { isVSlot as isCompilerVSlot } from "@vue/compiler-core";
|
|
5215
6020
|
function createInterpolationNodeIR(content) {
|
|
@@ -5232,126 +6037,13 @@ function isTemplateVSlotNode(node) {
|
|
|
5232
6037
|
}
|
|
5233
6038
|
|
|
5234
6039
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-text.ts
|
|
5235
|
-
function resolveVText(
|
|
5236
|
-
const exp =
|
|
6040
|
+
function resolveVText(directive, ir, ctx, nodeIR) {
|
|
6041
|
+
const exp = directive.exp;
|
|
5237
6042
|
const interpolationIR = createInterpolationNodeIR(exp.content);
|
|
5238
6043
|
interpolationIR.babelExp = resolveStringExpr(exp.content, ctx);
|
|
5239
6044
|
nodeIR.children = [interpolationIR];
|
|
5240
6045
|
}
|
|
5241
6046
|
|
|
5242
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-directive-prop.ts
|
|
5243
|
-
var SUPPORTED_DIRECTIVES = /* @__PURE__ */ new Set([
|
|
5244
|
-
"text",
|
|
5245
|
-
"html",
|
|
5246
|
-
"show",
|
|
5247
|
-
"if",
|
|
5248
|
-
"else",
|
|
5249
|
-
"else-if",
|
|
5250
|
-
"for",
|
|
5251
|
-
"on",
|
|
5252
|
-
"once",
|
|
5253
|
-
"bind",
|
|
5254
|
-
"model",
|
|
5255
|
-
"cloak",
|
|
5256
|
-
"slot",
|
|
5257
|
-
"memo",
|
|
5258
|
-
"is"
|
|
5259
|
-
]);
|
|
5260
|
-
function resolveDirectiveProp(node, ir, ctx, elementNode, nodeIR, siblingNodesIR) {
|
|
5261
|
-
const { name, rawName } = node;
|
|
5262
|
-
if (!SUPPORTED_DIRECTIVES.has(name)) {
|
|
5263
|
-
warnUnsupportedDirective(ctx, node.loc, rawName);
|
|
5264
|
-
return;
|
|
5265
|
-
}
|
|
5266
|
-
warnUnsupportedVueDollarVar(ctx, node);
|
|
5267
|
-
if (isVConditional(rawName)) {
|
|
5268
|
-
return resolveVIf(node, ir, ctx, nodeIR, siblingNodesIR);
|
|
5269
|
-
}
|
|
5270
|
-
function processExactDirectives() {
|
|
5271
|
-
switch (node.rawName) {
|
|
5272
|
-
case "v-html":
|
|
5273
|
-
resolveVHtml(node, ir, ctx, nodeIR);
|
|
5274
|
-
return true;
|
|
5275
|
-
case "v-text":
|
|
5276
|
-
resolveVText(node, ir, ctx, nodeIR);
|
|
5277
|
-
return true;
|
|
5278
|
-
case "v-once":
|
|
5279
|
-
case "v-memo":
|
|
5280
|
-
return resolveVMemo(node, ir, ctx, nodeIR);
|
|
5281
|
-
case "v-show":
|
|
5282
|
-
return resolveVShow(node, ir, ctx, nodeIR);
|
|
5283
|
-
case "v-for":
|
|
5284
|
-
return resolveVFor(node, ir, ctx, nodeIR);
|
|
5285
|
-
}
|
|
5286
|
-
}
|
|
5287
|
-
function processRangeDirectives() {
|
|
5288
|
-
const { rawName: rawName2 } = node;
|
|
5289
|
-
if (isVModel(rawName2)) {
|
|
5290
|
-
return resolveVModel(node, ir, ctx, elementNode, nodeIR);
|
|
5291
|
-
}
|
|
5292
|
-
if (isVBind(rawName2)) {
|
|
5293
|
-
return resolveDynamicAttributeProp(node, ir, ctx, nodeIR);
|
|
5294
|
-
}
|
|
5295
|
-
if (isVOn(rawName2)) {
|
|
5296
|
-
return resolveVOn(node, ir, ctx, nodeIR);
|
|
5297
|
-
}
|
|
5298
|
-
if (isVSlot(rawName2)) {
|
|
5299
|
-
if (nodeIR.tag === VUE_API_MAP.RouterLink) {
|
|
5300
|
-
resolveRouterLinkVSlotProp(node, nodeIR, ctx);
|
|
5301
|
-
}
|
|
5302
|
-
return true;
|
|
5303
|
-
}
|
|
5304
|
-
}
|
|
5305
|
-
return processExactDirectives() || processRangeDirectives();
|
|
5306
|
-
}
|
|
5307
|
-
|
|
5308
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-props.ts
|
|
5309
|
-
import { NodeTypes as NodeTypes7 } from "@vue/compiler-core";
|
|
5310
|
-
function resolveProps(node, ir, ctx, nodeIR, siblingNodesIR) {
|
|
5311
|
-
for (const prop of node.props) {
|
|
5312
|
-
if (prop.type === NodeTypes7.ATTRIBUTE) {
|
|
5313
|
-
resolveAttributeProp(prop, ir, ctx, nodeIR);
|
|
5314
|
-
continue;
|
|
5315
|
-
}
|
|
5316
|
-
if (prop.type === NodeTypes7.DIRECTIVE) {
|
|
5317
|
-
const stop = resolveDirectiveProp(
|
|
5318
|
-
prop,
|
|
5319
|
-
ir,
|
|
5320
|
-
ctx,
|
|
5321
|
-
node,
|
|
5322
|
-
nodeIR,
|
|
5323
|
-
siblingNodesIR
|
|
5324
|
-
);
|
|
5325
|
-
if (stop) {
|
|
5326
|
-
break;
|
|
5327
|
-
}
|
|
5328
|
-
}
|
|
5329
|
-
}
|
|
5330
|
-
}
|
|
5331
|
-
|
|
5332
|
-
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-slot-prop.ts
|
|
5333
|
-
function resolveVSlotProp(node, _ir, ctx) {
|
|
5334
|
-
const arg = node.arg;
|
|
5335
|
-
const exp = node.exp;
|
|
5336
|
-
const isScoped = exp !== void 0;
|
|
5337
|
-
const name = !arg || arg.content === "default" ? "children" : arg.content;
|
|
5338
|
-
const content = !isScoped ? [] : void 0;
|
|
5339
|
-
const callback = isScoped ? {
|
|
5340
|
-
arg: exp?.content?.trim() ?? "",
|
|
5341
|
-
exp: []
|
|
5342
|
-
} : void 0;
|
|
5343
|
-
checkPropIsDynamicKey(ctx, node);
|
|
5344
|
-
return {
|
|
5345
|
-
type: 2 /* SLOT */,
|
|
5346
|
-
name,
|
|
5347
|
-
rawName: node.rawName ?? "default",
|
|
5348
|
-
isStatic: arg?.isStatic ?? true,
|
|
5349
|
-
isScoped,
|
|
5350
|
-
content,
|
|
5351
|
-
callback
|
|
5352
|
-
};
|
|
5353
|
-
}
|
|
5354
|
-
|
|
5355
6047
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-comment-node.ts
|
|
5356
6048
|
function resolveCommentNode(node, _ir, ctx, childrenIR) {
|
|
5357
6049
|
const nodeIR = createTextNodeIR(node.content, true);
|
|
@@ -5411,7 +6103,7 @@ function resolveInterpolationNode(node, _ir, ctx, childrenIR) {
|
|
|
5411
6103
|
}
|
|
5412
6104
|
|
|
5413
6105
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-slot-outlet-node.ts
|
|
5414
|
-
import { NodeTypes as
|
|
6106
|
+
import { NodeTypes as NodeTypes10 } from "@vue/compiler-core";
|
|
5415
6107
|
|
|
5416
6108
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
5417
6109
|
import {
|
|
@@ -5420,11 +6112,11 @@ import {
|
|
|
5420
6112
|
} from "@vue/compiler-core";
|
|
5421
6113
|
|
|
5422
6114
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-v-slot-node.ts
|
|
5423
|
-
import { NodeTypes as
|
|
6115
|
+
import { NodeTypes as NodeTypes9 } from "@vue/compiler-core";
|
|
5424
6116
|
function resolveTemplateVSlotNode(node, ir, ctx, nodeIR) {
|
|
5425
6117
|
let slotIR = {};
|
|
5426
6118
|
for (const prop of node.props) {
|
|
5427
|
-
if (prop.type ===
|
|
6119
|
+
if (prop.type === NodeTypes9.DIRECTIVE) {
|
|
5428
6120
|
slotIR = resolveVSlotProp(prop, ir, ctx);
|
|
5429
6121
|
}
|
|
5430
6122
|
}
|
|
@@ -5445,7 +6137,11 @@ function resolveTextNode(node, _ir, _ctx, childrenIR) {
|
|
|
5445
6137
|
}
|
|
5446
6138
|
|
|
5447
6139
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
6140
|
+
var isRootMarked = false;
|
|
5448
6141
|
function resolveTemplateChildren(node, nodeIR, ctx) {
|
|
6142
|
+
if (!isRootMarked) {
|
|
6143
|
+
isRootMarked = true;
|
|
6144
|
+
}
|
|
5449
6145
|
resolveChildNodes(node, nodeIR, ctx, null, nodeIR.children);
|
|
5450
6146
|
}
|
|
5451
6147
|
function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
@@ -5462,6 +6158,7 @@ function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
|
5462
6158
|
continue;
|
|
5463
6159
|
}
|
|
5464
6160
|
const elementIR = resolveElementNode(child, nodeIR, ctx, childrenIR);
|
|
6161
|
+
markRootNode(elementIR);
|
|
5465
6162
|
childrenIR.push(elementIR);
|
|
5466
6163
|
if (child.children.length) {
|
|
5467
6164
|
resolveChildNodes(child, nodeIR, ctx, elementIR, elementIR.children);
|
|
@@ -5482,6 +6179,12 @@ function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
|
5482
6179
|
}
|
|
5483
6180
|
return nodeIR;
|
|
5484
6181
|
}
|
|
6182
|
+
function markRootNode(nodeIR) {
|
|
6183
|
+
if (isRootMarked) {
|
|
6184
|
+
nodeIR.isRoot = isRootMarked;
|
|
6185
|
+
isRootMarked = void 0;
|
|
6186
|
+
}
|
|
6187
|
+
}
|
|
5485
6188
|
|
|
5486
6189
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-slot-outlet-node.ts
|
|
5487
6190
|
function resolveSlotOutletNode(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
@@ -5513,7 +6216,7 @@ function resolveSlotProps(node, ctx) {
|
|
|
5513
6216
|
});
|
|
5514
6217
|
};
|
|
5515
6218
|
for (const prop of node.props) {
|
|
5516
|
-
if (prop.type ===
|
|
6219
|
+
if (prop.type === NodeTypes10.ATTRIBUTE) {
|
|
5517
6220
|
const attr = prop.name;
|
|
5518
6221
|
const value = prop.value?.content.trim();
|
|
5519
6222
|
if (attr === "name" && value) {
|
|
@@ -5523,7 +6226,7 @@ function resolveSlotProps(node, ctx) {
|
|
|
5523
6226
|
}
|
|
5524
6227
|
continue;
|
|
5525
6228
|
}
|
|
5526
|
-
if (prop.type ===
|
|
6229
|
+
if (prop.type === NodeTypes10.DIRECTIVE) {
|
|
5527
6230
|
const arg = prop.arg;
|
|
5528
6231
|
const exp = prop.exp;
|
|
5529
6232
|
warnUnsupportedVueDollarVar(ctx, prop);
|
|
@@ -5555,7 +6258,7 @@ function resolveSlotNode(parentIR, node, nodeIR, slotContext, ctx) {
|
|
|
5555
6258
|
}
|
|
5556
6259
|
if (fallback.length === 1) {
|
|
5557
6260
|
const child = fallback[0];
|
|
5558
|
-
if (child.type ===
|
|
6261
|
+
if (child.type === NodeTypes10.TEXT) {
|
|
5559
6262
|
fallbackResolved = true;
|
|
5560
6263
|
expression += ` ?? ${JSON.stringify(child.content.trim())}`;
|
|
5561
6264
|
}
|
|
@@ -5617,7 +6320,7 @@ function transform(ast, ctx, options) {
|
|
|
5617
6320
|
}
|
|
5618
6321
|
|
|
5619
6322
|
// package.json
|
|
5620
|
-
var version = "1.
|
|
6323
|
+
var version = "1.6.1";
|
|
5621
6324
|
var bin = {
|
|
5622
6325
|
vureact: "./bin/vureact.js"
|
|
5623
6326
|
};
|
|
@@ -6234,18 +6937,19 @@ var CompilationContext = class {
|
|
|
6234
6937
|
fileId: "",
|
|
6235
6938
|
source: "",
|
|
6236
6939
|
filename: "",
|
|
6237
|
-
compName: "",
|
|
6238
6940
|
imports: /* @__PURE__ */ new Map(),
|
|
6239
6941
|
cssVars: [],
|
|
6240
6942
|
inputType: "sfc",
|
|
6241
|
-
propField: "
|
|
6943
|
+
propField: "props",
|
|
6242
6944
|
templateData: {
|
|
6243
6945
|
slots: {},
|
|
6244
6946
|
refBindings: {
|
|
6245
6947
|
domRefs: {},
|
|
6246
6948
|
componentRefs: {}
|
|
6247
6949
|
},
|
|
6248
|
-
reactiveBindings: {}
|
|
6950
|
+
reactiveBindings: {},
|
|
6951
|
+
declaredProps: /* @__PURE__ */ new Set(),
|
|
6952
|
+
declaredEmits: /* @__PURE__ */ new Set()
|
|
6249
6953
|
},
|
|
6250
6954
|
scriptData: {
|
|
6251
6955
|
lang: "js",
|
|
@@ -6265,6 +6969,7 @@ var CompilationContext = class {
|
|
|
6265
6969
|
enabled: false,
|
|
6266
6970
|
refField: "expose"
|
|
6267
6971
|
},
|
|
6972
|
+
declaredOptions: {},
|
|
6268
6973
|
source: ""
|
|
6269
6974
|
},
|
|
6270
6975
|
styleData: {
|