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