solid-optimizer 1.0.0-next.4 → 1.0.0-next.6

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.
@@ -67,7 +67,7 @@ function collectParents(program) {
67
67
  const RAW_TRANSFER = rawTransferSupported();
68
68
  function parse(filename, code) {
69
69
  const options = {
70
- lang: languageOf(filename),
70
+ lang: getLanguage(filename),
71
71
  sourceType: "module",
72
72
  preserveParens: true,
73
73
  experimentalRawTransfer: RAW_TRANSFER
@@ -77,7 +77,7 @@ function parse(filename, code) {
77
77
  if (error) throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
78
78
  return result.program;
79
79
  }
80
- function languageOf(filename) {
80
+ function getLanguage(filename) {
81
81
  if (/\.tsx$/i.test(filename)) return "tsx";
82
82
  if (/\.[cm]?ts$/i.test(filename)) return "ts";
83
83
  return "jsx";
@@ -87,7 +87,7 @@ function languageOf(filename) {
87
87
  */
88
88
  function unwrap(node) {
89
89
  let current = node;
90
- while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "TSTypeAssertion") current = current.expression;
90
+ while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "TSTypeAssertion" || current.type === "TSInstantiationExpression") current = current.expression;
91
91
  return current;
92
92
  }
93
93
  function isFunctionNode(node) {
@@ -221,7 +221,7 @@ function recordSkip(context, pass, reason, name) {
221
221
  /**
222
222
  * The current text of `node`, including the edits made inside it so far.
223
223
  */
224
- function textOf(context, node) {
224
+ function getText(context, node) {
225
225
  return context.s.slice(node.start, node.end);
226
226
  }
227
227
  //#endregion
@@ -728,6 +728,10 @@ function truthiness(node, lookup) {
728
728
  const value = evaluate(node, lookup);
729
729
  return value ? !!value.value : void 0;
730
730
  }
731
+ function areSideEffectFree(nodes) {
732
+ for (const node of nodes) if (!isSideEffectFree(node)) return false;
733
+ return true;
734
+ }
731
735
  /**
732
736
  * Whether skipping `node` entirely loses nothing observable.
733
737
  *
@@ -743,14 +747,18 @@ function isSideEffectFree(node) {
743
747
  case "ArrowFunctionExpression":
744
748
  case "FunctionExpression": return true;
745
749
  case "ParenthesizedExpression": return isSideEffectFree(node.expression);
746
- case "TemplateLiteral": return node.expressions.every(isSideEffectFree);
750
+ case "TemplateLiteral": return areSideEffectFree(node.expressions);
747
751
  case "UnaryExpression": return node.operator !== "delete" && isSideEffectFree(node.argument);
748
752
  case "BinaryExpression": return node.operator !== "in" && node.operator !== "instanceof" && isSideEffectFree(node.left) && isSideEffectFree(node.right);
749
753
  case "LogicalExpression": return isSideEffectFree(node.left) && isSideEffectFree(node.right);
750
- case "SequenceExpression": return node.expressions.every(isSideEffectFree);
754
+ case "SequenceExpression": return areSideEffectFree(node.expressions);
751
755
  case "ConditionalExpression": return isSideEffectFree(node.test) && isSideEffectFree(node.consequent) && isSideEffectFree(node.alternate);
752
- case "ArrayExpression": return node.elements.every((element) => element === null || element.type !== "SpreadElement" && isSideEffectFree(element));
753
- case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && (!property.computed || isSideEffectFree(property.key)) && isSideEffectFree(property.value));
756
+ case "ArrayExpression":
757
+ for (const element of node.elements) if (element !== null && (element.type === "SpreadElement" || !isSideEffectFree(element))) return false;
758
+ return true;
759
+ case "ObjectExpression":
760
+ for (const property of node.properties) if (property.type !== "Property" || property.computed && !isSideEffectFree(property.key) || !isSideEffectFree(property.value)) return false;
761
+ return true;
754
762
  default: return false;
755
763
  }
756
764
  }
@@ -771,7 +779,7 @@ function effectfulParts(node, parts = []) {
771
779
  function arrayLiteralLength(node) {
772
780
  if (node.type === "ParenthesizedExpression") return arrayLiteralLength(node.expression);
773
781
  if (node.type !== "ArrayExpression") return;
774
- if (node.elements.some((element) => element?.type === "SpreadElement")) return;
782
+ for (const element of node.elements) if (element?.type === "SpreadElement") return;
775
783
  return node.elements.length;
776
784
  }
777
785
  /**
@@ -806,7 +814,11 @@ function isLiteralForm(node) {
806
814
  //#region src/constants.ts
807
815
  function resolveImportedConstants(constants) {
808
816
  const result = /* @__PURE__ */ new Map();
809
- for (const [source, exports] of Object.entries(constants ?? {})) result.set(source, new Map(Object.entries(exports).map(([name, value]) => [name, { value }])));
817
+ for (const [source, exports] of Object.entries(constants ?? {})) {
818
+ const entries = /* @__PURE__ */ new Map();
819
+ for (const [name, value] of Object.entries(exports)) entries.set(name, { value });
820
+ result.set(source, entries);
821
+ }
810
822
  return result;
811
823
  }
812
824
  function globalValue(name) {
@@ -829,7 +841,7 @@ var ConstantTable = class {
829
841
  if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference) ?? this.globalMember(scopes, reference);
830
842
  if (!scopes.references.has(reference) || reference.type !== "Identifier") return;
831
843
  const binding = scopes.references.get(reference);
832
- if (binding) return this.constantOf(binding);
844
+ if (binding) return this.getConstant(binding);
833
845
  return this.globalConstant(reference.name) ?? globalValue(reference.name);
834
846
  };
835
847
  }
@@ -877,7 +889,7 @@ var ConstantTable = class {
877
889
  * A reference that runs before its declaration throws at runtime, and
878
890
  * folding turns it into the value. Minifiers make the same trade.
879
891
  */
880
- constantOf(binding) {
892
+ getConstant(binding) {
881
893
  const cached = this.constants.get(binding);
882
894
  if (cached !== void 0) return cached ?? void 0;
883
895
  let value;
@@ -956,7 +968,7 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
956
968
  if (declaration?.type === "VariableDeclaration") {
957
969
  for (const declarator of declaration.declarations) if (declarator.id.type === "Identifier") {
958
970
  const binding = topLevel.bindings.get(declarator.id.name);
959
- add(declarator.id.name, binding ? table.constantOf(binding) : void 0);
971
+ add(declarator.id.name, binding ? table.getConstant(binding) : void 0);
960
972
  }
961
973
  continue;
962
974
  }
@@ -968,7 +980,7 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
968
980
  if (statement.source) add(exported, options.importedConstants.get(statement.source.value)?.get(local));
969
981
  else {
970
982
  const binding = topLevel.bindings.get(local);
971
- add(exported, binding ? table.constantOf(binding) : void 0);
983
+ add(exported, binding ? table.getConstant(binding) : void 0);
972
984
  }
973
985
  }
974
986
  }
@@ -1064,19 +1076,21 @@ function findAttribute(element, name) {
1064
1076
  for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === name) return attribute;
1065
1077
  }
1066
1078
  function hasSpreadAttribute(element) {
1067
- return element.openingElement.attributes.some((attribute) => attribute.type === "JSXSpreadAttribute");
1079
+ for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXSpreadAttribute") return true;
1080
+ return false;
1068
1081
  }
1069
1082
  /**
1070
1083
  * Whether any child is a function, which the runtime may call with the
1071
1084
  * narrowed value. Folding one away would drop that call.
1072
1085
  */
1073
1086
  function hasCallbackChild(children) {
1074
- return children.some((child) => {
1075
- if (child.type !== "JSXExpressionContainer") return false;
1087
+ for (const child of children) {
1088
+ if (child.type !== "JSXExpressionContainer") continue;
1076
1089
  let expression = child.expression;
1077
1090
  while (expression.type === "ParenthesizedExpression") expression = expression.expression;
1078
- return expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression";
1079
- });
1091
+ if (expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression") return true;
1092
+ }
1093
+ return false;
1080
1094
  }
1081
1095
  /**
1082
1096
  * The text between an element's tags, or an empty string for a self-closing element.
@@ -1255,7 +1269,8 @@ var SizeEstimator = class {
1255
1269
  }
1256
1270
  } else size += name.length + GETTER + this.jsx(value, false);
1257
1271
  }
1258
- const significant = element.children.filter((child) => !isInsignificant(child));
1272
+ const significant = [];
1273
+ for (const child of element.children) if (!isInsignificant(child)) significant.push(child);
1259
1274
  if (significant.length > 0) size += 24 + this.children(significant);
1260
1275
  return size + (spread ? MERGE : 0);
1261
1276
  }
@@ -1286,6 +1301,18 @@ const EMPTY = { kind: "empty" };
1286
1301
  /** Solid's calls that only create a value, so the bundler can drop an unused one. */
1287
1302
  const PURE_CALLS = /* @__PURE__ */ new Set(["lazy", "createContext"]);
1288
1303
  /**
1304
+ * Solid's calls that only create a value when their first argument is not a
1305
+ * function. Given a function, they create a computation, which runs it.
1306
+ */
1307
+ const PURE_VALUE_CALLS = /* @__PURE__ */ new Set(["createSignal", "createStore"]);
1308
+ /** Expression types that are never a function. */
1309
+ const NOT_FUNCTION = /* @__PURE__ */ new Set([
1310
+ "Literal",
1311
+ "TemplateLiteral",
1312
+ "ObjectExpression",
1313
+ "ArrayExpression"
1314
+ ]);
1315
+ /**
1289
1316
  * The globals worth folding. They only apply to a reference that binds to
1290
1317
  * nothing, which is when the global is what runs.
1291
1318
  */
@@ -1335,6 +1362,29 @@ function containsHoistedDeclaration(statement) {
1335
1362
  visit(statement);
1336
1363
  return found;
1337
1364
  }
1365
+ /** The source text of each node. */
1366
+ function getTexts(context, nodes) {
1367
+ const texts = [];
1368
+ for (const node of nodes) texts.push(getText(context, node));
1369
+ return texts;
1370
+ }
1371
+ /** The children that are not whitespace-only text or an empty expression. */
1372
+ function getSignificant(children) {
1373
+ const significant = [];
1374
+ for (const child of children) if (!isInsignificant(child)) significant.push(child);
1375
+ return significant;
1376
+ }
1377
+ /** Whether the element has a `keyed` attribute that is not `{false}`. */
1378
+ function hasEnabledKeyed(element) {
1379
+ for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "keyed" && !(attribute.value?.type === "JSXExpressionContainer" && attribute.value.expression.type === "Literal" && attribute.value.expression.value === false)) return true;
1380
+ return false;
1381
+ }
1382
+ /** The summed size estimate of the children of a fragment spliced into a template. */
1383
+ function estimateChildren(estimator, children) {
1384
+ let size = 0;
1385
+ for (const child of children) size += child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child);
1386
+ return size;
1387
+ }
1338
1388
  /**
1339
1389
  * Whether a statement starting with `text` could join the statement before
1340
1390
  * it when that one has no semicolon.
@@ -1347,6 +1397,12 @@ function keyName(node) {
1347
1397
  if (node.type === "Identifier") return node.name;
1348
1398
  return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
1349
1399
  }
1400
+ /** Whether a call has no first argument, or one that is never a function. */
1401
+ function isValueArgument(call) {
1402
+ if (call.arguments.length === 0) return true;
1403
+ const first = call.arguments[0];
1404
+ return first.type !== "SpreadElement" && NOT_FUNCTION.has(unwrap(first).type);
1405
+ }
1350
1406
  /**
1351
1407
  * The text of an expression that always gives the same primitive: a literal
1352
1408
  * other than a regular expression, a template literal without expressions,
@@ -1356,9 +1412,9 @@ function primitiveText(context, node) {
1356
1412
  let expression = node;
1357
1413
  while (expression.type === "ParenthesizedExpression") expression = expression.expression;
1358
1414
  switch (expression.type) {
1359
- case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
1360
- case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
1361
- case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
1415
+ case "Literal": return "regex" in expression ? void 0 : getText(context, expression);
1416
+ case "TemplateLiteral": return expression.expressions.length === 0 ? getText(context, expression) : void 0;
1417
+ case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? getText(context, expression) : void 0;
1362
1418
  default: return;
1363
1419
  }
1364
1420
  }
@@ -1442,7 +1498,7 @@ var Folder = class {
1442
1498
  return isJSXChild(node, this.context.parents) || this.context.parents.get(node)?.type === "JSXAttribute";
1443
1499
  }
1444
1500
  replace(node, text) {
1445
- if (textOf(this.context, node) === text) return;
1501
+ if (getText(this.context, node) === text) return;
1446
1502
  this.context.s.overwrite(node.start, node.end, text);
1447
1503
  this.changed = true;
1448
1504
  }
@@ -1452,12 +1508,17 @@ var Folder = class {
1452
1508
  * branch can leave it unused, and the annotation lets the bundler drop it
1453
1509
  * along with its dynamic import. `createContext` only creates the context,
1454
1510
  * which inlining can leave unused.
1511
+ *
1512
+ * `createSignal` and `createStore` with a first argument that is not a
1513
+ * function only create a node. It takes no owner and no hydration id, so
1514
+ * dropping an unused one changes nothing else.
1455
1515
  */
1456
1516
  annotatePure(call) {
1457
1517
  if (call.callee.type !== "Identifier") return;
1458
1518
  const binding = this.context.scopes.references.get(call.callee);
1459
1519
  const identity = binding ? solidExport(binding, this.context.options) : void 0;
1460
- if (identity === void 0 || !PURE_CALLS.has(identity)) return;
1520
+ if (identity === void 0) return;
1521
+ if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return;
1461
1522
  const before = this.context.code.slice(0, call.start).trimEnd();
1462
1523
  if (/(#|@)__PURE__\s*\*\/$/.test(before)) return;
1463
1524
  this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
@@ -1502,11 +1563,11 @@ var Folder = class {
1502
1563
  keepAfter(target, discarded, value) {
1503
1564
  const parts = discarded ? effectfulParts(discarded) : [];
1504
1565
  if (parts.length === 0) {
1505
- const text = textOf(this.context, value);
1566
+ const text = getText(this.context, value);
1506
1567
  return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
1507
1568
  }
1508
- const texts = parts.map((part) => textOf(this.context, part));
1509
- texts.push(textOf(this.context, value));
1569
+ const texts = getTexts(this.context, parts);
1570
+ texts.push(getText(this.context, value));
1510
1571
  return `(${texts.join(", ")})`;
1511
1572
  }
1512
1573
  foldLogical(node) {
@@ -1630,17 +1691,17 @@ var Folder = class {
1630
1691
  case "JSXElement":
1631
1692
  case "JSXFragment": return {
1632
1693
  kind: "jsx",
1633
- text: textOf(this.context, value)
1694
+ text: getText(this.context, value)
1634
1695
  };
1635
1696
  default:
1636
1697
  if (value.expression.type === "JSXEmptyExpression") return EMPTY;
1637
1698
  if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return {
1638
1699
  kind: "jsx",
1639
- text: textOf(this.context, value.expression)
1700
+ text: getText(this.context, value.expression)
1640
1701
  };
1641
1702
  return {
1642
1703
  kind: "expression",
1643
- text: textOf(this.context, value.expression),
1704
+ text: getText(this.context, value.expression),
1644
1705
  primary: isPrimary(value.expression)
1645
1706
  };
1646
1707
  }
@@ -1676,11 +1737,11 @@ var Folder = class {
1676
1737
  * would run while the copy is made, so either one keeps the loop.
1677
1738
  */
1678
1739
  unrollFor(element, expression) {
1679
- if (element.openingElement.attributes.some((attribute) => attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "keyed" && !(attribute.value?.type === "JSXExpressionContainer" && attribute.value.expression.type === "Literal" && attribute.value.expression.value === false))) return;
1740
+ if (hasEnabledKeyed(element)) return;
1680
1741
  const byIndex = findAttribute(element, "keyed") !== void 0;
1681
1742
  const list = this.constantList(expression);
1682
1743
  if (!list) return;
1683
- const significant = element.children.filter((child) => !isInsignificant(child));
1744
+ const significant = getSignificant(element.children);
1684
1745
  const only = significant.at(0);
1685
1746
  if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return;
1686
1747
  let row = only.expression;
@@ -1718,16 +1779,20 @@ var Folder = class {
1718
1779
  * `<For>` reads it.
1719
1780
  */
1720
1781
  constantList(expression) {
1721
- let node = expression;
1722
- while (node.type === "ParenthesizedExpression") node = node.expression;
1782
+ let node = unwrap(expression);
1723
1783
  let declaration;
1724
1784
  if (node.type === "Identifier") {
1725
1785
  const binding = this.context.scopes.references.get(node);
1726
1786
  if (binding?.declaration.type !== "VariableDeclarator" || binding.declaration.id !== binding.identifier || !binding.declaration.init || !isConstantDeclaration(binding, this.context.options)) return;
1727
1787
  const statement = this.context.parents.get(binding.declaration);
1728
- if ((statement ? this.context.parents.get(statement) : void 0)?.type === "ExportNamedDeclaration" || !binding.references.every((reference) => this.isForEach(reference))) return;
1729
- node = binding.declaration.init;
1730
- while (node.type === "ParenthesizedExpression") node = node.expression;
1788
+ const owner = statement ? this.context.parents.get(statement) : void 0;
1789
+ let onlyForEach = true;
1790
+ for (const reference of binding.references) if (!this.isForEach(reference)) {
1791
+ onlyForEach = false;
1792
+ break;
1793
+ }
1794
+ if (owner?.type === "ExportNamedDeclaration" || !onlyForEach) return;
1795
+ node = unwrap(binding.declaration.init);
1731
1796
  declaration = binding.references.length === 1 && statement?.type === "VariableDeclaration" && statement.declarations.length === 1 ? statement : void 0;
1732
1797
  }
1733
1798
  if (node.type !== "ArrayExpression") return;
@@ -1827,7 +1892,7 @@ var Folder = class {
1827
1892
  const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
1828
1893
  if (init?.type !== "JSXFragment") return false;
1829
1894
  const estimator = new SizeEstimator(code);
1830
- return (inTemplate ? init.children.reduce((size, child) => size + (child.type === "JSXElement" ? estimator.jsx(child, true) : estimator.node(child)), 0) : estimator.jsx(init, false)) <= loop;
1895
+ return (inTemplate ? estimateChildren(estimator, init.children) : estimator.jsx(init, false)) <= loop;
1831
1896
  }
1832
1897
  /** Whether an element sits directly in an element or fragment whose template it joins. */
1833
1898
  joinsParentTemplate(element) {
@@ -1859,8 +1924,16 @@ var Folder = class {
1859
1924
  });
1860
1925
  }
1861
1926
  if (matches.length === 0) return;
1862
- const winner = matches.findIndex((item) => item.when === true);
1863
- if (winner !== -1 && matches.slice(0, winner).every((item) => item.when === false)) {
1927
+ let winner = -1;
1928
+ let allFalse = true;
1929
+ for (const [index, item] of matches.entries()) {
1930
+ if (item.when === true) {
1931
+ winner = index;
1932
+ break;
1933
+ }
1934
+ if (item.when !== false) allFalse = false;
1935
+ }
1936
+ if (winner !== -1 && allFalse) {
1864
1937
  const match = matches.at(winner);
1865
1938
  if (!match || hasCallbackChild(match.element.children)) return;
1866
1939
  return {
@@ -1868,7 +1941,7 @@ var Folder = class {
1868
1941
  children: match.element.children
1869
1942
  };
1870
1943
  }
1871
- if (matches.every((item) => item.when === false)) return this.fallback(element);
1944
+ if (winner === -1 && allFalse) return this.fallback(element);
1872
1945
  for (const item of matches) if (item.when === false) {
1873
1946
  this.context.s.remove(item.element.start, item.element.end);
1874
1947
  this.changed = true;
@@ -1899,7 +1972,8 @@ var Folder = class {
1899
1972
  }
1900
1973
  }
1901
1974
  if (tag === void 0) return;
1902
- const attributes = element.openingElement.attributes.filter((attribute) => attribute !== component).map((attribute) => textOf(this.context, attribute));
1975
+ const attributes = [];
1976
+ for (const attribute of element.openingElement.attributes) if (attribute !== component) attributes.push(getText(this.context, attribute));
1903
1977
  const opening = [tag, ...attributes].join(" ");
1904
1978
  return {
1905
1979
  kind: "jsx",
@@ -1920,10 +1994,10 @@ var Folder = class {
1920
1994
  case "jsx": return resolved.text;
1921
1995
  case "expression": return resolved.primary ? resolved.text : `(${resolved.text})`;
1922
1996
  default: {
1923
- const significant = resolved.children.filter((child) => !isInsignificant(child));
1997
+ const significant = getSignificant(resolved.children);
1924
1998
  const only = significant.at(0);
1925
1999
  if (only === void 0) return "null";
1926
- if (significant.length === 1 && (only.type === "JSXElement" || only.type === "JSXFragment")) return textOf(this.context, only);
2000
+ if (significant.length === 1 && (only.type === "JSXElement" || only.type === "JSXFragment")) return getText(this.context, only);
1927
2001
  const first = resolved.children.at(0);
1928
2002
  const last = resolved.children.at(-1);
1929
2003
  if (!first || !last) return "null";
@@ -2018,9 +2092,9 @@ var Folder = class {
2018
2092
  let node = arg;
2019
2093
  while (node.type === "ParenthesizedExpression") node = node.expression;
2020
2094
  switch (node.type) {
2021
- case "Literal": return "regex" in node ? void 0 : textOf(this.context, node);
2022
- case "TemplateLiteral": return node.expressions.length === 0 ? textOf(this.context, node) : void 0;
2023
- case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${textOf(this.context, node)})` : void 0;
2095
+ case "Literal": return "regex" in node ? void 0 : getText(this.context, node);
2096
+ case "TemplateLiteral": return node.expressions.length === 0 ? getText(this.context, node) : void 0;
2097
+ case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" ? `(${getText(this.context, node)})` : void 0;
2024
2098
  case "Identifier": {
2025
2099
  const binding = this.context.scopes.references.get(node);
2026
2100
  return binding && !binding.mutated ? node.name : void 0;
@@ -2042,7 +2116,12 @@ var Folder = class {
2042
2116
  const unreachable = statements.slice(terminator + 1);
2043
2117
  const first = unreachable.at(0);
2044
2118
  const last = unreachable.at(-1);
2045
- if (first && last && !unreachable.some(containsHoistedDeclaration)) {
2119
+ let hoists = false;
2120
+ for (const statement of unreachable) if (containsHoistedDeclaration(statement)) {
2121
+ hoists = true;
2122
+ break;
2123
+ }
2124
+ if (first && last && !hoists) {
2046
2125
  this.context.s.remove(first.start, last.end);
2047
2126
  this.changed = true;
2048
2127
  }
@@ -2063,7 +2142,7 @@ var Folder = class {
2063
2142
  const parts = effectfulParts(test);
2064
2143
  const first = parts.at(0);
2065
2144
  if (first === void 0) return;
2066
- const text = parts.map((part) => textOf(this.context, part)).join(", ");
2145
+ const text = getTexts(this.context, parts).join(", ");
2067
2146
  return startsLikeStatement(first) ? `(${text});` : `${text};`;
2068
2147
  }
2069
2148
  /**
@@ -2080,7 +2159,7 @@ var Folder = class {
2080
2159
  const parts = [];
2081
2160
  const effect = this.discardedTest(statement.test);
2082
2161
  if (effect !== void 0) parts.push(effect);
2083
- if (kept) parts.push(textOf(this.context, kept));
2162
+ if (kept) parts.push(getText(this.context, kept));
2084
2163
  this.replaceStatement(statement, parts.join(" "));
2085
2164
  return kept;
2086
2165
  }
@@ -2136,7 +2215,7 @@ const ACCESSOR_PAIRS = /* @__PURE__ */ new Set([
2136
2215
  * `useContext` throws when the value and the default are both `undefined`,
2137
2216
  * so a value that could be `undefined` keeps its provider.
2138
2217
  */
2139
- function providerOf(context, element) {
2218
+ function findProvider(context, element) {
2140
2219
  const binding = contextBinding(context, element.openingElement.name);
2141
2220
  if (!binding || element.openingElement.selfClosing) return;
2142
2221
  const { attributes } = element.openingElement;
@@ -2196,7 +2275,7 @@ function isDefined(context, node, depth) {
2196
2275
  case "Identifier": {
2197
2276
  const binding = context.scopes.references.get(expression);
2198
2277
  if (!binding || binding.mutated || depth > 2) return false;
2199
- if (binding.kind === "function" || binding.kind === "class" || isAccessor(context, binding)) return true;
2278
+ if (binding.kind === "function" || binding.kind === "class" || isAccessor$1(context, binding)) return true;
2200
2279
  const { declaration } = binding;
2201
2280
  return isConstantDeclaration(binding, context.options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init !== null && isDefined(context, declaration.init, depth + 1);
2202
2281
  }
@@ -2234,7 +2313,7 @@ function isHandlerFunction(attribute) {
2234
2313
  * Whether a binding is a signal or store getter or setter, or a memo, which
2235
2314
  * run no code of the app when called.
2236
2315
  */
2237
- function isAccessor(context, binding) {
2316
+ function isAccessor$1(context, binding) {
2238
2317
  if (binding.mutated) return false;
2239
2318
  const { declaration } = binding;
2240
2319
  if (declaration.type !== "VariableDeclarator") return false;
@@ -2244,8 +2323,8 @@ function isAccessor(context, binding) {
2244
2323
  const callee = solidCallee(context, call);
2245
2324
  if (id === binding.identifier) return callee === "createMemo";
2246
2325
  if (id.type !== "ArrayPattern" || callee === void 0 || !ACCESSOR_PAIRS.has(callee)) return false;
2247
- const index = id.elements.findIndex((element) => element === binding.identifier);
2248
- return index === 0 || index === 1;
2326
+ for (const [index, element] of id.elements.entries()) if (element === binding.identifier) return index === 0 || index === 1;
2327
+ return false;
2249
2328
  }
2250
2329
  /**
2251
2330
  * Whether all code that can run under `root` is visible, so a provider
@@ -2322,7 +2401,7 @@ function isClosed(context, root, options) {
2322
2401
  */
2323
2402
  function runsOutsideCode(context, binding, root, options, depth) {
2324
2403
  const { identifier, declaration } = binding;
2325
- if (binding === options.props || identifier.start >= root.start && identifier.end <= root.end || isAccessor(context, binding) || solidExport(binding, context.options) !== void 0) return false;
2404
+ if (binding === options.props || identifier.start >= root.start && identifier.end <= root.end || isAccessor$1(context, binding) || solidExport(binding, context.options) !== void 0) return false;
2326
2405
  if (binding.kind === "param" || binding.kind === "function" || depth > 4) return true;
2327
2406
  if (declaration.type !== "VariableDeclarator" || !declaration.init) return false;
2328
2407
  const init = unwrap(declaration.init);
@@ -2359,11 +2438,12 @@ function isOwnView(context, node, props, depth) {
2359
2438
  if (call?.type !== "CallExpression") return false;
2360
2439
  const callee = solidCallee(context, call);
2361
2440
  if (callee !== "merge" && callee !== "omit") return false;
2362
- return call.arguments.every((argument) => {
2441
+ for (const argument of call.arguments) {
2363
2442
  if (argument.type === "SpreadElement") return false;
2364
2443
  const inner = unwrap(argument);
2365
- return inner.type === "ObjectExpression" || inner.type === "Literal" && typeof inner.value === "string" || isOwnView(context, inner, props, depth + 1);
2366
- });
2444
+ if (inner.type !== "ObjectExpression" && !(inner.type === "Literal" && typeof inner.value === "string") && !isOwnView(context, inner, props, depth + 1)) return false;
2445
+ }
2446
+ return true;
2367
2447
  }
2368
2448
  function isVisibleElement(context, element, options) {
2369
2449
  const { name } = element.openingElement;
@@ -2394,13 +2474,13 @@ function isAccessorValue(context, node, options, depth) {
2394
2474
  const expression = unwrap(node);
2395
2475
  if (depth > 4) return false;
2396
2476
  if (expression.type === "CallExpression") {
2397
- const value = contextValueOf(context, expression, options);
2477
+ const value = findContextValue(context, expression, options);
2398
2478
  return value !== void 0 && isAccessorValue(context, value, options, depth + 1);
2399
2479
  }
2400
2480
  if (expression.type !== "Identifier") return false;
2401
2481
  const binding = context.scopes.references.get(expression);
2402
2482
  if (!binding) return false;
2403
- if (isAccessor(context, binding)) return true;
2483
+ if (isAccessor$1(context, binding)) return true;
2404
2484
  const init = constantInit(context, binding);
2405
2485
  return init !== void 0 && isAccessorValue(context, init, options, depth + 1);
2406
2486
  }
@@ -2415,7 +2495,7 @@ function constantInit(context, binding) {
2415
2495
  /**
2416
2496
  * The value `useContext(Ctx)` returns, when the provider is known.
2417
2497
  */
2418
- function contextValueOf(context, call, options) {
2498
+ function findContextValue(context, call, options) {
2419
2499
  const argument = call.arguments.at(0);
2420
2500
  if (solidCallee(context, call) !== "useContext" || call.arguments.length !== 1 || argument === void 0 || argument.type === "SpreadElement") return;
2421
2501
  const target = contextBinding(context, unwrap(argument));
@@ -2428,10 +2508,14 @@ function contextValueOf(context, call, options) {
2428
2508
  */
2429
2509
  function isVisibleMethod(context, member, options) {
2430
2510
  if (member.computed || member.property.type !== "Identifier" || member.optional) return false;
2431
- const object = objectLiteralOf(context, member.object, options, 0);
2432
- if (!object || object.properties.some((property) => property.type !== "Property")) return false;
2511
+ const object = findObjectLiteral(context, member.object, options, 0);
2512
+ if (!object) return false;
2433
2513
  const { name } = member.property;
2434
- const matches = object.properties.filter((property) => property.type === "Property" && !property.computed && property.key.type === "Identifier" && property.key.name === name);
2514
+ const matches = [];
2515
+ for (const property of object.properties) {
2516
+ if (property.type !== "Property") return false;
2517
+ if (!property.computed && property.key.type === "Identifier" && property.key.name === name) matches.push(property);
2518
+ }
2435
2519
  const property = matches.at(0);
2436
2520
  if (matches.length !== 1 || property?.type !== "Property" || property.kind !== "init") return false;
2437
2521
  const value = unwrap(property.value);
@@ -2446,18 +2530,18 @@ function isVisibleMethod(context, member, options) {
2446
2530
  * The object literal an expression evaluates to, through bindings that
2447
2531
  * never change and reads of a context whose value is known.
2448
2532
  */
2449
- function objectLiteralOf(context, node, options, depth) {
2533
+ function findObjectLiteral(context, node, options, depth) {
2450
2534
  const expression = unwrap(node);
2451
2535
  if (depth > 4) return;
2452
2536
  if (expression.type === "ObjectExpression") return expression;
2453
2537
  if (expression.type === "CallExpression") {
2454
- const value = contextValueOf(context, expression, options);
2455
- return value ? objectLiteralOf(context, value, options, depth + 1) : void 0;
2538
+ const value = findContextValue(context, expression, options);
2539
+ return value ? findObjectLiteral(context, value, options, depth + 1) : void 0;
2456
2540
  }
2457
2541
  if (expression.type !== "Identifier") return;
2458
2542
  const binding = context.scopes.references.get(expression);
2459
2543
  const init = binding ? constantInit(context, binding) : void 0;
2460
- return init ? objectLiteralOf(context, init, options, depth + 1) : void 0;
2544
+ return init ? findObjectLiteral(context, init, options, depth + 1) : void 0;
2461
2545
  }
2462
2546
  /**
2463
2547
  * The `useContext(Ctx)` calls under `root` that run under its owner, so
@@ -2502,7 +2586,7 @@ function findProviders(context) {
2502
2586
  const providers = [];
2503
2587
  const visit = (node) => {
2504
2588
  if (node.type === "JSXElement") {
2505
- const provider = providerOf(context, node);
2589
+ const provider = findProvider(context, node);
2506
2590
  if (provider) providers.push(provider);
2507
2591
  }
2508
2592
  forEachChild(node, visit);
@@ -2519,7 +2603,12 @@ var ProviderRemover = class {
2519
2603
  run() {
2520
2604
  for (const provider of findProviders(this.context)) {
2521
2605
  const { element } = provider;
2522
- if (this.removed.some((other) => element.start < other.end && other.start < element.end)) continue;
2606
+ let overlaps = false;
2607
+ for (const other of this.removed) if (element.start < other.end && other.start < element.end) {
2608
+ overlaps = true;
2609
+ break;
2610
+ }
2611
+ if (overlaps) continue;
2523
2612
  if (hasNestedProvider(this.context, provider)) this.skip(provider, "provider-nested");
2524
2613
  else if (this.tryRemove(provider)) this.removed.push(element);
2525
2614
  }
@@ -2536,7 +2625,7 @@ var ProviderRemover = class {
2536
2625
  const visit = (node) => {
2537
2626
  if (node.type === "JSXElement") {
2538
2627
  const binding = contextBinding(this.context, node.openingElement.name);
2539
- if (binding && !providerOf(this.context, node)) {
2628
+ if (binding && !findProvider(this.context, node)) {
2540
2629
  const { attributes } = node.openingElement;
2541
2630
  const attribute = attributes.at(0);
2542
2631
  const onlyValue = attributes.length === 1 && attribute?.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === "value";
@@ -2551,17 +2640,24 @@ var ProviderRemover = class {
2551
2640
  const { element } = provider;
2552
2641
  const children = element.children;
2553
2642
  let reason = "open-other";
2554
- if (!children.every((child) => isClosed(this.context, child, {
2643
+ const closure = {
2555
2644
  component: () => false,
2556
2645
  contextValue: (target) => target === provider.context ? provider.value : void 0,
2557
2646
  onOpen: (open) => {
2558
2647
  reason = open;
2559
2648
  }
2560
- }))) {
2649
+ };
2650
+ let closed = true;
2651
+ for (const child of children) if (!isClosed(this.context, child, closure)) {
2652
+ closed = false;
2653
+ break;
2654
+ }
2655
+ if (!closed) {
2561
2656
  this.skip(provider, reason);
2562
2657
  return false;
2563
2658
  }
2564
- const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
2659
+ const reads = [];
2660
+ for (const child of children) reads.push(...contextReads(this.context, child, provider.context));
2565
2661
  const texts = [];
2566
2662
  for (const read of reads) {
2567
2663
  const text = stableValueText(this.context, provider, read);
@@ -2753,7 +2849,8 @@ function inBodyOrder(sites) {
2753
2849
  const pending = [...sites];
2754
2850
  const ordered = [];
2755
2851
  while (pending.length > 0) {
2756
- const ready = pending.filter((site) => !pending.some((other) => other !== site && (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))));
2852
+ const ready = [];
2853
+ for (const site of pending) if (!hasPendingDependency(pending, site)) ready.push(site);
2757
2854
  const next = ready.length > 0 ? ready : pending;
2758
2855
  for (const site of next) {
2759
2856
  ordered.push(site);
@@ -2910,7 +3007,7 @@ function acceptsAnyExpression(context, node) {
2910
3007
  /**
2911
3008
  * The function a top-level binding declares, if it declares one.
2912
3009
  */
2913
- function functionOf(binding, options) {
3010
+ function findComponentFunction(binding, options) {
2914
3011
  const { declaration } = binding;
2915
3012
  if (binding.kind === "function" && declaration.type === "FunctionDeclaration") return declaration;
2916
3013
  if (isConstantDeclaration(binding, options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init) {
@@ -2921,7 +3018,7 @@ function functionOf(binding, options) {
2921
3018
  /**
2922
3019
  * The attributes of a call site by name, or `undefined` when one cannot be inlined.
2923
3020
  */
2924
- function attributesOf(context, element) {
3021
+ function getAttributes(context, element) {
2925
3022
  const attributes = /* @__PURE__ */ new Map();
2926
3023
  for (const attribute of element.openingElement.attributes) {
2927
3024
  if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" && !isForwardableRef(context, attribute) || attributes.has(attribute.name.name)) return;
@@ -2936,7 +3033,7 @@ function attributesOf(context, element) {
2936
3033
  */
2937
3034
  function isDataDeclaration(statement) {
2938
3035
  const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
2939
- return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && declaration.declarations.every((declarator) => declarator.id.type === "Identifier" && declarator.init !== null && isDataLiteral(declarator.init));
3036
+ return declaration?.type === "VariableDeclaration" && !declaration.declare && (declaration.kind === "const" || declaration.kind === "var") && areDataDeclarators(declaration.declarations);
2940
3037
  }
2941
3038
  /** Whether an expression is a literal, or an array or object literal of them. */
2942
3039
  function isDataLiteral(node) {
@@ -2947,8 +3044,8 @@ function isDataLiteral(node) {
2947
3044
  case "ParenthesizedExpression":
2948
3045
  case "TSAsExpression":
2949
3046
  case "TSSatisfiesExpression": return isDataLiteral(node.expression);
2950
- case "ArrayExpression": return node.elements.every((element) => element !== null && element.type !== "SpreadElement" && isDataLiteral(element));
2951
- case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && property.kind === "init" && !property.method && !property.computed && isDataLiteral(property.value));
3047
+ case "ArrayExpression": return areDataElements(node.elements);
3048
+ case "ObjectExpression": return areDataProperties(node.properties);
2952
3049
  default: return false;
2953
3050
  }
2954
3051
  }
@@ -2972,7 +3069,10 @@ function isForwardableRef(context, attribute) {
2972
3069
  if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression") return false;
2973
3070
  const isTarget = (node, inArray) => {
2974
3071
  const inner = unwrap(node);
2975
- if (inner.type === "ArrayExpression") return inner.elements.every((element) => element !== null && element.type !== "SpreadElement" && isTarget(element, true));
3072
+ if (inner.type === "ArrayExpression") {
3073
+ for (const element of inner.elements) if (element === null || element.type === "SpreadElement" || !isTarget(element, true)) return false;
3074
+ return true;
3075
+ }
2976
3076
  if (inArray && isVoidZero(inner)) return true;
2977
3077
  if (inArray && inner.type === "CallExpression" && solidCallee(context, inner) === "untrack") {
2978
3078
  const callback = inner.arguments.at(0);
@@ -2987,7 +3087,7 @@ function isVoidZero(node) {
2987
3087
  return node.type === "UnaryExpression" && node.operator === "void" && node.argument.type === "Literal" && node.argument.value === 0;
2988
3088
  }
2989
3089
  /**
2990
- * Why `attributesOf` turns down a call site, for the report.
3090
+ * Why `getAttributes` turns down a call site, for the report.
2991
3091
  */
2992
3092
  function callSiteReason(element) {
2993
3093
  const names = /* @__PURE__ */ new Set();
@@ -3050,7 +3150,7 @@ var Inliner = class {
3050
3150
  visit(loader);
3051
3151
  return imports;
3052
3152
  }
3053
- scopeOf(fn) {
3153
+ getScope(fn) {
3054
3154
  return this.context.scopes.scopes.get(fn);
3055
3155
  }
3056
3156
  /**
@@ -3090,7 +3190,7 @@ var Inliner = class {
3090
3190
  this.reason = "component-mutated";
3091
3191
  return;
3092
3192
  }
3093
- const fn = functionOf(binding, this.context.options);
3193
+ const fn = findComponentFunction(binding, this.context.options);
3094
3194
  if (!fn?.body) {
3095
3195
  this.reason = this.isLazy(binding) ? "lazy" : "not-a-function";
3096
3196
  return;
@@ -3103,7 +3203,7 @@ var Inliner = class {
3103
3203
  this.reason = "component-params";
3104
3204
  return;
3105
3205
  }
3106
- const scope = this.scopeOf(fn);
3206
+ const scope = this.getScope(fn);
3107
3207
  if (!scope) {
3108
3208
  this.reason = "not-a-function";
3109
3209
  return;
@@ -3172,7 +3272,9 @@ var Inliner = class {
3172
3272
  }
3173
3273
  props.push(read);
3174
3274
  }
3175
- statements = statements.filter((statement) => !views.declarations.includes(statement));
3275
+ const kept = [];
3276
+ for (const statement of statements) if (!views.declarations.includes(statement)) kept.push(statement);
3277
+ statements = kept;
3176
3278
  const locals = [];
3177
3279
  const seen = /* @__PURE__ */ new Set();
3178
3280
  for (const inner of this.context.scopes.scopes.values()) {
@@ -3249,7 +3351,7 @@ var Inliner = class {
3249
3351
  const init = unwrap(declarator.init);
3250
3352
  const callee = init.type === "CallExpression" ? solidCallee(this.context, init) : void 0;
3251
3353
  if (init.type !== "CallExpression" || callee !== "merge" && callee !== "omit") continue;
3252
- const view = this.viewOf(callee, init, param, views, args);
3354
+ const view = this.resolveView(callee, init, param, views, args);
3253
3355
  if (view === null) continue;
3254
3356
  const binding = scope.bindings.get(declarator.id.name);
3255
3357
  if (!view || !binding || binding.mutated) return;
@@ -3316,12 +3418,12 @@ var Inliner = class {
3316
3418
  * take the props or another view, and `undefined` when it does but its
3317
3419
  * other arguments are not literal.
3318
3420
  */
3319
- viewOf(callee, call, param, views, args) {
3320
- const sourcesOf = (argument) => {
3421
+ resolveView(callee, call, param, views, args) {
3422
+ const resolveSources = (argument) => {
3321
3423
  if (argument.type === "CallExpression") {
3322
3424
  const nested = solidCallee(this.context, argument);
3323
3425
  if (nested !== "merge" && nested !== "omit") return;
3324
- return this.viewOf(nested, argument, param, views, args)?.sources;
3426
+ return this.resolveView(nested, argument, param, views, args)?.sources;
3325
3427
  }
3326
3428
  if (argument.type !== "Identifier") return;
3327
3429
  const binding = this.context.scopes.references.get(argument);
@@ -3331,13 +3433,18 @@ var Inliner = class {
3331
3433
  }];
3332
3434
  return binding ? views.get(binding)?.sources : void 0;
3333
3435
  };
3334
- if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && sourcesOf(unwrap(argument)) !== void 0)) return null;
3436
+ let involved = false;
3437
+ for (const argument of call.arguments) if (argument.type !== "SpreadElement" && resolveSources(unwrap(argument)) !== void 0) {
3438
+ involved = true;
3439
+ break;
3440
+ }
3441
+ if (!involved) return null;
3335
3442
  if (callee === "omit") {
3336
3443
  const first = call.arguments.at(0);
3337
3444
  const keys = call.arguments.slice(1);
3338
3445
  if (first === void 0 || first.type === "SpreadElement") return;
3339
3446
  const target = unwrap(first);
3340
- const base = sourcesOf(target);
3447
+ const base = resolveSources(target);
3341
3448
  const hidden = [];
3342
3449
  for (const key of keys) {
3343
3450
  if (key.type !== "Literal" || typeof key.value !== "string") return;
@@ -3345,16 +3452,18 @@ var Inliner = class {
3345
3452
  }
3346
3453
  if (!base) return;
3347
3454
  args.add(target);
3348
- return { sources: base.map((source) => ({
3455
+ const hiddenSources = [];
3456
+ for (const source of base) hiddenSources.push({
3349
3457
  defaults: source.defaults,
3350
3458
  hidden: /* @__PURE__ */ new Set([...source.hidden, ...hidden])
3351
- })) };
3459
+ });
3460
+ return { sources: hiddenSources };
3352
3461
  }
3353
3462
  const sources = [];
3354
3463
  for (const argument of call.arguments) {
3355
3464
  if (argument.type === "SpreadElement") return;
3356
3465
  const inner = unwrap(argument);
3357
- const existing = sourcesOf(inner);
3466
+ const existing = resolveSources(inner);
3358
3467
  if (existing) {
3359
3468
  args.add(inner);
3360
3469
  sources.push(...existing);
@@ -3409,7 +3518,7 @@ var Inliner = class {
3409
3518
  if (parent?.type === "CallExpression") return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === "render";
3410
3519
  const element = parent?.type === "JSXExpressionContainer" ? this.context.parents.get(parent) : void 0;
3411
3520
  if (element?.type !== "JSXElement") return false;
3412
- const significant = element.children.filter((child) => !isInsignificant(child));
3521
+ const significant = getSignificantChildren(element.children);
3413
3522
  if (significant.length !== 1 || significant.at(0) !== parent) return false;
3414
3523
  switch (this.solidCallee(element.openingElement.name)) {
3415
3524
  case "For":
@@ -3432,7 +3541,7 @@ var Inliner = class {
3432
3541
  * returned JSX of a component, with only intrinsic elements, fragments,
3433
3542
  * and closed providers between it and the call.
3434
3543
  */
3435
- hostOf(element) {
3544
+ findHost(element) {
3436
3545
  let node = element;
3437
3546
  for (;;) {
3438
3547
  const parent = this.context.parents.get(node);
@@ -3441,7 +3550,7 @@ var Inliner = class {
3441
3550
  const { name } = parent.openingElement;
3442
3551
  if (!(this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && (isIntrinsicTag(name.name) || this.transparent.has(parent)))) return;
3443
3552
  const child = node;
3444
- if (!parent.children.some((item) => item === child)) return;
3553
+ if (!hasChild(parent.children, child)) return;
3445
3554
  } else if (parent.type === "ReturnStatement") {
3446
3555
  const list = this.context.parents.get(parent);
3447
3556
  if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
@@ -3470,8 +3579,8 @@ var Inliner = class {
3470
3579
  }
3471
3580
  childrenValue(element, reads, fresh, snapshots) {
3472
3581
  const { children } = element;
3473
- if (children.some((child) => child.type === "JSXSpreadChild")) return;
3474
- const significant = children.filter((child) => !isInsignificant(child));
3582
+ if (hasSpreadChild(children)) return;
3583
+ const significant = getSignificantChildren(children);
3475
3584
  const only = significant.at(0);
3476
3585
  if (only === void 0) return {
3477
3586
  kind: "children",
@@ -3485,10 +3594,10 @@ var Inliner = class {
3485
3594
  else if (only.type === "JSXExpressionContainer") {
3486
3595
  if (only.expression.type === "JSXEmptyExpression") return;
3487
3596
  const value = this.expressionValue(only.expression, "children", reads, fresh, snapshots);
3488
- if (value.kind !== "expression" || value.text !== textOf(this.context, only.expression)) return value;
3597
+ if (value.kind !== "expression" || value.text !== getText(this.context, only.expression)) return value;
3489
3598
  text = value.text;
3490
3599
  primary = value.primary;
3491
- } else text = textOf(this.context, only);
3600
+ } else text = getText(this.context, only);
3492
3601
  return {
3493
3602
  kind: "children",
3494
3603
  children,
@@ -3515,7 +3624,7 @@ var Inliner = class {
3515
3624
  }
3516
3625
  return {
3517
3626
  kind: "expression",
3518
- text: textOf(this.context, value.expression),
3627
+ text: getText(this.context, value.expression),
3519
3628
  primary: true
3520
3629
  };
3521
3630
  }
@@ -3542,7 +3651,7 @@ var Inliner = class {
3542
3651
  case "JSXElement":
3543
3652
  case "JSXFragment": return {
3544
3653
  kind: "jsx",
3545
- text: textOf(this.context, value),
3654
+ text: getText(this.context, value),
3546
3655
  primary: true
3547
3656
  };
3548
3657
  }
@@ -3555,7 +3664,7 @@ var Inliner = class {
3555
3664
  * How an expression passed as a prop reads in a copy of the component.
3556
3665
  */
3557
3666
  expressionValue(expression, name, reads, fresh, snapshots) {
3558
- const text = textOf(this.context, expression);
3667
+ const text = getText(this.context, expression);
3559
3668
  const inner = unwrap(expression);
3560
3669
  if (inner.type === "JSXElement" || inner.type === "JSXFragment") return {
3561
3670
  kind: "jsx",
@@ -3617,12 +3726,12 @@ var Inliner = class {
3617
3726
  * when it cannot be inlined there. Nothing is written until `commit`.
3618
3727
  */
3619
3728
  prepare(component, element) {
3620
- const attributes = attributesOf(this.context, element);
3729
+ const attributes = getAttributes(this.context, element);
3621
3730
  if (!attributes) {
3622
3731
  this.reason = callSiteReason(element);
3623
3732
  return;
3624
3733
  }
3625
- const hasChildren = element.children.some((child) => !isInsignificant(child));
3734
+ const hasChildren = getSignificantChildren(element.children).length > 0;
3626
3735
  if (hasChildren && attributes.has("children")) {
3627
3736
  this.reason = "call-children";
3628
3737
  return;
@@ -3668,7 +3777,7 @@ var Inliner = class {
3668
3777
  return;
3669
3778
  }
3670
3779
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
3671
- const host = hoisted ? this.hostOf(element) : void 0;
3780
+ const host = hoisted ? this.findHost(element) : void 0;
3672
3781
  if (hoisted && !host) {
3673
3782
  this.reason = "no-host";
3674
3783
  return;
@@ -3745,7 +3854,8 @@ var Inliner = class {
3745
3854
  * stored in a `const` of the host first, so every read gets the same one.
3746
3855
  */
3747
3856
  findTransparentProviders(components, sites) {
3748
- const byBinding = new Map(components.map((component) => [component.binding, component]));
3857
+ const byBinding = /* @__PURE__ */ new Map();
3858
+ for (const component of components) byBinding.set(component.binding, component);
3749
3859
  const closedUnder = (provider) => {
3750
3860
  const closedComponents = /* @__PURE__ */ new Map();
3751
3861
  const options = {
@@ -3770,21 +3880,26 @@ var Inliner = class {
3770
3880
  const changed = [];
3771
3881
  for (const provider of findProviders(this.context)) {
3772
3882
  const { element } = provider;
3773
- if (!sites.some((site) => isWithin(site.element, element) && site.element !== element)) continue;
3883
+ if (!hasSiteInside(sites, element)) continue;
3774
3884
  const isClosedNode = closedUnder(provider);
3775
- if (!element.children.every((child) => isClosedNode(child))) continue;
3885
+ let closed = true;
3886
+ for (const child of element.children) if (!isClosedNode(child)) {
3887
+ closed = false;
3888
+ break;
3889
+ }
3890
+ if (!closed) continue;
3776
3891
  if (stableValueText(this.context, provider, element) !== void 0) {
3777
3892
  this.transparent.set(element, { provider });
3778
3893
  continue;
3779
3894
  }
3780
- const host = this.hostOf(element);
3895
+ const host = this.findHost(element);
3781
3896
  if (!host) continue;
3782
3897
  const name = this.freshName(`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`);
3783
3898
  const entry = this.hosts.get(host.fn) ?? {
3784
3899
  host: host.host,
3785
3900
  texts: []
3786
3901
  };
3787
- entry.texts.push(`const ${name} = ${textOf(this.context, provider.value)};`);
3902
+ entry.texts.push(`const ${name} = ${getText(this.context, provider.value)};`);
3788
3903
  this.hosts.set(host.fn, entry);
3789
3904
  this.context.s.overwrite(provider.value.start, provider.value.end, name);
3790
3905
  this.transparent.set(element, {
@@ -3830,7 +3945,8 @@ var Inliner = class {
3830
3945
  const order = [];
3831
3946
  for (const source of spread.view.sources) {
3832
3947
  const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
3833
- for (const key of keys.filter((name) => !source.hidden.has(name))) {
3948
+ for (const key of keys) {
3949
+ if (source.hidden.has(key)) continue;
3834
3950
  const index = order.indexOf(key);
3835
3951
  if (index !== -1) order.splice(index, 1);
3836
3952
  order.push(key);
@@ -3839,7 +3955,7 @@ var Inliner = class {
3839
3955
  let names = taken.get(spread.element);
3840
3956
  if (!names) {
3841
3957
  names = /* @__PURE__ */ new Set();
3842
- for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.name : textOf(this.context, attribute.name));
3958
+ for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.name : getText(this.context, attribute.name));
3843
3959
  taken.set(spread.element, names);
3844
3960
  }
3845
3961
  const keys = /* @__PURE__ */ new Map();
@@ -3928,7 +4044,11 @@ var Inliner = class {
3928
4044
  const bare = value.primary || acceptsAnyExpression(this.context, read.member);
3929
4045
  copy.overwrite(read.member.start, read.member.end, bare ? value.text : `(${value.text})`);
3930
4046
  }
3931
- for (const [parent, pieces] of spliced) writePieces(copy, parent.children.map((child) => pieces.get(child) ?? keptPiece(child)));
4047
+ for (const [parent, pieces] of spliced) {
4048
+ const children = [];
4049
+ for (const child of parent.children) children.push(pieces.get(child) ?? keptPiece(child));
4050
+ writePieces(copy, children);
4051
+ }
3932
4052
  }
3933
4053
  splicedPiece(container, value) {
3934
4054
  if (value.kind === "children") {
@@ -3966,12 +4086,16 @@ var Inliner = class {
3966
4086
  this.scratchNames = new Set(this.context.scopes.names);
3967
4087
  try {
3968
4088
  const copiesOf = /* @__PURE__ */ new Map();
3969
- for (const [component, elements] of byComponent) copiesOf.set(component, new Map(elements.map((element) => [element, this.prepare(component, element)])));
4089
+ for (const [component, elements] of byComponent) {
4090
+ const prepared = /* @__PURE__ */ new Map();
4091
+ for (const element of elements) prepared.set(element, this.prepare(component, element));
4092
+ copiesOf.set(component, prepared);
4093
+ }
3970
4094
  this.sites = sites;
3971
4095
  this.otherUses = otherUses;
3972
4096
  this.blocked = /* @__PURE__ */ new Map();
3973
4097
  for (const [inner, byElement] of copiesOf) for (const [element, copy] of byElement) {
3974
- const outer = copy ? void 0 : this.components.find((item) => item !== inner && isWithin(element, item.fn));
4098
+ const outer = copy ? void 0 : findOuterComponent(this.components, inner, element);
3975
4099
  if (outer) {
3976
4100
  const list = this.blocked.get(outer) ?? [];
3977
4101
  list.push(inner);
@@ -3979,7 +4103,7 @@ var Inliner = class {
3979
4103
  }
3980
4104
  }
3981
4105
  for (const [component, byElement] of copiesOf) {
3982
- const copies = [...byElement.values()].filter((copy) => copy !== void 0);
4106
+ const copies = collectPrepared(byElement.values());
3983
4107
  if (copies.length === 0) continue;
3984
4108
  if (this.inliningGain(component, copies, this.removedShare(component, byElement, otherUses)) + this.enabledGain(component, copies, copiesOf, otherUses) >= 0) result.add(component);
3985
4109
  }
@@ -3997,7 +4121,7 @@ var Inliner = class {
3997
4121
  * keeps it.
3998
4122
  */
3999
4123
  removedShare(component, copies, otherUses) {
4000
- if (component.declaration === void 0 || otherUses.get(component) !== 0 || [...copies.values()].some((copy) => copy === void 0)) return 0;
4124
+ if (component.declaration === void 0 || otherUses.get(component) !== 0 || hasMissingCopy(copies.values())) return 0;
4001
4125
  const users = this.context.options.sharedComponents.get(component.binding.name) ?? 1;
4002
4126
  return users > 0 ? 1 / users : 0;
4003
4127
  }
@@ -4007,7 +4131,8 @@ var Inliner = class {
4007
4131
  * element around its call to hoist them to.
4008
4132
  */
4009
4133
  enabledGain(component, copies, copiesOf, otherUses) {
4010
- const elements = copies.map((copy) => copy.element);
4134
+ const elements = [];
4135
+ for (const copy of copies) elements.push(copy.element);
4011
4136
  this.assumedHosts = new Set(elements);
4012
4137
  let gain = 0;
4013
4138
  try {
@@ -4015,14 +4140,14 @@ var Inliner = class {
4015
4140
  if (other === component) continue;
4016
4141
  const enabled = new Map(byElement);
4017
4142
  let changed = false;
4018
- for (const [element, copy] of byElement) if (!copy && elements.some((outer) => isWithin(element, outer))) {
4143
+ for (const [element, copy] of byElement) if (!copy && isWithinAny(element, elements)) {
4019
4144
  const next = this.prepare(other, element);
4020
4145
  enabled.set(element, next);
4021
4146
  changed ||= next !== void 0;
4022
4147
  }
4023
4148
  if (!changed) continue;
4024
- const now = [...byElement.values()].filter((copy) => copy !== void 0);
4025
- const then = [...enabled.values()].filter((copy) => copy !== void 0);
4149
+ const now = collectPrepared(byElement.values());
4150
+ const then = collectPrepared(enabled.values());
4026
4151
  const before = now.length > 0 ? Math.max(0, this.inliningGain(other, now, 0)) : 0;
4027
4152
  const after = this.inliningGain(other, then, this.removedShare(other, enabled, otherUses));
4028
4153
  gain += Math.max(0, after - before);
@@ -4048,7 +4173,7 @@ var Inliner = class {
4048
4173
  calls += estimator.jsx(copy.element, inTemplate);
4049
4174
  const measured = { code: "" };
4050
4175
  inlined += copySize(this.context.filename, this.sizeHeader(), copy, inTemplate, this.context.options, blocked.length > 0 ? {
4051
- declarations: this.declarationsOf(blocked),
4176
+ declarations: this.createDeclarations(blocked),
4052
4177
  inline
4053
4178
  } : void 0, measured);
4054
4179
  const body = measured.code.slice(measured.code.lastIndexOf(COPY_WRAPPER));
@@ -4074,17 +4199,19 @@ var Inliner = class {
4074
4199
  return found;
4075
4200
  }
4076
4201
  /** The declarations of components, for a copy measured on its own. */
4077
- declarationsOf(components) {
4202
+ createDeclarations(components) {
4078
4203
  const texts = [];
4079
4204
  for (const component of components) {
4080
4205
  const statement = this.topLevelStatement(component.binding.declaration);
4081
- if (statement) texts.push(textOf(this.context, statement));
4206
+ if (statement) texts.push(getText(this.context, statement));
4082
4207
  }
4083
4208
  return texts.join("\n");
4084
4209
  }
4085
4210
  /** Whether every call of `inner` is in `outer`, and nothing else uses it. */
4086
4211
  onlyUsedWithin(inner, outer) {
4087
- return this.otherUses?.get(inner) === 0 && this.sites.every((site) => site.component !== inner || isWithin(site.element, outer.fn));
4212
+ if (this.otherUses?.get(inner) !== 0) return false;
4213
+ for (const site of this.sites) if (site.component === inner && !isWithin(site.element, outer.fn)) return false;
4214
+ return true;
4088
4215
  }
4089
4216
  /**
4090
4217
  * Whether an element sits directly in an intrinsic element, whose
@@ -4116,7 +4243,8 @@ var Inliner = class {
4116
4243
  */
4117
4244
  sizeHeader() {
4118
4245
  if (this.header === void 0) {
4119
- const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration" || isDataDeclaration(statement)).map((statement) => this.context.code.slice(statement.start, statement.end));
4246
+ const imports = [];
4247
+ for (const statement of this.context.program.body) if (statement.type === "ImportDeclaration" || isDataDeclaration(statement)) imports.push(this.context.code.slice(statement.start, statement.end));
4120
4248
  const aliases = [...this.context.options.builtInAliases.keys()];
4121
4249
  if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
4122
4250
  this.header = imports.join("\n");
@@ -4230,7 +4358,7 @@ var Inliner = class {
4230
4358
  this.report(rejected, siteReasons);
4231
4359
  const copied = [...inlined.keys()];
4232
4360
  for (const [component, elements] of inlined) {
4233
- const insideCopy = elements.some((element) => copied.some((other) => other !== component && element.start >= other.fn.start && element.end <= other.fn.end));
4361
+ const insideCopy = isInsideOtherCopy(component, elements, copied);
4234
4362
  if (component.declaration && otherUses.get(component) === 0 && !insideCopy) {
4235
4363
  this.context.s.remove(component.declaration.start, component.declaration.end);
4236
4364
  for (const alias of aliases.get(component) ?? []) this.context.s.remove(alias.start, alias.end);
@@ -4271,7 +4399,7 @@ var Inliner = class {
4271
4399
  const tag = element.openingElement.name;
4272
4400
  if (tag.type === "JSXMemberExpression") return {
4273
4401
  reason: "member-tag",
4274
- name: textOf(this.context, tag)
4402
+ name: getText(this.context, tag)
4275
4403
  };
4276
4404
  if (tag.type !== "JSXIdentifier" || !isCapitalized(tag.name)) return;
4277
4405
  const { name } = tag;
@@ -4307,7 +4435,9 @@ var Inliner = class {
4307
4435
  writeHosts() {
4308
4436
  for (const { host, texts } of this.hosts.values()) if (host.kind === "return") {
4309
4437
  const indent = this.indentationAt(host.statement.start);
4310
- this.context.s.prependRight(host.statement.start, texts.map((text) => `${text}\n${indent}`).join(""));
4438
+ let written = "";
4439
+ for (const text of texts) written += `${text}\n${indent}`;
4440
+ this.context.s.prependRight(host.statement.start, written);
4311
4441
  } else {
4312
4442
  this.context.s.prependRight(host.body.start, `{ ${texts.join(" ")} return `);
4313
4443
  this.context.s.appendLeft(host.body.end, "; }");
@@ -4322,6 +4452,60 @@ function inline(context) {
4322
4452
  inliner.run();
4323
4453
  return inliner.changed;
4324
4454
  }
4455
+ /** Whether another pending site must be done before this one. */
4456
+ function hasPendingDependency(pending, site) {
4457
+ for (const other of pending) if (other !== site && (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))) return true;
4458
+ return false;
4459
+ }
4460
+ function areDataDeclarators(declarators) {
4461
+ for (const declarator of declarators) if (declarator.id.type !== "Identifier" || declarator.init === null || !isDataLiteral(declarator.init)) return false;
4462
+ return true;
4463
+ }
4464
+ function areDataElements(elements) {
4465
+ for (const element of elements) if (element === null || element.type === "SpreadElement" || !isDataLiteral(element)) return false;
4466
+ return true;
4467
+ }
4468
+ function areDataProperties(properties) {
4469
+ for (const property of properties) if (property.type !== "Property" || property.kind !== "init" || property.method || property.computed || !isDataLiteral(property.value)) return false;
4470
+ return true;
4471
+ }
4472
+ function getSignificantChildren(children) {
4473
+ const significant = [];
4474
+ for (const child of children) if (!isInsignificant(child)) significant.push(child);
4475
+ return significant;
4476
+ }
4477
+ function hasSpreadChild(children) {
4478
+ for (const child of children) if (child.type === "JSXSpreadChild") return true;
4479
+ return false;
4480
+ }
4481
+ function hasSiteInside(sites, element) {
4482
+ for (const site of sites) if (isWithin(site.element, element) && site.element !== element) return true;
4483
+ return false;
4484
+ }
4485
+ function findOuterComponent(components, inner, element) {
4486
+ for (const item of components) if (item !== inner && isWithin(element, item.fn)) return item;
4487
+ }
4488
+ function collectPrepared(copies) {
4489
+ const prepared = [];
4490
+ for (const copy of copies) if (copy !== void 0) prepared.push(copy);
4491
+ return prepared;
4492
+ }
4493
+ function hasMissingCopy(copies) {
4494
+ for (const copy of copies) if (copy === void 0) return true;
4495
+ return false;
4496
+ }
4497
+ function isWithinAny(element, outers) {
4498
+ for (const outer of outers) if (isWithin(element, outer)) return true;
4499
+ return false;
4500
+ }
4501
+ function isInsideOtherCopy(component, elements, copied) {
4502
+ for (const element of elements) for (const other of copied) if (other !== component && element.start >= other.fn.start && element.end <= other.fn.end) return true;
4503
+ return false;
4504
+ }
4505
+ function hasChild(children, child) {
4506
+ for (const item of children) if (item === child) return true;
4507
+ return false;
4508
+ }
4325
4509
  //#endregion
4326
4510
  //#region src/memo.ts
4327
4511
  /**
@@ -4364,9 +4548,9 @@ function isFresh(node) {
4364
4548
  function constantText(context, node) {
4365
4549
  const expression = unwrap(node);
4366
4550
  switch (expression.type) {
4367
- case "Literal": return "regex" in expression ? void 0 : textOf(context, expression);
4368
- case "TemplateLiteral": return expression.expressions.length === 0 ? textOf(context, expression) : void 0;
4369
- case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? textOf(context, expression) : void 0;
4551
+ case "Literal": return "regex" in expression ? void 0 : getText(context, expression);
4552
+ case "TemplateLiteral": return expression.expressions.length === 0 ? getText(context, expression) : void 0;
4553
+ case "UnaryExpression": return expression.operator === "-" && expression.argument.type === "Literal" && typeof expression.argument.value === "number" ? getText(context, expression) : void 0;
4370
4554
  default: return;
4371
4555
  }
4372
4556
  }
@@ -4382,6 +4566,7 @@ var MemoInliner = class {
4382
4566
  constructor(context) {
4383
4567
  this.context = context;
4384
4568
  this.changed = false;
4569
+ this.edited = [];
4385
4570
  this.reason = "";
4386
4571
  }
4387
4572
  /** Whether a callee is Solid's `createMemo` or `children`. */
@@ -4440,21 +4625,21 @@ var MemoInliner = class {
4440
4625
  };
4441
4626
  }
4442
4627
  /**
4443
- * Replaces every read of a memo whose computation is a literal with the
4628
+ * Replaces every read of a memo or signal whose value is a literal with the
4444
4629
  * literal. Each reference has to be a call after the declaration, since a
4445
4630
  * call before it would throw.
4446
4631
  */
4447
- inlineConstant(declarator, binding, text) {
4632
+ inlineConstant(declarator, binding, text, kind = "memo") {
4448
4633
  const statement = this.context.parents.get(declarator);
4449
4634
  if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
4450
- this.reason = "memo-declaration";
4635
+ this.reason = `${kind}-declaration`;
4451
4636
  return;
4452
4637
  }
4453
4638
  const calls = [];
4454
4639
  for (const reference of binding.references) {
4455
4640
  const call = this.context.parents.get(reference);
4456
4641
  if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
4457
- this.reason = "memo-constant";
4642
+ this.reason = `${kind}-constant`;
4458
4643
  return;
4459
4644
  }
4460
4645
  calls.push(call);
@@ -4463,21 +4648,23 @@ var MemoInliner = class {
4463
4648
  const parent = this.context.parents.get(call);
4464
4649
  const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
4465
4650
  this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
4651
+ this.edited.push([call.start, call.end]);
4466
4652
  }
4467
4653
  this.context.s.remove(statement.start, statement.end);
4654
+ this.edited.push([statement.start, statement.end]);
4468
4655
  this.changed = true;
4469
4656
  return true;
4470
4657
  }
4471
4658
  /**
4472
4659
  * The function a node runs in.
4473
4660
  */
4474
- functionOf(node) {
4661
+ findEnclosingFunction(node) {
4475
4662
  let current = this.context.parents.get(node);
4476
4663
  while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
4477
4664
  return current;
4478
4665
  }
4479
4666
  /** The JSX expression container a call is the whole of. */
4480
- containerOf(call) {
4667
+ findContainer(call) {
4481
4668
  let parent = this.context.parents.get(call);
4482
4669
  while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
4483
4670
  return parent?.type === "JSXExpressionContainer" ? parent : void 0;
@@ -4510,11 +4697,11 @@ var MemoInliner = class {
4510
4697
  const tag = element.openingElement.name.name;
4511
4698
  if (isIntrinsicTag(tag)) {
4512
4699
  if (attribute.startsWith("on") || attribute === "ref") return false;
4513
- } else if (this.builtInOf(element.openingElement.name) !== tag || TRACKED_PROPS.get(tag) !== attribute) return false;
4700
+ } else if (this.getBuiltIn(element.openingElement.name) !== tag || TRACKED_PROPS.get(tag) !== attribute) return false;
4514
4701
  } else return false;
4515
4702
  return this.isStaticJSX(element, fn);
4516
4703
  }
4517
- builtInOf(name) {
4704
+ getBuiltIn(name) {
4518
4705
  if (name.type !== "JSXIdentifier") return;
4519
4706
  const { references } = this.context.scopes;
4520
4707
  if (!references.has(name)) return;
@@ -4539,7 +4726,7 @@ var MemoInliner = class {
4539
4726
  }
4540
4727
  case "JSXFragment":
4541
4728
  case "ParenthesizedExpression": break;
4542
- case "ReturnStatement": return this.functionOf(parent) === fn && !this.inLoop(parent, fn);
4729
+ case "ReturnStatement": return this.findEnclosingFunction(parent) === fn && !this.inLoop(parent, fn);
4543
4730
  default: return false;
4544
4731
  }
4545
4732
  node = parent;
@@ -4590,7 +4777,7 @@ var MemoInliner = class {
4590
4777
  return;
4591
4778
  }
4592
4779
  const statement = this.context.parents.get(declarator);
4593
- const fn = this.functionOf(declarator);
4780
+ const fn = this.findEnclosingFunction(declarator);
4594
4781
  if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) {
4595
4782
  this.reason = "memo-declaration";
4596
4783
  return;
@@ -4605,7 +4792,7 @@ var MemoInliner = class {
4605
4792
  this.reason = "memo-read-place";
4606
4793
  return;
4607
4794
  }
4608
- const container = this.containerOf(call);
4795
+ const container = this.findContainer(call);
4609
4796
  const owner = container ? this.context.parents.get(container) : void 0;
4610
4797
  if (kind === "children" && (!container || owner?.type !== "JSXElement" && owner?.type !== "JSXFragment")) {
4611
4798
  this.reason = "memo-read-place";
@@ -4615,18 +4802,91 @@ var MemoInliner = class {
4615
4802
  this.reason = "memo-scope";
4616
4803
  return;
4617
4804
  }
4618
- if (kind === "children" && container) this.context.s.overwrite(container.start, container.end, textOf(this.context, expression));
4619
- else this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
4805
+ if (kind === "children" && container) {
4806
+ this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
4807
+ this.edited.push([container.start, container.end]);
4808
+ } else {
4809
+ this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
4810
+ this.edited.push([call.start, call.end]);
4811
+ }
4620
4812
  this.context.s.remove(statement.start, statement.end);
4813
+ this.edited.push([statement.start, statement.end]);
4621
4814
  this.changed = true;
4622
4815
  return true;
4623
4816
  }
4817
+ /**
4818
+ * The literal a signal always holds: `const [get] = createSignal(literal)`,
4819
+ * or `const [get, set] = ...` with nothing referencing `set`. Options can
4820
+ * change what a read does, such as `unobserved`, so a signal with options
4821
+ * stays.
4822
+ */
4823
+ signalConstant(declarator, binding) {
4824
+ const pattern = declarator.id;
4825
+ const init = declarator.init ? unwrap(declarator.init) : void 0;
4826
+ if (pattern.type !== "ArrayPattern" || pattern.elements.at(0) !== binding.identifier || pattern.elements.length > 2 || init?.type !== "CallExpression" || init.callee.type !== "Identifier" || init.arguments.length !== 1) return;
4827
+ const callee = this.context.scopes.references.get(init.callee);
4828
+ if (!callee || solidExport(callee, this.context.options) !== "createSignal") return;
4829
+ const value = init.arguments.at(0);
4830
+ if (!value || value.type === "SpreadElement") return;
4831
+ const statement = this.context.parents.get(declarator);
4832
+ if (statement && this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
4833
+ const setter = pattern.elements.at(1);
4834
+ if (setter) {
4835
+ const setterBinding = setter.type === "Identifier" ? binding.scope.bindings.get(setter.name) : void 0;
4836
+ if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) return;
4837
+ }
4838
+ return constantText(this.context, value);
4839
+ }
4840
+ tryInlineSignal(declarator, binding) {
4841
+ const text = this.signalConstant(declarator, binding);
4842
+ if (text === void 0) return;
4843
+ this.reason = "";
4844
+ if (this.inlineConstant(declarator, binding, text, "signal") === void 0) recordSkip(this.context, "memos", this.reason, binding.name);
4845
+ }
4846
+ /**
4847
+ * Replaces every call of `const name = () => expression` with the
4848
+ * expression. Each reference has to be a call with no arguments after the
4849
+ * declaration, and every name in the body has to mean the same at the call.
4850
+ */
4851
+ tryInlineArrow(declarator, binding) {
4852
+ const fn = declarator.init ? unwrap(declarator.init) : void 0;
4853
+ if (fn?.type !== "ArrowFunctionExpression" || fn.async || fn.params.length > 0) return;
4854
+ const body = returnedExpression(fn);
4855
+ const statement = this.context.parents.get(declarator);
4856
+ if (!body || statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
4857
+ const calls = [];
4858
+ for (const reference of binding.references) {
4859
+ const call = this.context.parents.get(reference);
4860
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.optional || call.arguments.length > 0 || call.start < declarator.end) return;
4861
+ const parent = this.context.parents.get(call);
4862
+ if (parent?.type === "CallExpression" && parent.callee === call || parent?.type === "TaggedTemplateExpression" && parent.tag === call || parent?.type === "UnaryExpression" && parent.operator === "delete") return;
4863
+ calls.push(call);
4864
+ }
4865
+ if (calls.length === 0) return;
4866
+ for (const [start, end] of this.edited) if (start < statement.end && end > statement.start) return;
4867
+ const text = getText(this.context, body);
4868
+ const length = compactLength(text);
4869
+ if ((calls.length - 1) * length > calls.length * 3 + 13) return;
4870
+ if (usesFunctionContext(this.context, fn)) return;
4871
+ for (const call of calls) if (!this.resolvesSameAt(body, call, binding)) return;
4872
+ const bare = isPrimary(body);
4873
+ for (const call of calls) {
4874
+ const inContainer = this.context.parents.get(call)?.type === "JSXExpressionContainer" && body.type !== "SequenceExpression";
4875
+ this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
4876
+ this.edited.push([call.start, call.end]);
4877
+ }
4878
+ this.context.s.remove(statement.start, statement.end);
4879
+ this.edited.push([statement.start, statement.end]);
4880
+ this.changed = true;
4881
+ }
4624
4882
  run() {
4625
- for (const scope of new Set(this.context.scopes.scopes.values())) {
4626
- if (scope.parent === void 0) continue;
4627
- for (const binding of scope.bindings.values()) {
4628
- const { declaration } = binding;
4629
- if (binding.kind === "const" && !binding.mutated && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier) this.tryInline(declaration, binding);
4883
+ for (const scope of new Set(this.context.scopes.scopes.values())) for (const binding of scope.bindings.values()) {
4884
+ const { declaration } = binding;
4885
+ if (binding.kind !== "const" || binding.mutated || declaration.type !== "VariableDeclarator") continue;
4886
+ if (declaration.id.type === "ArrayPattern") this.tryInlineSignal(declaration, binding);
4887
+ else if (declaration.id === binding.identifier) {
4888
+ this.tryInlineArrow(declaration, binding);
4889
+ if (scope.parent !== void 0) this.tryInline(declaration, binding);
4630
4890
  }
4631
4891
  }
4632
4892
  }
@@ -4640,6 +4900,301 @@ function inlineMemos(context) {
4640
4900
  return inliner.changed;
4641
4901
  }
4642
4902
  //#endregion
4903
+ //#region src/conditional.ts
4904
+ /**
4905
+ * Rewrites each `<Show>` and `<Switch>` that can be lowered. One inside
4906
+ * another that is lowered stays for the next run, since both would edit the
4907
+ * same text.
4908
+ */
4909
+ function lowerConditionals(context) {
4910
+ let changed = false;
4911
+ visit(context.program);
4912
+ return changed;
4913
+ function visit(node) {
4914
+ if (node.type === "JSXElement") {
4915
+ const ternary = ternaryOf(context, node);
4916
+ const form = ternary === void 0 ? void 0 : formAt(context, node);
4917
+ if (ternary !== void 0 && form !== void 0) {
4918
+ context.s.overwrite(node.start, node.end, form === "container" ? `{${ternary}}` : `<>{${ternary}}</>`);
4919
+ changed = true;
4920
+ return;
4921
+ }
4922
+ }
4923
+ forEachChild(node, visit);
4924
+ }
4925
+ }
4926
+ /**
4927
+ * The Solid built-in an element's tag resolves to, by the name Solid exports it under.
4928
+ */
4929
+ function builtInOf(context, element) {
4930
+ const { name } = element.openingElement;
4931
+ if (name.type !== "JSXIdentifier") return;
4932
+ const { references } = context.scopes;
4933
+ if (!references.has(name)) return;
4934
+ const binding = references.get(name);
4935
+ const identity = binding ? solidExport(binding, context.options) : name.name;
4936
+ return identity !== void 0 && context.options.builtIns.has(identity) ? identity : void 0;
4937
+ }
4938
+ /**
4939
+ * The ternary a `<Show>` or `<Switch>` lowers to, or `undefined` when its
4940
+ * props or children need the component.
4941
+ */
4942
+ function ternaryOf(context, element) {
4943
+ switch (builtInOf(context, element)) {
4944
+ case "Show": {
4945
+ const branch = branchOf(context, element, ["when", "fallback"]);
4946
+ if (branch === void 0) return;
4947
+ return `${branch.when} ? ${branch.children} : ${branch.fallback ?? "undefined"}`;
4948
+ }
4949
+ case "Switch": return switchTernary(context, element);
4950
+ default: return;
4951
+ }
4952
+ }
4953
+ /**
4954
+ * A `<Switch>` whose children are all `<Match>`es that lower, as a chain of ternaries.
4955
+ */
4956
+ function switchTernary(context, element) {
4957
+ const props = propsOf(context, element, ["fallback"]);
4958
+ if (props === void 0) return;
4959
+ let chain = "";
4960
+ let count = 0;
4961
+ for (const child of significantChildren(element.children)) {
4962
+ if (child.type !== "JSXElement" || builtInOf(context, child) !== "Match") return;
4963
+ const branch = branchOf(context, child, ["when"]);
4964
+ if (branch === void 0) return;
4965
+ chain += `${branch.when} ? ${branch.children} : `;
4966
+ count += 1;
4967
+ }
4968
+ if (count === 0) return;
4969
+ return `${chain}${props.get("fallback") ?? "undefined"}`;
4970
+ }
4971
+ /**
4972
+ * How a `<Show>` or `<Switch>` at this position is written once lowered, or `undefined`
4973
+ * when the position is not supported.
4974
+ */
4975
+ function formAt(context, element) {
4976
+ const parent = context.parents.get(element);
4977
+ if (parent === void 0) return "fragment";
4978
+ switch (parent.type) {
4979
+ case "JSXFragment": return "container";
4980
+ case "JSXElement": {
4981
+ const { name } = parent.openingElement;
4982
+ if (name.type === "JSXNamespacedName" || name.type === "JSXIdentifier" && isIntrinsicTag(name.name)) return "container";
4983
+ return isSoleChild(parent.children, element) ? "fragment" : void 0;
4984
+ }
4985
+ case "JSXExpressionContainer":
4986
+ case "JSXAttribute": return;
4987
+ default: return isInJSXExpression(context, parent) ? void 0 : "fragment";
4988
+ }
4989
+ }
4990
+ /**
4991
+ * Whether a node is part of an expression in a JSX container, such as
4992
+ * `{ok && <Show />}`, where a function boundary does not separate them.
4993
+ */
4994
+ function isInJSXExpression(context, node) {
4995
+ let current = node;
4996
+ while (current !== void 0 && !isFunctionNode(current)) {
4997
+ if (current.type === "JSXExpressionContainer") return true;
4998
+ current = context.parents.get(current);
4999
+ }
5000
+ return false;
5001
+ }
5002
+ function isSoleChild(children, element) {
5003
+ for (const child of children) if (child !== element && !isInsignificant(child)) return false;
5004
+ return true;
5005
+ }
5006
+ /**
5007
+ * The text of each prop of an element, or `undefined` when it has a prop
5008
+ * other than `names`, a spread, or a prop more than once. A repeated prop
5009
+ * takes its last value, and a spread could hold any prop, such as `keyed`.
5010
+ */
5011
+ function propsOf(context, element, names) {
5012
+ const props = /* @__PURE__ */ new Map();
5013
+ for (const attribute of element.openingElement.attributes) {
5014
+ if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || !names.includes(attribute.name.name) || props.has(attribute.name.name)) return;
5015
+ const text = expressionText(context, attribute);
5016
+ if (text === void 0) return;
5017
+ props.set(attribute.name.name, text);
5018
+ }
5019
+ return props;
5020
+ }
5021
+ /**
5022
+ * The condition and children of a `<Show>` or `<Match>`, or `undefined` when
5023
+ * the children could need the narrowed value.
5024
+ */
5025
+ function branchOf(context, element, names) {
5026
+ const props = propsOf(context, element, names);
5027
+ const when = props?.get("when");
5028
+ if (props === void 0 || when === void 0) return;
5029
+ const significant = significantChildren(element.children);
5030
+ const sole = significant.length === 1 ? significant[0] : void 0;
5031
+ const callback = sole?.type === "JSXExpressionContainer" && sole.expression.type !== "JSXEmptyExpression" ? unwrap(sole.expression) : void 0;
5032
+ if (callback?.type === "ArrowFunctionExpression") {
5033
+ const body = callbackBody(context, element, callback);
5034
+ return body === void 0 ? void 0 : {
5035
+ when,
5036
+ children: body,
5037
+ fallback: props.get("fallback")
5038
+ };
5039
+ }
5040
+ if (sole?.type === "JSXExpressionContainer" && sole.expression.type !== "JSXEmptyExpression" && !isNeverFunction(sole.expression)) return;
5041
+ for (const child of significant) if (child.type === "JSXExpressionContainer" && child.expression.type !== "JSXEmptyExpression" && isFunctionNode(unwrap(child.expression))) return;
5042
+ const { closingElement } = element;
5043
+ const inner = closingElement ? context.s.slice(element.openingElement.end, closingElement.start) : "";
5044
+ return {
5045
+ when,
5046
+ children: significant.length > 0 ? `<>${inner}</>` : "undefined",
5047
+ fallback: props.get("fallback")
5048
+ };
5049
+ }
5050
+ /**
5051
+ * The body of a callback child with each read of its parameter written as
5052
+ * `when`, or `undefined` when that could change what the body renders.
5053
+ */
5054
+ function callbackBody(context, element, callback) {
5055
+ const param = callback.params.at(0);
5056
+ if (callback.async || callback.body.type === "BlockStatement" || callback.params.length !== 1 || param?.type !== "Identifier") return;
5057
+ const body = unwrap(callback.body);
5058
+ if (body.type !== "JSXElement" && body.type !== "JSXFragment") return;
5059
+ const value = findAttribute(element, "when")?.value;
5060
+ if (value?.type !== "JSXExpressionContainer" || value.expression.type === "JSXEmptyExpression" || !isPlainRead(context, value.expression)) return;
5061
+ const when = value.expression;
5062
+ const whenText = `(${context.s.slice(when.start, when.end)})`;
5063
+ const names = [];
5064
+ collectReferences(context, when, names);
5065
+ const binding = lookup(scopeAt(context.scopes, context.parents, param), param.name);
5066
+ if (binding?.identifier !== param || binding.mutated || namesInTypes(body, param.name)) return;
5067
+ const edits = [];
5068
+ for (const reference of binding.references) {
5069
+ if (reference.type !== "Identifier") return;
5070
+ const scope = scopeAt(context.scopes, context.parents, reference);
5071
+ for (const name of names) if (name.type !== "Identifier" || lookup(scope, name.name) !== context.scopes.references.get(name)) return;
5072
+ const parent = context.parents.get(reference);
5073
+ if (parent?.type === "CallExpression" && parent.callee === reference && parent.arguments.length === 0 && !parent.optional) edits.push({
5074
+ start: parent.start,
5075
+ end: parent.end,
5076
+ text: whenText
5077
+ });
5078
+ else if (parent?.type === "Property" && parent.shorthand) return;
5079
+ else edits.push({
5080
+ start: reference.start,
5081
+ end: reference.end,
5082
+ text: `(() => ${whenText})`
5083
+ });
5084
+ }
5085
+ edits.sort((a, b) => a.start - b.start);
5086
+ let text = "";
5087
+ let at = body.start;
5088
+ for (const edit of edits) {
5089
+ text += context.s.slice(at, edit.start) + edit.text;
5090
+ at = edit.end;
5091
+ }
5092
+ return text + context.s.slice(at, body.end);
5093
+ }
5094
+ /**
5095
+ * Whether reading an expression again gives the same value, with nothing run
5096
+ * between the reads: names, members, literals, and operators, and calls of
5097
+ * a signal or a memo. Another call could change something or return
5098
+ * something new, and `this` changes inside a `function` in the body.
5099
+ */
5100
+ function isPlainRead(context, node) {
5101
+ switch (node.type) {
5102
+ case "Identifier":
5103
+ case "Literal": return true;
5104
+ case "ParenthesizedExpression":
5105
+ case "TSAsExpression":
5106
+ case "TSSatisfiesExpression":
5107
+ case "TSNonNullExpression":
5108
+ case "ChainExpression": return isPlainRead(context, node.expression);
5109
+ case "MemberExpression": return isPlainRead(context, node.object) && (!node.computed || isPlainRead(context, node.property));
5110
+ case "CallExpression": return node.arguments.length === 0 && isAccessor(context, node.callee);
5111
+ case "UnaryExpression": return node.operator !== "delete" && isPlainRead(context, node.argument);
5112
+ case "BinaryExpression":
5113
+ case "LogicalExpression": return isPlainRead(context, node.left) && isPlainRead(context, node.right);
5114
+ case "ConditionalExpression": return isPlainRead(context, node.test) && isPlainRead(context, node.consequent) && isPlainRead(context, node.alternate);
5115
+ default: return false;
5116
+ }
5117
+ }
5118
+ /**
5119
+ * Whether a callee is the getter of a `createSignal` or a `createMemo` from
5120
+ * Solid, declared with `const` and never written, which returns its current
5121
+ * value without running anything else.
5122
+ */
5123
+ function isAccessor(context, callee) {
5124
+ if (callee.type !== "Identifier") return false;
5125
+ const binding = context.scopes.references.get(callee);
5126
+ if (!binding || binding.mutated || binding.kind !== "const") return false;
5127
+ const { declaration } = binding;
5128
+ if (declaration.type !== "VariableDeclarator" || !declaration.init) return false;
5129
+ const init = unwrap(declaration.init);
5130
+ if (init.type !== "CallExpression" || init.callee.type !== "Identifier") return false;
5131
+ const factory = context.scopes.references.get(init.callee);
5132
+ const name = factory ? solidExport(factory, context.options) : void 0;
5133
+ if (name === "createMemo") return declaration.id === binding.identifier;
5134
+ return name === "createSignal" && declaration.id.type === "ArrayPattern" && declaration.id.elements.at(0) === binding.identifier;
5135
+ }
5136
+ /** Whether a TypeScript type in `node`, like `typeof v`, names `name`. */
5137
+ function namesInTypes(node, name) {
5138
+ if (typeof node !== "object" || node === null) return false;
5139
+ if (Array.isArray(node)) {
5140
+ for (const item of node) if (namesInTypes(item, name)) return true;
5141
+ return false;
5142
+ }
5143
+ if (Reflect.get(node, "type") === "TSTypeQuery") {
5144
+ const exprName = Reflect.get(node, "exprName");
5145
+ return typeof exprName === "object" && exprName !== null && Reflect.get(exprName, "name") === name;
5146
+ }
5147
+ for (const key of Object.keys(node)) if (key !== "parent" && namesInTypes(Reflect.get(node, key), name)) return true;
5148
+ return false;
5149
+ }
5150
+ /** The identifiers in an expression that refer to a binding or a global. */
5151
+ function collectReferences(context, node, out) {
5152
+ if (node.type === "Identifier" && context.scopes.references.has(node)) out.push(node);
5153
+ forEachChild(node, (child) => {
5154
+ collectReferences(context, child, out);
5155
+ });
5156
+ }
5157
+ /**
5158
+ * Whether an expression's value can never be a function: a literal, JSX, or
5159
+ * an operator that returns a string, number, or boolean.
5160
+ */
5161
+ function isNeverFunction(node) {
5162
+ switch (node.type) {
5163
+ case "Literal":
5164
+ case "TemplateLiteral":
5165
+ case "JSXElement":
5166
+ case "JSXFragment":
5167
+ case "BinaryExpression":
5168
+ case "UnaryExpression":
5169
+ case "UpdateExpression": return true;
5170
+ case "ParenthesizedExpression": return isNeverFunction(node.expression);
5171
+ case "ConditionalExpression": return isNeverFunction(node.consequent) && isNeverFunction(node.alternate);
5172
+ case "LogicalExpression": return isNeverFunction(node.left) && isNeverFunction(node.right);
5173
+ default: return false;
5174
+ }
5175
+ }
5176
+ function significantChildren(children) {
5177
+ const result = [];
5178
+ for (const child of children) if (!isInsignificant(child)) result.push(child);
5179
+ return result;
5180
+ }
5181
+ /**
5182
+ * The text of an attribute's value as an expression, or `undefined` when the
5183
+ * value is not supported.
5184
+ */
5185
+ function expressionText(context, attribute) {
5186
+ const { value } = attribute;
5187
+ if (value === null) return;
5188
+ switch (value.type) {
5189
+ case "JSXExpressionContainer":
5190
+ if (value.expression.type === "JSXEmptyExpression") return;
5191
+ return `(${context.s.slice(value.expression.start, value.expression.end)})`;
5192
+ case "JSXElement":
5193
+ case "JSXFragment": return context.s.slice(value.start, value.end);
5194
+ default: return;
5195
+ }
5196
+ }
5197
+ //#endregion
4643
5198
  //#region src/compile.ts
4644
5199
  const DEFAULT_BUILT_INS = [
4645
5200
  "For",
@@ -4655,6 +5210,8 @@ const DEFAULT_BUILT_INS = [
4655
5210
  ];
4656
5211
  const DEFAULT_MODULE_SOURCES = ["solid-js", "@solidjs/web"];
4657
5212
  function resolveOptions(options) {
5213
+ const importedContexts = /* @__PURE__ */ new Map();
5214
+ for (const [source, names] of Object.entries(options.importedContexts ?? {})) importedContexts.set(source, new Set(names));
4658
5215
  return {
4659
5216
  builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
4660
5217
  moduleSources: options.moduleSources ?? DEFAULT_MODULE_SOURCES,
@@ -4662,7 +5219,7 @@ function resolveOptions(options) {
4662
5219
  constantVars: options.constantVars ?? false,
4663
5220
  importedConstants: resolveImportedConstants(options.importedConstants),
4664
5221
  globalConstants: new Map(Object.entries(options.globalConstants ?? {})),
4665
- importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
5222
+ importedContexts,
4666
5223
  sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
4667
5224
  alwaysInline: options.alwaysInline ?? false,
4668
5225
  alwaysInlineComponents: new Set(options.alwaysInlineComponents ?? [])
@@ -4744,6 +5301,12 @@ function compile(code, options = {}) {
4744
5301
  }
4745
5302
  if (!changed) break;
4746
5303
  }
5304
+ if (options.conditionals ?? true) for (let round = 0; round < maxPasses; round += 1) {
5305
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, lowerConditionals, void 0);
5306
+ if (!result) break;
5307
+ current = result.code;
5308
+ if (result.map !== void 0) maps.push(result.map);
5309
+ }
4747
5310
  const report = skipped ? { skipped } : {};
4748
5311
  if (maps.length === 0) return {
4749
5312
  code: current,
@@ -4773,4 +5336,4 @@ function readModuleConstants(code, options = {}) {
4773
5336
  //#endregion
4774
5337
  export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
4775
5338
 
4776
- //# sourceMappingURL=compile-Dx577-sp.mjs.map
5339
+ //# sourceMappingURL=compile-DnfP-Quf.mjs.map