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.
Files changed (3) hide show
  1. package/dist/index.cjs +407 -225
  2. package/dist/index.js +407 -225
  3. 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
- "toRawHtmlString",
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 SERVER_ONLY_NAMES = [
117
- "render",
118
- "toRawHtmlString",
119
- "toEscapedHtmlString",
120
- "markAsRawHtml",
121
- "escapeHTML",
122
- "Fragment",
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("\0null");
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 serverSpecifiers = [];
261
- const clientSpecifiers = [];
262
- for (const name of ctx.imports) {
263
- const local = ctx.importIdentifiers[name];
264
- const importedName = resolveImportedName(name, mode);
265
- const specifier = core.types.importSpecifier(local, core.types.identifier(importedName));
266
- if (mode === "server" /* SERVER */ && SERVER_EXPORTS.has(importedName)) {
267
- serverSpecifiers.push(specifier);
268
- } else {
269
- clientSpecifiers.push(specifier);
270
- }
271
- }
272
- if (serverSpecifiers.length > 0) {
273
- path.node.body.unshift(core.types.importDeclaration(serverSpecifiers, core.types.stringLiteral("essor/server")));
274
- }
275
- if (clientSpecifiers.length > 0) {
276
- path.node.body.unshift(core.types.importDeclaration(clientSpecifiers, core.types.stringLiteral("essor")));
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
- function isMemberAccessingProperty(node, propertyName) {
825
- if (!node.computed && core.types.isIdentifier(node.property) && node.property.name === propertyName) {
826
- return true;
827
- }
828
- if (node.computed && core.types.isStringLiteral(node.property) && node.property.value === propertyName) {
829
- return true;
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 false;
816
+ return cachedPrefix;
832
817
  }
833
818
  function isSignal(name) {
834
- var _a;
835
- const prefix = (_a = getCompileContext().options.signalPrefix) != null ? _a : "$";
836
- return !!name && x(name, prefix);
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 (!core.types.isIdentifier(id)) return;
841
- if (!isSignal(id.name)) return;
842
- if (isAlreadySignalCall(init)) return;
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
- const importName = isComputed ? "computed" : "signal";
845
- const args = init ? [init] : [];
846
- path.node.init = core.types.callExpression(useImport(importName), args);
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.name;
990
+ const { name } = path.node;
857
991
  if (!isSignal(name)) return;
858
- if (!shouldProcessIdentifier(path, path.parentPath)) return;
992
+ if (!shouldProcessIdentifier(path)) return;
859
993
  if (isAlreadyValueAccess(path)) return;
860
994
  const parent = path.parent;
861
- if (core.types.isMemberExpression(parent) && parent.property === path.node) return;
862
- path.replaceWith(core.types.memberExpression(core.types.identifier(name), core.types.identifier("value")));
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, parentPath) {
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 (core.types.isMemberExpression(parent) && parent.object === path.node) {
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.isMemberExpression()) return false;
900
- const m = p2.node;
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
- if (!isSignal(left.name)) return;
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
- if (!isSignal(argument.name)) return;
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 transformProperty(path, properties, parentPath, defaults = {}) {
932
- for (const property of properties) {
933
- if (!core.types.isObjectProperty(property)) continue;
934
- const key = property.key;
935
- const keyName = core.types.isIdentifier(key) ? key.name : core.types.isStringLiteral(key) ? key.value : "";
936
- if (!keyName) continue;
937
- const value = property.value;
938
- const childPath = `${parentPath}.${keyName}`;
939
- if (core.types.isAssignmentPattern(value)) {
940
- const left = value.left;
941
- if (core.types.isIdentifier(left)) {
942
- defaults[keyName] = value.right;
943
- path.scope.rename(left.name, childPath);
944
- } else if (core.types.isObjectPattern(left)) {
945
- transformProperty(path, left.properties, childPath, defaults);
946
- defaults[keyName] = value.right;
947
- }
948
- } else if (core.types.isIdentifier(value)) {
949
- path.scope.rename(value.name, childPath);
950
- } else if (core.types.isObjectPattern(value)) {
951
- const nested = {};
952
- transformProperty(path, value.properties, childPath, nested);
953
- if (Object.keys(nested).length > 0) defaults[keyName] = nested;
954
- }
955
- }
956
- return defaults;
957
- }
958
- function buildRestVariableDeclaration(restName, parentPath, excludeProps) {
959
- const validExcludeProps = excludeProps.filter(Boolean);
960
- const pathParts = parentPath.split(".").filter(Boolean);
961
- let sourceObject = core.types.identifier(pathParts[0] || "__props");
962
- for (let i2 = 1; i2 < pathParts.length; i2++) {
963
- sourceObject = core.types.memberExpression(sourceObject, core.types.identifier(pathParts[i2]));
964
- }
965
- const init = validExcludeProps.length === 0 ? sourceObject : core.types.callExpression(useImport(importMap.omitProps), [
966
- sourceObject,
967
- core.types.arrayExpression(validExcludeProps.map((name) => core.types.stringLiteral(name)))
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 transformRestProperties(path, restProperties, notRestNames = []) {
972
- if (!core.types.isIdentifier(restProperties.argument)) return;
973
- const restName = restProperties.argument.name;
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
- const declaration = buildRestVariableDeclaration(restName, TRANSFORM_PROPERTY_NAME, notRestNames);
979
- const body = path.node.body;
980
- body.body.unshift(declaration);
981
- }
982
- function buildDefaultValueObject(defaults) {
983
- if (!P(defaults)) return core.types.objectExpression([]);
984
- const properties = [];
985
- for (const [key, value] of Object.entries(defaults)) {
986
- if (!key) continue;
987
- let propertyValue;
988
- if (P(value) && !core.types.isNode(value)) {
989
- propertyValue = buildDefaultValueObject(value);
990
- } else if (core.types.isExpression(value)) {
991
- propertyValue = value;
992
- } else {
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
- properties.push(core.types.objectProperty(core.types.identifier(key), propertyValue));
996
- }
997
- return core.types.objectExpression(properties);
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 buildDefaultValue(path, defaults) {
1000
- path.node.params[0] = core.types.identifier(TRANSFORM_PROPERTY_NAME);
1001
- if (Object.keys(defaults).length === 0) return;
1002
- const resolveDefaultProps = useImport(importMap.resolveDefaultProps);
1003
- const body = core.types.isBlockStatement(path.node.body) ? path.node.body : core.types.blockStatement([core.types.returnStatement(path.node.body)]);
1004
- path.node.body = body;
1005
- body.body.unshift(
1006
- core.types.expressionStatement(
1007
- core.types.assignmentExpression(
1008
- "=",
1009
- core.types.identifier(TRANSFORM_PROPERTY_NAME),
1010
- core.types.callExpression(resolveDefaultProps, [
1011
- core.types.identifier(TRANSFORM_PROPERTY_NAME),
1012
- buildDefaultValueObject(defaults)
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 notRestProperties = properties.filter((prop) => !core.types.isRestElement(prop));
1025
- const restProperties = properties.find((prop) => core.types.isRestElement(prop));
1026
- const notRestNames = notRestProperties.map((prop) => core.types.isIdentifier(prop.key) ? prop.key.name : null).filter((name) => name !== null);
1027
- const signalConflicts = notRestNames.filter((name) => x(name, signalPrefix));
1028
- if (signalConflicts.length > 0) {
1029
- re("transformProps", "Property names cannot start with signal prefix", signalConflicts);
1030
- }
1031
- if (notRestProperties.length > 0) {
1032
- const defaults = transformProperty(path, notRestProperties, TRANSFORM_PROPERTY_NAME);
1033
- if (restProperties) transformRestProperties(path, restProperties, notRestNames);
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
- buildDefaultValue(path, {});
1038
- if (restProperties) transformRestProperties(path, restProperties, notRestNames);
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, clone = false) {
1455
- return clone ? core.types.cloneNode(value, true) : value;
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
  ">": "&gt;"
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
- const expression = node.asRawChildren ? core.types.cloneNode(node.value, true) : markServerHtmlSubexpressions(core.types.cloneNode(node.value, true));
1729
- const converter = node.asRawChildren ? "toRawHtmlString" : "toEscapedHtmlString";
1730
- return core.types.callExpression(useImport(converter), [expression]);
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);