babel-plugin-essor 0.0.16 → 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 +407 -225
- package/dist/index.js +407 -225
- 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,29 +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
|
-
|
|
836
|
-
|
|
819
|
+
return !!name && x(name, getSignalPrefix());
|
|
820
|
+
}
|
|
821
|
+
function stripSignalPrefix(name) {
|
|
822
|
+
const prefix = getSignalPrefix();
|
|
823
|
+
return x(name, prefix) ? name.slice(prefix.length) : name;
|
|
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)]);
|
|
837
842
|
}
|
|
838
843
|
function replaceSymbol(path) {
|
|
839
844
|
const { init, id } = path.node;
|
|
840
|
-
if (
|
|
841
|
-
|
|
842
|
-
|
|
845
|
+
if (core.types.isObjectPattern(id) || core.types.isArrayPattern(id)) {
|
|
846
|
+
transformDestructuring(path);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (!core.types.isIdentifier(id) || !isSignal(id.name) || isAlreadySignalCall(init)) return;
|
|
843
850
|
const isComputed = isFunctionLikeExpression(init) && path.parent.kind === "const";
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
851
|
+
path.node.init = core.types.callExpression(
|
|
852
|
+
useImport(isComputed ? "computed" : "signal"),
|
|
853
|
+
init ? [init] : []
|
|
854
|
+
);
|
|
847
855
|
}
|
|
848
856
|
function isAlreadySignalCall(init) {
|
|
849
857
|
if (!init || !core.types.isCallExpression(init) || !core.types.isIdentifier(init.callee)) return false;
|
|
@@ -852,24 +860,151 @@ function isAlreadySignalCall(init) {
|
|
|
852
860
|
const { importIdentifiers } = getCompileContext();
|
|
853
861
|
return calleeName === importIdentifiers.signal.name || calleeName === importIdentifiers.computed.name;
|
|
854
862
|
}
|
|
863
|
+
function transformDestructuring(path) {
|
|
864
|
+
var _a;
|
|
865
|
+
const { init, id } = path.node;
|
|
866
|
+
const pattern = id;
|
|
867
|
+
if (!init) {
|
|
868
|
+
if (patternHasSignalBinding(pattern)) path.skip();
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
if (!isSignalSource(init) && !patternHasSignalBinding(pattern)) return;
|
|
872
|
+
const declPath = path.parentPath;
|
|
873
|
+
if (!declPath || !declPath.isVariableDeclaration()) return;
|
|
874
|
+
if ((_a = declPath.parentPath) == null ? void 0 : _a.isForStatement()) {
|
|
875
|
+
path.skip();
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const declarators = bindPattern(pattern, signalSourceBase(init));
|
|
879
|
+
const declaration = declPath.node;
|
|
880
|
+
const index = declaration.declarations.indexOf(path.node);
|
|
881
|
+
declaration.declarations.splice(index, 1, ...declarators);
|
|
882
|
+
path.skip();
|
|
883
|
+
}
|
|
884
|
+
function isSignalSource(init) {
|
|
885
|
+
if (core.types.isIdentifier(init)) return isSignal(init.name);
|
|
886
|
+
if (isMemberLike(init) && core.types.isIdentifier(init.object) && isSignal(init.object.name)) {
|
|
887
|
+
return isMemberAccessingProperty(init, "value");
|
|
888
|
+
}
|
|
889
|
+
return false;
|
|
890
|
+
}
|
|
891
|
+
function signalSourceBase(init) {
|
|
892
|
+
if (core.types.isIdentifier(init) && isSignal(init.name)) {
|
|
893
|
+
return valueAccess(core.types.identifier(init.name));
|
|
894
|
+
}
|
|
895
|
+
return clone(init);
|
|
896
|
+
}
|
|
897
|
+
function patternHasSignalBinding(node) {
|
|
898
|
+
if (core.types.isIdentifier(node)) return isSignal(node.name);
|
|
899
|
+
if (core.types.isObjectPattern(node)) {
|
|
900
|
+
return node.properties.some(
|
|
901
|
+
(p2) => core.types.isRestElement(p2) ? patternHasSignalBinding(p2.argument) : patternHasSignalBinding(p2.value)
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
if (core.types.isArrayPattern(node)) {
|
|
905
|
+
return node.elements.some((el) => el != null && patternHasSignalBinding(el));
|
|
906
|
+
}
|
|
907
|
+
if (core.types.isAssignmentPattern(node)) return patternHasSignalBinding(node.left);
|
|
908
|
+
if (core.types.isRestElement(node)) return patternHasSignalBinding(node.argument);
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
function bindPattern(pattern, base) {
|
|
912
|
+
return core.types.isObjectPattern(pattern) ? bindObjectPattern(pattern, base) : bindArrayPattern(pattern, base);
|
|
913
|
+
}
|
|
914
|
+
function bindObjectPattern(pattern, base) {
|
|
915
|
+
const out = [];
|
|
916
|
+
const handledKeys = [];
|
|
917
|
+
for (const prop of pattern.properties) {
|
|
918
|
+
if (core.types.isRestElement(prop)) {
|
|
919
|
+
out.push(buildObjectRest(prop, base, handledKeys));
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
const { keyExpr, computed, target } = resolveObjectProp(prop);
|
|
923
|
+
handledKeys.push({ key: clone(keyExpr), computed });
|
|
924
|
+
out.push(...bindTarget(target, core.types.memberExpression(clone(base), keyExpr, computed)));
|
|
925
|
+
}
|
|
926
|
+
return out;
|
|
927
|
+
}
|
|
928
|
+
function resolveObjectProp(prop) {
|
|
929
|
+
const target = prop.value;
|
|
930
|
+
const shorthandId = core.types.isAssignmentPattern(target) ? target.left : target;
|
|
931
|
+
if (prop.shorthand && core.types.isIdentifier(shorthandId) && isSignal(shorthandId.name)) {
|
|
932
|
+
return { keyExpr: core.types.identifier(stripSignalPrefix(shorthandId.name)), computed: false, target };
|
|
933
|
+
}
|
|
934
|
+
if (prop.computed) {
|
|
935
|
+
return { keyExpr: prop.key, computed: true, target };
|
|
936
|
+
}
|
|
937
|
+
return { keyExpr: prop.key, computed: !core.types.isIdentifier(prop.key), target };
|
|
938
|
+
}
|
|
939
|
+
function bindArrayPattern(pattern, base) {
|
|
940
|
+
const out = [];
|
|
941
|
+
pattern.elements.forEach((el, i2) => {
|
|
942
|
+
if (el == null) return;
|
|
943
|
+
if (core.types.isRestElement(el)) {
|
|
944
|
+
const slice = core.types.callExpression(core.types.memberExpression(clone(base), core.types.identifier("slice")), [
|
|
945
|
+
core.types.numericLiteral(i2)
|
|
946
|
+
]);
|
|
947
|
+
out.push(...bindTarget(el.argument, slice));
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
out.push(...bindTarget(el, core.types.memberExpression(clone(base), core.types.numericLiteral(i2), true)));
|
|
951
|
+
});
|
|
952
|
+
return out;
|
|
953
|
+
}
|
|
954
|
+
function bindTarget(target, access) {
|
|
955
|
+
if (core.types.isAssignmentPattern(target)) {
|
|
956
|
+
return bindTarget(target.left, applyDefault(access, target.right));
|
|
957
|
+
}
|
|
958
|
+
if (core.types.isIdentifier(target)) {
|
|
959
|
+
const value = isSignal(target.name) ? makeComputed(access) : access;
|
|
960
|
+
return [core.types.variableDeclarator(core.types.identifier(target.name), value)];
|
|
961
|
+
}
|
|
962
|
+
if (core.types.isObjectPattern(target)) return bindObjectPattern(target, access);
|
|
963
|
+
if (core.types.isArrayPattern(target)) return bindArrayPattern(target, access);
|
|
964
|
+
return [core.types.variableDeclarator(target, access)];
|
|
965
|
+
}
|
|
966
|
+
function applyDefault(access, def) {
|
|
967
|
+
return core.types.conditionalExpression(
|
|
968
|
+
core.types.binaryExpression("===", clone(access), core.types.identifier("undefined")),
|
|
969
|
+
def,
|
|
970
|
+
clone(access)
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
function buildObjectRest(rest, base, handledKeys) {
|
|
974
|
+
const omits = (target) => core.types.objectPattern([
|
|
975
|
+
...handledKeys.map(({ key, computed }) => core.types.objectProperty(key, genUid("_omit$"), computed)),
|
|
976
|
+
core.types.restElement(target)
|
|
977
|
+
]);
|
|
978
|
+
const arg = rest.argument;
|
|
979
|
+
if (core.types.isIdentifier(arg) && isSignal(arg.name)) {
|
|
980
|
+
const inner = genUid("_rest$");
|
|
981
|
+
const body = core.types.blockStatement([
|
|
982
|
+
core.types.variableDeclaration("const", [core.types.variableDeclarator(omits(inner), clone(base))]),
|
|
983
|
+
core.types.returnStatement(clone(inner))
|
|
984
|
+
]);
|
|
985
|
+
return core.types.variableDeclarator(core.types.identifier(arg.name), makeComputed(body));
|
|
986
|
+
}
|
|
987
|
+
return core.types.variableDeclarator(omits(arg), clone(base));
|
|
988
|
+
}
|
|
855
989
|
function symbolIdentifier(path) {
|
|
856
|
-
const name = path.node
|
|
990
|
+
const { name } = path.node;
|
|
857
991
|
if (!isSignal(name)) return;
|
|
858
|
-
if (!shouldProcessIdentifier(path
|
|
992
|
+
if (!shouldProcessIdentifier(path)) return;
|
|
859
993
|
if (isAlreadyValueAccess(path)) return;
|
|
860
994
|
const parent = path.parent;
|
|
861
|
-
if (
|
|
862
|
-
path.replaceWith(
|
|
995
|
+
if (isMemberLike(parent) && parent.property === path.node && !parent.computed) return;
|
|
996
|
+
path.replaceWith(valueAccess(core.types.identifier(name)));
|
|
863
997
|
path.skip();
|
|
864
998
|
}
|
|
865
|
-
function shouldProcessIdentifier(path
|
|
999
|
+
function shouldProcessIdentifier(path) {
|
|
1000
|
+
const parentPath = path.parentPath;
|
|
866
1001
|
if (!parentPath) return false;
|
|
867
1002
|
const parent = parentPath.node;
|
|
868
1003
|
const node = path.node;
|
|
869
|
-
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)) {
|
|
870
1005
|
return false;
|
|
871
1006
|
}
|
|
872
|
-
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)) {
|
|
873
1008
|
return false;
|
|
874
1009
|
}
|
|
875
1010
|
if (core.types.isFunctionDeclaration(parent) || core.types.isFunctionExpression(parent)) {
|
|
@@ -880,7 +1015,7 @@ function shouldProcessIdentifier(path, parentPath) {
|
|
|
880
1015
|
}
|
|
881
1016
|
if (core.types.isClassDeclaration(parent) && parent.id === node) return false;
|
|
882
1017
|
if (core.types.isObjectMethod(parent) || core.types.isClassMethod(parent)) return false;
|
|
883
|
-
if (core.types.isObjectProperty(parent) && parent.key === node) return false;
|
|
1018
|
+
if (core.types.isObjectProperty(parent) && parent.key === node && !parent.computed) return false;
|
|
884
1019
|
if (core.types.isLabeledStatement(parent) && parent.label === node) return false;
|
|
885
1020
|
if ((core.types.isBreakStatement(parent) || core.types.isContinueStatement(parent)) && parent.label === node) {
|
|
886
1021
|
return false;
|
|
@@ -889,38 +1024,27 @@ function shouldProcessIdentifier(path, parentPath) {
|
|
|
889
1024
|
}
|
|
890
1025
|
function isAlreadyValueAccess(path) {
|
|
891
1026
|
const parent = path.parent;
|
|
892
|
-
if (
|
|
1027
|
+
if (isMemberLike(parent) && parent.object === path.node) {
|
|
893
1028
|
return isMemberAccessingProperty(parent, "value");
|
|
894
1029
|
}
|
|
895
1030
|
if (!core.types.isParenthesizedExpression(parent) && !core.types.isTSAsExpression(parent) && !core.types.isTSNonNullExpression(parent)) {
|
|
896
1031
|
return false;
|
|
897
1032
|
}
|
|
898
1033
|
const ancestor = path.findParent((p2) => {
|
|
899
|
-
if (!p2.
|
|
900
|
-
|
|
901
|
-
return m.object === path.node && isMemberAccessingProperty(m, "value");
|
|
1034
|
+
if (!isMemberLike(p2.node)) return false;
|
|
1035
|
+
return isMemberAccessingProperty(p2.node, "value");
|
|
902
1036
|
});
|
|
903
1037
|
return !!ancestor;
|
|
904
1038
|
}
|
|
905
1039
|
function symbolAssignment(path) {
|
|
906
1040
|
const { left } = path.node;
|
|
907
|
-
if (!core.types.isIdentifier(left)) return;
|
|
908
|
-
|
|
909
|
-
if (isAlreadyValueAssignment(left)) return;
|
|
910
|
-
path.node.left = core.types.memberExpression(core.types.identifier(left.name), core.types.identifier("value"));
|
|
911
|
-
}
|
|
912
|
-
function isAlreadyValueAssignment(left) {
|
|
913
|
-
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));
|
|
914
1043
|
}
|
|
915
1044
|
function symbolUpdate(path) {
|
|
916
1045
|
const { argument } = path.node;
|
|
917
|
-
if (!core.types.isIdentifier(argument)) return;
|
|
918
|
-
|
|
919
|
-
if (isAlreadyValueUpdate(argument)) return;
|
|
920
|
-
path.node.argument = core.types.memberExpression(core.types.identifier(argument.name), core.types.identifier("value"));
|
|
921
|
-
}
|
|
922
|
-
function isAlreadyValueUpdate(argument) {
|
|
923
|
-
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));
|
|
924
1048
|
}
|
|
925
1049
|
var symbolVisitors = {
|
|
926
1050
|
VariableDeclarator: replaceSymbol,
|
|
@@ -928,114 +1052,174 @@ var symbolVisitors = {
|
|
|
928
1052
|
AssignmentExpression: symbolAssignment,
|
|
929
1053
|
UpdateExpression: symbolUpdate
|
|
930
1054
|
};
|
|
931
|
-
function
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
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)))
|
|
968
1101
|
]);
|
|
969
|
-
return core.types.variableDeclaration("const", [core.types.variableDeclarator(core.types.identifier(restName), init)]);
|
|
970
1102
|
}
|
|
971
|
-
function
|
|
972
|
-
if (
|
|
973
|
-
|
|
974
|
-
if (notRestNames.length === 0) {
|
|
975
|
-
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);
|
|
976
1106
|
return;
|
|
977
1107
|
}
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
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) });
|
|
993
1127
|
continue;
|
|
994
1128
|
}
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
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
|
+
});
|
|
998
1154
|
}
|
|
999
|
-
function
|
|
1000
|
-
path.node.
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
])
|
|
1014
|
-
)
|
|
1015
|
-
)
|
|
1016
|
-
);
|
|
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;
|
|
1017
1169
|
}
|
|
1018
1170
|
function transformFnProps(path) {
|
|
1171
|
+
var _a, _b;
|
|
1019
1172
|
const firstParam = path.node.params[0];
|
|
1020
1173
|
if (!firstParam || !core.types.isObjectPattern(firstParam) || !checkHasJSXReturn(path)) return;
|
|
1021
1174
|
const ctx = getCompileContext();
|
|
1022
|
-
const properties = firstParam.properties;
|
|
1023
1175
|
const signalPrefix = ctx.options.signalPrefix || "$";
|
|
1024
|
-
const
|
|
1025
|
-
const
|
|
1026
|
-
const
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
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);
|
|
1035
1186
|
return;
|
|
1036
1187
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
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
|
+
}
|
|
1039
1223
|
}
|
|
1040
1224
|
var propsVisitors = {
|
|
1041
1225
|
FunctionDeclaration: transformFnProps,
|
|
@@ -1451,8 +1635,8 @@ function setNodeText(path, text) {
|
|
|
1451
1635
|
function createComponentPropKey(name, forceStringLiteralKeys = false) {
|
|
1452
1636
|
return forceStringLiteralKeys || !core.types.isValidIdentifier(name) ? core.types.stringLiteral(name) : core.types.identifier(name);
|
|
1453
1637
|
}
|
|
1454
|
-
function cloneValue(value,
|
|
1455
|
-
return
|
|
1638
|
+
function cloneValue(value, clone3 = false) {
|
|
1639
|
+
return clone3 ? core.types.cloneNode(value, true) : value;
|
|
1456
1640
|
}
|
|
1457
1641
|
function createPropNode(name, value, kind, options) {
|
|
1458
1642
|
const key = createComponentPropKey(name, options.forceStringLiteralKeys);
|
|
@@ -1598,44 +1782,6 @@ var SERVER_TEXT_ESCAPES = {
|
|
|
1598
1782
|
">": ">"
|
|
1599
1783
|
};
|
|
1600
1784
|
var serverTextEscapeRE = /[&<>]/g;
|
|
1601
|
-
function markSafeHtmlCall(expression) {
|
|
1602
|
-
return core.types.callExpression(useImport("markAsRawHtml"), [expression]);
|
|
1603
|
-
}
|
|
1604
|
-
function isGeneratedServerHtmlCall(expression) {
|
|
1605
|
-
const { importIdentifiers } = getCompileContext();
|
|
1606
|
-
const renderId = importIdentifiers.render;
|
|
1607
|
-
const componentId = importIdentifiers.createComponent;
|
|
1608
|
-
return core.types.isIdentifier(expression.callee) && (expression.callee.name === renderId.name || expression.callee.name === componentId.name);
|
|
1609
|
-
}
|
|
1610
|
-
function visitServerHtmlSubexpressions(node) {
|
|
1611
|
-
var _a;
|
|
1612
|
-
if (!node) return node;
|
|
1613
|
-
if (core.types.isJSXElement(node) || core.types.isJSXFragment(node)) {
|
|
1614
|
-
return markSafeHtmlCall(node);
|
|
1615
|
-
}
|
|
1616
|
-
if (core.types.isCallExpression(node) && isGeneratedServerHtmlCall(node)) {
|
|
1617
|
-
return markSafeHtmlCall(node);
|
|
1618
|
-
}
|
|
1619
|
-
const keys = (_a = core.types.VISITOR_KEYS[node.type]) != null ? _a : [];
|
|
1620
|
-
for (const key of keys) {
|
|
1621
|
-
const record = node;
|
|
1622
|
-
const value = record[key];
|
|
1623
|
-
if (Array.isArray(value)) {
|
|
1624
|
-
for (let i2 = 0; i2 < value.length; i2++) {
|
|
1625
|
-
const child = value[i2];
|
|
1626
|
-
if (child && typeof child === "object" && "type" in child) {
|
|
1627
|
-
value[i2] = visitServerHtmlSubexpressions(child);
|
|
1628
|
-
}
|
|
1629
|
-
}
|
|
1630
|
-
} else if (value && typeof value === "object" && "type" in value) {
|
|
1631
|
-
record[key] = visitServerHtmlSubexpressions(value);
|
|
1632
|
-
}
|
|
1633
|
-
}
|
|
1634
|
-
return node;
|
|
1635
|
-
}
|
|
1636
|
-
function markServerHtmlSubexpressions(expression) {
|
|
1637
|
-
return visitServerHtmlSubexpressions(expression);
|
|
1638
|
-
}
|
|
1639
1785
|
function escapeServerTemplateText(value) {
|
|
1640
1786
|
return value.replaceAll(serverTextEscapeRE, (char) => {
|
|
1641
1787
|
var _a;
|
|
@@ -1720,14 +1866,50 @@ function createSSRSelectedExpression(selectValue, optionValue) {
|
|
|
1720
1866
|
core.types.cloneNode(optionValue, true)
|
|
1721
1867
|
]);
|
|
1722
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
|
+
}
|
|
1723
1904
|
function generateServerNode(node, ctx, withHydrationKey = true) {
|
|
1724
1905
|
switch (node.type) {
|
|
1725
1906
|
case 2 /* TEXT */:
|
|
1726
1907
|
return core.types.stringLiteral(node.value);
|
|
1727
1908
|
case 3 /* EXPRESSION */: {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1909
|
+
if (node.asRawChildren) {
|
|
1910
|
+
return core.types.cloneNode(node.value, true);
|
|
1911
|
+
}
|
|
1912
|
+
return escapeChildExpression(core.types.cloneNode(node.value, true));
|
|
1731
1913
|
}
|
|
1732
1914
|
case 1 /* COMPONENT */:
|
|
1733
1915
|
return generateServerComponent(node, ctx);
|