solid-optimizer 0.3.0 → 1.0.0-next.0

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
@@ -9,13 +9,15 @@
9
9
  *
10
10
  * - It is declared once at the top level of the module, with a capitalized
11
11
  * name, as a function declaration or a `const` bound to a function.
12
- * - It takes no parameter, or one identifier that is only read as `props.name`.
12
+ * - It takes no parameter, or one identifier that is only read as `props.name`,
13
+ * or spread onto an element as `{...props}`.
13
14
  * - Its body is statements followed by one final `return` of JSX, or an arrow
14
15
  * function whose body is JSX.
15
16
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
16
17
  *
17
- * A call site inlines when it passes plain attributes: no spread, no `ref`,
18
- * and no namespaced attribute.
18
+ * A call site inlines when it passes plain attributes: no spread and no
19
+ * namespaced attribute. A `ref` moves to the element the component forwards
20
+ * `props.ref` to. See `refValue`.
19
21
  *
20
22
  * # How props keep their meaning
21
23
  *
@@ -35,28 +37,36 @@
35
37
  *
36
38
  * Statements, and stored props, have to run where the component would have
37
39
  * been created. Solid runs a component untracked, so the statements move into
38
- * the function whose returned JSX holds the call: a component itself, which
39
- * also runs untracked. The call has to be in that returned JSX with only
40
- * intrinsic elements and fragments around it, so it is created exactly once
41
- * each time that function runs.
40
+ * the function whose returned JSX holds the call, when that function also
41
+ * runs untracked:
42
+ *
43
+ * - a component;
44
+ * - the child callback of `<For>` or `<Repeat>`, which runs once per item
45
+ * under its own owner;
46
+ * - the child callback of `<Show>` or `<Match>` that takes the value, which
47
+ * runs once each time the condition turns true;
48
+ * - the callback given to `render`, which runs once under its root.
49
+ *
50
+ * The call has to be in that returned JSX with only intrinsic elements and
51
+ * fragments around it, so it is created exactly once each time that function
52
+ * runs.
42
53
  *
43
54
  * A context provider is a component, so statements do not move out of it,
44
55
  * and `useContext` still runs under it. The exception is a provider whose
45
56
  * subtree only runs visible code. Statements move out through it, and each
46
57
  * `useContext` of its context in a copy becomes its value. See `provider.ts`.
47
58
  *
48
- * The props can also be read through `mergeProps()` and `splitProps()` views
49
- * declared in the component. Each call site knows which props it passes, so
50
- * a read of a view resolves to a prop, a prop with a literal default, a
51
- * default, or `undefined`, and a spread of a view becomes the attributes it
52
- * holds.
59
+ * The props can also be read through `merge()` and `omit()` views declared
60
+ * in the component. Each call site knows which props it passes, so a read
61
+ * of a view resolves to a prop, a literal default, or `undefined`, and a
62
+ * spread of a view becomes the attributes it holds.
53
63
  *
54
64
  * # Accepted differences
55
65
  *
56
66
  * - A prop that is a function and is called as `props.name()` runs with
57
67
  * `this` set to its own receiver instead of the props object.
58
68
  * - Statements moved into the host run before the host's JSX is created,
59
- * instead of while it is created. The order of `onMount` and effect
69
+ * instead of while it is created. The order of `onSettled` and effect
60
70
  * registration across siblings can change.
61
71
  */
62
72
  import MagicString from 'magic-string';
@@ -86,21 +96,22 @@ import {
86
96
  unwrap,
87
97
  } from './ast';
88
98
  import type { PassContext, ResolvedOptions } from './context';
89
- import { isConstantDeclaration, textOf } from './context';
99
+ import { isConstantDeclaration, recordSkip, solidExport, textOf } from './context';
90
100
  import type { Piece } from './jsx';
91
101
  import type { ClosedOptions, Provider } from './provider';
92
102
  import {
103
+ contextBinding,
93
104
  contextReads,
94
105
  findProviders,
95
106
  isClosed,
96
- providerContext,
97
107
  solidCallee,
98
108
  stableValueText,
99
109
  } from './provider';
100
110
  import { keptPiece, replacedPiece, writePieces } from './jsx';
101
111
  import type { Binding, Scope } from './scope';
102
112
  import { isWithinScope, lookup, scopeAt } from './scope';
103
- import { SizeEstimator, copySize } from './size';
113
+ import { copySize } from './copy-size';
114
+ import { SizeEstimator } from './size';
104
115
  import { evaluate, literalText } from './value';
105
116
 
106
117
  type ComponentFunction = FunctionNode | ArrowFunctionExpression;
@@ -116,19 +127,16 @@ interface PropRead extends ReadCount {
116
127
  }
117
128
 
118
129
  /**
119
- * One source of a `mergeProps()` or `splitProps()` view: the props, or an
120
- * object of literal defaults, with the keys a `splitProps()` hides from it
121
- * or keeps only.
130
+ * One source of a `merge()` or `omit()` view: the props, or an object of
131
+ * literal defaults, with the keys an `omit()` hides from it.
122
132
  */
123
133
  interface ViewSource {
124
134
  /** The default of each key, as literal text, or `undefined` for the props. */
125
135
  readonly defaults: ReadonlyMap<string, string> | undefined;
126
136
  readonly hidden: ReadonlySet<string>;
127
- /** The only keys it holds, for a group `splitProps()` picked. */
128
- readonly only: ReadonlySet<string> | undefined;
129
137
  }
130
138
 
131
- /** A props object made with `mergeProps()` and `splitProps()`, in source order. */
139
+ /** A props object made with `merge()` and `omit()`, in source order. */
132
140
  interface View {
133
141
  readonly sources: readonly ViewSource[];
134
142
  }
@@ -137,63 +145,20 @@ interface ViewRead extends PropRead {
137
145
  readonly view: View;
138
146
  }
139
147
 
140
- /**
141
- * What a key of a view reads as at a call site: the prop, the prop with a
142
- * default for when it is `undefined`, or a text.
143
- */
144
- type Resolution =
145
- | { readonly kind: 'props' }
146
- | { readonly kind: 'fallback'; readonly text: string }
147
- | { readonly kind: 'text'; readonly text: string };
148
-
149
148
  interface ResolvedSpread {
150
149
  readonly spread: ViewSpread;
151
- readonly keys: ReadonlyMap<string, Resolution>;
150
+ /** Each key and its default text, or `undefined` when the props hold it. */
151
+ readonly keys: ReadonlyMap<string, string | undefined>;
152
152
  }
153
153
 
154
154
  interface ResolvedViews {
155
155
  /** Reads of the props, including view reads the props answer. */
156
156
  readonly propReads: readonly PropRead[];
157
- /** View reads the props answer, unless the prop is `undefined`. */
158
- readonly fallbacks: ReadonlyMap<PropRead, string>;
159
157
  /** View reads a default or nothing answers, and their text. */
160
158
  readonly texts: ReadonlyMap<Node, string>;
161
159
  readonly spreads: readonly ResolvedSpread[];
162
160
  }
163
161
 
164
- /**
165
- * A prop read that falls back to a default when the prop is `undefined`,
166
- * the way `mergeProps()` reads it.
167
- */
168
- function withFallback(text: string, fallback: string): string {
169
- return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
170
- }
171
-
172
- function allows(source: ViewSource, key: string): boolean {
173
- return !source.hidden.has(key) && (source.only === undefined || source.only.has(key));
174
- }
175
-
176
- /**
177
- * Whether an expression passed as a prop is never `undefined`.
178
- */
179
- function isDefinedExpression(node: Expression): boolean {
180
- const inner = unwrap(node);
181
- switch (inner.type) {
182
- case 'ObjectExpression':
183
- case 'ArrayExpression':
184
- case 'ArrowFunctionExpression':
185
- case 'FunctionExpression':
186
- case 'ClassExpression':
187
- case 'NewExpression':
188
- case 'TemplateLiteral':
189
- case 'JSXElement':
190
- case 'JSXFragment':
191
- return true;
192
- default:
193
- return evaluate(inner, () => undefined)?.value !== undefined;
194
- }
195
- }
196
-
197
162
  interface ViewSpread extends ReadCount {
198
163
  readonly attribute: JSXSpreadAttribute;
199
164
  readonly element: JSXElement;
@@ -215,7 +180,7 @@ interface Component {
215
180
  readonly props: readonly PropRead[];
216
181
  /** The props parameter, when there is one. */
217
182
  readonly param: Binding | undefined;
218
- /** Reads of a `mergeProps()` or `splitProps()` view, which a call site resolves. */
183
+ /** Reads of a `merge()` or `omit()` view, which a call site resolves. */
219
184
  readonly viewReads: readonly ViewRead[];
220
185
  /** Spreads of a view onto an element, which become the attributes it holds. */
221
186
  readonly viewSpreads: readonly ViewSpread[];
@@ -403,7 +368,7 @@ function isDynamic(node: Node): boolean {
403
368
  * Whether any expression short of a sequence can replace `node` without
404
369
  * parentheses, because its slot is a whole expression of its own.
405
370
  */
406
- export function acceptsAnyExpression(context: PassContext, node: Node): boolean {
371
+ function acceptsAnyExpression(context: PassContext, node: Node): boolean {
407
372
  const parent = context.parents.get(node);
408
373
  if (!parent) {
409
374
  return false;
@@ -458,12 +423,12 @@ function functionOf(binding: Binding, options: ResolvedOptions): ComponentFuncti
458
423
  function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefined {
459
424
  const attributes = new Map<string, JSXAttribute>();
460
425
  for (const attribute of element.openingElement.attributes) {
461
- // A spread can supply any prop, and a namespaced attribute or `ref` is
462
- // something the JSX transform handles on its own.
426
+ // A spread can supply any prop, and a namespaced attribute is something
427
+ // the JSX transform handles on its own.
463
428
  if (
464
429
  attribute.type !== 'JSXAttribute' ||
465
430
  attribute.name.type !== 'JSXIdentifier' ||
466
- attribute.name.name === 'ref' ||
431
+ (attribute.name.name === 'ref' && !isForwardableRef(attribute)) ||
467
432
  attributes.has(attribute.name.name)
468
433
  ) {
469
434
  return undefined;
@@ -473,6 +438,113 @@ function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefine
473
438
  return attributes;
474
439
  }
475
440
 
441
+ /**
442
+ * Whether a top-level statement declares only constants made of literals,
443
+ * like `const ROWS = [{ name: 'a' }]`. An export keeps its `export`, since
444
+ * the fold pass treats an exported list differently.
445
+ */
446
+ function isDataDeclaration(statement: Node): boolean {
447
+ const declaration =
448
+ statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
449
+ return (
450
+ declaration?.type === 'VariableDeclaration' &&
451
+ !declaration.declare &&
452
+ (declaration.kind === 'const' || declaration.kind === 'var') &&
453
+ declaration.declarations.every(
454
+ (declarator) =>
455
+ declarator.id.type === 'Identifier' &&
456
+ declarator.init !== null &&
457
+ isDataLiteral(declarator.init),
458
+ )
459
+ );
460
+ }
461
+
462
+ /** Whether an expression is a literal, or an array or object literal of them. */
463
+ function isDataLiteral(node: Expression): boolean {
464
+ switch (node.type) {
465
+ case 'Literal':
466
+ return !('regex' in node);
467
+ case 'TemplateLiteral':
468
+ return node.expressions.length === 0;
469
+ case 'UnaryExpression':
470
+ return node.operator === '-' && node.argument.type === 'Literal';
471
+ case 'ParenthesizedExpression':
472
+ case 'TSAsExpression':
473
+ case 'TSSatisfiesExpression':
474
+ return isDataLiteral(node.expression);
475
+ case 'ArrayExpression':
476
+ return node.elements.every(
477
+ (element) => element !== null && element.type !== 'SpreadElement' && isDataLiteral(element),
478
+ );
479
+ case 'ObjectExpression':
480
+ return node.properties.every(
481
+ (property) =>
482
+ property.type === 'Property' &&
483
+ property.kind === 'init' &&
484
+ !property.method &&
485
+ !property.computed &&
486
+ isDataLiteral(property.value),
487
+ );
488
+ default:
489
+ return false;
490
+ }
491
+ }
492
+
493
+ function isMemberRead(read: ReadCount): read is PropRead {
494
+ return 'member' in read;
495
+ }
496
+
497
+ /**
498
+ * Whether a `ref` at a call site can move to the element the component
499
+ * forwards it to: a name, a function, or an array of them. A name is
500
+ * assigned the element, so it has to stay the name itself. Anything else
501
+ * would be evaluated when the element is made, outside the `untrack` the
502
+ * component's `ref` runs it in.
503
+ */
504
+ function isForwardableRef(attribute: JSXAttribute): boolean {
505
+ const { value } = attribute;
506
+ if (value?.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression') {
507
+ return false;
508
+ }
509
+ const isTarget = (node: Expression): boolean => {
510
+ const inner = unwrap(node);
511
+ if (inner.type === 'ArrayExpression') {
512
+ return inner.elements.every(
513
+ (element) => element !== null && element.type !== 'SpreadElement' && isTarget(element),
514
+ );
515
+ }
516
+ return (
517
+ inner.type === 'Identifier' ||
518
+ inner.type === 'ArrowFunctionExpression' ||
519
+ inner.type === 'FunctionExpression'
520
+ );
521
+ };
522
+ return isTarget(value.expression);
523
+ }
524
+
525
+ /**
526
+ * Why `attributesOf` turns down a call site, for the report.
527
+ */
528
+ function callSiteReason(element: JSXElement): string {
529
+ const names = new Set<string>();
530
+ for (const attribute of element.openingElement.attributes) {
531
+ if (attribute.type !== 'JSXAttribute') {
532
+ return 'call-spread';
533
+ }
534
+ if (attribute.name.type !== 'JSXIdentifier') {
535
+ return 'call-namespace';
536
+ }
537
+ if (attribute.name.name === 'ref') {
538
+ return 'call-ref';
539
+ }
540
+ if (names.has(attribute.name.name)) {
541
+ break;
542
+ }
543
+ names.add(attribute.name.name);
544
+ }
545
+ return 'call-duplicate';
546
+ }
547
+
476
548
  function isAssignmentTarget(context: PassContext, node: Node): boolean {
477
549
  const parent = context.parents.get(node);
478
550
  if (!parent) {
@@ -517,6 +589,44 @@ class Inliner {
517
589
 
518
590
  constructor(private readonly context: PassContext) {}
519
591
 
592
+ /** Why the last component or call site was turned down, for the report. */
593
+ private reason = '';
594
+
595
+ /**
596
+ * Whether a binding holds a component made by Solid's `lazy`, for the
597
+ * report. In a chunk, `lazy` can be a local declaration under another
598
+ * name, so a call given a function with a dynamic import counts too.
599
+ */
600
+ private isLazy(binding: Binding): boolean {
601
+ const { declaration } = binding;
602
+ const init =
603
+ declaration.type === 'VariableDeclarator' && declaration.init
604
+ ? unwrap(declaration.init)
605
+ : undefined;
606
+ if (init?.type !== 'CallExpression' || init.callee.type !== 'Identifier') {
607
+ return false;
608
+ }
609
+ const callee = this.context.scopes.references.get(init.callee);
610
+ if (callee && solidExport(callee, this.context.options) === 'lazy') {
611
+ return true;
612
+ }
613
+ const loader = init.arguments.at(0);
614
+ if (!loader || !isFunctionNode(loader)) {
615
+ return false;
616
+ }
617
+ let imports = false;
618
+ const visit = (node: Node): void => {
619
+ if (node.type === 'ImportExpression') {
620
+ imports = true;
621
+ }
622
+ if (!imports) {
623
+ forEachChild(node, visit);
624
+ }
625
+ };
626
+ visit(loader);
627
+ return imports;
628
+ }
629
+
520
630
  // ---------------------------------------------------------------------------
521
631
  // Components
522
632
  // ---------------------------------------------------------------------------
@@ -546,15 +656,29 @@ class Inliner {
546
656
  }
547
657
 
548
658
  private analyzeComponent(binding: Binding): Component | undefined {
549
- if (!isCapitalized(binding.name) || binding.mutated) {
659
+ if (!isCapitalized(binding.name)) {
660
+ return undefined;
661
+ }
662
+ if (binding.mutated) {
663
+ this.reason = 'component-mutated';
550
664
  return undefined;
551
665
  }
552
666
  const fn = functionOf(binding, this.context.options);
553
- if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) {
667
+ if (!fn?.body) {
668
+ this.reason = this.isLazy(binding) ? 'lazy' : 'not-a-function';
669
+ return undefined;
670
+ }
671
+ if (fn.async || fn.generator) {
672
+ this.reason = 'component-async';
673
+ return undefined;
674
+ }
675
+ if (fn.params.length > 1) {
676
+ this.reason = 'component-params';
554
677
  return undefined;
555
678
  }
556
679
  const scope = this.scopeOf(fn);
557
680
  if (!scope) {
681
+ this.reason = 'not-a-function';
558
682
  return undefined;
559
683
  }
560
684
 
@@ -565,6 +689,7 @@ class Inliner {
565
689
  const body = fn.body.body;
566
690
  const last = body.at(-1);
567
691
  if (last?.type !== 'ReturnStatement' || !last.argument) {
692
+ this.reason = 'component-body';
568
693
  return undefined;
569
694
  }
570
695
  const rest: Statement[] = [];
@@ -573,7 +698,12 @@ class Inliner {
573
698
  // TypeScript's AST gives every expression statement a `directive`
574
699
  // key, which is `null` unless the statement is a directive.
575
700
  const directive = 'directive' in statement ? statement.directive : null;
576
- if (typeof directive === 'string' || containsReturn(statement)) {
701
+ if (typeof directive === 'string') {
702
+ this.reason = 'component-directive';
703
+ return undefined;
704
+ }
705
+ if (containsReturn(statement)) {
706
+ this.reason = 'component-return';
577
707
  return undefined;
578
708
  }
579
709
  rest.push(statement);
@@ -584,9 +714,11 @@ class Inliner {
584
714
  root = unwrap(fn.body);
585
715
  }
586
716
  if (root.type !== 'JSXElement' && root.type !== 'JSXFragment') {
717
+ this.reason = 'component-body';
587
718
  return undefined;
588
719
  }
589
720
  if (usesFunctionContext(this.context, fn.body)) {
721
+ this.reason = 'component-this';
590
722
  return undefined;
591
723
  }
592
724
 
@@ -595,15 +727,18 @@ class Inliner {
595
727
  let paramBinding: Binding | undefined;
596
728
  if (param) {
597
729
  if (param.type !== 'Identifier') {
730
+ this.reason = 'component-params';
598
731
  return undefined;
599
732
  }
600
733
  paramBinding = scope.bindings.get(param.name);
601
734
  if (!paramBinding || paramBinding.mutated) {
735
+ this.reason = 'props-use';
602
736
  return undefined;
603
737
  }
604
738
  }
605
739
  const views = this.analyzeViews(fn, scope, statements, paramBinding);
606
740
  if (!views) {
741
+ this.reason = 'view-use';
607
742
  return undefined;
608
743
  }
609
744
  const props: PropRead[] = [];
@@ -613,6 +748,7 @@ class Inliner {
613
748
  }
614
749
  const read = this.propRead(fn, reference);
615
750
  if (!read) {
751
+ this.reason = 'props-use';
616
752
  return undefined;
617
753
  }
618
754
  props.push(read);
@@ -641,6 +777,7 @@ class Inliner {
641
777
  }
642
778
  // A component that renders itself cannot be inlined into itself.
643
779
  if (target === binding) {
780
+ this.reason = 'component-recursive';
644
781
  return undefined;
645
782
  }
646
783
  if (target && isWithinScope(target.scope, scope)) {
@@ -712,9 +849,9 @@ class Inliner {
712
849
  }
713
850
 
714
851
  /**
715
- * Finds the `mergeProps()` and `splitProps()` views of the props declared
716
- * in a component's statements, and how each is used. A view can only be
717
- * read as `view.name`, spread onto an element, or passed to another view.
852
+ * Finds the `merge()` and `omit()` views of the props declared in a
853
+ * component's statements, and how each is used. A view can only be read
854
+ * as `view.name`, spread onto an element, or passed to another view.
718
855
  * `undefined` when a view is used any other way.
719
856
  */
720
857
  private analyzeViews(
@@ -741,63 +878,56 @@ class Inliner {
741
878
  statement.type !== 'VariableDeclaration' ||
742
879
  statement.kind !== 'const' ||
743
880
  statement.declarations.length !== 1 ||
744
- !declarator?.init
881
+ declarator?.id.type !== 'Identifier' ||
882
+ !declarator.init
745
883
  ) {
746
884
  continue;
747
885
  }
748
886
  const init = unwrap(declarator.init);
749
- if (init.type !== 'CallExpression') {
887
+ const callee = init.type === 'CallExpression' ? solidCallee(this.context, init) : undefined;
888
+ if (init.type !== 'CallExpression' || (callee !== 'merge' && callee !== 'omit')) {
750
889
  continue;
751
890
  }
752
- const callee = solidCallee(this.context, init);
753
- let declared: [Node | null, View][] | null | undefined;
754
- if (callee === 'mergeProps' && declarator.id.type === 'Identifier') {
755
- const view = this.viewOf(init, param, views, args);
756
- declared = view ? [[declarator.id, view]] : view;
757
- } else if (callee === 'splitProps' && declarator.id.type === 'ArrayPattern') {
758
- declared = this.splitViews(init, declarator.id.elements, param, views, args);
759
- } else {
891
+ const view = this.viewOf(callee, init, param, views, args);
892
+ if (view === null) {
760
893
  continue;
761
894
  }
762
- if (declared === null) {
763
- continue;
764
- }
765
- if (!declared) {
895
+ const binding = scope.bindings.get(declarator.id.name);
896
+ if (!view || !binding || binding.mutated) {
766
897
  return undefined;
767
898
  }
768
- for (const [identifier, view] of declared) {
769
- if (identifier?.type !== 'Identifier') {
770
- continue;
771
- }
772
- const binding = scope.bindings.get(identifier.name);
773
- if (!binding || binding.mutated) {
774
- return undefined;
775
- }
776
- views.set(binding, view);
777
- }
899
+ views.set(binding, view);
778
900
  declarations.push(statement);
779
901
  }
780
902
 
781
903
  const reads: ViewRead[] = [];
782
904
  const spreads: ViewSpread[] = [];
905
+ // `{...props}` spreads every prop, like a view of the props with nothing hidden.
906
+ const whole: View = { sources: [{ defaults: undefined, hidden: new Set() }] };
907
+ for (const reference of param?.references ?? []) {
908
+ const spread = this.spreadOnElement(reference);
909
+ if (spread) {
910
+ args.add(reference);
911
+ spreads.push({
912
+ attribute: spread.attribute,
913
+ element: spread.element,
914
+ view: whole,
915
+ repeated: this.isRepeated(fn, spread.attribute),
916
+ });
917
+ }
918
+ }
783
919
  for (const [binding, view] of views) {
784
920
  for (const reference of binding.references) {
785
921
  if (args.has(reference)) {
786
922
  continue;
787
923
  }
788
- const parent = this.context.parents.get(reference);
789
- const opening = parent ? this.context.parents.get(parent) : undefined;
790
- const element = opening ? this.context.parents.get(opening) : undefined;
791
- if (
792
- parent?.type === 'JSXSpreadAttribute' &&
793
- opening?.type === 'JSXOpeningElement' &&
794
- element?.type === 'JSXElement'
795
- ) {
924
+ const spread = this.spreadOnElement(reference);
925
+ if (spread) {
796
926
  spreads.push({
797
- attribute: parent,
798
- element,
927
+ attribute: spread.attribute,
928
+ element: spread.element,
799
929
  view,
800
- repeated: this.isRepeated(fn, parent),
930
+ repeated: this.isRepeated(fn, spread.attribute),
801
931
  });
802
932
  continue;
803
933
  }
@@ -811,58 +941,90 @@ class Inliner {
811
941
  return { bindings: new Set(views.keys()), arguments: args, declarations, reads, spreads };
812
942
  }
813
943
 
814
- /**
815
- * The sources a view argument stands for: the props, a view, or a nested
816
- * `mergeProps()` call.
817
- */
818
- private viewSources(
819
- argument: Node,
820
- param: Binding | undefined,
821
- views: ReadonlyMap<Binding, View>,
822
- args: Set<Node>,
823
- ): readonly ViewSource[] | undefined {
824
- if (argument.type === 'CallExpression') {
825
- if (solidCallee(this.context, argument) !== 'mergeProps') {
826
- return undefined;
827
- }
828
- return this.viewOf(argument, param, views, args)?.sources;
829
- }
830
- if (argument.type !== 'Identifier') {
831
- return undefined;
832
- }
833
- const binding = this.context.scopes.references.get(argument);
834
- if (binding && binding === param) {
835
- return [{ defaults: undefined, hidden: new Set(), only: undefined }];
836
- }
837
- return binding ? views.get(binding)?.sources : undefined;
944
+ /** The spread a reference is the whole argument of, and the element it is on. */
945
+ private spreadOnElement(
946
+ reference: Node,
947
+ ): { attribute: JSXSpreadAttribute; element: JSXElement } | undefined {
948
+ const parent = this.context.parents.get(reference);
949
+ const opening = parent ? this.context.parents.get(parent) : undefined;
950
+ const element = opening ? this.context.parents.get(opening) : undefined;
951
+ return parent?.type === 'JSXSpreadAttribute' &&
952
+ opening?.type === 'JSXOpeningElement' &&
953
+ element?.type === 'JSXElement'
954
+ ? { attribute: parent, element }
955
+ : undefined;
838
956
  }
839
957
 
840
958
  /**
841
- * The view a `mergeProps()` call makes. `null` when it does not take the
842
- * props or another view, and `undefined` when it does but its other
843
- * arguments are not literal objects.
959
+ * The view a `merge()` or `omit()` call makes. `null` when it does not
960
+ * take the props or another view, and `undefined` when it does but its
961
+ * other arguments are not literal.
844
962
  */
845
963
  private viewOf(
964
+ callee: 'merge' | 'omit',
846
965
  call: CallExpression,
847
966
  param: Binding | undefined,
848
967
  views: ReadonlyMap<Binding, View>,
849
968
  args: Set<Node>,
850
969
  ): View | null | undefined {
970
+ const sourcesOf = (argument: Node): readonly ViewSource[] | undefined => {
971
+ if (argument.type === 'CallExpression') {
972
+ const nested = solidCallee(this.context, argument);
973
+ if (nested !== 'merge' && nested !== 'omit') {
974
+ return undefined;
975
+ }
976
+ return this.viewOf(nested, argument, param, views, args)?.sources;
977
+ }
978
+ if (argument.type !== 'Identifier') {
979
+ return undefined;
980
+ }
981
+ const binding = this.context.scopes.references.get(argument);
982
+ if (binding && binding === param) {
983
+ return [{ defaults: undefined, hidden: new Set() }];
984
+ }
985
+ return binding ? views.get(binding)?.sources : undefined;
986
+ };
851
987
  const involved = call.arguments.some(
852
- (argument) =>
853
- argument.type !== 'SpreadElement' &&
854
- this.viewSources(unwrap(argument), param, views, new Set()) !== undefined,
988
+ (argument) => argument.type !== 'SpreadElement' && sourcesOf(unwrap(argument)) !== undefined,
855
989
  );
856
990
  if (!involved) {
857
991
  return null;
858
992
  }
993
+
994
+ if (callee === 'omit') {
995
+ const first = call.arguments.at(0);
996
+ const keys = call.arguments.slice(1);
997
+ if (first === undefined || first.type === 'SpreadElement') {
998
+ return undefined;
999
+ }
1000
+ const target = unwrap(first);
1001
+ const base = sourcesOf(target);
1002
+ const hidden: string[] = [];
1003
+ for (const key of keys) {
1004
+ if (key.type !== 'Literal' || typeof key.value !== 'string') {
1005
+ return undefined;
1006
+ }
1007
+ hidden.push(key.value);
1008
+ }
1009
+ if (!base) {
1010
+ return undefined;
1011
+ }
1012
+ args.add(target);
1013
+ return {
1014
+ sources: base.map((source) => ({
1015
+ defaults: source.defaults,
1016
+ hidden: new Set([...source.hidden, ...hidden]),
1017
+ })),
1018
+ };
1019
+ }
1020
+
859
1021
  const sources: ViewSource[] = [];
860
1022
  for (const argument of call.arguments) {
861
1023
  if (argument.type === 'SpreadElement') {
862
1024
  return undefined;
863
1025
  }
864
1026
  const inner = unwrap(argument);
865
- const existing = this.viewSources(inner, param, views, args);
1027
+ const existing = sourcesOf(inner);
866
1028
  if (existing) {
867
1029
  args.add(inner);
868
1030
  sources.push(...existing);
@@ -891,74 +1053,14 @@ class Inliner {
891
1053
  }
892
1054
  defaults.set(key, text);
893
1055
  }
894
- sources.push({ defaults, hidden: new Set(), only: undefined });
1056
+ sources.push({ defaults, hidden: new Set() });
895
1057
  }
896
1058
  return { sources };
897
1059
  }
898
1060
 
899
- /**
900
- * The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
901
- * each with its identifier. A key belongs to the first group that names
902
- * it, and the last view holds the keys no group names. `null` when the
903
- * call does not take the props or a view.
904
- */
905
- private splitViews(
906
- call: CallExpression,
907
- elements: readonly (Node | null)[],
908
- param: Binding | undefined,
909
- views: ReadonlyMap<Binding, View>,
910
- args: Set<Node>,
911
- ): [Node | null, View][] | null | undefined {
912
- const first = call.arguments.at(0);
913
- if (first === undefined || first.type === 'SpreadElement') {
914
- return null;
915
- }
916
- const target = unwrap(first);
917
- const base = this.viewSources(target, param, views, args);
918
- if (!base) {
919
- return null;
920
- }
921
- args.add(target);
922
- const groups: string[][] = [];
923
- for (const group of call.arguments.slice(1)) {
924
- if (group.type !== 'ArrayExpression') {
925
- return undefined;
926
- }
927
- const keys: string[] = [];
928
- for (const key of group.elements) {
929
- if (key?.type !== 'Literal' || typeof key.value !== 'string') {
930
- return undefined;
931
- }
932
- keys.push(key.value);
933
- }
934
- groups.push(keys);
935
- }
936
- if (
937
- elements.length > groups.length + 1 ||
938
- elements.some((element) => element?.type === 'RestElement')
939
- ) {
940
- return undefined;
941
- }
942
- const claimed = new Set<string>();
943
- const filters = groups.map((keys) => {
944
- const owned = keys.filter((key) => !claimed.has(key));
945
- for (const key of owned) {
946
- claimed.add(key);
947
- }
948
- return new Set(owned);
949
- });
950
- return elements.map((element, index): [Node | null, View] => {
951
- const only = filters.at(index);
952
- const sources = base.map((source) => ({
953
- defaults: source.defaults,
954
- hidden: only ? source.hidden : new Set([...source.hidden, ...claimed]),
955
- only: only
956
- ? new Set([...only].filter((key) => source.only === undefined || source.only.has(key)))
957
- : source.only,
958
- }));
959
- return [element, { sources }];
960
- });
961
- }
1061
+ // ---------------------------------------------------------------------------
1062
+ // Call sites
1063
+ // ---------------------------------------------------------------------------
962
1064
 
963
1065
  /**
964
1066
  * Whether a function is a component, which Solid runs untracked. Only a
@@ -985,6 +1087,68 @@ class Inliner {
985
1087
  );
986
1088
  }
987
1089
 
1090
+ /**
1091
+ * Whether a function runs untracked, once each time the JSX it returns is
1092
+ * created, so it can take a component's statements. See "Where a component
1093
+ * body can go" above.
1094
+ */
1095
+ private isHostFunction(fn: Node): boolean {
1096
+ if (this.isComponentFunction(fn)) {
1097
+ return true;
1098
+ }
1099
+ if (
1100
+ (fn.type !== 'FunctionExpression' && fn.type !== 'ArrowFunctionExpression') ||
1101
+ fn.async ||
1102
+ fn.generator
1103
+ ) {
1104
+ return false;
1105
+ }
1106
+ let node: Node = fn;
1107
+ let parent = this.context.parents.get(node);
1108
+ while (parent?.type === 'ParenthesizedExpression') {
1109
+ node = parent;
1110
+ parent = this.context.parents.get(node);
1111
+ }
1112
+ // `render(() => <App />, element)` calls it once under a new root.
1113
+ if (parent?.type === 'CallExpression') {
1114
+ return parent.arguments.at(0) === node && this.solidCallee(parent.callee) === 'render';
1115
+ }
1116
+ // A built-in's only child.
1117
+ const element =
1118
+ parent?.type === 'JSXExpressionContainer' ? this.context.parents.get(parent) : undefined;
1119
+ if (element?.type !== 'JSXElement') {
1120
+ return false;
1121
+ }
1122
+ const significant = element.children.filter((child) => !isInsignificant(child));
1123
+ if (significant.length !== 1 || significant.at(0) !== parent) {
1124
+ return false;
1125
+ }
1126
+ switch (this.solidCallee(element.openingElement.name)) {
1127
+ case 'For':
1128
+ case 'Repeat':
1129
+ return true;
1130
+ // These only call a callback that takes the value. One without a parameter is a child to insert.
1131
+ case 'Show':
1132
+ case 'Match':
1133
+ return fn.params.length > 0;
1134
+ default:
1135
+ return false;
1136
+ }
1137
+ }
1138
+
1139
+ /** The Solid export a callee or tag refers to. */
1140
+ private solidCallee(name: Node): string | undefined {
1141
+ if (name.type !== 'Identifier' && name.type !== 'JSXIdentifier') {
1142
+ return undefined;
1143
+ }
1144
+ const { references } = this.context.scopes;
1145
+ if (!references.has(name)) {
1146
+ return undefined;
1147
+ }
1148
+ const binding = references.get(name);
1149
+ return binding ? solidExport(binding, this.context.options) : undefined;
1150
+ }
1151
+
988
1152
  /**
989
1153
  * Where the statements of a component created at `element` can go: the
990
1154
  * returned JSX of a component, with only intrinsic elements, fragments,
@@ -1000,9 +1164,9 @@ class Inliner {
1000
1164
  if (parent.type === 'JSXElement') {
1001
1165
  const { name } = parent.openingElement;
1002
1166
  const passable =
1003
- this.transparent.has(parent) ||
1004
1167
  this.assumedHosts?.has(parent) === true ||
1005
- (name.type === 'JSXIdentifier' && isIntrinsicTag(name.name));
1168
+ (name.type === 'JSXIdentifier' &&
1169
+ (isIntrinsicTag(name.name) || this.transparent.has(parent)));
1006
1170
  if (!passable) {
1007
1171
  return undefined;
1008
1172
  }
@@ -1019,12 +1183,12 @@ class Inliner {
1019
1183
  while (fn && !isFunctionNode(fn)) {
1020
1184
  fn = this.context.parents.get(fn);
1021
1185
  }
1022
- if (!fn || !this.isComponentFunction(fn)) {
1186
+ if (!fn || !this.isHostFunction(fn)) {
1023
1187
  return undefined;
1024
1188
  }
1025
1189
  return { fn, host: { kind: 'return', statement: parent } };
1026
1190
  } else if (parent.type === 'ArrowFunctionExpression') {
1027
- if (parent.body !== node || !this.isComponentFunction(parent)) {
1191
+ if (parent.body !== node || !this.isHostFunction(parent)) {
1028
1192
  return undefined;
1029
1193
  }
1030
1194
  return { fn: parent, host: { kind: 'arrow', body: parent.body } };
@@ -1072,6 +1236,43 @@ class Inliner {
1072
1236
  return { kind: 'children', children, text, primary };
1073
1237
  }
1074
1238
 
1239
+ /**
1240
+ * How a `ref` from the call site reads in a copy. Solid passes a `ref` to
1241
+ * a component as a function that sets the call site's ref to the element
1242
+ * it is given. The component applies it with its own `ref`, which runs
1243
+ * while the element is made, so the call site's `ref` can move to that
1244
+ * element. Every read has to be the whole `ref` of an element or of
1245
+ * another component, or a key of a spread, which becomes one.
1246
+ */
1247
+ private refValue(
1248
+ attribute: JSXAttribute | undefined,
1249
+ reads: readonly ReadCount[],
1250
+ ): PropValue | undefined {
1251
+ const value = attribute?.value;
1252
+ if (
1253
+ value?.type !== 'JSXExpressionContainer' ||
1254
+ value.expression.type === 'JSXEmptyExpression'
1255
+ ) {
1256
+ return undefined;
1257
+ }
1258
+ for (const read of reads) {
1259
+ if (!isMemberRead(read)) {
1260
+ continue;
1261
+ }
1262
+ const container = this.context.parents.get(read.member);
1263
+ const owner = container ? this.context.parents.get(container) : undefined;
1264
+ if (
1265
+ container?.type !== 'JSXExpressionContainer' ||
1266
+ owner?.type !== 'JSXAttribute' ||
1267
+ owner.name.type !== 'JSXIdentifier' ||
1268
+ owner.name.name !== 'ref'
1269
+ ) {
1270
+ return undefined;
1271
+ }
1272
+ }
1273
+ return { kind: 'expression', text: textOf(this.context, value.expression), primary: true };
1274
+ }
1275
+
1075
1276
  /**
1076
1277
  * How a prop reads in a copy of the component, and the `const` that stores
1077
1278
  * it first when the value has to be evaluated once.
@@ -1176,16 +1377,18 @@ class Inliner {
1176
1377
  */
1177
1378
  private assumedHosts: ReadonlySet<JSXElement> | undefined;
1178
1379
 
1179
- private freshName(base: string): string {
1380
+ private freshName(name: string): string {
1180
1381
  const names = this.scratchNames ?? this.context.scopes.names;
1382
+ // A name a copy gave out before, like `theme$1`, is renamed from `theme` again.
1383
+ const base = name.replace(/\$\d+$/, '');
1181
1384
  let index = 1;
1182
- let name = `${base}$${String(index)}`;
1183
- while (names.has(name)) {
1385
+ let fresh = `${base}$${String(index)}`;
1386
+ while (names.has(fresh)) {
1184
1387
  index += 1;
1185
- name = `${base}$${String(index)}`;
1388
+ fresh = `${base}$${String(index)}`;
1186
1389
  }
1187
- names.add(name);
1188
- return name;
1390
+ names.add(fresh);
1391
+ return fresh;
1189
1392
  }
1190
1393
 
1191
1394
  /**
@@ -1195,26 +1398,31 @@ class Inliner {
1195
1398
  private prepare(component: Component, element: JSXElement): PreparedSite | undefined {
1196
1399
  const attributes = attributesOf(element);
1197
1400
  if (!attributes) {
1401
+ this.reason = callSiteReason(element);
1198
1402
  return undefined;
1199
1403
  }
1200
1404
  const hasChildren = element.children.some((child) => !isInsignificant(child));
1201
1405
  if (hasChildren && attributes.has('children')) {
1406
+ this.reason = 'call-children';
1202
1407
  return undefined;
1203
1408
  }
1204
1409
  for (const attribute of attributes.values()) {
1205
1410
  if (attribute.value && usesFunctionContext(this.context, attribute.value)) {
1411
+ this.reason = 'call-this';
1206
1412
  return undefined;
1207
1413
  }
1208
1414
  }
1209
1415
  for (const child of element.children) {
1210
1416
  if (usesFunctionContext(this.context, child)) {
1417
+ this.reason = 'call-this';
1211
1418
  return undefined;
1212
1419
  }
1213
1420
  }
1214
1421
 
1215
1422
  // What each view read and spread resolves to at this call site.
1216
- const views = this.resolveViews(component, element, attributes, hasChildren);
1423
+ const views = this.resolveViews(component, attributes, hasChildren);
1217
1424
  if (!views) {
1425
+ this.reason = 'view-conflict';
1218
1426
  return undefined;
1219
1427
  }
1220
1428
 
@@ -1230,12 +1438,9 @@ class Inliner {
1230
1438
  for (const read of views.propReads) {
1231
1439
  count(read.name, read);
1232
1440
  }
1233
- for (const read of views.fallbacks.keys()) {
1234
- count(read.name, read);
1235
- }
1236
1441
  for (const spread of views.spreads) {
1237
- for (const [key, resolution] of spread.keys) {
1238
- if (resolution.kind !== 'text') {
1442
+ for (const [key, source] of spread.keys) {
1443
+ if (source === undefined) {
1239
1444
  count(key, spread.spread);
1240
1445
  }
1241
1446
  }
@@ -1245,10 +1450,14 @@ class Inliner {
1245
1450
  let value: PropValue | undefined;
1246
1451
  if (name === 'children' && !attributes.has('children')) {
1247
1452
  value = this.childrenValue(element, reads, fresh, snapshots);
1453
+ } else if (name === 'ref' && attributes.has('ref')) {
1454
+ value = this.refValue(attributes.get('ref'), reads);
1248
1455
  } else {
1249
1456
  value = this.propValue(attributes.get(name), reads, fresh, snapshots);
1250
1457
  }
1251
1458
  if (!value) {
1459
+ // A `ref` is turned down when the component uses it other than as a `ref`.
1460
+ this.reason = name === 'ref' && attributes.has('ref') ? 'ref-use' : 'prop-value';
1252
1461
  return undefined;
1253
1462
  }
1254
1463
  values.set(name, value);
@@ -1258,6 +1467,7 @@ class Inliner {
1258
1467
  const scope = scopeAt(this.context.scopes, this.context.parents, element);
1259
1468
  for (const reference of component.outer) {
1260
1469
  if (lookup(scope, reference.name) !== reference.binding) {
1470
+ this.reason = 'shadowed';
1261
1471
  return undefined;
1262
1472
  }
1263
1473
  }
@@ -1265,11 +1475,13 @@ class Inliner {
1265
1475
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
1266
1476
  const host = hoisted ? this.hostOf(element) : undefined;
1267
1477
  if (hoisted && !host) {
1478
+ this.reason = 'no-host';
1268
1479
  return undefined;
1269
1480
  }
1270
1481
 
1271
1482
  const contextTexts = this.contextTexts(component, element);
1272
1483
  if (!contextTexts) {
1484
+ this.reason = 'context-value';
1273
1485
  return undefined;
1274
1486
  }
1275
1487
 
@@ -1341,7 +1553,7 @@ class Inliner {
1341
1553
  ) {
1342
1554
  const target =
1343
1555
  node.type === 'JSXElement'
1344
- ? providerContext(this.context, node.openingElement.name)
1556
+ ? contextBinding(this.context, node.openingElement.name)
1345
1557
  : undefined;
1346
1558
  if (node.type !== 'JSXElement' || !target || seen.has(target)) {
1347
1559
  continue;
@@ -1436,73 +1648,45 @@ class Inliner {
1436
1648
  }
1437
1649
 
1438
1650
  /**
1439
- * Resolves each view read and spread of a component at a call site.
1440
- * `mergeProps()` reads the last source whose value is not `undefined`, so
1441
- * a prop that can be `undefined` falls back to the default behind it. A
1442
- * spread becomes the keys it holds, and its `children` become the
1443
- * element's children. `undefined` when a spread repeats an attribute, or
1444
- * holds `children` for an element that has some.
1651
+ * Resolves each view read and spread of a component at a call site. A
1652
+ * read becomes a read of the props, or the text of a default. A spread
1653
+ * becomes the keys it holds, in the order `merge()` gives them, and its
1654
+ * `children` become the element's children. `undefined` when a spread
1655
+ * repeats an attribute, or holds `children` for an element that has some.
1445
1656
  */
1446
1657
  private resolveViews(
1447
1658
  component: Component,
1448
- element: JSXElement,
1449
1659
  attributes: ReadonlyMap<string, JSXAttribute>,
1450
1660
  hasChildren: boolean,
1451
1661
  ): ResolvedViews | undefined {
1452
1662
  const has = (key: string): boolean =>
1453
1663
  attributes.has(key) || (key === 'children' && hasChildren);
1454
- // Whether the prop the call site passes for `key` is never `undefined`.
1455
- const defined = (key: string): boolean => {
1456
- const attribute = attributes.get(key);
1457
- if (attribute) {
1458
- const { value } = attribute;
1459
- if (value?.type !== 'JSXExpressionContainer') {
1460
- return true;
1664
+ // The last source that has the key answers, even when its value is `undefined`.
1665
+ const resolve = (view: View, key: string): string | undefined => {
1666
+ for (let index = view.sources.length - 1; index >= 0; index -= 1) {
1667
+ const source = view.sources.at(index);
1668
+ if (!source || source.hidden.has(key)) {
1669
+ continue;
1461
1670
  }
1462
- return (
1463
- value.expression.type !== 'JSXEmptyExpression' && isDefinedExpression(value.expression)
1464
- );
1465
- }
1466
- const significant = element.children.filter((child) => !isInsignificant(child));
1467
- const only = significant.at(0);
1468
- if (significant.length !== 1 || only?.type !== 'JSXExpressionContainer') {
1469
- return true;
1470
- }
1471
- return only.expression.type !== 'JSXEmptyExpression' && isDefinedExpression(only.expression);
1472
- };
1473
- const resolve = (view: View, key: string): Resolution => {
1474
- const sources = view.sources.filter((source) => allows(source, key));
1475
- for (let index = sources.length - 1; index >= 0; index -= 1) {
1476
- const source = sources.at(index);
1477
- const text = source?.defaults?.get(key);
1671
+ const text = source.defaults?.get(key);
1478
1672
  if (text !== undefined) {
1479
- return { kind: 'text', text };
1673
+ return text;
1480
1674
  }
1481
- if (source && !source.defaults && has(key)) {
1482
- if (defined(key)) {
1483
- return { kind: 'props' };
1484
- }
1485
- const fallback = sources
1486
- .slice(0, index)
1487
- .findLast((earlier) => earlier.defaults?.has(key))
1488
- ?.defaults?.get(key);
1489
- return fallback === undefined ? { kind: 'props' } : { kind: 'fallback', text: fallback };
1675
+ if (!source.defaults && has(key)) {
1676
+ return undefined;
1490
1677
  }
1491
1678
  }
1492
- return { kind: 'text', text: 'void 0' };
1679
+ return 'void 0';
1493
1680
  };
1494
1681
 
1495
1682
  const propReads: PropRead[] = [...component.props];
1496
- const fallbacks = new Map<PropRead, string>();
1497
1683
  const texts = new Map<Node, string>();
1498
1684
  for (const read of component.viewReads) {
1499
- const resolution = resolve(read.view, read.name);
1500
- if (resolution.kind === 'props') {
1685
+ const text = resolve(read.view, read.name);
1686
+ if (text === undefined) {
1501
1687
  propReads.push(read);
1502
- } else if (resolution.kind === 'fallback') {
1503
- fallbacks.set(read, resolution.text);
1504
1688
  } else {
1505
- texts.set(read.member, resolution.text);
1689
+ texts.set(read.member, text);
1506
1690
  }
1507
1691
  }
1508
1692
 
@@ -1513,12 +1697,16 @@ class Inliner {
1513
1697
  const spreads: ResolvedSpread[] = [];
1514
1698
  const taken = new Map<JSXElement, Set<string>>();
1515
1699
  for (const spread of component.viewSpreads) {
1516
- // Keys come in the order the sources first hold them.
1517
- const order = new Set<string>();
1700
+ // A key sits where the last source that carries it puts it.
1701
+ const order: string[] = [];
1518
1702
  for (const source of spread.view.sources) {
1519
1703
  const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
1520
- for (const key of keys.filter((name) => allows(source, name))) {
1521
- order.add(key);
1704
+ for (const key of keys.filter((name) => !source.hidden.has(name))) {
1705
+ const index = order.indexOf(key);
1706
+ if (index !== -1) {
1707
+ order.splice(index, 1);
1708
+ }
1709
+ order.push(key);
1522
1710
  }
1523
1711
  }
1524
1712
  let names = taken.get(spread.element);
@@ -1535,23 +1723,22 @@ class Inliner {
1535
1723
  }
1536
1724
  taken.set(spread.element, names);
1537
1725
  }
1538
- const keys = new Map<string, Resolution>();
1726
+ const keys = new Map<string, string | undefined>();
1539
1727
  for (const key of order) {
1540
- const resolution = resolve(spread.view, key);
1541
1728
  // Children become the element's children, so it must have none of its own.
1542
1729
  const fits =
1543
1730
  key === 'children'
1544
- ? spread.element.openingElement.selfClosing && resolution.kind !== 'fallback'
1731
+ ? spread.element.openingElement.selfClosing
1545
1732
  : /^[A-Za-z_$][\w$-]*$/.test(key);
1546
1733
  if (!fits || names.has(key)) {
1547
1734
  return undefined;
1548
1735
  }
1549
1736
  names.add(key);
1550
- keys.set(key, resolution);
1737
+ keys.set(key, resolve(spread.view, key));
1551
1738
  }
1552
1739
  spreads.push({ spread, keys });
1553
1740
  }
1554
- return { propReads, fallbacks, texts, spreads };
1741
+ return { propReads, texts, spreads };
1555
1742
  }
1556
1743
 
1557
1744
  /**
@@ -1569,26 +1756,16 @@ class Inliner {
1569
1756
  const bare = text !== 'void 0' || acceptsAnyExpression(this.context, member);
1570
1757
  copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
1571
1758
  }
1572
- for (const [read, fallback] of views.fallbacks) {
1573
- const text = values.get(read.name)?.text ?? 'void 0';
1574
- copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
1575
- }
1576
1759
  for (const { spread, keys } of views.spreads) {
1577
1760
  const parts: string[] = [];
1578
1761
  let children: string | undefined;
1579
- for (const [key, resolution] of keys) {
1762
+ for (const [key, text] of keys) {
1580
1763
  const value = values.get(key);
1581
1764
  if (key === 'children') {
1582
- children = this.spreadChildren(element, resolution, value);
1583
- continue;
1584
- }
1585
- let text = value?.text ?? 'void 0';
1586
- if (resolution.kind === 'text') {
1587
- text = resolution.text;
1588
- } else if (resolution.kind === 'fallback') {
1589
- text = withFallback(text, resolution.text);
1765
+ children = this.spreadChildren(element, text, value);
1766
+ } else {
1767
+ parts.push(`${key}={${text ?? value?.text ?? 'void 0'}}`);
1590
1768
  }
1591
- parts.push(`${key}={${text}}`);
1592
1769
  }
1593
1770
  const { attribute } = spread;
1594
1771
  if (parts.length === 0) {
@@ -1614,11 +1791,11 @@ class Inliner {
1614
1791
  */
1615
1792
  private spreadChildren(
1616
1793
  element: JSXElement,
1617
- resolution: Resolution,
1794
+ text: string | undefined,
1618
1795
  value: PropValue | undefined,
1619
1796
  ): string | undefined {
1620
- if (resolution.kind === 'text') {
1621
- return resolution.text === 'void 0' ? undefined : `{${resolution.text}}`;
1797
+ if (text !== undefined) {
1798
+ return text === 'void 0' ? undefined : `{${text}}`;
1622
1799
  }
1623
1800
  if (!value) {
1624
1801
  return undefined;
@@ -1895,8 +2072,12 @@ class Inliner {
1895
2072
  */
1896
2073
  private sizeHeader(): string {
1897
2074
  if (this.header === undefined) {
2075
+ // The imports, and the top-level constants made of literals, so a copy
2076
+ // folds with the values its call site passes by name, as it will once inlined.
1898
2077
  const imports = this.context.program.body
1899
- .filter((statement) => statement.type === 'ImportDeclaration')
2078
+ .filter(
2079
+ (statement) => statement.type === 'ImportDeclaration' || isDataDeclaration(statement),
2080
+ )
1900
2081
  .map((statement) => this.context.code.slice(statement.start, statement.end));
1901
2082
  const aliases = [...this.context.options.builtInAliases.keys()];
1902
2083
  if (aliases.length > 0) {
@@ -1909,13 +2090,20 @@ class Inliner {
1909
2090
 
1910
2091
  run(): void {
1911
2092
  const components: Component[] = [];
2093
+ const rejected = new Map<Binding, string>();
1912
2094
  for (const binding of this.context.scopes.root.bindings.values()) {
2095
+ this.reason = 'not-a-function';
1913
2096
  const component = this.analyzeComponent(binding);
1914
2097
  if (component) {
1915
2098
  components.push(component);
2099
+ } else {
2100
+ rejected.set(binding, this.reason);
1916
2101
  }
1917
2102
  }
2103
+ // Why each call site that stays a call was not inlined, for the report.
2104
+ const siteReasons = new Map<JSXElement, string>();
1918
2105
  if (components.length === 0) {
2106
+ this.report(rejected, siteReasons);
1919
2107
  return;
1920
2108
  }
1921
2109
 
@@ -1962,6 +2150,9 @@ class Inliner {
1962
2150
  inlined.set(site.component, elements);
1963
2151
  } else {
1964
2152
  otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
2153
+ if (this.context.skips && !postponed) {
2154
+ siteReasons.set(site.element, this.siteReason(site, worthwhile));
2155
+ }
1965
2156
  }
1966
2157
  // A component holding a call that was inlined now, or postponed to the
1967
2158
  // next pass, waits too. A copy made now would copy the call as it is.
@@ -1975,6 +2166,7 @@ class Inliner {
1975
2166
  }
1976
2167
 
1977
2168
  this.writeHosts();
2169
+ this.report(rejected, siteReasons);
1978
2170
 
1979
2171
  // A component nothing uses anymore is removed. A copy made in this pass
1980
2172
  // comes from the original code, so a component used inside a copied
@@ -1993,6 +2185,83 @@ class Inliner {
1993
2185
  }
1994
2186
  }
1995
2187
 
2188
+ /**
2189
+ * Why a call site of a component was not inlined. A component that is
2190
+ * not worth inlining is only turned down for its size when the call site
2191
+ * could be inlined.
2192
+ */
2193
+ private siteReason(site: CallSite, worthwhile: ReadonlySet<Component>): string {
2194
+ if (worthwhile.has(site.component)) {
2195
+ return this.reason;
2196
+ }
2197
+ this.scratchNames = new Set(this.context.scopes.names);
2198
+ try {
2199
+ return this.prepare(site.component, site.element) ? 'size' : this.reason;
2200
+ } finally {
2201
+ this.scratchNames = undefined;
2202
+ }
2203
+ }
2204
+
2205
+ /**
2206
+ * Records each component call that stays a call, and why. Solid's
2207
+ * built-ins and context providers are left out. The provider pass
2208
+ * reports providers.
2209
+ */
2210
+ private report(
2211
+ rejected: ReadonlyMap<Binding, string>,
2212
+ siteReasons: ReadonlyMap<JSXElement, string>,
2213
+ ): void {
2214
+ if (!this.context.skips) {
2215
+ return;
2216
+ }
2217
+ const visit = (node: Node): void => {
2218
+ if (node.type === 'JSXElement') {
2219
+ const reason = this.callReason(node, rejected, siteReasons);
2220
+ if (reason) {
2221
+ recordSkip(this.context, 'inline', reason.reason, reason.name);
2222
+ }
2223
+ }
2224
+ forEachChild(node, visit);
2225
+ };
2226
+ visit(this.context.program);
2227
+ }
2228
+
2229
+ private callReason(
2230
+ element: JSXElement,
2231
+ rejected: ReadonlyMap<Binding, string>,
2232
+ siteReasons: ReadonlyMap<JSXElement, string>,
2233
+ ): { reason: string; name: string } | undefined {
2234
+ const tag = element.openingElement.name;
2235
+ if (tag.type === 'JSXMemberExpression') {
2236
+ return { reason: 'member-tag', name: textOf(this.context, tag) };
2237
+ }
2238
+ if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
2239
+ return undefined;
2240
+ }
2241
+ const { name } = tag;
2242
+ const binding = this.context.scopes.references.get(tag);
2243
+ const identity = binding ? solidExport(binding, this.context.options) : name;
2244
+ if (
2245
+ (identity !== undefined && this.context.options.builtIns.has(identity)) ||
2246
+ contextBinding(this.context, tag)
2247
+ ) {
2248
+ return undefined;
2249
+ }
2250
+ if (!binding) {
2251
+ return { reason: 'global', name };
2252
+ }
2253
+ if (binding.kind === 'import') {
2254
+ // A chunk imports other chunks through placeholders the bundler fills in later.
2255
+ const source = (binding.source ?? '').replaceAll(/!~\{\w+\}~/g, '*');
2256
+ return { reason: 'imported', name: `${name} (${source})` };
2257
+ }
2258
+ if (binding.scope.parent !== undefined) {
2259
+ return { reason: 'nested', name };
2260
+ }
2261
+ const reason = rejected.get(binding) ?? siteReasons.get(element);
2262
+ return reason === undefined ? undefined : { reason, name };
2263
+ }
2264
+
1996
2265
  /**
1997
2266
  * The whitespace before `position` on its line.
1998
2267
  */