babel-plugin-essor 0.0.17-beta.1 → 0.0.17-beta.2
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/dist/index.cjs +279 -241
- package/dist/index.js +279 -241
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -61,7 +61,6 @@ var IMPORTS_MAPS = [
|
|
|
61
61
|
"reactive",
|
|
62
62
|
"memoEffect",
|
|
63
63
|
"omitProps",
|
|
64
|
-
"resolveDefaultProps",
|
|
65
64
|
// component
|
|
66
65
|
"createComponent",
|
|
67
66
|
"Fragment",
|
|
@@ -83,9 +82,7 @@ var IMPORTS_MAPS = [
|
|
|
83
82
|
"addEventListener",
|
|
84
83
|
// rendering related
|
|
85
84
|
"render",
|
|
86
|
-
"
|
|
87
|
-
"toEscapedHtmlString",
|
|
88
|
-
"markAsRawHtml",
|
|
85
|
+
"escape",
|
|
89
86
|
"escapeHTML",
|
|
90
87
|
"getHydrationKey",
|
|
91
88
|
"hydrationAnchor",
|
|
@@ -113,31 +110,13 @@ var HYDRATE_IMPORT_REMAPS = {
|
|
|
113
110
|
patchAttr: "patchAttrHydrate",
|
|
114
111
|
patchStyle: "patchStyleHydrate"
|
|
115
112
|
};
|
|
116
|
-
var
|
|
117
|
-
"
|
|
118
|
-
"
|
|
119
|
-
"
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
|
|
123
|
-
"Portal",
|
|
124
|
-
"Suspense",
|
|
125
|
-
"getHydrationKey",
|
|
126
|
-
"ssrAttr",
|
|
127
|
-
"ssrBind",
|
|
128
|
-
"ssrClass",
|
|
129
|
-
"ssrSelected",
|
|
130
|
-
"ssrStyle",
|
|
131
|
-
"ssrSpread",
|
|
132
|
-
"ssrTextValue",
|
|
133
|
-
"createComponent",
|
|
134
|
-
"patchAttr"
|
|
135
|
-
];
|
|
136
|
-
var _remaps = SERVER_IMPORT_REMAPS;
|
|
137
|
-
var SERVER_EXPORTS = new Set(SERVER_ONLY_NAMES.map((name) => {
|
|
138
|
-
var _a;
|
|
139
|
-
return (_a = _remaps[name]) != null ? _a : name;
|
|
140
|
-
}));
|
|
113
|
+
var UNIVERSAL_IMPORTS = /* @__PURE__ */ new Set([
|
|
114
|
+
"signal",
|
|
115
|
+
"computed",
|
|
116
|
+
"reactive",
|
|
117
|
+
"memoEffect",
|
|
118
|
+
"omitProps"
|
|
119
|
+
]);
|
|
141
120
|
var importMap = Object.fromEntries(IMPORTS_MAPS.map((name) => [name, name]));
|
|
142
121
|
var TRANSFORM_PROPERTY_NAME = "__props";
|
|
143
122
|
var FRAGMENT_NAME = "Fragment";
|
|
@@ -224,7 +203,7 @@ function computeDeclarationCacheKey(expression) {
|
|
|
224
203
|
const parts = [];
|
|
225
204
|
for (const el of expression.elements) {
|
|
226
205
|
if (el == null) {
|
|
227
|
-
parts.push("
|
|
206
|
+
parts.push(" null");
|
|
228
207
|
continue;
|
|
229
208
|
}
|
|
230
209
|
if (core.types.isStringLiteral(el)) {
|
|
@@ -257,23 +236,26 @@ function createImport(path) {
|
|
|
257
236
|
const ctx = getCompileContext();
|
|
258
237
|
if (!ctx.imports.size) return;
|
|
259
238
|
const { mode } = ctx.options;
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
239
|
+
const emit = (names, source) => {
|
|
240
|
+
if (!names.length) return;
|
|
241
|
+
const specifiers = names.map(
|
|
242
|
+
(name) => core.types.importSpecifier(
|
|
243
|
+
ctx.importIdentifiers[name],
|
|
244
|
+
core.types.identifier(resolveImportedName(name, mode))
|
|
245
|
+
)
|
|
246
|
+
);
|
|
247
|
+
path.node.body.unshift(core.types.importDeclaration(specifiers, core.types.stringLiteral(source)));
|
|
248
|
+
};
|
|
249
|
+
if (mode === "server" /* SERVER */) {
|
|
250
|
+
const universal = [];
|
|
251
|
+
const server = [];
|
|
252
|
+
for (const name of ctx.imports) {
|
|
253
|
+
(UNIVERSAL_IMPORTS.has(name) ? universal : server).push(name);
|
|
254
|
+
}
|
|
255
|
+
emit(server, "essor/server");
|
|
256
|
+
emit(universal, "essor");
|
|
257
|
+
} else {
|
|
258
|
+
emit([...ctx.imports], "essor");
|
|
277
259
|
}
|
|
278
260
|
}
|
|
279
261
|
|
|
@@ -821,38 +803,55 @@ function applyHmr(programPath, ctx) {
|
|
|
821
803
|
])
|
|
822
804
|
);
|
|
823
805
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
806
|
+
var DEFAULT_PREFIX = "$";
|
|
807
|
+
var cachedContext = null;
|
|
808
|
+
var cachedPrefix = DEFAULT_PREFIX;
|
|
809
|
+
function getSignalPrefix() {
|
|
810
|
+
var _a;
|
|
811
|
+
const ctx = getCompileContext();
|
|
812
|
+
if (ctx !== cachedContext) {
|
|
813
|
+
cachedContext = ctx;
|
|
814
|
+
cachedPrefix = (_a = ctx.options.signalPrefix) != null ? _a : DEFAULT_PREFIX;
|
|
830
815
|
}
|
|
831
|
-
return
|
|
816
|
+
return cachedPrefix;
|
|
832
817
|
}
|
|
833
818
|
function isSignal(name) {
|
|
834
|
-
|
|
835
|
-
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
836
|
-
return !!name && x(name, prefix);
|
|
819
|
+
return !!name && x(name, getSignalPrefix());
|
|
837
820
|
}
|
|
838
821
|
function stripSignalPrefix(name) {
|
|
839
|
-
|
|
840
|
-
const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
|
|
822
|
+
const prefix = getSignalPrefix();
|
|
841
823
|
return x(name, prefix) ? name.slice(prefix.length) : name;
|
|
842
824
|
}
|
|
825
|
+
function isMemberLike(node) {
|
|
826
|
+
return core.types.isMemberExpression(node) || core.types.isOptionalMemberExpression(node);
|
|
827
|
+
}
|
|
828
|
+
function isMemberAccessingProperty(node, propertyName) {
|
|
829
|
+
if (node.computed) {
|
|
830
|
+
return core.types.isStringLiteral(node.property) && node.property.value === propertyName;
|
|
831
|
+
}
|
|
832
|
+
return core.types.isIdentifier(node.property) && node.property.name === propertyName;
|
|
833
|
+
}
|
|
834
|
+
function clone(node) {
|
|
835
|
+
return core.types.cloneNode(node, true);
|
|
836
|
+
}
|
|
837
|
+
function valueAccess(object) {
|
|
838
|
+
return core.types.memberExpression(object, core.types.identifier("value"));
|
|
839
|
+
}
|
|
840
|
+
function makeComputed(access) {
|
|
841
|
+
return core.types.callExpression(useImport("computed"), [core.types.arrowFunctionExpression([], access)]);
|
|
842
|
+
}
|
|
843
843
|
function replaceSymbol(path) {
|
|
844
844
|
const { init, id } = path.node;
|
|
845
845
|
if (core.types.isObjectPattern(id) || core.types.isArrayPattern(id)) {
|
|
846
846
|
transformDestructuring(path);
|
|
847
847
|
return;
|
|
848
848
|
}
|
|
849
|
-
if (!core.types.isIdentifier(id)) return;
|
|
850
|
-
if (!isSignal(id.name)) return;
|
|
851
|
-
if (isAlreadySignalCall(init)) return;
|
|
849
|
+
if (!core.types.isIdentifier(id) || !isSignal(id.name) || isAlreadySignalCall(init)) return;
|
|
852
850
|
const isComputed = isFunctionLikeExpression(init) && path.parent.kind === "const";
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
851
|
+
path.node.init = core.types.callExpression(
|
|
852
|
+
useImport(isComputed ? "computed" : "signal"),
|
|
853
|
+
init ? [init] : []
|
|
854
|
+
);
|
|
856
855
|
}
|
|
857
856
|
function isAlreadySignalCall(init) {
|
|
858
857
|
if (!init || !core.types.isCallExpression(init) || !core.types.isIdentifier(init.callee)) return false;
|
|
@@ -876,26 +875,22 @@ function transformDestructuring(path) {
|
|
|
876
875
|
path.skip();
|
|
877
876
|
return;
|
|
878
877
|
}
|
|
879
|
-
const
|
|
880
|
-
const declarators = bindPattern(pattern, base);
|
|
878
|
+
const declarators = bindPattern(pattern, signalSourceBase(init));
|
|
881
879
|
const declaration = declPath.node;
|
|
882
880
|
const index = declaration.declarations.indexOf(path.node);
|
|
883
881
|
declaration.declarations.splice(index, 1, ...declarators);
|
|
884
882
|
path.skip();
|
|
885
883
|
}
|
|
886
|
-
function clone(node) {
|
|
887
|
-
return core.types.cloneNode(node, true);
|
|
888
|
-
}
|
|
889
884
|
function isSignalSource(init) {
|
|
890
885
|
if (core.types.isIdentifier(init)) return isSignal(init.name);
|
|
891
|
-
if (
|
|
886
|
+
if (isMemberLike(init) && core.types.isIdentifier(init.object) && isSignal(init.object.name)) {
|
|
892
887
|
return isMemberAccessingProperty(init, "value");
|
|
893
888
|
}
|
|
894
889
|
return false;
|
|
895
890
|
}
|
|
896
891
|
function signalSourceBase(init) {
|
|
897
892
|
if (core.types.isIdentifier(init) && isSignal(init.name)) {
|
|
898
|
-
return
|
|
893
|
+
return valueAccess(core.types.identifier(init.name));
|
|
899
894
|
}
|
|
900
895
|
return clone(init);
|
|
901
896
|
}
|
|
@@ -926,8 +921,7 @@ function bindObjectPattern(pattern, base) {
|
|
|
926
921
|
}
|
|
927
922
|
const { keyExpr, computed, target } = resolveObjectProp(prop);
|
|
928
923
|
handledKeys.push({ key: clone(keyExpr), computed });
|
|
929
|
-
|
|
930
|
-
out.push(...bindTarget(target, access));
|
|
924
|
+
out.push(...bindTarget(target, core.types.memberExpression(clone(base), keyExpr, computed)));
|
|
931
925
|
}
|
|
932
926
|
return out;
|
|
933
927
|
}
|
|
@@ -953,8 +947,7 @@ function bindArrayPattern(pattern, base) {
|
|
|
953
947
|
out.push(...bindTarget(el.argument, slice));
|
|
954
948
|
return;
|
|
955
949
|
}
|
|
956
|
-
|
|
957
|
-
out.push(...bindTarget(el, access));
|
|
950
|
+
out.push(...bindTarget(el, core.types.memberExpression(clone(base), core.types.numericLiteral(i2), true)));
|
|
958
951
|
});
|
|
959
952
|
return out;
|
|
960
953
|
}
|
|
@@ -970,9 +963,6 @@ function bindTarget(target, access) {
|
|
|
970
963
|
if (core.types.isArrayPattern(target)) return bindArrayPattern(target, access);
|
|
971
964
|
return [core.types.variableDeclarator(target, access)];
|
|
972
965
|
}
|
|
973
|
-
function makeComputed(access) {
|
|
974
|
-
return core.types.callExpression(useImport("computed"), [core.types.arrowFunctionExpression([], access)]);
|
|
975
|
-
}
|
|
976
966
|
function applyDefault(access, def) {
|
|
977
967
|
return core.types.conditionalExpression(
|
|
978
968
|
core.types.binaryExpression("===", clone(access), core.types.identifier("undefined")),
|
|
@@ -997,23 +987,24 @@ function buildObjectRest(rest, base, handledKeys) {
|
|
|
997
987
|
return core.types.variableDeclarator(omits(arg), clone(base));
|
|
998
988
|
}
|
|
999
989
|
function symbolIdentifier(path) {
|
|
1000
|
-
const name = path.node
|
|
990
|
+
const { name } = path.node;
|
|
1001
991
|
if (!isSignal(name)) return;
|
|
1002
|
-
if (!shouldProcessIdentifier(path
|
|
992
|
+
if (!shouldProcessIdentifier(path)) return;
|
|
1003
993
|
if (isAlreadyValueAccess(path)) return;
|
|
1004
994
|
const parent = path.parent;
|
|
1005
|
-
if (
|
|
1006
|
-
path.replaceWith(
|
|
995
|
+
if (isMemberLike(parent) && parent.property === path.node && !parent.computed) return;
|
|
996
|
+
path.replaceWith(valueAccess(core.types.identifier(name)));
|
|
1007
997
|
path.skip();
|
|
1008
998
|
}
|
|
1009
|
-
function shouldProcessIdentifier(path
|
|
999
|
+
function shouldProcessIdentifier(path) {
|
|
1000
|
+
const parentPath = path.parentPath;
|
|
1010
1001
|
if (!parentPath) return false;
|
|
1011
1002
|
const parent = parentPath.node;
|
|
1012
1003
|
const node = path.node;
|
|
1013
|
-
if (core.types.isVariableDeclarator(parent) || core.types.isArrayPattern(parent) || core.types.isObjectPattern(parent)) {
|
|
1004
|
+
if (core.types.isVariableDeclarator(parent) || core.types.isArrayPattern(parent) || core.types.isObjectPattern(parent) || core.types.isRestElement(parent)) {
|
|
1014
1005
|
return false;
|
|
1015
1006
|
}
|
|
1016
|
-
if (core.types.isImportSpecifier(parent) || core.types.isImportDefaultSpecifier(parent) || core.types.isImportNamespaceSpecifier(parent)) {
|
|
1007
|
+
if (core.types.isImportSpecifier(parent) || core.types.isImportDefaultSpecifier(parent) || core.types.isImportNamespaceSpecifier(parent) || core.types.isExportSpecifier(parent)) {
|
|
1017
1008
|
return false;
|
|
1018
1009
|
}
|
|
1019
1010
|
if (core.types.isFunctionDeclaration(parent) || core.types.isFunctionExpression(parent)) {
|
|
@@ -1024,7 +1015,7 @@ function shouldProcessIdentifier(path, parentPath) {
|
|
|
1024
1015
|
}
|
|
1025
1016
|
if (core.types.isClassDeclaration(parent) && parent.id === node) return false;
|
|
1026
1017
|
if (core.types.isObjectMethod(parent) || core.types.isClassMethod(parent)) return false;
|
|
1027
|
-
if (core.types.isObjectProperty(parent) && parent.key === node) return false;
|
|
1018
|
+
if (core.types.isObjectProperty(parent) && parent.key === node && !parent.computed) return false;
|
|
1028
1019
|
if (core.types.isLabeledStatement(parent) && parent.label === node) return false;
|
|
1029
1020
|
if ((core.types.isBreakStatement(parent) || core.types.isContinueStatement(parent)) && parent.label === node) {
|
|
1030
1021
|
return false;
|
|
@@ -1033,38 +1024,27 @@ function shouldProcessIdentifier(path, parentPath) {
|
|
|
1033
1024
|
}
|
|
1034
1025
|
function isAlreadyValueAccess(path) {
|
|
1035
1026
|
const parent = path.parent;
|
|
1036
|
-
if (
|
|
1027
|
+
if (isMemberLike(parent) && parent.object === path.node) {
|
|
1037
1028
|
return isMemberAccessingProperty(parent, "value");
|
|
1038
1029
|
}
|
|
1039
1030
|
if (!core.types.isParenthesizedExpression(parent) && !core.types.isTSAsExpression(parent) && !core.types.isTSNonNullExpression(parent)) {
|
|
1040
1031
|
return false;
|
|
1041
1032
|
}
|
|
1042
1033
|
const ancestor = path.findParent((p2) => {
|
|
1043
|
-
if (!p2.
|
|
1044
|
-
|
|
1045
|
-
return m.object === path.node && isMemberAccessingProperty(m, "value");
|
|
1034
|
+
if (!isMemberLike(p2.node)) return false;
|
|
1035
|
+
return isMemberAccessingProperty(p2.node, "value");
|
|
1046
1036
|
});
|
|
1047
1037
|
return !!ancestor;
|
|
1048
1038
|
}
|
|
1049
1039
|
function symbolAssignment(path) {
|
|
1050
1040
|
const { left } = path.node;
|
|
1051
|
-
if (!core.types.isIdentifier(left)) return;
|
|
1052
|
-
|
|
1053
|
-
if (isAlreadyValueAssignment(left)) return;
|
|
1054
|
-
path.node.left = core.types.memberExpression(core.types.identifier(left.name), core.types.identifier("value"));
|
|
1055
|
-
}
|
|
1056
|
-
function isAlreadyValueAssignment(left) {
|
|
1057
|
-
return core.types.isMemberExpression(left) && isMemberAccessingProperty(left, "value");
|
|
1041
|
+
if (!core.types.isIdentifier(left) || !isSignal(left.name)) return;
|
|
1042
|
+
path.node.left = valueAccess(core.types.identifier(left.name));
|
|
1058
1043
|
}
|
|
1059
1044
|
function symbolUpdate(path) {
|
|
1060
1045
|
const { argument } = path.node;
|
|
1061
|
-
if (!core.types.isIdentifier(argument)) return;
|
|
1062
|
-
|
|
1063
|
-
if (isAlreadyValueUpdate(argument)) return;
|
|
1064
|
-
path.node.argument = core.types.memberExpression(core.types.identifier(argument.name), core.types.identifier("value"));
|
|
1065
|
-
}
|
|
1066
|
-
function isAlreadyValueUpdate(argument) {
|
|
1067
|
-
return core.types.isMemberExpression(argument) && isMemberAccessingProperty(argument, "value");
|
|
1046
|
+
if (!core.types.isIdentifier(argument) || !isSignal(argument.name)) return;
|
|
1047
|
+
path.node.argument = valueAccess(core.types.identifier(argument.name));
|
|
1068
1048
|
}
|
|
1069
1049
|
var symbolVisitors = {
|
|
1070
1050
|
VariableDeclarator: replaceSymbol,
|
|
@@ -1072,114 +1052,174 @@ var symbolVisitors = {
|
|
|
1072
1052
|
AssignmentExpression: symbolAssignment,
|
|
1073
1053
|
UpdateExpression: symbolUpdate
|
|
1074
1054
|
};
|
|
1075
|
-
function
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1055
|
+
function clone2(node) {
|
|
1056
|
+
return core.types.cloneNode(node, true);
|
|
1057
|
+
}
|
|
1058
|
+
function applyDefault2(access, def) {
|
|
1059
|
+
return core.types.conditionalExpression(
|
|
1060
|
+
core.types.binaryExpression("===", clone2(access), core.types.identifier("undefined")),
|
|
1061
|
+
def,
|
|
1062
|
+
clone2(access)
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
1065
|
+
function resolveKey(prop) {
|
|
1066
|
+
const key = prop.key;
|
|
1067
|
+
if (prop.computed) {
|
|
1068
|
+
return { keyExpr: key, computed: true, excludeKey: key };
|
|
1069
|
+
}
|
|
1070
|
+
if (core.types.isIdentifier(key)) {
|
|
1071
|
+
return {
|
|
1072
|
+
keyExpr: core.types.identifier(key.name),
|
|
1073
|
+
computed: false,
|
|
1074
|
+
excludeKey: core.types.stringLiteral(key.name)
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
if (core.types.isStringLiteral(key)) {
|
|
1078
|
+
return {
|
|
1079
|
+
keyExpr: core.types.stringLiteral(key.value),
|
|
1080
|
+
computed: true,
|
|
1081
|
+
excludeKey: core.types.stringLiteral(key.value)
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
if (core.types.isNumericLiteral(key)) {
|
|
1085
|
+
return {
|
|
1086
|
+
keyExpr: core.types.numericLiteral(key.value),
|
|
1087
|
+
computed: true,
|
|
1088
|
+
excludeKey: core.types.stringLiteral(String(key.value))
|
|
1089
|
+
};
|
|
1090
|
+
}
|
|
1091
|
+
return { keyExpr: key, computed: true, excludeKey: null };
|
|
1092
|
+
}
|
|
1093
|
+
function restName(argument) {
|
|
1094
|
+
return core.types.isIdentifier(argument) ? argument.name : null;
|
|
1095
|
+
}
|
|
1096
|
+
function buildObjectRestInit(base, excludeKeys) {
|
|
1097
|
+
if (excludeKeys.length === 0) return clone2(base);
|
|
1098
|
+
return core.types.callExpression(useImport(importMap.omitProps), [
|
|
1099
|
+
clone2(base),
|
|
1100
|
+
core.types.arrayExpression(excludeKeys.map((key) => clone2(key)))
|
|
1112
1101
|
]);
|
|
1113
|
-
return core.types.variableDeclaration("const", [core.types.variableDeclarator(core.types.identifier(restName), init)]);
|
|
1114
1102
|
}
|
|
1115
|
-
function
|
|
1116
|
-
if (
|
|
1117
|
-
|
|
1118
|
-
if (notRestNames.length === 0) {
|
|
1119
|
-
path.node.params[0] = core.types.identifier(restName);
|
|
1103
|
+
function collectTarget(target, access, leaves, rests) {
|
|
1104
|
+
if (core.types.isAssignmentPattern(target)) {
|
|
1105
|
+
collectTarget(target.left, applyDefault2(access, target.right), leaves, rests);
|
|
1120
1106
|
return;
|
|
1121
1107
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1108
|
+
if (core.types.isIdentifier(target)) {
|
|
1109
|
+
leaves.push({ name: target.name, accessor: access });
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
if (core.types.isObjectPattern(target)) {
|
|
1113
|
+
collectObjectPattern(target, access, leaves, rests);
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
if (core.types.isArrayPattern(target)) {
|
|
1117
|
+
collectArrayPattern(target, access, leaves, rests);
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function collectObjectPattern(pattern, base, leaves, rests) {
|
|
1122
|
+
const excludeKeys = [];
|
|
1123
|
+
for (const prop of pattern.properties) {
|
|
1124
|
+
if (core.types.isRestElement(prop)) {
|
|
1125
|
+
const name = restName(prop.argument);
|
|
1126
|
+
if (name) rests.push({ name, init: buildObjectRestInit(base, excludeKeys) });
|
|
1137
1127
|
continue;
|
|
1138
1128
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1129
|
+
const { keyExpr, computed, excludeKey } = resolveKey(prop);
|
|
1130
|
+
if (excludeKey !== null) excludeKeys.push(excludeKey);
|
|
1131
|
+
const access = core.types.memberExpression(clone2(base), keyExpr, computed);
|
|
1132
|
+
collectTarget(prop.value, access, leaves, rests);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
function spreadOf(base) {
|
|
1136
|
+
return core.types.arrayExpression([core.types.spreadElement(clone2(base))]);
|
|
1137
|
+
}
|
|
1138
|
+
function collectArrayPattern(pattern, base, leaves, rests) {
|
|
1139
|
+
pattern.elements.forEach((element, index) => {
|
|
1140
|
+
if (element == null) return;
|
|
1141
|
+
if (core.types.isRestElement(element)) {
|
|
1142
|
+
const name = restName(element.argument);
|
|
1143
|
+
if (name) {
|
|
1144
|
+
const init = core.types.callExpression(core.types.memberExpression(spreadOf(base), core.types.identifier("slice")), [
|
|
1145
|
+
core.types.numericLiteral(index)
|
|
1146
|
+
]);
|
|
1147
|
+
rests.push({ name, init });
|
|
1148
|
+
}
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
const access = core.types.memberExpression(spreadOf(base), core.types.numericLiteral(index), true);
|
|
1152
|
+
collectTarget(element, access, leaves, rests);
|
|
1153
|
+
});
|
|
1142
1154
|
}
|
|
1143
|
-
function
|
|
1144
|
-
path.node.
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
])
|
|
1158
|
-
)
|
|
1159
|
-
)
|
|
1160
|
-
);
|
|
1155
|
+
function ensureBlockBody(path) {
|
|
1156
|
+
if (core.types.isBlockStatement(path.node.body)) return path.node.body;
|
|
1157
|
+
const block = core.types.blockStatement([core.types.returnStatement(path.node.body)]);
|
|
1158
|
+
path.node.body = block;
|
|
1159
|
+
return block;
|
|
1160
|
+
}
|
|
1161
|
+
function isInsidePattern(ref, pattern) {
|
|
1162
|
+
return !!ref.findParent((p2) => p2.node === pattern);
|
|
1163
|
+
}
|
|
1164
|
+
function accessorFor(pattern, base, name) {
|
|
1165
|
+
var _a;
|
|
1166
|
+
const leaves = [];
|
|
1167
|
+
collectObjectPattern(pattern, base, leaves, []);
|
|
1168
|
+
return (_a = leaves.find((leaf) => leaf.name === name)) == null ? void 0 : _a.accessor;
|
|
1161
1169
|
}
|
|
1162
1170
|
function transformFnProps(path) {
|
|
1171
|
+
var _a, _b;
|
|
1163
1172
|
const firstParam = path.node.params[0];
|
|
1164
1173
|
if (!firstParam || !core.types.isObjectPattern(firstParam) || !checkHasJSXReturn(path)) return;
|
|
1165
1174
|
const ctx = getCompileContext();
|
|
1166
|
-
const properties = firstParam.properties;
|
|
1167
1175
|
const signalPrefix = ctx.options.signalPrefix || "$";
|
|
1168
|
-
const
|
|
1169
|
-
const
|
|
1170
|
-
const
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
buildDefaultValue(path, defaults);
|
|
1176
|
+
const base = core.types.identifier(TRANSFORM_PROPERTY_NAME);
|
|
1177
|
+
const initLeaves = [];
|
|
1178
|
+
const initRests = [];
|
|
1179
|
+
collectObjectPattern(firstParam, base, initLeaves, initRests);
|
|
1180
|
+
const conflicts = [...initLeaves, ...initRests].map((b2) => b2.name).filter((name) => x(name, signalPrefix));
|
|
1181
|
+
if (conflicts.length > 0) {
|
|
1182
|
+
re("transformProps", "Property names cannot start with signal prefix", conflicts);
|
|
1183
|
+
}
|
|
1184
|
+
if (initLeaves.length === 0 && initRests.length === 1 && firstParam.properties.length === 1 && core.types.isRestElement(firstParam.properties[0])) {
|
|
1185
|
+
path.node.params[0] = core.types.identifier(initRests[0].name);
|
|
1179
1186
|
return;
|
|
1180
1187
|
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1188
|
+
const inPatternRefs = /* @__PURE__ */ new Map();
|
|
1189
|
+
const bodyRefs = /* @__PURE__ */ new Map();
|
|
1190
|
+
for (const leaf of initLeaves) {
|
|
1191
|
+
const inside = [];
|
|
1192
|
+
const outside = [];
|
|
1193
|
+
for (const ref of (_b = (_a = path.scope.getBinding(leaf.name)) == null ? void 0 : _a.referencePaths) != null ? _b : []) {
|
|
1194
|
+
(isInsidePattern(ref, firstParam) ? inside : outside).push(ref);
|
|
1195
|
+
}
|
|
1196
|
+
if (inside.length > 0) inPatternRefs.set(leaf.name, inside);
|
|
1197
|
+
bodyRefs.set(leaf.name, outside);
|
|
1198
|
+
}
|
|
1199
|
+
for (const leaf of initLeaves) {
|
|
1200
|
+
const refs = inPatternRefs.get(leaf.name);
|
|
1201
|
+
if (!refs) continue;
|
|
1202
|
+
const accessor = accessorFor(firstParam, base, leaf.name);
|
|
1203
|
+
if (!accessor) continue;
|
|
1204
|
+
for (const ref of refs) ref.replaceWith(clone2(accessor));
|
|
1205
|
+
}
|
|
1206
|
+
const leaves = [];
|
|
1207
|
+
const rests = [];
|
|
1208
|
+
collectObjectPattern(firstParam, base, leaves, rests);
|
|
1209
|
+
const accessorByName = new Map(leaves.map((leaf) => [leaf.name, leaf.accessor]));
|
|
1210
|
+
path.node.params[0] = core.types.identifier(TRANSFORM_PROPERTY_NAME);
|
|
1211
|
+
for (const [name, refs] of bodyRefs) {
|
|
1212
|
+
const accessor = accessorByName.get(name);
|
|
1213
|
+
if (!accessor) continue;
|
|
1214
|
+
for (const ref of refs) ref.replaceWith(clone2(accessor));
|
|
1215
|
+
}
|
|
1216
|
+
if (rests.length > 0) {
|
|
1217
|
+
const body = ensureBlockBody(path);
|
|
1218
|
+
const declarations = rests.map(
|
|
1219
|
+
(rest) => core.types.variableDeclaration("const", [core.types.variableDeclarator(core.types.identifier(rest.name), rest.init)])
|
|
1220
|
+
);
|
|
1221
|
+
body.body.unshift(...declarations);
|
|
1222
|
+
}
|
|
1183
1223
|
}
|
|
1184
1224
|
var propsVisitors = {
|
|
1185
1225
|
FunctionDeclaration: transformFnProps,
|
|
@@ -1595,8 +1635,8 @@ function setNodeText(path, text) {
|
|
|
1595
1635
|
function createComponentPropKey(name, forceStringLiteralKeys = false) {
|
|
1596
1636
|
return forceStringLiteralKeys || !core.types.isValidIdentifier(name) ? core.types.stringLiteral(name) : core.types.identifier(name);
|
|
1597
1637
|
}
|
|
1598
|
-
function cloneValue(value,
|
|
1599
|
-
return
|
|
1638
|
+
function cloneValue(value, clone3 = false) {
|
|
1639
|
+
return clone3 ? core.types.cloneNode(value, true) : value;
|
|
1600
1640
|
}
|
|
1601
1641
|
function createPropNode(name, value, kind, options) {
|
|
1602
1642
|
const key = createComponentPropKey(name, options.forceStringLiteralKeys);
|
|
@@ -1742,44 +1782,6 @@ var SERVER_TEXT_ESCAPES = {
|
|
|
1742
1782
|
">": ">"
|
|
1743
1783
|
};
|
|
1744
1784
|
var serverTextEscapeRE = /[&<>]/g;
|
|
1745
|
-
function markSafeHtmlCall(expression) {
|
|
1746
|
-
return core.types.callExpression(useImport("markAsRawHtml"), [expression]);
|
|
1747
|
-
}
|
|
1748
|
-
function isGeneratedServerHtmlCall(expression) {
|
|
1749
|
-
const { importIdentifiers } = getCompileContext();
|
|
1750
|
-
const renderId = importIdentifiers.render;
|
|
1751
|
-
const componentId = importIdentifiers.createComponent;
|
|
1752
|
-
return core.types.isIdentifier(expression.callee) && (expression.callee.name === renderId.name || expression.callee.name === componentId.name);
|
|
1753
|
-
}
|
|
1754
|
-
function visitServerHtmlSubexpressions(node) {
|
|
1755
|
-
var _a;
|
|
1756
|
-
if (!node) return node;
|
|
1757
|
-
if (core.types.isJSXElement(node) || core.types.isJSXFragment(node)) {
|
|
1758
|
-
return markSafeHtmlCall(node);
|
|
1759
|
-
}
|
|
1760
|
-
if (core.types.isCallExpression(node) && isGeneratedServerHtmlCall(node)) {
|
|
1761
|
-
return markSafeHtmlCall(node);
|
|
1762
|
-
}
|
|
1763
|
-
const keys = (_a = core.types.VISITOR_KEYS[node.type]) != null ? _a : [];
|
|
1764
|
-
for (const key of keys) {
|
|
1765
|
-
const record = node;
|
|
1766
|
-
const value = record[key];
|
|
1767
|
-
if (Array.isArray(value)) {
|
|
1768
|
-
for (let i2 = 0; i2 < value.length; i2++) {
|
|
1769
|
-
const child = value[i2];
|
|
1770
|
-
if (child && typeof child === "object" && "type" in child) {
|
|
1771
|
-
value[i2] = visitServerHtmlSubexpressions(child);
|
|
1772
|
-
}
|
|
1773
|
-
}
|
|
1774
|
-
} else if (value && typeof value === "object" && "type" in value) {
|
|
1775
|
-
record[key] = visitServerHtmlSubexpressions(value);
|
|
1776
|
-
}
|
|
1777
|
-
}
|
|
1778
|
-
return node;
|
|
1779
|
-
}
|
|
1780
|
-
function markServerHtmlSubexpressions(expression) {
|
|
1781
|
-
return visitServerHtmlSubexpressions(expression);
|
|
1782
|
-
}
|
|
1783
1785
|
function escapeServerTemplateText(value) {
|
|
1784
1786
|
return value.replaceAll(serverTextEscapeRE, (char) => {
|
|
1785
1787
|
var _a;
|
|
@@ -1864,14 +1866,50 @@ function createSSRSelectedExpression(selectValue, optionValue) {
|
|
|
1864
1866
|
core.types.cloneNode(optionValue, true)
|
|
1865
1867
|
]);
|
|
1866
1868
|
}
|
|
1869
|
+
function isSafeServerHtml(expr) {
|
|
1870
|
+
if (core.types.isJSXElement(expr) || core.types.isJSXFragment(expr)) return true;
|
|
1871
|
+
if (!core.types.isCallExpression(expr) || !core.types.isIdentifier(expr.callee)) return false;
|
|
1872
|
+
const { importIdentifiers } = getCompileContext();
|
|
1873
|
+
return expr.callee.name === importIdentifiers.render.name || expr.callee.name === importIdentifiers.createComponent.name;
|
|
1874
|
+
}
|
|
1875
|
+
function escapeChildExpression(expr) {
|
|
1876
|
+
if (isSafeServerHtml(expr)) {
|
|
1877
|
+
return expr;
|
|
1878
|
+
}
|
|
1879
|
+
if (core.types.isParenthesizedExpression(expr)) {
|
|
1880
|
+
expr.expression = escapeChildExpression(expr.expression);
|
|
1881
|
+
return expr;
|
|
1882
|
+
}
|
|
1883
|
+
if (core.types.isLogicalExpression(expr)) {
|
|
1884
|
+
if (expr.operator === "&&") {
|
|
1885
|
+
expr.right = escapeChildExpression(expr.right);
|
|
1886
|
+
} else {
|
|
1887
|
+
expr.left = escapeChildExpression(expr.left);
|
|
1888
|
+
expr.right = escapeChildExpression(expr.right);
|
|
1889
|
+
}
|
|
1890
|
+
return expr;
|
|
1891
|
+
}
|
|
1892
|
+
if (core.types.isConditionalExpression(expr)) {
|
|
1893
|
+
expr.consequent = escapeChildExpression(expr.consequent);
|
|
1894
|
+
expr.alternate = escapeChildExpression(expr.alternate);
|
|
1895
|
+
return expr;
|
|
1896
|
+
}
|
|
1897
|
+
if (core.types.isSequenceExpression(expr) && expr.expressions.length > 0) {
|
|
1898
|
+
const last = expr.expressions.length - 1;
|
|
1899
|
+
expr.expressions[last] = escapeChildExpression(expr.expressions[last]);
|
|
1900
|
+
return expr;
|
|
1901
|
+
}
|
|
1902
|
+
return core.types.callExpression(useImport("escape"), [expr]);
|
|
1903
|
+
}
|
|
1867
1904
|
function generateServerNode(node, ctx, withHydrationKey = true) {
|
|
1868
1905
|
switch (node.type) {
|
|
1869
1906
|
case 2 /* TEXT */:
|
|
1870
1907
|
return core.types.stringLiteral(node.value);
|
|
1871
1908
|
case 3 /* EXPRESSION */: {
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1909
|
+
if (node.asRawChildren) {
|
|
1910
|
+
return core.types.cloneNode(node.value, true);
|
|
1911
|
+
}
|
|
1912
|
+
return escapeChildExpression(core.types.cloneNode(node.value, true));
|
|
1875
1913
|
}
|
|
1876
1914
|
case 1 /* COMPONENT */:
|
|
1877
1915
|
return generateServerComponent(node, ctx);
|