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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"compile-X_SMDFr2.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCD/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;;EAQA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAqGI,QAAQ,cAAc,UAAS,iBAAsB;UAiFpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
1
+ {"version":3,"file":"compile-X_SMDFr2.d.mts","names":[],"sources":["../src/value.ts","../src/context.ts","../src/constants.ts","../src/compile.ts"],"mappings":";;;;KAUY;;;;KC4DA;;;;;UAMK;WACN,MAAM;;WAEN;;WAEA;;;;;;;;KC9DC,oBAAoB,SAAS,eAAe,SAAS,eAAe;;;UCA/D;;;;;;;EAOf;;;;;;;EAOA;;;;;;EAMA;;;;;;;EAOA;;;;;;;EAOA;;;;;;;;EAQA;;;;;;;EAOA;;;;;;;EAOA;;;;;;EAMA,iBAAiB,SAAS;;;;;;;EAO1B;;;;;;EAMA,oBAAoB;;;;;;EAMpB,kBAAkB,SAAS,eAAe;;;;;;EAM1C,mBAAmB,SAAS;;;;;;;;EAQ5B,mBAAmB,SAAS;;;;;;;;EAQ5B;;;;;;EAMA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;EAOA;;UAGe;EACf;;EAEA,KAAK;;;;;EAKL,UAAU;;;;;;;;;;;;;iBAqGI,QAAQ,cAAc,UAAS,iBAAsB;UAqGpD;;;;;EAKf;;;;;;EAMA,SAAS,eAAe;;;;;;;;iBASV,oBACd,cACA,UAAS,KAAK,wEACb"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { n as readModuleConstants, t as compile } from "./compile-DnfP-Quf.mjs";
1
+ import { n as readModuleConstants, t as compile } from "./compile-BuPR3pVT.mjs";
2
2
  export { compile, readModuleConstants };
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-DnfP-Quf.mjs";
1
+ import { a as lookup, c as forEachChild, d as unwrap, i as analyzeScopes, l as isIntrinsicTag, n as readModuleConstants, o as scopeAt, r as CONTEXT_SAFE_PRIMITIVES, s as collectParents, t as compile, u as parse } from "./compile-BuPR3pVT.mjs";
2
2
  import { createRequire } from "node:module";
3
3
  import remapping from "@jridgewell/remapping";
4
4
  import MagicString from "magic-string";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "1.0.0-next.6",
3
+ "version": "1.0.0-next.7",
4
4
  "description": "Experimental compile-time optimizer for SolidJS",
5
5
  "keywords": [
6
6
  "compiler",
@@ -36,14 +36,17 @@
36
36
  "dependencies": {
37
37
  "@jridgewell/remapping": "^2.3.5",
38
38
  "magic-string": "^1.4.2",
39
- "oxc-parser": "^0.151.0"
39
+ "oxc-parser": "^0.151.0",
40
+ "parse5": "^8.1.0"
40
41
  },
41
42
  "devDependencies": {
43
+ "@fast-check/vitest": "^0.5.0",
42
44
  "@jridgewell/trace-mapping": "^0.3.31",
43
45
  "@solidjs/compiler": "2.0.0-rc.21",
44
46
  "@solidjs/vite-plugin": "3.0.0-next.50",
45
47
  "@solidjs/web": "2.0.0-rc.21",
46
48
  "@types/node": "^26.6.3",
49
+ "fast-check": "^4.10.2",
47
50
  "happy-dom": "^20.14.5",
48
51
  "publint": "^0.3.24",
49
52
  "solid-js": "2.0.0-rc.21",
package/src/compile.ts CHANGED
@@ -10,6 +10,7 @@ import type { PassContext, ResolvedOptions, Skip } from './context';
10
10
  import { fold } from './fold';
11
11
  import { inline } from './inline';
12
12
  import { inlineMemos } from './memo';
13
+ import { repairNesting } from './nesting';
13
14
  import { removeProviders } from './provider';
14
15
  import { lowerConditionals } from './conditional';
15
16
  import type { ScopeAnalysis } from './scope';
@@ -331,6 +332,26 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
331
332
  }
332
333
  }
333
334
 
335
+ // A pass can put an element straight inside a parent that cannot hold it,
336
+ // which Solid's JSX transform rejects. Only a changed module can have one
337
+ // that its source did not.
338
+ if (current !== code) {
339
+ const result = runPass(
340
+ parsedCurrent(),
341
+ filename,
342
+ resolved,
343
+ sourceMap,
344
+ repairNesting,
345
+ undefined,
346
+ );
347
+ if (result) {
348
+ current = result.code;
349
+ if (result.map !== undefined) {
350
+ maps.push(result.map);
351
+ }
352
+ }
353
+ }
354
+
334
355
  const report = skipped ? { skipped } : {};
335
356
  if (maps.length === 0) {
336
357
  return { code: current, map: null, ...report };
package/src/fold.ts CHANGED
@@ -753,14 +753,14 @@ class Folder {
753
753
  };
754
754
  case 'JSXElement':
755
755
  case 'JSXFragment':
756
- return { kind: 'jsx', text: getText(this.context, value) };
756
+ return this.foldedJSX(value);
757
757
  default:
758
758
  if (value.expression.type === 'JSXEmptyExpression') {
759
759
  return EMPTY;
760
760
  }
761
761
  // JSX in a container splices like JSX, so it can join the surrounding template.
762
762
  if (value.expression.type === 'JSXElement' || value.expression.type === 'JSXFragment') {
763
- return { kind: 'jsx', text: getText(this.context, value.expression) };
763
+ return this.foldedJSX(value.expression);
764
764
  }
765
765
  return {
766
766
  kind: 'expression',
@@ -770,6 +770,19 @@ class Folder {
770
770
  }
771
771
  }
772
772
 
773
+ /**
774
+ * What a JSX value renders, read after the folds of this round. A fold
775
+ * inside it can have replaced it with an expression already, such as an
776
+ * inner `<Show>` that became its string `fallback`.
777
+ */
778
+ private foldedJSX(node: JSXElement | JSXFragment): Fold {
779
+ const text = getText(this.context, node);
780
+ if (text.startsWith('<')) {
781
+ return { kind: 'jsx', text };
782
+ }
783
+ return { kind: 'expression', text, primary: false };
784
+ }
785
+
773
786
  private foldShow(element: JSXElement): Fold | undefined {
774
787
  const when = this.attributeTruthiness(element, 'when');
775
788
  if (when === undefined) {
package/src/inline.ts CHANGED
@@ -112,7 +112,7 @@ import {
112
112
  solidCallee,
113
113
  stableValueText,
114
114
  } from './provider';
115
- import { keptPiece, replacedPiece, writePieces } from './jsx';
115
+ import { keptPiece, replacedPiece, splicePiece, writePieces } from './jsx';
116
116
  import type { Binding, Scope } from './scope';
117
117
  import { isWithinScope, lookup, scopeAt } from './scope';
118
118
  import { copySize } from './copy-size';
@@ -1627,7 +1627,9 @@ class Inliner {
1627
1627
  const first = component.statements.at(0);
1628
1628
  const last = component.statements.at(-1);
1629
1629
  if (first && last) {
1630
- statements.push(copy.slice(first.start, last.end));
1630
+ // The last statement can rely on a line break that it loses here.
1631
+ const text = copy.slice(first.start, last.end);
1632
+ statements.push(text.endsWith(';') ? text : `${text};`);
1631
1633
  }
1632
1634
  return {
1633
1635
  element,
@@ -1637,15 +1639,21 @@ class Inliner {
1637
1639
  };
1638
1640
  }
1639
1641
 
1642
+ /** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
1643
+ private addToHost(found: { fn: Node; host: Host }, texts: readonly string[]): void {
1644
+ const key = found.host.kind === 'return' ? found.host.statement : found.fn;
1645
+ const entry = this.hosts.get(key) ?? { host: found.host, texts: [] };
1646
+ entry.texts.push(...texts);
1647
+ this.hosts.set(key, entry);
1648
+ }
1649
+
1640
1650
  /**
1641
1651
  * Writes a prepared copy over its call site.
1642
1652
  */
1643
1653
  private commit(site: PreparedSite): void {
1644
1654
  this.context.s.overwrite(site.element.start, site.element.end, site.root);
1645
1655
  if (site.host) {
1646
- const entry = this.hosts.get(site.host.fn) ?? { host: site.host.host, texts: [] };
1647
- entry.texts.push(...site.statements);
1648
- this.hosts.set(site.host.fn, entry);
1656
+ this.addToHost(site.host, site.statements);
1649
1657
  }
1650
1658
  }
1651
1659
 
@@ -1768,9 +1776,7 @@ class Inliner {
1768
1776
  const name = this.freshName(
1769
1777
  `${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
1770
1778
  );
1771
- const entry = this.hosts.get(host.fn) ?? { host: host.host, texts: [] };
1772
- entry.texts.push(`const ${name} = ${getText(this.context, provider.value)};`);
1773
- this.hosts.set(host.fn, entry);
1779
+ this.addToHost(host, [`const ${name} = ${getText(this.context, provider.value)};`]);
1774
1780
  this.context.s.overwrite(provider.value.start, provider.value.end, name);
1775
1781
  this.transparent.set(element, { provider, text: name });
1776
1782
  this.changed = true;
@@ -2013,17 +2019,7 @@ class Inliner {
2013
2019
 
2014
2020
  private splicedPiece(container: JSXExpressionContainer, value: PropValue): Piece {
2015
2021
  if (value.kind === 'children') {
2016
- const first = value.children.at(0);
2017
- const last = value.children.at(-1);
2018
- if (!first || !last) {
2019
- return replacedPiece(container, '');
2020
- }
2021
- return {
2022
- node: container,
2023
- text: this.context.s.slice(first.start, last.end),
2024
- startText: first.type === 'JSXText' ? first : undefined,
2025
- endText: last.type === 'JSXText' ? last : undefined,
2026
- };
2022
+ return splicePiece(this.context.s, container, value.children);
2027
2023
  }
2028
2024
  return replacedPiece(container, value.text);
2029
2025
  }
package/src/jsx.ts CHANGED
@@ -7,8 +7,7 @@
7
7
  * whenever a replacement would put two texts that were apart next to each
8
8
  * other, an empty `{}` keeps them apart.
9
9
  */
10
- import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, JSXText, Node } from 'oxc-parser';
11
- import { isFilteredText } from './ast';
10
+ import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, Node } from 'oxc-parser';
12
11
  import type MagicString from 'magic-string';
13
12
 
14
13
  /**
@@ -19,14 +18,14 @@ export interface Piece {
19
18
  readonly node: JSXChild;
20
19
  /** The replacement text, or `undefined` to keep the child as written. */
21
20
  text: string | undefined;
22
- /** The JSX text the replacement starts with. */
23
- startText: JSXText | undefined;
24
- /** The JSX text the replacement ends with. */
25
- endText: JSXText | undefined;
21
+ /** The JSX text the piece starts with, or `undefined` when it starts with a tag or a container. */
22
+ startText: string | undefined;
23
+ /** The JSX text the piece ends with. */
24
+ endText: string | undefined;
26
25
  }
27
26
 
28
27
  export function keptPiece(node: JSXChild): Piece {
29
- const text = node.type === 'JSXText' ? node : undefined;
28
+ const text = node.type === 'JSXText' ? node.value : undefined;
30
29
  return { node, text: undefined, startText: text, endText: text };
31
30
  }
32
31
 
@@ -39,27 +38,43 @@ export function splicePiece(s: MagicString, node: JSXChild, children: readonly J
39
38
  if (!first || !last) {
40
39
  return replacedPiece(node, '');
41
40
  }
41
+ return replacedPiece(node, s.slice(first.start, last.end));
42
+ }
43
+
44
+ /**
45
+ * A piece that writes `text`, JSX children as source, in place of `node`.
46
+ *
47
+ * The texts it starts and ends with are read from `text` itself, not from
48
+ * the children it came from. A fold inside them in the same round can have
49
+ * turned a child that was an element into text, such as an inner `<Show>`.
50
+ * JSX text holds no `<`, `>`, `{`, or `}`, so the first and last of those
51
+ * mark where the text ends.
52
+ */
53
+ export function replacedPiece(node: JSXChild, text: string): Piece {
54
+ const start = text.search(/[<{]/);
55
+ const end = Math.max(text.lastIndexOf('>'), text.lastIndexOf('}'));
56
+ const startText = start === -1 ? text : text.slice(0, start);
57
+ const endText = text.slice(end + 1);
42
58
  return {
43
59
  node,
44
- text: s.slice(first.start, last.end),
45
- startText: first.type === 'JSXText' ? first : undefined,
46
- endText: last.type === 'JSXText' ? last : undefined,
60
+ text,
61
+ startText: startText === '' ? undefined : startText,
62
+ endText: endText === '' ? undefined : endText,
47
63
  };
48
64
  }
49
65
 
50
- export function replacedPiece(node: JSXChild, text: string): Piece {
51
- return { node, text, startText: undefined, endText: undefined };
66
+ /** Whether JSX drops this text: whitespace that contains a line break. */
67
+ function isDroppedText(text: string): boolean {
68
+ return /^\s*$/.test(text) && /[\r\n]/.test(text);
52
69
  }
53
70
 
54
71
  /**
55
72
  * Whether two texts render differently as one text node than apart.
56
73
  * Two texts that JSX drops, like line breaks and indentation, still drop together.
57
74
  */
58
- function mergeChangesText(before: JSXText | undefined, after: JSXText | undefined): boolean {
75
+ function mergeChangesText(before: string | undefined, after: string | undefined): boolean {
59
76
  return (
60
- before !== undefined &&
61
- after !== undefined &&
62
- !(isFilteredText(before) && isFilteredText(after))
77
+ before !== undefined && after !== undefined && !(isDroppedText(before) && isDroppedText(after))
63
78
  );
64
79
  }
65
80
 
package/src/memo.ts CHANGED
@@ -87,6 +87,7 @@ import type {
87
87
  Function as FunctionNode,
88
88
  JSXExpressionContainer,
89
89
  Node,
90
+ ObjectExpression,
90
91
  VariableDeclarator,
91
92
  } from 'oxc-parser';
92
93
  import { isFunctionNode, isIntrinsicTag, isPrimary, unwrap } from './ast';
@@ -107,34 +108,107 @@ const TRACKED_PROPS = new Map([
107
108
  ['Repeat', 'count'],
108
109
  ]);
109
110
 
111
+ /**
112
+ * Global constructors whose instances are never thenable or async iterable.
113
+ */
114
+ const SYNC_CONSTRUCTORS = new Set([
115
+ 'Array',
116
+ 'Date',
117
+ 'Error',
118
+ 'Map',
119
+ 'RegExp',
120
+ 'Set',
121
+ 'URL',
122
+ 'URLSearchParams',
123
+ 'WeakMap',
124
+ 'WeakSet',
125
+ ]);
126
+
127
+ /**
128
+ * Whether an object literal can have a `then` method or `Symbol.asyncIterator`,
129
+ * its own or from a `__proto__` it sets.
130
+ */
131
+ function mayBeThenable(node: ObjectExpression): boolean {
132
+ for (const property of node.properties) {
133
+ if (property.type === 'SpreadElement' || property.computed) {
134
+ return true;
135
+ }
136
+ const { key } = property;
137
+ let name: unknown;
138
+ if (key.type === 'Identifier') {
139
+ name = key.name;
140
+ } else if ('value' in key) {
141
+ name = key.value;
142
+ }
143
+ if (name === 'then' || name === '__proto__') {
144
+ return true;
145
+ }
146
+ }
147
+ return false;
148
+ }
149
+
110
150
  /**
111
151
  * Whether evaluating `node` always creates a new value, which reference
112
- * equality never finds equal to the last one.
152
+ * equality never finds equal to the last one. The value is never a promise
153
+ * or an async iterable, which a memo waits for and an inlined read does not.
154
+ * `isGlobal` tells whether a name is one the module does not declare.
113
155
  */
114
- function isFresh(node: Node): boolean {
156
+ function isFresh(node: Node, isGlobal: (node: Node) => boolean): boolean {
115
157
  switch (node.type) {
116
- case 'ObjectExpression':
117
158
  case 'ArrayExpression':
118
159
  case 'ArrowFunctionExpression':
119
160
  case 'FunctionExpression':
120
- case 'ClassExpression':
121
- case 'NewExpression':
122
161
  case 'JSXElement':
123
162
  case 'JSXFragment':
124
163
  return true;
164
+ case 'ObjectExpression':
165
+ return !mayBeThenable(node);
166
+ case 'NewExpression':
167
+ return (
168
+ node.callee.type === 'Identifier' &&
169
+ SYNC_CONSTRUCTORS.has(node.callee.name) &&
170
+ isGlobal(node.callee)
171
+ );
125
172
  case 'ParenthesizedExpression':
126
- return isFresh(node.expression);
173
+ return isFresh(node.expression, isGlobal);
127
174
  case 'ConditionalExpression':
128
- return isFresh(node.consequent) && isFresh(node.alternate);
175
+ return isFresh(node.consequent, isGlobal) && isFresh(node.alternate, isGlobal);
129
176
  case 'SequenceExpression': {
130
177
  const last = node.expressions.at(-1);
131
- return last !== undefined && isFresh(last);
178
+ return last !== undefined && isFresh(last, isGlobal);
132
179
  }
133
180
  default:
134
181
  return false;
135
182
  }
136
183
  }
137
184
 
185
+ /**
186
+ * Whether `node` evaluates to a value that is never a promise or an async
187
+ * iterable. A memo waits for one of those, and an inlined read does not.
188
+ */
189
+ function isSync(node: Node, isGlobal: (node: Node) => boolean): boolean {
190
+ switch (node.type) {
191
+ case 'Literal':
192
+ case 'TemplateLiteral':
193
+ case 'UnaryExpression':
194
+ case 'UpdateExpression':
195
+ case 'BinaryExpression':
196
+ return true;
197
+ case 'ParenthesizedExpression':
198
+ return isSync(node.expression, isGlobal);
199
+ case 'ConditionalExpression':
200
+ return isSync(node.consequent, isGlobal) && isSync(node.alternate, isGlobal);
201
+ case 'LogicalExpression':
202
+ return isSync(node.left, isGlobal) && isSync(node.right, isGlobal);
203
+ case 'SequenceExpression': {
204
+ const last = node.expressions.at(-1);
205
+ return last !== undefined && isSync(last, isGlobal);
206
+ }
207
+ default:
208
+ return isFresh(node, isGlobal);
209
+ }
210
+ }
211
+
138
212
  /**
139
213
  * The expression a compute function returns, when that is all it does.
140
214
  */
@@ -497,8 +571,16 @@ class MemoInliner {
497
571
  if (constant !== undefined) {
498
572
  return this.inlineConstant(declarator, binding, constant);
499
573
  }
574
+ const isGlobal = (node: Node): boolean =>
575
+ this.context.scopes.references.get(node) === undefined;
500
576
  // A memo whose result can equal its last one stops updates, which is what it is for.
501
- if (!alwaysNotifies && !isFresh(expression)) {
577
+ if (!alwaysNotifies && !isFresh(expression, isGlobal)) {
578
+ return undefined;
579
+ }
580
+ // A memo waits for a promise or an async iterable. The insert an inlined
581
+ // read becomes takes the value as it is.
582
+ if (alwaysNotifies && !isSync(expression, isGlobal)) {
583
+ this.reason = 'memo-async';
502
584
  return undefined;
503
585
  }
504
586
  }