solid-optimizer 1.0.0-next.5 → 1.0.0-next.7

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.
@@ -1,6 +1,7 @@
1
1
  import remapping from "@jridgewell/remapping";
2
2
  import MagicString from "magic-string";
3
3
  import { parseSync, rawTransferSupported, visitorKeys } from "oxc-parser";
4
+ import { parse, parseFragment, serialize } from "parse5";
4
5
  //#region src/ast.ts
5
6
  /**
6
7
  * Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
@@ -65,7 +66,7 @@ function collectParents(program) {
65
66
  * is several times faster to read. It is not in the options' type yet.
66
67
  */
67
68
  const RAW_TRANSFER = rawTransferSupported();
68
- function parse(filename, code) {
69
+ function parse$1(filename, code) {
69
70
  const options = {
70
71
  lang: getLanguage(filename),
71
72
  sourceType: "module",
@@ -993,7 +994,7 @@ function constantExports(program, options, scopes = analyzeScopes(program)) {
993
994
  //#endregion
994
995
  //#region src/jsx.ts
995
996
  function keptPiece(node) {
996
- const text = node.type === "JSXText" ? node : void 0;
997
+ const text = node.type === "JSXText" ? node.value : void 0;
997
998
  return {
998
999
  node,
999
1000
  text: void 0,
@@ -1008,27 +1009,39 @@ function splicePiece(s, node, children) {
1008
1009
  const first = children.at(0);
1009
1010
  const last = children.at(-1);
1010
1011
  if (!first || !last) return replacedPiece(node, "");
1011
- return {
1012
- node,
1013
- text: s.slice(first.start, last.end),
1014
- startText: first.type === "JSXText" ? first : void 0,
1015
- endText: last.type === "JSXText" ? last : void 0
1016
- };
1012
+ return replacedPiece(node, s.slice(first.start, last.end));
1017
1013
  }
1014
+ /**
1015
+ * A piece that writes `text`, JSX children as source, in place of `node`.
1016
+ *
1017
+ * The texts it starts and ends with are read from `text` itself, not from
1018
+ * the children it came from. A fold inside them in the same round can have
1019
+ * turned a child that was an element into text, such as an inner `<Show>`.
1020
+ * JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
1021
+ * mark where the text ends.
1022
+ */
1018
1023
  function replacedPiece(node, text) {
1024
+ const start = text.search(/[<{]/);
1025
+ const end = Math.max(text.lastIndexOf(">"), text.lastIndexOf("}"));
1026
+ const startText = start === -1 ? text : text.slice(0, start);
1027
+ const endText = text.slice(end + 1);
1019
1028
  return {
1020
1029
  node,
1021
1030
  text,
1022
- startText: void 0,
1023
- endText: void 0
1031
+ startText: startText === "" ? void 0 : startText,
1032
+ endText: endText === "" ? void 0 : endText
1024
1033
  };
1025
1034
  }
1035
+ /** Whether JSX drops this text: whitespace that contains a line break. */
1036
+ function isDroppedText(text) {
1037
+ return /^\s*$/.test(text) && /[\r\n]/.test(text);
1038
+ }
1026
1039
  /**
1027
1040
  * Whether two texts render differently as one text node than apart.
1028
1041
  * Two texts that JSX drops, like line breaks and indentation, still drop together.
1029
1042
  */
1030
1043
  function mergeChangesText(before, after) {
1031
- return before !== void 0 && after !== void 0 && !(isFilteredText(before) && isFilteredText(after));
1044
+ return before !== void 0 && after !== void 0 && !(isDroppedText(before) && isDroppedText(after));
1032
1045
  }
1033
1046
  /**
1034
1047
  * Puts `{}` between two pieces that meet as text, on whichever side is replaced.
@@ -1301,6 +1314,18 @@ const EMPTY = { kind: "empty" };
1301
1314
  /** Solid's calls that only create a value, so the bundler can drop an unused one. */
1302
1315
  const PURE_CALLS = /* @__PURE__ */ new Set(["lazy", "createContext"]);
1303
1316
  /**
1317
+ * Solid's calls that only create a value when their first argument is not a
1318
+ * function. Given a function, they create a computation, which runs it.
1319
+ */
1320
+ const PURE_VALUE_CALLS = /* @__PURE__ */ new Set(["createSignal", "createStore"]);
1321
+ /** Expression types that are never a function. */
1322
+ const NOT_FUNCTION = /* @__PURE__ */ new Set([
1323
+ "Literal",
1324
+ "TemplateLiteral",
1325
+ "ObjectExpression",
1326
+ "ArrayExpression"
1327
+ ]);
1328
+ /**
1304
1329
  * The globals worth folding. They only apply to a reference that binds to
1305
1330
  * nothing, which is when the global is what runs.
1306
1331
  */
@@ -1385,6 +1410,12 @@ function keyName(node) {
1385
1410
  if (node.type === "Identifier") return node.name;
1386
1411
  return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
1387
1412
  }
1413
+ /** Whether a call has no first argument, or one that is never a function. */
1414
+ function isValueArgument(call) {
1415
+ if (call.arguments.length === 0) return true;
1416
+ const first = call.arguments[0];
1417
+ return first.type !== "SpreadElement" && NOT_FUNCTION.has(unwrap(first).type);
1418
+ }
1388
1419
  /**
1389
1420
  * The text of an expression that always gives the same primitive: a literal
1390
1421
  * other than a regular expression, a template literal without expressions,
@@ -1490,12 +1521,17 @@ var Folder = class {
1490
1521
  * branch can leave it unused, and the annotation lets the bundler drop it
1491
1522
  * along with its dynamic import. `createContext` only creates the context,
1492
1523
  * which inlining can leave unused.
1524
+ *
1525
+ * `createSignal` and `createStore` with a first argument that is not a
1526
+ * function only create a node. It takes no owner and no hydration id, so
1527
+ * dropping an unused one changes nothing else.
1493
1528
  */
1494
1529
  annotatePure(call) {
1495
1530
  if (call.callee.type !== "Identifier") return;
1496
1531
  const binding = this.context.scopes.references.get(call.callee);
1497
1532
  const identity = binding ? solidExport(binding, this.context.options) : void 0;
1498
- if (identity === void 0 || !PURE_CALLS.has(identity)) return;
1533
+ if (identity === void 0) return;
1534
+ if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return;
1499
1535
  const before = this.context.code.slice(0, call.start).trimEnd();
1500
1536
  if (/(#|@)__PURE__\s*\*\/$/.test(before)) return;
1501
1537
  this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
@@ -1666,16 +1702,10 @@ var Folder = class {
1666
1702
  primary: true
1667
1703
  };
1668
1704
  case "JSXElement":
1669
- case "JSXFragment": return {
1670
- kind: "jsx",
1671
- text: getText(this.context, value)
1672
- };
1705
+ case "JSXFragment": return this.foldedJSX(value);
1673
1706
  default:
1674
1707
  if (value.expression.type === "JSXEmptyExpression") return EMPTY;
1675
- if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return {
1676
- kind: "jsx",
1677
- text: getText(this.context, value.expression)
1678
- };
1708
+ if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return this.foldedJSX(value.expression);
1679
1709
  return {
1680
1710
  kind: "expression",
1681
1711
  text: getText(this.context, value.expression),
@@ -1683,6 +1713,23 @@ var Folder = class {
1683
1713
  };
1684
1714
  }
1685
1715
  }
1716
+ /**
1717
+ * What a JSX value renders, read after the folds of this round. A fold
1718
+ * inside it can have replaced it with an expression already, such as an
1719
+ * inner `<Show>` that became its string `fallback`.
1720
+ */
1721
+ foldedJSX(node) {
1722
+ const text = getText(this.context, node);
1723
+ if (text.startsWith("<")) return {
1724
+ kind: "jsx",
1725
+ text
1726
+ };
1727
+ return {
1728
+ kind: "expression",
1729
+ text,
1730
+ primary: false
1731
+ };
1732
+ }
1686
1733
  foldShow(element) {
1687
1734
  const when = this.attributeTruthiness(element, "when");
1688
1735
  if (when === void 0) return;
@@ -1865,7 +1912,7 @@ var Folder = class {
1865
1912
  const inTemplate = this.joinsParentTemplate(element);
1866
1913
  const loop = new SizeEstimator(this.context.code).jsx(element, inTemplate) + (declaration ? compactLength(this.context.code.slice(declaration.start, declaration.end)) : 0);
1867
1914
  const code = `const __so_rows__ = ${text};`;
1868
- const statement = parse(this.context.filename, code).body.at(0);
1915
+ const statement = parse$1(this.context.filename, code).body.at(0);
1869
1916
  const init = statement?.type === "VariableDeclaration" ? statement.declarations.at(0)?.init : void 0;
1870
1917
  if (init?.type !== "JSXFragment") return false;
1871
1918
  const estimator = new SizeEstimator(code);
@@ -2699,7 +2746,7 @@ function copySize(filename, header, copy, inTemplate, options, called, measured)
2699
2746
  for (let round = 0; round < 4; round += 1) {
2700
2747
  let changed = false;
2701
2748
  for (const pass of calls ? [fold, calls.inline] : [fold]) {
2702
- const program = parse(filename, code);
2749
+ const program = parse$1(filename, code);
2703
2750
  const s = new MagicString(code);
2704
2751
  depth += 1;
2705
2752
  let passChanged;
@@ -2725,7 +2772,7 @@ function copySize(filename, header, copy, inTemplate, options, called, measured)
2725
2772
  if (!changed) break;
2726
2773
  }
2727
2774
  if (measured) measured.code = code;
2728
- const wrapper = parse(filename, code).body.at(-1);
2775
+ const wrapper = parse$1(filename, code).body.at(-1);
2729
2776
  if (wrapper?.type !== "FunctionDeclaration" || !wrapper.body) return Number.POSITIVE_INFINITY;
2730
2777
  const estimator = new SizeEstimator(code);
2731
2778
  let size = 0;
@@ -3772,7 +3819,10 @@ var Inliner = class {
3772
3819
  const statements = [...snapshots];
3773
3820
  const first = component.statements.at(0);
3774
3821
  const last = component.statements.at(-1);
3775
- if (first && last) statements.push(copy.slice(first.start, last.end));
3822
+ if (first && last) {
3823
+ const text = copy.slice(first.start, last.end);
3824
+ statements.push(text.endsWith(";") ? text : `${text};`);
3825
+ }
3776
3826
  return {
3777
3827
  element,
3778
3828
  host,
@@ -3780,19 +3830,22 @@ var Inliner = class {
3780
3830
  root: copy.slice(component.root.start, component.root.end)
3781
3831
  };
3782
3832
  }
3833
+ /** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
3834
+ addToHost(found, texts) {
3835
+ const key = found.host.kind === "return" ? found.host.statement : found.fn;
3836
+ const entry = this.hosts.get(key) ?? {
3837
+ host: found.host,
3838
+ texts: []
3839
+ };
3840
+ entry.texts.push(...texts);
3841
+ this.hosts.set(key, entry);
3842
+ }
3783
3843
  /**
3784
3844
  * Writes a prepared copy over its call site.
3785
3845
  */
3786
3846
  commit(site) {
3787
3847
  this.context.s.overwrite(site.element.start, site.element.end, site.root);
3788
- if (site.host) {
3789
- const entry = this.hosts.get(site.host.fn) ?? {
3790
- host: site.host.host,
3791
- texts: []
3792
- };
3793
- entry.texts.push(...site.statements);
3794
- this.hosts.set(site.host.fn, entry);
3795
- }
3848
+ if (site.host) this.addToHost(site.host, site.statements);
3796
3849
  }
3797
3850
  /**
3798
3851
  * The context reads in a component's copy at `element` that a closed
@@ -3872,12 +3925,7 @@ var Inliner = class {
3872
3925
  const host = this.findHost(element);
3873
3926
  if (!host) continue;
3874
3927
  const name = this.freshName(`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`);
3875
- const entry = this.hosts.get(host.fn) ?? {
3876
- host: host.host,
3877
- texts: []
3878
- };
3879
- entry.texts.push(`const ${name} = ${getText(this.context, provider.value)};`);
3880
- this.hosts.set(host.fn, entry);
3928
+ this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
3881
3929
  this.context.s.overwrite(provider.value.start, provider.value.end, name);
3882
3930
  this.transparent.set(element, {
3883
3931
  provider,
@@ -4028,17 +4076,7 @@ var Inliner = class {
4028
4076
  }
4029
4077
  }
4030
4078
  splicedPiece(container, value) {
4031
- if (value.kind === "children") {
4032
- const first = value.children.at(0);
4033
- const last = value.children.at(-1);
4034
- if (!first || !last) return replacedPiece(container, "");
4035
- return {
4036
- node: container,
4037
- text: this.context.s.slice(first.start, last.end),
4038
- startText: first.type === "JSXText" ? first : void 0,
4039
- endText: last.type === "JSXText" ? last : void 0
4040
- };
4041
- }
4079
+ if (value.kind === "children") return splicePiece(this.context.s, container, value.children);
4042
4080
  return replacedPiece(container, value.text);
4043
4081
  }
4044
4082
  /**
@@ -4495,29 +4533,81 @@ const TRACKED_PROPS = /* @__PURE__ */ new Map([
4495
4533
  ["Repeat", "count"]
4496
4534
  ]);
4497
4535
  /**
4536
+ * Global constructors whose instances are never thenable or async iterable.
4537
+ */
4538
+ const SYNC_CONSTRUCTORS = /* @__PURE__ */ new Set([
4539
+ "Array",
4540
+ "Date",
4541
+ "Error",
4542
+ "Map",
4543
+ "RegExp",
4544
+ "Set",
4545
+ "URL",
4546
+ "URLSearchParams",
4547
+ "WeakMap",
4548
+ "WeakSet"
4549
+ ]);
4550
+ /**
4551
+ * Whether an object literal can have a `then` method or `Symbol.asyncIterator`,
4552
+ * its own or from a `__proto__` it sets.
4553
+ */
4554
+ function mayBeThenable(node) {
4555
+ for (const property of node.properties) {
4556
+ if (property.type === "SpreadElement" || property.computed) return true;
4557
+ const { key } = property;
4558
+ let name;
4559
+ if (key.type === "Identifier") name = key.name;
4560
+ else if ("value" in key) name = key.value;
4561
+ if (name === "then" || name === "__proto__") return true;
4562
+ }
4563
+ return false;
4564
+ }
4565
+ /**
4498
4566
  * Whether evaluating `node` always creates a new value, which reference
4499
- * equality never finds equal to the last one.
4567
+ * equality never finds equal to the last one. The value is never a promise
4568
+ * or an async iterable, which a memo waits for and an inlined read does not.
4569
+ * `isGlobal` tells whether a name is one the module does not declare.
4500
4570
  */
4501
- function isFresh(node) {
4571
+ function isFresh(node, isGlobal) {
4502
4572
  switch (node.type) {
4503
- case "ObjectExpression":
4504
4573
  case "ArrayExpression":
4505
4574
  case "ArrowFunctionExpression":
4506
4575
  case "FunctionExpression":
4507
- case "ClassExpression":
4508
- case "NewExpression":
4509
4576
  case "JSXElement":
4510
4577
  case "JSXFragment": return true;
4511
- case "ParenthesizedExpression": return isFresh(node.expression);
4512
- case "ConditionalExpression": return isFresh(node.consequent) && isFresh(node.alternate);
4578
+ case "ObjectExpression": return !mayBeThenable(node);
4579
+ case "NewExpression": return node.callee.type === "Identifier" && SYNC_CONSTRUCTORS.has(node.callee.name) && isGlobal(node.callee);
4580
+ case "ParenthesizedExpression": return isFresh(node.expression, isGlobal);
4581
+ case "ConditionalExpression": return isFresh(node.consequent, isGlobal) && isFresh(node.alternate, isGlobal);
4513
4582
  case "SequenceExpression": {
4514
4583
  const last = node.expressions.at(-1);
4515
- return last !== void 0 && isFresh(last);
4584
+ return last !== void 0 && isFresh(last, isGlobal);
4516
4585
  }
4517
4586
  default: return false;
4518
4587
  }
4519
4588
  }
4520
4589
  /**
4590
+ * Whether `node` evaluates to a value that is never a promise or an async
4591
+ * iterable. A memo waits for one of those, and an inlined read does not.
4592
+ */
4593
+ function isSync(node, isGlobal) {
4594
+ switch (node.type) {
4595
+ case "Literal":
4596
+ case "TemplateLiteral":
4597
+ case "UnaryExpression":
4598
+ case "UpdateExpression":
4599
+ case "BinaryExpression": return true;
4600
+ case "ParenthesizedExpression": return isSync(node.expression, isGlobal);
4601
+ case "ConditionalExpression": return isSync(node.consequent, isGlobal) && isSync(node.alternate, isGlobal);
4602
+ case "LogicalExpression": return isSync(node.left, isGlobal) && isSync(node.right, isGlobal);
4603
+ case "SequenceExpression": {
4604
+ const last = node.expressions.at(-1);
4605
+ return last !== void 0 && isSync(last, isGlobal);
4606
+ }
4607
+ default: return isFresh(node, isGlobal);
4608
+ }
4609
+ }
4610
+ /**
4521
4611
  * The text of an expression that always gives the same primitive: a literal
4522
4612
  * other than a regular expression, a template literal without expressions,
4523
4613
  * or a negated number.
@@ -4543,6 +4633,7 @@ var MemoInliner = class {
4543
4633
  constructor(context) {
4544
4634
  this.context = context;
4545
4635
  this.changed = false;
4636
+ this.edited = [];
4546
4637
  this.reason = "";
4547
4638
  }
4548
4639
  /** Whether a callee is Solid's `createMemo` or `children`. */
@@ -4601,21 +4692,21 @@ var MemoInliner = class {
4601
4692
  };
4602
4693
  }
4603
4694
  /**
4604
- * Replaces every read of a memo whose computation is a literal with the
4695
+ * Replaces every read of a memo or signal whose value is a literal with the
4605
4696
  * literal. Each reference has to be a call after the declaration, since a
4606
4697
  * call before it would throw.
4607
4698
  */
4608
- inlineConstant(declarator, binding, text) {
4699
+ inlineConstant(declarator, binding, text, kind = "memo") {
4609
4700
  const statement = this.context.parents.get(declarator);
4610
4701
  if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
4611
- this.reason = "memo-declaration";
4702
+ this.reason = `${kind}-declaration`;
4612
4703
  return;
4613
4704
  }
4614
4705
  const calls = [];
4615
4706
  for (const reference of binding.references) {
4616
4707
  const call = this.context.parents.get(reference);
4617
4708
  if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
4618
- this.reason = "memo-constant";
4709
+ this.reason = `${kind}-constant`;
4619
4710
  return;
4620
4711
  }
4621
4712
  calls.push(call);
@@ -4624,8 +4715,10 @@ var MemoInliner = class {
4624
4715
  const parent = this.context.parents.get(call);
4625
4716
  const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
4626
4717
  this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
4718
+ this.edited.push([call.start, call.end]);
4627
4719
  }
4628
4720
  this.context.s.remove(statement.start, statement.end);
4721
+ this.edited.push([statement.start, statement.end]);
4629
4722
  this.changed = true;
4630
4723
  return true;
4631
4724
  }
@@ -4744,7 +4837,12 @@ var MemoInliner = class {
4744
4837
  } else {
4745
4838
  const constant = constantText(this.context, expression);
4746
4839
  if (constant !== void 0) return this.inlineConstant(declarator, binding, constant);
4747
- if (!alwaysNotifies && !isFresh(expression)) return;
4840
+ const isGlobal = (node) => this.context.scopes.references.get(node) === void 0;
4841
+ if (!alwaysNotifies && !isFresh(expression, isGlobal)) return;
4842
+ if (alwaysNotifies && !isSync(expression, isGlobal)) {
4843
+ this.reason = "memo-async";
4844
+ return;
4845
+ }
4748
4846
  }
4749
4847
  if (binding.references.length !== 1) {
4750
4848
  this.reason = "memo-reads";
@@ -4776,18 +4874,91 @@ var MemoInliner = class {
4776
4874
  this.reason = "memo-scope";
4777
4875
  return;
4778
4876
  }
4779
- if (kind === "children" && container) this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
4780
- else this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
4877
+ if (kind === "children" && container) {
4878
+ this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
4879
+ this.edited.push([container.start, container.end]);
4880
+ } else {
4881
+ this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
4882
+ this.edited.push([call.start, call.end]);
4883
+ }
4781
4884
  this.context.s.remove(statement.start, statement.end);
4885
+ this.edited.push([statement.start, statement.end]);
4782
4886
  this.changed = true;
4783
4887
  return true;
4784
4888
  }
4889
+ /**
4890
+ * The literal a signal always holds: `const [get] = createSignal(literal)`,
4891
+ * or `const [get, set] = ...` with nothing referencing `set`. Options can
4892
+ * change what a read does, such as `unobserved`, so a signal with options
4893
+ * stays.
4894
+ */
4895
+ signalConstant(declarator, binding) {
4896
+ const pattern = declarator.id;
4897
+ const init = declarator.init ? unwrap(declarator.init) : void 0;
4898
+ 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;
4899
+ const callee = this.context.scopes.references.get(init.callee);
4900
+ if (!callee || solidExport(callee, this.context.options) !== "createSignal") return;
4901
+ const value = init.arguments.at(0);
4902
+ if (!value || value.type === "SpreadElement") return;
4903
+ const statement = this.context.parents.get(declarator);
4904
+ if (statement && this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
4905
+ const setter = pattern.elements.at(1);
4906
+ if (setter) {
4907
+ const setterBinding = setter.type === "Identifier" ? binding.scope.bindings.get(setter.name) : void 0;
4908
+ if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) return;
4909
+ }
4910
+ return constantText(this.context, value);
4911
+ }
4912
+ tryInlineSignal(declarator, binding) {
4913
+ const text = this.signalConstant(declarator, binding);
4914
+ if (text === void 0) return;
4915
+ this.reason = "";
4916
+ if (this.inlineConstant(declarator, binding, text, "signal") === void 0) recordSkip(this.context, "memos", this.reason, binding.name);
4917
+ }
4918
+ /**
4919
+ * Replaces every call of `const name = () => expression` with the
4920
+ * expression. Each reference has to be a call with no arguments after the
4921
+ * declaration, and every name in the body has to mean the same at the call.
4922
+ */
4923
+ tryInlineArrow(declarator, binding) {
4924
+ const fn = declarator.init ? unwrap(declarator.init) : void 0;
4925
+ if (fn?.type !== "ArrowFunctionExpression" || fn.async || fn.params.length > 0) return;
4926
+ const body = returnedExpression(fn);
4927
+ const statement = this.context.parents.get(declarator);
4928
+ if (!body || statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
4929
+ const calls = [];
4930
+ for (const reference of binding.references) {
4931
+ const call = this.context.parents.get(reference);
4932
+ if (call?.type !== "CallExpression" || call.callee !== reference || call.optional || call.arguments.length > 0 || call.start < declarator.end) return;
4933
+ const parent = this.context.parents.get(call);
4934
+ if (parent?.type === "CallExpression" && parent.callee === call || parent?.type === "TaggedTemplateExpression" && parent.tag === call || parent?.type === "UnaryExpression" && parent.operator === "delete") return;
4935
+ calls.push(call);
4936
+ }
4937
+ if (calls.length === 0) return;
4938
+ for (const [start, end] of this.edited) if (start < statement.end && end > statement.start) return;
4939
+ const text = getText(this.context, body);
4940
+ const length = compactLength(text);
4941
+ if ((calls.length - 1) * length > calls.length * 3 + 13) return;
4942
+ if (usesFunctionContext(this.context, fn)) return;
4943
+ for (const call of calls) if (!this.resolvesSameAt(body, call, binding)) return;
4944
+ const bare = isPrimary(body);
4945
+ for (const call of calls) {
4946
+ const inContainer = this.context.parents.get(call)?.type === "JSXExpressionContainer" && body.type !== "SequenceExpression";
4947
+ this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
4948
+ this.edited.push([call.start, call.end]);
4949
+ }
4950
+ this.context.s.remove(statement.start, statement.end);
4951
+ this.edited.push([statement.start, statement.end]);
4952
+ this.changed = true;
4953
+ }
4785
4954
  run() {
4786
- for (const scope of new Set(this.context.scopes.scopes.values())) {
4787
- if (scope.parent === void 0) continue;
4788
- for (const binding of scope.bindings.values()) {
4789
- const { declaration } = binding;
4790
- if (binding.kind === "const" && !binding.mutated && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier) this.tryInline(declaration, binding);
4955
+ for (const scope of new Set(this.context.scopes.scopes.values())) for (const binding of scope.bindings.values()) {
4956
+ const { declaration } = binding;
4957
+ if (binding.kind !== "const" || binding.mutated || declaration.type !== "VariableDeclarator") continue;
4958
+ if (declaration.id.type === "ArrayPattern") this.tryInlineSignal(declaration, binding);
4959
+ else if (declaration.id === binding.identifier) {
4960
+ this.tryInlineArrow(declaration, binding);
4961
+ if (scope.parent !== void 0) this.tryInline(declaration, binding);
4791
4962
  }
4792
4963
  }
4793
4964
  }
@@ -4801,6 +4972,233 @@ function inlineMemos(context) {
4801
4972
  return inliner.changed;
4802
4973
  }
4803
4974
  //#endregion
4975
+ //#region src/nesting.ts
4976
+ const VOID_ELEMENTS = /* @__PURE__ */ new Set([
4977
+ "area",
4978
+ "base",
4979
+ "br",
4980
+ "col",
4981
+ "embed",
4982
+ "hr",
4983
+ "img",
4984
+ "input",
4985
+ "keygen",
4986
+ "link",
4987
+ "meta",
4988
+ "param",
4989
+ "source",
4990
+ "track",
4991
+ "wbr"
4992
+ ]);
4993
+ const SVG_ELEMENTS = /* @__PURE__ */ new Set([
4994
+ "altGlyph",
4995
+ "altGlyphDef",
4996
+ "altGlyphItem",
4997
+ "animate",
4998
+ "animateColor",
4999
+ "animateMotion",
5000
+ "animateTransform",
5001
+ "circle",
5002
+ "clipPath",
5003
+ "cursor",
5004
+ "defs",
5005
+ "desc",
5006
+ "ellipse",
5007
+ "feBlend",
5008
+ "feColorMatrix",
5009
+ "feComponentTransfer",
5010
+ "feComposite",
5011
+ "feConvolveMatrix",
5012
+ "feDiffuseLighting",
5013
+ "feDisplacementMap",
5014
+ "feDistantLight",
5015
+ "feDropShadow",
5016
+ "feFlood",
5017
+ "feFuncA",
5018
+ "feFuncB",
5019
+ "feFuncG",
5020
+ "feFuncR",
5021
+ "feGaussianBlur",
5022
+ "feImage",
5023
+ "feMerge",
5024
+ "feMergeNode",
5025
+ "feMorphology",
5026
+ "feOffset",
5027
+ "fePointLight",
5028
+ "feSpecularLighting",
5029
+ "feSpotLight",
5030
+ "feTile",
5031
+ "feTurbulence",
5032
+ "filter",
5033
+ "font",
5034
+ "foreignObject",
5035
+ "g",
5036
+ "glyph",
5037
+ "glyphRef",
5038
+ "hkern",
5039
+ "image",
5040
+ "line",
5041
+ "linearGradient",
5042
+ "marker",
5043
+ "mask",
5044
+ "metadata",
5045
+ "mpath",
5046
+ "path",
5047
+ "pattern",
5048
+ "polygon",
5049
+ "polyline",
5050
+ "radialGradient",
5051
+ "rect",
5052
+ "set",
5053
+ "stop",
5054
+ "switch",
5055
+ "symbol",
5056
+ "text",
5057
+ "textPath",
5058
+ "tref",
5059
+ "tspan",
5060
+ "use",
5061
+ "view",
5062
+ "vkern"
5063
+ ]);
5064
+ function findElement(parent, name) {
5065
+ for (const child of parent.childNodes) if (child.nodeName === name && "childNodes" in child) return child;
5066
+ throw new Error(`[solid-optimizer] no <${name}> in an empty document`);
5067
+ }
5068
+ let body;
5069
+ /** What a browser makes of `html` set as the body's `innerHTML`. */
5070
+ function parseAsBody(html) {
5071
+ body ??= findElement(findElement(parse("<!DOCTYPE html><html><body></body></html>"), "html"), "body");
5072
+ return serialize(parseFragment(body, html, {}));
5073
+ }
5074
+ /** The tag of an intrinsic element, or `undefined` for a component or a namespaced tag. */
5075
+ function readIntrinsicTag(element) {
5076
+ const name = element.openingElement.name;
5077
+ return name.type === "JSXIdentifier" && isIntrinsicTag(name.name) ? name.name : void 0;
5078
+ }
5079
+ /**
5080
+ * The template rooted at `element`. Elements inside fragments join it.
5081
+ * Elements in expression containers or under a component start their own.
5082
+ */
5083
+ function collectTemplate(element, tag) {
5084
+ const node = {
5085
+ element,
5086
+ tag,
5087
+ children: []
5088
+ };
5089
+ addTemplateChildren(node, element.children);
5090
+ return node;
5091
+ }
5092
+ function addTemplateChildren(node, children) {
5093
+ for (const child of children) if (child.type === "JSXFragment") addTemplateChildren(node, child.children);
5094
+ else if (child.type === "JSXElement") {
5095
+ const tag = readIntrinsicTag(child);
5096
+ if (tag !== void 0) node.children.push(collectTemplate(child, tag));
5097
+ }
5098
+ }
5099
+ /**
5100
+ * The tags of a template, without text or attributes. Elements not in
5101
+ * `included` are left out with what they hold.
5102
+ */
5103
+ function writeTags(node, included) {
5104
+ if (VOID_ELEMENTS.has(node.tag)) return `<${node.tag}>`;
5105
+ let inner = "";
5106
+ for (const child of node.children) if (included.has(child)) inner += writeTags(child, included);
5107
+ return `<${node.tag}>${inner}</${node.tag}>`;
5108
+ }
5109
+ /**
5110
+ * Table parts parse only inside a table, and SVG parts only inside an
5111
+ * `<svg>`, so a template rooted at one is checked inside one, as Solid
5112
+ * creates it.
5113
+ */
5114
+ function placeInContext(tag, html) {
5115
+ if (SVG_ELEMENTS.has(tag)) return `<svg>${html}</svg>`;
5116
+ switch (tag) {
5117
+ case "td":
5118
+ case "th": return `<table><tbody><tr>${html}</tr></tbody></table>`;
5119
+ case "tr": return `<table><tbody>${html}</tbody></table>`;
5120
+ case "col": return `<table><colgroup>${html}</colgroup></table>`;
5121
+ case "thead":
5122
+ case "tbody":
5123
+ case "tfoot":
5124
+ case "colgroup":
5125
+ case "caption": return `<table>${html}</table>`;
5126
+ default: return html;
5127
+ }
5128
+ }
5129
+ /** Whether a `<template>` in the template holds an element, which Solid rejects. */
5130
+ function hasTemplateContent(node, included) {
5131
+ for (const child of node.children) if (included.has(child) && (node.tag === "template" || hasTemplateContent(child, included))) return true;
5132
+ return false;
5133
+ }
5134
+ function isValidTemplate(root, included) {
5135
+ if (hasTemplateContent(root, included)) return false;
5136
+ const html = placeInContext(root.tag, writeTags(root, included));
5137
+ return parseAsBody(html).toLowerCase() === html.toLowerCase();
5138
+ }
5139
+ function collectAll(node, into) {
5140
+ into.add(node);
5141
+ for (const child of node.children) collectAll(child, into);
5142
+ return into;
5143
+ }
5144
+ /**
5145
+ * Wraps the elements that make `root`'s template parse differently, and
5146
+ * returns the wrapped elements, each the root of a template of its own.
5147
+ */
5148
+ function wrapInvalidElements(context, root) {
5149
+ if (isValidTemplate(root, collectAll(root, /* @__PURE__ */ new Set()))) return [];
5150
+ const included = /* @__PURE__ */ new Set([root]);
5151
+ if (!isValidTemplate(root, included)) return [];
5152
+ const wrapped = [];
5153
+ const pending = [];
5154
+ pushChildren(pending, root);
5155
+ for (let node = pending.pop(); node; node = pending.pop()) {
5156
+ included.add(node);
5157
+ if (!isValidTemplate(root, included)) {
5158
+ included.delete(node);
5159
+ context.s.prependRight(node.element.start, "{");
5160
+ context.s.appendLeft(node.element.end, "}");
5161
+ wrapped.push(node);
5162
+ continue;
5163
+ }
5164
+ pushChildren(pending, node);
5165
+ }
5166
+ return wrapped;
5167
+ }
5168
+ /** Pushes the children of `node` so that they pop in source order. */
5169
+ function pushChildren(pending, node) {
5170
+ for (let index = node.children.length - 1; index >= 0; index -= 1) pending.push(node.children[index]);
5171
+ }
5172
+ function repairNesting(context) {
5173
+ const roots = [];
5174
+ const visit = (node, inTemplate) => {
5175
+ if (node.type === "JSXElement") {
5176
+ const tag = readIntrinsicTag(node);
5177
+ if (tag !== void 0 && !inTemplate) roots.push(collectTemplate(node, tag));
5178
+ for (const child of node.children) visit(child, tag !== void 0);
5179
+ for (const attribute of node.openingElement.attributes) visit(attribute, false);
5180
+ return;
5181
+ }
5182
+ if (node.type === "JSXFragment") {
5183
+ for (const child of node.children) visit(child, inTemplate);
5184
+ return;
5185
+ }
5186
+ forEachChild(node, (child) => {
5187
+ visit(child, false);
5188
+ });
5189
+ };
5190
+ visit(context.program, false);
5191
+ let changed = false;
5192
+ for (let root = roots.pop(); root; root = roots.pop()) {
5193
+ const wrapped = wrapInvalidElements(context, root);
5194
+ if (wrapped.length > 0) {
5195
+ changed = true;
5196
+ roots.push(...wrapped);
5197
+ }
5198
+ }
5199
+ return changed;
5200
+ }
5201
+ //#endregion
4804
5202
  //#region src/conditional.ts
4805
5203
  /**
4806
5204
  * Rewrites each `<Show>` and `<Switch>` that can be lowered. One inside
@@ -5127,7 +5525,7 @@ function resolveOptions(options) {
5127
5525
  };
5128
5526
  }
5129
5527
  function parseCode(code, filename) {
5130
- const program = parse(filename, code);
5528
+ const program = parse$1(filename, code);
5131
5529
  return {
5132
5530
  code,
5133
5531
  program,
@@ -5208,6 +5606,13 @@ function compile(code, options = {}) {
5208
5606
  current = result.code;
5209
5607
  if (result.map !== void 0) maps.push(result.map);
5210
5608
  }
5609
+ if (current !== code) {
5610
+ const result = runPass(parsedCurrent(), filename, resolved, sourceMap, repairNesting, void 0);
5611
+ if (result) {
5612
+ current = result.code;
5613
+ if (result.map !== void 0) maps.push(result.map);
5614
+ }
5615
+ }
5211
5616
  const report = skipped ? { skipped } : {};
5212
5617
  if (maps.length === 0) return {
5213
5618
  code: current,
@@ -5227,7 +5632,7 @@ function compile(code, options = {}) {
5227
5632
  * Only `filename`, `importedConstants`, and `globalConstants` of the options are used.
5228
5633
  */
5229
5634
  function readModuleConstants(code, options = {}) {
5230
- const program = parse(options.filename ?? "input.jsx", code);
5635
+ const program = parse$1(options.filename ?? "input.jsx", code);
5231
5636
  const scopes = analyzeScopes(program);
5232
5637
  return {
5233
5638
  imports: namedImportSources(program, scopes),
@@ -5235,6 +5640,6 @@ function readModuleConstants(code, options = {}) {
5235
5640
  };
5236
5641
  }
5237
5642
  //#endregion
5238
- 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 };
5643
+ 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$1 as u };
5239
5644
 
5240
- //# sourceMappingURL=compile-By9Fecbx.mjs.map
5645
+ //# sourceMappingURL=compile-BuPR3pVT.mjs.map