octane 0.6.2 → 0.7.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/dist/cjs/constants.cjs +3 -0
- package/dist/cjs/dom-binding-handoff.cjs +2 -1
- package/dist/cjs/dom-binding-protocol.cjs +4 -3
- package/dist/cjs/dom-binding-styles.cjs +3 -2
- package/dist/cjs/dom-bindings.cjs +24 -23
- package/dist/cjs/error-codes.client.generated.cjs +7 -1
- package/dist/cjs/error-codes.server.generated.cjs +27 -1
- package/dist/cjs/hydration/event-capture.cjs +2 -1
- package/dist/cjs/independent-hydration-protocol.cjs +3 -2
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/runtime.cjs +866 -370
- package/dist/cjs/runtime.server.cjs +250 -62
- package/dist/cjs/server/index.cjs +2 -0
- package/dist/cjs/server/rpc-stream.cjs +19 -12
- package/dist/cjs/server/streamed-signals.cjs +18 -1
- package/dist/cjs/shared-value-helpers.cjs +24 -2
- package/dist/cjs/signals/control-binding.cjs +7 -6
- package/dist/cjs/signals/derived-facade.cjs +2 -1
- package/dist/cjs/signals/document-owner.cjs +4 -3
- package/dist/cjs/signals/facade.cjs +10 -9
- package/dist/cjs/signals/graph.cjs +5 -4
- package/dist/cjs/signals/native-read-client.cjs +4 -3
- package/dist/cjs/signals/owner-context.cjs +4 -3
- package/dist/cjs/signals/query-attempt-observer.cjs +2 -1
- package/dist/cjs/signals/query-facade.cjs +2 -1
- package/dist/cjs/signals/scalar-computations.cjs +2 -1
- package/dist/cjs/signals/transition-action.cjs +10 -9
- package/dist/cjs/signals/transition-candidate.cjs +2 -1
- package/dist/cjs/version.cjs +1 -1
- package/dist/compiler/arm-exits.js +151 -0
- package/dist/compiler/compile-universal.js +262 -65
- package/dist/compiler/compile.js +1929 -306
- package/dist/compiler/deferred-snapshots.js +791 -0
- package/dist/compiler/strong-mode.js +59 -10
- package/dist/constants.d.ts +5 -1
- package/dist/constants.js +2 -0
- package/dist/error-codes.client.generated.d.ts +1 -0
- package/dist/error-codes.client.generated.js +5 -0
- package/dist/error-codes.server.generated.d.ts +5 -0
- package/dist/error-codes.server.generated.js +25 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -0
- package/dist/node/aria-diagnostics.js +92 -0
- package/dist/node/behavior-form-submissions.js +228 -0
- package/dist/node/behavior-root-types.js +0 -0
- package/dist/node/behavior-root.js +746 -0
- package/dist/node/class-names.js +25 -0
- package/dist/node/compatibility.js +10 -0
- package/dist/node/component-flags.js +23 -0
- package/dist/node/constants.js +115 -0
- package/dist/node/context-epoch.js +13 -0
- package/dist/node/context-identity.js +46 -0
- package/dist/node/css.js +80 -0
- package/dist/node/data-encoding.js +134 -0
- package/dist/node/devtools-hook.js +229 -0
- package/dist/node/dom-binding-claims.js +4 -0
- package/dist/node/dom-binding-classes.js +152 -0
- package/dist/node/dom-binding-controls.js +6 -0
- package/dist/node/dom-binding-handoff.js +77 -0
- package/dist/node/dom-binding-program.js +1617 -0
- package/dist/node/dom-binding-projections.js +204 -0
- package/dist/node/dom-binding-protocol.js +130 -0
- package/dist/node/dom-binding-signals.js +135 -0
- package/dist/node/dom-binding-styles.js +280 -0
- package/dist/node/dom-bindings.js +1087 -0
- package/dist/node/dom-focused-move.js +46 -0
- package/dist/node/dom-stage.js +878 -0
- package/dist/node/dom-tables.js +382 -0
- package/dist/node/error-codes.client.generated.js +1071 -0
- package/dist/node/error-codes.server.generated.js +761 -0
- package/dist/node/error-message.js +27 -0
- package/dist/node/event-names.js +15 -0
- package/dist/node/form-diagnostics.js +96 -0
- package/dist/node/form-submission.js +43 -0
- package/dist/node/has-own.js +4 -0
- package/dist/node/head-ownership.js +31 -0
- package/dist/node/hook-slot-cache.js +91 -0
- package/dist/node/host-property-diagnostics.js +189 -0
- package/dist/node/html-tree-validation.js +193 -0
- package/dist/node/hydration/condition.js +15 -0
- package/dist/node/hydration/control-capture.js +237 -0
- package/dist/node/hydration/document-lifecycle.js +89 -0
- package/dist/node/hydration/event-capture.js +362 -0
- package/dist/node/hydration/idle.js +23 -0
- package/dist/node/hydration/independent-island.js +317 -0
- package/dist/node/hydration/index.js +74 -0
- package/dist/node/hydration/interaction-config.js +61 -0
- package/dist/node/hydration/interaction.js +35 -0
- package/dist/node/hydration/load.js +15 -0
- package/dist/node/hydration/media.js +33 -0
- package/dist/node/hydration/native-intent.js +68 -0
- package/dist/node/hydration/never.js +12 -0
- package/dist/node/hydration/stream-delivery.js +345 -0
- package/dist/node/hydration/stream-receiver.js +190 -0
- package/dist/node/hydration/stream-result-receiver.js +276 -0
- package/dist/node/hydration/streamed-signals.js +186 -0
- package/dist/node/hydration/types.js +0 -0
- package/dist/node/hydration/visible.js +69 -0
- package/dist/node/hydration-markers.js +44 -0
- package/dist/node/independent-hydration-protocol.js +91 -0
- package/dist/node/index.js +463 -0
- package/dist/node/internal/client.js +7 -0
- package/dist/node/internal/context.js +5 -0
- package/dist/node/internal/server.js +5 -0
- package/dist/node/internal/signal-read.js +4 -0
- package/dist/node/method-dep.js +25 -0
- package/dist/node/profiling.js +432 -0
- package/dist/node/public-types.js +0 -0
- package/dist/node/react/fiber-adapter.js +57 -0
- package/dist/node/react/index.js +397 -0
- package/dist/node/react/react-compat-envelope.js +78 -0
- package/dist/node/react/react-compat-server.js +129 -0
- package/dist/node/react/react-compat-shared.js +104 -0
- package/dist/node/react/react-compat.js +223 -0
- package/dist/node/react/server.js +81 -0
- package/dist/node/react/shared.js +75 -0
- package/dist/node/renderer-bridge.js +35 -0
- package/dist/node/resize-observer.js +105 -0
- package/dist/node/resource-hint-diagnostics.js +67 -0
- package/dist/node/runtime-tags.js +22 -0
- package/dist/node/runtime.js +29364 -0
- package/dist/node/runtime.server.js +8200 -0
- package/dist/node/sanitize-url.js +34 -0
- package/dist/node/server/early-signals.js +54 -0
- package/dist/node/server/index.js +338 -0
- package/dist/node/server/rpc-batch.js +129 -0
- package/dist/node/server/rpc-stream.js +132 -0
- package/dist/node/server/rpc.js +48 -0
- package/dist/node/server/signal-query-observation.js +134 -0
- package/dist/node/server/streamed-region.js +31 -0
- package/dist/node/server/streamed-signals.js +574 -0
- package/dist/node/server-call.js +75 -0
- package/dist/node/server-rpc-batch-client.js +119 -0
- package/dist/node/server-rpc-client.js +108 -0
- package/dist/node/server-rpc-protocol.js +109 -0
- package/dist/node/server-rpc-stream-client.js +98 -0
- package/dist/node/shared-value-helpers.js +60 -0
- package/dist/node/signals/actions.js +463 -0
- package/dist/node/signals/client.js +22 -0
- package/dist/node/signals/computations.js +452 -0
- package/dist/node/signals/control-binding.js +388 -0
- package/dist/node/signals/control-handoff.js +12 -0
- package/dist/node/signals/derived-facade.js +25 -0
- package/dist/node/signals/document-owner.js +48 -0
- package/dist/node/signals/early-values.js +124 -0
- package/dist/node/signals/encoding.js +6 -0
- package/dist/node/signals/engine.js +668 -0
- package/dist/node/signals/errors.js +54 -0
- package/dist/node/signals/facade.js +441 -0
- package/dist/node/signals/graph.js +817 -0
- package/dist/node/signals/handle-protocol.js +11 -0
- package/dist/node/signals/index.js +87 -0
- package/dist/node/signals/native-read-client.js +319 -0
- package/dist/node/signals/native-read-collector.js +132 -0
- package/dist/node/signals/native-read-events.js +45 -0
- package/dist/node/signals/native-read-inspection.js +20 -0
- package/dist/node/signals/native-read-retry.js +30 -0
- package/dist/node/signals/native-read-seeds.js +243 -0
- package/dist/node/signals/native-read-server.js +99 -0
- package/dist/node/signals/owner-context.js +88 -0
- package/dist/node/signals/query-attempt-observer.js +42 -0
- package/dist/node/signals/query-facade.js +41 -0
- package/dist/node/signals/read-protocol.js +125 -0
- package/dist/node/signals/requests.js +716 -0
- package/dist/node/signals/scalar-computations.js +57 -0
- package/dist/node/signals/scope-streams.js +147 -0
- package/dist/node/signals/server.js +22 -0
- package/dist/node/signals/transition-action.js +555 -0
- package/dist/node/signals/transition-candidate.js +101 -0
- package/dist/node/signals/transition-coordinator.js +255 -0
- package/dist/node/signals/transition-state.js +82 -0
- package/dist/node/signals/types.js +20 -0
- package/dist/node/static/index.js +10 -0
- package/dist/node/stream-protocol.js +114 -0
- package/dist/node/streamed-signals-protocol.js +130 -0
- package/dist/node/style-values.js +96 -0
- package/dist/node/sub-slot.js +35 -0
- package/dist/node/testing.js +14 -0
- package/dist/node/trusted-html.js +6 -0
- package/dist/node/tsrx-iterable.js +5 -0
- package/dist/node/tsrx-spread.js +10 -0
- package/dist/node/universal-core.js +10138 -0
- package/dist/node/universal-dom-boundary.js +237 -0
- package/dist/node/universal-native.js +28 -0
- package/dist/node/universal.js +17 -0
- package/dist/node/version.js +4 -0
- package/dist/runtime.d.ts +219 -8
- package/dist/runtime.js +714 -220
- package/dist/runtime.server.d.ts +33 -2
- package/dist/runtime.server.js +217 -29
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +2 -0
- package/dist/server/rpc-stream.js +19 -12
- package/dist/server/streamed-signals.js +18 -1
- package/dist/shared-value-helpers.d.ts +11 -0
- package/dist/shared-value-helpers.js +21 -1
- package/dist/universal-core.d.ts +3 -1
- package/dist/universal-core.js +178 -50
- package/dist/version.js +1 -1
- package/package.json +62 -1
|
@@ -0,0 +1,791 @@
|
|
|
1
|
+
// Creation-time snapshots for compiler-deferred JSX reads.
|
|
2
|
+
//
|
|
3
|
+
// Value-position JSX defers its non-literal parts into compiler thunks
|
|
4
|
+
// (`createScopedElement(type, props, () => children)`,
|
|
5
|
+
// `createScopedValue(() => record)`), which run when Octane renders or inspects
|
|
6
|
+
// the element. A captured binding that code reassigns can hold a different
|
|
7
|
+
// value by then: `n++` in a `.map` callback, a `var` re-initialized by a loop,
|
|
8
|
+
// a counter bumped after the element was stored. React evaluates JSX
|
|
9
|
+
// expressions eagerly, so every element sees the value current at creation.
|
|
10
|
+
//
|
|
11
|
+
// This pass runs once on the final, TypeScript-free output Program. It finds
|
|
12
|
+
// each outermost deferral thunk, resolves the bindings its deferred code reads
|
|
13
|
+
// directly (outside nested user functions), and rebinds those that can be
|
|
14
|
+
// reassigned after the thunk exists to a parameter bound at its creation:
|
|
15
|
+
//
|
|
16
|
+
// createScopedElement('li', { key: x }, () => String(n))
|
|
17
|
+
// createScopedElement('li', { key: x }, ((n$) => () => String(n$))(n))
|
|
18
|
+
//
|
|
19
|
+
// Only the thunk argument is wrapped, so the snapshot is taken after the eager
|
|
20
|
+
// type and props, in React's argument order. User functions inside the thunk
|
|
21
|
+
// (event handlers, render props) keep the live binding, as JavaScript closures
|
|
22
|
+
// do in React. A thunk that itself writes a binding keeps reading it live: its
|
|
23
|
+
// write already runs at render, so snapshotting only the read would split one
|
|
24
|
+
// variable into two.
|
|
25
|
+
//
|
|
26
|
+
// A binding needs a snapshot only when a write can follow the thunk's creation.
|
|
27
|
+
// Its frame is the function body that declares it (or the loop body, for a
|
|
28
|
+
// block binding declared in one, or the module). A write in a function or
|
|
29
|
+
// thunk nested in that frame can run at any time. A nested loop runs in place,
|
|
30
|
+
// so its writes land where it ends: before a thunk created after the loop,
|
|
31
|
+
// after one created inside it. Other writes are the frame's straight-line code,
|
|
32
|
+
// ordered by the walk, which follows evaluation order. A nested function counts
|
|
33
|
+
// from where it is created. A function declaration counts from there too,
|
|
34
|
+
// unless code before it names it or it is exported, since hoisting then lets it
|
|
35
|
+
// run from its block's start. So `let x; if (c) x = a; else x = b;` before the
|
|
36
|
+
// JSX needs no snapshot, and its output stays byte-identical.
|
|
37
|
+
//
|
|
38
|
+
// The record a module-level function declaration returns when called directly
|
|
39
|
+
// keeps reading at render (docs/differences-from-react.md). Both emitters mark
|
|
40
|
+
// that record DEFERRED_LIVE, so server and client agree. Value JSX built in a
|
|
41
|
+
// callback inside such a record still snapshots.
|
|
42
|
+
//
|
|
43
|
+
// The walk is positional rather than keyed on node identity, because lowering
|
|
44
|
+
// reuses one subtree in several branches (a body call and a direct call).
|
|
45
|
+
// Thunks are matched between the two walks by identity, and a thunk reached at
|
|
46
|
+
// several positions keeps a name only while every position resolves it to the
|
|
47
|
+
// same binding.
|
|
48
|
+
|
|
49
|
+
import { builders as b } from '@tsrx/core';
|
|
50
|
+
|
|
51
|
+
/** Marker on compiler-built deferral thunks (arrow functions with no params). */
|
|
52
|
+
export const DEFERRED_READ = '_octaneDeferredRead';
|
|
53
|
+
|
|
54
|
+
/** Marker on a direct call's returned record, whose thunks keep live reads. */
|
|
55
|
+
export const DEFERRED_LIVE = '_octaneDeferredLive';
|
|
56
|
+
|
|
57
|
+
/** Mark a freshly built deferral thunk. */
|
|
58
|
+
export function markDeferredRead(arrow) {
|
|
59
|
+
return { ...arrow, [DEFERRED_READ]: true };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Keep the deferral thunks directly inside `record` reading live bindings. */
|
|
63
|
+
export function markDeferredLive(record) {
|
|
64
|
+
return { ...record, [DEFERRED_LIVE]: true };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isDeferredThunk = (node) =>
|
|
68
|
+
node.type === 'ArrowFunctionExpression' &&
|
|
69
|
+
node[DEFERRED_READ] === true &&
|
|
70
|
+
(node.params || []).length === 0;
|
|
71
|
+
|
|
72
|
+
const NON_RUNTIME_KEYS = new Set([
|
|
73
|
+
'type',
|
|
74
|
+
'loc',
|
|
75
|
+
'start',
|
|
76
|
+
'end',
|
|
77
|
+
'range',
|
|
78
|
+
'metadata',
|
|
79
|
+
'parent',
|
|
80
|
+
'typeAnnotation',
|
|
81
|
+
'returnType',
|
|
82
|
+
'typeParameters',
|
|
83
|
+
'typeArguments',
|
|
84
|
+
'superTypeParameters',
|
|
85
|
+
'superTypeArguments',
|
|
86
|
+
'implements',
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const isFunctionNode = (node) =>
|
|
90
|
+
node.type === 'FunctionDeclaration' ||
|
|
91
|
+
node.type === 'FunctionExpression' ||
|
|
92
|
+
node.type === 'ArrowFunctionExpression';
|
|
93
|
+
|
|
94
|
+
function createScope(parent, site) {
|
|
95
|
+
return { parent, bindings: new Map(), site };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function resolve(scope, name) {
|
|
99
|
+
for (let current = scope; current !== null; current = current.parent) {
|
|
100
|
+
const binding = current.bindings.get(name);
|
|
101
|
+
if (binding !== undefined) return binding;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function patternNames(pattern, into) {
|
|
107
|
+
if (pattern == null) return into;
|
|
108
|
+
switch (pattern.type) {
|
|
109
|
+
case 'Identifier':
|
|
110
|
+
into.push(pattern.name);
|
|
111
|
+
break;
|
|
112
|
+
case 'ObjectPattern':
|
|
113
|
+
for (const property of pattern.properties || []) {
|
|
114
|
+
patternNames(property.type === 'RestElement' ? property.argument : property.value, into);
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case 'ArrayPattern':
|
|
118
|
+
for (const element of pattern.elements || []) patternNames(element, into);
|
|
119
|
+
break;
|
|
120
|
+
case 'AssignmentPattern':
|
|
121
|
+
patternNames(pattern.left, into);
|
|
122
|
+
break;
|
|
123
|
+
case 'RestElement':
|
|
124
|
+
patternNames(pattern.argument, into);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
return into;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function eachChild(node, visit) {
|
|
131
|
+
for (const key in node) {
|
|
132
|
+
if (NON_RUNTIME_KEYS.has(key)) continue;
|
|
133
|
+
const value = node[key];
|
|
134
|
+
if (value === null || typeof value !== 'object') continue;
|
|
135
|
+
visit(value);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Names a function scope binds with `var` in any of its nested statements.
|
|
140
|
+
function collectVarNames(node, into) {
|
|
141
|
+
if (node == null || typeof node !== 'object') return into;
|
|
142
|
+
if (Array.isArray(node)) {
|
|
143
|
+
for (const child of node) collectVarNames(child, into);
|
|
144
|
+
return into;
|
|
145
|
+
}
|
|
146
|
+
if (isFunctionNode(node) || node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
|
|
147
|
+
return into;
|
|
148
|
+
}
|
|
149
|
+
if (node.type === 'VariableDeclaration') {
|
|
150
|
+
if (node.kind === 'var') {
|
|
151
|
+
for (const declaration of node.declarations || []) patternNames(declaration.id, into);
|
|
152
|
+
}
|
|
153
|
+
return into;
|
|
154
|
+
}
|
|
155
|
+
if (typeof node.type === 'string' && node.type.endsWith('Expression')) return into;
|
|
156
|
+
eachChild(node, (child) => collectVarNames(child, into));
|
|
157
|
+
return into;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Names a statement list binds lexically: let/const, functions, classes, imports.
|
|
161
|
+
function lexicalNames(statements, into) {
|
|
162
|
+
for (let statement of statements || []) {
|
|
163
|
+
if (
|
|
164
|
+
statement?.type === 'ExportNamedDeclaration' ||
|
|
165
|
+
statement?.type === 'ExportDefaultDeclaration'
|
|
166
|
+
) {
|
|
167
|
+
statement = statement.declaration;
|
|
168
|
+
}
|
|
169
|
+
if (statement == null) continue;
|
|
170
|
+
if (statement.type === 'VariableDeclaration' && statement.kind !== 'var') {
|
|
171
|
+
for (const declaration of statement.declarations || []) patternNames(declaration.id, into);
|
|
172
|
+
} else if (
|
|
173
|
+
(statement.type === 'FunctionDeclaration' || statement.type === 'ClassDeclaration') &&
|
|
174
|
+
statement.id
|
|
175
|
+
) {
|
|
176
|
+
into.push(statement.id.name);
|
|
177
|
+
} else if (statement.type === 'ImportDeclaration') {
|
|
178
|
+
for (const specifier of statement.specifiers || []) into.push(specifier.local.name);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return into;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Rebind reassigned captures of every outermost marked thunk in `program`.
|
|
186
|
+
* `allocateName(name)` returns a module-unique identifier for a snapshot of
|
|
187
|
+
* `name`. Returns `program` itself when nothing needs a snapshot.
|
|
188
|
+
*/
|
|
189
|
+
export function snapshotDeferredReads(program, allocateName) {
|
|
190
|
+
const sites = analyze(program);
|
|
191
|
+
if (sites === null) return program;
|
|
192
|
+
const names = new Map();
|
|
193
|
+
const snapshotName = (name) => {
|
|
194
|
+
let snapshot = names.get(name);
|
|
195
|
+
if (snapshot === undefined) {
|
|
196
|
+
snapshot = allocateName(name);
|
|
197
|
+
names.set(name, snapshot);
|
|
198
|
+
}
|
|
199
|
+
return snapshot;
|
|
200
|
+
};
|
|
201
|
+
return rewrite(program, sites, snapshotName);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Walk 1: bindings and their writes, and what each outermost thunk reads and
|
|
205
|
+
// writes. Returns null when no thunk reads a binding that a later write can
|
|
206
|
+
// change.
|
|
207
|
+
function analyze(program) {
|
|
208
|
+
const sites = new Map();
|
|
209
|
+
// Each enclosing function, loop, and thunk: its walk-order start, and for a
|
|
210
|
+
// loop the bindings of outer frames it writes.
|
|
211
|
+
const frames = [];
|
|
212
|
+
let clock = 0;
|
|
213
|
+
let blockStart = 0;
|
|
214
|
+
let site = null;
|
|
215
|
+
let live = false;
|
|
216
|
+
let loopUpdate = null;
|
|
217
|
+
let siteCount = 0;
|
|
218
|
+
// Names of hoisted function declarations, to notice uses that precede them.
|
|
219
|
+
const hoistedNames = new Set();
|
|
220
|
+
|
|
221
|
+
const declare = (scope, names) => {
|
|
222
|
+
for (const name of names) {
|
|
223
|
+
if (!scope.bindings.has(name)) {
|
|
224
|
+
scope.bindings.set(name, {
|
|
225
|
+
scope,
|
|
226
|
+
frame: frames.length - 1,
|
|
227
|
+
volatile: false,
|
|
228
|
+
lastWrite: -1,
|
|
229
|
+
loop: null,
|
|
230
|
+
hoisted: false,
|
|
231
|
+
referenced: false,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// Declare a statement list's lexical names, marking its function declarations.
|
|
238
|
+
const declareStatements = (scope, statements) => {
|
|
239
|
+
declare(scope, lexicalNames(statements, []));
|
|
240
|
+
for (const statement of statements || []) {
|
|
241
|
+
const exported =
|
|
242
|
+
statement?.type === 'ExportNamedDeclaration' ||
|
|
243
|
+
statement?.type === 'ExportDefaultDeclaration';
|
|
244
|
+
const declaration = exported ? statement.declaration : statement;
|
|
245
|
+
if (declaration?.type !== 'FunctionDeclaration' || !declaration.id) continue;
|
|
246
|
+
const binding = scope.bindings.get(declaration.id.name);
|
|
247
|
+
binding.hoisted = true;
|
|
248
|
+
if (exported) binding.referenced = true;
|
|
249
|
+
hoistedNames.add(declaration.id.name);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const write = (name, scope) => {
|
|
254
|
+
const binding = resolve(scope, name);
|
|
255
|
+
if (binding === null) return;
|
|
256
|
+
// A `for (let …)` binding is copied into a fresh environment before the
|
|
257
|
+
// update runs, so an update write never reaches an earlier iteration's
|
|
258
|
+
// thunk. The loop re-checks this when a thunk sits in the update itself.
|
|
259
|
+
if (binding.loop !== null && binding.loop === loopUpdate) {
|
|
260
|
+
// Covered by the loop.
|
|
261
|
+
} else if (frames.length - 1 === binding.frame) {
|
|
262
|
+
if (clock > binding.lastWrite) binding.lastWrite = clock;
|
|
263
|
+
} else {
|
|
264
|
+
let loops = true;
|
|
265
|
+
for (let depth = binding.frame + 1; loops && depth < frames.length; depth++) {
|
|
266
|
+
loops = frames[depth].writes !== null;
|
|
267
|
+
}
|
|
268
|
+
if (loops) frames[binding.frame + 1].writes.add(binding);
|
|
269
|
+
else binding.volatile = true;
|
|
270
|
+
}
|
|
271
|
+
if (site !== null && binding.scope.site !== site) site.writes.add(name);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const read = (name, scope) => {
|
|
275
|
+
if (site === null && !hoistedNames.has(name)) return;
|
|
276
|
+
const binding = resolve(scope, name);
|
|
277
|
+
if (binding === null) return;
|
|
278
|
+
if (binding.hoisted) binding.referenced = true;
|
|
279
|
+
if (site === null || binding.scope.site === site) return;
|
|
280
|
+
// The outermost frame between the binding's and this read: a write before
|
|
281
|
+
// it in the binding's frame has already run when the thunk is created.
|
|
282
|
+
const anchor = frames[binding.frame + 1].start;
|
|
283
|
+
const previous = site.reads.get(name);
|
|
284
|
+
if (previous === undefined) site.reads.set(name, { binding, anchor });
|
|
285
|
+
else if (previous.binding !== binding) site.conflicts.add(name);
|
|
286
|
+
else if (anchor < previous.anchor) previous.anchor = anchor;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const writePattern = (pattern, scope) => {
|
|
290
|
+
if (pattern == null) return;
|
|
291
|
+
switch (pattern.type) {
|
|
292
|
+
case 'Identifier':
|
|
293
|
+
write(pattern.name, scope);
|
|
294
|
+
return;
|
|
295
|
+
case 'ObjectPattern':
|
|
296
|
+
for (const property of pattern.properties || []) {
|
|
297
|
+
if (property.type === 'RestElement') {
|
|
298
|
+
writePattern(property.argument, scope);
|
|
299
|
+
} else {
|
|
300
|
+
if (property.computed) walk(property.key, scope);
|
|
301
|
+
writePattern(property.value, scope);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return;
|
|
305
|
+
case 'ArrayPattern':
|
|
306
|
+
for (const element of pattern.elements || []) writePattern(element, scope);
|
|
307
|
+
return;
|
|
308
|
+
case 'AssignmentPattern':
|
|
309
|
+
walk(pattern.right, scope);
|
|
310
|
+
writePattern(pattern.left, scope);
|
|
311
|
+
return;
|
|
312
|
+
case 'RestElement':
|
|
313
|
+
writePattern(pattern.argument, scope);
|
|
314
|
+
return;
|
|
315
|
+
default:
|
|
316
|
+
// A member target (`obj.x = …`) reads its object; it writes no binding.
|
|
317
|
+
walk(pattern, scope);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// Default values and computed keys of a declaration pattern are reads.
|
|
322
|
+
const declarationPattern = (pattern, scope) => {
|
|
323
|
+
if (pattern == null) return;
|
|
324
|
+
switch (pattern.type) {
|
|
325
|
+
case 'ObjectPattern':
|
|
326
|
+
for (const property of pattern.properties || []) {
|
|
327
|
+
if (property.type === 'RestElement') {
|
|
328
|
+
declarationPattern(property.argument, scope);
|
|
329
|
+
} else {
|
|
330
|
+
if (property.computed) walk(property.key, scope);
|
|
331
|
+
declarationPattern(property.value, scope);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return;
|
|
335
|
+
case 'ArrayPattern':
|
|
336
|
+
for (const element of pattern.elements || []) declarationPattern(element, scope);
|
|
337
|
+
return;
|
|
338
|
+
case 'AssignmentPattern':
|
|
339
|
+
declarationPattern(pattern.left, scope);
|
|
340
|
+
walk(pattern.right, scope);
|
|
341
|
+
return;
|
|
342
|
+
case 'RestElement':
|
|
343
|
+
declarationPattern(pattern.argument, scope);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const block = (statements, scope) => {
|
|
348
|
+
const savedBlockStart = blockStart;
|
|
349
|
+
blockStart = clock;
|
|
350
|
+
const inner = createScope(scope, site);
|
|
351
|
+
declareStatements(inner, statements);
|
|
352
|
+
walk(statements, inner);
|
|
353
|
+
blockStart = savedBlockStart;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// Walk `visit` inside a new frame that starts at `start`.
|
|
357
|
+
const frame = (start, visit, loop = false) => {
|
|
358
|
+
const entry = { start, writes: loop ? new Set() : null };
|
|
359
|
+
frames.push(entry);
|
|
360
|
+
visit();
|
|
361
|
+
frames.pop();
|
|
362
|
+
if (loop) {
|
|
363
|
+
for (const binding of entry.writes) {
|
|
364
|
+
if (clock > binding.lastWrite) binding.lastWrite = clock;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const fn = (node, scope) => {
|
|
370
|
+
const thunk = isDeferredThunk(node);
|
|
371
|
+
const savedSite = site;
|
|
372
|
+
const savedLive = live;
|
|
373
|
+
const savedLoopUpdate = loopUpdate;
|
|
374
|
+
const savedBlockStart = blockStart;
|
|
375
|
+
if (!thunk) {
|
|
376
|
+
site = null;
|
|
377
|
+
live = false;
|
|
378
|
+
} else if (site === null && !live) {
|
|
379
|
+
site = sites.get(node);
|
|
380
|
+
if (site === undefined) {
|
|
381
|
+
site = { reads: new Map(), writes: new Set(), conflicts: new Set() };
|
|
382
|
+
sites.set(node, site);
|
|
383
|
+
}
|
|
384
|
+
siteCount++;
|
|
385
|
+
}
|
|
386
|
+
loopUpdate = null;
|
|
387
|
+
const declared =
|
|
388
|
+
node.type === 'FunctionDeclaration' && node.id ? resolve(scope, node.id.name) : null;
|
|
389
|
+
frame(declared?.referenced === true ? blockStart : clock, () => {
|
|
390
|
+
blockStart = clock;
|
|
391
|
+
const inner = createScope(scope, site);
|
|
392
|
+
if (node.type === 'FunctionExpression' && node.id) declare(inner, [node.id.name]);
|
|
393
|
+
const params = [];
|
|
394
|
+
for (const param of node.params || []) patternNames(param, params);
|
|
395
|
+
declare(inner, params);
|
|
396
|
+
declare(inner, collectVarNames(node.body, []));
|
|
397
|
+
if (node.body?.type === 'BlockStatement') declareStatements(inner, node.body.body);
|
|
398
|
+
for (const param of node.params || []) declarationPattern(param, inner);
|
|
399
|
+
if (node.body?.type === 'BlockStatement') walk(node.body.body, inner);
|
|
400
|
+
else walk(node.body, inner);
|
|
401
|
+
});
|
|
402
|
+
site = savedSite;
|
|
403
|
+
live = savedLive;
|
|
404
|
+
loopUpdate = savedLoopUpdate;
|
|
405
|
+
blockStart = savedBlockStart;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// A body that runs apart from its surrounding expression, with no parameters:
|
|
409
|
+
// a class field initializer or static block.
|
|
410
|
+
const detached = (node, scope, statements) => {
|
|
411
|
+
const savedSite = site;
|
|
412
|
+
const savedLive = live;
|
|
413
|
+
const savedLoopUpdate = loopUpdate;
|
|
414
|
+
const savedBlockStart = blockStart;
|
|
415
|
+
site = null;
|
|
416
|
+
live = false;
|
|
417
|
+
loopUpdate = null;
|
|
418
|
+
frame(clock, () => {
|
|
419
|
+
blockStart = clock;
|
|
420
|
+
const inner = createScope(scope, null);
|
|
421
|
+
if (statements) {
|
|
422
|
+
declare(inner, collectVarNames(node, []));
|
|
423
|
+
declareStatements(inner, node);
|
|
424
|
+
}
|
|
425
|
+
walk(node, inner);
|
|
426
|
+
});
|
|
427
|
+
site = savedSite;
|
|
428
|
+
live = savedLive;
|
|
429
|
+
loopUpdate = savedLoopUpdate;
|
|
430
|
+
blockStart = savedBlockStart;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const variableDeclaration = (node, scope) => {
|
|
434
|
+
for (const declaration of node.declarations || []) {
|
|
435
|
+
declarationPattern(declaration.id, scope);
|
|
436
|
+
walk(declaration.init, scope);
|
|
437
|
+
// A `var` initializer assigns an existing function-scoped binding: it
|
|
438
|
+
// re-runs on each loop iteration and whenever the name is declared again.
|
|
439
|
+
if (node.kind === 'var' && declaration.init != null) {
|
|
440
|
+
for (const name of patternNames(declaration.id, [])) write(name, scope);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
function walk(node, scope) {
|
|
446
|
+
if (node == null || typeof node !== 'object') return;
|
|
447
|
+
if (Array.isArray(node)) {
|
|
448
|
+
for (const child of node) walk(child, scope);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const type = node.type;
|
|
452
|
+
if (typeof type !== 'string' || type.startsWith('TS')) return;
|
|
453
|
+
clock++;
|
|
454
|
+
if (node[DEFERRED_LIVE] === true && !live && site === null) {
|
|
455
|
+
live = true;
|
|
456
|
+
walkNode(node, type, scope);
|
|
457
|
+
live = false;
|
|
458
|
+
} else {
|
|
459
|
+
walkNode(node, type, scope);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function walkNode(node, type, scope) {
|
|
464
|
+
switch (type) {
|
|
465
|
+
case 'Identifier':
|
|
466
|
+
read(node.name, scope);
|
|
467
|
+
return;
|
|
468
|
+
case 'Program':
|
|
469
|
+
frame(clock, () => {
|
|
470
|
+
blockStart = clock;
|
|
471
|
+
const inner = createScope(null, null);
|
|
472
|
+
declareStatements(inner, node.body);
|
|
473
|
+
declare(inner, collectVarNames(node.body, []));
|
|
474
|
+
walk(node.body, inner);
|
|
475
|
+
});
|
|
476
|
+
return;
|
|
477
|
+
case 'FunctionDeclaration':
|
|
478
|
+
case 'FunctionExpression':
|
|
479
|
+
case 'ArrowFunctionExpression':
|
|
480
|
+
fn(node, scope);
|
|
481
|
+
return;
|
|
482
|
+
case 'ClassDeclaration':
|
|
483
|
+
case 'ClassExpression': {
|
|
484
|
+
const inner = createScope(scope, site);
|
|
485
|
+
if (node.type === 'ClassExpression' && node.id) declare(inner, [node.id.name]);
|
|
486
|
+
walk(node.decorators, scope);
|
|
487
|
+
walk(node.superClass, inner);
|
|
488
|
+
walk(node.body, inner);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
case 'MethodDefinition':
|
|
492
|
+
case 'PropertyDefinition':
|
|
493
|
+
case 'AccessorProperty':
|
|
494
|
+
walk(node.decorators, scope);
|
|
495
|
+
if (node.computed) walk(node.key, scope);
|
|
496
|
+
if (type === 'MethodDefinition') walk(node.value, scope);
|
|
497
|
+
else if (node.value != null) detached(node.value, scope, false);
|
|
498
|
+
return;
|
|
499
|
+
case 'StaticBlock':
|
|
500
|
+
detached(node.body, scope, true);
|
|
501
|
+
return;
|
|
502
|
+
case 'BlockStatement':
|
|
503
|
+
block(node.body, scope);
|
|
504
|
+
return;
|
|
505
|
+
case 'SwitchStatement': {
|
|
506
|
+
walk(node.discriminant, scope);
|
|
507
|
+
const savedBlockStart = blockStart;
|
|
508
|
+
blockStart = clock;
|
|
509
|
+
const inner = createScope(scope, site);
|
|
510
|
+
const statements = [];
|
|
511
|
+
for (const switchCase of node.cases || [])
|
|
512
|
+
statements.push(...(switchCase.consequent || []));
|
|
513
|
+
declareStatements(inner, statements);
|
|
514
|
+
for (const switchCase of node.cases || []) {
|
|
515
|
+
walk(switchCase.test, inner);
|
|
516
|
+
walk(switchCase.consequent, inner);
|
|
517
|
+
}
|
|
518
|
+
blockStart = savedBlockStart;
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
case 'CatchClause': {
|
|
522
|
+
const inner = createScope(scope, site);
|
|
523
|
+
declare(inner, patternNames(node.param, []));
|
|
524
|
+
declarationPattern(node.param, inner);
|
|
525
|
+
walk(node.body, inner);
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
case 'ForStatement': {
|
|
529
|
+
const inner = createScope(scope, site);
|
|
530
|
+
const head = node.init?.type === 'VariableDeclaration' && node.init.kind !== 'var';
|
|
531
|
+
const headNames = head ? lexicalNames([node.init], []) : [];
|
|
532
|
+
declare(inner, headNames);
|
|
533
|
+
const headBindings = headNames.map((name) => inner.bindings.get(name));
|
|
534
|
+
for (const binding of headBindings) binding.loop = node;
|
|
535
|
+
walk(node.init, inner);
|
|
536
|
+
frame(
|
|
537
|
+
clock,
|
|
538
|
+
() => {
|
|
539
|
+
walk(node.test, inner);
|
|
540
|
+
const savedLoopUpdate = loopUpdate;
|
|
541
|
+
const sitesBefore = siteCount;
|
|
542
|
+
loopUpdate = node;
|
|
543
|
+
walk(node.update, inner);
|
|
544
|
+
loopUpdate = savedLoopUpdate;
|
|
545
|
+
// A thunk created in the update clause shares the environment the
|
|
546
|
+
// update then writes.
|
|
547
|
+
if (siteCount !== sitesBefore) {
|
|
548
|
+
for (const binding of headBindings) binding.volatile = true;
|
|
549
|
+
}
|
|
550
|
+
walk(node.body, inner);
|
|
551
|
+
},
|
|
552
|
+
true,
|
|
553
|
+
);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
case 'ForInStatement':
|
|
557
|
+
case 'ForOfStatement':
|
|
558
|
+
walk(node.right, scope);
|
|
559
|
+
frame(
|
|
560
|
+
clock,
|
|
561
|
+
() => {
|
|
562
|
+
const inner = createScope(scope, site);
|
|
563
|
+
if (node.left?.type === 'VariableDeclaration') {
|
|
564
|
+
if (node.left.kind !== 'var') declare(inner, lexicalNames([node.left], []));
|
|
565
|
+
for (const declaration of node.left.declarations || []) {
|
|
566
|
+
declarationPattern(declaration.id, inner);
|
|
567
|
+
if (node.left.kind === 'var') writePattern(declaration.id, inner);
|
|
568
|
+
}
|
|
569
|
+
} else {
|
|
570
|
+
writePattern(node.left, inner);
|
|
571
|
+
}
|
|
572
|
+
walk(node.body, inner);
|
|
573
|
+
},
|
|
574
|
+
true,
|
|
575
|
+
);
|
|
576
|
+
return;
|
|
577
|
+
case 'WhileStatement':
|
|
578
|
+
case 'DoWhileStatement':
|
|
579
|
+
frame(
|
|
580
|
+
clock,
|
|
581
|
+
() => {
|
|
582
|
+
walk(node.test, scope);
|
|
583
|
+
walk(node.body, scope);
|
|
584
|
+
},
|
|
585
|
+
true,
|
|
586
|
+
);
|
|
587
|
+
return;
|
|
588
|
+
case 'VariableDeclaration':
|
|
589
|
+
variableDeclaration(node, scope);
|
|
590
|
+
return;
|
|
591
|
+
case 'AssignmentExpression':
|
|
592
|
+
// The target is written after the value evaluates.
|
|
593
|
+
if (node.operator !== '=' && node.left?.type === 'Identifier') read(node.left.name, scope);
|
|
594
|
+
walk(node.right, scope);
|
|
595
|
+
writePattern(node.left, scope);
|
|
596
|
+
return;
|
|
597
|
+
case 'UpdateExpression':
|
|
598
|
+
if (node.argument?.type === 'Identifier') {
|
|
599
|
+
read(node.argument.name, scope);
|
|
600
|
+
write(node.argument.name, scope);
|
|
601
|
+
} else {
|
|
602
|
+
walk(node.argument, scope);
|
|
603
|
+
}
|
|
604
|
+
return;
|
|
605
|
+
case 'MemberExpression':
|
|
606
|
+
case 'OptionalMemberExpression':
|
|
607
|
+
walk(node.object, scope);
|
|
608
|
+
if (node.computed) walk(node.property, scope);
|
|
609
|
+
return;
|
|
610
|
+
case 'Property':
|
|
611
|
+
if (node.computed) walk(node.key, scope);
|
|
612
|
+
walk(node.value, scope);
|
|
613
|
+
return;
|
|
614
|
+
case 'LabeledStatement':
|
|
615
|
+
walk(node.body, scope);
|
|
616
|
+
return;
|
|
617
|
+
case 'BreakStatement':
|
|
618
|
+
case 'ContinueStatement':
|
|
619
|
+
case 'MetaProperty':
|
|
620
|
+
case 'ImportDeclaration':
|
|
621
|
+
case 'ExportAllDeclaration':
|
|
622
|
+
return;
|
|
623
|
+
case 'ExportNamedDeclaration':
|
|
624
|
+
if (node.declaration) walk(node.declaration, scope);
|
|
625
|
+
else if (node.source == null) {
|
|
626
|
+
for (const specifier of node.specifiers || []) {
|
|
627
|
+
if (specifier.local?.type === 'Identifier') read(specifier.local.name, scope);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return;
|
|
631
|
+
case 'ExportDefaultDeclaration':
|
|
632
|
+
walk(node.declaration, scope);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
eachChild(node, (child) => walk(child, scope));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
walk(program, null);
|
|
639
|
+
|
|
640
|
+
let needed = false;
|
|
641
|
+
for (const entry of sites.values()) {
|
|
642
|
+
const captures = [];
|
|
643
|
+
for (const [name, { binding, anchor }] of entry.reads) {
|
|
644
|
+
if (entry.writes.has(name) || entry.conflicts.has(name)) continue;
|
|
645
|
+
if (binding.volatile || binding.lastWrite > anchor) captures.push(name);
|
|
646
|
+
}
|
|
647
|
+
entry.captures = captures;
|
|
648
|
+
if (captures.length > 0) needed = true;
|
|
649
|
+
}
|
|
650
|
+
return needed ? sites : null;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Walk 2: copy-on-write rewrite, tracking the same site boundaries as walk 1.
|
|
654
|
+
// `renames` is null outside a site and the site's name map inside one; user
|
|
655
|
+
// functions reset it and `live`, nested thunks keep them.
|
|
656
|
+
function rewrite(program, sites, snapshotName) {
|
|
657
|
+
let renames = null;
|
|
658
|
+
let live = false;
|
|
659
|
+
|
|
660
|
+
const within = (nextRenames, nextLive, visit) => {
|
|
661
|
+
const savedRenames = renames;
|
|
662
|
+
const savedLive = live;
|
|
663
|
+
renames = nextRenames;
|
|
664
|
+
live = nextLive;
|
|
665
|
+
try {
|
|
666
|
+
return visit();
|
|
667
|
+
} finally {
|
|
668
|
+
renames = savedRenames;
|
|
669
|
+
live = savedLive;
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
const children = (node) => {
|
|
674
|
+
let out = null;
|
|
675
|
+
for (const key in node) {
|
|
676
|
+
if (NON_RUNTIME_KEYS.has(key)) continue;
|
|
677
|
+
const child = node[key];
|
|
678
|
+
if (child === null || typeof child !== 'object') continue;
|
|
679
|
+
const mapped = visit(child);
|
|
680
|
+
if (mapped !== child) {
|
|
681
|
+
if (out === null) out = { ...node };
|
|
682
|
+
out[key] = mapped;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return out ?? node;
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
const thunk = (node) => {
|
|
689
|
+
if (renames !== null || live) return children(node);
|
|
690
|
+
const captures = sites.get(node)?.captures ?? [];
|
|
691
|
+
if (captures.length === 0) return within(new Map(), false, () => children(node));
|
|
692
|
+
const own = new Map(captures.map((name) => [name, snapshotName(name)]));
|
|
693
|
+
const body = within(own, false, () => children(node));
|
|
694
|
+
const origin = (created) =>
|
|
695
|
+
node.loc == null ? created : { ...created, start: node.start, end: node.end, loc: node.loc };
|
|
696
|
+
return origin(
|
|
697
|
+
b.call(
|
|
698
|
+
origin(
|
|
699
|
+
b.arrow(
|
|
700
|
+
captures.map((name) => origin(b.id(own.get(name)))),
|
|
701
|
+
body,
|
|
702
|
+
),
|
|
703
|
+
),
|
|
704
|
+
...captures.map((name) => origin(b.id(name))),
|
|
705
|
+
),
|
|
706
|
+
);
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
function visit(node) {
|
|
710
|
+
if (node == null || typeof node !== 'object') return node;
|
|
711
|
+
if (Array.isArray(node)) {
|
|
712
|
+
let out = null;
|
|
713
|
+
for (let index = 0; index < node.length; index++) {
|
|
714
|
+
const mapped = visit(node[index]);
|
|
715
|
+
if (out === null && mapped !== node[index]) out = node.slice(0, index);
|
|
716
|
+
if (out !== null) out.push(mapped);
|
|
717
|
+
}
|
|
718
|
+
return out ?? node;
|
|
719
|
+
}
|
|
720
|
+
const type = node.type;
|
|
721
|
+
if (typeof type !== 'string') return node;
|
|
722
|
+
if (node[DEFERRED_LIVE] === true && !live && renames === null) {
|
|
723
|
+
return within(null, true, () => visitNode(node, type));
|
|
724
|
+
}
|
|
725
|
+
return visitNode(node, type);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function visitNode(node, type) {
|
|
729
|
+
switch (type) {
|
|
730
|
+
case 'Identifier': {
|
|
731
|
+
const snapshot = renames?.get(node.name);
|
|
732
|
+
return snapshot === undefined ? node : { ...node, name: snapshot };
|
|
733
|
+
}
|
|
734
|
+
case 'ArrowFunctionExpression':
|
|
735
|
+
if (isDeferredThunk(node)) return thunk(node);
|
|
736
|
+
return within(null, false, () => children(node));
|
|
737
|
+
case 'FunctionDeclaration':
|
|
738
|
+
case 'FunctionExpression':
|
|
739
|
+
case 'StaticBlock':
|
|
740
|
+
return within(null, false, () => children(node));
|
|
741
|
+
case 'ClassDeclaration':
|
|
742
|
+
case 'ClassExpression': {
|
|
743
|
+
// A class keeps its own name; only its heritage and members are visited.
|
|
744
|
+
const superClass = visit(node.superClass);
|
|
745
|
+
const body = visit(node.body);
|
|
746
|
+
return superClass === node.superClass && body === node.body
|
|
747
|
+
? node
|
|
748
|
+
: { ...node, superClass, body };
|
|
749
|
+
}
|
|
750
|
+
case 'MethodDefinition':
|
|
751
|
+
case 'PropertyDefinition':
|
|
752
|
+
case 'AccessorProperty': {
|
|
753
|
+
const key = node.computed ? visit(node.key) : node.key;
|
|
754
|
+
const value = within(null, false, () => visit(node.value));
|
|
755
|
+
return key === node.key && value === node.value ? node : { ...node, key, value };
|
|
756
|
+
}
|
|
757
|
+
case 'MemberExpression':
|
|
758
|
+
case 'OptionalMemberExpression': {
|
|
759
|
+
const object = visit(node.object);
|
|
760
|
+
const property = node.computed ? visit(node.property) : node.property;
|
|
761
|
+
return object === node.object && property === node.property
|
|
762
|
+
? node
|
|
763
|
+
: { ...node, object, property };
|
|
764
|
+
}
|
|
765
|
+
case 'Property': {
|
|
766
|
+
const key = node.computed ? visit(node.key) : node.key;
|
|
767
|
+
const value = visit(node.value);
|
|
768
|
+
if (key === node.key && value === node.value) return node;
|
|
769
|
+
// `{ n }` becomes `{ n: n$ }`.
|
|
770
|
+
return { ...node, key, value, shorthand: false };
|
|
771
|
+
}
|
|
772
|
+
case 'LabeledStatement': {
|
|
773
|
+
const body = visit(node.body);
|
|
774
|
+
return body === node.body ? node : { ...node, body };
|
|
775
|
+
}
|
|
776
|
+
case 'BreakStatement':
|
|
777
|
+
case 'ContinueStatement':
|
|
778
|
+
case 'MetaProperty':
|
|
779
|
+
case 'ImportDeclaration':
|
|
780
|
+
case 'ExportAllDeclaration':
|
|
781
|
+
return node;
|
|
782
|
+
case 'ExportNamedDeclaration': {
|
|
783
|
+
const declaration = visit(node.declaration);
|
|
784
|
+
return declaration === node.declaration ? node : { ...node, declaration };
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return children(node);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
return visit(program);
|
|
791
|
+
}
|