solid-optimizer 1.0.0-next.1 → 1.0.0-next.10
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 +27 -13
- package/dist/{compile-BpoiBa6b.mjs → compile-Cvjy2e11.mjs} +1910 -387
- package/dist/compile-Cvjy2e11.mjs.map +1 -0
- package/dist/{compile-BRCj4UN7.d.mts → compile-ioIkBKzR.d.mts} +15 -2
- package/dist/compile-ioIkBKzR.d.mts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +12 -2
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +401 -116
- package/dist/vite.mjs.map +1 -1
- package/package.json +5 -2
- package/src/ast.ts +4 -3
- package/src/compile.ts +93 -14
- package/src/conditional.ts +547 -0
- package/src/constants.ts +31 -5
- package/src/context.ts +5 -3
- package/src/copy-size.ts +68 -27
- package/src/fold.ts +334 -107
- package/src/inline.ts +832 -177
- package/src/jsx.ts +55 -34
- package/src/memo.ts +309 -38
- package/src/nesting.ts +325 -0
- package/src/provider.ts +62 -40
- package/src/scope.ts +115 -9
- package/src/size.ts +6 -1
- package/src/terminate.ts +66 -0
- package/src/text.ts +297 -0
- package/src/value.ts +32 -15
- package/src/vite/globals.ts +26 -11
- package/src/vite/index.ts +419 -117
- package/src/vite/inject.ts +91 -16
- package/src/vite/report.ts +21 -12
- package/src/vite/runtime.ts +32 -6
- package/src/vite/usage.ts +89 -48
- package/dist/compile-BRCj4UN7.d.mts.map +0 -1
- package/dist/compile-BpoiBa6b.mjs.map +0 -1
package/src/inline.ts
CHANGED
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
* A component inlines when its shape keeps the result equivalent:
|
|
9
9
|
*
|
|
10
10
|
* - It is declared once at the top level of the module, with a capitalized
|
|
11
|
-
* name, as a function declaration or a `const` bound to a function.
|
|
11
|
+
* name, as a function declaration or a `const` bound to a function. A
|
|
12
|
+
* tag of an alias, like `const Separator = SeparatorRoot`, is renamed to
|
|
13
|
+
* the component first. See `renameAliasTags`.
|
|
12
14
|
* - It takes no parameter, or one identifier that is only read as `props.name`,
|
|
13
15
|
* or spread onto an element as `{...props}`.
|
|
14
16
|
* - Its body is statements followed by one final `return` of JSX, or an arrow
|
|
@@ -69,8 +71,8 @@
|
|
|
69
71
|
* instead of while it is created. The order of `onSettled` and effect
|
|
70
72
|
* registration across siblings can change.
|
|
71
73
|
*/
|
|
72
|
-
import MagicString from 'magic-string';
|
|
73
74
|
import type {
|
|
75
|
+
ArrayExpressionElement,
|
|
74
76
|
ArrowFunctionExpression,
|
|
75
77
|
CallExpression,
|
|
76
78
|
Expression,
|
|
@@ -83,8 +85,10 @@ import type {
|
|
|
83
85
|
JSXSpreadAttribute,
|
|
84
86
|
MemberExpression,
|
|
85
87
|
Node,
|
|
88
|
+
ObjectPropertyKind,
|
|
86
89
|
ReturnStatement,
|
|
87
90
|
Statement,
|
|
91
|
+
VariableDeclarator,
|
|
88
92
|
} from 'oxc-parser';
|
|
89
93
|
import {
|
|
90
94
|
forEachChild,
|
|
@@ -96,7 +100,7 @@ import {
|
|
|
96
100
|
unwrap,
|
|
97
101
|
} from './ast';
|
|
98
102
|
import type { PassContext, ResolvedOptions } from './context';
|
|
99
|
-
import { isConstantDeclaration, recordSkip, solidExport
|
|
103
|
+
import { getText, isConstantDeclaration, recordSkip, solidExport } from './context';
|
|
100
104
|
import type { Piece } from './jsx';
|
|
101
105
|
import type { ClosedOptions, Provider } from './provider';
|
|
102
106
|
import {
|
|
@@ -107,12 +111,14 @@ import {
|
|
|
107
111
|
solidCallee,
|
|
108
112
|
stableValueText,
|
|
109
113
|
} from './provider';
|
|
110
|
-
import { keptPiece, replacedPiece, writePieces } from './jsx';
|
|
114
|
+
import { keptPiece, replacedPiece, splicePiece, writePieces } from './jsx';
|
|
111
115
|
import type { Binding, Scope } from './scope';
|
|
112
116
|
import { isWithinScope, lookup, scopeAt } from './scope';
|
|
113
117
|
import { copySize } from './copy-size';
|
|
114
118
|
import { SizeEstimator } from './size';
|
|
115
119
|
import { evaluate, literalText } from './value';
|
|
120
|
+
import type { Content, Editor, Text } from './text';
|
|
121
|
+
import { getCode, join, joinText } from './text';
|
|
116
122
|
|
|
117
123
|
type ComponentFunction = FunctionNode | ArrowFunctionExpression;
|
|
118
124
|
|
|
@@ -132,7 +138,7 @@ interface PropRead extends ReadCount {
|
|
|
132
138
|
*/
|
|
133
139
|
interface ViewSource {
|
|
134
140
|
/** The default of each key, as literal text, or `undefined` for the props. */
|
|
135
|
-
readonly defaults: ReadonlyMap<string,
|
|
141
|
+
readonly defaults: ReadonlyMap<string, Text> | undefined;
|
|
136
142
|
readonly hidden: ReadonlySet<string>;
|
|
137
143
|
}
|
|
138
144
|
|
|
@@ -148,14 +154,14 @@ interface ViewRead extends PropRead {
|
|
|
148
154
|
interface ResolvedSpread {
|
|
149
155
|
readonly spread: ViewSpread;
|
|
150
156
|
/** Each key and its default text, or `undefined` when the props hold it. */
|
|
151
|
-
readonly keys: ReadonlyMap<string,
|
|
157
|
+
readonly keys: ReadonlyMap<string, Content | undefined>;
|
|
152
158
|
}
|
|
153
159
|
|
|
154
160
|
interface ResolvedViews {
|
|
155
161
|
/** Reads of the props, including view reads the props answer. */
|
|
156
162
|
readonly propReads: readonly PropRead[];
|
|
157
163
|
/** View reads a default or nothing answers, and their text. */
|
|
158
|
-
readonly texts: ReadonlyMap<Node,
|
|
164
|
+
readonly texts: ReadonlyMap<Node, Content>;
|
|
159
165
|
readonly spreads: readonly ResolvedSpread[];
|
|
160
166
|
}
|
|
161
167
|
|
|
@@ -202,8 +208,8 @@ interface PreparedSite {
|
|
|
202
208
|
readonly element: JSXElement;
|
|
203
209
|
/** Where the statements go, when there are any. */
|
|
204
210
|
readonly host: { fn: Node; host: Host } | undefined;
|
|
205
|
-
readonly statements:
|
|
206
|
-
readonly root:
|
|
211
|
+
readonly statements: Content[];
|
|
212
|
+
readonly root: Text;
|
|
207
213
|
}
|
|
208
214
|
|
|
209
215
|
function isWithin(node: Node, container: Node): boolean {
|
|
@@ -220,14 +226,12 @@ function inBodyOrder(sites: readonly CallSite[]): CallSite[] {
|
|
|
220
226
|
const pending = [...sites];
|
|
221
227
|
const ordered: CallSite[] = [];
|
|
222
228
|
while (pending.length > 0) {
|
|
223
|
-
const ready =
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
),
|
|
230
|
-
);
|
|
229
|
+
const ready: CallSite[] = [];
|
|
230
|
+
for (const site of pending) {
|
|
231
|
+
if (!hasPendingDependency(pending, site)) {
|
|
232
|
+
ready.push(site);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
231
235
|
// A cycle cannot happen, since a component that renders itself is not
|
|
232
236
|
// inlined, but falling back to the original order keeps this finite.
|
|
233
237
|
const next = ready.length > 0 ? ready : pending;
|
|
@@ -251,19 +255,27 @@ type Host =
|
|
|
251
255
|
*/
|
|
252
256
|
type PropValue =
|
|
253
257
|
/** Replaced with an expression at every read. */
|
|
254
|
-
| { readonly kind: 'expression'; readonly text:
|
|
258
|
+
| { readonly kind: 'expression'; readonly text: Content; readonly primary: boolean }
|
|
255
259
|
/** JSX, spliced in place when read as a JSX child. */
|
|
256
|
-
| { readonly kind: 'jsx'; readonly text:
|
|
260
|
+
| { readonly kind: 'jsx'; readonly text: Content; readonly primary: boolean }
|
|
257
261
|
/** JSX children, spliced in place when read as a JSX child. */
|
|
258
262
|
| {
|
|
259
263
|
readonly kind: 'children';
|
|
260
264
|
readonly children: readonly JSXChild[];
|
|
261
|
-
readonly text:
|
|
265
|
+
readonly text: Content;
|
|
262
266
|
readonly primary: boolean;
|
|
263
267
|
};
|
|
264
268
|
|
|
265
269
|
const UNDEFINED: PropValue = { kind: 'expression', text: 'void 0', primary: false };
|
|
266
270
|
|
|
271
|
+
/** The function a copy is wrapped in when it is measured. See `copy-size.ts`. */
|
|
272
|
+
const COPY_WRAPPER = 'function SoSize$$(';
|
|
273
|
+
|
|
274
|
+
/** Whether code has a JSX tag named `name`. */
|
|
275
|
+
function hasTag(code: string, name: string): boolean {
|
|
276
|
+
return new RegExp(`<${name.replaceAll('$', String.raw`\$`)}[\\s/>]`).test(code);
|
|
277
|
+
}
|
|
278
|
+
|
|
267
279
|
function isCapitalized(name: string): boolean {
|
|
268
280
|
return /^[A-Z]/.test(name);
|
|
269
281
|
}
|
|
@@ -333,6 +345,50 @@ function containsReturn(node: Node): boolean {
|
|
|
333
345
|
return found;
|
|
334
346
|
}
|
|
335
347
|
|
|
348
|
+
/**
|
|
349
|
+
* Collects the nodes an assignment target writes through: an identifier, or
|
|
350
|
+
* the root of a member chain, like `cache` in `cache.items[0] = x`. A root
|
|
351
|
+
* that is not a name, like a call, is collected as it is.
|
|
352
|
+
*/
|
|
353
|
+
function collectWriteRoots(target: Node, roots: Node[]): void {
|
|
354
|
+
switch (target.type) {
|
|
355
|
+
case 'MemberExpression':
|
|
356
|
+
collectWriteRoots(target.object, roots);
|
|
357
|
+
break;
|
|
358
|
+
case 'ParenthesizedExpression':
|
|
359
|
+
case 'TSAsExpression':
|
|
360
|
+
case 'TSSatisfiesExpression':
|
|
361
|
+
case 'TSNonNullExpression':
|
|
362
|
+
case 'TSTypeAssertion':
|
|
363
|
+
collectWriteRoots(target.expression, roots);
|
|
364
|
+
break;
|
|
365
|
+
case 'ObjectPattern':
|
|
366
|
+
for (const property of target.properties) {
|
|
367
|
+
collectWriteRoots(
|
|
368
|
+
property.type === 'RestElement' ? property.argument : property.value,
|
|
369
|
+
roots,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
break;
|
|
373
|
+
case 'ArrayPattern':
|
|
374
|
+
for (const element of target.elements) {
|
|
375
|
+
if (element) {
|
|
376
|
+
collectWriteRoots(element, roots);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
break;
|
|
380
|
+
case 'AssignmentPattern':
|
|
381
|
+
collectWriteRoots(target.left, roots);
|
|
382
|
+
break;
|
|
383
|
+
case 'RestElement':
|
|
384
|
+
collectWriteRoots(target.argument, roots);
|
|
385
|
+
break;
|
|
386
|
+
default:
|
|
387
|
+
roots.push(target);
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
336
392
|
/**
|
|
337
393
|
* Whether Solid would pass an attribute expression as a getter. The JSX
|
|
338
394
|
* transform wraps any expression that reads a member or calls a function.
|
|
@@ -398,7 +454,10 @@ function acceptsAnyExpression(context: PassContext, node: Node): boolean {
|
|
|
398
454
|
/**
|
|
399
455
|
* The function a top-level binding declares, if it declares one.
|
|
400
456
|
*/
|
|
401
|
-
function
|
|
457
|
+
function findComponentFunction(
|
|
458
|
+
binding: Binding,
|
|
459
|
+
options: ResolvedOptions,
|
|
460
|
+
): ComponentFunction | undefined {
|
|
402
461
|
const { declaration } = binding;
|
|
403
462
|
if (binding.kind === 'function' && declaration.type === 'FunctionDeclaration') {
|
|
404
463
|
return declaration;
|
|
@@ -420,7 +479,10 @@ function functionOf(binding: Binding, options: ResolvedOptions): ComponentFuncti
|
|
|
420
479
|
/**
|
|
421
480
|
* The attributes of a call site by name, or `undefined` when one cannot be inlined.
|
|
422
481
|
*/
|
|
423
|
-
function
|
|
482
|
+
function getAttributes(
|
|
483
|
+
context: PassContext,
|
|
484
|
+
element: JSXElement,
|
|
485
|
+
): Map<string, JSXAttribute> | undefined {
|
|
424
486
|
const attributes = new Map<string, JSXAttribute>();
|
|
425
487
|
for (const attribute of element.openingElement.attributes) {
|
|
426
488
|
// A spread can supply any prop, and a namespaced attribute is something
|
|
@@ -428,7 +490,7 @@ function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefine
|
|
|
428
490
|
if (
|
|
429
491
|
attribute.type !== 'JSXAttribute' ||
|
|
430
492
|
attribute.name.type !== 'JSXIdentifier' ||
|
|
431
|
-
(attribute.name.name === 'ref' && !isForwardableRef(attribute)) ||
|
|
493
|
+
(attribute.name.name === 'ref' && !isForwardableRef(context, attribute)) ||
|
|
432
494
|
attributes.has(attribute.name.name)
|
|
433
495
|
) {
|
|
434
496
|
return undefined;
|
|
@@ -450,12 +512,7 @@ function isDataDeclaration(statement: Node): boolean {
|
|
|
450
512
|
declaration?.type === 'VariableDeclaration' &&
|
|
451
513
|
!declaration.declare &&
|
|
452
514
|
(declaration.kind === 'const' || declaration.kind === 'var') &&
|
|
453
|
-
declaration.declarations
|
|
454
|
-
(declarator) =>
|
|
455
|
-
declarator.id.type === 'Identifier' &&
|
|
456
|
-
declarator.init !== null &&
|
|
457
|
-
isDataLiteral(declarator.init),
|
|
458
|
-
)
|
|
515
|
+
areDataDeclarators(declaration.declarations)
|
|
459
516
|
);
|
|
460
517
|
}
|
|
461
518
|
|
|
@@ -473,18 +530,9 @@ function isDataLiteral(node: Expression): boolean {
|
|
|
473
530
|
case 'TSSatisfiesExpression':
|
|
474
531
|
return isDataLiteral(node.expression);
|
|
475
532
|
case 'ArrayExpression':
|
|
476
|
-
return node.elements
|
|
477
|
-
(element) => element !== null && element.type !== 'SpreadElement' && isDataLiteral(element),
|
|
478
|
-
);
|
|
533
|
+
return areDataElements(node.elements);
|
|
479
534
|
case 'ObjectExpression':
|
|
480
|
-
return node.properties
|
|
481
|
-
(property) =>
|
|
482
|
-
property.type === 'Property' &&
|
|
483
|
-
property.kind === 'init' &&
|
|
484
|
-
!property.method &&
|
|
485
|
-
!property.computed &&
|
|
486
|
-
isDataLiteral(property.value),
|
|
487
|
-
);
|
|
535
|
+
return areDataProperties(node.properties);
|
|
488
536
|
default:
|
|
489
537
|
return false;
|
|
490
538
|
}
|
|
@@ -500,17 +548,39 @@ function isMemberRead(read: ReadCount): read is PropRead {
|
|
|
500
548
|
* assigned the element, so it has to stay the name itself. Anything else
|
|
501
549
|
* would be evaluated when the element is made, outside the `untrack` the
|
|
502
550
|
* component's `ref` runs it in.
|
|
551
|
+
*
|
|
552
|
+
* Inside an array, `void 0` and `untrack(() => target)` count too. Solid
|
|
553
|
+
* skips an empty entry of a ref array, and evaluates the whole `ref`
|
|
554
|
+
* untracked. They come from a component that forwards `props.ref` in an
|
|
555
|
+
* array, once a call site that passes no `ref` is inlined.
|
|
503
556
|
*/
|
|
504
|
-
function isForwardableRef(attribute: JSXAttribute): boolean {
|
|
557
|
+
function isForwardableRef(context: PassContext, attribute: JSXAttribute): boolean {
|
|
505
558
|
const { value } = attribute;
|
|
506
559
|
if (value?.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression') {
|
|
507
560
|
return false;
|
|
508
561
|
}
|
|
509
|
-
const isTarget = (node: Expression): boolean => {
|
|
562
|
+
const isTarget = (node: Expression, inArray: boolean): boolean => {
|
|
510
563
|
const inner = unwrap(node);
|
|
511
564
|
if (inner.type === 'ArrayExpression') {
|
|
512
|
-
|
|
513
|
-
(element
|
|
565
|
+
for (const element of inner.elements) {
|
|
566
|
+
if (element === null || element.type === 'SpreadElement' || !isTarget(element, true)) {
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
572
|
+
if (inArray && isVoidZero(inner)) {
|
|
573
|
+
return true;
|
|
574
|
+
}
|
|
575
|
+
if (inArray && inner.type === 'CallExpression' && solidCallee(context, inner) === 'untrack') {
|
|
576
|
+
const callback = inner.arguments.at(0);
|
|
577
|
+
return (
|
|
578
|
+
inner.arguments.length === 1 &&
|
|
579
|
+
callback?.type === 'ArrowFunctionExpression' &&
|
|
580
|
+
callback.expression &&
|
|
581
|
+
callback.params.length === 0 &&
|
|
582
|
+
callback.body.type !== 'BlockStatement' &&
|
|
583
|
+
isTarget(callback.body, true)
|
|
514
584
|
);
|
|
515
585
|
}
|
|
516
586
|
return (
|
|
@@ -519,11 +589,21 @@ function isForwardableRef(attribute: JSXAttribute): boolean {
|
|
|
519
589
|
inner.type === 'FunctionExpression'
|
|
520
590
|
);
|
|
521
591
|
};
|
|
522
|
-
return isTarget(value.expression);
|
|
592
|
+
return isTarget(value.expression, false);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Whether an expression is `void 0`. */
|
|
596
|
+
function isVoidZero(node: Expression): boolean {
|
|
597
|
+
return (
|
|
598
|
+
node.type === 'UnaryExpression' &&
|
|
599
|
+
node.operator === 'void' &&
|
|
600
|
+
node.argument.type === 'Literal' &&
|
|
601
|
+
node.argument.value === 0
|
|
602
|
+
);
|
|
523
603
|
}
|
|
524
604
|
|
|
525
605
|
/**
|
|
526
|
-
* Why `
|
|
606
|
+
* Why `getAttributes` turns down a call site, for the report.
|
|
527
607
|
*/
|
|
528
608
|
function callSiteReason(element: JSXElement): string {
|
|
529
609
|
const names = new Set<string>();
|
|
@@ -578,14 +658,14 @@ function isAssignmentTarget(context: PassContext, node: Node): boolean {
|
|
|
578
658
|
class Inliner {
|
|
579
659
|
changed = false;
|
|
580
660
|
|
|
581
|
-
private readonly hosts = new Map<Node, { host: Host; texts:
|
|
661
|
+
private readonly hosts = new Map<Node, { host: Host; texts: Content[] }>();
|
|
582
662
|
|
|
583
663
|
/**
|
|
584
664
|
* Providers whose subtree is closed, so statements can move out through
|
|
585
665
|
* them. Each maps to the text its reads become, or `undefined` when a
|
|
586
666
|
* binding that never changes is read at each site.
|
|
587
667
|
*/
|
|
588
|
-
private readonly transparent = new Map<JSXElement, { provider: Provider; text?:
|
|
668
|
+
private readonly transparent = new Map<JSXElement, { provider: Provider; text?: Content }>();
|
|
589
669
|
|
|
590
670
|
constructor(private readonly context: PassContext) {}
|
|
591
671
|
|
|
@@ -631,7 +711,7 @@ class Inliner {
|
|
|
631
711
|
// Components
|
|
632
712
|
// ---------------------------------------------------------------------------
|
|
633
713
|
|
|
634
|
-
private
|
|
714
|
+
private getScope(fn: ComponentFunction): Scope | undefined {
|
|
635
715
|
return this.context.scopes.scopes.get(fn);
|
|
636
716
|
}
|
|
637
717
|
|
|
@@ -655,6 +735,38 @@ class Inliner {
|
|
|
655
735
|
return undefined;
|
|
656
736
|
}
|
|
657
737
|
|
|
738
|
+
/**
|
|
739
|
+
* Whether statements assign, update, or delete through a binding declared
|
|
740
|
+
* outside `scope`, or through a global. Writes inside nested functions
|
|
741
|
+
* count too, since a function can run right away, like a memo's.
|
|
742
|
+
*/
|
|
743
|
+
private writesOutside(statements: readonly Statement[], scope: Scope): boolean {
|
|
744
|
+
const roots: Node[] = [];
|
|
745
|
+
const visit = (node: Node): void => {
|
|
746
|
+
if (node.type === 'AssignmentExpression') {
|
|
747
|
+
collectWriteRoots(node.left, roots);
|
|
748
|
+
} else if (node.type === 'UpdateExpression') {
|
|
749
|
+
collectWriteRoots(node.argument, roots);
|
|
750
|
+
} else if (node.type === 'UnaryExpression' && node.operator === 'delete') {
|
|
751
|
+
collectWriteRoots(node.argument, roots);
|
|
752
|
+
}
|
|
753
|
+
forEachChild(node, visit);
|
|
754
|
+
};
|
|
755
|
+
for (const statement of statements) {
|
|
756
|
+
visit(statement);
|
|
757
|
+
}
|
|
758
|
+
for (const root of roots) {
|
|
759
|
+
if (root.type !== 'Identifier') {
|
|
760
|
+
return true;
|
|
761
|
+
}
|
|
762
|
+
const target = this.context.scopes.references.get(root);
|
|
763
|
+
if (!target || !isWithinScope(target.scope, scope)) {
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
|
|
658
770
|
private analyzeComponent(binding: Binding): Component | undefined {
|
|
659
771
|
if (!isCapitalized(binding.name)) {
|
|
660
772
|
return undefined;
|
|
@@ -663,7 +775,7 @@ class Inliner {
|
|
|
663
775
|
this.reason = 'component-mutated';
|
|
664
776
|
return undefined;
|
|
665
777
|
}
|
|
666
|
-
const fn =
|
|
778
|
+
const fn = findComponentFunction(binding, this.context.options);
|
|
667
779
|
if (!fn?.body) {
|
|
668
780
|
this.reason = this.isLazy(binding) ? 'lazy' : 'not-a-function';
|
|
669
781
|
return undefined;
|
|
@@ -676,7 +788,7 @@ class Inliner {
|
|
|
676
788
|
this.reason = 'component-params';
|
|
677
789
|
return undefined;
|
|
678
790
|
}
|
|
679
|
-
const scope = this.
|
|
791
|
+
const scope = this.getScope(fn);
|
|
680
792
|
if (!scope) {
|
|
681
793
|
this.reason = 'not-a-function';
|
|
682
794
|
return undefined;
|
|
@@ -721,6 +833,12 @@ class Inliner {
|
|
|
721
833
|
this.reason = 'component-this';
|
|
722
834
|
return undefined;
|
|
723
835
|
}
|
|
836
|
+
// A copy's statements run before any JSX of the function they move into,
|
|
837
|
+
// so JSX there could read a value they write later than it should.
|
|
838
|
+
if (this.writesOutside(statements, scope)) {
|
|
839
|
+
this.reason = 'component-write';
|
|
840
|
+
return undefined;
|
|
841
|
+
}
|
|
724
842
|
|
|
725
843
|
// The parameter is only read as `props.name`.
|
|
726
844
|
const param = fn.params.at(0);
|
|
@@ -753,7 +871,13 @@ class Inliner {
|
|
|
753
871
|
}
|
|
754
872
|
props.push(read);
|
|
755
873
|
}
|
|
756
|
-
|
|
874
|
+
const kept: typeof statements = [];
|
|
875
|
+
for (const statement of statements) {
|
|
876
|
+
if (!views.declarations.includes(statement)) {
|
|
877
|
+
kept.push(statement);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
statements = kept;
|
|
757
881
|
|
|
758
882
|
const locals: Binding[] = [];
|
|
759
883
|
const seen = new Set<Scope>();
|
|
@@ -888,11 +1012,13 @@ class Inliner {
|
|
|
888
1012
|
if (init.type !== 'CallExpression' || (callee !== 'merge' && callee !== 'omit')) {
|
|
889
1013
|
continue;
|
|
890
1014
|
}
|
|
891
|
-
const view = this.
|
|
1015
|
+
const view = this.resolveView(callee, init, param, views, args);
|
|
892
1016
|
if (view === null) {
|
|
893
1017
|
continue;
|
|
894
1018
|
}
|
|
895
|
-
|
|
1019
|
+
// A body with its own scope (a parameter has a default) holds the declaration.
|
|
1020
|
+
const body = (fn.body && this.context.scopes.scopes.get(fn.body)) ?? scope;
|
|
1021
|
+
const binding = body.bindings.get(declarator.id.name);
|
|
896
1022
|
if (!view || !binding || binding.mutated) {
|
|
897
1023
|
return undefined;
|
|
898
1024
|
}
|
|
@@ -960,20 +1086,20 @@ class Inliner {
|
|
|
960
1086
|
* take the props or another view, and `undefined` when it does but its
|
|
961
1087
|
* other arguments are not literal.
|
|
962
1088
|
*/
|
|
963
|
-
private
|
|
1089
|
+
private resolveView(
|
|
964
1090
|
callee: 'merge' | 'omit',
|
|
965
1091
|
call: CallExpression,
|
|
966
1092
|
param: Binding | undefined,
|
|
967
1093
|
views: ReadonlyMap<Binding, View>,
|
|
968
1094
|
args: Set<Node>,
|
|
969
1095
|
): View | null | undefined {
|
|
970
|
-
const
|
|
1096
|
+
const resolveSources = (argument: Node): readonly ViewSource[] | undefined => {
|
|
971
1097
|
if (argument.type === 'CallExpression') {
|
|
972
1098
|
const nested = solidCallee(this.context, argument);
|
|
973
1099
|
if (nested !== 'merge' && nested !== 'omit') {
|
|
974
1100
|
return undefined;
|
|
975
1101
|
}
|
|
976
|
-
return this.
|
|
1102
|
+
return this.resolveView(nested, argument, param, views, args)?.sources;
|
|
977
1103
|
}
|
|
978
1104
|
if (argument.type !== 'Identifier') {
|
|
979
1105
|
return undefined;
|
|
@@ -984,9 +1110,13 @@ class Inliner {
|
|
|
984
1110
|
}
|
|
985
1111
|
return binding ? views.get(binding)?.sources : undefined;
|
|
986
1112
|
};
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1113
|
+
let involved = false;
|
|
1114
|
+
for (const argument of call.arguments) {
|
|
1115
|
+
if (argument.type !== 'SpreadElement' && resolveSources(unwrap(argument)) !== undefined) {
|
|
1116
|
+
involved = true;
|
|
1117
|
+
break;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
990
1120
|
if (!involved) {
|
|
991
1121
|
return null;
|
|
992
1122
|
}
|
|
@@ -998,7 +1128,7 @@ class Inliner {
|
|
|
998
1128
|
return undefined;
|
|
999
1129
|
}
|
|
1000
1130
|
const target = unwrap(first);
|
|
1001
|
-
const base =
|
|
1131
|
+
const base = resolveSources(target);
|
|
1002
1132
|
const hidden: string[] = [];
|
|
1003
1133
|
for (const key of keys) {
|
|
1004
1134
|
if (key.type !== 'Literal' || typeof key.value !== 'string') {
|
|
@@ -1010,12 +1140,14 @@ class Inliner {
|
|
|
1010
1140
|
return undefined;
|
|
1011
1141
|
}
|
|
1012
1142
|
args.add(target);
|
|
1013
|
-
|
|
1014
|
-
|
|
1143
|
+
const hiddenSources: ViewSource[] = [];
|
|
1144
|
+
for (const source of base) {
|
|
1145
|
+
hiddenSources.push({
|
|
1015
1146
|
defaults: source.defaults,
|
|
1016
1147
|
hidden: new Set([...source.hidden, ...hidden]),
|
|
1017
|
-
})
|
|
1018
|
-
}
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
return { sources: hiddenSources };
|
|
1019
1151
|
}
|
|
1020
1152
|
|
|
1021
1153
|
const sources: ViewSource[] = [];
|
|
@@ -1024,7 +1156,7 @@ class Inliner {
|
|
|
1024
1156
|
return undefined;
|
|
1025
1157
|
}
|
|
1026
1158
|
const inner = unwrap(argument);
|
|
1027
|
-
const existing =
|
|
1159
|
+
const existing = resolveSources(inner);
|
|
1028
1160
|
if (existing) {
|
|
1029
1161
|
args.add(inner);
|
|
1030
1162
|
sources.push(...existing);
|
|
@@ -1033,7 +1165,7 @@ class Inliner {
|
|
|
1033
1165
|
if (inner.type !== 'ObjectExpression') {
|
|
1034
1166
|
return undefined;
|
|
1035
1167
|
}
|
|
1036
|
-
const defaults = new Map<string,
|
|
1168
|
+
const defaults = new Map<string, Text>();
|
|
1037
1169
|
for (const property of inner.properties) {
|
|
1038
1170
|
if (property.type !== 'Property' || property.kind !== 'init' || property.computed) {
|
|
1039
1171
|
return undefined;
|
|
@@ -1051,7 +1183,7 @@ class Inliner {
|
|
|
1051
1183
|
if (text === undefined || isReservedPropName(key)) {
|
|
1052
1184
|
return undefined;
|
|
1053
1185
|
}
|
|
1054
|
-
defaults.set(key, text);
|
|
1186
|
+
defaults.set(key, this.context.s.written(text, property.value.start));
|
|
1055
1187
|
}
|
|
1056
1188
|
sources.push({ defaults, hidden: new Set() });
|
|
1057
1189
|
}
|
|
@@ -1119,7 +1251,7 @@ class Inliner {
|
|
|
1119
1251
|
if (element?.type !== 'JSXElement') {
|
|
1120
1252
|
return false;
|
|
1121
1253
|
}
|
|
1122
|
-
const significant = element.children
|
|
1254
|
+
const significant = getSignificantChildren(element.children);
|
|
1123
1255
|
if (significant.length !== 1 || significant.at(0) !== parent) {
|
|
1124
1256
|
return false;
|
|
1125
1257
|
}
|
|
@@ -1154,7 +1286,7 @@ class Inliner {
|
|
|
1154
1286
|
* returned JSX of a component, with only intrinsic elements, fragments,
|
|
1155
1287
|
* and closed providers between it and the call.
|
|
1156
1288
|
*/
|
|
1157
|
-
private
|
|
1289
|
+
private findHost(element: JSXElement): { fn: Node; host: Host } | undefined {
|
|
1158
1290
|
let node: Node = element;
|
|
1159
1291
|
for (;;) {
|
|
1160
1292
|
const parent = this.context.parents.get(node);
|
|
@@ -1171,7 +1303,7 @@ class Inliner {
|
|
|
1171
1303
|
return undefined;
|
|
1172
1304
|
}
|
|
1173
1305
|
const child = node;
|
|
1174
|
-
if (!parent.children
|
|
1306
|
+
if (!hasChild(parent.children, child)) {
|
|
1175
1307
|
return undefined;
|
|
1176
1308
|
}
|
|
1177
1309
|
} else if (parent.type === 'ReturnStatement') {
|
|
@@ -1203,21 +1335,21 @@ class Inliner {
|
|
|
1203
1335
|
element: JSXElement,
|
|
1204
1336
|
reads: readonly ReadCount[],
|
|
1205
1337
|
fresh: (base: string) => string,
|
|
1206
|
-
snapshots:
|
|
1338
|
+
snapshots: Content[],
|
|
1207
1339
|
): PropValue | undefined {
|
|
1208
1340
|
const { children } = element;
|
|
1209
|
-
if (children
|
|
1341
|
+
if (hasSpreadChild(children)) {
|
|
1210
1342
|
return undefined;
|
|
1211
1343
|
}
|
|
1212
|
-
const significant = children
|
|
1344
|
+
const significant = getSignificantChildren(children);
|
|
1213
1345
|
const only = significant.at(0);
|
|
1214
1346
|
if (only === undefined) {
|
|
1215
1347
|
return { kind: 'children', children: [], text: 'void 0', primary: false };
|
|
1216
1348
|
}
|
|
1217
|
-
let text:
|
|
1349
|
+
let text: Content;
|
|
1218
1350
|
let primary = true;
|
|
1219
1351
|
if (significant.length > 1 || only.type === 'JSXText') {
|
|
1220
|
-
text = `<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
1352
|
+
text = joinText`<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
1221
1353
|
} else if (only.type === 'JSXExpressionContainer') {
|
|
1222
1354
|
if (only.expression.type === 'JSXEmptyExpression') {
|
|
1223
1355
|
return undefined;
|
|
@@ -1225,13 +1357,16 @@ class Inliner {
|
|
|
1225
1357
|
// A single expression is passed like an attribute value, such as a
|
|
1226
1358
|
// render function that has to stay one function.
|
|
1227
1359
|
const value = this.expressionValue(only.expression, 'children', reads, fresh, snapshots);
|
|
1228
|
-
if (
|
|
1360
|
+
if (
|
|
1361
|
+
value.kind !== 'expression' ||
|
|
1362
|
+
getCode(value.text) !== getText(this.context, only.expression).code
|
|
1363
|
+
) {
|
|
1229
1364
|
return value;
|
|
1230
1365
|
}
|
|
1231
1366
|
text = value.text;
|
|
1232
1367
|
primary = value.primary;
|
|
1233
1368
|
} else {
|
|
1234
|
-
text =
|
|
1369
|
+
text = getText(this.context, only);
|
|
1235
1370
|
}
|
|
1236
1371
|
return { kind: 'children', children, text, primary };
|
|
1237
1372
|
}
|
|
@@ -1270,7 +1405,7 @@ class Inliner {
|
|
|
1270
1405
|
return undefined;
|
|
1271
1406
|
}
|
|
1272
1407
|
}
|
|
1273
|
-
return { kind: 'expression', text:
|
|
1408
|
+
return { kind: 'expression', text: getText(this.context, value.expression), primary: true };
|
|
1274
1409
|
}
|
|
1275
1410
|
|
|
1276
1411
|
/**
|
|
@@ -1281,7 +1416,7 @@ class Inliner {
|
|
|
1281
1416
|
attribute: JSXAttribute | undefined,
|
|
1282
1417
|
reads: readonly ReadCount[],
|
|
1283
1418
|
fresh: (base: string) => string,
|
|
1284
|
-
snapshots:
|
|
1419
|
+
snapshots: Content[],
|
|
1285
1420
|
): PropValue | undefined {
|
|
1286
1421
|
if (!attribute) {
|
|
1287
1422
|
return UNDEFINED;
|
|
@@ -1296,10 +1431,14 @@ class Inliner {
|
|
|
1296
1431
|
if (value.value.includes('&')) {
|
|
1297
1432
|
return undefined;
|
|
1298
1433
|
}
|
|
1299
|
-
return {
|
|
1434
|
+
return {
|
|
1435
|
+
kind: 'expression',
|
|
1436
|
+
text: this.context.s.written(JSON.stringify(value.value), value.start),
|
|
1437
|
+
primary: true,
|
|
1438
|
+
};
|
|
1300
1439
|
case 'JSXElement':
|
|
1301
1440
|
case 'JSXFragment':
|
|
1302
|
-
return { kind: 'jsx', text:
|
|
1441
|
+
return { kind: 'jsx', text: getText(this.context, value), primary: true };
|
|
1303
1442
|
default:
|
|
1304
1443
|
break;
|
|
1305
1444
|
}
|
|
@@ -1319,9 +1458,9 @@ class Inliner {
|
|
|
1319
1458
|
name: string,
|
|
1320
1459
|
reads: readonly ReadCount[],
|
|
1321
1460
|
fresh: (base: string) => string,
|
|
1322
|
-
snapshots:
|
|
1461
|
+
snapshots: Content[],
|
|
1323
1462
|
): PropValue {
|
|
1324
|
-
const text =
|
|
1463
|
+
const text = getText(this.context, expression);
|
|
1325
1464
|
const inner = unwrap(expression);
|
|
1326
1465
|
if (inner.type === 'JSXElement' || inner.type === 'JSXFragment') {
|
|
1327
1466
|
return { kind: 'jsx', text, primary: true };
|
|
@@ -1336,7 +1475,7 @@ class Inliner {
|
|
|
1336
1475
|
return { kind: 'expression', text, primary };
|
|
1337
1476
|
}
|
|
1338
1477
|
const local = fresh(name);
|
|
1339
|
-
snapshots.push(`const ${local} = ${text};`);
|
|
1478
|
+
snapshots.push(joinText`const ${local} = ${text};`);
|
|
1340
1479
|
return { kind: 'expression', text: local, primary: true };
|
|
1341
1480
|
}
|
|
1342
1481
|
|
|
@@ -1396,12 +1535,12 @@ class Inliner {
|
|
|
1396
1535
|
* when it cannot be inlined there. Nothing is written until `commit`.
|
|
1397
1536
|
*/
|
|
1398
1537
|
private prepare(component: Component, element: JSXElement): PreparedSite | undefined {
|
|
1399
|
-
const attributes =
|
|
1538
|
+
const attributes = getAttributes(this.context, element);
|
|
1400
1539
|
if (!attributes) {
|
|
1401
1540
|
this.reason = callSiteReason(element);
|
|
1402
1541
|
return undefined;
|
|
1403
1542
|
}
|
|
1404
|
-
const hasChildren = element.children.
|
|
1543
|
+
const hasChildren = getSignificantChildren(element.children).length > 0;
|
|
1405
1544
|
if (hasChildren && attributes.has('children')) {
|
|
1406
1545
|
this.reason = 'call-children';
|
|
1407
1546
|
return undefined;
|
|
@@ -1427,7 +1566,7 @@ class Inliner {
|
|
|
1427
1566
|
}
|
|
1428
1567
|
|
|
1429
1568
|
// What each prop reads as.
|
|
1430
|
-
const snapshots:
|
|
1569
|
+
const snapshots: Content[] = [];
|
|
1431
1570
|
const fresh = (base: string): string => this.freshName(base);
|
|
1432
1571
|
const readsByName = new Map<string, ReadCount[]>();
|
|
1433
1572
|
const count = (name: string, read: ReadCount): void => {
|
|
@@ -1473,7 +1612,7 @@ class Inliner {
|
|
|
1473
1612
|
}
|
|
1474
1613
|
|
|
1475
1614
|
const hoisted = component.statements.length > 0 || snapshots.length > 0;
|
|
1476
|
-
const host = hoisted ? this.
|
|
1615
|
+
const host = hoisted ? this.findHost(element) : undefined;
|
|
1477
1616
|
if (hoisted && !host) {
|
|
1478
1617
|
this.reason = 'no-host';
|
|
1479
1618
|
return undefined;
|
|
@@ -1486,7 +1625,7 @@ class Inliner {
|
|
|
1486
1625
|
}
|
|
1487
1626
|
|
|
1488
1627
|
// Build the copy on the original code, so this pass's other edits stay out of it.
|
|
1489
|
-
const copy =
|
|
1628
|
+
const copy = this.context.s.fresh();
|
|
1490
1629
|
this.renameLocals(copy, component, fresh);
|
|
1491
1630
|
this.substituteProps(copy, views.propReads, values);
|
|
1492
1631
|
this.substituteViews(copy, component, element, views, values);
|
|
@@ -1498,7 +1637,9 @@ class Inliner {
|
|
|
1498
1637
|
const first = component.statements.at(0);
|
|
1499
1638
|
const last = component.statements.at(-1);
|
|
1500
1639
|
if (first && last) {
|
|
1501
|
-
|
|
1640
|
+
// The last statement can rely on a line break that it loses here.
|
|
1641
|
+
const text = copy.slice(first.start, last.end);
|
|
1642
|
+
statements.push(text.code.endsWith(';') ? text : joinText`${text};`);
|
|
1502
1643
|
}
|
|
1503
1644
|
return {
|
|
1504
1645
|
element,
|
|
@@ -1508,15 +1649,21 @@ class Inliner {
|
|
|
1508
1649
|
};
|
|
1509
1650
|
}
|
|
1510
1651
|
|
|
1652
|
+
/** Queues statements for a host. Each `return` gets its own, since their scopes can differ. */
|
|
1653
|
+
private addToHost(found: { fn: Node; host: Host }, texts: readonly Content[]): void {
|
|
1654
|
+
const key = found.host.kind === 'return' ? found.host.statement : found.fn;
|
|
1655
|
+
const entry = this.hosts.get(key) ?? { host: found.host, texts: [] };
|
|
1656
|
+
entry.texts.push(...texts);
|
|
1657
|
+
this.hosts.set(key, entry);
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1511
1660
|
/**
|
|
1512
1661
|
* Writes a prepared copy over its call site.
|
|
1513
1662
|
*/
|
|
1514
1663
|
private commit(site: PreparedSite): void {
|
|
1515
1664
|
this.context.s.overwrite(site.element.start, site.element.end, site.root);
|
|
1516
1665
|
if (site.host) {
|
|
1517
|
-
|
|
1518
|
-
entry.texts.push(...site.statements);
|
|
1519
|
-
this.hosts.set(site.host.fn, entry);
|
|
1666
|
+
this.addToHost(site.host, site.statements);
|
|
1520
1667
|
}
|
|
1521
1668
|
}
|
|
1522
1669
|
|
|
@@ -1525,8 +1672,8 @@ class Inliner {
|
|
|
1525
1672
|
* provider above it answers, with the text each becomes. `undefined` when
|
|
1526
1673
|
* a read cannot take the value there.
|
|
1527
1674
|
*/
|
|
1528
|
-
private contextTexts(component: Component, element: JSXElement): Map<Node,
|
|
1529
|
-
const texts = new Map<Node,
|
|
1675
|
+
private contextTexts(component: Component, element: JSXElement): Map<Node, Content> | undefined {
|
|
1676
|
+
const texts = new Map<Node, Content>();
|
|
1530
1677
|
for (const { provider, text } of this.answeringProviders(element)) {
|
|
1531
1678
|
for (const read of contextReads(this.context, component.fn, provider.context)) {
|
|
1532
1679
|
const value = text ?? stableValueText(this.context, provider, element);
|
|
@@ -1543,8 +1690,8 @@ class Inliner {
|
|
|
1543
1690
|
* The closed providers that answer context reads at `element`. The nearest
|
|
1544
1691
|
* provider of each context answers its reads, closed or not.
|
|
1545
1692
|
*/
|
|
1546
|
-
private answeringProviders(element: JSXElement): { provider: Provider; text?:
|
|
1547
|
-
const answering: { provider: Provider; text?:
|
|
1693
|
+
private answeringProviders(element: JSXElement): { provider: Provider; text?: Content }[] {
|
|
1694
|
+
const answering: { provider: Provider; text?: Content }[] = [];
|
|
1548
1695
|
const seen = new Set<Binding>();
|
|
1549
1696
|
for (
|
|
1550
1697
|
let node = this.context.parents.get(element);
|
|
@@ -1577,7 +1724,10 @@ class Inliner {
|
|
|
1577
1724
|
components: readonly Component[],
|
|
1578
1725
|
sites: readonly CallSite[],
|
|
1579
1726
|
): Component[] {
|
|
1580
|
-
const byBinding = new Map
|
|
1727
|
+
const byBinding = new Map<Binding, Component>();
|
|
1728
|
+
for (const component of components) {
|
|
1729
|
+
byBinding.set(component.binding, component);
|
|
1730
|
+
}
|
|
1581
1731
|
// Whether a subtree under `provider` only runs visible code, where each
|
|
1582
1732
|
// component is one this pass inlines and is visible too.
|
|
1583
1733
|
const closedUnder = (provider: Provider): ((node: Node) => boolean) => {
|
|
@@ -1610,14 +1760,18 @@ class Inliner {
|
|
|
1610
1760
|
const changed: Component[] = [];
|
|
1611
1761
|
for (const provider of findProviders(this.context)) {
|
|
1612
1762
|
const { element } = provider;
|
|
1613
|
-
const inside = sites
|
|
1614
|
-
(site) => isWithin(site.element, element) && site.element !== element,
|
|
1615
|
-
);
|
|
1763
|
+
const inside = hasSiteInside(sites, element);
|
|
1616
1764
|
if (!inside) {
|
|
1617
1765
|
continue;
|
|
1618
1766
|
}
|
|
1619
1767
|
const isClosedNode = closedUnder(provider);
|
|
1620
|
-
|
|
1768
|
+
let closed = true;
|
|
1769
|
+
for (const child of element.children) {
|
|
1770
|
+
if (!isClosedNode(child)) {
|
|
1771
|
+
closed = false;
|
|
1772
|
+
break;
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1621
1775
|
if (!closed) {
|
|
1622
1776
|
continue;
|
|
1623
1777
|
}
|
|
@@ -1625,16 +1779,14 @@ class Inliner {
|
|
|
1625
1779
|
this.transparent.set(element, { provider });
|
|
1626
1780
|
continue;
|
|
1627
1781
|
}
|
|
1628
|
-
const host = this.
|
|
1782
|
+
const host = this.findHost(element);
|
|
1629
1783
|
if (!host) {
|
|
1630
1784
|
continue;
|
|
1631
1785
|
}
|
|
1632
1786
|
const name = this.freshName(
|
|
1633
1787
|
`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`,
|
|
1634
1788
|
);
|
|
1635
|
-
|
|
1636
|
-
entry.texts.push(`const ${name} = ${textOf(this.context, provider.value)};`);
|
|
1637
|
-
this.hosts.set(host.fn, entry);
|
|
1789
|
+
this.addToHost(host, [joinText`const ${name} = ${getText(this.context, provider.value)};`]);
|
|
1638
1790
|
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
1639
1791
|
this.transparent.set(element, { provider, text: name });
|
|
1640
1792
|
this.changed = true;
|
|
@@ -1662,7 +1814,7 @@ class Inliner {
|
|
|
1662
1814
|
const has = (key: string): boolean =>
|
|
1663
1815
|
attributes.has(key) || (key === 'children' && hasChildren);
|
|
1664
1816
|
// The last source that has the key answers, even when its value is `undefined`.
|
|
1665
|
-
const resolve = (view: View, key: string):
|
|
1817
|
+
const resolve = (view: View, key: string): Content | undefined => {
|
|
1666
1818
|
for (let index = view.sources.length - 1; index >= 0; index -= 1) {
|
|
1667
1819
|
const source = view.sources.at(index);
|
|
1668
1820
|
if (!source || source.hidden.has(key)) {
|
|
@@ -1680,7 +1832,7 @@ class Inliner {
|
|
|
1680
1832
|
};
|
|
1681
1833
|
|
|
1682
1834
|
const propReads: PropRead[] = [...component.props];
|
|
1683
|
-
const texts = new Map<Node,
|
|
1835
|
+
const texts = new Map<Node, Content>();
|
|
1684
1836
|
for (const read of component.viewReads) {
|
|
1685
1837
|
const text = resolve(read.view, read.name);
|
|
1686
1838
|
if (text === undefined) {
|
|
@@ -1701,7 +1853,10 @@ class Inliner {
|
|
|
1701
1853
|
const order: string[] = [];
|
|
1702
1854
|
for (const source of spread.view.sources) {
|
|
1703
1855
|
const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
|
|
1704
|
-
for (const key of keys
|
|
1856
|
+
for (const key of keys) {
|
|
1857
|
+
if (source.hidden.has(key)) {
|
|
1858
|
+
continue;
|
|
1859
|
+
}
|
|
1705
1860
|
const index = order.indexOf(key);
|
|
1706
1861
|
if (index !== -1) {
|
|
1707
1862
|
order.splice(index, 1);
|
|
@@ -1717,13 +1872,13 @@ class Inliner {
|
|
|
1717
1872
|
names.add(
|
|
1718
1873
|
attribute.name.type === 'JSXIdentifier'
|
|
1719
1874
|
? attribute.name.name
|
|
1720
|
-
:
|
|
1875
|
+
: getText(this.context, attribute.name).code,
|
|
1721
1876
|
);
|
|
1722
1877
|
}
|
|
1723
1878
|
}
|
|
1724
1879
|
taken.set(spread.element, names);
|
|
1725
1880
|
}
|
|
1726
|
-
const keys = new Map<string,
|
|
1881
|
+
const keys = new Map<string, Content | undefined>();
|
|
1727
1882
|
for (const key of order) {
|
|
1728
1883
|
// Children become the element's children, so it must have none of its own.
|
|
1729
1884
|
const fits =
|
|
@@ -1746,38 +1901,39 @@ class Inliner {
|
|
|
1746
1901
|
* view declarations.
|
|
1747
1902
|
*/
|
|
1748
1903
|
private substituteViews(
|
|
1749
|
-
copy:
|
|
1904
|
+
copy: Editor,
|
|
1750
1905
|
component: Component,
|
|
1751
1906
|
element: JSXElement,
|
|
1752
1907
|
views: ResolvedViews,
|
|
1753
1908
|
values: ReadonlyMap<string, PropValue>,
|
|
1754
1909
|
): void {
|
|
1755
1910
|
for (const [member, text] of views.texts) {
|
|
1756
|
-
const bare = text !== 'void 0' || acceptsAnyExpression(this.context, member);
|
|
1757
|
-
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
1911
|
+
const bare = getCode(text) !== 'void 0' || acceptsAnyExpression(this.context, member);
|
|
1912
|
+
copy.overwrite(member.start, member.end, bare ? text : joinText`(${text})`);
|
|
1758
1913
|
}
|
|
1759
1914
|
for (const { spread, keys } of views.spreads) {
|
|
1760
|
-
const parts:
|
|
1761
|
-
let children:
|
|
1915
|
+
const parts: Text[] = [];
|
|
1916
|
+
let children: Content | undefined;
|
|
1762
1917
|
for (const [key, text] of keys) {
|
|
1763
1918
|
const value = values.get(key);
|
|
1764
1919
|
if (key === 'children') {
|
|
1765
1920
|
children = this.spreadChildren(element, text, value);
|
|
1766
1921
|
} else {
|
|
1767
|
-
parts.push(`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1922
|
+
parts.push(joinText`${key}={${text ?? value?.text ?? 'void 0'}}`);
|
|
1768
1923
|
}
|
|
1769
1924
|
}
|
|
1770
1925
|
const { attribute } = spread;
|
|
1771
1926
|
if (parts.length === 0) {
|
|
1772
1927
|
copy.remove(attribute.start, attribute.end);
|
|
1773
1928
|
} else {
|
|
1774
|
-
copy.overwrite(attribute.start, attribute.end,
|
|
1929
|
+
copy.overwrite(attribute.start, attribute.end, join(parts, ' '));
|
|
1775
1930
|
}
|
|
1776
1931
|
if (children !== undefined) {
|
|
1777
1932
|
const opening = spread.element.openingElement;
|
|
1778
|
-
|
|
1933
|
+
// Read the tag from the copy, which holds the renamed locals.
|
|
1934
|
+
const tag = copy.slice(opening.name.start, opening.name.end);
|
|
1779
1935
|
const close = this.context.code.lastIndexOf('/>', opening.end);
|
|
1780
|
-
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
1936
|
+
copy.overwrite(close, opening.end, joinText`>${children}</${tag}>`);
|
|
1781
1937
|
}
|
|
1782
1938
|
}
|
|
1783
1939
|
for (const declaration of component.viewDeclarations) {
|
|
@@ -1791,11 +1947,11 @@ class Inliner {
|
|
|
1791
1947
|
*/
|
|
1792
1948
|
private spreadChildren(
|
|
1793
1949
|
element: JSXElement,
|
|
1794
|
-
text:
|
|
1950
|
+
text: Content | undefined,
|
|
1795
1951
|
value: PropValue | undefined,
|
|
1796
|
-
):
|
|
1952
|
+
): Content | undefined {
|
|
1797
1953
|
if (text !== undefined) {
|
|
1798
|
-
return text === 'void 0' ? undefined : `{${text}}`;
|
|
1954
|
+
return getCode(text) === 'void 0' ? undefined : joinText`{${text}}`;
|
|
1799
1955
|
}
|
|
1800
1956
|
if (!value) {
|
|
1801
1957
|
return undefined;
|
|
@@ -1803,14 +1959,10 @@ class Inliner {
|
|
|
1803
1959
|
if (value.kind === 'children' && element.closingElement) {
|
|
1804
1960
|
return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
1805
1961
|
}
|
|
1806
|
-
return `{${value.text}}`;
|
|
1962
|
+
return joinText`{${value.text}}`;
|
|
1807
1963
|
}
|
|
1808
1964
|
|
|
1809
|
-
private renameLocals(
|
|
1810
|
-
copy: MagicString,
|
|
1811
|
-
component: Component,
|
|
1812
|
-
fresh: (base: string) => string,
|
|
1813
|
-
): void {
|
|
1965
|
+
private renameLocals(copy: Editor, component: Component, fresh: (base: string) => string): void {
|
|
1814
1966
|
for (const local of component.locals) {
|
|
1815
1967
|
const name = fresh(local.name);
|
|
1816
1968
|
for (const node of [local.identifier, ...local.references]) {
|
|
@@ -1833,7 +1985,7 @@ class Inliner {
|
|
|
1833
1985
|
}
|
|
1834
1986
|
|
|
1835
1987
|
private substituteProps(
|
|
1836
|
-
copy:
|
|
1988
|
+
copy: Editor,
|
|
1837
1989
|
reads: readonly PropRead[],
|
|
1838
1990
|
values: ReadonlyMap<string, PropValue>,
|
|
1839
1991
|
): void {
|
|
@@ -1860,29 +2012,24 @@ class Inliner {
|
|
|
1860
2012
|
continue;
|
|
1861
2013
|
}
|
|
1862
2014
|
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
1863
|
-
copy.overwrite(
|
|
2015
|
+
copy.overwrite(
|
|
2016
|
+
read.member.start,
|
|
2017
|
+
read.member.end,
|
|
2018
|
+
bare ? value.text : joinText`(${value.text})`,
|
|
2019
|
+
);
|
|
1864
2020
|
}
|
|
1865
2021
|
for (const [parent, pieces] of spliced) {
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
2022
|
+
const children: Piece[] = [];
|
|
2023
|
+
for (const child of parent.children) {
|
|
2024
|
+
children.push(pieces.get(child) ?? keptPiece(child));
|
|
2025
|
+
}
|
|
2026
|
+
writePieces(copy, children);
|
|
1870
2027
|
}
|
|
1871
2028
|
}
|
|
1872
2029
|
|
|
1873
2030
|
private splicedPiece(container: JSXExpressionContainer, value: PropValue): Piece {
|
|
1874
2031
|
if (value.kind === 'children') {
|
|
1875
|
-
|
|
1876
|
-
const last = value.children.at(-1);
|
|
1877
|
-
if (!first || !last) {
|
|
1878
|
-
return replacedPiece(container, '');
|
|
1879
|
-
}
|
|
1880
|
-
return {
|
|
1881
|
-
node: container,
|
|
1882
|
-
text: this.context.s.slice(first.start, last.end),
|
|
1883
|
-
startText: first.type === 'JSXText' ? first : undefined,
|
|
1884
|
-
endText: last.type === 'JSXText' ? last : undefined,
|
|
1885
|
-
};
|
|
2032
|
+
return splicePiece(this.context.s, container, value.children);
|
|
1886
2033
|
}
|
|
1887
2034
|
return replacedPiece(container, value.text);
|
|
1888
2035
|
}
|
|
@@ -1910,10 +2057,14 @@ class Inliner {
|
|
|
1910
2057
|
}
|
|
1911
2058
|
}
|
|
1912
2059
|
const result = new Set<Component>();
|
|
1913
|
-
|
|
1914
|
-
|
|
2060
|
+
const { alwaysInline, alwaysInlineComponents } = this.context.options;
|
|
2061
|
+
for (const component of byComponent.keys()) {
|
|
2062
|
+
if (alwaysInline || alwaysInlineComponents.has(component.binding.name)) {
|
|
1915
2063
|
result.add(component);
|
|
2064
|
+
byComponent.delete(component);
|
|
1916
2065
|
}
|
|
2066
|
+
}
|
|
2067
|
+
if (byComponent.size === 0) {
|
|
1917
2068
|
return result;
|
|
1918
2069
|
}
|
|
1919
2070
|
// The copies made here are only measured, so their fresh names are not kept.
|
|
@@ -1921,13 +2072,27 @@ class Inliner {
|
|
|
1921
2072
|
try {
|
|
1922
2073
|
const copiesOf = new Map<Component, Map<JSXElement, PreparedSite | undefined>>();
|
|
1923
2074
|
for (const [component, elements] of byComponent) {
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
2075
|
+
const prepared = new Map<JSXElement, PreparedSite | undefined>();
|
|
2076
|
+
for (const element of elements) {
|
|
2077
|
+
prepared.set(element, this.prepare(component, element));
|
|
2078
|
+
}
|
|
2079
|
+
copiesOf.set(component, prepared);
|
|
2080
|
+
}
|
|
2081
|
+
this.sites = sites;
|
|
2082
|
+
this.otherUses = otherUses;
|
|
2083
|
+
this.blocked = new Map();
|
|
2084
|
+
for (const [inner, byElement] of copiesOf) {
|
|
2085
|
+
for (const [element, copy] of byElement) {
|
|
2086
|
+
const outer = copy ? undefined : findOuterComponent(this.components, inner, element);
|
|
2087
|
+
if (outer) {
|
|
2088
|
+
const list = this.blocked.get(outer) ?? [];
|
|
2089
|
+
list.push(inner);
|
|
2090
|
+
this.blocked.set(outer, list);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
1928
2093
|
}
|
|
1929
2094
|
for (const [component, byElement] of copiesOf) {
|
|
1930
|
-
const copies =
|
|
2095
|
+
const copies = collectPrepared(byElement.values());
|
|
1931
2096
|
if (copies.length === 0) {
|
|
1932
2097
|
continue;
|
|
1933
2098
|
}
|
|
@@ -1940,6 +2105,8 @@ class Inliner {
|
|
|
1940
2105
|
}
|
|
1941
2106
|
} finally {
|
|
1942
2107
|
this.scratchNames = undefined;
|
|
2108
|
+
this.otherUses = undefined;
|
|
2109
|
+
this.blocked = new Map();
|
|
1943
2110
|
}
|
|
1944
2111
|
return result;
|
|
1945
2112
|
}
|
|
@@ -1958,7 +2125,7 @@ class Inliner {
|
|
|
1958
2125
|
if (
|
|
1959
2126
|
component.declaration === undefined ||
|
|
1960
2127
|
otherUses.get(component) !== 0 ||
|
|
1961
|
-
|
|
2128
|
+
hasMissingCopy(copies.values())
|
|
1962
2129
|
) {
|
|
1963
2130
|
return 0;
|
|
1964
2131
|
}
|
|
@@ -1977,7 +2144,10 @@ class Inliner {
|
|
|
1977
2144
|
copiesOf: ReadonlyMap<Component, ReadonlyMap<JSXElement, PreparedSite | undefined>>,
|
|
1978
2145
|
otherUses: ReadonlyMap<Component, number>,
|
|
1979
2146
|
): number {
|
|
1980
|
-
const elements =
|
|
2147
|
+
const elements: JSXElement[] = [];
|
|
2148
|
+
for (const copy of copies) {
|
|
2149
|
+
elements.push(copy.element);
|
|
2150
|
+
}
|
|
1981
2151
|
this.assumedHosts = new Set(elements);
|
|
1982
2152
|
let gain = 0;
|
|
1983
2153
|
try {
|
|
@@ -1988,7 +2158,7 @@ class Inliner {
|
|
|
1988
2158
|
const enabled = new Map(byElement);
|
|
1989
2159
|
let changed = false;
|
|
1990
2160
|
for (const [element, copy] of byElement) {
|
|
1991
|
-
if (!copy &&
|
|
2161
|
+
if (!copy && isWithinAny(element, elements)) {
|
|
1992
2162
|
const next = this.prepare(other, element);
|
|
1993
2163
|
enabled.set(element, next);
|
|
1994
2164
|
changed ||= next !== undefined;
|
|
@@ -1997,8 +2167,8 @@ class Inliner {
|
|
|
1997
2167
|
if (!changed) {
|
|
1998
2168
|
continue;
|
|
1999
2169
|
}
|
|
2000
|
-
const now =
|
|
2001
|
-
const then =
|
|
2170
|
+
const now = collectPrepared(byElement.values());
|
|
2171
|
+
const then = collectPrepared(enabled.values());
|
|
2002
2172
|
const before = now.length > 0 ? Math.max(0, this.inliningGain(other, now, 0)) : 0;
|
|
2003
2173
|
const after = this.inliningGain(other, then, this.removedShare(other, enabled, otherUses));
|
|
2004
2174
|
gain += Math.max(0, after - before);
|
|
@@ -2023,20 +2193,85 @@ class Inliner {
|
|
|
2023
2193
|
let calls =
|
|
2024
2194
|
share > 0 && component.declaration ? share * estimator.node(component.declaration) : 0;
|
|
2025
2195
|
let inlined = 0;
|
|
2196
|
+
const blocked = this.blockedWithin(component);
|
|
2197
|
+
// The blocked components that some copy still calls once measured.
|
|
2198
|
+
const kept = new Set<Component>();
|
|
2026
2199
|
for (const copy of copies) {
|
|
2027
2200
|
const inTemplate = this.sitsInTemplate(copy.element);
|
|
2028
2201
|
calls += estimator.jsx(copy.element, inTemplate);
|
|
2202
|
+
const measured = { code: '' };
|
|
2029
2203
|
inlined += copySize(
|
|
2030
2204
|
this.context.filename,
|
|
2031
2205
|
this.sizeHeader(),
|
|
2032
2206
|
copy,
|
|
2033
2207
|
inTemplate,
|
|
2034
2208
|
this.context.options,
|
|
2209
|
+
blocked.length > 0 ? { declarations: this.createDeclarations(blocked), inline } : undefined,
|
|
2210
|
+
measured,
|
|
2035
2211
|
);
|
|
2212
|
+
const body = measured.code.slice(measured.code.lastIndexOf(COPY_WRAPPER));
|
|
2213
|
+
for (const inner of blocked) {
|
|
2214
|
+
if (hasTag(body, inner.binding.name)) {
|
|
2215
|
+
kept.add(inner);
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
// A blocked component that inlines into every copy goes away with this
|
|
2220
|
+
// one, when nothing else uses it.
|
|
2221
|
+
if (share > 0) {
|
|
2222
|
+
for (const inner of blocked) {
|
|
2223
|
+
if (!kept.has(inner) && inner.declaration && this.onlyUsedWithin(inner, component)) {
|
|
2224
|
+
calls += share * estimator.node(inner.declaration);
|
|
2225
|
+
}
|
|
2226
|
+
}
|
|
2036
2227
|
}
|
|
2037
2228
|
return calls - inlined;
|
|
2038
2229
|
}
|
|
2039
2230
|
|
|
2231
|
+
/**
|
|
2232
|
+
* The components whose calls in a component's body cannot be inlined
|
|
2233
|
+
* there, but may be in a copy, once the call site resolves the props
|
|
2234
|
+
* they are given. Those they call in turn count too.
|
|
2235
|
+
*/
|
|
2236
|
+
private blockedWithin(component: Component): Component[] {
|
|
2237
|
+
const found: Component[] = [];
|
|
2238
|
+
const pending = [component];
|
|
2239
|
+
for (let outer = pending.pop(); outer; outer = pending.pop()) {
|
|
2240
|
+
for (const inner of this.blocked.get(outer) ?? []) {
|
|
2241
|
+
if (inner !== component && !found.includes(inner)) {
|
|
2242
|
+
found.push(inner);
|
|
2243
|
+
pending.push(inner);
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
return found;
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
/** The declarations of components, for a copy measured on its own. */
|
|
2251
|
+
private createDeclarations(components: readonly Component[]): string {
|
|
2252
|
+
const texts: string[] = [];
|
|
2253
|
+
for (const component of components) {
|
|
2254
|
+
const statement = this.topLevelStatement(component.binding.declaration);
|
|
2255
|
+
if (statement) {
|
|
2256
|
+
texts.push(getText(this.context, statement).code);
|
|
2257
|
+
}
|
|
2258
|
+
}
|
|
2259
|
+
return texts.join('\n');
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
/** Whether every call of `inner` is in `outer`, and nothing else uses it. */
|
|
2263
|
+
private onlyUsedWithin(inner: Component, outer: Component): boolean {
|
|
2264
|
+
if (this.otherUses?.get(inner) !== 0) {
|
|
2265
|
+
return false;
|
|
2266
|
+
}
|
|
2267
|
+
for (const site of this.sites) {
|
|
2268
|
+
if (site.component === inner && !isWithin(site.element, outer.fn)) {
|
|
2269
|
+
return false;
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
return true;
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2040
2275
|
/**
|
|
2041
2276
|
* Whether an element sits directly in an intrinsic element, whose
|
|
2042
2277
|
* template its copy joins. A provider this pass makes transparent is
|
|
@@ -2066,6 +2301,37 @@ class Inliner {
|
|
|
2066
2301
|
|
|
2067
2302
|
private header: string | undefined;
|
|
2068
2303
|
|
|
2304
|
+
/** The components of the module this pass can inline. */
|
|
2305
|
+
private readonly components: Component[] = [];
|
|
2306
|
+
|
|
2307
|
+
/** The call sites of those components. */
|
|
2308
|
+
private sites: readonly CallSite[] = [];
|
|
2309
|
+
|
|
2310
|
+
/** How many times each component is used other than as a call, while estimating. */
|
|
2311
|
+
private otherUses: ReadonlyMap<Component, number> | undefined;
|
|
2312
|
+
|
|
2313
|
+
/**
|
|
2314
|
+
* The components called in each component's body where the call cannot
|
|
2315
|
+
* be inlined as it is, while estimating.
|
|
2316
|
+
*/
|
|
2317
|
+
private blocked = new Map<Component, Component[]>();
|
|
2318
|
+
|
|
2319
|
+
/** The top-level statement a node is part of, without an `export`. */
|
|
2320
|
+
private topLevelStatement(node: Node): Node | undefined {
|
|
2321
|
+
let current = node;
|
|
2322
|
+
for (let parent = this.context.parents.get(current); parent;) {
|
|
2323
|
+
if (parent.type === 'Program') {
|
|
2324
|
+
return current.type === 'ExportNamedDeclaration' ||
|
|
2325
|
+
current.type === 'ExportDefaultDeclaration'
|
|
2326
|
+
? (current.declaration ?? undefined)
|
|
2327
|
+
: current;
|
|
2328
|
+
}
|
|
2329
|
+
current = parent;
|
|
2330
|
+
parent = this.context.parents.get(current);
|
|
2331
|
+
}
|
|
2332
|
+
return undefined;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2069
2335
|
/**
|
|
2070
2336
|
* What a copy measured on its own needs to know: the module's imports,
|
|
2071
2337
|
* and the names that alias Solid's exports.
|
|
@@ -2074,11 +2340,12 @@ class Inliner {
|
|
|
2074
2340
|
if (this.header === undefined) {
|
|
2075
2341
|
// The imports, and the top-level constants made of literals, so a copy
|
|
2076
2342
|
// folds with the values its call site passes by name, as it will once inlined.
|
|
2077
|
-
const imports =
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2343
|
+
const imports: string[] = [];
|
|
2344
|
+
for (const statement of this.context.program.body) {
|
|
2345
|
+
if (statement.type === 'ImportDeclaration' || isDataDeclaration(statement)) {
|
|
2346
|
+
imports.push(this.context.code.slice(statement.start, statement.end));
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2082
2349
|
const aliases = [...this.context.options.builtInAliases.keys()];
|
|
2083
2350
|
if (aliases.length > 0) {
|
|
2084
2351
|
imports.push(`var ${aliases.join(', ')};`);
|
|
@@ -2088,8 +2355,241 @@ class Inliner {
|
|
|
2088
2355
|
return this.header;
|
|
2089
2356
|
}
|
|
2090
2357
|
|
|
2358
|
+
/**
|
|
2359
|
+
* The component a top-level alias like `const Button = ButtonRoot` names,
|
|
2360
|
+
* or `undefined` when the binding is not one.
|
|
2361
|
+
*/
|
|
2362
|
+
private aliasTarget(binding: Binding): Binding | undefined {
|
|
2363
|
+
const { declaration } = binding;
|
|
2364
|
+
if (
|
|
2365
|
+
!isCapitalized(binding.name) ||
|
|
2366
|
+
!isConstantDeclaration(binding, this.context.options) ||
|
|
2367
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
2368
|
+
declaration.id !== binding.identifier ||
|
|
2369
|
+
!declaration.init
|
|
2370
|
+
) {
|
|
2371
|
+
return undefined;
|
|
2372
|
+
}
|
|
2373
|
+
const init = unwrap(declaration.init);
|
|
2374
|
+
if (init.type !== 'Identifier') {
|
|
2375
|
+
return undefined;
|
|
2376
|
+
}
|
|
2377
|
+
const target = this.context.scopes.references.get(init);
|
|
2378
|
+
return target &&
|
|
2379
|
+
target !== binding &&
|
|
2380
|
+
target.scope.parent === undefined &&
|
|
2381
|
+
!target.mutated &&
|
|
2382
|
+
isCapitalized(target.name)
|
|
2383
|
+
? target
|
|
2384
|
+
: undefined;
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* The parts of a compound component like Kobalte's
|
|
2389
|
+
* `const Dialog = Object.assign(DialogRoot, { Trigger: DialogTrigger })`,
|
|
2390
|
+
* or `undefined` when the binding is not one. `<Dialog>` is `<DialogRoot>`,
|
|
2391
|
+
* since `Object.assign` returns its first argument, and `<Dialog.Trigger>`
|
|
2392
|
+
* is `<DialogTrigger>` while nothing writes to a member of either.
|
|
2393
|
+
*/
|
|
2394
|
+
private compoundTarget(
|
|
2395
|
+
binding: Binding,
|
|
2396
|
+
): { root: Binding; parts: ReadonlyMap<string, Binding> } | undefined {
|
|
2397
|
+
const { declaration } = binding;
|
|
2398
|
+
if (
|
|
2399
|
+
!isCapitalized(binding.name) ||
|
|
2400
|
+
!isConstantDeclaration(binding, this.context.options) ||
|
|
2401
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
2402
|
+
declaration.id !== binding.identifier ||
|
|
2403
|
+
!declaration.init
|
|
2404
|
+
) {
|
|
2405
|
+
return undefined;
|
|
2406
|
+
}
|
|
2407
|
+
const init = unwrap(declaration.init);
|
|
2408
|
+
if (
|
|
2409
|
+
init.type !== 'CallExpression' ||
|
|
2410
|
+
init.optional ||
|
|
2411
|
+
init.arguments.length !== 2 ||
|
|
2412
|
+
!this.isObjectAssign(init.callee)
|
|
2413
|
+
) {
|
|
2414
|
+
return undefined;
|
|
2415
|
+
}
|
|
2416
|
+
const [first, second] = init.arguments;
|
|
2417
|
+
if (first.type !== 'Identifier' || second.type !== 'ObjectExpression') {
|
|
2418
|
+
return undefined;
|
|
2419
|
+
}
|
|
2420
|
+
const root = this.topLevelComponent(first);
|
|
2421
|
+
if (!root || root === binding || this.hasMemberWrite(binding) || this.hasMemberWrite(root)) {
|
|
2422
|
+
return undefined;
|
|
2423
|
+
}
|
|
2424
|
+
const parts = new Map<string, Binding>();
|
|
2425
|
+
for (const property of second.properties) {
|
|
2426
|
+
if (
|
|
2427
|
+
property.type !== 'Property' ||
|
|
2428
|
+
property.kind !== 'init' ||
|
|
2429
|
+
property.computed ||
|
|
2430
|
+
property.method ||
|
|
2431
|
+
property.value.type !== 'Identifier'
|
|
2432
|
+
) {
|
|
2433
|
+
return undefined;
|
|
2434
|
+
}
|
|
2435
|
+
const key = propertyKeyName(property.key);
|
|
2436
|
+
if (key === undefined) {
|
|
2437
|
+
return undefined;
|
|
2438
|
+
}
|
|
2439
|
+
// Only parts with a capitalized name can be tags. The others stay reads.
|
|
2440
|
+
const part = this.topLevelComponent(property.value);
|
|
2441
|
+
if (part) {
|
|
2442
|
+
parts.set(key, part);
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
return { root, parts };
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
/** Whether `callee` is the global `Object.assign`. */
|
|
2449
|
+
private isObjectAssign(callee: Node): boolean {
|
|
2450
|
+
return (
|
|
2451
|
+
callee.type === 'MemberExpression' &&
|
|
2452
|
+
!callee.computed &&
|
|
2453
|
+
!callee.optional &&
|
|
2454
|
+
callee.object.type === 'Identifier' &&
|
|
2455
|
+
callee.object.name === 'Object' &&
|
|
2456
|
+
!this.context.scopes.references.get(callee.object) &&
|
|
2457
|
+
callee.property.type === 'Identifier' &&
|
|
2458
|
+
callee.property.name === 'assign'
|
|
2459
|
+
);
|
|
2460
|
+
}
|
|
2461
|
+
|
|
2462
|
+
/** The top-level binding with a capitalized name that `reference` reads, when nothing reassigns it. */
|
|
2463
|
+
private topLevelComponent(reference: Node): Binding | undefined {
|
|
2464
|
+
const target = this.context.scopes.references.get(reference);
|
|
2465
|
+
return target &&
|
|
2466
|
+
target.scope.parent === undefined &&
|
|
2467
|
+
!target.mutated &&
|
|
2468
|
+
isCapitalized(target.name)
|
|
2469
|
+
? target
|
|
2470
|
+
: undefined;
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
/** Whether anything assigns to, updates, or deletes a member of the binding. */
|
|
2474
|
+
private hasMemberWrite(binding: Binding): boolean {
|
|
2475
|
+
for (const reference of binding.references) {
|
|
2476
|
+
const member = this.context.parents.get(reference);
|
|
2477
|
+
if (member?.type !== 'MemberExpression' || member.object !== reference) {
|
|
2478
|
+
continue;
|
|
2479
|
+
}
|
|
2480
|
+
const use = this.context.parents.get(member);
|
|
2481
|
+
if (
|
|
2482
|
+
(use?.type === 'AssignmentExpression' && use.left === member) ||
|
|
2483
|
+
use?.type === 'UpdateExpression' ||
|
|
2484
|
+
(use?.type === 'UnaryExpression' && use.operator === 'delete')
|
|
2485
|
+
) {
|
|
2486
|
+
return true;
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
return false;
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
/** Whether the code reads a member of the binding, like `DialogRoot.Trigger`. */
|
|
2493
|
+
private hasMemberRead(binding: Binding): boolean {
|
|
2494
|
+
for (const reference of binding.references) {
|
|
2495
|
+
const member = this.context.parents.get(reference);
|
|
2496
|
+
if (
|
|
2497
|
+
(member?.type === 'MemberExpression' || member?.type === 'JSXMemberExpression') &&
|
|
2498
|
+
member.object === reference
|
|
2499
|
+
) {
|
|
2500
|
+
return true;
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
return false;
|
|
2504
|
+
}
|
|
2505
|
+
|
|
2506
|
+
/**
|
|
2507
|
+
* The statement of an alias nothing uses that `reference` is the value
|
|
2508
|
+
* of, like `const Separator = SeparatorRoot` once its tags are renamed.
|
|
2509
|
+
* It does not keep the component, and goes away with it. A compound
|
|
2510
|
+
* component counts too, when its tags are renamed and nothing reads the
|
|
2511
|
+
* members `Object.assign` gave its root.
|
|
2512
|
+
*/
|
|
2513
|
+
private unusedAlias(reference: Node): Node | undefined {
|
|
2514
|
+
let declarator = this.context.parents.get(reference);
|
|
2515
|
+
if (declarator?.type === 'Property') {
|
|
2516
|
+
declarator = this.context.parents.get(declarator);
|
|
2517
|
+
}
|
|
2518
|
+
if (declarator?.type === 'ObjectExpression' || declarator?.type === 'CallExpression') {
|
|
2519
|
+
const call =
|
|
2520
|
+
declarator.type === 'CallExpression' ? declarator : this.context.parents.get(declarator);
|
|
2521
|
+
declarator = call ? this.context.parents.get(call) : undefined;
|
|
2522
|
+
}
|
|
2523
|
+
if (declarator?.type !== 'VariableDeclarator' || declarator.id.type !== 'Identifier') {
|
|
2524
|
+
return undefined;
|
|
2525
|
+
}
|
|
2526
|
+
const alias = this.context.scopes.root.bindings.get(declarator.id.name);
|
|
2527
|
+
if (alias?.declaration !== declarator || alias.references.length > 0) {
|
|
2528
|
+
return undefined;
|
|
2529
|
+
}
|
|
2530
|
+
const compound = this.compoundTarget(alias);
|
|
2531
|
+
if (this.aliasTarget(alias) === undefined && (!compound || this.hasMemberRead(compound.root))) {
|
|
2532
|
+
return undefined;
|
|
2533
|
+
}
|
|
2534
|
+
const statement = this.context.parents.get(declarator);
|
|
2535
|
+
return statement?.type === 'VariableDeclaration' &&
|
|
2536
|
+
statement.declarations.length === 1 &&
|
|
2537
|
+
this.context.parents.get(statement)?.type === 'Program'
|
|
2538
|
+
? statement
|
|
2539
|
+
: undefined;
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2542
|
+
/**
|
|
2543
|
+
* Renames each tag of an alias to the component it names, where that name
|
|
2544
|
+
* means the same. Libraries often export a component under a second name,
|
|
2545
|
+
* like `const Separator = SeparatorRoot`. The next pass inlines the calls.
|
|
2546
|
+
*/
|
|
2547
|
+
private renameAliasTags(): boolean {
|
|
2548
|
+
let renamed = false;
|
|
2549
|
+
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
2550
|
+
const compound = this.compoundTarget(binding);
|
|
2551
|
+
const target = this.aliasTarget(binding) ?? compound?.root;
|
|
2552
|
+
if (!target) {
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
for (const reference of binding.references) {
|
|
2556
|
+
const parent = this.context.parents.get(reference);
|
|
2557
|
+
// `<Dialog.Trigger>` is the part `Object.assign` gave the root.
|
|
2558
|
+
let tag: Node = reference;
|
|
2559
|
+
let renamedTo = target;
|
|
2560
|
+
if (parent?.type === 'JSXMemberExpression' && parent.object === reference) {
|
|
2561
|
+
const part = compound?.parts.get(parent.property.name);
|
|
2562
|
+
if (!part) {
|
|
2563
|
+
continue;
|
|
2564
|
+
}
|
|
2565
|
+
tag = parent;
|
|
2566
|
+
renamedTo = part;
|
|
2567
|
+
}
|
|
2568
|
+
const element = this.context.parents.get(tag);
|
|
2569
|
+
if (
|
|
2570
|
+
(element?.type !== 'JSXOpeningElement' && element?.type !== 'JSXClosingElement') ||
|
|
2571
|
+
element.name !== tag
|
|
2572
|
+
) {
|
|
2573
|
+
continue;
|
|
2574
|
+
}
|
|
2575
|
+
const scope = scopeAt(this.context.scopes, this.context.parents, reference);
|
|
2576
|
+
if (lookup(scope, renamedTo.name) !== renamedTo) {
|
|
2577
|
+
continue;
|
|
2578
|
+
}
|
|
2579
|
+
this.context.s.overwrite(tag.start, tag.end, renamedTo.name);
|
|
2580
|
+
renamed = true;
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
return renamed;
|
|
2584
|
+
}
|
|
2585
|
+
|
|
2091
2586
|
run(): void {
|
|
2092
|
-
|
|
2587
|
+
// The calls through an alias inline in the next pass, from the renamed tags.
|
|
2588
|
+
if (this.renameAliasTags()) {
|
|
2589
|
+
this.changed = true;
|
|
2590
|
+
return;
|
|
2591
|
+
}
|
|
2592
|
+
const { components } = this;
|
|
2093
2593
|
const rejected = new Map<Binding, string>();
|
|
2094
2594
|
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
2095
2595
|
this.reason = 'not-a-function';
|
|
@@ -2109,6 +2609,8 @@ class Inliner {
|
|
|
2109
2609
|
|
|
2110
2610
|
const sites: CallSite[] = [];
|
|
2111
2611
|
const otherUses = new Map<Component, number>();
|
|
2612
|
+
// The aliases nothing uses, which are removed with their component.
|
|
2613
|
+
const aliases = new Map<Component, Node[]>();
|
|
2112
2614
|
for (const component of components) {
|
|
2113
2615
|
let uses = 0;
|
|
2114
2616
|
for (const reference of component.binding.references) {
|
|
@@ -2117,8 +2619,11 @@ class Inliner {
|
|
|
2117
2619
|
continue;
|
|
2118
2620
|
}
|
|
2119
2621
|
const element = parent ? this.context.parents.get(parent) : undefined;
|
|
2622
|
+
const alias = this.unusedAlias(reference);
|
|
2120
2623
|
if (parent?.type === 'JSXOpeningElement' && element?.type === 'JSXElement') {
|
|
2121
2624
|
sites.push({ component, element });
|
|
2625
|
+
} else if (alias) {
|
|
2626
|
+
aliases.set(component, [...(aliases.get(component) ?? []), alias]);
|
|
2122
2627
|
} else {
|
|
2123
2628
|
uses += 1;
|
|
2124
2629
|
}
|
|
@@ -2173,14 +2678,12 @@ class Inliner {
|
|
|
2173
2678
|
// component stays until the next pass inlines it there too.
|
|
2174
2679
|
const copied = [...inlined.keys()];
|
|
2175
2680
|
for (const [component, elements] of inlined) {
|
|
2176
|
-
const insideCopy = elements
|
|
2177
|
-
copied.some(
|
|
2178
|
-
(other) =>
|
|
2179
|
-
other !== component && element.start >= other.fn.start && element.end <= other.fn.end,
|
|
2180
|
-
),
|
|
2181
|
-
);
|
|
2681
|
+
const insideCopy = isInsideOtherCopy(component, elements, copied);
|
|
2182
2682
|
if (component.declaration && otherUses.get(component) === 0 && !insideCopy) {
|
|
2183
2683
|
this.context.s.remove(component.declaration.start, component.declaration.end);
|
|
2684
|
+
for (const alias of aliases.get(component) ?? []) {
|
|
2685
|
+
this.context.s.remove(alias.start, alias.end);
|
|
2686
|
+
}
|
|
2184
2687
|
}
|
|
2185
2688
|
}
|
|
2186
2689
|
}
|
|
@@ -2233,7 +2736,7 @@ class Inliner {
|
|
|
2233
2736
|
): { reason: string; name: string } | undefined {
|
|
2234
2737
|
const tag = element.openingElement.name;
|
|
2235
2738
|
if (tag.type === 'JSXMemberExpression') {
|
|
2236
|
-
return { reason: 'member-tag', name:
|
|
2739
|
+
return { reason: 'member-tag', name: getText(this.context, tag).code };
|
|
2237
2740
|
}
|
|
2238
2741
|
if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
|
|
2239
2742
|
return undefined;
|
|
@@ -2275,12 +2778,13 @@ class Inliner {
|
|
|
2275
2778
|
for (const { host, texts } of this.hosts.values()) {
|
|
2276
2779
|
if (host.kind === 'return') {
|
|
2277
2780
|
const indent = this.indentationAt(host.statement.start);
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2781
|
+
const written: Content[] = [];
|
|
2782
|
+
for (const text of texts) {
|
|
2783
|
+
written.push(text, `\n${indent}`);
|
|
2784
|
+
}
|
|
2785
|
+
this.context.s.prependRight(host.statement.start, join(written));
|
|
2282
2786
|
} else {
|
|
2283
|
-
this.context.s.prependRight(host.body.start, `{ ${
|
|
2787
|
+
this.context.s.prependRight(host.body.start, joinText`{ ${join(texts, ' ')} return `);
|
|
2284
2788
|
this.context.s.appendLeft(host.body.end, '; }');
|
|
2285
2789
|
}
|
|
2286
2790
|
}
|
|
@@ -2295,3 +2799,154 @@ export function inline(context: PassContext): boolean {
|
|
|
2295
2799
|
inliner.run();
|
|
2296
2800
|
return inliner.changed;
|
|
2297
2801
|
}
|
|
2802
|
+
|
|
2803
|
+
/** Whether another pending site must be done before this one. */
|
|
2804
|
+
function hasPendingDependency(pending: readonly CallSite[], site: CallSite): boolean {
|
|
2805
|
+
for (const other of pending) {
|
|
2806
|
+
if (
|
|
2807
|
+
other !== site &&
|
|
2808
|
+
(isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))
|
|
2809
|
+
) {
|
|
2810
|
+
return true;
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
return false;
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
function areDataDeclarators(declarators: readonly VariableDeclarator[]): boolean {
|
|
2817
|
+
for (const declarator of declarators) {
|
|
2818
|
+
if (
|
|
2819
|
+
declarator.id.type !== 'Identifier' ||
|
|
2820
|
+
declarator.init === null ||
|
|
2821
|
+
!isDataLiteral(declarator.init)
|
|
2822
|
+
) {
|
|
2823
|
+
return false;
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
return true;
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
function areDataElements(elements: readonly (ArrayExpressionElement | null)[]): boolean {
|
|
2830
|
+
for (const element of elements) {
|
|
2831
|
+
if (element === null || element.type === 'SpreadElement' || !isDataLiteral(element)) {
|
|
2832
|
+
return false;
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
return true;
|
|
2836
|
+
}
|
|
2837
|
+
|
|
2838
|
+
function areDataProperties(properties: readonly ObjectPropertyKind[]): boolean {
|
|
2839
|
+
for (const property of properties) {
|
|
2840
|
+
if (
|
|
2841
|
+
property.type !== 'Property' ||
|
|
2842
|
+
property.kind !== 'init' ||
|
|
2843
|
+
property.method ||
|
|
2844
|
+
property.computed ||
|
|
2845
|
+
!isDataLiteral(property.value)
|
|
2846
|
+
) {
|
|
2847
|
+
return false;
|
|
2848
|
+
}
|
|
2849
|
+
}
|
|
2850
|
+
return true;
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
function getSignificantChildren(children: readonly JSXChild[]): JSXChild[] {
|
|
2854
|
+
const significant: JSXChild[] = [];
|
|
2855
|
+
for (const child of children) {
|
|
2856
|
+
if (!isInsignificant(child)) {
|
|
2857
|
+
significant.push(child);
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
return significant;
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
function hasSpreadChild(children: readonly JSXChild[]): boolean {
|
|
2864
|
+
for (const child of children) {
|
|
2865
|
+
if (child.type === 'JSXSpreadChild') {
|
|
2866
|
+
return true;
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
return false;
|
|
2870
|
+
}
|
|
2871
|
+
|
|
2872
|
+
function hasSiteInside(sites: readonly CallSite[], element: JSXElement): boolean {
|
|
2873
|
+
for (const site of sites) {
|
|
2874
|
+
if (isWithin(site.element, element) && site.element !== element) {
|
|
2875
|
+
return true;
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
return false;
|
|
2879
|
+
}
|
|
2880
|
+
|
|
2881
|
+
function findOuterComponent(
|
|
2882
|
+
components: readonly Component[],
|
|
2883
|
+
inner: Component,
|
|
2884
|
+
element: JSXElement,
|
|
2885
|
+
): Component | undefined {
|
|
2886
|
+
for (const item of components) {
|
|
2887
|
+
if (item !== inner && isWithin(element, item.fn)) {
|
|
2888
|
+
return item;
|
|
2889
|
+
}
|
|
2890
|
+
}
|
|
2891
|
+
return undefined;
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2894
|
+
function collectPrepared(copies: Iterable<PreparedSite | undefined>): PreparedSite[] {
|
|
2895
|
+
const prepared: PreparedSite[] = [];
|
|
2896
|
+
for (const copy of copies) {
|
|
2897
|
+
if (copy !== undefined) {
|
|
2898
|
+
prepared.push(copy);
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
return prepared;
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
function hasMissingCopy(copies: Iterable<PreparedSite | undefined>): boolean {
|
|
2905
|
+
for (const copy of copies) {
|
|
2906
|
+
if (copy === undefined) {
|
|
2907
|
+
return true;
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
return false;
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
function isWithinAny(element: JSXElement, outers: readonly JSXElement[]): boolean {
|
|
2914
|
+
for (const outer of outers) {
|
|
2915
|
+
if (isWithin(element, outer)) {
|
|
2916
|
+
return true;
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
return false;
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
function isInsideOtherCopy(
|
|
2923
|
+
component: Component,
|
|
2924
|
+
elements: readonly JSXElement[],
|
|
2925
|
+
copied: readonly Component[],
|
|
2926
|
+
): boolean {
|
|
2927
|
+
for (const element of elements) {
|
|
2928
|
+
for (const other of copied) {
|
|
2929
|
+
if (other !== component && element.start >= other.fn.start && element.end <= other.fn.end) {
|
|
2930
|
+
return true;
|
|
2931
|
+
}
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
return false;
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
function hasChild(children: readonly Node[], child: Node): boolean {
|
|
2938
|
+
for (const item of children) {
|
|
2939
|
+
if (item === child) {
|
|
2940
|
+
return true;
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
return false;
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
/** The name a non-computed object key spells, like `Trigger` or `'Trigger'`. */
|
|
2947
|
+
function propertyKeyName(key: Node): string | undefined {
|
|
2948
|
+
if (key.type === 'Identifier') {
|
|
2949
|
+
return key.name;
|
|
2950
|
+
}
|
|
2951
|
+
return key.type === 'Literal' && typeof key.value === 'string' ? key.value : undefined;
|
|
2952
|
+
}
|