solid-optimizer 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -4
- package/README.md +269 -14
- package/dist/compile-BroGlagZ.d.mts +172 -0
- package/dist/compile-BroGlagZ.d.mts.map +1 -0
- package/dist/compile-ocDYvJUW.mjs +3910 -0
- package/dist/compile-ocDYvJUW.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/vite.d.mts +35 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +1465 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +60 -58
- package/src/ast.ts +263 -0
- package/src/compile.ts +304 -0
- package/src/constants.ts +249 -0
- package/src/context.ts +87 -0
- package/src/fold.ts +850 -0
- package/src/index.ts +4 -319
- package/src/inline.ts +2028 -0
- package/src/jsx.ts +170 -0
- package/src/memo.ts +383 -0
- package/src/provider.ts +842 -0
- package/src/scope.ts +588 -0
- package/src/server.ts +275 -0
- package/src/size.ts +307 -0
- package/src/value.ts +386 -0
- package/src/vite/index.ts +1135 -0
- package/src/vite/inject.ts +311 -0
- package/src/vite/namespace.ts +66 -0
- package/src/vite/runtime.ts +390 -0
- package/src/vite/usage.ts +405 -0
- package/dist/cjs/development/index.cjs +0 -316
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -1
- package/dist/esm/development/index.mjs +0 -285
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/unwrap-node.d.ts +0 -7
- package/dist/types/unwrap-node.d.ts.map +0 -1
- package/src/unwrap-node.ts +0 -27
package/src/jsx.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for replacing JSX children as text.
|
|
3
|
+
*
|
|
4
|
+
* Adjacent JSX text is one text node to the parser, and JSX trims a text
|
|
5
|
+
* node as a whole: `a` and `b` on separate lines render as `ab` when an
|
|
6
|
+
* element sits between them, but as `a b` once they are one text node. So
|
|
7
|
+
* whenever a replacement would put two texts that were apart next to each
|
|
8
|
+
* other, an empty `{}` keeps them apart.
|
|
9
|
+
*/
|
|
10
|
+
import type { JSXAttribute, JSXChild, JSXElement, JSXFragment, JSXText, Node } from 'oxc-parser';
|
|
11
|
+
import { isFilteredText } from './ast';
|
|
12
|
+
import type MagicString from 'magic-string';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What a JSX child turns into.
|
|
16
|
+
*/
|
|
17
|
+
export interface Piece {
|
|
18
|
+
/** The child the piece replaces. */
|
|
19
|
+
readonly node: JSXChild;
|
|
20
|
+
/** The replacement text, or `undefined` to keep the child as written. */
|
|
21
|
+
text: string | undefined;
|
|
22
|
+
/** The JSX text the replacement starts with. */
|
|
23
|
+
startText: JSXText | undefined;
|
|
24
|
+
/** The JSX text the replacement ends with. */
|
|
25
|
+
endText: JSXText | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function keptPiece(node: JSXChild): Piece {
|
|
29
|
+
const text = node.type === 'JSXText' ? node : undefined;
|
|
30
|
+
return { node, text: undefined, startText: text, endText: text };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A piece that splices `children` in place of `node`.
|
|
35
|
+
*/
|
|
36
|
+
export function splicePiece(s: MagicString, node: JSXChild, children: readonly JSXChild[]): Piece {
|
|
37
|
+
const first = children.at(0);
|
|
38
|
+
const last = children.at(-1);
|
|
39
|
+
if (!first || !last) {
|
|
40
|
+
return replacedPiece(node, '');
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
node,
|
|
44
|
+
text: s.slice(first.start, last.end),
|
|
45
|
+
startText: first.type === 'JSXText' ? first : undefined,
|
|
46
|
+
endText: last.type === 'JSXText' ? last : undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function replacedPiece(node: JSXChild, text: string): Piece {
|
|
51
|
+
return { node, text, startText: undefined, endText: undefined };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether two texts render differently as one text node than apart.
|
|
56
|
+
* Two texts that JSX drops, like line breaks and indentation, still drop together.
|
|
57
|
+
*/
|
|
58
|
+
function mergeChangesText(before: JSXText | undefined, after: JSXText | undefined): boolean {
|
|
59
|
+
return (
|
|
60
|
+
before !== undefined &&
|
|
61
|
+
after !== undefined &&
|
|
62
|
+
!(isFilteredText(before) && isFilteredText(after))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Puts `{}` between two pieces that meet as text, on whichever side is replaced.
|
|
68
|
+
*/
|
|
69
|
+
function separate(previous: Piece, next: Piece, emptiesBetween: readonly Piece[]): void {
|
|
70
|
+
if (next.text !== undefined) {
|
|
71
|
+
next.text = `{}${next.text}`;
|
|
72
|
+
next.startText = undefined;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (previous.text !== undefined) {
|
|
76
|
+
previous.text = `${previous.text}{}`;
|
|
77
|
+
previous.endText = undefined;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// Two kept texts only meet across a removed child.
|
|
81
|
+
const empty = emptiesBetween.at(-1);
|
|
82
|
+
if (empty) {
|
|
83
|
+
empty.text = '{}';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Writes the replaced pieces of a child list, adding `{}` wherever two texts
|
|
89
|
+
* would otherwise merge.
|
|
90
|
+
*/
|
|
91
|
+
export function writePieces(s: MagicString, pieces: readonly Piece[]): boolean {
|
|
92
|
+
let changed = false;
|
|
93
|
+
// The last piece that renders anything, and the empty pieces after it.
|
|
94
|
+
let previous: Piece | undefined;
|
|
95
|
+
let emptiesSincePrevious: Piece[] = [];
|
|
96
|
+
for (const piece of pieces) {
|
|
97
|
+
if (piece.text === '') {
|
|
98
|
+
emptiesSincePrevious.push(piece);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (previous && mergeChangesText(previous.endText, piece.startText)) {
|
|
102
|
+
separate(previous, piece, emptiesSincePrevious);
|
|
103
|
+
}
|
|
104
|
+
previous = piece;
|
|
105
|
+
emptiesSincePrevious = [];
|
|
106
|
+
}
|
|
107
|
+
for (const piece of pieces) {
|
|
108
|
+
if (piece.text === undefined) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
changed = true;
|
|
112
|
+
if (piece.text === '') {
|
|
113
|
+
s.remove(piece.node.start, piece.node.end);
|
|
114
|
+
} else {
|
|
115
|
+
s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return changed;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function findAttribute(element: JSXElement, name: string): JSXAttribute | undefined {
|
|
122
|
+
for (const attribute of element.openingElement.attributes) {
|
|
123
|
+
if (
|
|
124
|
+
attribute.type === 'JSXAttribute' &&
|
|
125
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
126
|
+
attribute.name.name === name
|
|
127
|
+
) {
|
|
128
|
+
return attribute;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function hasSpreadAttribute(element: JSXElement): boolean {
|
|
135
|
+
return element.openingElement.attributes.some(
|
|
136
|
+
(attribute) => attribute.type === 'JSXSpreadAttribute',
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Whether any child is a function, which the runtime may call with the
|
|
142
|
+
* narrowed value. Folding one away would drop that call.
|
|
143
|
+
*/
|
|
144
|
+
export function hasCallbackChild(children: readonly JSXChild[]): boolean {
|
|
145
|
+
return children.some((child) => {
|
|
146
|
+
if (child.type !== 'JSXExpressionContainer') {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
let expression: Node = child.expression;
|
|
150
|
+
while (expression.type === 'ParenthesizedExpression') {
|
|
151
|
+
expression = expression.expression;
|
|
152
|
+
}
|
|
153
|
+
return (
|
|
154
|
+
expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression'
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The text between an element's tags, or an empty string for a self-closing element.
|
|
161
|
+
*/
|
|
162
|
+
export function innerText(s: MagicString, node: JSXElement | JSXFragment): string {
|
|
163
|
+
if (node.type === 'JSXFragment') {
|
|
164
|
+
return s.slice(node.openingFragment.end, node.closingFragment.start);
|
|
165
|
+
}
|
|
166
|
+
if (!node.closingElement) {
|
|
167
|
+
return '';
|
|
168
|
+
}
|
|
169
|
+
return s.slice(node.openingElement.end, node.closingElement.start);
|
|
170
|
+
}
|
package/src/memo.ts
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The memo pass: inlines a `createMemo` whose memoization does nothing.
|
|
3
|
+
*
|
|
4
|
+
* A memo does two things for its readers. It runs its computation once and
|
|
5
|
+
* shares the result, and it only notifies them when the result changes. A
|
|
6
|
+
* memo that returns a new object, array, function, or JSX element on every
|
|
7
|
+
* run, or that sets `equals: false`, notifies on every run, so the second
|
|
8
|
+
* part never applies. When such a memo is also read exactly once, the first
|
|
9
|
+
* part does not apply either, and the read can run the computation itself:
|
|
10
|
+
*
|
|
11
|
+
* ```jsx
|
|
12
|
+
* const style = createMemo(() => ({ color: color() }));
|
|
13
|
+
* return <p style={style()} />;
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* becomes `<p style={{ color: color() }} />`.
|
|
17
|
+
*
|
|
18
|
+
* The read has to be the whole of a tracked expression, so it depends on
|
|
19
|
+
* exactly what the memo depended on and reruns exactly as often. It also has
|
|
20
|
+
* to run once each time the function holding the memo runs, so a read in a
|
|
21
|
+
* callback, a loop, or a component's children stays a read of the memo.
|
|
22
|
+
*
|
|
23
|
+
* # Accepted differences
|
|
24
|
+
*
|
|
25
|
+
* - The computation first runs when its reader first runs, instead of when
|
|
26
|
+
* the memo is created.
|
|
27
|
+
* - The memo no longer takes a slot in the owner tree, which shifts the
|
|
28
|
+
* hydration keys after it. A server build and its client build must
|
|
29
|
+
* compile with the same options.
|
|
30
|
+
*/
|
|
31
|
+
import type {
|
|
32
|
+
ArrowFunctionExpression,
|
|
33
|
+
Expression,
|
|
34
|
+
Function as FunctionNode,
|
|
35
|
+
Node,
|
|
36
|
+
VariableDeclarator,
|
|
37
|
+
} from 'oxc-parser';
|
|
38
|
+
import { isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
39
|
+
import type { PassContext } from './context';
|
|
40
|
+
import { solidExport, textOf } from './context';
|
|
41
|
+
import { usesFunctionContext } from './inline';
|
|
42
|
+
import type { Binding } from './scope';
|
|
43
|
+
import { lookup, scopeAt } from './scope';
|
|
44
|
+
import { isSideEffectFree } from './value';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Props of Solid's built-in components that are read inside a memo, once per update.
|
|
48
|
+
*/
|
|
49
|
+
const TRACKED_PROPS = new Map([
|
|
50
|
+
['Show', 'when'],
|
|
51
|
+
['Match', 'when'],
|
|
52
|
+
['For', 'each'],
|
|
53
|
+
['Index', 'each'],
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether evaluating `node` always creates a new value, which reference
|
|
58
|
+
* equality never finds equal to the last one.
|
|
59
|
+
*/
|
|
60
|
+
function isFresh(node: Node): boolean {
|
|
61
|
+
switch (node.type) {
|
|
62
|
+
case 'ObjectExpression':
|
|
63
|
+
case 'ArrayExpression':
|
|
64
|
+
case 'ArrowFunctionExpression':
|
|
65
|
+
case 'FunctionExpression':
|
|
66
|
+
case 'ClassExpression':
|
|
67
|
+
case 'NewExpression':
|
|
68
|
+
case 'JSXElement':
|
|
69
|
+
case 'JSXFragment':
|
|
70
|
+
return true;
|
|
71
|
+
case 'ParenthesizedExpression':
|
|
72
|
+
return isFresh(node.expression);
|
|
73
|
+
case 'ConditionalExpression':
|
|
74
|
+
return isFresh(node.consequent) && isFresh(node.alternate);
|
|
75
|
+
case 'SequenceExpression': {
|
|
76
|
+
const last = node.expressions.at(-1);
|
|
77
|
+
return last !== undefined && isFresh(last);
|
|
78
|
+
}
|
|
79
|
+
default:
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The expression a compute function returns, when that is all it does.
|
|
86
|
+
*/
|
|
87
|
+
function returnedExpression(fn: FunctionNode | ArrowFunctionExpression): Expression | undefined {
|
|
88
|
+
if (fn.async || fn.generator || fn.params.length > 0 || !fn.body) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
if (fn.body.type !== 'BlockStatement') {
|
|
92
|
+
return fn.body;
|
|
93
|
+
}
|
|
94
|
+
const statements = fn.body.body;
|
|
95
|
+
const statement = statements.at(0);
|
|
96
|
+
if (statements.length !== 1 || statement?.type !== 'ReturnStatement' || !statement.argument) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
return statement.argument;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class MemoInliner {
|
|
103
|
+
changed = false;
|
|
104
|
+
|
|
105
|
+
constructor(private readonly context: PassContext) {}
|
|
106
|
+
|
|
107
|
+
private isCreateMemo(callee: Node): boolean {
|
|
108
|
+
if (callee.type !== 'Identifier') {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const binding = this.context.scopes.references.get(callee);
|
|
112
|
+
return binding !== undefined && solidExport(binding, this.context.options) === 'createMemo';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The computation of a memo whose result is never equal to its last one.
|
|
117
|
+
*/
|
|
118
|
+
private unstableComputation(declarator: VariableDeclarator): Expression | undefined {
|
|
119
|
+
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
120
|
+
if (init?.type !== 'CallExpression' || !this.isCreateMemo(init.callee)) {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
// Solid 1 takes `createMemo(compute, initialValue?, options?)`. The
|
|
124
|
+
// initial value only reaches the computation as `prev`, which a
|
|
125
|
+
// computation without parameters never reads.
|
|
126
|
+
const args = init.arguments;
|
|
127
|
+
const compute = args.at(0);
|
|
128
|
+
const initialValue = args.at(1);
|
|
129
|
+
const options = args.at(2);
|
|
130
|
+
if (
|
|
131
|
+
args.length > 3 ||
|
|
132
|
+
compute === undefined ||
|
|
133
|
+
compute.type === 'SpreadElement' ||
|
|
134
|
+
initialValue?.type === 'SpreadElement' ||
|
|
135
|
+
(initialValue !== undefined && !isSideEffectFree(initialValue))
|
|
136
|
+
) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
const fn = unwrap(compute);
|
|
140
|
+
if (fn.type !== 'ArrowFunctionExpression' && fn.type !== 'FunctionExpression') {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const expression = returnedExpression(fn);
|
|
144
|
+
if (!expression) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
let alwaysNotifies = false;
|
|
148
|
+
if (options !== undefined) {
|
|
149
|
+
// Only `equals: false` is understood. Any other option changes what the memo does.
|
|
150
|
+
if (options.type !== 'ObjectExpression' || options.properties.length !== 1) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
const property = options.properties.at(0);
|
|
154
|
+
if (
|
|
155
|
+
property?.type !== 'Property' ||
|
|
156
|
+
property.computed ||
|
|
157
|
+
property.key.type !== 'Identifier' ||
|
|
158
|
+
property.key.name !== 'equals' ||
|
|
159
|
+
property.value.type !== 'Literal' ||
|
|
160
|
+
property.value.value !== false
|
|
161
|
+
) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
alwaysNotifies = true;
|
|
165
|
+
}
|
|
166
|
+
return alwaysNotifies || isFresh(expression) ? expression : undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The function a node runs in.
|
|
171
|
+
*/
|
|
172
|
+
private functionOf(node: Node): Node | undefined {
|
|
173
|
+
let current = this.context.parents.get(node);
|
|
174
|
+
while (current && !isFunctionNode(current)) {
|
|
175
|
+
current = this.context.parents.get(current);
|
|
176
|
+
}
|
|
177
|
+
return current;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Whether a call is the whole of an expression that Solid tracks, and that
|
|
182
|
+
* runs once each time `fn` runs.
|
|
183
|
+
*/
|
|
184
|
+
private isSingleTrackedRead(call: Node, fn: Node): boolean {
|
|
185
|
+
let node = call;
|
|
186
|
+
let parent = this.context.parents.get(node);
|
|
187
|
+
while (parent?.type === 'ParenthesizedExpression') {
|
|
188
|
+
node = parent;
|
|
189
|
+
parent = this.context.parents.get(node);
|
|
190
|
+
}
|
|
191
|
+
if (parent?.type !== 'JSXExpressionContainer') {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
const container = parent;
|
|
195
|
+
const owner = this.context.parents.get(container);
|
|
196
|
+
let element: Node | undefined;
|
|
197
|
+
if (owner?.type === 'JSXFragment') {
|
|
198
|
+
element = owner;
|
|
199
|
+
} else if (owner?.type === 'JSXElement') {
|
|
200
|
+
// A child of an element is inserted, which tracks it. A child of a
|
|
201
|
+
// component is a prop, which the component can read any number of times.
|
|
202
|
+
const { name } = owner.openingElement;
|
|
203
|
+
if (name.type !== 'JSXIdentifier' || !isIntrinsicTag(name.name)) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
element = owner;
|
|
207
|
+
} else if (owner?.type === 'JSXAttribute' && owner.name.type === 'JSXIdentifier') {
|
|
208
|
+
const attribute = owner.name.name;
|
|
209
|
+
const opening = this.context.parents.get(owner);
|
|
210
|
+
element = opening ? this.context.parents.get(opening) : undefined;
|
|
211
|
+
if (element?.type !== 'JSXElement' || element.openingElement.name.type !== 'JSXIdentifier') {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
const tag = element.openingElement.name.name;
|
|
215
|
+
if (isIntrinsicTag(tag)) {
|
|
216
|
+
// An event handler or ref is read once and never tracked.
|
|
217
|
+
if (attribute.startsWith('on') || attribute === 'ref') {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
} else if (
|
|
221
|
+
this.builtInOf(element.openingElement.name) !== tag ||
|
|
222
|
+
TRACKED_PROPS.get(tag) !== attribute
|
|
223
|
+
) {
|
|
224
|
+
// A component can read a prop any number of times.
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
return this.isStaticJSX(element, fn);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private builtInOf(name: Node): string | undefined {
|
|
234
|
+
if (name.type !== 'JSXIdentifier') {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
const { references } = this.context.scopes;
|
|
238
|
+
if (!references.has(name)) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
const binding = references.get(name);
|
|
242
|
+
return binding ? solidExport(binding, this.context.options) : name.name;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Whether JSX is created once each time `fn` runs: it sits in JSX that `fn`
|
|
247
|
+
* returns, with only intrinsic elements and fragments around it.
|
|
248
|
+
*/
|
|
249
|
+
private isStaticJSX(element: Node, fn: Node): boolean {
|
|
250
|
+
let node = element;
|
|
251
|
+
for (;;) {
|
|
252
|
+
const parent = this.context.parents.get(node);
|
|
253
|
+
if (!parent) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
if (parent === fn) {
|
|
257
|
+
// An arrow function's expression body.
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
switch (parent.type) {
|
|
261
|
+
case 'JSXElement': {
|
|
262
|
+
const { name } = parent.openingElement;
|
|
263
|
+
if (name.type !== 'JSXIdentifier' || !isIntrinsicTag(name.name)) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
case 'JSXFragment':
|
|
269
|
+
case 'ParenthesizedExpression':
|
|
270
|
+
break;
|
|
271
|
+
case 'ReturnStatement':
|
|
272
|
+
return this.functionOf(parent) === fn && !this.inLoop(parent, fn);
|
|
273
|
+
default:
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
node = parent;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private inLoop(node: Node, fn: Node): boolean {
|
|
281
|
+
let current = this.context.parents.get(node);
|
|
282
|
+
while (current && current !== fn) {
|
|
283
|
+
if (
|
|
284
|
+
current.type === 'ForStatement' ||
|
|
285
|
+
current.type === 'ForInStatement' ||
|
|
286
|
+
current.type === 'ForOfStatement' ||
|
|
287
|
+
current.type === 'WhileStatement' ||
|
|
288
|
+
current.type === 'DoWhileStatement'
|
|
289
|
+
) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
current = this.context.parents.get(current);
|
|
293
|
+
}
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Whether every name in `expression` means the same at `site`.
|
|
299
|
+
*/
|
|
300
|
+
private resolvesSameAt(expression: Node, site: Node, memo: Binding): boolean {
|
|
301
|
+
const scope = scopeAt(this.context.scopes, this.context.parents, site);
|
|
302
|
+
for (const [reference, binding] of this.context.scopes.references) {
|
|
303
|
+
if (reference.start < expression.start || reference.end > expression.end) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (binding === memo) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
// A name declared inside the computation, like a callback's parameter, moves with it.
|
|
310
|
+
if (
|
|
311
|
+
binding !== undefined &&
|
|
312
|
+
binding.identifier.start >= expression.start &&
|
|
313
|
+
binding.identifier.end <= expression.end
|
|
314
|
+
) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (
|
|
318
|
+
(reference.type === 'Identifier' || reference.type === 'JSXIdentifier') &&
|
|
319
|
+
lookup(scope, reference.name) !== binding
|
|
320
|
+
) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private tryInline(declarator: VariableDeclarator, binding: Binding): void {
|
|
328
|
+
const expression = this.unstableComputation(declarator);
|
|
329
|
+
if (!expression || binding.references.length !== 1) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const statement = this.context.parents.get(declarator);
|
|
333
|
+
const fn = this.functionOf(declarator);
|
|
334
|
+
if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1 || !fn) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const reference = binding.references.at(0);
|
|
338
|
+
const call = reference ? this.context.parents.get(reference) : undefined;
|
|
339
|
+
if (
|
|
340
|
+
call?.type !== 'CallExpression' ||
|
|
341
|
+
call.callee !== reference ||
|
|
342
|
+
call.arguments.length > 0 ||
|
|
343
|
+
!this.isSingleTrackedRead(call, fn) ||
|
|
344
|
+
!this.resolvesSameAt(expression, call, binding) ||
|
|
345
|
+
usesFunctionContext(this.context, expression)
|
|
346
|
+
) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
// The read sits in a JSX container, which takes any expression.
|
|
350
|
+
this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
|
|
351
|
+
this.context.s.remove(statement.start, statement.end);
|
|
352
|
+
this.changed = true;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
run(): void {
|
|
356
|
+
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
357
|
+
// A memo outside any function has no owner, and disposes differently.
|
|
358
|
+
if (scope.parent === undefined) {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
for (const binding of scope.bindings.values()) {
|
|
362
|
+
const { declaration } = binding;
|
|
363
|
+
if (
|
|
364
|
+
binding.kind === 'const' &&
|
|
365
|
+
!binding.mutated &&
|
|
366
|
+
declaration.type === 'VariableDeclarator' &&
|
|
367
|
+
declaration.id === binding.identifier
|
|
368
|
+
) {
|
|
369
|
+
this.tryInline(declaration, binding);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Runs the memo pass over a parsed program. Returns whether anything changed.
|
|
378
|
+
*/
|
|
379
|
+
export function inlineMemos(context: PassContext): boolean {
|
|
380
|
+
const inliner = new MemoInliner(context);
|
|
381
|
+
inliner.run();
|
|
382
|
+
return inliner.changed;
|
|
383
|
+
}
|