solid-optimizer 1.0.0-next.6 → 1.0.0-next.8
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/README.md +4 -2
- package/dist/{compile-DnfP-Quf.mjs → compile-O1Z60VsW.mjs} +528 -68
- package/dist/compile-O1Z60VsW.mjs.map +1 -0
- package/dist/compile-X_SMDFr2.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +59 -32
- package/dist/vite.mjs.map +1 -1
- package/package.json +5 -2
- package/src/compile.ts +41 -0
- package/src/constants.ts +22 -0
- package/src/fold.ts +15 -2
- package/src/inline.ts +180 -30
- package/src/jsx.ts +31 -16
- package/src/memo.ts +91 -9
- package/src/nesting.ts +325 -0
- package/src/terminate.ts +66 -0
- package/src/vite/index.ts +83 -47
- package/dist/compile-DnfP-Quf.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solid-optimizer",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.8",
|
|
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,10 +10,12 @@ 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';
|
|
16
17
|
import { analyzeScopes } from './scope';
|
|
18
|
+
import { terminateStatements } from './terminate';
|
|
17
19
|
import type { Primitive } from './value';
|
|
18
20
|
|
|
19
21
|
export interface CompileOptions {
|
|
@@ -287,6 +289,21 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
287
289
|
}
|
|
288
290
|
return parsed;
|
|
289
291
|
};
|
|
292
|
+
const terminated = runPass(
|
|
293
|
+
parsedCurrent(),
|
|
294
|
+
filename,
|
|
295
|
+
resolved,
|
|
296
|
+
sourceMap,
|
|
297
|
+
terminateStatements,
|
|
298
|
+
undefined,
|
|
299
|
+
);
|
|
300
|
+
if (terminated) {
|
|
301
|
+
current = terminated.code;
|
|
302
|
+
if (terminated.map !== undefined) {
|
|
303
|
+
maps.push(terminated.map);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const start = current;
|
|
290
307
|
const maxPasses = options.maxPasses ?? 10;
|
|
291
308
|
let skipped: Skip[] | undefined;
|
|
292
309
|
for (let round = 0; round < maxPasses; round += 1) {
|
|
@@ -331,7 +348,31 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
331
348
|
}
|
|
332
349
|
}
|
|
333
350
|
|
|
351
|
+
// A pass can put an element straight inside a parent that cannot hold it,
|
|
352
|
+
// which Solid's JSX transform rejects. Only a changed module can have one
|
|
353
|
+
// that its source did not.
|
|
354
|
+
if (current !== start) {
|
|
355
|
+
const result = runPass(
|
|
356
|
+
parsedCurrent(),
|
|
357
|
+
filename,
|
|
358
|
+
resolved,
|
|
359
|
+
sourceMap,
|
|
360
|
+
repairNesting,
|
|
361
|
+
undefined,
|
|
362
|
+
);
|
|
363
|
+
if (result) {
|
|
364
|
+
current = result.code;
|
|
365
|
+
if (result.map !== undefined) {
|
|
366
|
+
maps.push(result.map);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
334
371
|
const report = skipped ? { skipped } : {};
|
|
372
|
+
// Semicolons alone are no reason to change a module.
|
|
373
|
+
if (current === start) {
|
|
374
|
+
return { code, map: null, ...report };
|
|
375
|
+
}
|
|
335
376
|
if (maps.length === 0) {
|
|
336
377
|
return { code: current, map: null, ...report };
|
|
337
378
|
}
|
package/src/constants.ts
CHANGED
|
@@ -202,6 +202,28 @@ export function namedImportSources(
|
|
|
202
202
|
return [...sources];
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
+
/**
|
|
206
|
+
* The modules a module still imports once its types are stripped: the
|
|
207
|
+
* sources of `namedImportSources`, and imports for side effects only, like
|
|
208
|
+
* `import './setup'`.
|
|
209
|
+
*/
|
|
210
|
+
export function runtimeImportSources(
|
|
211
|
+
program: Program,
|
|
212
|
+
scopes: ScopeAnalysis = analyzeScopes(program),
|
|
213
|
+
): string[] {
|
|
214
|
+
const sources = new Set(namedImportSources(program, scopes));
|
|
215
|
+
for (const statement of program.body) {
|
|
216
|
+
if (
|
|
217
|
+
statement.type === 'ImportDeclaration' &&
|
|
218
|
+
statement.importKind !== 'type' &&
|
|
219
|
+
statement.specifiers.length === 0
|
|
220
|
+
) {
|
|
221
|
+
sources.add(statement.source.value);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return [...sources];
|
|
225
|
+
}
|
|
226
|
+
|
|
205
227
|
/**
|
|
206
228
|
* Whether the module reads one of these import bindings as a value. Scope
|
|
207
229
|
* analysis skips type positions, so a binding used only in types has no
|
package/src/fold.ts
CHANGED
|
@@ -753,14 +753,14 @@ class Folder {
|
|
|
753
753
|
};
|
|
754
754
|
case 'JSXElement':
|
|
755
755
|
case 'JSXFragment':
|
|
756
|
-
return
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
|
@@ -2378,22 +2374,155 @@ class Inliner {
|
|
|
2378
2374
|
: undefined;
|
|
2379
2375
|
}
|
|
2380
2376
|
|
|
2377
|
+
/**
|
|
2378
|
+
* The parts of a compound component like Kobalte's
|
|
2379
|
+
* `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`,
|
|
2380
|
+
* or `undefined` when the binding is not one. `<Dialog>` is `<DialogRoot>`,
|
|
2381
|
+
* since `Object.assign` returns its first argument, and `<Dialog.Trigger>`
|
|
2382
|
+
* is `<DialogTrigger>` while nothing writes to a member of either.
|
|
2383
|
+
*/
|
|
2384
|
+
private compoundTarget(
|
|
2385
|
+
binding: Binding,
|
|
2386
|
+
): { root: Binding; parts: ReadonlyMap<string, Binding> } | undefined {
|
|
2387
|
+
const { declaration } = binding;
|
|
2388
|
+
if (
|
|
2389
|
+
!isCapitalized(binding.name) ||
|
|
2390
|
+
!isConstantDeclaration(binding, this.context.options) ||
|
|
2391
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
2392
|
+
declaration.id !== binding.identifier ||
|
|
2393
|
+
!declaration.init
|
|
2394
|
+
) {
|
|
2395
|
+
return undefined;
|
|
2396
|
+
}
|
|
2397
|
+
const init = unwrap(declaration.init);
|
|
2398
|
+
if (
|
|
2399
|
+
init.type !== 'CallExpression' ||
|
|
2400
|
+
init.optional ||
|
|
2401
|
+
init.arguments.length !== 2 ||
|
|
2402
|
+
!this.isObjectAssign(init.callee)
|
|
2403
|
+
) {
|
|
2404
|
+
return undefined;
|
|
2405
|
+
}
|
|
2406
|
+
const [first, second] = init.arguments;
|
|
2407
|
+
if (first.type !== 'Identifier' || second.type !== 'ObjectExpression') {
|
|
2408
|
+
return undefined;
|
|
2409
|
+
}
|
|
2410
|
+
const root = this.topLevelComponent(first);
|
|
2411
|
+
if (!root || root === binding || this.hasMemberWrite(binding) || this.hasMemberWrite(root)) {
|
|
2412
|
+
return undefined;
|
|
2413
|
+
}
|
|
2414
|
+
const parts = new Map<string, Binding>();
|
|
2415
|
+
for (const property of second.properties) {
|
|
2416
|
+
if (
|
|
2417
|
+
property.type !== 'Property' ||
|
|
2418
|
+
property.kind !== 'init' ||
|
|
2419
|
+
property.computed ||
|
|
2420
|
+
property.method ||
|
|
2421
|
+
property.value.type !== 'Identifier'
|
|
2422
|
+
) {
|
|
2423
|
+
return undefined;
|
|
2424
|
+
}
|
|
2425
|
+
const key = propertyKeyName(property.key);
|
|
2426
|
+
if (key === undefined) {
|
|
2427
|
+
return undefined;
|
|
2428
|
+
}
|
|
2429
|
+
// Only parts with a capitalized name can be tags. The others stay reads.
|
|
2430
|
+
const part = this.topLevelComponent(property.value);
|
|
2431
|
+
if (part) {
|
|
2432
|
+
parts.set(key, part);
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
return { root, parts };
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
/** Whether `callee` is the global `Object.assign`. */
|
|
2439
|
+
private isObjectAssign(callee: Node): boolean {
|
|
2440
|
+
return (
|
|
2441
|
+
callee.type === 'MemberExpression' &&
|
|
2442
|
+
!callee.computed &&
|
|
2443
|
+
!callee.optional &&
|
|
2444
|
+
callee.object.type === 'Identifier' &&
|
|
2445
|
+
callee.object.name === 'Object' &&
|
|
2446
|
+
!this.context.scopes.references.get(callee.object) &&
|
|
2447
|
+
callee.property.type === 'Identifier' &&
|
|
2448
|
+
callee.property.name === 'assign'
|
|
2449
|
+
);
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
/** The top-level binding with a capitalized name that `reference` reads, when nothing reassigns it. */
|
|
2453
|
+
private topLevelComponent(reference: Node): Binding | undefined {
|
|
2454
|
+
const target = this.context.scopes.references.get(reference);
|
|
2455
|
+
return target &&
|
|
2456
|
+
target.scope.parent === undefined &&
|
|
2457
|
+
!target.mutated &&
|
|
2458
|
+
isCapitalized(target.name)
|
|
2459
|
+
? target
|
|
2460
|
+
: undefined;
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
/** Whether anything assigns to, updates, or deletes a member of the binding. */
|
|
2464
|
+
private hasMemberWrite(binding: Binding): boolean {
|
|
2465
|
+
for (const reference of binding.references) {
|
|
2466
|
+
const member = this.context.parents.get(reference);
|
|
2467
|
+
if (member?.type !== 'MemberExpression' || member.object !== reference) {
|
|
2468
|
+
continue;
|
|
2469
|
+
}
|
|
2470
|
+
const use = this.context.parents.get(member);
|
|
2471
|
+
if (
|
|
2472
|
+
(use?.type === 'AssignmentExpression' && use.left === member) ||
|
|
2473
|
+
use?.type === 'UpdateExpression' ||
|
|
2474
|
+
(use?.type === 'UnaryExpression' && use.operator === 'delete')
|
|
2475
|
+
) {
|
|
2476
|
+
return true;
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
return false;
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
/** Whether the code reads a member of the binding, like `DialogRoot.Trigger`. */
|
|
2483
|
+
private hasMemberRead(binding: Binding): boolean {
|
|
2484
|
+
for (const reference of binding.references) {
|
|
2485
|
+
const member = this.context.parents.get(reference);
|
|
2486
|
+
if (
|
|
2487
|
+
(member?.type === 'MemberExpression' || member?.type === 'JSXMemberExpression') &&
|
|
2488
|
+
member.object === reference
|
|
2489
|
+
) {
|
|
2490
|
+
return true;
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
return false;
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2381
2496
|
/**
|
|
2382
2497
|
* The statement of an alias nothing uses that `reference` is the value
|
|
2383
2498
|
* of, like `const Separator = SeparatorRoot` once its tags are renamed.
|
|
2384
|
-
* It does not keep the component, and goes away with it.
|
|
2499
|
+
* It does not keep the component, and goes away with it. A compound
|
|
2500
|
+
* component counts too, when its tags are renamed and nothing reads the
|
|
2501
|
+
* members `Object.assign` gave its root.
|
|
2385
2502
|
*/
|
|
2386
2503
|
private unusedAlias(reference: Node): Node | undefined {
|
|
2387
|
-
|
|
2504
|
+
let declarator = this.context.parents.get(reference);
|
|
2505
|
+
if (declarator?.type === 'Property') {
|
|
2506
|
+
declarator = this.context.parents.get(declarator);
|
|
2507
|
+
}
|
|
2508
|
+
if (declarator?.type === 'ObjectExpression' || declarator?.type === 'CallExpression') {
|
|
2509
|
+
const call =
|
|
2510
|
+
declarator.type === 'CallExpression' ? declarator : this.context.parents.get(declarator);
|
|
2511
|
+
declarator = call ? this.context.parents.get(call) : undefined;
|
|
2512
|
+
}
|
|
2388
2513
|
if (declarator?.type !== 'VariableDeclarator' || declarator.id.type !== 'Identifier') {
|
|
2389
2514
|
return undefined;
|
|
2390
2515
|
}
|
|
2391
2516
|
const alias = this.context.scopes.root.bindings.get(declarator.id.name);
|
|
2517
|
+
if (alias?.declaration !== declarator || alias.references.length > 0) {
|
|
2518
|
+
return undefined;
|
|
2519
|
+
}
|
|
2520
|
+
const compound = this.compoundTarget(alias);
|
|
2521
|
+
if (this.aliasTarget(alias) === undefined && (!compound || this.hasMemberRead(compound.root))) {
|
|
2522
|
+
return undefined;
|
|
2523
|
+
}
|
|
2392
2524
|
const statement = this.context.parents.get(declarator);
|
|
2393
|
-
return
|
|
2394
|
-
this.aliasTarget(alias) !== undefined &&
|
|
2395
|
-
alias.references.length === 0 &&
|
|
2396
|
-
statement?.type === 'VariableDeclaration' &&
|
|
2525
|
+
return statement?.type === 'VariableDeclaration' &&
|
|
2397
2526
|
statement.declarations.length === 1 &&
|
|
2398
2527
|
this.context.parents.get(statement)?.type === 'Program'
|
|
2399
2528
|
? statement
|
|
@@ -2408,23 +2537,36 @@ class Inliner {
|
|
|
2408
2537
|
private renameAliasTags(): boolean {
|
|
2409
2538
|
let renamed = false;
|
|
2410
2539
|
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
2411
|
-
const
|
|
2540
|
+
const compound = this.compoundTarget(binding);
|
|
2541
|
+
const target = this.aliasTarget(binding) ?? compound?.root;
|
|
2412
2542
|
if (!target) {
|
|
2413
2543
|
continue;
|
|
2414
2544
|
}
|
|
2415
2545
|
for (const reference of binding.references) {
|
|
2416
2546
|
const parent = this.context.parents.get(reference);
|
|
2547
|
+
// `<Dialog.Trigger>` is the part `Object.assign` gave the root.
|
|
2548
|
+
let tag: Node = reference;
|
|
2549
|
+
let renamedTo = target;
|
|
2550
|
+
if (parent?.type === 'JSXMemberExpression' && parent.object === reference) {
|
|
2551
|
+
const part = compound?.parts.get(parent.property.name);
|
|
2552
|
+
if (!part) {
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
tag = parent;
|
|
2556
|
+
renamedTo = part;
|
|
2557
|
+
}
|
|
2558
|
+
const element = this.context.parents.get(tag);
|
|
2417
2559
|
if (
|
|
2418
|
-
(
|
|
2419
|
-
|
|
2560
|
+
(element?.type !== 'JSXOpeningElement' && element?.type !== 'JSXClosingElement') ||
|
|
2561
|
+
element.name !== tag
|
|
2420
2562
|
) {
|
|
2421
2563
|
continue;
|
|
2422
2564
|
}
|
|
2423
2565
|
const scope = scopeAt(this.context.scopes, this.context.parents, reference);
|
|
2424
|
-
if (lookup(scope,
|
|
2566
|
+
if (lookup(scope, renamedTo.name) !== renamedTo) {
|
|
2425
2567
|
continue;
|
|
2426
2568
|
}
|
|
2427
|
-
this.context.s.overwrite(
|
|
2569
|
+
this.context.s.overwrite(tag.start, tag.end, renamedTo.name);
|
|
2428
2570
|
renamed = true;
|
|
2429
2571
|
}
|
|
2430
2572
|
}
|
|
@@ -2790,3 +2932,11 @@ function hasChild(children: readonly Node[], child: Node): boolean {
|
|
|
2790
2932
|
}
|
|
2791
2933
|
return false;
|
|
2792
2934
|
}
|
|
2935
|
+
|
|
2936
|
+
/** The name a non-computed object key spells, like `Trigger` or `'Trigger'`. */
|
|
2937
|
+
function propertyKeyName(key: Node): string | undefined {
|
|
2938
|
+
if (key.type === 'Identifier') {
|
|
2939
|
+
return key.name;
|
|
2940
|
+
}
|
|
2941
|
+
return key.type === 'Literal' && typeof key.value === 'string' ? key.value : undefined;
|
|
2942
|
+
}
|
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,
|
|
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
|
|
23
|
-
startText:
|
|
24
|
-
/** The JSX text the
|
|
25
|
-
endText:
|
|
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
|
|
45
|
-
startText:
|
|
46
|
-
endText:
|
|
60
|
+
text,
|
|
61
|
+
startText: startText === '' ? undefined : startText,
|
|
62
|
+
endText: endText === '' ? undefined : endText,
|
|
47
63
|
};
|
|
48
64
|
}
|
|
49
65
|
|
|
50
|
-
|
|
51
|
-
|
|
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:
|
|
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
|
}
|