@vureact/compiler-core 1.2.1 → 1.3.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/lib/{chunk-SQRJUILR.js → chunk-7LBUUA24.js} +829 -740
- package/lib/{chunk-7FIMRVQS.esm.js → chunk-CUCUXW56.esm.js} +782 -693
- package/lib/cli.d.cts +1 -2
- package/lib/cli.d.ts +1 -2
- package/lib/cli.esm.js +47 -2
- package/lib/cli.js +54 -9
- package/lib/compiler-core.d.cts +9 -0
- package/lib/compiler-core.d.ts +9 -0
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +13 -5
- package/templates/route-setup-notes.md +192 -0
- package/templates/route-setup-notes.zh.md +192 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vureact/compiler-core v1.
|
|
2
|
+
* @vureact/compiler-core v1.3.0
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -24,7 +24,7 @@ function executePlugins(map, result, ctx) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-ctx-provider.ts
|
|
27
|
-
import * as
|
|
27
|
+
import * as t11 from "@babel/types";
|
|
28
28
|
|
|
29
29
|
// src/consts/other.ts
|
|
30
30
|
var PACKAGE_NAME = {
|
|
@@ -418,8 +418,8 @@ function getBabelParseOptions(lang = "js", context = "script", filename) {
|
|
|
418
418
|
function stringToExpr(input, lang, filename = "") {
|
|
419
419
|
return parseExpression(input, getBabelParseOptions(lang, "expression", filename));
|
|
420
420
|
}
|
|
421
|
-
function atComponentOrHookRoot(
|
|
422
|
-
const { parentPath, scope } =
|
|
421
|
+
function atComponentOrHookRoot(path8, rootScope, inScriptFile = false) {
|
|
422
|
+
const { parentPath, scope } = path8;
|
|
423
423
|
const parentBlock = scope.block;
|
|
424
424
|
if (!parentPath) return !inScriptFile;
|
|
425
425
|
if (parentBlock === rootScope) {
|
|
@@ -448,7 +448,7 @@ function createJsxElement(tag, props, children, selfClosing) {
|
|
|
448
448
|
}
|
|
449
449
|
|
|
450
450
|
// src/core/codegen/component/jsx/syntax-processor/process/build-condition-node.ts
|
|
451
|
-
import * as
|
|
451
|
+
import * as t10 from "@babel/types";
|
|
452
452
|
|
|
453
453
|
// src/core/codegen/component/jsx/utils/jsx-expression-utils.ts
|
|
454
454
|
import * as t2 from "@babel/types";
|
|
@@ -544,7 +544,82 @@ function buildJsxChildren(children, ctx) {
|
|
|
544
544
|
}
|
|
545
545
|
|
|
546
546
|
// src/core/codegen/component/jsx/syntax-processor/process/build-loop-node.ts
|
|
547
|
+
import * as t6 from "@babel/types";
|
|
548
|
+
|
|
549
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
547
550
|
import * as t5 from "@babel/types";
|
|
551
|
+
|
|
552
|
+
// src/utils/camelCase.ts
|
|
553
|
+
var camelCase = (str) => {
|
|
554
|
+
if (str.includes("-")) {
|
|
555
|
+
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
556
|
+
}
|
|
557
|
+
return str;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// src/utils/capitalize.ts
|
|
561
|
+
var capitalize = (str) => {
|
|
562
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
566
|
+
function resolveSpecialExpressions(input, ctx) {
|
|
567
|
+
input = resolveEmitsCalls(input, ctx);
|
|
568
|
+
input = resolveRefVariable(input, ctx);
|
|
569
|
+
return input;
|
|
570
|
+
}
|
|
571
|
+
function resolveEmitsCalls(input, ctx) {
|
|
572
|
+
const result = matchEmitCalls(input, ctx);
|
|
573
|
+
if (!result) return input;
|
|
574
|
+
const [, , eventName, args] = result;
|
|
575
|
+
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
576
|
+
const event = args ? `on${callee}?.(${args})` : `on${callee}?.()`;
|
|
577
|
+
return `${ctx.propField}?.${event}`;
|
|
578
|
+
}
|
|
579
|
+
function matchEmitCalls(input, ctx) {
|
|
580
|
+
const { reactiveBindings } = ctx.templateData;
|
|
581
|
+
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
582
|
+
if (!macroBinding) return null;
|
|
583
|
+
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
584
|
+
const regex = new RegExp(
|
|
585
|
+
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
586
|
+
// 可选的第二个参数
|
|
587
|
+
"s"
|
|
588
|
+
// s 标志让 . 匹配换行符
|
|
589
|
+
);
|
|
590
|
+
return input.trim().match(regex);
|
|
591
|
+
}
|
|
592
|
+
function resolveRefVariable(input, ctx) {
|
|
593
|
+
const { reactiveBindings } = ctx.templateData;
|
|
594
|
+
const addValueProperty = (input2, varName) => {
|
|
595
|
+
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
596
|
+
const regex = new RegExp(
|
|
597
|
+
`(?<![a-zA-Z0-9_\\.])${escapedVarName}(?![a-zA-Z0-9_])(?!\\.value)`,
|
|
598
|
+
"g"
|
|
599
|
+
);
|
|
600
|
+
return input2.replace(regex, `${varName}.value`);
|
|
601
|
+
};
|
|
602
|
+
for (const name in reactiveBindings) {
|
|
603
|
+
const binding = reactiveBindings[name];
|
|
604
|
+
if (binding?.reactiveType !== "ref") continue;
|
|
605
|
+
input = addValueProperty(input, name);
|
|
606
|
+
}
|
|
607
|
+
return input;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
611
|
+
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
612
|
+
if (toStrLiteral) return t5.stringLiteral(input);
|
|
613
|
+
const { filename, scriptData } = ctx;
|
|
614
|
+
const newContent = resolveSpecialExpressions(input, ctx);
|
|
615
|
+
try {
|
|
616
|
+
return stringToExpr(newContent, scriptData.lang, filename);
|
|
617
|
+
} catch {
|
|
618
|
+
return t5.identifier(newContent);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-loop-node.ts
|
|
548
623
|
function buildLoopNode(nodeIR, ctx) {
|
|
549
624
|
const loop = nodeIR.meta.loop;
|
|
550
625
|
loop.isHandled = true;
|
|
@@ -554,15 +629,15 @@ function buildLoopNode(nodeIR, ctx) {
|
|
|
554
629
|
function buildArrayLoopNode(nodeIR, ctx) {
|
|
555
630
|
const loop = nodeIR.meta.loop;
|
|
556
631
|
const { source, value, index, key } = loop.value;
|
|
557
|
-
const
|
|
558
|
-
const params = [
|
|
632
|
+
const sourceExpression = resolveStringExpr(source, ctx);
|
|
633
|
+
const params = [t6.identifier(value)];
|
|
559
634
|
if (index ?? key) {
|
|
560
|
-
params.push(
|
|
635
|
+
params.push(t6.identifier(index ?? key));
|
|
561
636
|
}
|
|
562
|
-
const mapCallExpression =
|
|
563
|
-
|
|
637
|
+
const mapCallExpression = t6.callExpression(
|
|
638
|
+
t6.memberExpression(sourceExpression, t6.identifier("map")),
|
|
564
639
|
[
|
|
565
|
-
|
|
640
|
+
t6.arrowFunctionExpression(
|
|
566
641
|
params,
|
|
567
642
|
convertJsxChildToExpression(buildElementNode(nodeIR, ctx))
|
|
568
643
|
)
|
|
@@ -573,21 +648,21 @@ function buildArrayLoopNode(nodeIR, ctx) {
|
|
|
573
648
|
function buildObjectLoopNode(nodeIR, ctx) {
|
|
574
649
|
const loop = nodeIR.meta.loop;
|
|
575
650
|
const { source, value, key, index } = loop.value;
|
|
576
|
-
const
|
|
577
|
-
const objectEntriesCallExpression =
|
|
578
|
-
|
|
579
|
-
[
|
|
651
|
+
const sourceExpression = resolveStringExpr(source, ctx);
|
|
652
|
+
const objectEntriesCallExpression = t6.callExpression(
|
|
653
|
+
t6.memberExpression(t6.identifier("Object"), t6.identifier("entries")),
|
|
654
|
+
[sourceExpression]
|
|
580
655
|
);
|
|
581
656
|
const params = [
|
|
582
|
-
|
|
657
|
+
t6.arrayPattern([t6.identifier(key || "key"), t6.identifier(value)])
|
|
583
658
|
];
|
|
584
659
|
if (index) {
|
|
585
|
-
params.push(
|
|
660
|
+
params.push(t6.identifier(index));
|
|
586
661
|
}
|
|
587
|
-
const mapCallExpression =
|
|
588
|
-
|
|
662
|
+
const mapCallExpression = t6.callExpression(
|
|
663
|
+
t6.memberExpression(objectEntriesCallExpression, t6.identifier("map")),
|
|
589
664
|
[
|
|
590
|
-
|
|
665
|
+
t6.arrowFunctionExpression(
|
|
591
666
|
params,
|
|
592
667
|
convertJsxChildToExpression(buildElementNode(nodeIR, ctx))
|
|
593
668
|
)
|
|
@@ -597,25 +672,25 @@ function buildObjectLoopNode(nodeIR, ctx) {
|
|
|
597
672
|
}
|
|
598
673
|
|
|
599
674
|
// src/core/codegen/component/jsx/syntax-processor/process/build-memo-node.ts
|
|
600
|
-
import * as
|
|
675
|
+
import * as t7 from "@babel/types";
|
|
601
676
|
function buildMemoNode(nodeIR, ctx) {
|
|
602
677
|
const memo = nodeIR.meta.memo;
|
|
603
678
|
memo.isHandled = true;
|
|
604
679
|
const depsExpression = memo.babelExp.ast;
|
|
605
680
|
const bodyExpression = convertJsxChildToExpression(buildElementNode(nodeIR, ctx));
|
|
606
|
-
const useMemoCallExpression =
|
|
607
|
-
|
|
681
|
+
const useMemoCallExpression = t7.callExpression(t7.identifier("useMemo"), [
|
|
682
|
+
t7.arrowFunctionExpression([], bodyExpression),
|
|
608
683
|
depsExpression
|
|
609
684
|
]);
|
|
610
685
|
return buildJsxExpressionNode(useMemoCallExpression);
|
|
611
686
|
}
|
|
612
687
|
|
|
613
688
|
// src/core/codegen/component/jsx/syntax-processor/process/build-props.ts
|
|
614
|
-
import * as
|
|
689
|
+
import * as t9 from "@babel/types";
|
|
615
690
|
|
|
616
691
|
// src/core/codegen/component/jsx/syntax-processor/process/build-slot-prop.ts
|
|
617
692
|
import { parseExpression as parseExpression2 } from "@babel/parser";
|
|
618
|
-
import * as
|
|
693
|
+
import * as t8 from "@babel/types";
|
|
619
694
|
|
|
620
695
|
// src/shared/logger.ts
|
|
621
696
|
import kleur2 from "kleur";
|
|
@@ -624,6 +699,9 @@ import kleur2 from "kleur";
|
|
|
624
699
|
import { minimatch } from "minimatch";
|
|
625
700
|
import { fileURLToPath } from "url";
|
|
626
701
|
import path from "path";
|
|
702
|
+
function getDirname(metaUrl) {
|
|
703
|
+
return path.dirname(fileURLToPath(metaUrl));
|
|
704
|
+
}
|
|
627
705
|
var PathFilter = class {
|
|
628
706
|
patterns = [];
|
|
629
707
|
cwd;
|
|
@@ -864,7 +942,7 @@ var logger = new Logger();
|
|
|
864
942
|
|
|
865
943
|
// src/core/codegen/component/jsx/syntax-processor/process/build-slot-prop.ts
|
|
866
944
|
function buildSlotProp(nodeIR, ctx) {
|
|
867
|
-
const slotKey =
|
|
945
|
+
const slotKey = t8.jsxIdentifier(nodeIR.name);
|
|
868
946
|
const childrenNodeIR = !nodeIR.isScoped ? nodeIR.content : nodeIR.callback?.exp;
|
|
869
947
|
if (!childrenNodeIR?.length) {
|
|
870
948
|
return null;
|
|
@@ -873,7 +951,7 @@ function buildSlotProp(nodeIR, ctx) {
|
|
|
873
951
|
if (!jsxChild) {
|
|
874
952
|
return null;
|
|
875
953
|
}
|
|
876
|
-
const slotValue = nodeIR.isScoped ?
|
|
954
|
+
const slotValue = nodeIR.isScoped ? t8.arrowFunctionExpression(
|
|
877
955
|
buildSlotCallbackParams(nodeIR, ctx),
|
|
878
956
|
convertJsxChildToExpression(jsxChild)
|
|
879
957
|
) : jsxChild;
|
|
@@ -882,15 +960,15 @@ function buildSlotProp(nodeIR, ctx) {
|
|
|
882
960
|
}
|
|
883
961
|
if (!nodeIR.isStatic) {
|
|
884
962
|
const dynamicSlotKey = parseExpression2(nodeIR.name);
|
|
885
|
-
const spreadObject =
|
|
886
|
-
|
|
963
|
+
const spreadObject = t8.objectExpression([
|
|
964
|
+
t8.objectProperty(dynamicSlotKey, convertSlotValueToExpression(slotValue), true)
|
|
887
965
|
]);
|
|
888
|
-
return
|
|
966
|
+
return t8.jsxSpreadAttribute(spreadObject);
|
|
889
967
|
}
|
|
890
|
-
return
|
|
968
|
+
return t8.jsxAttribute(slotKey, buildJsxExpressionNode(convertSlotValueToExpression(slotValue)));
|
|
891
969
|
}
|
|
892
970
|
function convertSlotValueToExpression(nodeIR) {
|
|
893
|
-
if (
|
|
971
|
+
if (t8.isArrowFunctionExpression(nodeIR)) {
|
|
894
972
|
return nodeIR;
|
|
895
973
|
}
|
|
896
974
|
return convertJsxChildToExpression(nodeIR);
|
|
@@ -903,7 +981,7 @@ function buildSlotCallbackParams(nodeIR, ctx) {
|
|
|
903
981
|
const source = rawArg.startsWith("(") ? `${rawArg} => null` : `(${rawArg}) => null`;
|
|
904
982
|
try {
|
|
905
983
|
const expression = parseExpression2(source);
|
|
906
|
-
if (!
|
|
984
|
+
if (!t8.isArrowFunctionExpression(expression)) {
|
|
907
985
|
return [];
|
|
908
986
|
}
|
|
909
987
|
return expression.params;
|
|
@@ -946,13 +1024,13 @@ function buildStandardProp(nodeIR) {
|
|
|
946
1024
|
}
|
|
947
1025
|
} = nodeIR;
|
|
948
1026
|
if (!isStatic || isKeyLessVBind) {
|
|
949
|
-
return
|
|
1027
|
+
return t9.jsxSpreadAttribute(valueAST);
|
|
950
1028
|
}
|
|
951
1029
|
let value;
|
|
952
1030
|
if (content !== "true") {
|
|
953
|
-
value = isStringLiteral13 ?
|
|
1031
|
+
value = isStringLiteral13 ? t9.stringLiteral(content) : buildJsxExpressionNode(valueAST);
|
|
954
1032
|
}
|
|
955
|
-
return
|
|
1033
|
+
return t9.jsxAttribute(keyAST, value);
|
|
956
1034
|
}
|
|
957
1035
|
|
|
958
1036
|
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node.ts
|
|
@@ -998,8 +1076,8 @@ function buildConditionNode(nodeIR, ctx) {
|
|
|
998
1076
|
}
|
|
999
1077
|
const testExpression = condition.babelExp.ast;
|
|
1000
1078
|
const trueBranchExpression = convertJsxChildToExpression(buildCurrentNode());
|
|
1001
|
-
const falseBranchExpression = nextNodeIR ? convertJsxChildToExpression(buildElementNode(nextNodeIR, ctx)) :
|
|
1002
|
-
const conditionalExpression2 =
|
|
1079
|
+
const falseBranchExpression = nextNodeIR ? convertJsxChildToExpression(buildElementNode(nextNodeIR, ctx)) : t10.nullLiteral();
|
|
1080
|
+
const conditionalExpression2 = t10.conditionalExpression(
|
|
1003
1081
|
testExpression,
|
|
1004
1082
|
// 条件测试部分
|
|
1005
1083
|
trueBranchExpression,
|
|
@@ -1019,19 +1097,19 @@ function buildCtxProviderNode(nodeIR, ctx, children) {
|
|
|
1019
1097
|
childNodes = [buildCtxProviderNode(nextProvide, ctx, children)];
|
|
1020
1098
|
}
|
|
1021
1099
|
const parseProviderExpr = (raw) => {
|
|
1022
|
-
if (!raw) return
|
|
1100
|
+
if (!raw) return t11.stringLiteral("");
|
|
1023
1101
|
try {
|
|
1024
1102
|
return stringToExpr(raw, ctx.scriptData.lang, ctx.filename);
|
|
1025
1103
|
} catch {
|
|
1026
|
-
return
|
|
1104
|
+
return t11.stringLiteral(raw);
|
|
1027
1105
|
}
|
|
1028
1106
|
};
|
|
1029
|
-
const nameProp =
|
|
1030
|
-
|
|
1107
|
+
const nameProp = t11.jsxAttribute(
|
|
1108
|
+
t11.jsxIdentifier("name"),
|
|
1031
1109
|
buildJsxExpressionNode(parseProviderExpr(name))
|
|
1032
1110
|
);
|
|
1033
|
-
const valueProp =
|
|
1034
|
-
|
|
1111
|
+
const valueProp = t11.jsxAttribute(
|
|
1112
|
+
t11.jsxIdentifier("value"),
|
|
1035
1113
|
buildJsxExpressionNode(parseProviderExpr(value))
|
|
1036
1114
|
);
|
|
1037
1115
|
void ctx;
|
|
@@ -1085,29 +1163,29 @@ function buildJSX(nodeIR, ctx) {
|
|
|
1085
1163
|
}
|
|
1086
1164
|
|
|
1087
1165
|
// src/core/codegen/component/script/syntax-processor/index.ts
|
|
1088
|
-
import * as
|
|
1166
|
+
import * as t14 from "@babel/types";
|
|
1089
1167
|
|
|
1090
1168
|
// src/core/codegen/component/script/syntax-processor/postprocess/build-program-node.ts
|
|
1091
|
-
import * as
|
|
1169
|
+
import * as t12 from "@babel/types";
|
|
1092
1170
|
function buildProgramNode(nodeIR, ctx, state) {
|
|
1093
1171
|
const statements = [...state.preambleStatements];
|
|
1094
1172
|
if (!state.component) {
|
|
1095
|
-
state.result =
|
|
1173
|
+
state.result = t12.program(statements, void 0, "module");
|
|
1096
1174
|
return;
|
|
1097
1175
|
}
|
|
1098
1176
|
statements.push(state.component);
|
|
1099
1177
|
if (state.expose) {
|
|
1100
1178
|
const [declarator] = state.component.declarations;
|
|
1101
1179
|
const { name } = declarator.id;
|
|
1102
|
-
statements.push(
|
|
1180
|
+
statements.push(t12.exportDefaultDeclaration(t12.identifier(name)));
|
|
1103
1181
|
}
|
|
1104
|
-
state.result =
|
|
1182
|
+
state.result = t12.program(statements, void 0, "module");
|
|
1105
1183
|
void nodeIR;
|
|
1106
1184
|
void ctx;
|
|
1107
1185
|
}
|
|
1108
1186
|
|
|
1109
1187
|
// src/core/codegen/component/script/syntax-processor/process/build-component-function.ts
|
|
1110
|
-
import * as
|
|
1188
|
+
import * as t13 from "@babel/types";
|
|
1111
1189
|
|
|
1112
1190
|
// src/consts/react-api-map.ts
|
|
1113
1191
|
var REACT_API_MAP = {
|
|
@@ -1121,19 +1199,6 @@ var REACT_API_MAP = {
|
|
|
1121
1199
|
useImperativeHandle: "useImperativeHandle"
|
|
1122
1200
|
};
|
|
1123
1201
|
|
|
1124
|
-
// src/utils/camelCase.ts
|
|
1125
|
-
var camelCase = (str) => {
|
|
1126
|
-
if (str.includes("-")) {
|
|
1127
|
-
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
1128
|
-
}
|
|
1129
|
-
return str;
|
|
1130
|
-
};
|
|
1131
|
-
|
|
1132
|
-
// src/utils/capitalize.ts
|
|
1133
|
-
var capitalize = (str) => {
|
|
1134
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1135
|
-
};
|
|
1136
|
-
|
|
1137
1202
|
// src/utils/hash.ts
|
|
1138
1203
|
import XXH from "xxhashjs";
|
|
1139
1204
|
function genHashByXXH(str) {
|
|
@@ -1157,7 +1222,7 @@ function buildComponent(scriptIR, ctx, state) {
|
|
|
1157
1222
|
} = ctx;
|
|
1158
1223
|
const hasProps = getHasProps(propsTSIface.propsTypes) || getHasProps(propsTSIface.emitTypes) || getHasProps(propsTSIface.slotTypes);
|
|
1159
1224
|
const shouldMemo = scriptIR !== null || hasProps;
|
|
1160
|
-
const jsxStatement =
|
|
1225
|
+
const jsxStatement = t13.returnStatement(state.jsx || t13.nullLiteral());
|
|
1161
1226
|
const component = !shouldMemo ? resolvePureUIComponent(jsxStatement, ctx) : resolveMemoComponent(scriptIR.statement.local, jsxStatement, ctx);
|
|
1162
1227
|
state.component = component;
|
|
1163
1228
|
}
|
|
@@ -1165,24 +1230,24 @@ function getHasProps(list) {
|
|
|
1165
1230
|
return list.length > 0;
|
|
1166
1231
|
}
|
|
1167
1232
|
function resolvePureUIComponent(jsxStmt, ctx) {
|
|
1168
|
-
return
|
|
1169
|
-
|
|
1233
|
+
return t13.variableDeclaration("const", [
|
|
1234
|
+
t13.variableDeclarator(
|
|
1170
1235
|
resolveComponentName(ctx),
|
|
1171
|
-
|
|
1236
|
+
t13.arrowFunctionExpression([], t13.blockStatement([jsxStmt]))
|
|
1172
1237
|
)
|
|
1173
1238
|
]);
|
|
1174
1239
|
}
|
|
1175
1240
|
function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
1176
1241
|
const name = resolveComponentName(ctx);
|
|
1177
1242
|
const param = resolvePropsParam(ctx);
|
|
1178
|
-
const body =
|
|
1243
|
+
const body = t13.blockStatement(resolveLocalStatements(local, jsxStmt));
|
|
1179
1244
|
const component = resolveComponent(param, body, ctx);
|
|
1180
1245
|
let compFn = component;
|
|
1181
1246
|
if (ctx.scriptData.forwardRef.enabled) {
|
|
1182
1247
|
compFn = resolveForwardRef(body, ctx);
|
|
1183
1248
|
}
|
|
1184
|
-
const memoCall =
|
|
1185
|
-
return
|
|
1249
|
+
const memoCall = t13.callExpression(t13.identifier(REACT_API_MAP.memo), [compFn]);
|
|
1250
|
+
return t13.variableDeclaration("const", [t13.variableDeclarator(name, memoCall)]);
|
|
1186
1251
|
}
|
|
1187
1252
|
function resolveComponentName(ctx) {
|
|
1188
1253
|
const { filename, compName } = ctx;
|
|
@@ -1196,7 +1261,7 @@ function resolveComponentName(ctx) {
|
|
|
1196
1261
|
file: filename
|
|
1197
1262
|
});
|
|
1198
1263
|
}
|
|
1199
|
-
return
|
|
1264
|
+
return t13.identifier(name);
|
|
1200
1265
|
}
|
|
1201
1266
|
function resolveLocalStatements(local, jsx) {
|
|
1202
1267
|
const stmts = [jsx];
|
|
@@ -1216,7 +1281,7 @@ function resolveComponent(param, body, ctx) {
|
|
|
1216
1281
|
if (forwardRef.enabled) {
|
|
1217
1282
|
return resolveForwardRef(body, ctx);
|
|
1218
1283
|
}
|
|
1219
|
-
const baseComponent =
|
|
1284
|
+
const baseComponent = t13.arrowFunctionExpression(!param ? [] : [param], body);
|
|
1220
1285
|
return baseComponent;
|
|
1221
1286
|
}
|
|
1222
1287
|
function resolveForwardRef(body, ctx) {
|
|
@@ -1224,18 +1289,18 @@ function resolveForwardRef(body, ctx) {
|
|
|
1224
1289
|
propField,
|
|
1225
1290
|
scriptData: { forwardRef, lang }
|
|
1226
1291
|
} = ctx;
|
|
1227
|
-
const params = [
|
|
1292
|
+
const params = [t13.identifier(forwardRef.refField)];
|
|
1228
1293
|
const propsId = resolvePropsParam(ctx);
|
|
1229
|
-
const callExpr =
|
|
1230
|
-
|
|
1294
|
+
const callExpr = t13.callExpression(t13.identifier(REACT_API_MAP.forwardRef), [
|
|
1295
|
+
t13.arrowFunctionExpression(params, body)
|
|
1231
1296
|
]);
|
|
1232
1297
|
if (lang.startsWith("ts")) {
|
|
1233
|
-
const types = [
|
|
1298
|
+
const types = [t13.tsAnyKeyword()];
|
|
1234
1299
|
if (propsId) {
|
|
1235
1300
|
const propsType = propsId?.typeAnnotation?.typeAnnotation;
|
|
1236
|
-
types.push(propsType ||
|
|
1301
|
+
types.push(propsType || t13.tsAnyKeyword());
|
|
1237
1302
|
}
|
|
1238
|
-
callExpr.typeParameters =
|
|
1303
|
+
callExpr.typeParameters = t13.tsTypeParameterInstantiation(types);
|
|
1239
1304
|
}
|
|
1240
1305
|
if (propsId) {
|
|
1241
1306
|
propsId.typeAnnotation = null;
|
|
@@ -1246,7 +1311,7 @@ function resolveForwardRef(body, ctx) {
|
|
|
1246
1311
|
function resolvePropsParam(ctx) {
|
|
1247
1312
|
const { propField, scriptData } = ctx;
|
|
1248
1313
|
const { propsTSIface } = scriptData;
|
|
1249
|
-
const propsIdentifier =
|
|
1314
|
+
const propsIdentifier = t13.identifier(propField);
|
|
1250
1315
|
if (scriptData.lang.startsWith("js")) {
|
|
1251
1316
|
if (propsTSIface.hasPropsInJsEnv) {
|
|
1252
1317
|
return propsIdentifier;
|
|
@@ -1256,8 +1321,8 @@ function resolvePropsParam(ctx) {
|
|
|
1256
1321
|
if (!propsTSIface.name) {
|
|
1257
1322
|
return;
|
|
1258
1323
|
}
|
|
1259
|
-
const typeIdentifier =
|
|
1260
|
-
propsIdentifier.typeAnnotation =
|
|
1324
|
+
const typeIdentifier = t13.identifier(propsTSIface.name);
|
|
1325
|
+
propsIdentifier.typeAnnotation = t13.tsTypeAnnotation(t13.tsTypeReference(typeIdentifier));
|
|
1261
1326
|
return propsIdentifier;
|
|
1262
1327
|
}
|
|
1263
1328
|
|
|
@@ -1282,7 +1347,7 @@ function buildScriptSyntax(nodeIR, ctx, jsx, expose = true) {
|
|
|
1282
1347
|
process: [buildProgramPreamble, buildComponent],
|
|
1283
1348
|
postprocess: [buildProgramNode]
|
|
1284
1349
|
});
|
|
1285
|
-
return state.result ||
|
|
1350
|
+
return state.result || t14.program([], void 0, "module");
|
|
1286
1351
|
}
|
|
1287
1352
|
function scriptSyntaxProcessor(nodeIR, ctx, state, options) {
|
|
1288
1353
|
const run = (p) => {
|
|
@@ -1377,7 +1442,7 @@ import {
|
|
|
1377
1442
|
// src/core/parse/sfc/process/resolve-script.ts
|
|
1378
1443
|
import { generate as generate2 } from "@babel/generator";
|
|
1379
1444
|
import { parse as babelParse2 } from "@babel/parser";
|
|
1380
|
-
import * as
|
|
1445
|
+
import * as t15 from "@babel/types";
|
|
1381
1446
|
function resolveScript(descriptor, ctx, pResult) {
|
|
1382
1447
|
const scriptBlock = descriptor.scriptSetup || descriptor.script;
|
|
1383
1448
|
if (!scriptBlock) return null;
|
|
@@ -1419,41 +1484,41 @@ function extractSetupBodyToTopLevel(content, options) {
|
|
|
1419
1484
|
const otherTopLevel = [];
|
|
1420
1485
|
let setupStatements = [];
|
|
1421
1486
|
for (const node of ast.program.body) {
|
|
1422
|
-
if (
|
|
1487
|
+
if (t15.isImportDeclaration(node)) {
|
|
1423
1488
|
importNodes.push(node);
|
|
1424
1489
|
continue;
|
|
1425
1490
|
}
|
|
1426
|
-
if (
|
|
1491
|
+
if (t15.isExportDefaultDeclaration(node)) {
|
|
1427
1492
|
const decl = node.declaration;
|
|
1428
|
-
if (decl &&
|
|
1493
|
+
if (decl && t15.isObjectExpression(decl)) {
|
|
1429
1494
|
const nameProp = decl.properties.find((p) => {
|
|
1430
|
-
if (
|
|
1495
|
+
if (t15.isObjectProperty(p)) {
|
|
1431
1496
|
const key = p.key;
|
|
1432
|
-
return
|
|
1497
|
+
return t15.isIdentifier(key) && key.name === "name" || t15.isStringLiteral(key) && key.value === "name";
|
|
1433
1498
|
}
|
|
1434
1499
|
return false;
|
|
1435
1500
|
});
|
|
1436
|
-
if (nameProp &&
|
|
1437
|
-
if (
|
|
1501
|
+
if (nameProp && t15.isObjectProperty(nameProp)) {
|
|
1502
|
+
if (t15.isStringLiteral(nameProp.value)) {
|
|
1438
1503
|
name = nameProp.value.value;
|
|
1439
1504
|
}
|
|
1440
1505
|
}
|
|
1441
1506
|
const setupProp = decl.properties.find((p) => {
|
|
1442
|
-
if (
|
|
1507
|
+
if (t15.isObjectMethod(p)) {
|
|
1443
1508
|
return p.key && p.key.name === "setup";
|
|
1444
1509
|
}
|
|
1445
|
-
if (
|
|
1510
|
+
if (t15.isObjectProperty(p)) {
|
|
1446
1511
|
const key = p.key;
|
|
1447
|
-
return
|
|
1512
|
+
return t15.isIdentifier(key) && key.name === "setup" || t15.isStringLiteral(key) && key.value === "setup";
|
|
1448
1513
|
}
|
|
1449
1514
|
return false;
|
|
1450
1515
|
});
|
|
1451
1516
|
if (setupProp) {
|
|
1452
|
-
const value =
|
|
1453
|
-
if (value && (
|
|
1454
|
-
const fnBody =
|
|
1517
|
+
const value = t15.isObjectMethod(setupProp) ? setupProp : t15.isObjectProperty(setupProp) ? setupProp.value : null;
|
|
1518
|
+
if (value && (t15.isFunction(value) || t15.isObjectMethod(value))) {
|
|
1519
|
+
const fnBody = t15.isBlockStatement(value.body) ? value.body.body : [];
|
|
1455
1520
|
for (const stmt of fnBody) {
|
|
1456
|
-
if (
|
|
1521
|
+
if (t15.isReturnStatement(stmt)) continue;
|
|
1457
1522
|
setupStatements.push(stmt);
|
|
1458
1523
|
}
|
|
1459
1524
|
}
|
|
@@ -1481,7 +1546,7 @@ function extractCompName(source) {
|
|
|
1481
1546
|
|
|
1482
1547
|
// src/core/parse/sfc/process/resolve-script-meta.ts
|
|
1483
1548
|
import { traverse } from "@babel/core";
|
|
1484
|
-
import * as
|
|
1549
|
+
import * as t16 from "@babel/types";
|
|
1485
1550
|
|
|
1486
1551
|
// src/consts/html-tag-types.ts
|
|
1487
1552
|
var HTML_TAG_TYPES = {
|
|
@@ -1585,12 +1650,12 @@ function resolveScriptMeta(result, ctx) {
|
|
|
1585
1650
|
const scriptAST = result.script?.ast;
|
|
1586
1651
|
if (!scriptAST) return;
|
|
1587
1652
|
traverse(scriptAST, {
|
|
1588
|
-
VariableDeclarator(
|
|
1589
|
-
const { node } =
|
|
1590
|
-
if (!atComponentOrHookRoot(
|
|
1653
|
+
VariableDeclarator(path8) {
|
|
1654
|
+
const { node } = path8;
|
|
1655
|
+
if (!atComponentOrHookRoot(path8, scriptAST.program) || !t16.isIdentifier(node.id)) {
|
|
1591
1656
|
return;
|
|
1592
1657
|
}
|
|
1593
|
-
if (node.init &&
|
|
1658
|
+
if (node.init && t16.isCallExpression(node.init) && t16.isIdentifier(node.init.callee)) {
|
|
1594
1659
|
collectReactiveBindings(node, ctx);
|
|
1595
1660
|
collectRefBindings(node, ctx);
|
|
1596
1661
|
}
|
|
@@ -1868,7 +1933,7 @@ import { parse as babelParse3 } from "@babel/parser";
|
|
|
1868
1933
|
import { traverse as traverse3 } from "@babel/core";
|
|
1869
1934
|
|
|
1870
1935
|
// src/core/transform/sfc/script/syntax-processor/postprocess/insert-css-import.ts
|
|
1871
|
-
import * as
|
|
1936
|
+
import * as t17 from "@babel/types";
|
|
1872
1937
|
function insertCSSImport(ctx) {
|
|
1873
1938
|
const { inputType } = ctx;
|
|
1874
1939
|
if (inputType !== "sfc") return;
|
|
@@ -1877,15 +1942,15 @@ function insertCSSImport(ctx) {
|
|
|
1877
1942
|
const scriptIR = getScriptIR(ctx);
|
|
1878
1943
|
const filename = normalizePath(filePath).split("/").pop();
|
|
1879
1944
|
const importPath = `./${filename}`;
|
|
1880
|
-
const importDecl =
|
|
1881
|
-
!moduleName ? [] : [
|
|
1882
|
-
|
|
1945
|
+
const importDecl = t17.importDeclaration(
|
|
1946
|
+
!moduleName ? [] : [t17.importDefaultSpecifier(t17.identifier(moduleName))],
|
|
1947
|
+
t17.stringLiteral(importPath)
|
|
1883
1948
|
);
|
|
1884
1949
|
scriptIR.imports.push(importDecl);
|
|
1885
1950
|
}
|
|
1886
1951
|
|
|
1887
1952
|
// src/core/transform/sfc/script/syntax-processor/postprocess/insert-required-imports.ts
|
|
1888
|
-
import * as
|
|
1953
|
+
import * as t18 from "@babel/types";
|
|
1889
1954
|
|
|
1890
1955
|
// src/core/transform/shared.ts
|
|
1891
1956
|
function recordImport(ctx, pkg, name, onDemand = true) {
|
|
@@ -1921,36 +1986,36 @@ function insertRequiredImports(ctx) {
|
|
|
1921
1986
|
const processedModules = /* @__PURE__ */ new Set();
|
|
1922
1987
|
let hasProcessedImports = false;
|
|
1923
1988
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.memo);
|
|
1924
|
-
function resolveRequiredImport(
|
|
1925
|
-
const { node } =
|
|
1989
|
+
function resolveRequiredImport(path8) {
|
|
1990
|
+
const { node } = path8;
|
|
1926
1991
|
const moduleName = node.source.value.toLowerCase();
|
|
1927
1992
|
const isVueLike = isVueEcosystemPackage(moduleName);
|
|
1928
1993
|
mergeImports(node, ctx);
|
|
1929
|
-
if (processedModules.has(moduleName) && !
|
|
1930
|
-
|
|
1994
|
+
if (processedModules.has(moduleName) && !path8.removed) {
|
|
1995
|
+
path8.remove();
|
|
1931
1996
|
return;
|
|
1932
1997
|
}
|
|
1933
1998
|
processedModules.add(moduleName);
|
|
1934
1999
|
if (!hasProcessedImports) {
|
|
1935
2000
|
const required = createRequiredImports(ctx);
|
|
1936
2001
|
if (isVueLike) {
|
|
1937
|
-
|
|
2002
|
+
path8.replaceWithMultiple(required);
|
|
1938
2003
|
} else if (moduleName === PACKAGE_NAME.react) {
|
|
1939
|
-
|
|
2004
|
+
path8.insertAfter(required);
|
|
1940
2005
|
} else {
|
|
1941
|
-
|
|
2006
|
+
path8.insertBefore(required);
|
|
1942
2007
|
}
|
|
1943
2008
|
hasProcessedImports = true;
|
|
1944
2009
|
}
|
|
1945
|
-
if (isVueLike && !
|
|
1946
|
-
|
|
2010
|
+
if (isVueLike && !path8.removed) {
|
|
2011
|
+
path8.remove();
|
|
1947
2012
|
return;
|
|
1948
2013
|
}
|
|
1949
2014
|
replaceVueSuffix(ctx, node.source);
|
|
1950
2015
|
}
|
|
1951
|
-
function resolveStyleFileExt(
|
|
2016
|
+
function resolveStyleFileExt(path8) {
|
|
1952
2017
|
if (!ctx.preprocessStyles) return;
|
|
1953
|
-
const { node } =
|
|
2018
|
+
const { node } = path8;
|
|
1954
2019
|
if (!node || !node.source || !node.source.value) return;
|
|
1955
2020
|
const importSource = node.source.value;
|
|
1956
2021
|
if (typeof importSource !== "string") return;
|
|
@@ -1962,16 +2027,16 @@ function insertRequiredImports(ctx) {
|
|
|
1962
2027
|
return {
|
|
1963
2028
|
// 增加 Program.exit 兜底注入 required imports(处理无 ImportDeclaration 的 SFC)
|
|
1964
2029
|
Program: {
|
|
1965
|
-
exit(
|
|
2030
|
+
exit(path8) {
|
|
1966
2031
|
if (hasProcessedImports) return;
|
|
1967
2032
|
const required = createRequiredImports(ctx);
|
|
1968
|
-
|
|
2033
|
+
path8.unshiftContainer("body", required);
|
|
1969
2034
|
hasProcessedImports = true;
|
|
1970
2035
|
}
|
|
1971
2036
|
},
|
|
1972
|
-
ImportDeclaration(
|
|
1973
|
-
resolveRequiredImport(
|
|
1974
|
-
resolveStyleFileExt(
|
|
2037
|
+
ImportDeclaration(path8) {
|
|
2038
|
+
resolveRequiredImport(path8);
|
|
2039
|
+
resolveStyleFileExt(path8);
|
|
1975
2040
|
}
|
|
1976
2041
|
};
|
|
1977
2042
|
}
|
|
@@ -1995,17 +2060,17 @@ function mergeImports(currentNode, ctx) {
|
|
|
1995
2060
|
}
|
|
1996
2061
|
const currentImports = /* @__PURE__ */ new Set();
|
|
1997
2062
|
for (const spec of currentNode.specifiers) {
|
|
1998
|
-
if (
|
|
2063
|
+
if (t18.isImportSpecifier(spec) && t18.isIdentifier(spec.imported)) {
|
|
1999
2064
|
currentImports.add(spec.imported.name);
|
|
2000
2065
|
}
|
|
2001
|
-
if (
|
|
2066
|
+
if (t18.isImportDefaultSpecifier(spec) && t18.isIdentifier(spec.local)) {
|
|
2002
2067
|
currentImports.add(spec.local.name);
|
|
2003
2068
|
}
|
|
2004
2069
|
}
|
|
2005
2070
|
for (const item of ctxImportItems) {
|
|
2006
2071
|
if (currentImports.has(item.name)) return;
|
|
2007
|
-
const local =
|
|
2008
|
-
const newNode = !item.onDemand ?
|
|
2072
|
+
const local = t18.identifier(item.name);
|
|
2073
|
+
const newNode = !item.onDemand ? t18.importDefaultSpecifier(local) : t18.importSpecifier(local, local);
|
|
2009
2074
|
currentNode.specifiers.push(newNode);
|
|
2010
2075
|
}
|
|
2011
2076
|
ctx.imports.delete(moduleName);
|
|
@@ -2016,18 +2081,18 @@ function createRequiredImports(ctx) {
|
|
|
2016
2081
|
ctx.imports.forEach((items, moduleName) => {
|
|
2017
2082
|
const specifier = [];
|
|
2018
2083
|
for (const item of items) {
|
|
2019
|
-
const local =
|
|
2084
|
+
const local = t18.identifier(item.name);
|
|
2020
2085
|
if (!item.onDemand) {
|
|
2021
|
-
specifier.push(
|
|
2086
|
+
specifier.push(t18.importDefaultSpecifier(local));
|
|
2022
2087
|
} else {
|
|
2023
|
-
specifier.push(
|
|
2088
|
+
specifier.push(t18.importSpecifier(local, local));
|
|
2024
2089
|
}
|
|
2025
2090
|
}
|
|
2026
2091
|
importMap[moduleName] = specifier;
|
|
2027
2092
|
});
|
|
2028
2093
|
for (const name in importMap) {
|
|
2029
2094
|
const specifiers = importMap[name];
|
|
2030
|
-
const importDecl =
|
|
2095
|
+
const importDecl = t18.importDeclaration(specifiers, t18.stringLiteral(name));
|
|
2031
2096
|
if (name === PACKAGE_NAME.react) {
|
|
2032
2097
|
result.unshift(importDecl);
|
|
2033
2098
|
} else {
|
|
@@ -2038,33 +2103,33 @@ function createRequiredImports(ctx) {
|
|
|
2038
2103
|
}
|
|
2039
2104
|
|
|
2040
2105
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-static-hoisting.ts
|
|
2041
|
-
import * as
|
|
2106
|
+
import * as t20 from "@babel/types";
|
|
2042
2107
|
|
|
2043
2108
|
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
2044
|
-
import * as
|
|
2045
|
-
function findRootVariablePath(
|
|
2046
|
-
const rootId = findRootIdentifier(
|
|
2109
|
+
import * as t19 from "@babel/types";
|
|
2110
|
+
function findRootVariablePath(path8) {
|
|
2111
|
+
const rootId = findRootIdentifier(path8.node);
|
|
2047
2112
|
if (!rootId?.name) return null;
|
|
2048
|
-
const binding =
|
|
2113
|
+
const binding = path8.scope.getBinding(rootId.name);
|
|
2049
2114
|
if (!binding) return null;
|
|
2050
2115
|
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
2051
2116
|
return rootPath;
|
|
2052
2117
|
}
|
|
2053
2118
|
function findRootIdentifier(node) {
|
|
2054
2119
|
let current = node.object;
|
|
2055
|
-
while (
|
|
2120
|
+
while (t19.isMemberExpression(current) || t19.isOptionalMemberExpression(current)) {
|
|
2056
2121
|
current = current.object;
|
|
2057
2122
|
}
|
|
2058
|
-
return
|
|
2123
|
+
return t19.isIdentifier(current) ? current : null;
|
|
2059
2124
|
}
|
|
2060
|
-
function getVariableDeclaratorPath(
|
|
2061
|
-
if (
|
|
2062
|
-
return
|
|
2125
|
+
function getVariableDeclaratorPath(path8) {
|
|
2126
|
+
if (path8.isVariableDeclarator()) {
|
|
2127
|
+
return path8;
|
|
2063
2128
|
}
|
|
2064
|
-
return
|
|
2129
|
+
return path8.findParent((p) => p.isVariableDeclarator());
|
|
2065
2130
|
}
|
|
2066
|
-
function isVariableDeclTopLevel(
|
|
2067
|
-
const variableDeclaratorPath =
|
|
2131
|
+
function isVariableDeclTopLevel(path8) {
|
|
2132
|
+
const variableDeclaratorPath = path8;
|
|
2068
2133
|
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
2069
2134
|
if (!variableDeclarationPath) {
|
|
2070
2135
|
return false;
|
|
@@ -2078,75 +2143,75 @@ function isVariableDeclTopLevel(path7) {
|
|
|
2078
2143
|
}
|
|
2079
2144
|
return false;
|
|
2080
2145
|
}
|
|
2081
|
-
function isRealVariableAccess(
|
|
2082
|
-
return isIdentifierAccess(
|
|
2146
|
+
function isRealVariableAccess(path8) {
|
|
2147
|
+
return isIdentifierAccess(path8) && !isPropertyName(path8);
|
|
2083
2148
|
}
|
|
2084
|
-
function isIdentifierAccess(
|
|
2085
|
-
if (isIdentifierDeclaration(
|
|
2149
|
+
function isIdentifierAccess(path8) {
|
|
2150
|
+
if (isIdentifierDeclaration(path8)) {
|
|
2086
2151
|
return false;
|
|
2087
2152
|
}
|
|
2088
|
-
const binding =
|
|
2153
|
+
const binding = path8.scope.getBinding(path8.node.name);
|
|
2089
2154
|
if (!binding) {
|
|
2090
2155
|
return true;
|
|
2091
2156
|
}
|
|
2092
|
-
return binding.identifier !==
|
|
2157
|
+
return binding.identifier !== path8.node;
|
|
2093
2158
|
}
|
|
2094
|
-
function isIdentifierDeclaration(
|
|
2095
|
-
const parent =
|
|
2159
|
+
function isIdentifierDeclaration(path8) {
|
|
2160
|
+
const parent = path8.parentPath;
|
|
2096
2161
|
if (!parent) return false;
|
|
2097
|
-
if (parent.isVariableDeclarator() && parent.node.id ===
|
|
2162
|
+
if (parent.isVariableDeclarator() && parent.node.id === path8.node) {
|
|
2098
2163
|
return true;
|
|
2099
2164
|
}
|
|
2100
|
-
if (parent.isFunctionDeclaration() && parent.node.id ===
|
|
2165
|
+
if (parent.isFunctionDeclaration() && parent.node.id === path8.node) {
|
|
2101
2166
|
return true;
|
|
2102
2167
|
}
|
|
2103
|
-
if (parent.isFunctionExpression() && parent.node.id ===
|
|
2168
|
+
if (parent.isFunctionExpression() && parent.node.id === path8.node) {
|
|
2104
2169
|
return true;
|
|
2105
2170
|
}
|
|
2106
|
-
if (parent.isClassDeclaration() && parent.node.id ===
|
|
2171
|
+
if (parent.isClassDeclaration() && parent.node.id === path8.node) {
|
|
2107
2172
|
return true;
|
|
2108
2173
|
}
|
|
2109
|
-
if (parent.isImportSpecifier() && parent.node.local ===
|
|
2174
|
+
if (parent.isImportSpecifier() && parent.node.local === path8.node) {
|
|
2110
2175
|
return true;
|
|
2111
2176
|
}
|
|
2112
|
-
if (parent.isImportDefaultSpecifier() && parent.node.local ===
|
|
2177
|
+
if (parent.isImportDefaultSpecifier() && parent.node.local === path8.node) {
|
|
2113
2178
|
return true;
|
|
2114
2179
|
}
|
|
2115
|
-
if (parent.isImportNamespaceSpecifier() && parent.node.local ===
|
|
2180
|
+
if (parent.isImportNamespaceSpecifier() && parent.node.local === path8.node) {
|
|
2116
2181
|
return true;
|
|
2117
2182
|
}
|
|
2118
|
-
if (parent.isFunction() && parent.node.params.includes(
|
|
2183
|
+
if (parent.isFunction() && parent.node.params.includes(path8.node)) {
|
|
2119
2184
|
return true;
|
|
2120
2185
|
}
|
|
2121
|
-
if (parent.isCatchClause() && parent.node.param ===
|
|
2186
|
+
if (parent.isCatchClause() && parent.node.param === path8.node) {
|
|
2122
2187
|
return true;
|
|
2123
2188
|
}
|
|
2124
2189
|
return false;
|
|
2125
2190
|
}
|
|
2126
|
-
function isPropertyName(
|
|
2127
|
-
const parent =
|
|
2191
|
+
function isPropertyName(path8) {
|
|
2192
|
+
const parent = path8.parentPath;
|
|
2128
2193
|
if (!parent) return false;
|
|
2129
|
-
if (parent.isObjectProperty() && parent.node.key ===
|
|
2194
|
+
if (parent.isObjectProperty() && parent.node.key === path8.node) {
|
|
2130
2195
|
return true;
|
|
2131
2196
|
}
|
|
2132
|
-
if (parent.isClassProperty() && parent.node.key ===
|
|
2197
|
+
if (parent.isClassProperty() && parent.node.key === path8.node) {
|
|
2133
2198
|
return true;
|
|
2134
2199
|
}
|
|
2135
|
-
if (parent.isMemberExpression() && parent.node.property ===
|
|
2200
|
+
if (parent.isMemberExpression() && parent.node.property === path8.node) {
|
|
2136
2201
|
return true;
|
|
2137
2202
|
}
|
|
2138
2203
|
return false;
|
|
2139
2204
|
}
|
|
2140
2205
|
function replaceCallName(callExp, identifierName) {
|
|
2141
2206
|
const { callee } = callExp;
|
|
2142
|
-
if (!
|
|
2207
|
+
if (!t19.isIdentifier(callee)) return;
|
|
2143
2208
|
callee.name = identifierName;
|
|
2144
2209
|
if (callee.loc) {
|
|
2145
2210
|
callee.loc.identifierName = identifierName;
|
|
2146
2211
|
}
|
|
2147
2212
|
}
|
|
2148
2213
|
function replaceIdName(id, newName) {
|
|
2149
|
-
if (!
|
|
2214
|
+
if (!t19.isIdentifier(id)) return;
|
|
2150
2215
|
id.name = newName;
|
|
2151
2216
|
if (id.loc) {
|
|
2152
2217
|
id.loc.identifierName = newName;
|
|
@@ -2156,65 +2221,65 @@ function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
|
2156
2221
|
const { filename, scriptData } = ctx;
|
|
2157
2222
|
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
2158
2223
|
const ts = expressionToTSType(exp);
|
|
2159
|
-
return tsTypeAnnotation7 ?
|
|
2224
|
+
return tsTypeAnnotation7 ? t19.tsTypeAnnotation(ts) : ts;
|
|
2160
2225
|
}
|
|
2161
2226
|
function expressionToTSType(exp) {
|
|
2162
|
-
if (
|
|
2163
|
-
if (
|
|
2164
|
-
if (
|
|
2165
|
-
if (
|
|
2166
|
-
if (
|
|
2227
|
+
if (t19.isStringLiteral(exp)) return t19.tsStringKeyword();
|
|
2228
|
+
if (t19.isNumericLiteral(exp)) return t19.tsNumberKeyword();
|
|
2229
|
+
if (t19.isBooleanLiteral(exp)) return t19.tsBooleanKeyword();
|
|
2230
|
+
if (t19.isArrayExpression(exp)) return t19.tsArrayType(t19.tsAnyKeyword());
|
|
2231
|
+
if (t19.isObjectExpression(exp)) {
|
|
2167
2232
|
const members = [];
|
|
2168
2233
|
for (const p of exp.properties) {
|
|
2169
|
-
if (!
|
|
2234
|
+
if (!t19.isObjectProperty(p)) continue;
|
|
2170
2235
|
let key;
|
|
2171
|
-
if (
|
|
2172
|
-
else if (
|
|
2236
|
+
if (t19.isIdentifier(p.key)) key = p.key.name;
|
|
2237
|
+
else if (t19.isStringLiteral(p.key)) key = p.key.value;
|
|
2173
2238
|
if (!key) continue;
|
|
2174
|
-
if (
|
|
2239
|
+
if (t19.isExpression(p.value)) {
|
|
2175
2240
|
members.push(
|
|
2176
|
-
|
|
2241
|
+
t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
2177
2242
|
);
|
|
2178
2243
|
} else {
|
|
2179
2244
|
members.push(
|
|
2180
|
-
|
|
2245
|
+
t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(t19.tsAnyKeyword()))
|
|
2181
2246
|
);
|
|
2182
2247
|
}
|
|
2183
2248
|
}
|
|
2184
|
-
return
|
|
2249
|
+
return t19.tsTypeLiteral(members);
|
|
2185
2250
|
}
|
|
2186
|
-
if (
|
|
2251
|
+
if (t19.isArrowFunctionExpression(exp) || t19.isFunctionExpression(exp)) {
|
|
2187
2252
|
const params = exp.params.map((p, i) => {
|
|
2188
|
-
const id =
|
|
2189
|
-
id.typeAnnotation =
|
|
2253
|
+
const id = t19.isIdentifier(p) ? t19.identifier(p.name) : t19.identifier(`arg${i}`);
|
|
2254
|
+
id.typeAnnotation = t19.tsTypeAnnotation(t19.tsAnyKeyword());
|
|
2190
2255
|
return id;
|
|
2191
2256
|
});
|
|
2192
|
-
let returnType =
|
|
2193
|
-
if (
|
|
2257
|
+
let returnType = t19.tsAnyKeyword();
|
|
2258
|
+
if (t19.isBlockStatement(exp.body)) {
|
|
2194
2259
|
for (const stmt of exp.body.body) {
|
|
2195
|
-
if (
|
|
2196
|
-
if (
|
|
2260
|
+
if (t19.isReturnStatement(stmt) && stmt.argument) {
|
|
2261
|
+
if (t19.isExpression(stmt.argument)) {
|
|
2197
2262
|
returnType = expressionToTSType(stmt.argument);
|
|
2198
2263
|
break;
|
|
2199
2264
|
}
|
|
2200
2265
|
}
|
|
2201
2266
|
}
|
|
2202
|
-
} else if (
|
|
2267
|
+
} else if (t19.isExpression(exp.body)) {
|
|
2203
2268
|
returnType = expressionToTSType(exp.body);
|
|
2204
2269
|
}
|
|
2205
|
-
return
|
|
2270
|
+
return t19.tsFunctionType(null, params, t19.tsTypeAnnotation(returnType));
|
|
2206
2271
|
}
|
|
2207
|
-
return
|
|
2272
|
+
return t19.tsAnyKeyword();
|
|
2208
2273
|
}
|
|
2209
2274
|
function isCalleeNamed(node, name) {
|
|
2210
|
-
if (!
|
|
2275
|
+
if (!t19.isIdentifier(node.callee)) {
|
|
2211
2276
|
return false;
|
|
2212
2277
|
}
|
|
2213
2278
|
return node.callee.name === name;
|
|
2214
2279
|
}
|
|
2215
2280
|
function isSimpleLiteral(node) {
|
|
2216
2281
|
if (!node) return false;
|
|
2217
|
-
if (
|
|
2282
|
+
if (t19.isStringLiteral(node) || t19.isNumericLiteral(node) || t19.isBooleanLiteral(node) || t19.isNullLiteral(node) || t19.isRegExpLiteral(node) || t19.isBigIntLiteral(node) || t19.isDecimalLiteral(node)) {
|
|
2218
2283
|
return true;
|
|
2219
2284
|
}
|
|
2220
2285
|
return false;
|
|
@@ -2243,26 +2308,26 @@ function resolveStaticHoisting(ctx) {
|
|
|
2243
2308
|
return {};
|
|
2244
2309
|
}
|
|
2245
2310
|
return {
|
|
2246
|
-
"ImportDeclaration|ExportDeclaration"(
|
|
2247
|
-
if (
|
|
2248
|
-
scriptIR.imports.push(
|
|
2249
|
-
} else if (
|
|
2250
|
-
scriptIR.exports.push(
|
|
2311
|
+
"ImportDeclaration|ExportDeclaration"(path8) {
|
|
2312
|
+
if (t20.isImportDeclaration(path8.node)) {
|
|
2313
|
+
scriptIR.imports.push(path8.node);
|
|
2314
|
+
} else if (t20.isExportDeclaration(path8.node)) {
|
|
2315
|
+
scriptIR.exports.push(path8.node);
|
|
2251
2316
|
}
|
|
2252
|
-
|
|
2317
|
+
path8.remove();
|
|
2253
2318
|
},
|
|
2254
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration|TSEnumDeclaration|TSModuleDeclaration|TSModuleDeclaration"(
|
|
2255
|
-
if (
|
|
2256
|
-
scriptIR.tsTypes.push(
|
|
2257
|
-
|
|
2319
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration|TSEnumDeclaration|TSModuleDeclaration|TSModuleDeclaration"(path8) {
|
|
2320
|
+
if (t20.isProgram(path8.parent)) {
|
|
2321
|
+
scriptIR.tsTypes.push(path8.node);
|
|
2322
|
+
path8.remove();
|
|
2258
2323
|
}
|
|
2259
2324
|
},
|
|
2260
|
-
VariableDeclarator(
|
|
2261
|
-
const { node } =
|
|
2262
|
-
if (!isVariableDeclTopLevel(
|
|
2325
|
+
VariableDeclarator(path8) {
|
|
2326
|
+
const { node } = path8;
|
|
2327
|
+
if (!isVariableDeclTopLevel(path8) || !isSimpleLiteral(node.init) || getScriptNodeMeta(node)) {
|
|
2263
2328
|
return;
|
|
2264
2329
|
}
|
|
2265
|
-
const declarationPath =
|
|
2330
|
+
const declarationPath = path8.findParent((p) => p.isVariableDeclaration());
|
|
2266
2331
|
if (!declarationPath) return;
|
|
2267
2332
|
scriptIR.statement.global.push(declarationPath.node);
|
|
2268
2333
|
declarationPath.remove();
|
|
@@ -2276,27 +2341,27 @@ function collectLocalStatements(ctx, ast) {
|
|
|
2276
2341
|
}
|
|
2277
2342
|
|
|
2278
2343
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2279
|
-
import * as
|
|
2344
|
+
import * as t21 from "@babel/types";
|
|
2280
2345
|
function resolveDefineAsyncComponent(ctx) {
|
|
2281
2346
|
return {
|
|
2282
|
-
CallExpression(
|
|
2283
|
-
const { node } =
|
|
2347
|
+
CallExpression(path8) {
|
|
2348
|
+
const { node } = path8;
|
|
2284
2349
|
if (!isCalleeNamed(node, VUE_API_MAP.defineAsyncComponent)) {
|
|
2285
2350
|
return;
|
|
2286
2351
|
}
|
|
2287
2352
|
const [arg] = node.arguments;
|
|
2288
2353
|
checkIsUnsupported(ctx, arg);
|
|
2289
|
-
pushToGlobalScope(
|
|
2354
|
+
pushToGlobalScope(path8, ctx);
|
|
2290
2355
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.lazy);
|
|
2291
2356
|
}
|
|
2292
2357
|
};
|
|
2293
2358
|
}
|
|
2294
2359
|
function checkIsUnsupported(ctx, arg) {
|
|
2295
|
-
if (
|
|
2360
|
+
if (t21.isFunction(arg)) {
|
|
2296
2361
|
checkIsDynamicImport(ctx, arg);
|
|
2297
|
-
} else if (
|
|
2362
|
+
} else if (t21.isObjectExpression(arg)) {
|
|
2298
2363
|
const { value } = arg.properties.find(
|
|
2299
|
-
(p) =>
|
|
2364
|
+
(p) => t21.isObjectProperty(p) && t21.isIdentifier(p.key) && p.key.name === "loader"
|
|
2300
2365
|
);
|
|
2301
2366
|
checkIsDynamicImport(ctx, value);
|
|
2302
2367
|
if (arg.properties.length > 1) {
|
|
@@ -2307,7 +2372,7 @@ function checkIsUnsupported(ctx, arg) {
|
|
|
2307
2372
|
function checkIsDynamicImport(ctx, node) {
|
|
2308
2373
|
const { scriptData, filename } = ctx;
|
|
2309
2374
|
const warnIsNotImport = (target) => {
|
|
2310
|
-
if (!target || !
|
|
2375
|
+
if (!target || !t21.isImport(target)) {
|
|
2311
2376
|
logger.error(
|
|
2312
2377
|
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2313
2378
|
{
|
|
@@ -2318,20 +2383,20 @@ function checkIsDynamicImport(ctx, node) {
|
|
|
2318
2383
|
);
|
|
2319
2384
|
}
|
|
2320
2385
|
};
|
|
2321
|
-
if (
|
|
2386
|
+
if (t21.isFunction(node)) {
|
|
2322
2387
|
checkIsDynamicImport(ctx, node.body);
|
|
2323
2388
|
return;
|
|
2324
2389
|
}
|
|
2325
|
-
if (
|
|
2390
|
+
if (t21.isBlockStatement(node)) {
|
|
2326
2391
|
const [returnSmt] = node.body;
|
|
2327
|
-
if (
|
|
2392
|
+
if (t21.isReturnStatement(returnSmt)) {
|
|
2328
2393
|
warnIsNotImport(returnSmt.argument);
|
|
2329
2394
|
}
|
|
2330
2395
|
return;
|
|
2331
2396
|
}
|
|
2332
|
-
if (
|
|
2397
|
+
if (t21.isCallExpression(node)) {
|
|
2333
2398
|
warnIsNotImport(node.callee);
|
|
2334
|
-
if (
|
|
2399
|
+
if (t21.isStringLiteral(node.arguments[0])) {
|
|
2335
2400
|
replaceVueSuffix(ctx, node.arguments[0]);
|
|
2336
2401
|
}
|
|
2337
2402
|
return;
|
|
@@ -2349,15 +2414,15 @@ function warnMultipleOptionsUsed(ctx, node) {
|
|
|
2349
2414
|
}
|
|
2350
2415
|
);
|
|
2351
2416
|
}
|
|
2352
|
-
function pushToGlobalScope(
|
|
2353
|
-
const { node } =
|
|
2417
|
+
function pushToGlobalScope(path8, ctx) {
|
|
2418
|
+
const { node } = path8;
|
|
2354
2419
|
const callee = node.callee;
|
|
2355
2420
|
callee.name = REACT_API_MAP.lazy;
|
|
2356
2421
|
callee.loc.identifierName = REACT_API_MAP.lazy;
|
|
2357
2422
|
if (node.typeParameters) {
|
|
2358
2423
|
node.typeParameters = void 0;
|
|
2359
2424
|
}
|
|
2360
|
-
let declarationPath =
|
|
2425
|
+
let declarationPath = path8.parentPath;
|
|
2361
2426
|
while (declarationPath) {
|
|
2362
2427
|
if (declarationPath.isVariableDeclaration()) {
|
|
2363
2428
|
break;
|
|
@@ -2368,78 +2433,78 @@ function pushToGlobalScope(path7, ctx) {
|
|
|
2368
2433
|
if (declarationPath?.isVariableDeclaration()) {
|
|
2369
2434
|
fullNode = declarationPath.node;
|
|
2370
2435
|
declarationPath.remove();
|
|
2371
|
-
} else if (
|
|
2372
|
-
fullNode =
|
|
2373
|
-
|
|
2436
|
+
} else if (path8.parentPath.isVariableDeclarator()) {
|
|
2437
|
+
fullNode = path8.parent;
|
|
2438
|
+
path8.parentPath.remove();
|
|
2374
2439
|
} else {
|
|
2375
|
-
fullNode =
|
|
2376
|
-
|
|
2440
|
+
fullNode = path8.node;
|
|
2441
|
+
path8.remove();
|
|
2377
2442
|
}
|
|
2378
2443
|
const scriptIR = getScriptIR(ctx);
|
|
2379
2444
|
scriptIR.statement.global.push(fullNode);
|
|
2380
2445
|
}
|
|
2381
2446
|
|
|
2382
2447
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2383
|
-
import * as
|
|
2448
|
+
import * as t23 from "@babel/types";
|
|
2384
2449
|
|
|
2385
2450
|
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2386
|
-
import * as
|
|
2451
|
+
import * as t22 from "@babel/types";
|
|
2387
2452
|
function createUseCallback(body, deps) {
|
|
2388
|
-
return
|
|
2453
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useCallback), [
|
|
2389
2454
|
body,
|
|
2390
|
-
deps ??
|
|
2455
|
+
deps ?? t22.arrayExpression([])
|
|
2391
2456
|
]);
|
|
2392
2457
|
}
|
|
2393
2458
|
function createUseMemo(body, deps) {
|
|
2394
|
-
return
|
|
2395
|
-
|
|
2396
|
-
deps ??
|
|
2459
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useMemo), [
|
|
2460
|
+
t22.arrowFunctionExpression([], body),
|
|
2461
|
+
deps ?? t22.arrayExpression([])
|
|
2397
2462
|
]);
|
|
2398
2463
|
}
|
|
2399
2464
|
function createUseImperativeHandle(refId, init) {
|
|
2400
|
-
return
|
|
2465
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2401
2466
|
}
|
|
2402
2467
|
|
|
2403
2468
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2404
2469
|
function resolveDefineExpose(ctx) {
|
|
2405
2470
|
if (ctx.inputType !== "sfc") return {};
|
|
2406
2471
|
return {
|
|
2407
|
-
CallExpression(
|
|
2408
|
-
const { node } =
|
|
2472
|
+
CallExpression(path8) {
|
|
2473
|
+
const { node } = path8;
|
|
2409
2474
|
const { filename, scriptData } = ctx;
|
|
2410
2475
|
if (!isCalleeNamed(node, MACRO_API_NAMES.expose)) {
|
|
2411
2476
|
return;
|
|
2412
2477
|
}
|
|
2413
2478
|
const [expose] = node.arguments;
|
|
2414
2479
|
if (!expose) {
|
|
2415
|
-
|
|
2480
|
+
path8.remove();
|
|
2416
2481
|
return;
|
|
2417
2482
|
}
|
|
2418
2483
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2419
2484
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2420
2485
|
recordImport(ctx, adapter.package, adapter.target);
|
|
2421
|
-
if (!
|
|
2486
|
+
if (!t23.isObjectExpression(expose) && !t23.isFunction(expose)) {
|
|
2422
2487
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2423
2488
|
file: filename,
|
|
2424
2489
|
loc: expose.loc,
|
|
2425
2490
|
source: scriptData.source
|
|
2426
2491
|
});
|
|
2427
2492
|
}
|
|
2428
|
-
const init = !
|
|
2493
|
+
const init = !t23.isFunction(expose) ? t23.arrowFunctionExpression([], expose) : expose;
|
|
2429
2494
|
const { forwardRef } = scriptData;
|
|
2430
|
-
const newNode = createUseImperativeHandle(
|
|
2495
|
+
const newNode = createUseImperativeHandle(t23.identifier(forwardRef.refField), init);
|
|
2431
2496
|
forwardRef.enabled = true;
|
|
2432
|
-
|
|
2497
|
+
path8.replaceWith(newNode);
|
|
2433
2498
|
}
|
|
2434
2499
|
};
|
|
2435
2500
|
}
|
|
2436
2501
|
|
|
2437
2502
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2438
|
-
import * as
|
|
2503
|
+
import * as t24 from "@babel/types";
|
|
2439
2504
|
function resolveDefineOptions(ctx) {
|
|
2440
2505
|
return {
|
|
2441
|
-
CallExpression(
|
|
2442
|
-
const { node } =
|
|
2506
|
+
CallExpression(path8) {
|
|
2507
|
+
const { node } = path8;
|
|
2443
2508
|
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
2444
2509
|
return;
|
|
2445
2510
|
}
|
|
@@ -2451,10 +2516,10 @@ function resolveDefineOptions(ctx) {
|
|
|
2451
2516
|
file: filename,
|
|
2452
2517
|
loc: node?.loc
|
|
2453
2518
|
});
|
|
2454
|
-
|
|
2519
|
+
path8.remove();
|
|
2455
2520
|
return;
|
|
2456
2521
|
}
|
|
2457
|
-
if (!
|
|
2522
|
+
if (!t24.isObjectExpression(options)) {
|
|
2458
2523
|
logger.warn("Argument for defineOptions must be an object expression.", {
|
|
2459
2524
|
source: scriptData.source,
|
|
2460
2525
|
file: filename,
|
|
@@ -2462,13 +2527,13 @@ function resolveDefineOptions(ctx) {
|
|
|
2462
2527
|
});
|
|
2463
2528
|
} else {
|
|
2464
2529
|
for (const prop of options.properties) {
|
|
2465
|
-
if (!
|
|
2530
|
+
if (!t24.isObjectProperty(prop) || !t24.isIdentifier(prop.key)) {
|
|
2466
2531
|
continue;
|
|
2467
2532
|
}
|
|
2468
2533
|
extractName(prop, ctx);
|
|
2469
2534
|
}
|
|
2470
2535
|
}
|
|
2471
|
-
|
|
2536
|
+
path8.remove();
|
|
2472
2537
|
}
|
|
2473
2538
|
};
|
|
2474
2539
|
}
|
|
@@ -2476,7 +2541,7 @@ function extractName(prop, ctx) {
|
|
|
2476
2541
|
if (ctx.compName) return;
|
|
2477
2542
|
const { filename, scriptData } = ctx;
|
|
2478
2543
|
if (!prop.computed && prop.key.name === "name") {
|
|
2479
|
-
if (
|
|
2544
|
+
if (t24.isStringLiteral(prop.value)) {
|
|
2480
2545
|
ctx.compName = prop.value.value;
|
|
2481
2546
|
return;
|
|
2482
2547
|
}
|
|
@@ -2489,7 +2554,7 @@ function extractName(prop, ctx) {
|
|
|
2489
2554
|
}
|
|
2490
2555
|
|
|
2491
2556
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2492
|
-
import * as
|
|
2557
|
+
import * as t25 from "@babel/types";
|
|
2493
2558
|
function resolveEmitCalls(ctx) {
|
|
2494
2559
|
const formatEmitEventName = (raw) => {
|
|
2495
2560
|
if (raw.startsWith("update:")) {
|
|
@@ -2500,10 +2565,10 @@ function resolveEmitCalls(ctx) {
|
|
|
2500
2565
|
return `on${capitalize(camelCase(normalized))}`;
|
|
2501
2566
|
};
|
|
2502
2567
|
return {
|
|
2503
|
-
CallExpression(
|
|
2504
|
-
const { node } =
|
|
2568
|
+
CallExpression(path8) {
|
|
2569
|
+
const { node } = path8;
|
|
2505
2570
|
const { filename, templateData, scriptData } = ctx;
|
|
2506
|
-
if (!
|
|
2571
|
+
if (!t25.isIdentifier(node.callee)) return;
|
|
2507
2572
|
const { name } = node.callee;
|
|
2508
2573
|
const checkIfFromDefineEmits = () => {
|
|
2509
2574
|
let result = false;
|
|
@@ -2512,10 +2577,10 @@ function resolveEmitCalls(ctx) {
|
|
|
2512
2577
|
result = meta.source === MACRO_API_NAMES.emits;
|
|
2513
2578
|
}
|
|
2514
2579
|
if (!result) {
|
|
2515
|
-
const binding =
|
|
2580
|
+
const binding = path8.scope.getBinding(name);
|
|
2516
2581
|
if (binding) {
|
|
2517
2582
|
const parent = binding.path.node;
|
|
2518
|
-
if (
|
|
2583
|
+
if (t25.isVariableDeclarator(parent) && t25.isCallExpression(parent.init) && t25.isIdentifier(parent.init.callee)) {
|
|
2519
2584
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2520
2585
|
}
|
|
2521
2586
|
}
|
|
@@ -2524,55 +2589,56 @@ function resolveEmitCalls(ctx) {
|
|
|
2524
2589
|
};
|
|
2525
2590
|
if (!checkIfFromDefineEmits()) return;
|
|
2526
2591
|
const [callee, ...args] = node.arguments;
|
|
2527
|
-
const eventName =
|
|
2592
|
+
const eventName = t25.isStringLiteral(callee) ? formatEmitEventName(callee.value) : void 0;
|
|
2528
2593
|
if (!eventName) {
|
|
2529
2594
|
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2530
2595
|
file: filename,
|
|
2531
2596
|
source: scriptData.source,
|
|
2532
2597
|
loc: callee?.loc
|
|
2533
2598
|
});
|
|
2534
|
-
|
|
2599
|
+
path8.remove();
|
|
2535
2600
|
return;
|
|
2536
2601
|
}
|
|
2537
|
-
const propCall =
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2602
|
+
const propCall = t25.optionalCallExpression(
|
|
2603
|
+
t25.optionalMemberExpression(
|
|
2604
|
+
t25.identifier(ctx.propField),
|
|
2605
|
+
t25.identifier(eventName),
|
|
2541
2606
|
false,
|
|
2542
2607
|
true
|
|
2543
2608
|
),
|
|
2544
|
-
args
|
|
2609
|
+
args,
|
|
2610
|
+
true
|
|
2545
2611
|
);
|
|
2546
|
-
|
|
2612
|
+
path8.replaceWith(propCall);
|
|
2547
2613
|
}
|
|
2548
2614
|
};
|
|
2549
2615
|
}
|
|
2550
2616
|
|
|
2551
2617
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2552
|
-
import * as
|
|
2618
|
+
import * as t30 from "@babel/types";
|
|
2553
2619
|
|
|
2554
2620
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2555
|
-
import * as
|
|
2621
|
+
import * as t27 from "@babel/types";
|
|
2556
2622
|
|
|
2557
2623
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2558
|
-
import * as
|
|
2624
|
+
import * as t26 from "@babel/types";
|
|
2559
2625
|
function cloneCallableParams(params) {
|
|
2560
2626
|
const cloneCallableParam = (param, index) => {
|
|
2561
|
-
if (
|
|
2627
|
+
if (t26.isRestElement(param)) {
|
|
2562
2628
|
const arg = param.argument;
|
|
2563
|
-
const name =
|
|
2564
|
-
const rest =
|
|
2565
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
2629
|
+
const name = t26.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
2630
|
+
const rest = t26.restElement(t26.identifier(name));
|
|
2631
|
+
rest.typeAnnotation = param.typeAnnotation || (t26.isIdentifier(arg) ? arg.typeAnnotation : null) || t26.tsTypeAnnotation(t26.tsArrayType(t26.tsAnyKeyword()));
|
|
2566
2632
|
return rest;
|
|
2567
2633
|
}
|
|
2568
|
-
if (
|
|
2569
|
-
const id =
|
|
2634
|
+
if (t26.isIdentifier(param)) {
|
|
2635
|
+
const id = t26.identifier(param.name || `arg${index}`);
|
|
2570
2636
|
id.optional = param.optional;
|
|
2571
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
2637
|
+
id.typeAnnotation = param.typeAnnotation || t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2572
2638
|
return id;
|
|
2573
2639
|
}
|
|
2574
|
-
const fallback =
|
|
2575
|
-
fallback.typeAnnotation =
|
|
2640
|
+
const fallback = t26.identifier(`arg${index}`);
|
|
2641
|
+
fallback.typeAnnotation = t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2576
2642
|
return fallback;
|
|
2577
2643
|
};
|
|
2578
2644
|
return params.map(cloneCallableParam);
|
|
@@ -2581,19 +2647,19 @@ function cloneCallableParams(params) {
|
|
|
2581
2647
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2582
2648
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2583
2649
|
return {
|
|
2584
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(
|
|
2585
|
-
if (!
|
|
2586
|
-
const { node } =
|
|
2587
|
-
if (
|
|
2588
|
-
const typeLiteral =
|
|
2650
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
2651
|
+
if (!t27.isProgram(path8.parent)) return;
|
|
2652
|
+
const { node } = path8;
|
|
2653
|
+
if (t27.isTSInterfaceDeclaration(node)) {
|
|
2654
|
+
const typeLiteral = t27.tsTypeLiteral(node.body.body);
|
|
2589
2655
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2590
2656
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2591
|
-
if (resolved &&
|
|
2657
|
+
if (resolved && t27.isTSTypeLiteral(resolved)) {
|
|
2592
2658
|
node.body.body = resolved.members;
|
|
2593
2659
|
}
|
|
2594
2660
|
return;
|
|
2595
2661
|
}
|
|
2596
|
-
if (
|
|
2662
|
+
if (t27.isTSTypeAliasDeclaration(node)) {
|
|
2597
2663
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2598
2664
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2599
2665
|
if (resolved) {
|
|
@@ -2604,52 +2670,52 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2604
2670
|
};
|
|
2605
2671
|
}
|
|
2606
2672
|
function resolveTopLevelEmitType(tsType) {
|
|
2607
|
-
if (
|
|
2673
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2608
2674
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2609
2675
|
}
|
|
2610
|
-
if (
|
|
2676
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2611
2677
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2612
2678
|
return tsType;
|
|
2613
2679
|
}
|
|
2614
2680
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2615
|
-
return
|
|
2681
|
+
return t27.tsTypeReference(
|
|
2616
2682
|
tsType.typeName,
|
|
2617
|
-
|
|
2683
|
+
t27.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2618
2684
|
);
|
|
2619
2685
|
}
|
|
2620
|
-
if (
|
|
2686
|
+
if (t27.isTSIntersectionType(tsType)) {
|
|
2621
2687
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2622
2688
|
if (!types.length) return null;
|
|
2623
2689
|
if (types.length === 1) return types[0];
|
|
2624
|
-
return
|
|
2690
|
+
return t27.tsIntersectionType(types);
|
|
2625
2691
|
}
|
|
2626
|
-
if (
|
|
2692
|
+
if (t27.isTSUnionType(tsType)) {
|
|
2627
2693
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2628
2694
|
if (!types.length) return null;
|
|
2629
2695
|
if (types.length === 1) return types[0];
|
|
2630
|
-
return
|
|
2696
|
+
return t27.tsUnionType(types);
|
|
2631
2697
|
}
|
|
2632
|
-
if (
|
|
2698
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2633
2699
|
const members = [];
|
|
2634
2700
|
for (const member of tsType.members) {
|
|
2635
|
-
if (
|
|
2701
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2636
2702
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2637
2703
|
continue;
|
|
2638
2704
|
}
|
|
2639
2705
|
members.push(member);
|
|
2640
2706
|
}
|
|
2641
2707
|
if (!members.length) return null;
|
|
2642
|
-
return
|
|
2708
|
+
return t27.tsTypeLiteral(members);
|
|
2643
2709
|
}
|
|
2644
|
-
if (
|
|
2710
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2645
2711
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2646
2712
|
if (!props.length) return null;
|
|
2647
|
-
return
|
|
2713
|
+
return t27.tsTypeLiteral(props);
|
|
2648
2714
|
}
|
|
2649
2715
|
return tsType;
|
|
2650
2716
|
}
|
|
2651
|
-
function resolveDefineEmitsIface(
|
|
2652
|
-
const { node } =
|
|
2717
|
+
function resolveDefineEmitsIface(path8, ctx) {
|
|
2718
|
+
const { node } = path8;
|
|
2653
2719
|
const [runtimeArg] = node.arguments;
|
|
2654
2720
|
const tsParams = node.typeParameters?.params;
|
|
2655
2721
|
if (tsParams?.length) {
|
|
@@ -2665,41 +2731,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2665
2731
|
propsTSIface: { emitTypes }
|
|
2666
2732
|
} = ctx.scriptData;
|
|
2667
2733
|
const members = [];
|
|
2668
|
-
if (
|
|
2734
|
+
if (t27.isArrayExpression(runtimeArg)) {
|
|
2669
2735
|
for (const element of runtimeArg.elements) {
|
|
2670
|
-
if (!element || !
|
|
2736
|
+
if (!element || !t27.isStringLiteral(element)) continue;
|
|
2671
2737
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2672
2738
|
const key = buildKey(handlerName);
|
|
2673
|
-
const fnType =
|
|
2739
|
+
const fnType = t27.tsFunctionType(
|
|
2674
2740
|
null,
|
|
2675
2741
|
[createRestAnyParam("args")],
|
|
2676
|
-
|
|
2742
|
+
t27.tsTypeAnnotation(t27.tsAnyKeyword())
|
|
2677
2743
|
);
|
|
2678
|
-
const prop =
|
|
2744
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2679
2745
|
prop.optional = true;
|
|
2680
2746
|
members.push(prop);
|
|
2681
2747
|
}
|
|
2682
2748
|
if (members.length) {
|
|
2683
|
-
emitTypes.push(
|
|
2749
|
+
emitTypes.push(t27.tsTypeLiteral(members));
|
|
2684
2750
|
}
|
|
2685
2751
|
return;
|
|
2686
2752
|
}
|
|
2687
|
-
if (
|
|
2753
|
+
if (t27.isObjectExpression(runtimeArg)) {
|
|
2688
2754
|
for (const prop of runtimeArg.properties) {
|
|
2689
|
-
if (!
|
|
2690
|
-
if (
|
|
2755
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
2756
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2691
2757
|
const rawName = resolvePropName(prop.key);
|
|
2692
2758
|
if (!rawName) continue;
|
|
2693
2759
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2694
2760
|
const key = buildKey(handlerName);
|
|
2695
|
-
const params =
|
|
2696
|
-
const fnType =
|
|
2697
|
-
const propSig =
|
|
2761
|
+
const params = t27.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
2762
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2763
|
+
const propSig = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2698
2764
|
propSig.optional = true;
|
|
2699
2765
|
members.push(propSig);
|
|
2700
2766
|
}
|
|
2701
2767
|
if (members.length) {
|
|
2702
|
-
emitTypes.push(
|
|
2768
|
+
emitTypes.push(t27.tsTypeLiteral(members));
|
|
2703
2769
|
}
|
|
2704
2770
|
}
|
|
2705
2771
|
}
|
|
@@ -2720,129 +2786,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2720
2786
|
return `on${name}`;
|
|
2721
2787
|
}
|
|
2722
2788
|
function resolvePropName(key) {
|
|
2723
|
-
if (
|
|
2724
|
-
if (
|
|
2725
|
-
if (
|
|
2789
|
+
if (t27.isIdentifier(key)) return key.name;
|
|
2790
|
+
if (t27.isStringLiteral(key)) return key.value;
|
|
2791
|
+
if (t27.isNumericLiteral(key)) return String(key.value);
|
|
2726
2792
|
return null;
|
|
2727
2793
|
}
|
|
2728
2794
|
function buildKey(name) {
|
|
2729
|
-
return
|
|
2795
|
+
return t27.isValidIdentifier(name) ? t27.identifier(name) : t27.stringLiteral(name);
|
|
2730
2796
|
}
|
|
2731
2797
|
function createRestAnyParam(name) {
|
|
2732
|
-
const id =
|
|
2733
|
-
const rest =
|
|
2734
|
-
rest.typeAnnotation =
|
|
2798
|
+
const id = t27.identifier(name);
|
|
2799
|
+
const rest = t27.restElement(id);
|
|
2800
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(t27.tsArrayType(t27.tsAnyKeyword()));
|
|
2735
2801
|
return rest;
|
|
2736
2802
|
}
|
|
2737
2803
|
function resolveRuntimeTupleParams(value) {
|
|
2738
2804
|
const params = [];
|
|
2739
2805
|
value.elements.forEach((element, index) => {
|
|
2740
2806
|
if (!element) return;
|
|
2741
|
-
if (
|
|
2807
|
+
if (t27.isSpreadElement(element)) {
|
|
2742
2808
|
params.push(createRestAnyParam(`args${index}`));
|
|
2743
2809
|
return;
|
|
2744
2810
|
}
|
|
2745
|
-
if (
|
|
2746
|
-
const id =
|
|
2747
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
2811
|
+
if (t27.isIdentifier(element)) {
|
|
2812
|
+
const id = t27.identifier(element.name);
|
|
2813
|
+
id.typeAnnotation = element.typeAnnotation || t27.tsTypeAnnotation(t27.tsAnyKeyword());
|
|
2748
2814
|
params.push(id);
|
|
2749
2815
|
return;
|
|
2750
2816
|
}
|
|
2751
|
-
if (
|
|
2752
|
-
const id =
|
|
2753
|
-
id.typeAnnotation =
|
|
2817
|
+
if (t27.isTSAsExpression(element)) {
|
|
2818
|
+
const id = t27.identifier(`arg${index}`);
|
|
2819
|
+
id.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2754
2820
|
params.push(id);
|
|
2755
2821
|
return;
|
|
2756
2822
|
}
|
|
2757
|
-
const fallback =
|
|
2758
|
-
fallback.typeAnnotation =
|
|
2823
|
+
const fallback = t27.identifier(`arg${index}`);
|
|
2824
|
+
fallback.typeAnnotation = t27.tsTypeAnnotation(t27.tsAnyKeyword());
|
|
2759
2825
|
params.push(fallback);
|
|
2760
2826
|
});
|
|
2761
2827
|
return params;
|
|
2762
2828
|
}
|
|
2763
2829
|
function resolveExplicitEmitType(tsType) {
|
|
2764
|
-
if (
|
|
2830
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2765
2831
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2766
2832
|
}
|
|
2767
|
-
if (
|
|
2833
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2768
2834
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2769
2835
|
return tsType;
|
|
2770
2836
|
}
|
|
2771
2837
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2772
|
-
return
|
|
2838
|
+
return t27.tsTypeReference(
|
|
2773
2839
|
tsType.typeName,
|
|
2774
|
-
|
|
2840
|
+
t27.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2775
2841
|
);
|
|
2776
2842
|
}
|
|
2777
|
-
if (
|
|
2843
|
+
if (t27.isTSIntersectionType(tsType)) {
|
|
2778
2844
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2779
2845
|
if (!types.length) return null;
|
|
2780
2846
|
if (types.length === 1) return types[0];
|
|
2781
|
-
return
|
|
2847
|
+
return t27.tsIntersectionType(types);
|
|
2782
2848
|
}
|
|
2783
|
-
if (
|
|
2849
|
+
if (t27.isTSUnionType(tsType)) {
|
|
2784
2850
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2785
2851
|
if (!types.length) return null;
|
|
2786
2852
|
if (types.length === 1) return types[0];
|
|
2787
|
-
return
|
|
2853
|
+
return t27.tsUnionType(types);
|
|
2788
2854
|
}
|
|
2789
|
-
if (
|
|
2855
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2790
2856
|
const members = [];
|
|
2791
2857
|
for (const member of tsType.members) {
|
|
2792
|
-
if (
|
|
2858
|
+
if (t27.isTSPropertySignature(member)) {
|
|
2793
2859
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2794
2860
|
if (prop) members.push(prop);
|
|
2795
2861
|
continue;
|
|
2796
2862
|
}
|
|
2797
|
-
if (
|
|
2863
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2798
2864
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2799
2865
|
continue;
|
|
2800
2866
|
}
|
|
2801
2867
|
}
|
|
2802
2868
|
if (!members.length) return null;
|
|
2803
|
-
return
|
|
2869
|
+
return t27.tsTypeLiteral(members);
|
|
2804
2870
|
}
|
|
2805
|
-
if (
|
|
2871
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2806
2872
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2807
2873
|
if (!props.length) return null;
|
|
2808
|
-
return
|
|
2874
|
+
return t27.tsTypeLiteral(props);
|
|
2809
2875
|
}
|
|
2810
2876
|
return tsType;
|
|
2811
2877
|
}
|
|
2812
2878
|
function hasEmitsSignatureInType(tsType) {
|
|
2813
|
-
if (
|
|
2879
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2814
2880
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
2815
2881
|
}
|
|
2816
|
-
if (
|
|
2882
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2817
2883
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2818
2884
|
return false;
|
|
2819
2885
|
}
|
|
2820
2886
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
2821
2887
|
}
|
|
2822
|
-
if (
|
|
2888
|
+
if (t27.isTSIntersectionType(tsType) || t27.isTSUnionType(tsType)) {
|
|
2823
2889
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
2824
2890
|
}
|
|
2825
|
-
if (
|
|
2891
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2826
2892
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
2827
2893
|
}
|
|
2828
|
-
if (
|
|
2894
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2829
2895
|
return isEmitsCallable(tsType.parameters);
|
|
2830
2896
|
}
|
|
2831
2897
|
return false;
|
|
2832
2898
|
}
|
|
2833
2899
|
function hasEmitsSignatureInMember(member) {
|
|
2834
|
-
if (
|
|
2900
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2835
2901
|
return isEmitsCallable(member.parameters);
|
|
2836
2902
|
}
|
|
2837
2903
|
return false;
|
|
2838
2904
|
}
|
|
2839
2905
|
function isEmitsCallable(parameters) {
|
|
2840
2906
|
const [eventParam] = parameters;
|
|
2841
|
-
if (!eventParam || !
|
|
2907
|
+
if (!eventParam || !t27.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2842
2908
|
return false;
|
|
2843
2909
|
}
|
|
2844
2910
|
const { typeAnnotation } = eventParam;
|
|
2845
|
-
if (
|
|
2911
|
+
if (t27.isNoop(typeAnnotation)) return false;
|
|
2846
2912
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
2847
2913
|
}
|
|
2848
2914
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -2852,19 +2918,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
2852
2918
|
const key = buildKey(handlerName);
|
|
2853
2919
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2854
2920
|
let params = [];
|
|
2855
|
-
let returnType =
|
|
2856
|
-
if (typeAnnotation &&
|
|
2921
|
+
let returnType = t27.tsAnyKeyword();
|
|
2922
|
+
if (typeAnnotation && t27.isTSFunctionType(typeAnnotation)) {
|
|
2857
2923
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
2858
2924
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
2859
|
-
} else if (typeAnnotation &&
|
|
2925
|
+
} else if (typeAnnotation && t27.isTSTupleType(typeAnnotation)) {
|
|
2860
2926
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
2861
2927
|
} else if (typeAnnotation) {
|
|
2862
|
-
const id =
|
|
2863
|
-
id.typeAnnotation =
|
|
2928
|
+
const id = t27.identifier("value");
|
|
2929
|
+
id.typeAnnotation = t27.tsTypeAnnotation(typeAnnotation);
|
|
2864
2930
|
params = [id];
|
|
2865
2931
|
}
|
|
2866
|
-
const fnType =
|
|
2867
|
-
const prop =
|
|
2932
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(returnType));
|
|
2933
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2868
2934
|
prop.optional = !!member.optional;
|
|
2869
2935
|
return prop;
|
|
2870
2936
|
}
|
|
@@ -2873,32 +2939,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
2873
2939
|
}
|
|
2874
2940
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
2875
2941
|
const [eventParam, ...restParams] = parameters;
|
|
2876
|
-
if (!eventParam || !
|
|
2942
|
+
if (!eventParam || !t27.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2877
2943
|
return [];
|
|
2878
2944
|
}
|
|
2879
2945
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
2880
|
-
if (
|
|
2946
|
+
if (t27.isNoop(paramTypeAnnotation)) return [];
|
|
2881
2947
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
2882
2948
|
if (!eventNames.length) return [];
|
|
2883
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
2949
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t27.tsAnyKeyword();
|
|
2884
2950
|
return eventNames.map((eventName) => {
|
|
2885
2951
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
2886
2952
|
const key = buildKey(handlerName);
|
|
2887
2953
|
const params = cloneCallableParams(restParams);
|
|
2888
|
-
const fnType =
|
|
2889
|
-
const prop =
|
|
2954
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(returnType));
|
|
2955
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2890
2956
|
prop.optional = true;
|
|
2891
2957
|
return prop;
|
|
2892
2958
|
});
|
|
2893
2959
|
}
|
|
2894
2960
|
function resolveEventNames(type) {
|
|
2895
|
-
if (
|
|
2961
|
+
if (t27.isTSLiteralType(type) && t27.isStringLiteral(type.literal)) {
|
|
2896
2962
|
return [type.literal.value];
|
|
2897
2963
|
}
|
|
2898
|
-
if (
|
|
2964
|
+
if (t27.isTSUnionType(type)) {
|
|
2899
2965
|
return type.types.flatMap(resolveEventNames);
|
|
2900
2966
|
}
|
|
2901
|
-
if (
|
|
2967
|
+
if (t27.isTSParenthesizedType(type)) {
|
|
2902
2968
|
return resolveEventNames(type.typeAnnotation);
|
|
2903
2969
|
}
|
|
2904
2970
|
return [];
|
|
@@ -2911,46 +2977,46 @@ function resolveTupleTypeParams(tuple) {
|
|
|
2911
2977
|
return params;
|
|
2912
2978
|
}
|
|
2913
2979
|
function resolveTupleElementParam(element, index) {
|
|
2914
|
-
const isNamedTuple = typeof
|
|
2980
|
+
const isNamedTuple = typeof t27.isTSNamedTupleMember === "function" && t27.isTSNamedTupleMember(element);
|
|
2915
2981
|
if (isNamedTuple) {
|
|
2916
2982
|
const tupleMember = element;
|
|
2917
2983
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
2918
2984
|
let innerType = tupleMember.elementType;
|
|
2919
2985
|
let optional = tupleMember.optional;
|
|
2920
|
-
if (
|
|
2986
|
+
if (t27.isTSOptionalType(innerType)) {
|
|
2921
2987
|
optional = true;
|
|
2922
2988
|
innerType = innerType.typeAnnotation;
|
|
2923
2989
|
}
|
|
2924
|
-
if (
|
|
2925
|
-
const rest =
|
|
2926
|
-
rest.typeAnnotation =
|
|
2990
|
+
if (t27.isTSRestType(innerType)) {
|
|
2991
|
+
const rest = t27.restElement(t27.identifier(name));
|
|
2992
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(innerType.typeAnnotation);
|
|
2927
2993
|
return rest;
|
|
2928
2994
|
}
|
|
2929
|
-
const id2 =
|
|
2995
|
+
const id2 = t27.identifier(name);
|
|
2930
2996
|
id2.optional = optional;
|
|
2931
|
-
id2.typeAnnotation =
|
|
2997
|
+
id2.typeAnnotation = t27.tsTypeAnnotation(innerType);
|
|
2932
2998
|
return id2;
|
|
2933
2999
|
}
|
|
2934
|
-
if (
|
|
2935
|
-
const rest =
|
|
2936
|
-
rest.typeAnnotation =
|
|
3000
|
+
if (t27.isTSRestType(element)) {
|
|
3001
|
+
const rest = t27.restElement(t27.identifier(`args${index}`));
|
|
3002
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2937
3003
|
return rest;
|
|
2938
3004
|
}
|
|
2939
|
-
if (
|
|
2940
|
-
const id2 =
|
|
3005
|
+
if (t27.isTSOptionalType(element)) {
|
|
3006
|
+
const id2 = t27.identifier(`arg${index}`);
|
|
2941
3007
|
id2.optional = true;
|
|
2942
|
-
id2.typeAnnotation =
|
|
3008
|
+
id2.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2943
3009
|
return id2;
|
|
2944
3010
|
}
|
|
2945
|
-
const id =
|
|
2946
|
-
id.typeAnnotation =
|
|
3011
|
+
const id = t27.identifier(`arg${index}`);
|
|
3012
|
+
id.typeAnnotation = t27.tsTypeAnnotation(element);
|
|
2947
3013
|
return id;
|
|
2948
3014
|
}
|
|
2949
3015
|
|
|
2950
3016
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
2951
|
-
import * as
|
|
2952
|
-
function resolveDefinePropsIface(
|
|
2953
|
-
const { node } =
|
|
3017
|
+
import * as t28 from "@babel/types";
|
|
3018
|
+
function resolveDefinePropsIface(path8, ctx) {
|
|
3019
|
+
const { node } = path8;
|
|
2954
3020
|
const [runtimeArg] = node.arguments;
|
|
2955
3021
|
const tsParams = node.typeParameters?.params;
|
|
2956
3022
|
if (tsParams?.length) {
|
|
@@ -2968,38 +3034,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2968
3034
|
} = scriptData;
|
|
2969
3035
|
if (!runtimeArg) return;
|
|
2970
3036
|
const members = [];
|
|
2971
|
-
if (
|
|
3037
|
+
if (t28.isArrayExpression(runtimeArg)) {
|
|
2972
3038
|
for (const element of runtimeArg.elements) {
|
|
2973
|
-
if (!element || !
|
|
2974
|
-
const key =
|
|
2975
|
-
const prop =
|
|
3039
|
+
if (!element || !t28.isStringLiteral(element)) continue;
|
|
3040
|
+
const key = t28.isValidIdentifier(element.value) ? t28.identifier(element.value) : t28.stringLiteral(element.value);
|
|
3041
|
+
const prop = t28.tsPropertySignature(key, t28.tsTypeAnnotation(t28.tsAnyKeyword()));
|
|
2976
3042
|
prop.optional = true;
|
|
2977
3043
|
members.push(prop);
|
|
2978
3044
|
}
|
|
2979
3045
|
if (members.length) {
|
|
2980
|
-
propsTypes.push(
|
|
3046
|
+
propsTypes.push(t28.tsTypeLiteral(members));
|
|
2981
3047
|
}
|
|
2982
3048
|
return;
|
|
2983
3049
|
}
|
|
2984
|
-
if (
|
|
3050
|
+
if (t28.isObjectExpression(runtimeArg)) {
|
|
2985
3051
|
for (const prop of runtimeArg.properties) {
|
|
2986
|
-
if (!
|
|
2987
|
-
if (
|
|
3052
|
+
if (!t28.isObjectProperty(prop)) continue;
|
|
3053
|
+
if (t28.isSpreadElement(prop)) continue;
|
|
2988
3054
|
const key = prop.key;
|
|
2989
3055
|
let propName = null;
|
|
2990
|
-
if (
|
|
2991
|
-
if (
|
|
2992
|
-
if (
|
|
3056
|
+
if (t28.isIdentifier(key)) propName = key.name;
|
|
3057
|
+
if (t28.isStringLiteral(key)) propName = key.value;
|
|
3058
|
+
if (t28.isNumericLiteral(key)) propName = String(key.value);
|
|
2993
3059
|
if (!propName) continue;
|
|
2994
3060
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
2995
|
-
const tsType = type ??
|
|
2996
|
-
const tsKey =
|
|
2997
|
-
const tsProp =
|
|
3061
|
+
const tsType = type ?? t28.tsAnyKeyword();
|
|
3062
|
+
const tsKey = t28.isValidIdentifier(propName) ? t28.identifier(propName) : t28.stringLiteral(propName);
|
|
3063
|
+
const tsProp = t28.tsPropertySignature(tsKey, t28.tsTypeAnnotation(tsType));
|
|
2998
3064
|
tsProp.optional = !required;
|
|
2999
3065
|
members.push(tsProp);
|
|
3000
3066
|
}
|
|
3001
3067
|
if (members.length) {
|
|
3002
|
-
propsTypes.push(
|
|
3068
|
+
propsTypes.push(t28.tsTypeLiteral(members));
|
|
3003
3069
|
}
|
|
3004
3070
|
return;
|
|
3005
3071
|
}
|
|
@@ -3013,42 +3079,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
3013
3079
|
);
|
|
3014
3080
|
}
|
|
3015
3081
|
function resolveRuntimePropMeta(value) {
|
|
3016
|
-
if (
|
|
3082
|
+
if (t28.isIdentifier(value)) {
|
|
3017
3083
|
return {
|
|
3018
3084
|
type: mapRuntimeTypeToTSType(value),
|
|
3019
3085
|
required: false
|
|
3020
3086
|
};
|
|
3021
3087
|
}
|
|
3022
|
-
if (
|
|
3088
|
+
if (t28.isArrayExpression(value)) {
|
|
3023
3089
|
return {
|
|
3024
3090
|
type: resolveRuntimeUnionType(value),
|
|
3025
3091
|
required: false
|
|
3026
3092
|
};
|
|
3027
3093
|
}
|
|
3028
|
-
if (!
|
|
3094
|
+
if (!t28.isObjectExpression(value)) {
|
|
3029
3095
|
return { required: false };
|
|
3030
3096
|
}
|
|
3031
3097
|
let type;
|
|
3032
3098
|
let required = false;
|
|
3033
3099
|
for (const prop of value.properties) {
|
|
3034
|
-
if (!
|
|
3035
|
-
if (
|
|
3100
|
+
if (!t28.isObjectProperty(prop)) continue;
|
|
3101
|
+
if (t28.isSpreadElement(prop)) continue;
|
|
3036
3102
|
const key = prop.key;
|
|
3037
|
-
const propName =
|
|
3103
|
+
const propName = t28.isIdentifier(key) ? key.name : t28.isStringLiteral(key) ? key.value : null;
|
|
3038
3104
|
if (!propName) continue;
|
|
3039
3105
|
if (propName === "type") {
|
|
3040
3106
|
const valueNode = prop.value;
|
|
3041
|
-
if (
|
|
3107
|
+
if (t28.isArrayExpression(valueNode)) {
|
|
3042
3108
|
type = resolveRuntimeUnionType(valueNode);
|
|
3043
3109
|
continue;
|
|
3044
3110
|
}
|
|
3045
|
-
if (
|
|
3111
|
+
if (t28.isIdentifier(valueNode)) {
|
|
3046
3112
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
3047
3113
|
continue;
|
|
3048
3114
|
}
|
|
3049
3115
|
}
|
|
3050
3116
|
if (propName === "required") {
|
|
3051
|
-
if (
|
|
3117
|
+
if (t28.isBooleanLiteral(prop.value)) {
|
|
3052
3118
|
required = prop.value.value;
|
|
3053
3119
|
}
|
|
3054
3120
|
}
|
|
@@ -3058,58 +3124,58 @@ function resolveRuntimePropMeta(value) {
|
|
|
3058
3124
|
function resolveRuntimeUnionType(value) {
|
|
3059
3125
|
const types = [];
|
|
3060
3126
|
for (const element of value.elements) {
|
|
3061
|
-
if (!element || !
|
|
3127
|
+
if (!element || !t28.isIdentifier(element)) continue;
|
|
3062
3128
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
3063
3129
|
if (resolved) types.push(resolved);
|
|
3064
3130
|
}
|
|
3065
|
-
if (!types.length) return
|
|
3131
|
+
if (!types.length) return t28.tsAnyKeyword();
|
|
3066
3132
|
if (types.length === 1) return types[0];
|
|
3067
|
-
return
|
|
3133
|
+
return t28.tsUnionType(types);
|
|
3068
3134
|
}
|
|
3069
3135
|
function mapRuntimeTypeToTSType(value) {
|
|
3070
3136
|
switch (value.name) {
|
|
3071
3137
|
case "String":
|
|
3072
|
-
return
|
|
3138
|
+
return t28.tsStringKeyword();
|
|
3073
3139
|
case "Number":
|
|
3074
|
-
return
|
|
3140
|
+
return t28.tsNumberKeyword();
|
|
3075
3141
|
case "Boolean":
|
|
3076
|
-
return
|
|
3142
|
+
return t28.tsBooleanKeyword();
|
|
3077
3143
|
case "Object":
|
|
3078
|
-
return
|
|
3144
|
+
return t28.tsTypeLiteral([]);
|
|
3079
3145
|
case "Array":
|
|
3080
|
-
return
|
|
3146
|
+
return t28.tsArrayType(t28.tsAnyKeyword());
|
|
3081
3147
|
case "Function":
|
|
3082
|
-
return
|
|
3148
|
+
return t28.tsFunctionType(null, [], t28.tsTypeAnnotation(t28.tsAnyKeyword()));
|
|
3083
3149
|
case "Symbol":
|
|
3084
|
-
return
|
|
3150
|
+
return t28.tsSymbolKeyword();
|
|
3085
3151
|
case "BigInt":
|
|
3086
|
-
return
|
|
3152
|
+
return t28.tsBigIntKeyword();
|
|
3087
3153
|
default:
|
|
3088
|
-
return
|
|
3154
|
+
return t28.tsAnyKeyword();
|
|
3089
3155
|
}
|
|
3090
3156
|
}
|
|
3091
3157
|
|
|
3092
3158
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
3093
|
-
import * as
|
|
3159
|
+
import * as t29 from "@babel/types";
|
|
3094
3160
|
var SLOT_DEFAULT_NAME = "default";
|
|
3095
3161
|
var SLOT_CHILDREN_NAME = "children";
|
|
3096
3162
|
var SLOT_FN_PARAM_NAME = "props";
|
|
3097
3163
|
function resolveSlotsTopLevelTypes(ctx) {
|
|
3098
3164
|
return {
|
|
3099
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(
|
|
3100
|
-
if (!
|
|
3101
|
-
const { node } =
|
|
3102
|
-
if (
|
|
3103
|
-
const typeLiteral =
|
|
3165
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
3166
|
+
if (!t29.isProgram(path8.parent)) return;
|
|
3167
|
+
const { node } = path8;
|
|
3168
|
+
if (t29.isTSInterfaceDeclaration(node)) {
|
|
3169
|
+
const typeLiteral = t29.tsTypeLiteral(node.body.body);
|
|
3104
3170
|
if (!hasSlotsSignatureInType(typeLiteral)) return;
|
|
3105
3171
|
const resolved = resolveSlotType(typeLiteral);
|
|
3106
|
-
if (resolved &&
|
|
3172
|
+
if (resolved && t29.isTSTypeLiteral(resolved)) {
|
|
3107
3173
|
node.body.body = resolved.members;
|
|
3108
3174
|
recordReactNode(ctx);
|
|
3109
3175
|
}
|
|
3110
3176
|
return;
|
|
3111
3177
|
}
|
|
3112
|
-
if (
|
|
3178
|
+
if (t29.isTSTypeAliasDeclaration(node)) {
|
|
3113
3179
|
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
3114
3180
|
const resolved = resolveSlotType(node.typeAnnotation);
|
|
3115
3181
|
if (resolved) {
|
|
@@ -3120,8 +3186,8 @@ function resolveSlotsTopLevelTypes(ctx) {
|
|
|
3120
3186
|
}
|
|
3121
3187
|
};
|
|
3122
3188
|
}
|
|
3123
|
-
function resolveDefineSlotsIface(
|
|
3124
|
-
const { node } =
|
|
3189
|
+
function resolveDefineSlotsIface(path8, ctx) {
|
|
3190
|
+
const { node } = path8;
|
|
3125
3191
|
const tsParams = node.typeParameters?.params;
|
|
3126
3192
|
if (!tsParams?.length) return;
|
|
3127
3193
|
const {
|
|
@@ -3154,7 +3220,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
3154
3220
|
}
|
|
3155
3221
|
if (tsMembers.length) {
|
|
3156
3222
|
recordReactNode(ctx);
|
|
3157
|
-
slotTypes.push(
|
|
3223
|
+
slotTypes.push(t29.tsTypeLiteral(tsMembers));
|
|
3158
3224
|
}
|
|
3159
3225
|
}
|
|
3160
3226
|
function recordReactNode(ctx) {
|
|
@@ -3164,32 +3230,32 @@ function recordReactNode(ctx) {
|
|
|
3164
3230
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
3165
3231
|
}
|
|
3166
3232
|
function resolveSlotType(tsType) {
|
|
3167
|
-
if (
|
|
3233
|
+
if (t29.isTSParenthesizedType(tsType)) {
|
|
3168
3234
|
return resolveSlotType(tsType.typeAnnotation);
|
|
3169
3235
|
}
|
|
3170
|
-
if (
|
|
3236
|
+
if (t29.isTSTypeReference(tsType)) {
|
|
3171
3237
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3172
3238
|
return tsType;
|
|
3173
3239
|
}
|
|
3174
3240
|
const params = tsType.typeParameters.params.map((param) => resolveSlotType(param)).filter(Boolean);
|
|
3175
|
-
return
|
|
3241
|
+
return t29.tsTypeReference(
|
|
3176
3242
|
tsType.typeName,
|
|
3177
|
-
|
|
3243
|
+
t29.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3178
3244
|
);
|
|
3179
3245
|
}
|
|
3180
|
-
if (
|
|
3246
|
+
if (t29.isTSIntersectionType(tsType)) {
|
|
3181
3247
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
3182
3248
|
if (!types.length) return null;
|
|
3183
3249
|
if (types.length === 1) return types[0];
|
|
3184
|
-
return
|
|
3250
|
+
return t29.tsIntersectionType(types);
|
|
3185
3251
|
}
|
|
3186
|
-
if (
|
|
3252
|
+
if (t29.isTSUnionType(tsType)) {
|
|
3187
3253
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
3188
3254
|
if (!types.length) return null;
|
|
3189
3255
|
if (types.length === 1) return types[0];
|
|
3190
|
-
return
|
|
3256
|
+
return t29.tsUnionType(types);
|
|
3191
3257
|
}
|
|
3192
|
-
if (
|
|
3258
|
+
if (t29.isTSTypeLiteral(tsType)) {
|
|
3193
3259
|
const members = [];
|
|
3194
3260
|
for (const member of tsType.members) {
|
|
3195
3261
|
const resolved = resolveSlotPropFromMember(member);
|
|
@@ -3200,56 +3266,56 @@ function resolveSlotType(tsType) {
|
|
|
3200
3266
|
members.push(member);
|
|
3201
3267
|
}
|
|
3202
3268
|
if (!members.length) return null;
|
|
3203
|
-
return
|
|
3269
|
+
return t29.tsTypeLiteral(members);
|
|
3204
3270
|
}
|
|
3205
|
-
if (
|
|
3271
|
+
if (t29.isTSFunctionType(tsType)) {
|
|
3206
3272
|
const props = buildSlotPropSignature(
|
|
3207
3273
|
SLOT_DEFAULT_NAME,
|
|
3208
3274
|
cloneCallableParams(tsType.parameters),
|
|
3209
3275
|
false
|
|
3210
3276
|
);
|
|
3211
|
-
return
|
|
3277
|
+
return t29.tsTypeLiteral([props]);
|
|
3212
3278
|
}
|
|
3213
3279
|
return tsType;
|
|
3214
3280
|
}
|
|
3215
3281
|
function hasSlotsSignatureInType(tsType) {
|
|
3216
|
-
if (
|
|
3282
|
+
if (t29.isTSParenthesizedType(tsType)) {
|
|
3217
3283
|
return hasSlotsSignatureInType(tsType.typeAnnotation);
|
|
3218
3284
|
}
|
|
3219
|
-
if (
|
|
3285
|
+
if (t29.isTSTypeReference(tsType)) {
|
|
3220
3286
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3221
3287
|
return false;
|
|
3222
3288
|
}
|
|
3223
3289
|
return tsType.typeParameters.params.some(hasSlotsSignatureInType);
|
|
3224
3290
|
}
|
|
3225
|
-
if (
|
|
3291
|
+
if (t29.isTSIntersectionType(tsType) || t29.isTSUnionType(tsType)) {
|
|
3226
3292
|
return tsType.types.some(hasSlotsSignatureInType);
|
|
3227
3293
|
}
|
|
3228
|
-
if (
|
|
3294
|
+
if (t29.isTSTypeLiteral(tsType)) {
|
|
3229
3295
|
return tsType.members.some(hasSlotsSignatureInMember);
|
|
3230
3296
|
}
|
|
3231
|
-
if (
|
|
3297
|
+
if (t29.isTSFunctionType(tsType)) {
|
|
3232
3298
|
return true;
|
|
3233
3299
|
}
|
|
3234
3300
|
return false;
|
|
3235
3301
|
}
|
|
3236
3302
|
function hasSlotsSignatureInMember(member) {
|
|
3237
|
-
if (
|
|
3238
|
-
if (
|
|
3239
|
-
if (
|
|
3303
|
+
if (t29.isTSMethodSignature(member)) return true;
|
|
3304
|
+
if (t29.isTSCallSignatureDeclaration(member)) return true;
|
|
3305
|
+
if (t29.isTSPropertySignature(member)) {
|
|
3240
3306
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3241
3307
|
return !!(typeAnnotation && resolveCallableType(typeAnnotation));
|
|
3242
3308
|
}
|
|
3243
3309
|
return false;
|
|
3244
3310
|
}
|
|
3245
3311
|
function resolveSlotPropFromMember(member) {
|
|
3246
|
-
if (
|
|
3312
|
+
if (t29.isTSMethodSignature(member)) {
|
|
3247
3313
|
const rawName = resolvePropName2(member.key);
|
|
3248
3314
|
if (!rawName) return null;
|
|
3249
3315
|
const params = cloneCallableParams(member.parameters);
|
|
3250
3316
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
3251
3317
|
}
|
|
3252
|
-
if (
|
|
3318
|
+
if (t29.isTSPropertySignature(member)) {
|
|
3253
3319
|
const rawName = resolvePropName2(member.key);
|
|
3254
3320
|
if (!rawName) return null;
|
|
3255
3321
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
@@ -3258,53 +3324,53 @@ function resolveSlotPropFromMember(member) {
|
|
|
3258
3324
|
const params = cloneCallableParams(callable.parameters);
|
|
3259
3325
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
3260
3326
|
}
|
|
3261
|
-
if (
|
|
3327
|
+
if (t29.isTSCallSignatureDeclaration(member)) {
|
|
3262
3328
|
const params = cloneCallableParams(member.parameters);
|
|
3263
3329
|
return buildSlotPropSignature(SLOT_DEFAULT_NAME, params, true);
|
|
3264
3330
|
}
|
|
3265
3331
|
return null;
|
|
3266
3332
|
}
|
|
3267
3333
|
function resolveCallableType(tsType) {
|
|
3268
|
-
if (
|
|
3269
|
-
if (
|
|
3334
|
+
if (t29.isTSFunctionType(tsType)) return tsType;
|
|
3335
|
+
if (t29.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
3270
3336
|
return null;
|
|
3271
3337
|
}
|
|
3272
3338
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
3273
3339
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
3274
|
-
const key =
|
|
3275
|
-
const reactNodeType =
|
|
3276
|
-
|
|
3340
|
+
const key = t29.isValidIdentifier(propName) ? t29.identifier(propName) : t29.stringLiteral(propName);
|
|
3341
|
+
const reactNodeType = t29.tsTypeAnnotation(
|
|
3342
|
+
t29.tsTypeReference(t29.identifier(REACT_API_MAP.ReactNode))
|
|
3277
3343
|
);
|
|
3278
3344
|
let typeAnnotation;
|
|
3279
3345
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
3280
3346
|
typeAnnotation = reactNodeType;
|
|
3281
3347
|
} else {
|
|
3282
|
-
const fnType =
|
|
3283
|
-
typeAnnotation =
|
|
3348
|
+
const fnType = t29.tsFunctionType(null, params, reactNodeType);
|
|
3349
|
+
typeAnnotation = t29.tsTypeAnnotation(fnType);
|
|
3284
3350
|
}
|
|
3285
|
-
const prop =
|
|
3351
|
+
const prop = t29.tsPropertySignature(key, typeAnnotation);
|
|
3286
3352
|
prop.optional = optional;
|
|
3287
3353
|
return prop;
|
|
3288
3354
|
}
|
|
3289
3355
|
function createSlotScopeParam(props, ctx) {
|
|
3290
|
-
const paramId =
|
|
3356
|
+
const paramId = t29.identifier(SLOT_FN_PARAM_NAME);
|
|
3291
3357
|
const propsSigns = [];
|
|
3292
3358
|
const { reactiveBindings } = ctx.templateData;
|
|
3293
3359
|
props.forEach(({ prop, tsType }) => {
|
|
3294
3360
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3295
3361
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3296
|
-
const typeAnnotation = foundBindingTypes ?
|
|
3297
|
-
const key =
|
|
3298
|
-
const propSign =
|
|
3362
|
+
const typeAnnotation = foundBindingTypes ? t29.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3363
|
+
const key = t29.isValidIdentifier(prop) ? t29.identifier(prop) : t29.stringLiteral(prop);
|
|
3364
|
+
const propSign = t29.tsPropertySignature(key, typeAnnotation);
|
|
3299
3365
|
propsSigns.push(propSign);
|
|
3300
3366
|
});
|
|
3301
|
-
paramId.typeAnnotation =
|
|
3367
|
+
paramId.typeAnnotation = t29.tsTypeAnnotation(t29.tsTypeLiteral(propsSigns));
|
|
3302
3368
|
return paramId;
|
|
3303
3369
|
}
|
|
3304
3370
|
function resolvePropName2(key) {
|
|
3305
|
-
if (
|
|
3306
|
-
if (
|
|
3307
|
-
if (
|
|
3371
|
+
if (t29.isIdentifier(key)) return key.name;
|
|
3372
|
+
if (t29.isStringLiteral(key)) return key.value;
|
|
3373
|
+
if (t29.isNumericLiteral(key)) return String(key.value);
|
|
3308
3374
|
return null;
|
|
3309
3375
|
}
|
|
3310
3376
|
|
|
@@ -3312,8 +3378,8 @@ function resolvePropName2(key) {
|
|
|
3312
3378
|
function resolvePropsIface(ctx) {
|
|
3313
3379
|
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
3314
3380
|
return {
|
|
3315
|
-
CallExpression(
|
|
3316
|
-
const { node, parentPath } =
|
|
3381
|
+
CallExpression(path8) {
|
|
3382
|
+
const { node, parentPath } = path8;
|
|
3317
3383
|
const name = node.callee.name;
|
|
3318
3384
|
if (!isCalleeNamed(node, MACRO_API_NAMES.props) && !isCalleeNamed(node, MACRO_API_NAMES.emits) && !isCalleeNamed(node, MACRO_API_NAMES.slots)) {
|
|
3319
3385
|
return;
|
|
@@ -3322,7 +3388,7 @@ function resolvePropsIface(ctx) {
|
|
|
3322
3388
|
if (parentPath.isVariableDeclaration() || parentPath.isVariableDeclarator()) {
|
|
3323
3389
|
parentPath.remove();
|
|
3324
3390
|
} else {
|
|
3325
|
-
|
|
3391
|
+
path8.remove();
|
|
3326
3392
|
}
|
|
3327
3393
|
};
|
|
3328
3394
|
if (ctx.inputType !== "sfc") {
|
|
@@ -3336,11 +3402,11 @@ function resolvePropsIface(ctx) {
|
|
|
3336
3402
|
propsTSIface.hasPropsInJsEnv = true;
|
|
3337
3403
|
} else {
|
|
3338
3404
|
if (name === MACRO_API_NAMES.props) {
|
|
3339
|
-
resolveDefinePropsIface(
|
|
3405
|
+
resolveDefinePropsIface(path8, ctx);
|
|
3340
3406
|
} else if (name === MACRO_API_NAMES.emits) {
|
|
3341
|
-
resolveDefineEmitsIface(
|
|
3407
|
+
resolveDefineEmitsIface(path8, ctx);
|
|
3342
3408
|
} else if (name === MACRO_API_NAMES.slots) {
|
|
3343
|
-
resolveDefineSlotsIface(
|
|
3409
|
+
resolveDefineSlotsIface(path8, ctx);
|
|
3344
3410
|
}
|
|
3345
3411
|
}
|
|
3346
3412
|
removePath();
|
|
@@ -3356,9 +3422,9 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3356
3422
|
}
|
|
3357
3423
|
const n = ctx.compName || "Comp";
|
|
3358
3424
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3359
|
-
const typeNode =
|
|
3360
|
-
const typeAliasDecl =
|
|
3361
|
-
const exportDecl =
|
|
3425
|
+
const typeNode = t30.tsIntersectionType(tsTypes);
|
|
3426
|
+
const typeAliasDecl = t30.tsTypeAliasDeclaration(t30.identifier(ns), null, typeNode);
|
|
3427
|
+
const exportDecl = t30.exportNamedDeclaration(typeAliasDecl);
|
|
3362
3428
|
propsTSIface.name = ns;
|
|
3363
3429
|
const scriptIR = getScriptIR(ctx);
|
|
3364
3430
|
scriptIR.exports.push(exportDecl);
|
|
@@ -3366,17 +3432,17 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3366
3432
|
}
|
|
3367
3433
|
|
|
3368
3434
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3369
|
-
import * as
|
|
3435
|
+
import * as t32 from "@babel/types";
|
|
3370
3436
|
|
|
3371
3437
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3372
3438
|
import { traverse as traverse2 } from "@babel/core";
|
|
3373
|
-
import * as
|
|
3439
|
+
import * as t31 from "@babel/types";
|
|
3374
3440
|
var TRACE_MAX_DEPTH = 20;
|
|
3375
3441
|
function analyzeDeps(node, ctx, parentPath) {
|
|
3376
3442
|
if (!parentPath) {
|
|
3377
|
-
return
|
|
3443
|
+
return t31.arrayExpression([]);
|
|
3378
3444
|
}
|
|
3379
|
-
const isFnExpr =
|
|
3445
|
+
const isFnExpr = t31.isArrowFunctionExpression(node) || t31.isFunctionExpression(node);
|
|
3380
3446
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
3381
3447
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
3382
3448
|
const reactiveStateApis = getReactiveStateApis();
|
|
@@ -3389,11 +3455,11 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3389
3455
|
analyzeTarget,
|
|
3390
3456
|
{
|
|
3391
3457
|
"MemberExpression|OptionalMemberExpression"(memberPath) {
|
|
3392
|
-
const
|
|
3393
|
-
if (isNestedMemberObject(
|
|
3394
|
-
const rootId = findRootIdentifier(
|
|
3458
|
+
const path8 = memberPath;
|
|
3459
|
+
if (isNestedMemberObject(path8)) return;
|
|
3460
|
+
const rootId = findRootIdentifier(path8.node);
|
|
3395
3461
|
if (!rootId) return;
|
|
3396
|
-
tryAddDependency(
|
|
3462
|
+
tryAddDependency(path8, rootId.name, path8.scope);
|
|
3397
3463
|
processedIdentifiers.add(rootId);
|
|
3398
3464
|
},
|
|
3399
3465
|
Identifier(idPath) {
|
|
@@ -3428,40 +3494,41 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3428
3494
|
addDependency(sourcedExpression);
|
|
3429
3495
|
}
|
|
3430
3496
|
}
|
|
3431
|
-
function normalizeDependencyExpr(
|
|
3432
|
-
if (
|
|
3433
|
-
return
|
|
3497
|
+
function normalizeDependencyExpr(path8, rootName) {
|
|
3498
|
+
if (t31.isIdentifier(path8.node)) {
|
|
3499
|
+
return t31.identifier(path8.node.name);
|
|
3434
3500
|
}
|
|
3435
|
-
if (
|
|
3436
|
-
const normalizedExp = normalizeMemberForCallSite(
|
|
3437
|
-
|
|
3438
|
-
|
|
3501
|
+
if (t31.isMemberExpression(path8.node) || t31.isOptionalMemberExpression(path8.node)) {
|
|
3502
|
+
const normalizedExp = normalizeMemberForCallSite(path8, path8.node);
|
|
3503
|
+
const safeExp = t31.isMemberExpression(normalizedExp) || t31.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForRefValue(normalizedExp) : normalizedExp;
|
|
3504
|
+
if (isReactValidDependencyExpr(safeExp)) {
|
|
3505
|
+
return t31.cloneNode(safeExp, true);
|
|
3439
3506
|
}
|
|
3440
|
-
return
|
|
3507
|
+
return t31.identifier(rootName);
|
|
3441
3508
|
}
|
|
3442
3509
|
return null;
|
|
3443
3510
|
}
|
|
3444
3511
|
function isReactValidDependencyExpr(node2) {
|
|
3445
|
-
if (
|
|
3512
|
+
if (t31.isIdentifier(node2)) {
|
|
3446
3513
|
return true;
|
|
3447
3514
|
}
|
|
3448
|
-
if (
|
|
3515
|
+
if (t31.isMemberExpression(node2) || t31.isOptionalMemberExpression(node2)) {
|
|
3449
3516
|
return isStaticMemberChain(node2);
|
|
3450
3517
|
}
|
|
3451
3518
|
return false;
|
|
3452
3519
|
}
|
|
3453
3520
|
function isStaticMemberChain(node2) {
|
|
3454
3521
|
let current = node2;
|
|
3455
|
-
while (
|
|
3456
|
-
if (!current.computed && !
|
|
3522
|
+
while (t31.isMemberExpression(current) || t31.isOptionalMemberExpression(current)) {
|
|
3523
|
+
if (!current.computed && !t31.isIdentifier(current.property)) {
|
|
3457
3524
|
return false;
|
|
3458
3525
|
}
|
|
3459
|
-
if (current.computed && !
|
|
3526
|
+
if (current.computed && !t31.isStringLiteral(current.property) && !t31.isNumericLiteral(current.property)) {
|
|
3460
3527
|
return false;
|
|
3461
3528
|
}
|
|
3462
3529
|
current = current.object;
|
|
3463
3530
|
}
|
|
3464
|
-
return
|
|
3531
|
+
return t31.isIdentifier(current);
|
|
3465
3532
|
}
|
|
3466
3533
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
3467
3534
|
let current = binding.path;
|
|
@@ -3473,17 +3540,42 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3473
3540
|
}
|
|
3474
3541
|
return false;
|
|
3475
3542
|
}
|
|
3476
|
-
function normalizeMemberForCallSite(
|
|
3477
|
-
const parent =
|
|
3543
|
+
function normalizeMemberForCallSite(path8, node2) {
|
|
3544
|
+
const parent = path8.parentPath;
|
|
3478
3545
|
const isDirectCallee = !!parent && (parent.isCallExpression() && parent.node.callee === node2 || parent.isOptionalCallExpression() && parent.node.callee === node2);
|
|
3479
3546
|
if (!isDirectCallee) {
|
|
3480
3547
|
return node2;
|
|
3481
3548
|
}
|
|
3482
|
-
if (!
|
|
3549
|
+
if (!t31.isExpression(node2.object)) {
|
|
3483
3550
|
return node2;
|
|
3484
3551
|
}
|
|
3485
3552
|
return node2.object;
|
|
3486
3553
|
}
|
|
3554
|
+
function ensureOptionalForRefValue(node2) {
|
|
3555
|
+
if (!hasRefValueAccess(node2)) {
|
|
3556
|
+
return node2;
|
|
3557
|
+
}
|
|
3558
|
+
if (t31.isOptionalMemberExpression(node2) && node2.optional) {
|
|
3559
|
+
return node2;
|
|
3560
|
+
}
|
|
3561
|
+
const object = t31.cloneNode(node2.object, true);
|
|
3562
|
+
const property = t31.cloneNode(node2.property, true);
|
|
3563
|
+
return t31.optionalMemberExpression(object, property, node2.computed, true);
|
|
3564
|
+
}
|
|
3565
|
+
function hasRefValueAccess(node2) {
|
|
3566
|
+
let current = node2;
|
|
3567
|
+
while (t31.isMemberExpression(current) || t31.isOptionalMemberExpression(current)) {
|
|
3568
|
+
if (current.computed) {
|
|
3569
|
+
if (t31.isStringLiteral(current.property) && current.property.value === "value") {
|
|
3570
|
+
return true;
|
|
3571
|
+
}
|
|
3572
|
+
} else if (t31.isIdentifier(current.property) && current.property.name === "value") {
|
|
3573
|
+
return true;
|
|
3574
|
+
}
|
|
3575
|
+
current = current.object;
|
|
3576
|
+
}
|
|
3577
|
+
return false;
|
|
3578
|
+
}
|
|
3487
3579
|
function isEligibleBindingSource(binding) {
|
|
3488
3580
|
if (binding.kind === "param") {
|
|
3489
3581
|
return false;
|
|
@@ -3493,19 +3585,19 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3493
3585
|
const isImportBinding = bindingPath.isImportSpecifier() || bindingPath.isImportDefaultSpecifier() || bindingPath.isImportNamespaceSpecifier();
|
|
3494
3586
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
3495
3587
|
const nodeInit = declaratorPath?.node.init;
|
|
3496
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
3497
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
3498
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
3499
|
-
if (declaratorPath && nodeInit && (
|
|
3588
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t31.isCallExpression(nodeInit) && t31.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
3589
|
+
const isHookCallVarBinding = !!declaratorPath && t31.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
3590
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t31.isArrowFunctionExpression(nodeInit) || t31.isFunctionExpression(nodeInit));
|
|
3591
|
+
if (declaratorPath && nodeInit && (t31.isArrowFunctionExpression(nodeInit) || t31.isFunctionExpression(nodeInit))) {
|
|
3500
3592
|
markAsAnalyzed(nodeInit, false);
|
|
3501
3593
|
}
|
|
3502
3594
|
return isImportBinding || isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isFunctionBinding;
|
|
3503
3595
|
}
|
|
3504
3596
|
function isHookLikeCallee(callee) {
|
|
3505
|
-
if (
|
|
3597
|
+
if (t31.isIdentifier(callee)) {
|
|
3506
3598
|
return callee.name.startsWith("use");
|
|
3507
3599
|
}
|
|
3508
|
-
if (
|
|
3600
|
+
if (t31.isMemberExpression(callee) && !callee.computed && t31.isIdentifier(callee.property)) {
|
|
3509
3601
|
return callee.property.name.startsWith("use");
|
|
3510
3602
|
}
|
|
3511
3603
|
return false;
|
|
@@ -3521,7 +3613,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3521
3613
|
}
|
|
3522
3614
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
3523
3615
|
if (depth <= 0) return null;
|
|
3524
|
-
if (
|
|
3616
|
+
if (t31.isIdentifier(exp)) {
|
|
3525
3617
|
const sourceBinding = scope.getBinding(exp.name);
|
|
3526
3618
|
if (!sourceBinding) return null;
|
|
3527
3619
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -3529,13 +3621,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3529
3621
|
}
|
|
3530
3622
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3531
3623
|
}
|
|
3532
|
-
if (
|
|
3624
|
+
if (t31.isMemberExpression(exp) || t31.isOptionalMemberExpression(exp)) {
|
|
3533
3625
|
const root = findRootIdentifier(exp);
|
|
3534
3626
|
if (!root) return null;
|
|
3535
3627
|
const sourceBinding = scope.getBinding(root.name);
|
|
3536
3628
|
if (!sourceBinding) return null;
|
|
3537
3629
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
3538
|
-
return
|
|
3630
|
+
return t31.cloneNode(exp);
|
|
3539
3631
|
}
|
|
3540
3632
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3541
3633
|
if (sourcedRoot) {
|
|
@@ -3543,17 +3635,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3543
3635
|
if (rebuilt) {
|
|
3544
3636
|
return rebuilt;
|
|
3545
3637
|
}
|
|
3546
|
-
return
|
|
3638
|
+
return t31.cloneNode(sourcedRoot, true);
|
|
3547
3639
|
}
|
|
3548
3640
|
}
|
|
3549
3641
|
return null;
|
|
3550
3642
|
}
|
|
3551
3643
|
function rebuildMemberWithNewRoot(node2, nextRoot) {
|
|
3552
3644
|
const replacedObject = (() => {
|
|
3553
|
-
if (
|
|
3554
|
-
return
|
|
3645
|
+
if (t31.isIdentifier(node2.object)) {
|
|
3646
|
+
return t31.cloneNode(nextRoot, true);
|
|
3555
3647
|
}
|
|
3556
|
-
if (
|
|
3648
|
+
if (t31.isMemberExpression(node2.object) || t31.isOptionalMemberExpression(node2.object)) {
|
|
3557
3649
|
return rebuildMemberWithNewRoot(node2.object, nextRoot);
|
|
3558
3650
|
}
|
|
3559
3651
|
return null;
|
|
@@ -3561,45 +3653,45 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3561
3653
|
if (!replacedObject) {
|
|
3562
3654
|
return null;
|
|
3563
3655
|
}
|
|
3564
|
-
const property =
|
|
3565
|
-
if (
|
|
3566
|
-
return
|
|
3656
|
+
const property = t31.cloneNode(node2.property, true);
|
|
3657
|
+
if (t31.isMemberExpression(node2)) {
|
|
3658
|
+
return t31.memberExpression(
|
|
3567
3659
|
replacedObject,
|
|
3568
3660
|
property,
|
|
3569
3661
|
node2.computed
|
|
3570
3662
|
);
|
|
3571
3663
|
}
|
|
3572
|
-
return
|
|
3664
|
+
return t31.optionalMemberExpression(
|
|
3573
3665
|
replacedObject,
|
|
3574
3666
|
property,
|
|
3575
3667
|
node2.computed,
|
|
3576
3668
|
node2.optional
|
|
3577
3669
|
);
|
|
3578
3670
|
}
|
|
3579
|
-
return
|
|
3671
|
+
return t31.arrayExpression(Array.from(deps.values()));
|
|
3580
3672
|
}
|
|
3581
3673
|
function getDependencyKey(exp) {
|
|
3582
|
-
if (
|
|
3674
|
+
if (t31.isIdentifier(exp)) {
|
|
3583
3675
|
return exp.name;
|
|
3584
3676
|
}
|
|
3585
|
-
if (
|
|
3677
|
+
if (t31.isMemberExpression(exp) || t31.isOptionalMemberExpression(exp)) {
|
|
3586
3678
|
const objectKey = getDependencyKey(exp.object);
|
|
3587
3679
|
const opt = exp.optional ? "?" : "";
|
|
3588
|
-
if (!exp.computed &&
|
|
3680
|
+
if (!exp.computed && t31.isIdentifier(exp.property)) {
|
|
3589
3681
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
3590
3682
|
}
|
|
3591
|
-
if (
|
|
3683
|
+
if (t31.isStringLiteral(exp.property) || t31.isNumericLiteral(exp.property)) {
|
|
3592
3684
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
3593
3685
|
}
|
|
3594
3686
|
return `${objectKey}${opt}[*]`;
|
|
3595
3687
|
}
|
|
3596
3688
|
return exp.type;
|
|
3597
3689
|
}
|
|
3598
|
-
function isNestedMemberObject(
|
|
3599
|
-
const parent =
|
|
3690
|
+
function isNestedMemberObject(path8) {
|
|
3691
|
+
const parent = path8.parentPath;
|
|
3600
3692
|
if (!parent) return false;
|
|
3601
3693
|
if (parent.isMemberExpression() || parent.isOptionalMemberExpression()) {
|
|
3602
|
-
return parent.node.object ===
|
|
3694
|
+
return parent.node.object === path8.node;
|
|
3603
3695
|
}
|
|
3604
3696
|
return false;
|
|
3605
3697
|
}
|
|
@@ -3619,15 +3711,15 @@ function getIsAnalyzed(node) {
|
|
|
3619
3711
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3620
3712
|
function resolveAnalysisOnlyAdapter(ctx) {
|
|
3621
3713
|
return {
|
|
3622
|
-
"CallExpression|Identifier"(
|
|
3623
|
-
const node =
|
|
3714
|
+
"CallExpression|Identifier"(path8) {
|
|
3715
|
+
const node = path8.node;
|
|
3624
3716
|
const apiName = getApiName(node);
|
|
3625
3717
|
const adapter = ADAPTER_RULES.runtime[apiName];
|
|
3626
3718
|
if (!adapter || adapter.type !== "analyzed-deps") {
|
|
3627
3719
|
return;
|
|
3628
3720
|
}
|
|
3629
|
-
if (
|
|
3630
|
-
resolveCallNode(
|
|
3721
|
+
if (t32.isCallExpression(node)) {
|
|
3722
|
+
resolveCallNode(path8, adapter, ctx);
|
|
3631
3723
|
} else {
|
|
3632
3724
|
replaceIdName(node, adapter.target);
|
|
3633
3725
|
}
|
|
@@ -3636,24 +3728,24 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3636
3728
|
};
|
|
3637
3729
|
}
|
|
3638
3730
|
function getApiName(node) {
|
|
3639
|
-
const isCallNode =
|
|
3731
|
+
const isCallNode = t32.isCallExpression(node);
|
|
3640
3732
|
let apiName = "";
|
|
3641
|
-
if (
|
|
3733
|
+
if (t32.isIdentifier(node)) {
|
|
3642
3734
|
apiName = node.name;
|
|
3643
|
-
} else if (isCallNode &&
|
|
3735
|
+
} else if (isCallNode && t32.isIdentifier(node.callee)) {
|
|
3644
3736
|
apiName = node.callee.name;
|
|
3645
3737
|
}
|
|
3646
3738
|
return apiName;
|
|
3647
3739
|
}
|
|
3648
|
-
function resolveCallNode(
|
|
3649
|
-
const { node } =
|
|
3740
|
+
function resolveCallNode(path8, adapter, ctx) {
|
|
3741
|
+
const { node } = path8;
|
|
3650
3742
|
const { arguments: args } = node;
|
|
3651
3743
|
if (!args.length) return;
|
|
3652
3744
|
const fn = args[0];
|
|
3653
|
-
if (!
|
|
3745
|
+
if (!t32.isArrowFunctionExpression(fn) && !t32.isFunctionExpression(fn)) {
|
|
3654
3746
|
return;
|
|
3655
3747
|
}
|
|
3656
|
-
const fnPath =
|
|
3748
|
+
const fnPath = path8.get("arguments")[0];
|
|
3657
3749
|
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
3658
3750
|
args.push(deps);
|
|
3659
3751
|
replaceCallName(node, adapter.target);
|
|
@@ -3663,41 +3755,41 @@ function resolveCallNode(path7, adapter, ctx) {
|
|
|
3663
3755
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-arrow-deps.ts
|
|
3664
3756
|
function resolveArrowFnDeps(ctx, ast) {
|
|
3665
3757
|
return {
|
|
3666
|
-
ArrowFunctionExpression(
|
|
3667
|
-
const { node, parentPath } =
|
|
3668
|
-
if (isSkip(
|
|
3758
|
+
ArrowFunctionExpression(path8) {
|
|
3759
|
+
const { node, parentPath } = path8;
|
|
3760
|
+
if (isSkip(path8) || !atComponentOrHookRoot(parentPath, ast.program)) {
|
|
3669
3761
|
return;
|
|
3670
3762
|
}
|
|
3671
|
-
const deps = analyzeDeps(node, ctx,
|
|
3763
|
+
const deps = analyzeDeps(node, ctx, path8);
|
|
3672
3764
|
if (!deps.elements.length) return;
|
|
3673
3765
|
const newNode = createUseCallback(node, deps);
|
|
3674
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
3766
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3675
3767
|
markAsAnalyzed(newNode.arguments[0]);
|
|
3676
3768
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useCallback);
|
|
3677
3769
|
setScriptNodeMeta(declaratorPath?.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3678
|
-
|
|
3770
|
+
path8.replaceWith(newNode);
|
|
3679
3771
|
}
|
|
3680
3772
|
};
|
|
3681
3773
|
}
|
|
3682
3774
|
function resolveUnanalyzedArrow(ctx) {
|
|
3683
3775
|
return {
|
|
3684
|
-
ArrowFunctionExpression(
|
|
3685
|
-
const { node } =
|
|
3776
|
+
ArrowFunctionExpression(path8) {
|
|
3777
|
+
const { node } = path8;
|
|
3686
3778
|
const analyzed = getIsAnalyzed(node);
|
|
3687
3779
|
if (typeof analyzed === "undefined" || analyzed) return;
|
|
3688
3780
|
const newNode = createUseCallback(node);
|
|
3689
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
3781
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3690
3782
|
if (declaratorPath?.node) {
|
|
3691
3783
|
setScriptNodeMeta(declaratorPath.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3692
3784
|
}
|
|
3693
3785
|
markAsAnalyzed(newNode.arguments[0]);
|
|
3694
|
-
|
|
3786
|
+
path8.replaceWith(newNode);
|
|
3695
3787
|
}
|
|
3696
3788
|
};
|
|
3697
3789
|
}
|
|
3698
|
-
function isSkip(
|
|
3699
|
-
const { parentPath } =
|
|
3700
|
-
const isVariableDecl = () => getVariableDeclaratorPath(
|
|
3790
|
+
function isSkip(path8) {
|
|
3791
|
+
const { parentPath } = path8;
|
|
3792
|
+
const isVariableDecl = () => getVariableDeclaratorPath(path8) !== null;
|
|
3701
3793
|
const isReturnFunc = () => !isVariableDecl() && parentPath.isReturnStatement();
|
|
3702
3794
|
const isCallback = () => {
|
|
3703
3795
|
if (!parentPath) {
|
|
@@ -3706,12 +3798,12 @@ function isSkip(path7) {
|
|
|
3706
3798
|
if (parentPath.isCallExpression()) {
|
|
3707
3799
|
const callExpressionPath = parentPath;
|
|
3708
3800
|
const args = callExpressionPath.node.arguments;
|
|
3709
|
-
return args.some((arg) => arg ===
|
|
3801
|
+
return args.some((arg) => arg === path8.node);
|
|
3710
3802
|
}
|
|
3711
3803
|
if (parentPath.isArrayExpression()) {
|
|
3712
3804
|
const arrayExpressionPath = parentPath;
|
|
3713
3805
|
const elements = arrayExpressionPath.node.elements;
|
|
3714
|
-
return elements.some((element) => element ===
|
|
3806
|
+
return elements.some((element) => element === path8.node);
|
|
3715
3807
|
}
|
|
3716
3808
|
return false;
|
|
3717
3809
|
};
|
|
@@ -3732,16 +3824,16 @@ function isSkip(path7) {
|
|
|
3732
3824
|
}
|
|
3733
3825
|
|
|
3734
3826
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3735
|
-
import * as
|
|
3827
|
+
import * as t33 from "@babel/types";
|
|
3736
3828
|
function resolveElementRef(ctx) {
|
|
3737
3829
|
return {
|
|
3738
|
-
CallExpression(
|
|
3830
|
+
CallExpression(path8) {
|
|
3739
3831
|
const {
|
|
3740
3832
|
inputType,
|
|
3741
3833
|
templateData: { refBindings }
|
|
3742
3834
|
} = ctx;
|
|
3743
3835
|
if (inputType !== "sfc") return;
|
|
3744
|
-
const { node } =
|
|
3836
|
+
const { node } = path8;
|
|
3745
3837
|
const isUseTemplateRef = isCalleeNamed(node, VUE_API_MAP.useTemplateRef);
|
|
3746
3838
|
const isCompRefBindings = Object.keys(refBindings.componentRefs).length > 0 && isCalleeNamed(node, VUE_API_MAP.ref);
|
|
3747
3839
|
const shouldProcess = isUseTemplateRef || isCompRefBindings;
|
|
@@ -3749,31 +3841,31 @@ function resolveElementRef(ctx) {
|
|
|
3749
3841
|
return;
|
|
3750
3842
|
}
|
|
3751
3843
|
if (isCompRefBindings) {
|
|
3752
|
-
const varDeclaratorPath = getVariableDeclaratorPath(
|
|
3753
|
-
if (!
|
|
3844
|
+
const varDeclaratorPath = getVariableDeclaratorPath(path8)?.node;
|
|
3845
|
+
if (!t33.isIdentifier(varDeclaratorPath?.id)) {
|
|
3754
3846
|
return;
|
|
3755
3847
|
}
|
|
3756
3848
|
const varName = varDeclaratorPath.id.name;
|
|
3757
3849
|
const compRef = refBindings.componentRefs[varName];
|
|
3758
3850
|
if (!compRef) return;
|
|
3759
3851
|
}
|
|
3760
|
-
node.arguments[0] =
|
|
3761
|
-
resolveTypeParameters(ctx,
|
|
3852
|
+
node.arguments[0] = t33.identifier("null");
|
|
3853
|
+
resolveTypeParameters(ctx, path8);
|
|
3762
3854
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
3763
3855
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
3764
3856
|
},
|
|
3765
|
-
MemberExpression(
|
|
3766
|
-
resolveRefValueToCurrent(
|
|
3857
|
+
MemberExpression(path8) {
|
|
3858
|
+
resolveRefValueToCurrent(path8);
|
|
3767
3859
|
}
|
|
3768
3860
|
};
|
|
3769
3861
|
}
|
|
3770
|
-
function resolveTypeParameters(ctx,
|
|
3862
|
+
function resolveTypeParameters(ctx, path8) {
|
|
3771
3863
|
const {
|
|
3772
3864
|
templateData: { refBindings },
|
|
3773
3865
|
scriptData
|
|
3774
3866
|
} = ctx;
|
|
3775
|
-
const { node } =
|
|
3776
|
-
const varDeclaratorNode = getVariableDeclaratorPath(
|
|
3867
|
+
const { node } = path8;
|
|
3868
|
+
const varDeclaratorNode = getVariableDeclaratorPath(path8)?.node;
|
|
3777
3869
|
if (!scriptData.lang.startsWith("ts") || !varDeclaratorNode) {
|
|
3778
3870
|
return;
|
|
3779
3871
|
}
|
|
@@ -3782,45 +3874,45 @@ function resolveTypeParameters(ctx, path7) {
|
|
|
3782
3874
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
3783
3875
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
3784
3876
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
3785
|
-
node.typeParameters =
|
|
3877
|
+
node.typeParameters = t33.tsTypeParameterInstantiation([t33.tsTypeReference(t33.identifier(type))]);
|
|
3786
3878
|
}
|
|
3787
3879
|
}
|
|
3788
|
-
function resolveRefValueToCurrent(
|
|
3789
|
-
const { node } =
|
|
3790
|
-
if (node.computed || !
|
|
3880
|
+
function resolveRefValueToCurrent(path8) {
|
|
3881
|
+
const { node } = path8;
|
|
3882
|
+
if (node.computed || !t33.isIdentifier(node.property) || node.property.name !== "value") {
|
|
3791
3883
|
return;
|
|
3792
3884
|
}
|
|
3793
|
-
const rootPath = findRootVariablePath(
|
|
3794
|
-
if (!rootPath?.node || !
|
|
3885
|
+
const rootPath = findRootVariablePath(path8);
|
|
3886
|
+
if (!rootPath?.node || !t33.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
3795
3887
|
return;
|
|
3796
3888
|
}
|
|
3797
3889
|
const rootId = findRootIdentifier(node);
|
|
3798
|
-
if (!
|
|
3890
|
+
if (!t33.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
3799
3891
|
return;
|
|
3800
3892
|
}
|
|
3801
3893
|
node.property.name = "current";
|
|
3802
3894
|
}
|
|
3803
3895
|
|
|
3804
3896
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
3805
|
-
import * as
|
|
3897
|
+
import * as t34 from "@babel/types";
|
|
3806
3898
|
function resolveExprMemo(ctx, ast) {
|
|
3807
3899
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
3808
3900
|
return {
|
|
3809
|
-
VariableDeclarator(
|
|
3810
|
-
const { node } =
|
|
3901
|
+
VariableDeclarator(path8) {
|
|
3902
|
+
const { node } = path8;
|
|
3811
3903
|
const { init } = node;
|
|
3812
3904
|
if (!init) return;
|
|
3813
3905
|
const shouldMemo = () => {
|
|
3814
|
-
if (!atComponentOrHookRoot(
|
|
3906
|
+
if (!atComponentOrHookRoot(path8, ast.program, isScriptFile)) {
|
|
3815
3907
|
return false;
|
|
3816
3908
|
}
|
|
3817
|
-
if (
|
|
3909
|
+
if (t34.isCallExpression(init) && t34.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
3818
3910
|
return false;
|
|
3819
3911
|
}
|
|
3820
3912
|
return true;
|
|
3821
3913
|
};
|
|
3822
3914
|
if (!shouldMemo()) return;
|
|
3823
|
-
const initPath =
|
|
3915
|
+
const initPath = path8.get("init");
|
|
3824
3916
|
if (!initPath.isExpression()) return;
|
|
3825
3917
|
const deps = analyzeDeps(initPath.node, ctx, initPath);
|
|
3826
3918
|
if (!deps.elements.length) return;
|
|
@@ -3832,13 +3924,13 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3832
3924
|
}
|
|
3833
3925
|
|
|
3834
3926
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
3835
|
-
import * as
|
|
3927
|
+
import * as t35 from "@babel/types";
|
|
3836
3928
|
function resolveLintRules(ctx, ast) {
|
|
3837
3929
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
3838
3930
|
return {
|
|
3839
|
-
CallExpression(
|
|
3840
|
-
const { node, parentPath } =
|
|
3841
|
-
if (!
|
|
3931
|
+
CallExpression(path8) {
|
|
3932
|
+
const { node, parentPath } = path8;
|
|
3933
|
+
if (!t35.isIdentifier(node.callee)) return;
|
|
3842
3934
|
const { name: callName } = node.callee;
|
|
3843
3935
|
const addLog = (t41) => {
|
|
3844
3936
|
logger.error(t41, {
|
|
@@ -3856,7 +3948,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
3856
3948
|
);
|
|
3857
3949
|
return;
|
|
3858
3950
|
}
|
|
3859
|
-
if (!atComponentOrHookRoot(
|
|
3951
|
+
if (!atComponentOrHookRoot(path8, ast.program)) {
|
|
3860
3952
|
addLog(
|
|
3861
3953
|
`The ${macro} must be defined at the top level of the component, not inside blocks or functions.`
|
|
3862
3954
|
);
|
|
@@ -3872,7 +3964,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
3872
3964
|
};
|
|
3873
3965
|
const lintHooks = () => {
|
|
3874
3966
|
if (!callName.startsWith("use")) return;
|
|
3875
|
-
if (!atComponentOrHookRoot(
|
|
3967
|
+
if (!atComponentOrHookRoot(path8, ast.program, inScriptFile)) {
|
|
3876
3968
|
addLog(
|
|
3877
3969
|
`The ${callName} hook must be called at the top level, not inside loops, conditions, or nested functions.`
|
|
3878
3970
|
);
|
|
@@ -3886,12 +3978,12 @@ function resolveLintRules(ctx, ast) {
|
|
|
3886
3978
|
|
|
3887
3979
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
3888
3980
|
import { generate as generate3 } from "@babel/generator";
|
|
3889
|
-
import * as
|
|
3981
|
+
import * as t36 from "@babel/types";
|
|
3890
3982
|
function resolveProvide(ctx) {
|
|
3891
3983
|
if (ctx.inputType === "style") return {};
|
|
3892
3984
|
return {
|
|
3893
|
-
CallExpression(
|
|
3894
|
-
const { node } =
|
|
3985
|
+
CallExpression(path8) {
|
|
3986
|
+
const { node } = path8;
|
|
3895
3987
|
const providerTarget = ADAPTER_RULES.runtime[VUE_API_MAP.provide]?.target;
|
|
3896
3988
|
const isProvideCall = isCalleeNamed(node, VUE_API_MAP.provide) || providerTarget && isCalleeNamed(node, providerTarget);
|
|
3897
3989
|
if (!isProvideCall) return;
|
|
@@ -3901,7 +3993,7 @@ function resolveProvide(ctx) {
|
|
|
3901
3993
|
const adapter = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
3902
3994
|
assignProviderValue(target, key, value);
|
|
3903
3995
|
recordImport(ctx, adapter.package, adapter.target);
|
|
3904
|
-
|
|
3996
|
+
path8.parentPath.remove();
|
|
3905
3997
|
}
|
|
3906
3998
|
};
|
|
3907
3999
|
}
|
|
@@ -3918,13 +4010,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
3918
4010
|
function assignProviderValue(target, key, value) {
|
|
3919
4011
|
const getRawExp = (exp) => {
|
|
3920
4012
|
if (!exp) return "''";
|
|
3921
|
-
if (
|
|
4013
|
+
if (t36.isStringLiteral(exp)) {
|
|
3922
4014
|
return JSON.stringify(exp.value);
|
|
3923
4015
|
}
|
|
3924
|
-
if (
|
|
4016
|
+
if (t36.isNumericLiteral(exp)) {
|
|
3925
4017
|
return exp.value.toString();
|
|
3926
4018
|
}
|
|
3927
|
-
if (
|
|
4019
|
+
if (t36.isIdentifier(exp)) {
|
|
3928
4020
|
return exp.name;
|
|
3929
4021
|
}
|
|
3930
4022
|
try {
|
|
@@ -3940,16 +4032,16 @@ function assignProviderValue(target, key, value) {
|
|
|
3940
4032
|
}
|
|
3941
4033
|
|
|
3942
4034
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
3943
|
-
import * as
|
|
4035
|
+
import * as t37 from "@babel/types";
|
|
3944
4036
|
function resolveRenameAdapter(ctx) {
|
|
3945
4037
|
return {
|
|
3946
|
-
"CallExpression|Identifier"(
|
|
3947
|
-
const node =
|
|
3948
|
-
const isCallNode =
|
|
4038
|
+
"CallExpression|Identifier"(path8) {
|
|
4039
|
+
const node = path8.node;
|
|
4040
|
+
const isCallNode = t37.isCallExpression(node);
|
|
3949
4041
|
let apiName = "";
|
|
3950
|
-
if (
|
|
4042
|
+
if (t37.isIdentifier(node)) {
|
|
3951
4043
|
apiName = node.name;
|
|
3952
|
-
} else if (isCallNode &&
|
|
4044
|
+
} else if (isCallNode && t37.isIdentifier(node.callee)) {
|
|
3953
4045
|
apiName = node.callee.name;
|
|
3954
4046
|
}
|
|
3955
4047
|
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
@@ -3960,7 +4052,7 @@ function resolveRenameAdapter(ctx) {
|
|
|
3960
4052
|
}
|
|
3961
4053
|
if (adapter.isTrackable) {
|
|
3962
4054
|
const reactiveType = getReactiveType(apiName);
|
|
3963
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
4055
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3964
4056
|
setScriptNodeMeta(declaratorPath?.node, {
|
|
3965
4057
|
is_reactive: true,
|
|
3966
4058
|
reactive_type: reactiveType
|
|
@@ -4105,7 +4197,7 @@ import * as t39 from "@babel/types";
|
|
|
4105
4197
|
|
|
4106
4198
|
// src/shared/string-code-types.ts
|
|
4107
4199
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
4108
|
-
import * as
|
|
4200
|
+
import * as t38 from "@babel/types";
|
|
4109
4201
|
var strCodeTypes = {
|
|
4110
4202
|
isIdentifier: isIdentifier20,
|
|
4111
4203
|
isSimpleExpression,
|
|
@@ -4118,22 +4210,22 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4118
4210
|
} catch {
|
|
4119
4211
|
return false;
|
|
4120
4212
|
}
|
|
4121
|
-
if (
|
|
4213
|
+
if (t38.isLiteral(node)) {
|
|
4122
4214
|
return true;
|
|
4123
4215
|
}
|
|
4124
|
-
if (!excludeVar &&
|
|
4216
|
+
if (!excludeVar && t38.isIdentifier(node)) {
|
|
4125
4217
|
return true;
|
|
4126
4218
|
}
|
|
4127
|
-
if (
|
|
4128
|
-
return isSimpleExpression(node.object) &&
|
|
4219
|
+
if (t38.isMemberExpression(node)) {
|
|
4220
|
+
return isSimpleExpression(node.object) && t38.isIdentifier(node.property);
|
|
4129
4221
|
}
|
|
4130
|
-
if (
|
|
4222
|
+
if (t38.isObjectExpression(node) || t38.isArrayExpression(node)) {
|
|
4131
4223
|
return false;
|
|
4132
4224
|
}
|
|
4133
|
-
if (
|
|
4225
|
+
if (t38.isCallExpression(node) || t38.isAssignmentExpression(node)) {
|
|
4134
4226
|
return false;
|
|
4135
4227
|
}
|
|
4136
|
-
if (
|
|
4228
|
+
if (t38.isBinaryExpression(node) || t38.isUnaryExpression(node)) {
|
|
4137
4229
|
return true;
|
|
4138
4230
|
}
|
|
4139
4231
|
return false;
|
|
@@ -4141,7 +4233,7 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4141
4233
|
function isIdentifier20(code) {
|
|
4142
4234
|
try {
|
|
4143
4235
|
const node = parseExpression3(code);
|
|
4144
|
-
return
|
|
4236
|
+
return t38.isIdentifier(node);
|
|
4145
4237
|
} catch {
|
|
4146
4238
|
return false;
|
|
4147
4239
|
}
|
|
@@ -4149,69 +4241,12 @@ function isIdentifier20(code) {
|
|
|
4149
4241
|
function isStringLiteral12(code) {
|
|
4150
4242
|
try {
|
|
4151
4243
|
const node = parseExpression3(code);
|
|
4152
|
-
return
|
|
4244
|
+
return t38.isStringLiteral(node) || t38.isTemplateLiteral(node);
|
|
4153
4245
|
} catch {
|
|
4154
4246
|
return false;
|
|
4155
4247
|
}
|
|
4156
4248
|
}
|
|
4157
4249
|
|
|
4158
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
4159
|
-
import * as t38 from "@babel/types";
|
|
4160
|
-
|
|
4161
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
4162
|
-
function resolveSpecialExpressions(input, ctx) {
|
|
4163
|
-
input = resolveEmitsCalls(input, ctx);
|
|
4164
|
-
input = resolveRefVariable(input, ctx);
|
|
4165
|
-
return input;
|
|
4166
|
-
}
|
|
4167
|
-
function resolveEmitsCalls(input, ctx) {
|
|
4168
|
-
const result = matchEmitCalls(input, ctx);
|
|
4169
|
-
if (!result) return input;
|
|
4170
|
-
const [, , eventName, args] = result;
|
|
4171
|
-
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
4172
|
-
const event = args ? `on${callee}?.(${args})` : `on${callee}?.()`;
|
|
4173
|
-
return `${ctx.propField}?.${event}`;
|
|
4174
|
-
}
|
|
4175
|
-
function matchEmitCalls(input, ctx) {
|
|
4176
|
-
const { reactiveBindings } = ctx.templateData;
|
|
4177
|
-
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
4178
|
-
if (!macroBinding) return null;
|
|
4179
|
-
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4180
|
-
const regex = new RegExp(
|
|
4181
|
-
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
4182
|
-
// 可选的第二个参数
|
|
4183
|
-
"s"
|
|
4184
|
-
// s 标志让 . 匹配换行符
|
|
4185
|
-
);
|
|
4186
|
-
return input.trim().match(regex);
|
|
4187
|
-
}
|
|
4188
|
-
function resolveRefVariable(input, ctx) {
|
|
4189
|
-
const { reactiveBindings } = ctx.templateData;
|
|
4190
|
-
const addValueProperty = (input2, varName) => {
|
|
4191
|
-
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4192
|
-
const regex = new RegExp(`(?<![a-zA-Z0-9_])${escapedVarName}(?![a-zA-Z0-9_])(?!\\.value)`, "g");
|
|
4193
|
-
return input2.replace(regex, `${varName}.value`);
|
|
4194
|
-
};
|
|
4195
|
-
for (const name in reactiveBindings) {
|
|
4196
|
-
const binding = reactiveBindings[name];
|
|
4197
|
-
if (binding?.reactiveType !== "ref") continue;
|
|
4198
|
-
input = addValueProperty(input, name);
|
|
4199
|
-
}
|
|
4200
|
-
return input;
|
|
4201
|
-
}
|
|
4202
|
-
|
|
4203
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
4204
|
-
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
4205
|
-
if (toStrLiteral) return t38.stringLiteral(input);
|
|
4206
|
-
const { filename, scriptData } = ctx;
|
|
4207
|
-
const newContent = resolveSpecialExpressions(input, ctx);
|
|
4208
|
-
try {
|
|
4209
|
-
return stringToExpr(newContent, scriptData.lang, filename);
|
|
4210
|
-
} catch {
|
|
4211
|
-
return t38.identifier(newContent);
|
|
4212
|
-
}
|
|
4213
|
-
}
|
|
4214
|
-
|
|
4215
4250
|
// src/core/transform/sfc/template/shared/style-utils.ts
|
|
4216
4251
|
function parseStyleString(styleStr) {
|
|
4217
4252
|
if (isSimpleStyle(styleStr) || strCodeTypes.isIdentifier(styleStr)) {
|
|
@@ -5425,7 +5460,7 @@ function transform(ast, ctx, options) {
|
|
|
5425
5460
|
}
|
|
5426
5461
|
|
|
5427
5462
|
// package.json
|
|
5428
|
-
var version = "1.
|
|
5463
|
+
var version = "1.3.0";
|
|
5429
5464
|
var bin = {
|
|
5430
5465
|
vureact: "./bin/vureact.js"
|
|
5431
5466
|
};
|
|
@@ -5737,9 +5772,9 @@ var Helper = class {
|
|
|
5737
5772
|
};
|
|
5738
5773
|
}
|
|
5739
5774
|
async removeOutputFile(filePath, resolveOutputPath) {
|
|
5740
|
-
const
|
|
5741
|
-
if (!fs.existsSync(
|
|
5742
|
-
await fs.promises.unlink(
|
|
5775
|
+
const path8 = resolveOutputPath ? this.resolveOutputPath(filePath) : filePath;
|
|
5776
|
+
if (!fs.existsSync(path8)) return;
|
|
5777
|
+
await fs.promises.unlink(path8);
|
|
5743
5778
|
}
|
|
5744
5779
|
updateCache(targetFile, newData, cache) {
|
|
5745
5780
|
const index = cache.target.findIndex((c) => c.file === targetFile);
|
|
@@ -5804,12 +5839,12 @@ var Helper = class {
|
|
|
5804
5839
|
/**
|
|
5805
5840
|
* 读取 package.json 文件内容,并处理成对象返回
|
|
5806
5841
|
*/
|
|
5807
|
-
async resolvePackageFile(
|
|
5842
|
+
async resolvePackageFile(path8) {
|
|
5808
5843
|
try {
|
|
5809
|
-
if (!fs.existsSync(
|
|
5844
|
+
if (!fs.existsSync(path8)) {
|
|
5810
5845
|
return {};
|
|
5811
5846
|
}
|
|
5812
|
-
return JSON.parse(await fs.promises.readFile(
|
|
5847
|
+
return JSON.parse(await fs.promises.readFile(path8, "utf-8"));
|
|
5813
5848
|
} catch (error) {
|
|
5814
5849
|
console.error(error);
|
|
5815
5850
|
return {};
|
|
@@ -6330,10 +6365,12 @@ var CacheManager = class {
|
|
|
6330
6365
|
const meta = { ...unit };
|
|
6331
6366
|
delete meta.source;
|
|
6332
6367
|
if (key === "sfc" /* SFC */) {
|
|
6333
|
-
delete meta.output
|
|
6334
|
-
delete meta.output
|
|
6368
|
+
delete meta.output?.jsx.code;
|
|
6369
|
+
delete meta.output?.css.code;
|
|
6335
6370
|
} else if (key === "script" /* SCRIPT */) {
|
|
6336
|
-
delete meta.output
|
|
6371
|
+
delete meta.output?.script.code;
|
|
6372
|
+
} else if (key === "style" /* STYLE */) {
|
|
6373
|
+
delete meta.output?.style.code;
|
|
6337
6374
|
}
|
|
6338
6375
|
this.updateCache(unit.file, meta, cache);
|
|
6339
6376
|
await this.fileCompiler.saveCache(cache);
|
|
@@ -6494,6 +6531,7 @@ var CompilationUnitProcessor = class {
|
|
|
6494
6531
|
|
|
6495
6532
|
// src/compiler/shared/file-compiler/file-processor.ts
|
|
6496
6533
|
import fs3 from "fs";
|
|
6534
|
+
import path6 from "path";
|
|
6497
6535
|
var FileProcessor = class {
|
|
6498
6536
|
constructor(fileCompiler, compilationUnitProcessor, cacheManager) {
|
|
6499
6537
|
this.fileCompiler = fileCompiler;
|
|
@@ -6552,6 +6590,7 @@ var FileProcessor = class {
|
|
|
6552
6590
|
if (key === "sfc" /* SFC */ || key === "script" /* SCRIPT */) {
|
|
6553
6591
|
if (processed?.hasRoute) {
|
|
6554
6592
|
await this.injectVuReactRouteDep();
|
|
6593
|
+
await this.copyRouteSetupNotes();
|
|
6555
6594
|
}
|
|
6556
6595
|
}
|
|
6557
6596
|
await this.cacheManager.updateCacheIncrementally(processed, key);
|
|
@@ -6562,13 +6601,34 @@ var FileProcessor = class {
|
|
|
6562
6601
|
const pkgPath = this.fileCompiler.getOutputPkgPath();
|
|
6563
6602
|
const pkg = await this.fileCompiler.resolvePackageFile(pkgPath);
|
|
6564
6603
|
const { router } = this.pkgs;
|
|
6604
|
+
if (!pkg["dependencies"]) {
|
|
6605
|
+
pkg["dependencies"] = {};
|
|
6606
|
+
}
|
|
6565
6607
|
pkg["dependencies"][router.name] = router.version;
|
|
6566
6608
|
await fs3.promises.writeFile(pkgPath, JSON.stringify(pkg, null, 2), "utf-8");
|
|
6567
6609
|
}
|
|
6610
|
+
/**
|
|
6611
|
+
* 如果使用了路由,则拷贝路由配置说明文档到输出目录根部。
|
|
6612
|
+
*/
|
|
6613
|
+
async copyRouteSetupNotes() {
|
|
6614
|
+
const outputDir = this.fileCompiler.getOuputPath();
|
|
6615
|
+
const packageRoot = path6.resolve(getDirname(import.meta.url), "../");
|
|
6616
|
+
const templateDir = path6.join(packageRoot, "templates");
|
|
6617
|
+
if (!fs3.existsSync(templateDir)) {
|
|
6618
|
+
return;
|
|
6619
|
+
}
|
|
6620
|
+
const files = ["route-setup-notes.md", "route-setup-notes.zh.md"];
|
|
6621
|
+
for (const file of files) {
|
|
6622
|
+
const srcPath = path6.join(templateDir, file);
|
|
6623
|
+
if (!fs3.existsSync(srcPath)) continue;
|
|
6624
|
+
const destPath = path6.join(outputDir, file);
|
|
6625
|
+
await fs3.promises.copyFile(srcPath, destPath);
|
|
6626
|
+
}
|
|
6627
|
+
}
|
|
6568
6628
|
};
|
|
6569
6629
|
|
|
6570
6630
|
// src/compiler/shared/file-compiler/pipeline-manager.ts
|
|
6571
|
-
import
|
|
6631
|
+
import path7 from "path";
|
|
6572
6632
|
var PipelineManager = class {
|
|
6573
6633
|
constructor(fileCompiler, fileProcessor) {
|
|
6574
6634
|
this.fileCompiler = fileCompiler;
|
|
@@ -6603,7 +6663,7 @@ var PipelineManager = class {
|
|
|
6603
6663
|
const scriptExtRegex = /\.(js|ts)$/i;
|
|
6604
6664
|
const styleExtRegex = /\.(css|less|sass|scss)$/i;
|
|
6605
6665
|
const files = this.fileCompiler.scanFiles(inputPath, (p) => {
|
|
6606
|
-
const ext =
|
|
6666
|
+
const ext = path7.extname(p);
|
|
6607
6667
|
if (key === "sfc" /* SFC */) {
|
|
6608
6668
|
return ext === ".vue";
|
|
6609
6669
|
}
|
|
@@ -6816,27 +6876,55 @@ var FileCompiler = class extends BaseCompiler {
|
|
|
6816
6876
|
}
|
|
6817
6877
|
await this.viteBootstrapper.bootstrapIfNeeded();
|
|
6818
6878
|
try {
|
|
6819
|
-
this.
|
|
6820
|
-
const
|
|
6821
|
-
this.
|
|
6822
|
-
this.
|
|
6823
|
-
const scriptCount = await this.pipelineManager.runScriptPipeline();
|
|
6824
|
-
this.spinner.stop();
|
|
6825
|
-
this.spinner.start("Compiling style files...");
|
|
6826
|
-
const styleCount = await this.pipelineManager.runStylePipeline();
|
|
6827
|
-
this.spinner.stop();
|
|
6828
|
-
this.spinner.start("Copying assets...");
|
|
6829
|
-
const assetCount = await this.assetManager.runAssetPipeline();
|
|
6830
|
-
this.spinner.stop();
|
|
6879
|
+
const sfcCount = await this.runPipelineWithSpinner("sfc" /* SFC */);
|
|
6880
|
+
const scriptCount = await this.runPipelineWithSpinner("script" /* SCRIPT */);
|
|
6881
|
+
const styleCount = await this.runPipelineWithSpinner("style" /* STYLE */);
|
|
6882
|
+
const assetCount = await this.runPipelineWithSpinner("copied" /* ASSET */);
|
|
6831
6883
|
await this.options.onSuccess?.();
|
|
6832
6884
|
const endTime = calcElapsedTime(startTime);
|
|
6833
6885
|
this.printCoreLogs();
|
|
6834
6886
|
this.showCompileStats(endTime, sfcCount, scriptCount, styleCount, assetCount);
|
|
6835
6887
|
} catch (error) {
|
|
6836
|
-
this.spinner.stop();
|
|
6837
6888
|
const endTime = calcElapsedTime(startTime);
|
|
6838
|
-
console.error(kleur6.red("\u2716"), `Build failed in ${endTime}
|
|
6839
|
-
|
|
6889
|
+
console.error(kleur6.red("\u2716"), `Build failed in ${endTime}`);
|
|
6890
|
+
console.error(error);
|
|
6891
|
+
}
|
|
6892
|
+
}
|
|
6893
|
+
/**
|
|
6894
|
+
* 运行管线并显示加载动画
|
|
6895
|
+
*
|
|
6896
|
+
* @private
|
|
6897
|
+
* @param text - 加载动画显示的文本
|
|
6898
|
+
* @param pipelineFn - 要执行的管线函数
|
|
6899
|
+
* @returns 返回的处理的文件数
|
|
6900
|
+
*/
|
|
6901
|
+
async runPipelineWithSpinner(name) {
|
|
6902
|
+
const options = {
|
|
6903
|
+
["sfc" /* SFC */]: {
|
|
6904
|
+
text: "Compiling Vue files...",
|
|
6905
|
+
pipeline: () => this.pipelineManager.runSfcPipeline()
|
|
6906
|
+
},
|
|
6907
|
+
["script" /* SCRIPT */]: {
|
|
6908
|
+
text: "Compiling script files...",
|
|
6909
|
+
pipeline: () => this.pipelineManager.runScriptPipeline()
|
|
6910
|
+
},
|
|
6911
|
+
["style" /* STYLE */]: {
|
|
6912
|
+
text: "Compiling style files...",
|
|
6913
|
+
pipeline: () => this.pipelineManager.runStylePipeline()
|
|
6914
|
+
},
|
|
6915
|
+
["copied" /* ASSET */]: {
|
|
6916
|
+
text: "Copying assets...",
|
|
6917
|
+
pipeline: () => this.assetManager.runAssetPipeline()
|
|
6918
|
+
}
|
|
6919
|
+
};
|
|
6920
|
+
const { text, pipeline } = options[name];
|
|
6921
|
+
try {
|
|
6922
|
+
this.spinner.start(text);
|
|
6923
|
+
return await pipeline();
|
|
6924
|
+
} catch (err) {
|
|
6925
|
+
throw err;
|
|
6926
|
+
} finally {
|
|
6927
|
+
this.spinner.stop();
|
|
6840
6928
|
}
|
|
6841
6929
|
}
|
|
6842
6930
|
/**
|
|
@@ -7006,6 +7094,7 @@ var VuReact = class extends FileCompiler {
|
|
|
7006
7094
|
};
|
|
7007
7095
|
|
|
7008
7096
|
export {
|
|
7097
|
+
getDirname,
|
|
7009
7098
|
normalizePath,
|
|
7010
7099
|
calcElapsedTime,
|
|
7011
7100
|
generateComponent,
|