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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/inline.ts CHANGED
@@ -73,6 +73,7 @@
73
73
  */
74
74
  import MagicString from 'magic-string';
75
75
  import type {
76
+ ArrayExpressionElement,
76
77
  ArrowFunctionExpression,
77
78
  CallExpression,
78
79
  Expression,
@@ -85,8 +86,10 @@ import type {
85
86
  JSXSpreadAttribute,
86
87
  MemberExpression,
87
88
  Node,
89
+ ObjectPropertyKind,
88
90
  ReturnStatement,
89
91
  Statement,
92
+ VariableDeclarator,
90
93
  } from 'oxc-parser';
91
94
  import {
92
95
  forEachChild,
@@ -98,7 +101,7 @@ import {
98
101
  unwrap,
99
102
  } from './ast';
100
103
  import type { PassContext, ResolvedOptions } from './context';
101
- import { isConstantDeclaration, recordSkip, solidExport, textOf } from './context';
104
+ import { getText, isConstantDeclaration, recordSkip, solidExport } from './context';
102
105
  import type { Piece } from './jsx';
103
106
  import type { ClosedOptions, Provider } from './provider';
104
107
  import {
@@ -222,14 +225,12 @@ function inBodyOrder(sites: readonly CallSite[]): CallSite[] {
222
225
  const pending = [...sites];
223
226
  const ordered: CallSite[] = [];
224
227
  while (pending.length > 0) {
225
- const ready = pending.filter(
226
- (site) =>
227
- !pending.some(
228
- (other) =>
229
- other !== site &&
230
- (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element)),
231
- ),
232
- );
228
+ const ready: CallSite[] = [];
229
+ for (const site of pending) {
230
+ if (!hasPendingDependency(pending, site)) {
231
+ ready.push(site);
232
+ }
233
+ }
233
234
  // A cycle cannot happen, since a component that renders itself is not
234
235
  // inlined, but falling back to the original order keeps this finite.
235
236
  const next = ready.length > 0 ? ready : pending;
@@ -452,7 +453,10 @@ function acceptsAnyExpression(context: PassContext, node: Node): boolean {
452
453
  /**
453
454
  * The function a top-level binding declares, if it declares one.
454
455
  */
455
- function functionOf(binding: Binding, options: ResolvedOptions): ComponentFunction | undefined {
456
+ function findComponentFunction(
457
+ binding: Binding,
458
+ options: ResolvedOptions,
459
+ ): ComponentFunction | undefined {
456
460
  const { declaration } = binding;
457
461
  if (binding.kind === 'function' && declaration.type === 'FunctionDeclaration') {
458
462
  return declaration;
@@ -474,7 +478,7 @@ function functionOf(binding: Binding, options: ResolvedOptions): ComponentFuncti
474
478
  /**
475
479
  * The attributes of a call site by name, or `undefined` when one cannot be inlined.
476
480
  */
477
- function attributesOf(
481
+ function getAttributes(
478
482
  context: PassContext,
479
483
  element: JSXElement,
480
484
  ): Map<string, JSXAttribute> | undefined {
@@ -507,12 +511,7 @@ function isDataDeclaration(statement: Node): boolean {
507
511
  declaration?.type === 'VariableDeclaration' &&
508
512
  !declaration.declare &&
509
513
  (declaration.kind === 'const' || declaration.kind === 'var') &&
510
- declaration.declarations.every(
511
- (declarator) =>
512
- declarator.id.type === 'Identifier' &&
513
- declarator.init !== null &&
514
- isDataLiteral(declarator.init),
515
- )
514
+ areDataDeclarators(declaration.declarations)
516
515
  );
517
516
  }
518
517
 
@@ -530,18 +529,9 @@ function isDataLiteral(node: Expression): boolean {
530
529
  case 'TSSatisfiesExpression':
531
530
  return isDataLiteral(node.expression);
532
531
  case 'ArrayExpression':
533
- return node.elements.every(
534
- (element) => element !== null && element.type !== 'SpreadElement' && isDataLiteral(element),
535
- );
532
+ return areDataElements(node.elements);
536
533
  case 'ObjectExpression':
537
- return node.properties.every(
538
- (property) =>
539
- property.type === 'Property' &&
540
- property.kind === 'init' &&
541
- !property.method &&
542
- !property.computed &&
543
- isDataLiteral(property.value),
544
- );
534
+ return areDataProperties(node.properties);
545
535
  default:
546
536
  return false;
547
537
  }
@@ -571,10 +561,12 @@ function isForwardableRef(context: PassContext, attribute: JSXAttribute): boolea
571
561
  const isTarget = (node: Expression, inArray: boolean): boolean => {
572
562
  const inner = unwrap(node);
573
563
  if (inner.type === 'ArrayExpression') {
574
- return inner.elements.every(
575
- (element) =>
576
- element !== null && element.type !== 'SpreadElement' && isTarget(element, true),
577
- );
564
+ for (const element of inner.elements) {
565
+ if (element === null || element.type === 'SpreadElement' || !isTarget(element, true)) {
566
+ return false;
567
+ }
568
+ }
569
+ return true;
578
570
  }
579
571
  if (inArray && isVoidZero(inner)) {
580
572
  return true;
@@ -610,7 +602,7 @@ function isVoidZero(node: Expression): boolean {
610
602
  }
611
603
 
612
604
  /**
613
- * Why `attributesOf` turns down a call site, for the report.
605
+ * Why `getAttributes` turns down a call site, for the report.
614
606
  */
615
607
  function callSiteReason(element: JSXElement): string {
616
608
  const names = new Set<string>();
@@ -718,7 +710,7 @@ class Inliner {
718
710
  // Components
719
711
  // ---------------------------------------------------------------------------
720
712
 
721
- private scopeOf(fn: ComponentFunction): Scope | undefined {
713
+ private getScope(fn: ComponentFunction): Scope | undefined {
722
714
  return this.context.scopes.scopes.get(fn);
723
715
  }
724
716
 
@@ -782,7 +774,7 @@ class Inliner {
782
774
  this.reason = 'component-mutated';
783
775
  return undefined;
784
776
  }
785
- const fn = functionOf(binding, this.context.options);
777
+ const fn = findComponentFunction(binding, this.context.options);
786
778
  if (!fn?.body) {
787
779
  this.reason = this.isLazy(binding) ? 'lazy' : 'not-a-function';
788
780
  return undefined;
@@ -795,7 +787,7 @@ class Inliner {
795
787
  this.reason = 'component-params';
796
788
  return undefined;
797
789
  }
798
- const scope = this.scopeOf(fn);
790
+ const scope = this.getScope(fn);
799
791
  if (!scope) {
800
792
  this.reason = 'not-a-function';
801
793
  return undefined;
@@ -878,7 +870,13 @@ class Inliner {
878
870
  }
879
871
  props.push(read);
880
872
  }
881
- statements = statements.filter((statement) => !views.declarations.includes(statement));
873
+ const kept: typeof statements = [];
874
+ for (const statement of statements) {
875
+ if (!views.declarations.includes(statement)) {
876
+ kept.push(statement);
877
+ }
878
+ }
879
+ statements = kept;
882
880
 
883
881
  const locals: Binding[] = [];
884
882
  const seen = new Set<Scope>();
@@ -1013,7 +1011,7 @@ class Inliner {
1013
1011
  if (init.type !== 'CallExpression' || (callee !== 'merge' && callee !== 'omit')) {
1014
1012
  continue;
1015
1013
  }
1016
- const view = this.viewOf(callee, init, param, views, args);
1014
+ const view = this.resolveView(callee, init, param, views, args);
1017
1015
  if (view === null) {
1018
1016
  continue;
1019
1017
  }
@@ -1085,20 +1083,20 @@ class Inliner {
1085
1083
  * take the props or another view, and `undefined` when it does but its
1086
1084
  * other arguments are not literal.
1087
1085
  */
1088
- private viewOf(
1086
+ private resolveView(
1089
1087
  callee: 'merge' | 'omit',
1090
1088
  call: CallExpression,
1091
1089
  param: Binding | undefined,
1092
1090
  views: ReadonlyMap<Binding, View>,
1093
1091
  args: Set<Node>,
1094
1092
  ): View | null | undefined {
1095
- const sourcesOf = (argument: Node): readonly ViewSource[] | undefined => {
1093
+ const resolveSources = (argument: Node): readonly ViewSource[] | undefined => {
1096
1094
  if (argument.type === 'CallExpression') {
1097
1095
  const nested = solidCallee(this.context, argument);
1098
1096
  if (nested !== 'merge' && nested !== 'omit') {
1099
1097
  return undefined;
1100
1098
  }
1101
- return this.viewOf(nested, argument, param, views, args)?.sources;
1099
+ return this.resolveView(nested, argument, param, views, args)?.sources;
1102
1100
  }
1103
1101
  if (argument.type !== 'Identifier') {
1104
1102
  return undefined;
@@ -1109,9 +1107,13 @@ class Inliner {
1109
1107
  }
1110
1108
  return binding ? views.get(binding)?.sources : undefined;
1111
1109
  };
1112
- const involved = call.arguments.some(
1113
- (argument) => argument.type !== 'SpreadElement' && sourcesOf(unwrap(argument)) !== undefined,
1114
- );
1110
+ let involved = false;
1111
+ for (const argument of call.arguments) {
1112
+ if (argument.type !== 'SpreadElement' && resolveSources(unwrap(argument)) !== undefined) {
1113
+ involved = true;
1114
+ break;
1115
+ }
1116
+ }
1115
1117
  if (!involved) {
1116
1118
  return null;
1117
1119
  }
@@ -1123,7 +1125,7 @@ class Inliner {
1123
1125
  return undefined;
1124
1126
  }
1125
1127
  const target = unwrap(first);
1126
- const base = sourcesOf(target);
1128
+ const base = resolveSources(target);
1127
1129
  const hidden: string[] = [];
1128
1130
  for (const key of keys) {
1129
1131
  if (key.type !== 'Literal' || typeof key.value !== 'string') {
@@ -1135,12 +1137,14 @@ class Inliner {
1135
1137
  return undefined;
1136
1138
  }
1137
1139
  args.add(target);
1138
- return {
1139
- sources: base.map((source) => ({
1140
+ const hiddenSources: ViewSource[] = [];
1141
+ for (const source of base) {
1142
+ hiddenSources.push({
1140
1143
  defaults: source.defaults,
1141
1144
  hidden: new Set([...source.hidden, ...hidden]),
1142
- })),
1143
- };
1145
+ });
1146
+ }
1147
+ return { sources: hiddenSources };
1144
1148
  }
1145
1149
 
1146
1150
  const sources: ViewSource[] = [];
@@ -1149,7 +1153,7 @@ class Inliner {
1149
1153
  return undefined;
1150
1154
  }
1151
1155
  const inner = unwrap(argument);
1152
- const existing = sourcesOf(inner);
1156
+ const existing = resolveSources(inner);
1153
1157
  if (existing) {
1154
1158
  args.add(inner);
1155
1159
  sources.push(...existing);
@@ -1244,7 +1248,7 @@ class Inliner {
1244
1248
  if (element?.type !== 'JSXElement') {
1245
1249
  return false;
1246
1250
  }
1247
- const significant = element.children.filter((child) => !isInsignificant(child));
1251
+ const significant = getSignificantChildren(element.children);
1248
1252
  if (significant.length !== 1 || significant.at(0) !== parent) {
1249
1253
  return false;
1250
1254
  }
@@ -1279,7 +1283,7 @@ class Inliner {
1279
1283
  * returned JSX of a component, with only intrinsic elements, fragments,
1280
1284
  * and closed providers between it and the call.
1281
1285
  */
1282
- private hostOf(element: JSXElement): { fn: Node; host: Host } | undefined {
1286
+ private findHost(element: JSXElement): { fn: Node; host: Host } | undefined {
1283
1287
  let node: Node = element;
1284
1288
  for (;;) {
1285
1289
  const parent = this.context.parents.get(node);
@@ -1296,7 +1300,7 @@ class Inliner {
1296
1300
  return undefined;
1297
1301
  }
1298
1302
  const child = node;
1299
- if (!parent.children.some((item) => item === child)) {
1303
+ if (!hasChild(parent.children, child)) {
1300
1304
  return undefined;
1301
1305
  }
1302
1306
  } else if (parent.type === 'ReturnStatement') {
@@ -1331,10 +1335,10 @@ class Inliner {
1331
1335
  snapshots: string[],
1332
1336
  ): PropValue | undefined {
1333
1337
  const { children } = element;
1334
- if (children.some((child) => child.type === 'JSXSpreadChild')) {
1338
+ if (hasSpreadChild(children)) {
1335
1339
  return undefined;
1336
1340
  }
1337
- const significant = children.filter((child) => !isInsignificant(child));
1341
+ const significant = getSignificantChildren(children);
1338
1342
  const only = significant.at(0);
1339
1343
  if (only === undefined) {
1340
1344
  return { kind: 'children', children: [], text: 'void 0', primary: false };
@@ -1350,13 +1354,13 @@ class Inliner {
1350
1354
  // A single expression is passed like an attribute value, such as a
1351
1355
  // render function that has to stay one function.
1352
1356
  const value = this.expressionValue(only.expression, 'children', reads, fresh, snapshots);
1353
- if (value.kind !== 'expression' || value.text !== textOf(this.context, only.expression)) {
1357
+ if (value.kind !== 'expression' || value.text !== getText(this.context, only.expression)) {
1354
1358
  return value;
1355
1359
  }
1356
1360
  text = value.text;
1357
1361
  primary = value.primary;
1358
1362
  } else {
1359
- text = textOf(this.context, only);
1363
+ text = getText(this.context, only);
1360
1364
  }
1361
1365
  return { kind: 'children', children, text, primary };
1362
1366
  }
@@ -1395,7 +1399,7 @@ class Inliner {
1395
1399
  return undefined;
1396
1400
  }
1397
1401
  }
1398
- return { kind: 'expression', text: textOf(this.context, value.expression), primary: true };
1402
+ return { kind: 'expression', text: getText(this.context, value.expression), primary: true };
1399
1403
  }
1400
1404
 
1401
1405
  /**
@@ -1424,7 +1428,7 @@ class Inliner {
1424
1428
  return { kind: 'expression', text: JSON.stringify(value.value), primary: true };
1425
1429
  case 'JSXElement':
1426
1430
  case 'JSXFragment':
1427
- return { kind: 'jsx', text: textOf(this.context, value), primary: true };
1431
+ return { kind: 'jsx', text: getText(this.context, value), primary: true };
1428
1432
  default:
1429
1433
  break;
1430
1434
  }
@@ -1446,7 +1450,7 @@ class Inliner {
1446
1450
  fresh: (base: string) => string,
1447
1451
  snapshots: string[],
1448
1452
  ): PropValue {
1449
- const text = textOf(this.context, expression);
1453
+ const text = getText(this.context, expression);
1450
1454
  const inner = unwrap(expression);
1451
1455
  if (inner.type === 'JSXElement' || inner.type === 'JSXFragment') {
1452
1456
  return { kind: 'jsx', text, primary: true };
@@ -1521,12 +1525,12 @@ class Inliner {
1521
1525
  * when it cannot be inlined there. Nothing is written until `commit`.
1522
1526
  */
1523
1527
  private prepare(component: Component, element: JSXElement): PreparedSite | undefined {
1524
- const attributes = attributesOf(this.context, element);
1528
+ const attributes = getAttributes(this.context, element);
1525
1529
  if (!attributes) {
1526
1530
  this.reason = callSiteReason(element);
1527
1531
  return undefined;
1528
1532
  }
1529
- const hasChildren = element.children.some((child) => !isInsignificant(child));
1533
+ const hasChildren = getSignificantChildren(element.children).length > 0;
1530
1534
  if (hasChildren && attributes.has('children')) {
1531
1535
  this.reason = 'call-children';
1532
1536
  return undefined;
@@ -1598,7 +1602,7 @@ class Inliner {
1598
1602
  }
1599
1603
 
1600
1604
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
1601
- const host = hoisted ? this.hostOf(element) : undefined;
1605
+ const host = hoisted ? this.findHost(element) : undefined;
1602
1606
  if (hoisted && !host) {
1603
1607
  this.reason = 'no-host';
1604
1608
  return undefined;
@@ -1702,7 +1706,10 @@ class Inliner {
1702
1706
  components: readonly Component[],
1703
1707
  sites: readonly CallSite[],
1704
1708
  ): Component[] {
1705
- const byBinding = new Map(components.map((component) => [component.binding, component]));
1709
+ const byBinding = new Map<Binding, Component>();
1710
+ for (const component of components) {
1711
+ byBinding.set(component.binding, component);
1712
+ }
1706
1713
  // Whether a subtree under `provider` only runs visible code, where each
1707
1714
  // component is one this pass inlines and is visible too.
1708
1715
  const closedUnder = (provider: Provider): ((node: Node) => boolean) => {
@@ -1735,14 +1742,18 @@ class Inliner {
1735
1742
  const changed: Component[] = [];
1736
1743
  for (const provider of findProviders(this.context)) {
1737
1744
  const { element } = provider;
1738
- const inside = sites.some(
1739
- (site) => isWithin(site.element, element) && site.element !== element,
1740
- );
1745
+ const inside = hasSiteInside(sites, element);
1741
1746
  if (!inside) {
1742
1747
  continue;
1743
1748
  }
1744
1749
  const isClosedNode = closedUnder(provider);
1745
- const closed = element.children.every((child) => isClosedNode(child));
1750
+ let closed = true;
1751
+ for (const child of element.children) {
1752
+ if (!isClosedNode(child)) {
1753
+ closed = false;
1754
+ break;
1755
+ }
1756
+ }
1746
1757
  if (!closed) {
1747
1758
  continue;
1748
1759
  }
@@ -1750,7 +1761,7 @@ class Inliner {
1750
1761
  this.transparent.set(element, { provider });
1751
1762
  continue;
1752
1763
  }
1753
- const host = this.hostOf(element);
1764
+ const host = this.findHost(element);
1754
1765
  if (!host) {
1755
1766
  continue;
1756
1767
  }
@@ -1758,7 +1769,7 @@ class Inliner {
1758
1769
  `${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
1759
1770
  );
1760
1771
  const entry = this.hosts.get(host.fn) ?? { host: host.host, texts: [] };
1761
- entry.texts.push(`const ${name} = ${textOf(this.context, provider.value)};`);
1772
+ entry.texts.push(`const ${name} = ${getText(this.context, provider.value)};`);
1762
1773
  this.hosts.set(host.fn, entry);
1763
1774
  this.context.s.overwrite(provider.value.start, provider.value.end, name);
1764
1775
  this.transparent.set(element, { provider, text: name });
@@ -1826,7 +1837,10 @@ class Inliner {
1826
1837
  const order: string[] = [];
1827
1838
  for (const source of spread.view.sources) {
1828
1839
  const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
1829
- for (const key of keys.filter((name) => !source.hidden.has(name))) {
1840
+ for (const key of keys) {
1841
+ if (source.hidden.has(key)) {
1842
+ continue;
1843
+ }
1830
1844
  const index = order.indexOf(key);
1831
1845
  if (index !== -1) {
1832
1846
  order.splice(index, 1);
@@ -1842,7 +1856,7 @@ class Inliner {
1842
1856
  names.add(
1843
1857
  attribute.name.type === 'JSXIdentifier'
1844
1858
  ? attribute.name.name
1845
- : textOf(this.context, attribute.name),
1859
+ : getText(this.context, attribute.name),
1846
1860
  );
1847
1861
  }
1848
1862
  }
@@ -1989,10 +2003,11 @@ class Inliner {
1989
2003
  copy.overwrite(read.member.start, read.member.end, bare ? value.text : `(${value.text})`);
1990
2004
  }
1991
2005
  for (const [parent, pieces] of spliced) {
1992
- writePieces(
1993
- copy,
1994
- parent.children.map((child) => pieces.get(child) ?? keptPiece(child)),
1995
- );
2006
+ const children: Piece[] = [];
2007
+ for (const child of parent.children) {
2008
+ children.push(pieces.get(child) ?? keptPiece(child));
2009
+ }
2010
+ writePieces(copy, children);
1996
2011
  }
1997
2012
  }
1998
2013
 
@@ -2051,19 +2066,18 @@ class Inliner {
2051
2066
  try {
2052
2067
  const copiesOf = new Map<Component, Map<JSXElement, PreparedSite | undefined>>();
2053
2068
  for (const [component, elements] of byComponent) {
2054
- copiesOf.set(
2055
- component,
2056
- new Map(elements.map((element) => [element, this.prepare(component, element)])),
2057
- );
2069
+ const prepared = new Map<JSXElement, PreparedSite | undefined>();
2070
+ for (const element of elements) {
2071
+ prepared.set(element, this.prepare(component, element));
2072
+ }
2073
+ copiesOf.set(component, prepared);
2058
2074
  }
2059
2075
  this.sites = sites;
2060
2076
  this.otherUses = otherUses;
2061
2077
  this.blocked = new Map();
2062
2078
  for (const [inner, byElement] of copiesOf) {
2063
2079
  for (const [element, copy] of byElement) {
2064
- const outer = copy
2065
- ? undefined
2066
- : this.components.find((item) => item !== inner && isWithin(element, item.fn));
2080
+ const outer = copy ? undefined : findOuterComponent(this.components, inner, element);
2067
2081
  if (outer) {
2068
2082
  const list = this.blocked.get(outer) ?? [];
2069
2083
  list.push(inner);
@@ -2072,7 +2086,7 @@ class Inliner {
2072
2086
  }
2073
2087
  }
2074
2088
  for (const [component, byElement] of copiesOf) {
2075
- const copies = [...byElement.values()].filter((copy) => copy !== undefined);
2089
+ const copies = collectPrepared(byElement.values());
2076
2090
  if (copies.length === 0) {
2077
2091
  continue;
2078
2092
  }
@@ -2105,7 +2119,7 @@ class Inliner {
2105
2119
  if (
2106
2120
  component.declaration === undefined ||
2107
2121
  otherUses.get(component) !== 0 ||
2108
- [...copies.values()].some((copy) => copy === undefined)
2122
+ hasMissingCopy(copies.values())
2109
2123
  ) {
2110
2124
  return 0;
2111
2125
  }
@@ -2124,7 +2138,10 @@ class Inliner {
2124
2138
  copiesOf: ReadonlyMap<Component, ReadonlyMap<JSXElement, PreparedSite | undefined>>,
2125
2139
  otherUses: ReadonlyMap<Component, number>,
2126
2140
  ): number {
2127
- const elements = copies.map((copy) => copy.element);
2141
+ const elements: JSXElement[] = [];
2142
+ for (const copy of copies) {
2143
+ elements.push(copy.element);
2144
+ }
2128
2145
  this.assumedHosts = new Set(elements);
2129
2146
  let gain = 0;
2130
2147
  try {
@@ -2135,7 +2152,7 @@ class Inliner {
2135
2152
  const enabled = new Map(byElement);
2136
2153
  let changed = false;
2137
2154
  for (const [element, copy] of byElement) {
2138
- if (!copy && elements.some((outer) => isWithin(element, outer))) {
2155
+ if (!copy && isWithinAny(element, elements)) {
2139
2156
  const next = this.prepare(other, element);
2140
2157
  enabled.set(element, next);
2141
2158
  changed ||= next !== undefined;
@@ -2144,8 +2161,8 @@ class Inliner {
2144
2161
  if (!changed) {
2145
2162
  continue;
2146
2163
  }
2147
- const now = [...byElement.values()].filter((copy) => copy !== undefined);
2148
- const then = [...enabled.values()].filter((copy) => copy !== undefined);
2164
+ const now = collectPrepared(byElement.values());
2165
+ const then = collectPrepared(enabled.values());
2149
2166
  const before = now.length > 0 ? Math.max(0, this.inliningGain(other, now, 0)) : 0;
2150
2167
  const after = this.inliningGain(other, then, this.removedShare(other, enabled, otherUses));
2151
2168
  gain += Math.max(0, after - before);
@@ -2183,7 +2200,7 @@ class Inliner {
2183
2200
  copy,
2184
2201
  inTemplate,
2185
2202
  this.context.options,
2186
- blocked.length > 0 ? { declarations: this.declarationsOf(blocked), inline } : undefined,
2203
+ blocked.length > 0 ? { declarations: this.createDeclarations(blocked), inline } : undefined,
2187
2204
  measured,
2188
2205
  );
2189
2206
  const body = measured.code.slice(measured.code.lastIndexOf(COPY_WRAPPER));
@@ -2225,12 +2242,12 @@ class Inliner {
2225
2242
  }
2226
2243
 
2227
2244
  /** The declarations of components, for a copy measured on its own. */
2228
- private declarationsOf(components: readonly Component[]): string {
2245
+ private createDeclarations(components: readonly Component[]): string {
2229
2246
  const texts: string[] = [];
2230
2247
  for (const component of components) {
2231
2248
  const statement = this.topLevelStatement(component.binding.declaration);
2232
2249
  if (statement) {
2233
- texts.push(textOf(this.context, statement));
2250
+ texts.push(getText(this.context, statement));
2234
2251
  }
2235
2252
  }
2236
2253
  return texts.join('\n');
@@ -2238,10 +2255,15 @@ class Inliner {
2238
2255
 
2239
2256
  /** Whether every call of `inner` is in `outer`, and nothing else uses it. */
2240
2257
  private onlyUsedWithin(inner: Component, outer: Component): boolean {
2241
- return (
2242
- this.otherUses?.get(inner) === 0 &&
2243
- this.sites.every((site) => site.component !== inner || isWithin(site.element, outer.fn))
2244
- );
2258
+ if (this.otherUses?.get(inner) !== 0) {
2259
+ return false;
2260
+ }
2261
+ for (const site of this.sites) {
2262
+ if (site.component === inner && !isWithin(site.element, outer.fn)) {
2263
+ return false;
2264
+ }
2265
+ }
2266
+ return true;
2245
2267
  }
2246
2268
 
2247
2269
  /**
@@ -2312,11 +2334,12 @@ class Inliner {
2312
2334
  if (this.header === undefined) {
2313
2335
  // The imports, and the top-level constants made of literals, so a copy
2314
2336
  // folds with the values its call site passes by name, as it will once inlined.
2315
- const imports = this.context.program.body
2316
- .filter(
2317
- (statement) => statement.type === 'ImportDeclaration' || isDataDeclaration(statement),
2318
- )
2319
- .map((statement) => this.context.code.slice(statement.start, statement.end));
2337
+ const imports: string[] = [];
2338
+ for (const statement of this.context.program.body) {
2339
+ if (statement.type === 'ImportDeclaration' || isDataDeclaration(statement)) {
2340
+ imports.push(this.context.code.slice(statement.start, statement.end));
2341
+ }
2342
+ }
2320
2343
  const aliases = [...this.context.options.builtInAliases.keys()];
2321
2344
  if (aliases.length > 0) {
2322
2345
  imports.push(`var ${aliases.join(', ')};`);
@@ -2503,12 +2526,7 @@ class Inliner {
2503
2526
  // component stays until the next pass inlines it there too.
2504
2527
  const copied = [...inlined.keys()];
2505
2528
  for (const [component, elements] of inlined) {
2506
- const insideCopy = elements.some((element) =>
2507
- copied.some(
2508
- (other) =>
2509
- other !== component && element.start >= other.fn.start && element.end <= other.fn.end,
2510
- ),
2511
- );
2529
+ const insideCopy = isInsideOtherCopy(component, elements, copied);
2512
2530
  if (component.declaration && otherUses.get(component) === 0 && !insideCopy) {
2513
2531
  this.context.s.remove(component.declaration.start, component.declaration.end);
2514
2532
  for (const alias of aliases.get(component) ?? []) {
@@ -2566,7 +2584,7 @@ class Inliner {
2566
2584
  ): { reason: string; name: string } | undefined {
2567
2585
  const tag = element.openingElement.name;
2568
2586
  if (tag.type === 'JSXMemberExpression') {
2569
- return { reason: 'member-tag', name: textOf(this.context, tag) };
2587
+ return { reason: 'member-tag', name: getText(this.context, tag) };
2570
2588
  }
2571
2589
  if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
2572
2590
  return undefined;
@@ -2608,10 +2626,11 @@ class Inliner {
2608
2626
  for (const { host, texts } of this.hosts.values()) {
2609
2627
  if (host.kind === 'return') {
2610
2628
  const indent = this.indentationAt(host.statement.start);
2611
- this.context.s.prependRight(
2612
- host.statement.start,
2613
- texts.map((text) => `${text}\n${indent}`).join(''),
2614
- );
2629
+ let written = '';
2630
+ for (const text of texts) {
2631
+ written += `${text}\n${indent}`;
2632
+ }
2633
+ this.context.s.prependRight(host.statement.start, written);
2615
2634
  } else {
2616
2635
  this.context.s.prependRight(host.body.start, `{ ${texts.join(' ')} return `);
2617
2636
  this.context.s.appendLeft(host.body.end, '; }');
@@ -2628,3 +2647,146 @@ export function inline(context: PassContext): boolean {
2628
2647
  inliner.run();
2629
2648
  return inliner.changed;
2630
2649
  }
2650
+
2651
+ /** Whether another pending site must be done before this one. */
2652
+ function hasPendingDependency(pending: readonly CallSite[], site: CallSite): boolean {
2653
+ for (const other of pending) {
2654
+ if (
2655
+ other !== site &&
2656
+ (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))
2657
+ ) {
2658
+ return true;
2659
+ }
2660
+ }
2661
+ return false;
2662
+ }
2663
+
2664
+ function areDataDeclarators(declarators: readonly VariableDeclarator[]): boolean {
2665
+ for (const declarator of declarators) {
2666
+ if (
2667
+ declarator.id.type !== 'Identifier' ||
2668
+ declarator.init === null ||
2669
+ !isDataLiteral(declarator.init)
2670
+ ) {
2671
+ return false;
2672
+ }
2673
+ }
2674
+ return true;
2675
+ }
2676
+
2677
+ function areDataElements(elements: readonly (ArrayExpressionElement | null)[]): boolean {
2678
+ for (const element of elements) {
2679
+ if (element === null || element.type === 'SpreadElement' || !isDataLiteral(element)) {
2680
+ return false;
2681
+ }
2682
+ }
2683
+ return true;
2684
+ }
2685
+
2686
+ function areDataProperties(properties: readonly ObjectPropertyKind[]): boolean {
2687
+ for (const property of properties) {
2688
+ if (
2689
+ property.type !== 'Property' ||
2690
+ property.kind !== 'init' ||
2691
+ property.method ||
2692
+ property.computed ||
2693
+ !isDataLiteral(property.value)
2694
+ ) {
2695
+ return false;
2696
+ }
2697
+ }
2698
+ return true;
2699
+ }
2700
+
2701
+ function getSignificantChildren(children: readonly JSXChild[]): JSXChild[] {
2702
+ const significant: JSXChild[] = [];
2703
+ for (const child of children) {
2704
+ if (!isInsignificant(child)) {
2705
+ significant.push(child);
2706
+ }
2707
+ }
2708
+ return significant;
2709
+ }
2710
+
2711
+ function hasSpreadChild(children: readonly JSXChild[]): boolean {
2712
+ for (const child of children) {
2713
+ if (child.type === 'JSXSpreadChild') {
2714
+ return true;
2715
+ }
2716
+ }
2717
+ return false;
2718
+ }
2719
+
2720
+ function hasSiteInside(sites: readonly CallSite[], element: JSXElement): boolean {
2721
+ for (const site of sites) {
2722
+ if (isWithin(site.element, element) && site.element !== element) {
2723
+ return true;
2724
+ }
2725
+ }
2726
+ return false;
2727
+ }
2728
+
2729
+ function findOuterComponent(
2730
+ components: readonly Component[],
2731
+ inner: Component,
2732
+ element: JSXElement,
2733
+ ): Component | undefined {
2734
+ for (const item of components) {
2735
+ if (item !== inner && isWithin(element, item.fn)) {
2736
+ return item;
2737
+ }
2738
+ }
2739
+ return undefined;
2740
+ }
2741
+
2742
+ function collectPrepared(copies: Iterable<PreparedSite | undefined>): PreparedSite[] {
2743
+ const prepared: PreparedSite[] = [];
2744
+ for (const copy of copies) {
2745
+ if (copy !== undefined) {
2746
+ prepared.push(copy);
2747
+ }
2748
+ }
2749
+ return prepared;
2750
+ }
2751
+
2752
+ function hasMissingCopy(copies: Iterable<PreparedSite | undefined>): boolean {
2753
+ for (const copy of copies) {
2754
+ if (copy === undefined) {
2755
+ return true;
2756
+ }
2757
+ }
2758
+ return false;
2759
+ }
2760
+
2761
+ function isWithinAny(element: JSXElement, outers: readonly JSXElement[]): boolean {
2762
+ for (const outer of outers) {
2763
+ if (isWithin(element, outer)) {
2764
+ return true;
2765
+ }
2766
+ }
2767
+ return false;
2768
+ }
2769
+
2770
+ function isInsideOtherCopy(
2771
+ component: Component,
2772
+ elements: readonly JSXElement[],
2773
+ copied: readonly Component[],
2774
+ ): boolean {
2775
+ for (const element of elements) {
2776
+ for (const other of copied) {
2777
+ if (other !== component && element.start >= other.fn.start && element.end <= other.fn.end) {
2778
+ return true;
2779
+ }
2780
+ }
2781
+ }
2782
+ return false;
2783
+ }
2784
+
2785
+ function hasChild(children: readonly Node[], child: Node): boolean {
2786
+ for (const item of children) {
2787
+ if (item === child) {
2788
+ return true;
2789
+ }
2790
+ }
2791
+ return false;
2792
+ }