solid-optimizer 0.4.1 → 1.0.0-next.1

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
@@ -15,8 +15,9 @@
15
15
  * function whose body is JSX.
16
16
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
17
17
  *
18
- * A call site inlines when it passes plain attributes: no spread, no `ref`,
19
- * 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`.
20
21
  *
21
22
  * # How props keep their meaning
22
23
  *
@@ -36,28 +37,36 @@
36
37
  *
37
38
  * Statements, and stored props, have to run where the component would have
38
39
  * been created. Solid runs a component untracked, so the statements move into
39
- * the function whose returned JSX holds the call: a component itself, which
40
- * also runs untracked. The call has to be in that returned JSX with only
41
- * intrinsic elements and fragments around it, so it is created exactly once
42
- * 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.
43
53
  *
44
54
  * A context provider is a component, so statements do not move out of it,
45
55
  * and `useContext` still runs under it. The exception is a provider whose
46
56
  * subtree only runs visible code. Statements move out through it, and each
47
57
  * `useContext` of its context in a copy becomes its value. See `provider.ts`.
48
58
  *
49
- * The props can also be read through `mergeProps()` and `splitProps()` views
50
- * declared in the component. Each call site knows which props it passes, so
51
- * a read of a view resolves to a prop, a prop with a literal default, a
52
- * default, or `undefined`, and a spread of a view becomes the attributes it
53
- * 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.
54
63
  *
55
64
  * # Accepted differences
56
65
  *
57
66
  * - A prop that is a function and is called as `props.name()` runs with
58
67
  * `this` set to its own receiver instead of the props object.
59
68
  * - Statements moved into the host run before the host's JSX is created,
60
- * 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
61
70
  * registration across siblings can change.
62
71
  */
63
72
  import MagicString from 'magic-string';
@@ -91,17 +100,18 @@ import { isConstantDeclaration, recordSkip, solidExport, textOf } from './contex
91
100
  import type { Piece } from './jsx';
92
101
  import type { ClosedOptions, Provider } from './provider';
93
102
  import {
103
+ contextBinding,
94
104
  contextReads,
95
105
  findProviders,
96
106
  isClosed,
97
- providerContext,
98
107
  solidCallee,
99
108
  stableValueText,
100
109
  } from './provider';
101
110
  import { keptPiece, replacedPiece, writePieces } from './jsx';
102
111
  import type { Binding, Scope } from './scope';
103
112
  import { isWithinScope, lookup, scopeAt } from './scope';
104
- import { SizeEstimator, copySize } from './size';
113
+ import { copySize } from './copy-size';
114
+ import { SizeEstimator } from './size';
105
115
  import { evaluate, literalText } from './value';
106
116
 
107
117
  type ComponentFunction = FunctionNode | ArrowFunctionExpression;
@@ -117,19 +127,16 @@ interface PropRead extends ReadCount {
117
127
  }
118
128
 
119
129
  /**
120
- * One source of a `mergeProps()` or `splitProps()` view: the props, or an
121
- * object of literal defaults, with the keys a `splitProps()` hides from it
122
- * 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.
123
132
  */
124
133
  interface ViewSource {
125
134
  /** The default of each key, as literal text, or `undefined` for the props. */
126
135
  readonly defaults: ReadonlyMap<string, string> | undefined;
127
136
  readonly hidden: ReadonlySet<string>;
128
- /** The only keys it holds, for a group `splitProps()` picked. */
129
- readonly only: ReadonlySet<string> | undefined;
130
137
  }
131
138
 
132
- /** A props object made with `mergeProps()` and `splitProps()`, in source order. */
139
+ /** A props object made with `merge()` and `omit()`, in source order. */
133
140
  interface View {
134
141
  readonly sources: readonly ViewSource[];
135
142
  }
@@ -138,63 +145,20 @@ interface ViewRead extends PropRead {
138
145
  readonly view: View;
139
146
  }
140
147
 
141
- /**
142
- * What a key of a view reads as at a call site: the prop, the prop with a
143
- * default for when it is `undefined`, or a text.
144
- */
145
- type Resolution =
146
- | { readonly kind: 'props' }
147
- | { readonly kind: 'fallback'; readonly text: string }
148
- | { readonly kind: 'text'; readonly text: string };
149
-
150
148
  interface ResolvedSpread {
151
149
  readonly spread: ViewSpread;
152
- 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>;
153
152
  }
154
153
 
155
154
  interface ResolvedViews {
156
155
  /** Reads of the props, including view reads the props answer. */
157
156
  readonly propReads: readonly PropRead[];
158
- /** View reads the props answer, unless the prop is `undefined`. */
159
- readonly fallbacks: ReadonlyMap<PropRead, string>;
160
157
  /** View reads a default or nothing answers, and their text. */
161
158
  readonly texts: ReadonlyMap<Node, string>;
162
159
  readonly spreads: readonly ResolvedSpread[];
163
160
  }
164
161
 
165
- /**
166
- * A prop read that falls back to a default when the prop is `undefined`,
167
- * the way `mergeProps()` reads it.
168
- */
169
- function withFallback(text: string, fallback: string): string {
170
- return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
171
- }
172
-
173
- function allows(source: ViewSource, key: string): boolean {
174
- return !source.hidden.has(key) && (source.only === undefined || source.only.has(key));
175
- }
176
-
177
- /**
178
- * Whether an expression passed as a prop is never `undefined`.
179
- */
180
- function isDefinedExpression(node: Expression): boolean {
181
- const inner = unwrap(node);
182
- switch (inner.type) {
183
- case 'ObjectExpression':
184
- case 'ArrayExpression':
185
- case 'ArrowFunctionExpression':
186
- case 'FunctionExpression':
187
- case 'ClassExpression':
188
- case 'NewExpression':
189
- case 'TemplateLiteral':
190
- case 'JSXElement':
191
- case 'JSXFragment':
192
- return true;
193
- default:
194
- return evaluate(inner, () => undefined)?.value !== undefined;
195
- }
196
- }
197
-
198
162
  interface ViewSpread extends ReadCount {
199
163
  readonly attribute: JSXSpreadAttribute;
200
164
  readonly element: JSXElement;
@@ -216,7 +180,7 @@ interface Component {
216
180
  readonly props: readonly PropRead[];
217
181
  /** The props parameter, when there is one. */
218
182
  readonly param: Binding | undefined;
219
- /** 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. */
220
184
  readonly viewReads: readonly ViewRead[];
221
185
  /** Spreads of a view onto an element, which become the attributes it holds. */
222
186
  readonly viewSpreads: readonly ViewSpread[];
@@ -404,7 +368,7 @@ function isDynamic(node: Node): boolean {
404
368
  * Whether any expression short of a sequence can replace `node` without
405
369
  * parentheses, because its slot is a whole expression of its own.
406
370
  */
407
- export function acceptsAnyExpression(context: PassContext, node: Node): boolean {
371
+ function acceptsAnyExpression(context: PassContext, node: Node): boolean {
408
372
  const parent = context.parents.get(node);
409
373
  if (!parent) {
410
374
  return false;
@@ -459,12 +423,12 @@ function functionOf(binding: Binding, options: ResolvedOptions): ComponentFuncti
459
423
  function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefined {
460
424
  const attributes = new Map<string, JSXAttribute>();
461
425
  for (const attribute of element.openingElement.attributes) {
462
- // A spread can supply any prop, and a namespaced attribute or `ref` is
463
- // 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.
464
428
  if (
465
429
  attribute.type !== 'JSXAttribute' ||
466
430
  attribute.name.type !== 'JSXIdentifier' ||
467
- attribute.name.name === 'ref' ||
431
+ (attribute.name.name === 'ref' && !isForwardableRef(attribute)) ||
468
432
  attributes.has(attribute.name.name)
469
433
  ) {
470
434
  return undefined;
@@ -474,6 +438,90 @@ function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefine
474
438
  return attributes;
475
439
  }
476
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
+
477
525
  /**
478
526
  * Why `attributesOf` turns down a call site, for the report.
479
527
  */
@@ -801,9 +849,9 @@ class Inliner {
801
849
  }
802
850
 
803
851
  /**
804
- * Finds the `mergeProps()` and `splitProps()` views of the props declared
805
- * in a component's statements, and how each is used. A view can only be
806
- * 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.
807
855
  * `undefined` when a view is used any other way.
808
856
  */
809
857
  private analyzeViews(
@@ -830,47 +878,32 @@ class Inliner {
830
878
  statement.type !== 'VariableDeclaration' ||
831
879
  statement.kind !== 'const' ||
832
880
  statement.declarations.length !== 1 ||
833
- !declarator?.init
881
+ declarator?.id.type !== 'Identifier' ||
882
+ !declarator.init
834
883
  ) {
835
884
  continue;
836
885
  }
837
886
  const init = unwrap(declarator.init);
838
- if (init.type !== 'CallExpression') {
839
- continue;
840
- }
841
- const callee = solidCallee(this.context, init);
842
- let declared: [Node | null, View][] | null | undefined;
843
- if (callee === 'mergeProps' && declarator.id.type === 'Identifier') {
844
- const view = this.viewOf(init, param, views, args);
845
- declared = view ? [[declarator.id, view]] : view;
846
- } else if (callee === 'splitProps' && declarator.id.type === 'ArrayPattern') {
847
- declared = this.splitViews(init, declarator.id.elements, param, views, args);
848
- } else {
887
+ const callee = init.type === 'CallExpression' ? solidCallee(this.context, init) : undefined;
888
+ if (init.type !== 'CallExpression' || (callee !== 'merge' && callee !== 'omit')) {
849
889
  continue;
850
890
  }
851
- if (declared === null) {
891
+ const view = this.viewOf(callee, init, param, views, args);
892
+ if (view === null) {
852
893
  continue;
853
894
  }
854
- if (!declared) {
895
+ const binding = scope.bindings.get(declarator.id.name);
896
+ if (!view || !binding || binding.mutated) {
855
897
  return undefined;
856
898
  }
857
- for (const [identifier, view] of declared) {
858
- if (identifier?.type !== 'Identifier') {
859
- continue;
860
- }
861
- const binding = scope.bindings.get(identifier.name);
862
- if (!binding || binding.mutated) {
863
- return undefined;
864
- }
865
- views.set(binding, view);
866
- }
899
+ views.set(binding, view);
867
900
  declarations.push(statement);
868
901
  }
869
902
 
870
903
  const reads: ViewRead[] = [];
871
904
  const spreads: ViewSpread[] = [];
872
905
  // `{...props}` spreads every prop, like a view of the props with nothing hidden.
873
- const whole: View = { sources: [{ defaults: undefined, hidden: new Set(), only: undefined }] };
906
+ const whole: View = { sources: [{ defaults: undefined, hidden: new Set() }] };
874
907
  for (const reference of param?.references ?? []) {
875
908
  const spread = this.spreadOnElement(reference);
876
909
  if (spread) {
@@ -923,57 +956,75 @@ class Inliner {
923
956
  }
924
957
 
925
958
  /**
926
- * The sources a view argument stands for: the props, a view, or a nested
927
- * `mergeProps()` call.
928
- */
929
- private viewSources(
930
- argument: Node,
931
- param: Binding | undefined,
932
- views: ReadonlyMap<Binding, View>,
933
- args: Set<Node>,
934
- ): readonly ViewSource[] | undefined {
935
- if (argument.type === 'CallExpression') {
936
- if (solidCallee(this.context, argument) !== 'mergeProps') {
937
- return undefined;
938
- }
939
- return this.viewOf(argument, param, views, args)?.sources;
940
- }
941
- if (argument.type !== 'Identifier') {
942
- return undefined;
943
- }
944
- const binding = this.context.scopes.references.get(argument);
945
- if (binding && binding === param) {
946
- return [{ defaults: undefined, hidden: new Set(), only: undefined }];
947
- }
948
- return binding ? views.get(binding)?.sources : undefined;
949
- }
950
-
951
- /**
952
- * The view a `mergeProps()` call makes. `null` when it does not take the
953
- * props or another view, and `undefined` when it does but its other
954
- * 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.
955
962
  */
956
963
  private viewOf(
964
+ callee: 'merge' | 'omit',
957
965
  call: CallExpression,
958
966
  param: Binding | undefined,
959
967
  views: ReadonlyMap<Binding, View>,
960
968
  args: Set<Node>,
961
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
+ };
962
987
  const involved = call.arguments.some(
963
- (argument) =>
964
- argument.type !== 'SpreadElement' &&
965
- this.viewSources(unwrap(argument), param, views, new Set()) !== undefined,
988
+ (argument) => argument.type !== 'SpreadElement' && sourcesOf(unwrap(argument)) !== undefined,
966
989
  );
967
990
  if (!involved) {
968
991
  return null;
969
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
+
970
1021
  const sources: ViewSource[] = [];
971
1022
  for (const argument of call.arguments) {
972
1023
  if (argument.type === 'SpreadElement') {
973
1024
  return undefined;
974
1025
  }
975
1026
  const inner = unwrap(argument);
976
- const existing = this.viewSources(inner, param, views, args);
1027
+ const existing = sourcesOf(inner);
977
1028
  if (existing) {
978
1029
  args.add(inner);
979
1030
  sources.push(...existing);
@@ -1002,74 +1053,14 @@ class Inliner {
1002
1053
  }
1003
1054
  defaults.set(key, text);
1004
1055
  }
1005
- sources.push({ defaults, hidden: new Set(), only: undefined });
1056
+ sources.push({ defaults, hidden: new Set() });
1006
1057
  }
1007
1058
  return { sources };
1008
1059
  }
1009
1060
 
1010
- /**
1011
- * The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
1012
- * each with its identifier. A key belongs to the first group that names
1013
- * it, and the last view holds the keys no group names. `null` when the
1014
- * call does not take the props or a view.
1015
- */
1016
- private splitViews(
1017
- call: CallExpression,
1018
- elements: readonly (Node | null)[],
1019
- param: Binding | undefined,
1020
- views: ReadonlyMap<Binding, View>,
1021
- args: Set<Node>,
1022
- ): [Node | null, View][] | null | undefined {
1023
- const first = call.arguments.at(0);
1024
- if (first === undefined || first.type === 'SpreadElement') {
1025
- return null;
1026
- }
1027
- const target = unwrap(first);
1028
- const base = this.viewSources(target, param, views, args);
1029
- if (!base) {
1030
- return null;
1031
- }
1032
- args.add(target);
1033
- const groups: string[][] = [];
1034
- for (const group of call.arguments.slice(1)) {
1035
- if (group.type !== 'ArrayExpression') {
1036
- return undefined;
1037
- }
1038
- const keys: string[] = [];
1039
- for (const key of group.elements) {
1040
- if (key?.type !== 'Literal' || typeof key.value !== 'string') {
1041
- return undefined;
1042
- }
1043
- keys.push(key.value);
1044
- }
1045
- groups.push(keys);
1046
- }
1047
- if (
1048
- elements.length > groups.length + 1 ||
1049
- elements.some((element) => element?.type === 'RestElement')
1050
- ) {
1051
- return undefined;
1052
- }
1053
- const claimed = new Set<string>();
1054
- const filters = groups.map((keys) => {
1055
- const owned = keys.filter((key) => !claimed.has(key));
1056
- for (const key of owned) {
1057
- claimed.add(key);
1058
- }
1059
- return new Set(owned);
1060
- });
1061
- return elements.map((element, index): [Node | null, View] => {
1062
- const only = filters.at(index);
1063
- const sources = base.map((source) => ({
1064
- defaults: source.defaults,
1065
- hidden: only ? source.hidden : new Set([...source.hidden, ...claimed]),
1066
- only: only
1067
- ? new Set([...only].filter((key) => source.only === undefined || source.only.has(key)))
1068
- : source.only,
1069
- }));
1070
- return [element, { sources }];
1071
- });
1072
- }
1061
+ // ---------------------------------------------------------------------------
1062
+ // Call sites
1063
+ // ---------------------------------------------------------------------------
1073
1064
 
1074
1065
  /**
1075
1066
  * Whether a function is a component, which Solid runs untracked. Only a
@@ -1096,6 +1087,68 @@ class Inliner {
1096
1087
  );
1097
1088
  }
1098
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
+
1099
1152
  /**
1100
1153
  * Where the statements of a component created at `element` can go: the
1101
1154
  * returned JSX of a component, with only intrinsic elements, fragments,
@@ -1111,9 +1164,9 @@ class Inliner {
1111
1164
  if (parent.type === 'JSXElement') {
1112
1165
  const { name } = parent.openingElement;
1113
1166
  const passable =
1114
- this.transparent.has(parent) ||
1115
1167
  this.assumedHosts?.has(parent) === true ||
1116
- (name.type === 'JSXIdentifier' && isIntrinsicTag(name.name));
1168
+ (name.type === 'JSXIdentifier' &&
1169
+ (isIntrinsicTag(name.name) || this.transparent.has(parent)));
1117
1170
  if (!passable) {
1118
1171
  return undefined;
1119
1172
  }
@@ -1130,12 +1183,12 @@ class Inliner {
1130
1183
  while (fn && !isFunctionNode(fn)) {
1131
1184
  fn = this.context.parents.get(fn);
1132
1185
  }
1133
- if (!fn || !this.isComponentFunction(fn)) {
1186
+ if (!fn || !this.isHostFunction(fn)) {
1134
1187
  return undefined;
1135
1188
  }
1136
1189
  return { fn, host: { kind: 'return', statement: parent } };
1137
1190
  } else if (parent.type === 'ArrowFunctionExpression') {
1138
- if (parent.body !== node || !this.isComponentFunction(parent)) {
1191
+ if (parent.body !== node || !this.isHostFunction(parent)) {
1139
1192
  return undefined;
1140
1193
  }
1141
1194
  return { fn: parent, host: { kind: 'arrow', body: parent.body } };
@@ -1183,6 +1236,43 @@ class Inliner {
1183
1236
  return { kind: 'children', children, text, primary };
1184
1237
  }
1185
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
+
1186
1276
  /**
1187
1277
  * How a prop reads in a copy of the component, and the `const` that stores
1188
1278
  * it first when the value has to be evaluated once.
@@ -1287,16 +1377,18 @@ class Inliner {
1287
1377
  */
1288
1378
  private assumedHosts: ReadonlySet<JSXElement> | undefined;
1289
1379
 
1290
- private freshName(base: string): string {
1380
+ private freshName(name: string): string {
1291
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+$/, '');
1292
1384
  let index = 1;
1293
- let name = `${base}$${String(index)}`;
1294
- while (names.has(name)) {
1385
+ let fresh = `${base}$${String(index)}`;
1386
+ while (names.has(fresh)) {
1295
1387
  index += 1;
1296
- name = `${base}$${String(index)}`;
1388
+ fresh = `${base}$${String(index)}`;
1297
1389
  }
1298
- names.add(name);
1299
- return name;
1390
+ names.add(fresh);
1391
+ return fresh;
1300
1392
  }
1301
1393
 
1302
1394
  /**
@@ -1328,7 +1420,7 @@ class Inliner {
1328
1420
  }
1329
1421
 
1330
1422
  // What each view read and spread resolves to at this call site.
1331
- const views = this.resolveViews(component, element, attributes, hasChildren);
1423
+ const views = this.resolveViews(component, attributes, hasChildren);
1332
1424
  if (!views) {
1333
1425
  this.reason = 'view-conflict';
1334
1426
  return undefined;
@@ -1346,12 +1438,9 @@ class Inliner {
1346
1438
  for (const read of views.propReads) {
1347
1439
  count(read.name, read);
1348
1440
  }
1349
- for (const read of views.fallbacks.keys()) {
1350
- count(read.name, read);
1351
- }
1352
1441
  for (const spread of views.spreads) {
1353
- for (const [key, resolution] of spread.keys) {
1354
- if (resolution.kind !== 'text') {
1442
+ for (const [key, source] of spread.keys) {
1443
+ if (source === undefined) {
1355
1444
  count(key, spread.spread);
1356
1445
  }
1357
1446
  }
@@ -1361,11 +1450,14 @@ class Inliner {
1361
1450
  let value: PropValue | undefined;
1362
1451
  if (name === 'children' && !attributes.has('children')) {
1363
1452
  value = this.childrenValue(element, reads, fresh, snapshots);
1453
+ } else if (name === 'ref' && attributes.has('ref')) {
1454
+ value = this.refValue(attributes.get('ref'), reads);
1364
1455
  } else {
1365
1456
  value = this.propValue(attributes.get(name), reads, fresh, snapshots);
1366
1457
  }
1367
1458
  if (!value) {
1368
- this.reason = 'prop-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';
1369
1461
  return undefined;
1370
1462
  }
1371
1463
  values.set(name, value);
@@ -1461,7 +1553,7 @@ class Inliner {
1461
1553
  ) {
1462
1554
  const target =
1463
1555
  node.type === 'JSXElement'
1464
- ? providerContext(this.context, node.openingElement.name)
1556
+ ? contextBinding(this.context, node.openingElement.name)
1465
1557
  : undefined;
1466
1558
  if (node.type !== 'JSXElement' || !target || seen.has(target)) {
1467
1559
  continue;
@@ -1556,73 +1648,45 @@ class Inliner {
1556
1648
  }
1557
1649
 
1558
1650
  /**
1559
- * Resolves each view read and spread of a component at a call site.
1560
- * `mergeProps()` reads the last source whose value is not `undefined`, so
1561
- * a prop that can be `undefined` falls back to the default behind it. A
1562
- * spread becomes the keys it holds, and its `children` become the
1563
- * element's children. `undefined` when a spread repeats an attribute, or
1564
- * 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.
1565
1656
  */
1566
1657
  private resolveViews(
1567
1658
  component: Component,
1568
- element: JSXElement,
1569
1659
  attributes: ReadonlyMap<string, JSXAttribute>,
1570
1660
  hasChildren: boolean,
1571
1661
  ): ResolvedViews | undefined {
1572
1662
  const has = (key: string): boolean =>
1573
1663
  attributes.has(key) || (key === 'children' && hasChildren);
1574
- // Whether the prop the call site passes for `key` is never `undefined`.
1575
- const defined = (key: string): boolean => {
1576
- const attribute = attributes.get(key);
1577
- if (attribute) {
1578
- const { value } = attribute;
1579
- if (value?.type !== 'JSXExpressionContainer') {
1580
- 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;
1581
1670
  }
1582
- return (
1583
- value.expression.type !== 'JSXEmptyExpression' && isDefinedExpression(value.expression)
1584
- );
1585
- }
1586
- const significant = element.children.filter((child) => !isInsignificant(child));
1587
- const only = significant.at(0);
1588
- if (significant.length !== 1 || only?.type !== 'JSXExpressionContainer') {
1589
- return true;
1590
- }
1591
- return only.expression.type !== 'JSXEmptyExpression' && isDefinedExpression(only.expression);
1592
- };
1593
- const resolve = (view: View, key: string): Resolution => {
1594
- const sources = view.sources.filter((source) => allows(source, key));
1595
- for (let index = sources.length - 1; index >= 0; index -= 1) {
1596
- const source = sources.at(index);
1597
- const text = source?.defaults?.get(key);
1671
+ const text = source.defaults?.get(key);
1598
1672
  if (text !== undefined) {
1599
- return { kind: 'text', text };
1673
+ return text;
1600
1674
  }
1601
- if (source && !source.defaults && has(key)) {
1602
- if (defined(key)) {
1603
- return { kind: 'props' };
1604
- }
1605
- const fallback = sources
1606
- .slice(0, index)
1607
- .findLast((earlier) => earlier.defaults?.has(key))
1608
- ?.defaults?.get(key);
1609
- return fallback === undefined ? { kind: 'props' } : { kind: 'fallback', text: fallback };
1675
+ if (!source.defaults && has(key)) {
1676
+ return undefined;
1610
1677
  }
1611
1678
  }
1612
- return { kind: 'text', text: 'void 0' };
1679
+ return 'void 0';
1613
1680
  };
1614
1681
 
1615
1682
  const propReads: PropRead[] = [...component.props];
1616
- const fallbacks = new Map<PropRead, string>();
1617
1683
  const texts = new Map<Node, string>();
1618
1684
  for (const read of component.viewReads) {
1619
- const resolution = resolve(read.view, read.name);
1620
- if (resolution.kind === 'props') {
1685
+ const text = resolve(read.view, read.name);
1686
+ if (text === undefined) {
1621
1687
  propReads.push(read);
1622
- } else if (resolution.kind === 'fallback') {
1623
- fallbacks.set(read, resolution.text);
1624
1688
  } else {
1625
- texts.set(read.member, resolution.text);
1689
+ texts.set(read.member, text);
1626
1690
  }
1627
1691
  }
1628
1692
 
@@ -1633,12 +1697,16 @@ class Inliner {
1633
1697
  const spreads: ResolvedSpread[] = [];
1634
1698
  const taken = new Map<JSXElement, Set<string>>();
1635
1699
  for (const spread of component.viewSpreads) {
1636
- // Keys come in the order the sources first hold them.
1637
- const order = new Set<string>();
1700
+ // A key sits where the last source that carries it puts it.
1701
+ const order: string[] = [];
1638
1702
  for (const source of spread.view.sources) {
1639
1703
  const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
1640
- for (const key of keys.filter((name) => allows(source, name))) {
1641
- 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);
1642
1710
  }
1643
1711
  }
1644
1712
  let names = taken.get(spread.element);
@@ -1655,23 +1723,22 @@ class Inliner {
1655
1723
  }
1656
1724
  taken.set(spread.element, names);
1657
1725
  }
1658
- const keys = new Map<string, Resolution>();
1726
+ const keys = new Map<string, string | undefined>();
1659
1727
  for (const key of order) {
1660
- const resolution = resolve(spread.view, key);
1661
1728
  // Children become the element's children, so it must have none of its own.
1662
1729
  const fits =
1663
1730
  key === 'children'
1664
- ? spread.element.openingElement.selfClosing && resolution.kind !== 'fallback'
1731
+ ? spread.element.openingElement.selfClosing
1665
1732
  : /^[A-Za-z_$][\w$-]*$/.test(key);
1666
1733
  if (!fits || names.has(key)) {
1667
1734
  return undefined;
1668
1735
  }
1669
1736
  names.add(key);
1670
- keys.set(key, resolution);
1737
+ keys.set(key, resolve(spread.view, key));
1671
1738
  }
1672
1739
  spreads.push({ spread, keys });
1673
1740
  }
1674
- return { propReads, fallbacks, texts, spreads };
1741
+ return { propReads, texts, spreads };
1675
1742
  }
1676
1743
 
1677
1744
  /**
@@ -1689,26 +1756,16 @@ class Inliner {
1689
1756
  const bare = text !== 'void 0' || acceptsAnyExpression(this.context, member);
1690
1757
  copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
1691
1758
  }
1692
- for (const [read, fallback] of views.fallbacks) {
1693
- const text = values.get(read.name)?.text ?? 'void 0';
1694
- copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
1695
- }
1696
1759
  for (const { spread, keys } of views.spreads) {
1697
1760
  const parts: string[] = [];
1698
1761
  let children: string | undefined;
1699
- for (const [key, resolution] of keys) {
1762
+ for (const [key, text] of keys) {
1700
1763
  const value = values.get(key);
1701
1764
  if (key === 'children') {
1702
- children = this.spreadChildren(element, resolution, value);
1703
- continue;
1704
- }
1705
- let text = value?.text ?? 'void 0';
1706
- if (resolution.kind === 'text') {
1707
- text = resolution.text;
1708
- } else if (resolution.kind === 'fallback') {
1709
- text = withFallback(text, resolution.text);
1765
+ children = this.spreadChildren(element, text, value);
1766
+ } else {
1767
+ parts.push(`${key}={${text ?? value?.text ?? 'void 0'}}`);
1710
1768
  }
1711
- parts.push(`${key}={${text}}`);
1712
1769
  }
1713
1770
  const { attribute } = spread;
1714
1771
  if (parts.length === 0) {
@@ -1734,11 +1791,11 @@ class Inliner {
1734
1791
  */
1735
1792
  private spreadChildren(
1736
1793
  element: JSXElement,
1737
- resolution: Resolution,
1794
+ text: string | undefined,
1738
1795
  value: PropValue | undefined,
1739
1796
  ): string | undefined {
1740
- if (resolution.kind === 'text') {
1741
- return resolution.text === 'void 0' ? undefined : `{${resolution.text}}`;
1797
+ if (text !== undefined) {
1798
+ return text === 'void 0' ? undefined : `{${text}}`;
1742
1799
  }
1743
1800
  if (!value) {
1744
1801
  return undefined;
@@ -2015,8 +2072,12 @@ class Inliner {
2015
2072
  */
2016
2073
  private sizeHeader(): string {
2017
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.
2018
2077
  const imports = this.context.program.body
2019
- .filter((statement) => statement.type === 'ImportDeclaration')
2078
+ .filter(
2079
+ (statement) => statement.type === 'ImportDeclaration' || isDataDeclaration(statement),
2080
+ )
2020
2081
  .map((statement) => this.context.code.slice(statement.start, statement.end));
2021
2082
  const aliases = [...this.context.options.builtInAliases.keys()];
2022
2083
  if (aliases.length > 0) {
@@ -2171,10 +2232,6 @@ class Inliner {
2171
2232
  siteReasons: ReadonlyMap<JSXElement, string>,
2172
2233
  ): { reason: string; name: string } | undefined {
2173
2234
  const tag = element.openingElement.name;
2174
- // The provider pass reports providers.
2175
- if (providerContext(this.context, tag)) {
2176
- return undefined;
2177
- }
2178
2235
  if (tag.type === 'JSXMemberExpression') {
2179
2236
  return { reason: 'member-tag', name: textOf(this.context, tag) };
2180
2237
  }
@@ -2184,7 +2241,10 @@ class Inliner {
2184
2241
  const { name } = tag;
2185
2242
  const binding = this.context.scopes.references.get(tag);
2186
2243
  const identity = binding ? solidExport(binding, this.context.options) : name;
2187
- if (identity !== undefined && this.context.options.builtIns.has(identity)) {
2244
+ if (
2245
+ (identity !== undefined && this.context.options.builtIns.has(identity)) ||
2246
+ contextBinding(this.context, tag)
2247
+ ) {
2188
2248
  return undefined;
2189
2249
  }
2190
2250
  if (!binding) {