what-compiler 0.14.0 → 0.15.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 +21 -0
- package/README.md +181 -2
- package/babel.d.ts +78 -2
- package/dist/babel/auto-selector.min.js +94 -0
- package/dist/babel/common.min.js +2053 -0
- package/dist/babel/dev-metadata.min.js +106 -0
- package/dist/babel/dom.min.js +1669 -0
- package/dist/babel/jsx-root.min.js +13 -0
- package/dist/babel/lists.min.js +492 -0
- package/dist/babel/nesting.min.js +328 -0
- package/dist/babel/reactivity.min.js +1218 -0
- package/dist/babel/server-actions.min.js +85 -0
- package/dist/babel/ssr.min.js +496 -0
- package/dist/babel/types.min.js +9 -0
- package/dist/babel-plugin.min.js +101 -1
- package/dist/file-router.min.js +368 -12
- package/dist/framework-files.min.js +62 -0
- package/dist/hot.min.js +315 -0
- package/dist/index.min.js +13 -414
- package/dist/release.min.js +78 -0
- package/dist/runtime.min.js +9 -1
- package/dist/tables/import-changes.min.js +272 -0
- package/dist/tables/markup-rules.min.js +213 -0
- package/dist/vite-plugin.min.js +432 -399
- package/file-router.d.ts +3 -1
- package/index.d.ts +1 -1
- package/package.json +11 -2
- package/runtime.d.ts +2 -1
- package/src/babel/auto-selector.js +248 -0
- package/src/babel/common.js +3800 -0
- package/src/babel/dev-metadata.js +167 -0
- package/src/babel/dom.js +3386 -0
- package/src/babel/jsx-root.js +27 -0
- package/src/babel/lists.js +704 -0
- package/src/babel/nesting.js +260 -0
- package/src/babel/reactivity.js +1998 -0
- package/src/babel/server-actions.js +144 -0
- package/src/babel/ssr.js +870 -0
- package/src/babel/types.js +10 -0
- package/src/babel-plugin.js +185 -3598
- package/src/file-router.js +58 -38
- package/src/framework-files.js +106 -0
- package/src/hot.js +523 -0
- package/src/release.js +127 -0
- package/src/runtime.js +7 -7
- package/src/tables/import-changes.js +157 -0
- package/src/tables/markup-rules.js +388 -0
- package/src/vite-plugin.js +460 -175
- package/vite.d.ts +47 -3
- package/src/error-overlay.js +0 -428
|
@@ -0,0 +1,1998 @@
|
|
|
1
|
+
import { t } from './types.js';
|
|
2
|
+
import { isWhatModuleSource } from './common.js';
|
|
3
|
+
|
|
4
|
+
// Known non-reactive call expressions — these should NOT be wrapped in effects
|
|
5
|
+
// unless their arguments contain signal reads.
|
|
6
|
+
const SAFE_GLOBAL_CALLS = new Set([
|
|
7
|
+
'Math', 'Number', 'String', 'Boolean', 'parseInt', 'parseFloat',
|
|
8
|
+
'isNaN', 'isFinite', 'encodeURIComponent', 'decodeURIComponent',
|
|
9
|
+
'encodeURI', 'decodeURI', 'JSON', 'Date', 'Array', 'Object',
|
|
10
|
+
'console', 'RegExp',
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
// Built-in prototype methods that convert or copy a value. A zero-argument call
|
|
14
|
+
// is otherwise assumed to be a signal read (see isPotentiallyReactive); these
|
|
15
|
+
// names are the ones JavaScript itself defines, so they never are, and they are
|
|
16
|
+
// common enough in JSX that wrapping them would be pure cost.
|
|
17
|
+
const PURE_BUILTIN_METHODS = new Set([
|
|
18
|
+
'toString', 'toLocaleString', 'valueOf', 'toJSON',
|
|
19
|
+
'toUpperCase', 'toLowerCase', 'toLocaleUpperCase', 'toLocaleLowerCase',
|
|
20
|
+
'trim', 'trimStart', 'trimEnd',
|
|
21
|
+
'toFixed', 'toPrecision', 'toExponential',
|
|
22
|
+
'toISOString', 'toUTCString', 'toDateString', 'toTimeString',
|
|
23
|
+
'toLocaleDateString', 'toLocaleTimeString',
|
|
24
|
+
'slice', 'reverse', 'sort', 'flat', 'concat', 'join',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
// Built-in methods that read only their receiver and their arguments, a
|
|
28
|
+
// callback argument included (see readsInCallback). Any other method of a value
|
|
29
|
+
// the compiler did not see built may read a signal inside (see callReadsInside).
|
|
30
|
+
const BUILTIN_METHODS = new Set([
|
|
31
|
+
'at', 'charAt', 'charCodeAt', 'codePointAt', 'includes', 'indexOf', 'lastIndexOf',
|
|
32
|
+
'startsWith', 'endsWith', 'padStart', 'padEnd', 'repeat', 'replace', 'replaceAll',
|
|
33
|
+
'split', 'substring', 'localeCompare', 'normalize', 'match', 'matchAll', 'search',
|
|
34
|
+
'map', 'filter', 'find', 'findIndex', 'findLast', 'findLastIndex', 'some', 'every',
|
|
35
|
+
'reduce', 'reduceRight', 'flatMap', 'forEach', 'toSorted', 'toReversed', 'toSpliced', 'with',
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
// Known signal-creating functions
|
|
39
|
+
const SIGNAL_CREATORS = new Set([
|
|
40
|
+
'useSignal', 'signal', 'computed', 'useComputed', 'useState', 'useReducer',
|
|
41
|
+
'createResource', 'useSWR', 'useQuery', 'useInfiniteQuery',
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
// =====================================================
|
|
45
|
+
// Reactivity Detection — Signal-Aware
|
|
46
|
+
// =====================================================
|
|
47
|
+
|
|
48
|
+
// Check if an identifier is known to be a signal (from useSignal/signal/computed/useState)
|
|
49
|
+
export function isSignalIdentifier(name, signalNames) {
|
|
50
|
+
return signalNames.has(name);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Collect signal identifiers using Babel's scope analysis.
|
|
54
|
+
// Walks the scope chain from the given path upward, collecting signals
|
|
55
|
+
// defined in each lexical scope (function/block).
|
|
56
|
+
export function collectSignalNamesFromScope(path) {
|
|
57
|
+
/** @type {Set<string> & { fromDestructuredProps?: Set<string>, propsObjects?: Set<string>, scope?: any, calls?: any[] }} */
|
|
58
|
+
const signalNames = new Set();
|
|
59
|
+
// Names that came from a DESTRUCTURED PROP rather than a signal() call.
|
|
60
|
+
//
|
|
61
|
+
// They belong in signalNames because they might hold an accessor and so
|
|
62
|
+
// must still be wrapped in an effect. They must NOT be auto-invoked: a prop
|
|
63
|
+
// is whatever the parent passed, and `_$createComponent` passes plain
|
|
64
|
+
// values, so `<span data-label={label}>` inside `({ label })` compiled to
|
|
65
|
+
// `setAttr(el, 'data-label', label())` and threw "label is not a function"
|
|
66
|
+
// on any ordinary string prop, blanking the whole component subtree.
|
|
67
|
+
//
|
|
68
|
+
// The same identifier as a CHILD compiled to `() => label`, uncalled, so
|
|
69
|
+
// the two positions disagreed inside a single element. The runtime setters
|
|
70
|
+
// (setAttr, setClass, setValue, ...) already resolve a function value
|
|
71
|
+
// reactively, so passing the identifier through uncalled is correct for
|
|
72
|
+
// both a plain value and an accessor.
|
|
73
|
+
const propNames = new Set();
|
|
74
|
+
signalNames.fromDestructuredProps = propNames;
|
|
75
|
+
// The props parameters of the functions around: `props` of `(props) => …`,
|
|
76
|
+
// and the one deferPlacedProps (common.js) made of a destructured pattern.
|
|
77
|
+
// A read `props.<key>` may be a live prop's getter (what-core dom.js
|
|
78
|
+
// _isLiveProp), so it is reactive (isPropsRead).
|
|
79
|
+
/** @type {Set<string>} */
|
|
80
|
+
const propsObjects = new Set();
|
|
81
|
+
signalNames.propsObjects = propsObjects;
|
|
82
|
+
// Where the names were collected: a call is resolved from here to the
|
|
83
|
+
// function it calls (see callReadsInside).
|
|
84
|
+
signalNames.scope = path.scope;
|
|
85
|
+
|
|
86
|
+
// Walk up the scope chain using Babel's scope API.
|
|
87
|
+
let scope = path.scope;
|
|
88
|
+
while (scope) {
|
|
89
|
+
const own = scopeNamesCache !== null ? scopeNamesCache.get(scope) : undefined;
|
|
90
|
+
const { signals, props, propsObject } = own || ownSignalNames(scope);
|
|
91
|
+
if (scopeNamesCache !== null && own === undefined) scopeNamesCache.set(scope, { signals, props, propsObject });
|
|
92
|
+
for (const name of signals) signalNames.add(name);
|
|
93
|
+
for (const name of props) propNames.add(name);
|
|
94
|
+
if (propsObject !== null) for (const name of propsObject) propsObjects.add(name);
|
|
95
|
+
scope = scope.parent;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return signalNames;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The names one scope declares that may hold an accessor: its signal-creating
|
|
102
|
+
// declarations (`signals`), and its function's destructured props (`props`,
|
|
103
|
+
// also in `signals`).
|
|
104
|
+
function ownSignalNames(scope) {
|
|
105
|
+
const signals = [];
|
|
106
|
+
const props = [];
|
|
107
|
+
|
|
108
|
+
// Helper: extract signal names from a VariableDeclarator node
|
|
109
|
+
function extractFromDeclarator(decl) {
|
|
110
|
+
const init = decl.init;
|
|
111
|
+
if (!init || !t.isCallExpression(init)) return;
|
|
112
|
+
|
|
113
|
+
const callee = init.callee;
|
|
114
|
+
let calleeName = '';
|
|
115
|
+
if (t.isIdentifier(callee)) {
|
|
116
|
+
calleeName = callee.name;
|
|
117
|
+
// `import { useState as _useState } from 'what-framework'` (what-react's
|
|
118
|
+
// translate mode writes its imports so): the name What exports.
|
|
119
|
+
const binding = scope.getBinding(callee.name);
|
|
120
|
+
const spec = binding && binding.kind === 'module' ? binding.path.node : null;
|
|
121
|
+
if (t.isImportSpecifier(spec) && t.isImportDeclaration(binding.path.parent) && isWhatModuleSource(binding.path.parent.source.value)) {
|
|
122
|
+
calleeName = t.isIdentifier(spec.imported) ? spec.imported.name : spec.imported.value;
|
|
123
|
+
}
|
|
124
|
+
} else if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
|
|
125
|
+
calleeName = callee.property.name;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (SIGNAL_CREATORS.has(calleeName)) {
|
|
129
|
+
const id = decl.id;
|
|
130
|
+
if (t.isIdentifier(id)) {
|
|
131
|
+
signals.push(id.name);
|
|
132
|
+
} else if (t.isArrayPattern(id)) {
|
|
133
|
+
for (const el of id.elements) {
|
|
134
|
+
if (t.isIdentifier(el)) signals.push(el.name);
|
|
135
|
+
}
|
|
136
|
+
} else if (t.isObjectPattern(id)) {
|
|
137
|
+
for (const prop of id.properties) {
|
|
138
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
139
|
+
signals.push(prop.value.name);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function extractFromPattern(param) {
|
|
147
|
+
if (!t.isObjectPattern(param)) return;
|
|
148
|
+
for (const prop of param.properties) {
|
|
149
|
+
if (t.isObjectProperty(prop) && t.isIdentifier(prop.value)) {
|
|
150
|
+
signals.push(prop.value.name);
|
|
151
|
+
props.push(prop.value.name);
|
|
152
|
+
} else if (t.isRestElement(prop) && t.isIdentifier(prop.argument)) {
|
|
153
|
+
signals.push(prop.argument.name);
|
|
154
|
+
props.push(prop.argument.name);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Check all variable bindings in this scope.
|
|
160
|
+
for (const binding of Object.values(scope.bindings)) {
|
|
161
|
+
if (binding.path.isVariableDeclarator()) {
|
|
162
|
+
extractFromDeclarator(binding.path.node);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Scan this scope's own function params (destructured props) ONCE per
|
|
166
|
+
// scope — not once per binding, which would make this O(params × bindings)
|
|
167
|
+
// per scope per JSXElement.
|
|
168
|
+
const fnNode = scope.path && scope.path.node;
|
|
169
|
+
if (fnNode) {
|
|
170
|
+
if (fnNode.params) {
|
|
171
|
+
for (const param of fnNode.params) extractFromPattern(param);
|
|
172
|
+
}
|
|
173
|
+
// deferPlacedProps moved this pattern out of the parameter list;
|
|
174
|
+
// it is still the same props destructuring and classifies the same way.
|
|
175
|
+
if (fnNode._whatMovedProps) extractFromPattern(fnNode._whatMovedProps);
|
|
176
|
+
}
|
|
177
|
+
// Its props parameter, and the rest elements of its props (common.js
|
|
178
|
+
// readRestsWhereUsed), which hold the same getters.
|
|
179
|
+
const objects = fnNode && fnNode._whatPropsParam ? [fnNode._whatPropsParam] : [];
|
|
180
|
+
if (fnNode && fnNode._whatPropsParam && fnNode._whatPropsRests) objects.push(...fnNode._whatPropsRests);
|
|
181
|
+
return { signals, props, propsObject: objects.length > 0 ? objects : null };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// A read of a component's props object (`props.on`, `props.user.name`): its
|
|
185
|
+
// root is the props parameter deferPlacedProps (common.js) marked, which the
|
|
186
|
+
// read's scope still binds to that parameter.
|
|
187
|
+
function isPropsRead(expr, signalNames) {
|
|
188
|
+
const objects = signalNames.propsObjects;
|
|
189
|
+
if (objects === undefined || objects.size === 0) return false;
|
|
190
|
+
let root = expr;
|
|
191
|
+
while (t.isMemberExpression(root) || t.isOptionalMemberExpression(root)) root = root.object;
|
|
192
|
+
if (!t.isIdentifier(root) || !objects.has(root.name)) return false;
|
|
193
|
+
const binding = bindingOf(root, signalNames.scope);
|
|
194
|
+
if (binding !== null && binding.path.node._whatPropsRest === true) return binding.constantViolations.length === 0;
|
|
195
|
+
return binding !== null && binding.kind === 'param' && binding.path.parentPath != null
|
|
196
|
+
&& binding.path.parentPath.node._whatPropsParam === root.name;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// A read off what a hook returned (`todos.remaining`, `query.data.title`,
|
|
200
|
+
// the callee of `form.field('email')`): its root names a declarator
|
|
201
|
+
// common.js markHookResults marked, which the read's scope still binds.
|
|
202
|
+
export function isHookResultRead(expr, signalNames) {
|
|
203
|
+
let root = expr;
|
|
204
|
+
while (t.isMemberExpression(root) || t.isOptionalMemberExpression(root)) root = root.object;
|
|
205
|
+
if (!t.isIdentifier(root)) return false;
|
|
206
|
+
const binding = bindingOf(root, signalNames.scope);
|
|
207
|
+
return binding !== null && binding.path.node._whatHookResult === true;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// A `collectSignalNames` wrapper lived here, commented "legacy wrapper for
|
|
211
|
+
// backward compat (used in collectSignalNames calls)". Nothing called it;
|
|
212
|
+
// every call site uses collectSignalNamesFromScope directly.
|
|
213
|
+
|
|
214
|
+
// `x.toUpperCase()` and friends, listed in PURE_BUILTIN_METHODS. Only the method
|
|
215
|
+
// name is checked: whether the RECEIVER is reactive is decided separately, so
|
|
216
|
+
// `user().name.trim()` stays reactive through its object.
|
|
217
|
+
function isPureBuiltinMethodCall(expr) {
|
|
218
|
+
return t.isMemberExpression(expr.callee)
|
|
219
|
+
&& !expr.callee.computed
|
|
220
|
+
&& t.isIdentifier(expr.callee.property)
|
|
221
|
+
&& PURE_BUILTIN_METHODS.has(expr.callee.property.name);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Check if a call expression is a safe (non-reactive) global call: the name
|
|
225
|
+
// must be the global itself, not a binding of the module that shadows it.
|
|
226
|
+
function isSafeGlobalCall(expr, fallbackScope) {
|
|
227
|
+
if (!t.isCallExpression(expr)) return false;
|
|
228
|
+
const callee = expr.callee;
|
|
229
|
+
// Math.max(), Number.parseInt(), etc., or parseInt(), isNaN(), etc.
|
|
230
|
+
const root = t.isMemberExpression(callee) ? callee.object : callee;
|
|
231
|
+
if (!t.isIdentifier(root) || !SAFE_GLOBAL_CALLS.has(root.name)) return false;
|
|
232
|
+
return !bindingOf(root, fallbackScope);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Check if an expression's reactivity is uncertain — e.g., a non-signal function call
|
|
236
|
+
// whose arguments happen to contain signal reads. The function itself may not produce
|
|
237
|
+
// a reactive result, so the compiler wraps it conservatively.
|
|
238
|
+
export function isUncertainReactive(expr, signalNames, importedIds) {
|
|
239
|
+
if (!signalNames) return false;
|
|
240
|
+
if (t.isCallExpression(expr) && !expr.callee._whatPlace) {
|
|
241
|
+
// Callee is a known signal — definitely reactive, not uncertain
|
|
242
|
+
if (t.isIdentifier(expr.callee) && isSignalIdentifier(expr.callee.name, signalNames)) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
// Imported identifier called as function — definitely reactive (not uncertain)
|
|
246
|
+
if (importedIds && t.isIdentifier(expr.callee) && importedIds.has(expr.callee.name) &&
|
|
247
|
+
!SAFE_GLOBAL_CALLS.has(expr.callee.name)) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
// Callee is a member of a known signal — definitely reactive
|
|
251
|
+
if (t.isMemberExpression(expr.callee) && t.isIdentifier(expr.callee.object) &&
|
|
252
|
+
isSignalIdentifier(expr.callee.object.name, signalNames)) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
// Safe global call (Math.max, etc.) with reactive args — still deterministic, not uncertain
|
|
256
|
+
if (isSafeGlobalCall(expr, signalNames.scope)) return false;
|
|
257
|
+
// Unknown function call — if args are reactive, the wrapping is uncertain
|
|
258
|
+
if (expr.arguments.some(arg => isPotentiallyReactive(arg, signalNames, importedIds))) {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Check if an expression is potentially reactive (reads a signal)
|
|
266
|
+
// importedIds: Set of identifiers imported from other modules — any imported
|
|
267
|
+
// function call is conservatively treated as potentially reactive since the
|
|
268
|
+
// imported binding could be a signal from another file.
|
|
269
|
+
export function isPotentiallyReactive(expr, signalNames, importedIds) {
|
|
270
|
+
if (!signalNames) signalNames = new Set();
|
|
271
|
+
|
|
272
|
+
// A condition or a text a lowered return reads live (common.js
|
|
273
|
+
// lowerLiveReturns), which may read state this walk cannot see made.
|
|
274
|
+
if (expr?._whatLive === true) return true;
|
|
275
|
+
|
|
276
|
+
// A placing read of a prop (common.js placingRead) is the property read.
|
|
277
|
+
// Off a name (the props, a rest element of them) the place itself follows a
|
|
278
|
+
// live prop (what-core dom.js _place), so the read needs no binding.
|
|
279
|
+
if (t.isCallExpression(expr) && expr.callee._whatPlace) return !t.isIdentifier(expr.arguments[0]) && isPotentiallyReactive(expr.arguments[0], signalNames, importedIds);
|
|
280
|
+
|
|
281
|
+
if (t.isCallExpression(expr) || t.isOptionalCallExpression(expr)) {
|
|
282
|
+
// If callee is a known signal identifier being called (signal read), it's reactive
|
|
283
|
+
if (t.isIdentifier(expr.callee) && isSignalIdentifier(expr.callee.name, signalNames)) {
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
// A selector lookup the auto-selector wrote (see lowerRowSelectors), or a
|
|
287
|
+
// keyed row's read of its item (see readItemThroughAccessor).
|
|
288
|
+
if (expr.callee._whatSelector || expr.callee._whatRowItem) return true;
|
|
289
|
+
// Imported identifier called as a function — conservatively reactive.
|
|
290
|
+
// Handles: import { count } from './store'; ... {count()} in JSX
|
|
291
|
+
if (importedIds && t.isIdentifier(expr.callee) && importedIds.has(expr.callee.name) &&
|
|
292
|
+
!SAFE_GLOBAL_CALLS.has(expr.callee.name)) {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
// member.call() — e.g., data(), isLoading()
|
|
296
|
+
if ((t.isMemberExpression(expr.callee) || t.isOptionalMemberExpression(expr.callee)) &&
|
|
297
|
+
t.isIdentifier(expr.callee.object) && isSignalIdentifier(expr.callee.object.name, signalNames)) {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
// Any other zero-argument call has the shape of an accessor read, so it
|
|
301
|
+
// is assumed to be one. `props.count()`, a plain parameter `count()` and
|
|
302
|
+
// `s()` from `const s = useThing()` are all opaque to the compiler: it
|
|
303
|
+
// knows only the names it watched being created. Assuming the narrow way
|
|
304
|
+
// round meant those three froze silently while the docs promised
|
|
305
|
+
// `{count()}` was reactive. Assuming this way round costs, when the call
|
|
306
|
+
// turns out to be constant, one effect that runs once and never again.
|
|
307
|
+
if (expr.arguments.length === 0 && !isPureBuiltinMethodCall(expr)) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
if (isPotentiallyReactive(expr.callee, signalNames, importedIds) ||
|
|
311
|
+
expr.arguments.some(arg => isPotentiallyReactive(arg, signalNames, importedIds))) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
// Everything else the call reaches (see callReads).
|
|
315
|
+
return callReads(expr, signalNames, importedIds);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (t.isIdentifier(expr)) {
|
|
319
|
+
return isSignalIdentifier(expr.name, signalNames) ||
|
|
320
|
+
(importedIds && importedIds.has(expr.name));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// A property read: reactive through its object or its key, and where the
|
|
324
|
+
// module declares the object (an object literal, an instance of a class it
|
|
325
|
+
// declares) and the read may run an accessor of it. A read of a value the
|
|
326
|
+
// compiler did not see built (a prop, a parameter) is a plain read.
|
|
327
|
+
if (t.isMemberExpression(expr) || t.isOptionalMemberExpression(expr)) {
|
|
328
|
+
return isPropsRead(expr, signalNames) || isHookResultRead(expr, signalNames) ||
|
|
329
|
+
isPotentiallyReactive(expr.object, signalNames, importedIds) ||
|
|
330
|
+
(expr.computed && isPotentiallyReactive(expr.property, signalNames, importedIds)) ||
|
|
331
|
+
mayRunAccessor(expr, signalNames.scope, importedIds);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (t.isConditionalExpression(expr)) {
|
|
335
|
+
return isPotentiallyReactive(expr.test, signalNames, importedIds) ||
|
|
336
|
+
isPotentiallyReactive(expr.consequent, signalNames, importedIds) ||
|
|
337
|
+
isPotentiallyReactive(expr.alternate, signalNames, importedIds);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (t.isBinaryExpression(expr) || t.isLogicalExpression(expr)) {
|
|
341
|
+
return isPotentiallyReactive(expr.left, signalNames, importedIds) ||
|
|
342
|
+
isPotentiallyReactive(expr.right, signalNames, importedIds);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (t.isUnaryExpression(expr)) {
|
|
346
|
+
return isPotentiallyReactive(expr.argument, signalNames, importedIds);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (t.isAssignmentExpression(expr)) {
|
|
350
|
+
return isPotentiallyReactive(expr.right, signalNames, importedIds) ||
|
|
351
|
+
(t.isMemberExpression(expr.left) && (mayRunAccessor(expr.left, signalNames.scope, importedIds) || (expr.left.computed &&
|
|
352
|
+
isPotentiallyReactive(expr.left.property, signalNames, importedIds))));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (t.isSequenceExpression(expr)) {
|
|
356
|
+
return expr.expressions.some(e => isPotentiallyReactive(e, signalNames, importedIds));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (t.isTemplateLiteral(expr)) {
|
|
360
|
+
return expr.expressions.some(e => isPotentiallyReactive(e, signalNames, importedIds));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// A tagged template is a call of its tag: reactive unless what it reaches
|
|
364
|
+
// is proven static (see callReads).
|
|
365
|
+
if (t.isTaggedTemplateExpression(expr)) {
|
|
366
|
+
return isPotentiallyReactive(expr.tag, signalNames, importedIds) ||
|
|
367
|
+
expr.quasi.expressions.some(e => isPotentiallyReactive(e, signalNames, importedIds)) ||
|
|
368
|
+
exprReads(expr, signalNames, importedIds);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// A spread runs code: an iterator (`[...model]`), getters (`{...model}`).
|
|
372
|
+
// An array or object literal with one is reactive unless what the spread
|
|
373
|
+
// reaches is proven static.
|
|
374
|
+
if (t.isObjectExpression(expr)) {
|
|
375
|
+
return expr.properties.some(prop =>
|
|
376
|
+
t.isObjectProperty(prop) ? isPotentiallyReactive(prop.value, signalNames, importedIds) :
|
|
377
|
+
t.isSpreadElement(prop) && isPotentiallyReactive(prop.argument, signalNames, importedIds)
|
|
378
|
+
) || (expr.properties.some(prop => t.isSpreadElement(prop)) && exprReads(expr, signalNames, importedIds));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (t.isArrayExpression(expr)) {
|
|
382
|
+
return expr.elements.some(el => el && isPotentiallyReactive(t.isSpreadElement(el) ? el.argument : el, signalNames, importedIds)) ||
|
|
383
|
+
(expr.elements.some(el => t.isSpreadElement(el)) && exprReads(expr, signalNames, importedIds));
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (t.isArrowFunctionExpression(expr) || t.isFunctionExpression(expr)) {
|
|
387
|
+
// Function expressions are not reactive themselves — they're callbacks
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// --- What a call can read ---
|
|
395
|
+
//
|
|
396
|
+
// A call is static only when everything its evaluation can reach is proven
|
|
397
|
+
// static; anything the compiler cannot prove is assumed to read a signal,
|
|
398
|
+
// because a wrong guess that way costs one effect that runs once, and a wrong
|
|
399
|
+
// guess the other way freezes the text in silence. The proof is an abstract
|
|
400
|
+
// evaluation of the call, its arguments and the bodies of the functions it
|
|
401
|
+
// reaches, over what kind of value each expression is:
|
|
402
|
+
//
|
|
403
|
+
// P a primitive: reading it, converting it and its built-in methods run
|
|
404
|
+
// no app code
|
|
405
|
+
// A an array the code being read built from primitives
|
|
406
|
+
// B a built-in object it built (new Date(), new Intl.NumberFormat(), a
|
|
407
|
+
// RegExp literal): its methods are the platform's
|
|
408
|
+
// D an object literal it built of primitives, with no accessor, method or
|
|
409
|
+
// spread: plain data
|
|
410
|
+
// { f } a function the module declares (its path), proven by reading it
|
|
411
|
+
// { o } an object literal the module declares and keeps (every use reads
|
|
412
|
+
// a property by name, so none is replaced), whose members it can read
|
|
413
|
+
// { g } a built-in global the module does not shadow (Math, JSON, String)
|
|
414
|
+
// U anything else: a parameter's value nobody proved, what an unknown
|
|
415
|
+
// call returned, a class instance, `this` outside a method of a kept
|
|
416
|
+
// object literal
|
|
417
|
+
//
|
|
418
|
+
// Evaluating reads (marks the evaluation as reading) at every step that may
|
|
419
|
+
// run code the compiler did not see: a property read or write on U (a getter
|
|
420
|
+
// or setter), converting U to a primitive (valueOf, toString), iterating or
|
|
421
|
+
// spreading U, calling U or a method of U, constructing anything but a
|
|
422
|
+
// built-in, a global the platform does not define, `await`, `yield`, and
|
|
423
|
+
// every syntax form not listed below. A call of a function the module
|
|
424
|
+
// declares is an edge of the call graph, to that function read with its
|
|
425
|
+
// arguments' kinds (and `this`); an async function or a generator reads, since
|
|
426
|
+
// part of its body runs where the caller cannot see. A function's default
|
|
427
|
+
// parameters and destructured parameters are read with its body.
|
|
428
|
+
|
|
429
|
+
/** @typedef {'p' | 'a' | 'b' | 'd' | 'u' | { f: any } | { o: any } | { g: string }} Kind */
|
|
430
|
+
const P = 'p';
|
|
431
|
+
const A = 'a';
|
|
432
|
+
const B = 'b';
|
|
433
|
+
const D = 'd';
|
|
434
|
+
const U = 'u';
|
|
435
|
+
|
|
436
|
+
// Constructors of built-in objects whose methods take and give primitives.
|
|
437
|
+
const BUILTIN_OBJECTS = new Set(['Date', 'RegExp', 'Intl']);
|
|
438
|
+
// Other built-in constructors: they run no app code beyond what they are
|
|
439
|
+
// handed (an error's message, a promise's executor).
|
|
440
|
+
const BUILTIN_CONSTRUCTORS = new Set([
|
|
441
|
+
'Error', 'TypeError', 'RangeError', 'SyntaxError', 'ReferenceError', 'EvalError', 'URIError', 'AggregateError',
|
|
442
|
+
'Map', 'Set', 'WeakMap', 'WeakSet', 'Promise', 'URL', 'URLSearchParams',
|
|
443
|
+
]);
|
|
444
|
+
|
|
445
|
+
// Methods of a primitive (String.prototype, Number.prototype) beyond the
|
|
446
|
+
// two lists above, all the platform's.
|
|
447
|
+
const PRIMITIVE_METHODS = new Set([
|
|
448
|
+
...PURE_BUILTIN_METHODS, ...BUILTIN_METHODS,
|
|
449
|
+
'concat', 'isWellFormed', 'toWellFormed',
|
|
450
|
+
]);
|
|
451
|
+
|
|
452
|
+
// Methods of an array the code built: [primitive, ...].
|
|
453
|
+
const ARRAY_METHODS = new Set([
|
|
454
|
+
...PURE_BUILTIN_METHODS, ...BUILTIN_METHODS,
|
|
455
|
+
'keys', 'values', 'entries', 'fill', 'copyWithin', 'push', 'pop', 'shift', 'unshift', 'splice',
|
|
456
|
+
]);
|
|
457
|
+
// The array methods that change the array.
|
|
458
|
+
const MUTATING = new Set(['push', 'pop', 'shift', 'unshift', 'splice', 'fill', 'copyWithin', 'sort', 'reverse']);
|
|
459
|
+
// The array methods whose result is an array of the same elements.
|
|
460
|
+
const SAME_ELEMENTS = new Set(['slice', 'reverse', 'sort', 'flat', 'concat', 'filter', 'toSorted', 'toReversed', 'toSpliced', 'with', 'fill', 'copyWithin', 'splice']);
|
|
461
|
+
|
|
462
|
+
// Object.prototype's methods: what an object literal has without declaring.
|
|
463
|
+
const OBJECT_METHODS = new Set(['hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf']);
|
|
464
|
+
|
|
465
|
+
// The bindings, functions and declarators of the module, recorded before any
|
|
466
|
+
// JSX is lowered (see resolveCallees): what an identifier names, and where a
|
|
467
|
+
// function or a declaration stands.
|
|
468
|
+
/** @type {WeakMap<object, any>} */
|
|
469
|
+
let references = new WeakMap();
|
|
470
|
+
/** @type {WeakMap<object, any>} */
|
|
471
|
+
let functionPaths = new WeakMap();
|
|
472
|
+
/** @type {WeakMap<object, any>} */
|
|
473
|
+
let declaratorPaths = new WeakMap();
|
|
474
|
+
// What each call of the module reads, decided before any JSX is lowered.
|
|
475
|
+
/** @type {WeakMap<object, boolean>} */
|
|
476
|
+
let decided = new WeakMap();
|
|
477
|
+
|
|
478
|
+
// Whether `call`, at the top of a JSX expression, may read a signal that its
|
|
479
|
+
// callee and arguments do not show by their names.
|
|
480
|
+
function callReads(call, signalNames, importedIds) {
|
|
481
|
+
const known = decided.get(call);
|
|
482
|
+
if (known !== undefined) return known;
|
|
483
|
+
return exprReads(call, signalNames, importedIds);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Whether evaluating `expr` may read a signal (the evaluation below).
|
|
487
|
+
function exprReads(expr, signalNames, importedIds) {
|
|
488
|
+
const ev = evaluation(null, null, U, signalNames.scope, importedIds);
|
|
489
|
+
evalExpr(expr, ev);
|
|
490
|
+
return finish(ev);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Whether an evaluation, or a function it reached, reads. */
|
|
494
|
+
function finish(ev) {
|
|
495
|
+
if (ev.reads) return true;
|
|
496
|
+
for (const node of ev.edges) if (readsThrough(node)) return true;
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function evaluation(fn, outer, thisKind, scope, importedIds) {
|
|
501
|
+
return { fn, outer, thisKind, scope, importedIds, env: new Map(), params: new Map(), written: new Map(), reads: false, edges: [] };
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// --- Property reads ---
|
|
505
|
+
//
|
|
506
|
+
// A property read with no call (`{model.text}`, `{(c ? a : b).text}`,
|
|
507
|
+
// `{make().text}`) is reactive when reading it may run code the module
|
|
508
|
+
// declares: a getter of an object it builds, of a class it declares (an
|
|
509
|
+
// instance's or a static one), or anything the walk below cannot follow. The
|
|
510
|
+
// read's root is followed to every value it may hold, each with the path the
|
|
511
|
+
// read takes from it, through: the names it passes through (a variable's
|
|
512
|
+
// initializer and every value assigned to it later), `a || b`, `c ? a : b`,
|
|
513
|
+
// `(x, a)`, a property of an object or an element of an array the module
|
|
514
|
+
// builds, a destructured name (its initializer at the destructured path, or
|
|
515
|
+
// the default), the return values of a function the module declares (its
|
|
516
|
+
// parameters taking the call's arguments), and every value the module
|
|
517
|
+
// assigns to a property it reads. The read is static only when every value
|
|
518
|
+
// is one whose properties run no app code: a primitive, a function, a
|
|
519
|
+
// built-in object, or a value from outside the module. That last one is the
|
|
520
|
+
// documented rule: a prop's, a parameter's or an imported function's
|
|
521
|
+
// result's property is read once (docs/GOTCHAS.md section 2). The walk is a
|
|
522
|
+
// list of its own (a chain of thousands of names nests nothing), bounded by
|
|
523
|
+
// PROPERTY_STEPS values: past it, the read is reactive.
|
|
524
|
+
const PROPERTY_STEPS = 50000;
|
|
525
|
+
// The imports of the module a walk reads (an accessor's body that calls one reads).
|
|
526
|
+
let walkImports = null;
|
|
527
|
+
|
|
528
|
+
function mayRunAccessor(expr, scope, importedIds) {
|
|
529
|
+
walkImports = importedIds || null;
|
|
530
|
+
const path = [];
|
|
531
|
+
let root = expr;
|
|
532
|
+
while (t.isMemberExpression(root) || t.isOptionalMemberExpression(root)) {
|
|
533
|
+
path.unshift(propertyName(root));
|
|
534
|
+
root = root.object;
|
|
535
|
+
}
|
|
536
|
+
const pending = [{ node: root, scope, path, args: null }];
|
|
537
|
+
const seen = new Set();
|
|
538
|
+
const push = (item) => {
|
|
539
|
+
const key = `${idOf(item.node)}|${item.path.join('\u0000')}|${item.args === null ? 0 : idOf(item.args)}`;
|
|
540
|
+
if (seen.has(key)) return;
|
|
541
|
+
seen.add(key);
|
|
542
|
+
pending.push(item);
|
|
543
|
+
};
|
|
544
|
+
for (let steps = 0; pending.length > 0; steps++) {
|
|
545
|
+
if (steps > PROPERTY_STEPS) return true;
|
|
546
|
+
if (followValue(/** @type {any} */ (pending.pop()), push)) return true;
|
|
547
|
+
}
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// One value of a read's walk: `node` in `scope`, read along `path` (null for
|
|
552
|
+
// a key the code computes), `args` the arguments of the call whose function
|
|
553
|
+
// it stands in. True when reading it may run app code; otherwise its next
|
|
554
|
+
// values are pushed.
|
|
555
|
+
function followValue(item, push) {
|
|
556
|
+
const { node, scope, path, args } = item;
|
|
557
|
+
if (path.length === 0) return false;
|
|
558
|
+
const next = (n) => push({ node: n, scope, path, args });
|
|
559
|
+
switch (node.type) {
|
|
560
|
+
case 'Identifier': return followName(item, push);
|
|
561
|
+
case 'ObjectExpression': return followObject(item, push);
|
|
562
|
+
case 'ArrayExpression': return followArray(item, push);
|
|
563
|
+
case 'MemberExpression': case 'OptionalMemberExpression':
|
|
564
|
+
if (t.isSuper(node.object)) return true;
|
|
565
|
+
push({ node: node.object, scope, path: [propertyName(node), ...path], args });
|
|
566
|
+
return false;
|
|
567
|
+
case 'ConditionalExpression': next(node.consequent); next(node.alternate); return false;
|
|
568
|
+
case 'LogicalExpression': next(node.left); next(node.right); return false;
|
|
569
|
+
case 'SequenceExpression': next(node.expressions[node.expressions.length - 1]); return false;
|
|
570
|
+
case 'AssignmentExpression': next(node.right); return false;
|
|
571
|
+
case 'ParenthesizedExpression': case 'TSAsExpression': case 'TSSatisfiesExpression': case 'TSNonNullExpression': case 'TypeCastExpression':
|
|
572
|
+
next(node.expression);
|
|
573
|
+
return false;
|
|
574
|
+
case 'NewExpression':
|
|
575
|
+
return !builtinConstructor(node.callee, scope) &&
|
|
576
|
+
!(t.isIdentifier(node.callee) && !bindingOf(node.callee, scope) && BUILTIN_CONSTRUCTORS.has(node.callee.name));
|
|
577
|
+
case 'CallExpression': case 'OptionalCallExpression': return followCall(item, push);
|
|
578
|
+
case 'ClassExpression': return followStatic(node, item, push);
|
|
579
|
+
case 'StringLiteral': case 'NumericLiteral': case 'BooleanLiteral': case 'NullLiteral': case 'BigIntLiteral': case 'RegExpLiteral':
|
|
580
|
+
case 'TemplateLiteral': case 'UnaryExpression': case 'BinaryExpression': case 'UpdateExpression':
|
|
581
|
+
case 'ArrowFunctionExpression': case 'FunctionExpression':
|
|
582
|
+
return false;
|
|
583
|
+
default:
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function followName(item, push) {
|
|
589
|
+
const { node, scope, path, args } = item;
|
|
590
|
+
if (node.name === 'undefined' && !bindingOf(node, scope)) return false;
|
|
591
|
+
const binding = bindingOf(node, scope);
|
|
592
|
+
// A global (document, Math): the platform's.
|
|
593
|
+
if (binding === null) return false;
|
|
594
|
+
if (args !== null && args.has(binding)) {
|
|
595
|
+
const arg = args.get(binding);
|
|
596
|
+
if (arg === null) return false;
|
|
597
|
+
push({ ...arg, path });
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
// A prop or a parameter: read once.
|
|
601
|
+
if (binding.kind === 'param') return false;
|
|
602
|
+
// An import held under another name is as reactive as the import.
|
|
603
|
+
if (binding.kind === 'module') return true;
|
|
604
|
+
const at = binding.path;
|
|
605
|
+
if (at.isFunctionDeclaration()) return false;
|
|
606
|
+
if (at.isClassDeclaration()) return followStatic(at.node, item, push);
|
|
607
|
+
if (!at.isVariableDeclarator()) return true;
|
|
608
|
+
const loop = at.parentPath && at.parentPath.parentPath;
|
|
609
|
+
if (loop && (loop.isForOfStatement() || loop.isForInStatement()) && loop.node.left === at.parentPath.node) return true;
|
|
610
|
+
// A value whose members the module writes, or that it hands to code the
|
|
611
|
+
// walk does not follow, may hold anything.
|
|
612
|
+
if (opaque(binding)) return true;
|
|
613
|
+
if (t.isIdentifier(at.node.id)) {
|
|
614
|
+
if (at.node.init) push({ node: at.node.init, scope: at.scope, path, args: null });
|
|
615
|
+
} else {
|
|
616
|
+
// A destructured name: the initializer at its path in the pattern, or a
|
|
617
|
+
// default.
|
|
618
|
+
const found = patternPath(at.node.id, binding.identifier);
|
|
619
|
+
if (found === null || !at.node.init) return true;
|
|
620
|
+
push({ node: at.node.init, scope: at.scope, path: [...found.keys, ...path], args: null });
|
|
621
|
+
for (const { at: depth, node: value } of found.defaults) push({ node: value, scope: at.scope, path: [...found.keys.slice(depth), ...path], args: null });
|
|
622
|
+
}
|
|
623
|
+
for (const write of binding.constantViolations) {
|
|
624
|
+
if (write.isUpdateExpression()) continue;
|
|
625
|
+
if (!write.isAssignmentExpression() || !t.isIdentifier(write.node.left)) return true;
|
|
626
|
+
const op = write.node.operator;
|
|
627
|
+
if (op === '=' || op === '||=' || op === '&&=' || op === '??=') push({ node: write.node.right, scope: write.scope, path, args: null });
|
|
628
|
+
}
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Where `id` stands in the destructuring pattern `pattern`: the keys from the
|
|
633
|
+
// destructured value to it, and the defaults on the way ({ at, node }: the
|
|
634
|
+
// default given for the value after `at` keys). Null under a rest element or
|
|
635
|
+
// a computed key.
|
|
636
|
+
function patternPath(pattern, id) {
|
|
637
|
+
const keys = [];
|
|
638
|
+
const defaults = [];
|
|
639
|
+
const walk = (node) => {
|
|
640
|
+
if (node === id) return true;
|
|
641
|
+
if (t.isAssignmentPattern(node)) {
|
|
642
|
+
defaults.push({ at: keys.length, node: node.right });
|
|
643
|
+
if (walk(node.left)) return true;
|
|
644
|
+
defaults.pop();
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
if (t.isObjectPattern(node)) {
|
|
648
|
+
for (const prop of node.properties) {
|
|
649
|
+
if (t.isRestElement(prop)) {
|
|
650
|
+
if (walk(prop.argument)) return 'rest';
|
|
651
|
+
continue;
|
|
652
|
+
}
|
|
653
|
+
keys.push(prop.computed && !t.isStringLiteral(prop.key) && !t.isNumericLiteral(prop.key) ? null : keyName(prop));
|
|
654
|
+
const found = walk(prop.value);
|
|
655
|
+
if (found) return found;
|
|
656
|
+
keys.pop();
|
|
657
|
+
}
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
if (t.isArrayPattern(node)) {
|
|
661
|
+
for (let i = 0; i < node.elements.length; i++) {
|
|
662
|
+
const element = node.elements[i];
|
|
663
|
+
if (element === null) continue;
|
|
664
|
+
if (t.isRestElement(element)) {
|
|
665
|
+
if (walk(element.argument)) return 'rest';
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
keys.push(String(i));
|
|
669
|
+
const found = walk(element);
|
|
670
|
+
if (found) return found;
|
|
671
|
+
keys.pop();
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return false;
|
|
675
|
+
};
|
|
676
|
+
const found = walk(pattern);
|
|
677
|
+
return found === true && !keys.includes(null) ? { keys, defaults } : null;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function followObject(item, push) {
|
|
681
|
+
const { node, scope, path, args } = item;
|
|
682
|
+
const key = path[0];
|
|
683
|
+
// A method that writes its object may change any member.
|
|
684
|
+
for (const prop of node.properties) {
|
|
685
|
+
const fn = t.isObjectMethod(prop) ? prop : t.isObjectProperty(prop) && t.isFunctionExpression(prop.value) ? prop.value : null;
|
|
686
|
+
if (fn === null) continue;
|
|
687
|
+
const at = functionPaths.get(fn);
|
|
688
|
+
if (!at || !thisStaysPut(at)) return true;
|
|
689
|
+
}
|
|
690
|
+
if (key === null) {
|
|
691
|
+
for (const prop of node.properties) {
|
|
692
|
+
if (t.isSpreadElement(prop) || (t.isObjectMethod(prop) && prop.kind !== 'method')) return true;
|
|
693
|
+
if (t.isObjectProperty(prop)) push({ node: prop.value, scope, path: path.slice(1), args });
|
|
694
|
+
}
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
/** @type {any} */
|
|
698
|
+
let found = null;
|
|
699
|
+
for (const prop of node.properties) {
|
|
700
|
+
if (t.isSpreadElement(prop) || (prop.computed && !t.isStringLiteral(prop.key) && !t.isNumericLiteral(prop.key))) {
|
|
701
|
+
found = 'unknown';
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
if (keyName(prop) !== key) continue;
|
|
705
|
+
found = t.isObjectMethod(prop) && prop.kind === 'set' ? null : prop;
|
|
706
|
+
}
|
|
707
|
+
if (found === 'unknown') return true;
|
|
708
|
+
if (found !== null && typeof found === 'object') {
|
|
709
|
+
if (found.type === 'ObjectMethod') return found.kind === 'get' && followAccessor(found, item, push);
|
|
710
|
+
push({ node: found.value, scope, path: path.slice(1), args });
|
|
711
|
+
}
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// A getter the read runs: its body read as a call is (what it calls, reads
|
|
716
|
+
// and converts, `this` a value not proven), and what it returns followed
|
|
717
|
+
// along the rest of the read.
|
|
718
|
+
function followAccessor(fn, item, push) {
|
|
719
|
+
const at = functionPaths.get(fn);
|
|
720
|
+
if (!at) return true;
|
|
721
|
+
const ev = evaluation(null, null, U, at.scope, walkImports);
|
|
722
|
+
edge(at, [], U, ev);
|
|
723
|
+
if (finish(ev)) return true;
|
|
724
|
+
if (item.path.length > 1) pushReturns(at, item.path.slice(1), null, push);
|
|
725
|
+
return false;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// The values the function at `fn` returns, read along `path`.
|
|
729
|
+
function pushReturns(fn, path, args, push) {
|
|
730
|
+
const body = fn.node.body;
|
|
731
|
+
if (!t.isBlockStatement(body)) {
|
|
732
|
+
push({ node: body, scope: fn.scope, path, args });
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
fn.traverse({
|
|
736
|
+
ReturnStatement(ret) {
|
|
737
|
+
if (ret.node.argument) push({ node: ret.node.argument, scope: ret.scope, path, args });
|
|
738
|
+
},
|
|
739
|
+
Function(inner) {
|
|
740
|
+
inner.skip();
|
|
741
|
+
},
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function followArray(item, push) {
|
|
746
|
+
const { node, scope, path, args } = item;
|
|
747
|
+
const key = path[0];
|
|
748
|
+
if (key !== null && !/^\d+$/.test(key)) return false;
|
|
749
|
+
for (let i = 0; i < node.elements.length; i++) {
|
|
750
|
+
const element = node.elements[i];
|
|
751
|
+
if (t.isSpreadElement(element)) return true;
|
|
752
|
+
if (element !== null && (key === null || String(i) === key)) push({ node: element, scope, path: path.slice(1), args });
|
|
753
|
+
}
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// A class's own (static) member read: a static accessor, a static block, an
|
|
758
|
+
// inherited one (a superclass) or a computed key may run its code; a static
|
|
759
|
+
// property's value is followed.
|
|
760
|
+
function followStatic(cls, item, push) {
|
|
761
|
+
if (cls.superClass) return true;
|
|
762
|
+
const key = item.path[0];
|
|
763
|
+
for (const member of cls.body.body) {
|
|
764
|
+
if (t.isStaticBlock(member)) return true;
|
|
765
|
+
if (!member.static) continue;
|
|
766
|
+
if (member.computed) return true;
|
|
767
|
+
const name = t.isIdentifier(member.key) ? member.key.name : t.isStringLiteral(member.key) ? member.key.value : null;
|
|
768
|
+
if (key !== null && name !== key) continue;
|
|
769
|
+
if (t.isClassMethod(member) && member.kind === 'get' && key !== null) {
|
|
770
|
+
if (followAccessor(member, item, push)) return true;
|
|
771
|
+
} else if (t.isClassMethod(member) && member.kind !== 'method') return true;
|
|
772
|
+
if (t.isClassProperty(member) && member.value) push({ node: member.value, scope: item.scope, path: item.path.slice(1), args: null });
|
|
773
|
+
}
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// What a call returns: a function the module declares returns its return
|
|
778
|
+
// statements' values, its parameters taking the arguments; an async function
|
|
779
|
+
// or a generator returns a promise or an iterator (the platform's); a
|
|
780
|
+
// function from outside the module (an import, a global, a parameter)
|
|
781
|
+
// returns a value from outside, read once. A method call's result is
|
|
782
|
+
// followed no further unless its receiver is plain.
|
|
783
|
+
function followCall(item, push) {
|
|
784
|
+
const { node, scope, path } = item;
|
|
785
|
+
const callee = node.callee;
|
|
786
|
+
if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) {
|
|
787
|
+
let receiver = callee.object;
|
|
788
|
+
while (t.isMemberExpression(receiver) || t.isOptionalMemberExpression(receiver)) receiver = receiver.object;
|
|
789
|
+
if (t.isStringLiteral(receiver) || t.isTemplateLiteral(receiver) || t.isNumericLiteral(receiver)) return false;
|
|
790
|
+
if (!t.isIdentifier(receiver)) return true;
|
|
791
|
+
const binding = bindingOf(receiver, scope);
|
|
792
|
+
if (binding === null || binding.kind === 'param') return false;
|
|
793
|
+
const kind = declaredKind(binding);
|
|
794
|
+
return !(kind === P || kind === A || kind === B || kind === D);
|
|
795
|
+
}
|
|
796
|
+
if (!t.isIdentifier(callee)) return true;
|
|
797
|
+
const binding = bindingOf(callee, scope);
|
|
798
|
+
if (binding === null || binding.kind === 'module' || binding.kind === 'param') return false;
|
|
799
|
+
if (item.args !== null && item.args.has(binding)) return true;
|
|
800
|
+
const fn = declaredFunction(callee, scope);
|
|
801
|
+
if (fn === null) return true;
|
|
802
|
+
if (fn.node.async || fn.node.generator) return false;
|
|
803
|
+
/** @type {Map<any, any>} */
|
|
804
|
+
const args = new Map();
|
|
805
|
+
for (let i = 0; i < fn.node.params.length; i++) {
|
|
806
|
+
const param = fn.node.params[i];
|
|
807
|
+
if (!t.isIdentifier(param)) return true;
|
|
808
|
+
const own = fn.scope.getBinding(param.name);
|
|
809
|
+
const arg = node.arguments[i];
|
|
810
|
+
if (arg !== undefined && t.isSpreadElement(arg)) return true;
|
|
811
|
+
if (own) args.set(own, arg === undefined ? null : { node: arg, scope, args: item.args });
|
|
812
|
+
}
|
|
813
|
+
pushReturns(fn, path, args, push);
|
|
814
|
+
return false;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// Whether the value a variable holds may change under the walk: the module
|
|
818
|
+
// writes a member of it (`v.a = x`, `v.a.b = x`, `v[k] = x`, an array's
|
|
819
|
+
// mutator), or the value, or a member of it, goes where the walk does not
|
|
820
|
+
// follow it (handed to a function from outside the module, stored in another
|
|
821
|
+
// value). It is followed where it flows: through `c ? v : w`, `v || w`,
|
|
822
|
+
// `(x, v)`, into a name (`const w = v`, `w = v`, `const w = v.c`, a
|
|
823
|
+
// destructured name), into a parameter of a function the module declares
|
|
824
|
+
// that it is passed to, and out of a function that returns it, to each call
|
|
825
|
+
// of that function. Reading it, calling its methods, constructing or
|
|
826
|
+
// extending a class, converting and comparing it change nothing.
|
|
827
|
+
/** @type {WeakMap<object, boolean>} */
|
|
828
|
+
let opaqueBindings = new WeakMap();
|
|
829
|
+
function opaque(binding) {
|
|
830
|
+
const known = opaqueBindings.get(binding);
|
|
831
|
+
if (known !== undefined) return known;
|
|
832
|
+
let result = false;
|
|
833
|
+
const uses = [...binding.referencePaths];
|
|
834
|
+
const names = new Set([binding]);
|
|
835
|
+
const places = new Set();
|
|
836
|
+
// A holder already known to change nothing is not read again: walking a
|
|
837
|
+
// chain of names from its last one reads each name once.
|
|
838
|
+
const hold = (other) => {
|
|
839
|
+
if (!other || opaqueBindings.get(other) === true) {
|
|
840
|
+
result = true;
|
|
841
|
+
} else if (!names.has(other) && opaqueBindings.get(other) !== false) {
|
|
842
|
+
names.add(other);
|
|
843
|
+
uses.push(...other.referencePaths);
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
const flow = (path) => uses.push(path);
|
|
847
|
+
while (uses.length > 0 && !result) {
|
|
848
|
+
const use = /** @type {any} */ (uses.pop());
|
|
849
|
+
if (places.has(use.node)) continue;
|
|
850
|
+
places.add(use.node);
|
|
851
|
+
if (valueChanges(use, hold, flow)) result = true;
|
|
852
|
+
}
|
|
853
|
+
opaqueBindings.set(binding, result);
|
|
854
|
+
return result;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// Whether the use `ref` of a value may change it or hand it where it is not
|
|
858
|
+
// followed; `hold` takes a binding that holds it next (undefined: one that
|
|
859
|
+
// cannot be named), `flow` an expression whose value it is.
|
|
860
|
+
function valueChanges(ref, hold, flow) {
|
|
861
|
+
let use = ref;
|
|
862
|
+
let member = false;
|
|
863
|
+
for (;;) {
|
|
864
|
+
const parent = use.parentPath;
|
|
865
|
+
if (!parent) return true;
|
|
866
|
+
if ((parent.isMemberExpression() || parent.isOptionalMemberExpression()) && parent.node.object === use.node) {
|
|
867
|
+
if (isWritten(parent)) return true;
|
|
868
|
+
use = parent;
|
|
869
|
+
member = true;
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
if ((parent.isCallExpression() || parent.isOptionalCallExpression()) && parent.node.callee === use.node) {
|
|
873
|
+
return member && MUTATING.has(propertyName(use.node));
|
|
874
|
+
}
|
|
875
|
+
if ((parent.isConditionalExpression() && parent.node.test !== use.node) || parent.isLogicalExpression() ||
|
|
876
|
+
(parent.isSequenceExpression() && parent.node.expressions[parent.node.expressions.length - 1] === use.node) ||
|
|
877
|
+
parent.isParenthesizedExpression() || parent.isTSAsExpression() || parent.isTSSatisfiesExpression() || parent.isTSNonNullExpression()) {
|
|
878
|
+
use = parent;
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
break;
|
|
882
|
+
}
|
|
883
|
+
const parent = use.parentPath;
|
|
884
|
+
if (parent.isVariableDeclarator({ init: use.node })) {
|
|
885
|
+
for (const name of Object.keys(parent.getBindingIdentifiers())) hold(parent.scope.getBinding(name));
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
if (parent.isAssignmentExpression({ right: use.node })) {
|
|
889
|
+
if (parent.node.operator !== '=' || !t.isIdentifier(parent.node.left)) return true;
|
|
890
|
+
hold(parent.scope.getBinding(parent.node.left.name));
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
if ((parent.isCallExpression() || parent.isOptionalCallExpression()) && parent.node.callee !== use.node) {
|
|
894
|
+
const fn = declaredFunction(parent.node.callee, parent.scope);
|
|
895
|
+
const index = parent.node.arguments.indexOf(use.node);
|
|
896
|
+
if (fn === null || index === -1) return true;
|
|
897
|
+
const param = fn.node.params[index];
|
|
898
|
+
if (param === undefined) return false;
|
|
899
|
+
for (const name of Object.keys(t.getBindingIdentifiers(param))) hold(fn.scope.getBinding(name));
|
|
900
|
+
return false;
|
|
901
|
+
}
|
|
902
|
+
if (parent.isReturnStatement() || (parent.isArrowFunctionExpression() && parent.node.body === use.node)) {
|
|
903
|
+
const fn = parent.isReturnStatement() ? parent.getFunctionParent() : parent;
|
|
904
|
+
const name = fn && functionName(fn);
|
|
905
|
+
if (!name) return true;
|
|
906
|
+
for (const call of name.referencePaths) {
|
|
907
|
+
if (!call.parentPath.isCallExpression({ callee: call.node })) return true;
|
|
908
|
+
flow(call.parentPath);
|
|
909
|
+
}
|
|
910
|
+
return false;
|
|
911
|
+
}
|
|
912
|
+
if (parent.isNewExpression({ callee: use.node }) || (parent.isClass() && parent.node.superClass === use.node)) return false;
|
|
913
|
+
return !(parent.isBinaryExpression() || parent.isUnaryExpression() || parent.isTemplateLiteral() || parent.isJSXExpressionContainer() ||
|
|
914
|
+
parent.isIfStatement({ test: use.node }) || parent.isConditionalExpression({ test: use.node }) || parent.isSpreadElement() ||
|
|
915
|
+
parent.isExpressionStatement() || parent.isForOfStatement({ right: use.node }) || parent.isForInStatement({ right: use.node }) ||
|
|
916
|
+
parent.isSwitchStatement({ discriminant: use.node }));
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
// The binding that names the function at `fn` (its declaration, or the
|
|
920
|
+
// constant it initializes), or null.
|
|
921
|
+
function functionName(fn) {
|
|
922
|
+
if (fn.isFunctionDeclaration() && fn.node.id) return fn.scope.parent.getBinding(fn.node.id.name) || null;
|
|
923
|
+
const holder = fn.parentPath;
|
|
924
|
+
if (holder.isVariableDeclarator({ init: fn.node }) && t.isIdentifier(holder.node.id)) {
|
|
925
|
+
const binding = holder.scope.getBinding(holder.node.id.name);
|
|
926
|
+
return binding && binding.constant ? binding : null;
|
|
927
|
+
}
|
|
928
|
+
return null;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// The function the module declares that `callee` names, or null.
|
|
932
|
+
function declaredFunction(callee, scope) {
|
|
933
|
+
if (!t.isIdentifier(callee)) return null;
|
|
934
|
+
const binding = bindingOf(callee, scope);
|
|
935
|
+
if (!binding) return null;
|
|
936
|
+
if (binding.path.isFunctionDeclaration()) return binding.constant ? binding.path : null;
|
|
937
|
+
if (!binding.constant || !binding.path.isVariableDeclarator()) return null;
|
|
938
|
+
const init = binding.path.get('init');
|
|
939
|
+
return init.isArrowFunctionExpression() || init.isFunctionExpression() ? init : null;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function bindingOf(id, scope) {
|
|
943
|
+
const known = references.get(id);
|
|
944
|
+
if (known !== undefined) return known;
|
|
945
|
+
return scope ? scope.getBinding(id.name) || null : null;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function keyName(prop) {
|
|
949
|
+
if (t.isIdentifier(prop.key)) return prop.key.name;
|
|
950
|
+
if (t.isStringLiteral(prop.key)) return prop.key.value;
|
|
951
|
+
if (t.isNumericLiteral(prop.key)) return String(prop.key.value);
|
|
952
|
+
return null;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
function propertyName(member) {
|
|
956
|
+
if (!member.computed) return t.isIdentifier(member.property) ? member.property.name : null;
|
|
957
|
+
if (t.isStringLiteral(member.property)) return member.property.value;
|
|
958
|
+
if (t.isNumericLiteral(member.property)) return String(member.property.value);
|
|
959
|
+
return null;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// --- Kinds ---
|
|
963
|
+
|
|
964
|
+
function same(a, b) {
|
|
965
|
+
return a === b || (typeof a === 'object' && typeof b === 'object' &&
|
|
966
|
+
((a.f !== undefined && a.f === b.f) || (a.o !== undefined && a.o === b.o) || (a.g !== undefined && a.g === b.g)));
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
function join(a, b) {
|
|
970
|
+
return same(a, b) ? a : U;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const ids = new WeakMap();
|
|
974
|
+
let nextId = 0;
|
|
975
|
+
function idOf(object) {
|
|
976
|
+
let id = ids.get(object);
|
|
977
|
+
if (id === undefined) ids.set(object, id = ++nextId);
|
|
978
|
+
return id;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function kindKey(kind) {
|
|
982
|
+
if (typeof kind === 'string') return kind;
|
|
983
|
+
if (kind.f !== undefined) return 'f' + idOf(kind.f.node);
|
|
984
|
+
if (kind.o !== undefined) return 'o' + idOf(kind.o.identifier);
|
|
985
|
+
return 'g' + kind.g;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
// A value a built-in may convert, read or call without running app code.
|
|
989
|
+
function inert(kind) {
|
|
990
|
+
return kind === P || kind === A || kind === B || kind === D || (typeof kind === 'object' && kind.g !== undefined);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// --- The kind of what a binding names, from its declaration ---
|
|
994
|
+
//
|
|
995
|
+
// A declaration's kind depends on the kinds of the bindings its initializer
|
|
996
|
+
// names (`const b = a`), so working it out by recursion nests as deep as a
|
|
997
|
+
// chain of aliases, and a generated module of thousands overflows the stack.
|
|
998
|
+
// Each binding's kind is worked out once instead, on a stack of its own: a
|
|
999
|
+
// declaration that names a binding not worked out yet waits under it, and one
|
|
1000
|
+
// that names a binding already waiting (a cycle, possible only through a
|
|
1001
|
+
// temporal dead zone) takes U for it.
|
|
1002
|
+
|
|
1003
|
+
/** @type {WeakMap<object, Kind>} */
|
|
1004
|
+
let declaredKinds = new WeakMap();
|
|
1005
|
+
const waitingKinds = new Set();
|
|
1006
|
+
|
|
1007
|
+
/** @returns {Kind} */
|
|
1008
|
+
function declaredKind(binding) {
|
|
1009
|
+
const known = declaredKinds.get(binding);
|
|
1010
|
+
if (known !== undefined) return known;
|
|
1011
|
+
if (waitingKinds.has(binding)) return U;
|
|
1012
|
+
const stack = [binding];
|
|
1013
|
+
waitingKinds.add(binding);
|
|
1014
|
+
try {
|
|
1015
|
+
while (stack.length > 0) {
|
|
1016
|
+
const top = stack[stack.length - 1];
|
|
1017
|
+
let next = null;
|
|
1018
|
+
const kind = declarationKind(top, (other) => {
|
|
1019
|
+
const otherKind = declaredKinds.get(other);
|
|
1020
|
+
if (otherKind !== undefined) return otherKind;
|
|
1021
|
+
if (next === null && !waitingKinds.has(other)) next = other;
|
|
1022
|
+
return U;
|
|
1023
|
+
});
|
|
1024
|
+
if (next !== null) {
|
|
1025
|
+
stack.push(next);
|
|
1026
|
+
waitingKinds.add(next);
|
|
1027
|
+
} else {
|
|
1028
|
+
declaredKinds.set(top, kind);
|
|
1029
|
+
stack.pop();
|
|
1030
|
+
waitingKinds.delete(top);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
} finally {
|
|
1034
|
+
for (const waiting of stack) waitingKinds.delete(waiting);
|
|
1035
|
+
}
|
|
1036
|
+
return /** @type {Kind} */ (declaredKinds.get(binding));
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// The kind of `binding` given `lookup`, the kind of each binding its
|
|
1040
|
+
// declaration names.
|
|
1041
|
+
/** @returns {Kind} */
|
|
1042
|
+
function declarationKind(binding, lookup) {
|
|
1043
|
+
if (binding.kind === 'module' || binding.kind === 'param') return U;
|
|
1044
|
+
const path = binding.path;
|
|
1045
|
+
if (!binding.constant) return countedKind(binding, lookup);
|
|
1046
|
+
if (path.isFunctionDeclaration()) return { f: path };
|
|
1047
|
+
if (!path.isVariableDeclarator() || !t.isIdentifier(path.node.id)) return U;
|
|
1048
|
+
const init = path.get('init');
|
|
1049
|
+
if (!init.node) return P;
|
|
1050
|
+
if (init.isArrowFunctionExpression() || init.isFunctionExpression()) return { f: init };
|
|
1051
|
+
if (init.isObjectExpression()) return keepsItself(binding) ? { o: binding } : U;
|
|
1052
|
+
const kind = constKind(init.node, init.scope, lookup);
|
|
1053
|
+
return kind === A && !keepsElements(binding) ? U : kind;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
// A variable that is written again (a loop counter, a total) is a primitive
|
|
1057
|
+
// when it starts as one and every write gives it one by the rules of
|
|
1058
|
+
// constKind: `i++`, `i = 0`, `n += 2`.
|
|
1059
|
+
function countedKind(binding, lookup) {
|
|
1060
|
+
const path = binding.path;
|
|
1061
|
+
if (!path.isVariableDeclarator() || !t.isIdentifier(path.node.id)) return U;
|
|
1062
|
+
if (path.node.init && constKind(path.node.init, path.scope, lookup) !== P) return U;
|
|
1063
|
+
return binding.constantViolations.every((write) => write.isUpdateExpression() ||
|
|
1064
|
+
(write.isAssignmentExpression() && t.isIdentifier(write.node.left) && constKind(write.node.right, write.scope, lookup) === P)) ? P : U;
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// Whether the array `binding` names keeps the primitives it was declared
|
|
1068
|
+
// with: every use reads it, iterates it or spreads it, and none changes it.
|
|
1069
|
+
function keepsElements(binding) {
|
|
1070
|
+
return binding.referencePaths.every((ref) => {
|
|
1071
|
+
const parent = ref.parentPath;
|
|
1072
|
+
if (parent.isForOfStatement({ right: ref.node }) || parent.isSpreadElement()) return true;
|
|
1073
|
+
if (!parent.isMemberExpression({ object: ref.node }) || isWritten(parent)) return false;
|
|
1074
|
+
const use = parent.parentPath;
|
|
1075
|
+
return !(use.isCallExpression({ callee: parent.node }) && MUTATING.has(propertyName(parent.node)));
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// Whether the member expression at `member` is written, not read: assigned
|
|
1080
|
+
// (`o.x = v`, `o.x += v`), the target of a destructuring however deep
|
|
1081
|
+
// (`[o.x] = list`, `({ a: o.x } = v)`, `[...o.x] = list`, `[o.x = 1] = list`),
|
|
1082
|
+
// the variable of a for-in or for-of loop, updated or deleted.
|
|
1083
|
+
function isWritten(member) {
|
|
1084
|
+
let at = member;
|
|
1085
|
+
for (;;) {
|
|
1086
|
+
const parent = at.parentPath;
|
|
1087
|
+
if (!parent) return false;
|
|
1088
|
+
if (parent.isAssignmentExpression() || parent.isForInStatement() || parent.isForOfStatement()) return parent.node.left === at.node;
|
|
1089
|
+
if (parent.isUpdateExpression()) return true;
|
|
1090
|
+
if (parent.isUnaryExpression({ operator: 'delete' })) return true;
|
|
1091
|
+
if (parent.isArrayPattern() || parent.isRestElement() || (parent.isAssignmentPattern() && parent.node.left === at.node)) {
|
|
1092
|
+
at = parent;
|
|
1093
|
+
continue;
|
|
1094
|
+
}
|
|
1095
|
+
if (parent.isObjectProperty() && parent.node.value === at.node && parent.parentPath.isObjectPattern()) {
|
|
1096
|
+
at = parent.parentPath;
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
return false;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
// The kind of a constant's initializer, where it is built of literals,
|
|
1104
|
+
// operators, built-in objects and other such constants, given `lookup`, the
|
|
1105
|
+
// kind of each binding it names.
|
|
1106
|
+
function constKind(node, scope, lookup) {
|
|
1107
|
+
if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node) || t.isNullLiteral(node) || t.isBigIntLiteral(node)) return P;
|
|
1108
|
+
if (t.isTemplateLiteral(node)) return node.expressions.every((e) => constKind(e, scope, lookup) === P) ? P : U;
|
|
1109
|
+
if (t.isUnaryExpression(node)) return constKind(node.argument, scope, lookup) === P ? P : U;
|
|
1110
|
+
if (t.isBinaryExpression(node) || t.isLogicalExpression(node)) {
|
|
1111
|
+
return constKind(node.left, scope, lookup) === P && constKind(node.right, scope, lookup) === P ? P : U;
|
|
1112
|
+
}
|
|
1113
|
+
if (t.isConditionalExpression(node)) {
|
|
1114
|
+
return [node.test, node.consequent, node.alternate].every((e) => constKind(e, scope, lookup) === P) ? P : U;
|
|
1115
|
+
}
|
|
1116
|
+
if (t.isArrayExpression(node)) return node.elements.every((e) => e !== null && !t.isSpreadElement(e) && constKind(e, scope, lookup) === P) ? A : U;
|
|
1117
|
+
if (t.isRegExpLiteral(node)) return B;
|
|
1118
|
+
if (t.isNewExpression(node) && builtinConstructor(node.callee, scope) && node.arguments.every((e) => !t.isSpreadElement(e) && constKind(e, scope, lookup) === P)) return B;
|
|
1119
|
+
if (t.isCallExpression(node)) return builtArrayKind(node, scope, lookup);
|
|
1120
|
+
if (t.isIdentifier(node)) {
|
|
1121
|
+
if (node.name === 'undefined' || node.name === 'NaN' || node.name === 'Infinity') return bindingOf(node, scope) ? U : P;
|
|
1122
|
+
const binding = bindingOf(node, scope);
|
|
1123
|
+
return binding ? lookup(binding) : U;
|
|
1124
|
+
}
|
|
1125
|
+
return U;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// `Array.of(...)` of primitives, and `Array.from(x)` of a primitive array, a
|
|
1129
|
+
// string or `{ length }`, mapped by nothing or by a function expression whose
|
|
1130
|
+
// returned expression is a primitive built of its parameters by constKind's
|
|
1131
|
+
// rules (`(_, i) => i * 2`): an array of primitives.
|
|
1132
|
+
function builtArrayKind(node, scope, lookup) {
|
|
1133
|
+
const callee = node.callee;
|
|
1134
|
+
if (!t.isMemberExpression(callee) || callee.computed || !t.isIdentifier(callee.object, { name: 'Array' }) ||
|
|
1135
|
+
bindingOf(callee.object, scope) || node.arguments.some((e) => t.isSpreadElement(e))) return U;
|
|
1136
|
+
const method = callee.property.name;
|
|
1137
|
+
if (method === 'of') return node.arguments.every((e) => constKind(e, scope, lookup) === P) ? A : U;
|
|
1138
|
+
if (method !== 'from' || node.arguments.length === 0 || node.arguments.length > 2) return U;
|
|
1139
|
+
const [source, mapper] = node.arguments;
|
|
1140
|
+
const sized = t.isObjectExpression(source) && source.properties.length === 1 && t.isObjectProperty(source.properties[0]) &&
|
|
1141
|
+
!source.properties[0].computed && keyName(source.properties[0]) === 'length' && constKind(source.properties[0].value, scope, lookup) === P;
|
|
1142
|
+
if (!sized) {
|
|
1143
|
+
const kind = constKind(source, scope, lookup);
|
|
1144
|
+
if (kind !== A && kind !== P) return U;
|
|
1145
|
+
}
|
|
1146
|
+
if (mapper === undefined) return A;
|
|
1147
|
+
if (!t.isArrowFunctionExpression(mapper) || mapper.async || t.isBlockStatement(mapper.body) || !mapper.params.every((p) => t.isIdentifier(p))) return U;
|
|
1148
|
+
const path = functionPaths.get(mapper);
|
|
1149
|
+
if (!path) return U;
|
|
1150
|
+
const own = (binding) => (binding.kind === 'param' && binding.scope === path.scope ? P : lookup(binding));
|
|
1151
|
+
return constKind(mapper.body, path.scope, own) === P ? A : U;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
function builtinConstructor(callee, scope) {
|
|
1155
|
+
let root = callee;
|
|
1156
|
+
if (t.isMemberExpression(root) && !root.computed) root = root.object;
|
|
1157
|
+
return t.isIdentifier(root) && BUILTIN_OBJECTS.has(root.name) && !bindingOf(root, scope);
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// Whether the object literal `binding` is declared with keeps the members it
|
|
1161
|
+
// was declared with: every use reads one of its properties by name (none is
|
|
1162
|
+
// replaced, deleted, or handed to code that could), and each of its methods
|
|
1163
|
+
// uses `this` only to read or call a property by name.
|
|
1164
|
+
const kept = new WeakMap();
|
|
1165
|
+
function keepsItself(binding) {
|
|
1166
|
+
let answer = kept.get(binding);
|
|
1167
|
+
if (answer !== undefined) return answer;
|
|
1168
|
+
answer = binding.referencePaths.every((ref) => {
|
|
1169
|
+
const parent = ref.parentPath;
|
|
1170
|
+
return parent && parent.isMemberExpression({ object: ref.node }) && !parent.node.computed && !isWritten(parent);
|
|
1171
|
+
}) && binding.path.get('init.properties').every((prop) => {
|
|
1172
|
+
const fn = prop.isObjectMethod() ? prop : prop.isObjectProperty() && prop.get('value').isFunctionExpression() ? prop.get('value') : null;
|
|
1173
|
+
return fn === null || thisStaysPut(fn);
|
|
1174
|
+
});
|
|
1175
|
+
kept.set(binding, answer);
|
|
1176
|
+
return answer;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const stayPut = new WeakMap();
|
|
1180
|
+
function thisStaysPut(fn) {
|
|
1181
|
+
const known = stayPut.get(fn.node);
|
|
1182
|
+
if (known !== undefined) return known;
|
|
1183
|
+
let ok = true;
|
|
1184
|
+
fn.traverse({
|
|
1185
|
+
ThisExpression(path) {
|
|
1186
|
+
const parent = path.parentPath;
|
|
1187
|
+
if (!parent.isMemberExpression({ object: path.node }) || parent.node.computed || isWritten(parent)) ok = false;
|
|
1188
|
+
},
|
|
1189
|
+
Function(path) {
|
|
1190
|
+
if (!path.isArrowFunctionExpression()) path.skip();
|
|
1191
|
+
},
|
|
1192
|
+
});
|
|
1193
|
+
stayPut.set(fn.node, ok);
|
|
1194
|
+
return ok;
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// What reading member `name` of the kept object literal `binding` finds:
|
|
1198
|
+
// { value } for a property or a method (the path of its value), { get } or
|
|
1199
|
+
// { set } for an accessor, null when a spread or a computed key may give it
|
|
1200
|
+
// another, and { inherited } when the literal declares no such member.
|
|
1201
|
+
function memberOf(binding, name, write) {
|
|
1202
|
+
/** @type {any} */
|
|
1203
|
+
let found = { inherited: true };
|
|
1204
|
+
for (const prop of binding.path.get('init.properties')) {
|
|
1205
|
+
if (prop.isSpreadElement() || prop.node.computed) {
|
|
1206
|
+
found = null;
|
|
1207
|
+
continue;
|
|
1208
|
+
}
|
|
1209
|
+
if (keyName(prop.node) !== name) continue;
|
|
1210
|
+
if (prop.isObjectMethod()) {
|
|
1211
|
+
if (prop.node.kind === 'method') found = { value: prop };
|
|
1212
|
+
else if ((prop.node.kind === 'set') === write) found = prop.node.kind === 'set' ? { set: prop } : { get: prop };
|
|
1213
|
+
} else {
|
|
1214
|
+
found = { value: prop.get('value') };
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
return found;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
// --- Evaluation ---
|
|
1221
|
+
//
|
|
1222
|
+
// The evaluation recurses as deep as the expressions it reads, and a binding
|
|
1223
|
+
// written from another one is read inside the evaluation that meets it, so a
|
|
1224
|
+
// chain of variables each written from the last with nested arithmetic nests
|
|
1225
|
+
// both at once. Past EVAL_DEPTH nested expressions the evaluation stops and
|
|
1226
|
+
// reads: the expression is reactive.
|
|
1227
|
+
const EVAL_DEPTH = 600;
|
|
1228
|
+
let evalDepth = 0;
|
|
1229
|
+
|
|
1230
|
+
/** @returns {Kind} */
|
|
1231
|
+
function evalExpr(e, ev) {
|
|
1232
|
+
if (e == null) return P;
|
|
1233
|
+
if (evalDepth >= EVAL_DEPTH) {
|
|
1234
|
+
ev.reads = true;
|
|
1235
|
+
return U;
|
|
1236
|
+
}
|
|
1237
|
+
evalDepth++;
|
|
1238
|
+
try {
|
|
1239
|
+
return evalNode(e, ev);
|
|
1240
|
+
} finally {
|
|
1241
|
+
evalDepth--;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
/** @returns {Kind} */
|
|
1246
|
+
function evalNode(e, ev) {
|
|
1247
|
+
switch (e.type) {
|
|
1248
|
+
case 'StringLiteral': case 'NumericLiteral': case 'BooleanLiteral': case 'NullLiteral': case 'BigIntLiteral':
|
|
1249
|
+
return P;
|
|
1250
|
+
case 'RegExpLiteral':
|
|
1251
|
+
return B;
|
|
1252
|
+
case 'TemplateLiteral':
|
|
1253
|
+
for (const x of e.expressions) convert(evalExpr(x, ev), ev);
|
|
1254
|
+
return P;
|
|
1255
|
+
case 'Identifier':
|
|
1256
|
+
return identKind(e, ev);
|
|
1257
|
+
case 'ThisExpression':
|
|
1258
|
+
return ev.thisKind;
|
|
1259
|
+
case 'ParenthesizedExpression': case 'TSAsExpression': case 'TSSatisfiesExpression': case 'TSNonNullExpression': case 'TypeCastExpression': case 'TSTypeAssertion':
|
|
1260
|
+
return evalExpr(e.expression, ev);
|
|
1261
|
+
case 'SequenceExpression': {
|
|
1262
|
+
/** @type {Kind} */
|
|
1263
|
+
let kind = P;
|
|
1264
|
+
for (const x of e.expressions) kind = evalExpr(x, ev);
|
|
1265
|
+
return kind;
|
|
1266
|
+
}
|
|
1267
|
+
case 'ArrayExpression': {
|
|
1268
|
+
let primitives = true;
|
|
1269
|
+
for (const x of e.elements) {
|
|
1270
|
+
if (x === null) continue;
|
|
1271
|
+
if (t.isSpreadElement(x)) {
|
|
1272
|
+
iterate(evalExpr(x.argument, ev), ev);
|
|
1273
|
+
primitives = false;
|
|
1274
|
+
} else if (evalExpr(x, ev) !== P) {
|
|
1275
|
+
primitives = false;
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
return primitives ? A : U;
|
|
1279
|
+
}
|
|
1280
|
+
case 'ObjectExpression': {
|
|
1281
|
+
let data = true;
|
|
1282
|
+
for (const prop of e.properties) {
|
|
1283
|
+
if (t.isSpreadElement(prop)) {
|
|
1284
|
+
spread(evalExpr(prop.argument, ev), ev);
|
|
1285
|
+
data = false;
|
|
1286
|
+
} else {
|
|
1287
|
+
if (prop.computed) convert(evalExpr(prop.key, ev), ev);
|
|
1288
|
+
if (!t.isObjectProperty(prop) || evalExpr(prop.value, ev) !== P) data = false;
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
return data ? D : U;
|
|
1292
|
+
}
|
|
1293
|
+
case 'ArrowFunctionExpression': case 'FunctionExpression': {
|
|
1294
|
+
const path = functionPaths.get(e);
|
|
1295
|
+
return path ? { f: path } : U;
|
|
1296
|
+
}
|
|
1297
|
+
case 'UnaryExpression': {
|
|
1298
|
+
const kind = evalExpr(e.argument, ev);
|
|
1299
|
+
if (e.operator === '-' || e.operator === '+' || e.operator === '~') convert(kind, ev);
|
|
1300
|
+
return P;
|
|
1301
|
+
}
|
|
1302
|
+
case 'BinaryExpression': {
|
|
1303
|
+
const left = t.isPrivateName(e.left) ? P : evalExpr(e.left, ev);
|
|
1304
|
+
const right = evalExpr(e.right, ev);
|
|
1305
|
+
if (e.operator === '===' || e.operator === '!==') return P;
|
|
1306
|
+
if (e.operator === 'in') {
|
|
1307
|
+
if (right === U) ev.reads = true;
|
|
1308
|
+
} else if (e.operator === 'instanceof') {
|
|
1309
|
+
if (typeof right !== 'object' || /** @type {any} */ (right).g === undefined) ev.reads = true;
|
|
1310
|
+
} else {
|
|
1311
|
+
convert(left, ev);
|
|
1312
|
+
convert(right, ev);
|
|
1313
|
+
}
|
|
1314
|
+
return P;
|
|
1315
|
+
}
|
|
1316
|
+
case 'LogicalExpression':
|
|
1317
|
+
return join(evalExpr(e.left, ev), evalExpr(e.right, ev));
|
|
1318
|
+
case 'ConditionalExpression':
|
|
1319
|
+
evalExpr(e.test, ev);
|
|
1320
|
+
return join(evalExpr(e.consequent, ev), evalExpr(e.alternate, ev));
|
|
1321
|
+
case 'AssignmentExpression':
|
|
1322
|
+
return assign(e, ev);
|
|
1323
|
+
case 'UpdateExpression':
|
|
1324
|
+
if (t.isIdentifier(e.argument)) convert(evalExpr(e.argument, ev), ev);
|
|
1325
|
+
else if (t.isMemberExpression(e.argument)) {
|
|
1326
|
+
convert(memberRead(e.argument, ev), ev);
|
|
1327
|
+
write(memberObject(e.argument, ev), ev);
|
|
1328
|
+
} else ev.reads = true;
|
|
1329
|
+
return P;
|
|
1330
|
+
case 'MemberExpression': case 'OptionalMemberExpression':
|
|
1331
|
+
return memberRead(e, ev);
|
|
1332
|
+
case 'CallExpression': case 'OptionalCallExpression':
|
|
1333
|
+
return call(e, ev);
|
|
1334
|
+
case 'NewExpression':
|
|
1335
|
+
return construct(e, ev);
|
|
1336
|
+
case 'TaggedTemplateExpression': {
|
|
1337
|
+
const args = [A, ...e.quasi.expressions.map((x) => evalExpr(x, ev))];
|
|
1338
|
+
if (t.isMemberExpression(e.tag)) return methodCall(evalExpr(e.tag.object, ev), propertyName(e.tag), args, ev);
|
|
1339
|
+
return callValue(evalExpr(e.tag, ev), args, U, ev);
|
|
1340
|
+
}
|
|
1341
|
+
// JSX the body builds tracks its own holes once it is lowered.
|
|
1342
|
+
case 'JSXElement': case 'JSXFragment':
|
|
1343
|
+
return U;
|
|
1344
|
+
default:
|
|
1345
|
+
ev.reads = true;
|
|
1346
|
+
return U;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/** @returns {Kind} */
|
|
1351
|
+
function identKind(id, ev) {
|
|
1352
|
+
const binding = bindingOf(id, ev.scope);
|
|
1353
|
+
if (binding === null) {
|
|
1354
|
+
if (id.name === 'undefined' || id.name === 'NaN' || id.name === 'Infinity') return P;
|
|
1355
|
+
if (SAFE_GLOBAL_CALLS.has(id.name) || BUILTIN_OBJECTS.has(id.name)) return { g: id.name };
|
|
1356
|
+
// A global the platform does not define may be a getter of globalThis.
|
|
1357
|
+
ev.reads = true;
|
|
1358
|
+
return U;
|
|
1359
|
+
}
|
|
1360
|
+
for (let at = ev; at !== null; at = at.outer) {
|
|
1361
|
+
const kind = at.env.get(binding);
|
|
1362
|
+
if (kind !== undefined) return kind;
|
|
1363
|
+
}
|
|
1364
|
+
if (!binding.constant) {
|
|
1365
|
+
const around = binding.scope.getFunctionParent();
|
|
1366
|
+
for (let at = ev; at !== null; at = at.outer) {
|
|
1367
|
+
if (around !== null && at.fn !== null && at.fn.node === around.block) return writtenKind(binding, at);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
return declaredKind(binding);
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
// The kind of a variable of the function `ev` reads that is written again:
|
|
1374
|
+
// what it starts with (its initializer, or the value of its parameter) joined
|
|
1375
|
+
// with every value written to it, each of a kind that may depend on the
|
|
1376
|
+
// variable's own (taken to be what it starts with while it is worked out: a
|
|
1377
|
+
// variable only ever given primitives is a primitive). A write the join
|
|
1378
|
+
// cannot see (a destructuring, a loop's left side) makes it U. A variable
|
|
1379
|
+
// written from one that is itself written again nests the work, so past
|
|
1380
|
+
// WRITTEN_DEPTH such variables the rest are U and the evaluation reads:
|
|
1381
|
+
// handwritten code nests a few, a generated chain of thousands would
|
|
1382
|
+
// overflow the stack.
|
|
1383
|
+
const WRITTEN_DEPTH = 200;
|
|
1384
|
+
let writtenDepth = 0;
|
|
1385
|
+
function writtenKind(binding, ev) {
|
|
1386
|
+
if (ev.written.has(binding)) return ev.written.get(binding);
|
|
1387
|
+
if (writtenDepth >= WRITTEN_DEPTH) {
|
|
1388
|
+
ev.reads = true;
|
|
1389
|
+
return U;
|
|
1390
|
+
}
|
|
1391
|
+
writtenDepth++;
|
|
1392
|
+
try {
|
|
1393
|
+
return writtenKindOf(binding, ev);
|
|
1394
|
+
} finally {
|
|
1395
|
+
writtenDepth--;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
function writtenKindOf(binding, ev) {
|
|
1400
|
+
const path = binding.path;
|
|
1401
|
+
/** @type {Kind} */
|
|
1402
|
+
let kind = U;
|
|
1403
|
+
if (binding.kind === 'param') kind = ev.params.get(binding) ?? U;
|
|
1404
|
+
else if (path.isVariableDeclarator() && t.isIdentifier(path.node.id)) kind = path.node.init ? kindOf(path.node.init, ev) : P;
|
|
1405
|
+
ev.written.set(binding, kind);
|
|
1406
|
+
for (const write of binding.constantViolations) {
|
|
1407
|
+
if (write.isUpdateExpression()) continue;
|
|
1408
|
+
if (!write.isAssignmentExpression() || !t.isIdentifier(write.node.left)) {
|
|
1409
|
+
kind = U;
|
|
1410
|
+
break;
|
|
1411
|
+
}
|
|
1412
|
+
const op = write.node.operator;
|
|
1413
|
+
kind = join(kind, op === '=' || op === '||=' || op === '&&=' || op === '??=' ? kindOf(write.node.right, ev) : P);
|
|
1414
|
+
ev.written.set(binding, kind);
|
|
1415
|
+
}
|
|
1416
|
+
ev.written.set(binding, kind);
|
|
1417
|
+
return kind;
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
// The kind of `expr`, without what evaluating it reads: the evaluation reads
|
|
1421
|
+
// it where it stands.
|
|
1422
|
+
function kindOf(expr, ev) {
|
|
1423
|
+
return evalExpr(expr, { ...ev, reads: false, edges: [] });
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
// Converting a value to a primitive runs its valueOf or toString.
|
|
1427
|
+
function convert(kind, ev) {
|
|
1428
|
+
if (kind === U) ev.reads = true;
|
|
1429
|
+
else if (typeof kind === 'object' && kind.o !== undefined) {
|
|
1430
|
+
for (const name of ['valueOf', 'toString']) {
|
|
1431
|
+
const member = memberOf(kind.o, name, false);
|
|
1432
|
+
if (member === null || member.inherited !== true) ev.reads = true;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
function iterate(kind, ev) {
|
|
1438
|
+
if (kind !== P && kind !== A && kind !== D) ev.reads = true;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
// Spreading an object runs its getters.
|
|
1442
|
+
function spread(kind, ev) {
|
|
1443
|
+
if (kind === U) ev.reads = true;
|
|
1444
|
+
else if (typeof kind === 'object' && kind.o !== undefined) {
|
|
1445
|
+
if (kind.o.path.node.init.properties.some((prop) => t.isSpreadElement(prop) || prop.computed || (t.isObjectMethod(prop) && prop.kind !== 'method'))) ev.reads = true;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// Writing a property of a value of `kind` runs its setter.
|
|
1450
|
+
function write(kind, ev) {
|
|
1451
|
+
if (kind === U || (typeof kind === 'object' && kind.o !== undefined)) ev.reads = true;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
function memberObject(member, ev) {
|
|
1455
|
+
if (t.isSuper(member.object)) {
|
|
1456
|
+
ev.reads = true;
|
|
1457
|
+
return U;
|
|
1458
|
+
}
|
|
1459
|
+
const kind = evalExpr(member.object, ev);
|
|
1460
|
+
if (member.computed) convert(evalExpr(member.property, ev), ev);
|
|
1461
|
+
return kind;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
/** @returns {Kind} */
|
|
1465
|
+
function memberRead(member, ev) {
|
|
1466
|
+
const object = memberObject(member, ev);
|
|
1467
|
+
return readMember(object, propertyName(member), ev);
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
/** @returns {Kind} */
|
|
1471
|
+
function readMember(object, name, ev) {
|
|
1472
|
+
if (object === P || object === A || object === B || object === D) return P;
|
|
1473
|
+
if (typeof object === 'object') {
|
|
1474
|
+
if (object.g !== undefined || object.f !== undefined) return P;
|
|
1475
|
+
const member = name === null ? null : memberOf(object.o, name, false);
|
|
1476
|
+
if (member === null) {
|
|
1477
|
+
ev.reads = true;
|
|
1478
|
+
return U;
|
|
1479
|
+
}
|
|
1480
|
+
if (member.inherited === true) return P;
|
|
1481
|
+
if (member.get !== undefined) {
|
|
1482
|
+
edge(member.get, [], object, ev);
|
|
1483
|
+
return U;
|
|
1484
|
+
}
|
|
1485
|
+
const value = member.value;
|
|
1486
|
+
if (value.isObjectMethod() || value.isFunctionExpression() || value.isArrowFunctionExpression()) return { f: value };
|
|
1487
|
+
if (value.isIdentifier()) {
|
|
1488
|
+
const binding = bindingOf(value.node, value.scope);
|
|
1489
|
+
return binding ? declaredKind(binding) : U;
|
|
1490
|
+
}
|
|
1491
|
+
return constKind(value.node, value.scope, declaredKind);
|
|
1492
|
+
}
|
|
1493
|
+
ev.reads = true;
|
|
1494
|
+
return U;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
function assign(e, ev) {
|
|
1498
|
+
const value = evalExpr(e.right, ev);
|
|
1499
|
+
const target = e.left;
|
|
1500
|
+
if (t.isIdentifier(target)) {
|
|
1501
|
+
if (e.operator !== '=') {
|
|
1502
|
+
const current = evalExpr(target, ev);
|
|
1503
|
+
if (e.operator !== '||=' && e.operator !== '&&=' && e.operator !== '??=') {
|
|
1504
|
+
convert(current, ev);
|
|
1505
|
+
convert(value, ev);
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
return value;
|
|
1509
|
+
}
|
|
1510
|
+
if (t.isMemberExpression(target)) {
|
|
1511
|
+
if (e.operator !== '=') {
|
|
1512
|
+
const current = memberRead(target, ev);
|
|
1513
|
+
if (e.operator !== '||=' && e.operator !== '&&=' && e.operator !== '??=') {
|
|
1514
|
+
convert(current, ev);
|
|
1515
|
+
convert(value, ev);
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
write(memberObject(target, ev), ev);
|
|
1519
|
+
return value;
|
|
1520
|
+
}
|
|
1521
|
+
destructure(target, value, ev);
|
|
1522
|
+
return value;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
// A destructuring target (a declaration, a parameter, an assignment) given a
|
|
1526
|
+
// value of `kind`: its defaults are evaluated, and taking its parts reads
|
|
1527
|
+
// properties of the value or iterates it.
|
|
1528
|
+
function destructure(pattern, kind, ev) {
|
|
1529
|
+
if (t.isIdentifier(pattern)) {
|
|
1530
|
+
const binding = bindingOf(pattern, ev.scope) || (ev.fn ? ev.fn.scope.getBinding(pattern.name) : null);
|
|
1531
|
+
if (binding && binding.constant) ev.env.set(binding, kind);
|
|
1532
|
+
else if (binding && binding.kind === 'param') ev.params.set(binding, kind);
|
|
1533
|
+
return;
|
|
1534
|
+
}
|
|
1535
|
+
if (t.isAssignmentPattern(pattern)) {
|
|
1536
|
+
destructure(pattern.left, join(kind, evalExpr(pattern.right, ev)), ev);
|
|
1537
|
+
return;
|
|
1538
|
+
}
|
|
1539
|
+
if (t.isArrayPattern(pattern)) {
|
|
1540
|
+
iterate(kind, ev);
|
|
1541
|
+
for (const element of pattern.elements) if (element) destructure(element, kind === A || kind === P ? P : U, ev);
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
if (t.isObjectPattern(pattern)) {
|
|
1545
|
+
for (const prop of pattern.properties) {
|
|
1546
|
+
if (t.isRestElement(prop)) {
|
|
1547
|
+
spread(kind, ev);
|
|
1548
|
+
destructure(prop.argument, U, ev);
|
|
1549
|
+
} else {
|
|
1550
|
+
if (prop.computed) convert(evalExpr(prop.key, ev), ev);
|
|
1551
|
+
destructure(prop.value, readMember(kind, prop.computed ? null : keyName(prop), ev), ev);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
if (t.isRestElement(pattern)) {
|
|
1557
|
+
destructure(pattern.argument, U, ev);
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
if (t.isMemberExpression(pattern)) {
|
|
1561
|
+
write(memberObject(pattern, ev), ev);
|
|
1562
|
+
return;
|
|
1563
|
+
}
|
|
1564
|
+
ev.reads = true;
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
function args(call, ev) {
|
|
1568
|
+
const kinds = [];
|
|
1569
|
+
for (const arg of call.arguments) {
|
|
1570
|
+
if (t.isSpreadElement(arg)) {
|
|
1571
|
+
iterate(evalExpr(arg.argument, ev), ev);
|
|
1572
|
+
kinds.push(U);
|
|
1573
|
+
} else {
|
|
1574
|
+
kinds.push(evalExpr(arg, ev));
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
return kinds;
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
/** @returns {Kind} */
|
|
1581
|
+
function call(e, ev) {
|
|
1582
|
+
const callee = e.callee;
|
|
1583
|
+
if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) {
|
|
1584
|
+
const receiver = memberObject(callee, ev);
|
|
1585
|
+
return methodCall(receiver, propertyName(callee), args(e, ev), ev);
|
|
1586
|
+
}
|
|
1587
|
+
if (t.isIdentifier(callee)) {
|
|
1588
|
+
const binding = bindingOf(callee, ev.scope);
|
|
1589
|
+
if (binding && binding.kind === 'module') {
|
|
1590
|
+
const argKinds = args(e, ev);
|
|
1591
|
+
// An import from What or from the app itself is as reactive as its
|
|
1592
|
+
// source; a package's utility reads nothing of the app's, except
|
|
1593
|
+
// through what it is handed: a function it may call, or a value whose
|
|
1594
|
+
// conversion or getters are the app's.
|
|
1595
|
+
if (ev.importedIds && ev.importedIds.has(callee.name)) ev.reads = true;
|
|
1596
|
+
else handOver(argKinds, ev);
|
|
1597
|
+
return U;
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
const fn = evalExpr(callee, ev);
|
|
1601
|
+
return callValue(fn, args(e, ev), U, ev);
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
// Values handed to code that may convert, read or call them: a function it
|
|
1605
|
+
// may call with anything.
|
|
1606
|
+
function handOver(argKinds, ev) {
|
|
1607
|
+
for (const kind of argKinds) {
|
|
1608
|
+
if (typeof kind === 'object' && kind.f !== undefined) edge(kind.f, kind.f.node.params.map(() => U), U, ev);
|
|
1609
|
+
else if (!inert(kind)) ev.reads = true;
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/** @returns {Kind} */
|
|
1614
|
+
function callValue(fn, argKinds, thisKind, ev) {
|
|
1615
|
+
if (typeof fn === 'object' && fn.f !== undefined) {
|
|
1616
|
+
edge(fn.f, argKinds, thisKind, ev);
|
|
1617
|
+
return U;
|
|
1618
|
+
}
|
|
1619
|
+
if (typeof fn === 'object' && fn.g !== undefined && SAFE_GLOBAL_CALLS.has(fn.g)) {
|
|
1620
|
+
handOver(argKinds, ev);
|
|
1621
|
+
return P;
|
|
1622
|
+
}
|
|
1623
|
+
ev.reads = true;
|
|
1624
|
+
return U;
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
/** @returns {Kind} */
|
|
1628
|
+
function methodCall(receiver, name, argKinds, ev) {
|
|
1629
|
+
if (receiver === P) {
|
|
1630
|
+
if (name === null || !PRIMITIVE_METHODS.has(name)) ev.reads = true;
|
|
1631
|
+
handOver(argKinds, ev);
|
|
1632
|
+
return name === 'split' || name === 'match' ? A : P;
|
|
1633
|
+
}
|
|
1634
|
+
if (receiver === A) {
|
|
1635
|
+
if (name === null || !ARRAY_METHODS.has(name)) ev.reads = true;
|
|
1636
|
+
// A callback is given the array's elements (primitives), their indexes
|
|
1637
|
+
// and the array (reduce: what the last call returned, first).
|
|
1638
|
+
for (const kind of argKinds) {
|
|
1639
|
+
if (typeof kind === 'object' && kind.f !== undefined) edge(kind.f, name === 'reduce' || name === 'reduceRight' ? [U, P, P, A] : [P, P, A], U, ev);
|
|
1640
|
+
else if (!inert(kind)) ev.reads = true;
|
|
1641
|
+
}
|
|
1642
|
+
if (name === 'join' || name === 'toString' || name === 'indexOf' || name === 'lastIndexOf' || name === 'includes' ||
|
|
1643
|
+
name === 'some' || name === 'every' || name === 'find' || name === 'findLast' || name === 'at' || name === 'findIndex' || name === 'findLastIndex') return P;
|
|
1644
|
+
return name !== null && SAME_ELEMENTS.has(name) ? A : U;
|
|
1645
|
+
}
|
|
1646
|
+
if (receiver === B) {
|
|
1647
|
+
handOver(argKinds, ev);
|
|
1648
|
+
return P;
|
|
1649
|
+
}
|
|
1650
|
+
if (receiver === D) {
|
|
1651
|
+
if (name === null || !OBJECT_METHODS.has(name)) ev.reads = true;
|
|
1652
|
+
handOver(argKinds, ev);
|
|
1653
|
+
return P;
|
|
1654
|
+
}
|
|
1655
|
+
if (typeof receiver === 'object') {
|
|
1656
|
+
if (receiver.g !== undefined) {
|
|
1657
|
+
// Math.max(), JSON.stringify(row), Number.parseFloat(), console.log().
|
|
1658
|
+
handOver(argKinds, ev);
|
|
1659
|
+
return receiver.g === 'Math' || receiver.g === 'Number' || receiver.g === 'String' || receiver.g === 'JSON' || receiver.g === 'Date' ? P : U;
|
|
1660
|
+
}
|
|
1661
|
+
if (receiver.f !== undefined) {
|
|
1662
|
+
if (name === 'call') return callValue(receiver, argKinds.slice(1), argKinds[0] ?? P, ev);
|
|
1663
|
+
if (name === 'apply') return callValue(receiver, argKinds[1] === A || argKinds[1] === undefined ? [P, P, P] : [U, U, U], argKinds[0] ?? P, ev);
|
|
1664
|
+
if (name !== 'bind') ev.reads = true;
|
|
1665
|
+
handOver(argKinds, ev);
|
|
1666
|
+
return U;
|
|
1667
|
+
}
|
|
1668
|
+
const member = name === null ? null : memberOf(receiver.o, name, false);
|
|
1669
|
+
if (member === null) {
|
|
1670
|
+
ev.reads = true;
|
|
1671
|
+
return U;
|
|
1672
|
+
}
|
|
1673
|
+
if (member.inherited === true) {
|
|
1674
|
+
if (!OBJECT_METHODS.has(/** @type {string} */ (name))) ev.reads = true;
|
|
1675
|
+
handOver(argKinds, ev);
|
|
1676
|
+
return P;
|
|
1677
|
+
}
|
|
1678
|
+
if (member.get !== undefined) {
|
|
1679
|
+
edge(member.get, [], receiver, ev);
|
|
1680
|
+
ev.reads = true;
|
|
1681
|
+
return U;
|
|
1682
|
+
}
|
|
1683
|
+
const value = member.value;
|
|
1684
|
+
// A method has the object as `this`; an arrow function its module's.
|
|
1685
|
+
const fn = readMember(receiver, name, ev);
|
|
1686
|
+
return callValue(fn, argKinds, value.isArrowFunctionExpression() ? U : receiver, ev);
|
|
1687
|
+
}
|
|
1688
|
+
ev.reads = true;
|
|
1689
|
+
return U;
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/** @returns {Kind} */
|
|
1693
|
+
function construct(e, ev) {
|
|
1694
|
+
const argKinds = args(e, ev);
|
|
1695
|
+
if (builtinConstructor(e.callee, ev.scope)) {
|
|
1696
|
+
handOver(argKinds, ev);
|
|
1697
|
+
return B;
|
|
1698
|
+
}
|
|
1699
|
+
if (t.isIdentifier(e.callee) && !bindingOf(e.callee, ev.scope) && (SAFE_GLOBAL_CALLS.has(e.callee.name) || BUILTIN_CONSTRUCTORS.has(e.callee.name))) {
|
|
1700
|
+
handOver(argKinds, ev);
|
|
1701
|
+
return U;
|
|
1702
|
+
}
|
|
1703
|
+
// A constructor the app declares runs its own code: a class instance is
|
|
1704
|
+
// never proven static.
|
|
1705
|
+
ev.reads = true;
|
|
1706
|
+
return U;
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
// A call of the function at `path` with arguments of `argKinds` and `this`
|
|
1710
|
+
// of `thisKind`: an edge of the call graph from the evaluation. A function
|
|
1711
|
+
// written inside the one being read sees that one's bindings.
|
|
1712
|
+
function edge(path, argKinds, thisKind, ev) {
|
|
1713
|
+
const node = path.node;
|
|
1714
|
+
if (node.async || node.generator) {
|
|
1715
|
+
ev.reads = true;
|
|
1716
|
+
return;
|
|
1717
|
+
}
|
|
1718
|
+
const around = path.parentPath && path.parentPath.getFunctionParent();
|
|
1719
|
+
let outer = null;
|
|
1720
|
+
for (let at = ev; at !== null; at = at.outer) {
|
|
1721
|
+
if (around && at.fn !== null && at.fn.node === around.node) {
|
|
1722
|
+
outer = at;
|
|
1723
|
+
break;
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
ev.edges.push(graphNode(path, argKinds, thisKind, outer, ev.importedIds));
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
// --- The call graph ---
|
|
1730
|
+
//
|
|
1731
|
+
// A node per function, per kinds of its arguments and `this`, and per
|
|
1732
|
+
// evaluation it is a closure of. Its edges are the calls of declared
|
|
1733
|
+
// functions its body makes; `direct` says whether its body reads by itself.
|
|
1734
|
+
// A node reads when it reads directly or reaches one that does.
|
|
1735
|
+
const graphNodes = new WeakMap();
|
|
1736
|
+
|
|
1737
|
+
function graphNode(path, argKinds, thisKind, outer, importedIds) {
|
|
1738
|
+
let byKey = graphNodes.get(path.node);
|
|
1739
|
+
if (!byKey) graphNodes.set(path.node, byKey = new Map());
|
|
1740
|
+
const key = argKinds.map(kindKey).join() + '|' + kindKey(thisKind) + '|' + (outer === null ? '' : idOf(outer));
|
|
1741
|
+
let node = byKey.get(key);
|
|
1742
|
+
if (!node) byKey.set(key, node = { path, argKinds, thisKind, outer, importedIds, edges: null, direct: false, reads: undefined });
|
|
1743
|
+
return node;
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
// Read a node's function once: whether it reads, and which declared
|
|
1747
|
+
// functions it calls.
|
|
1748
|
+
function expand(node) {
|
|
1749
|
+
const path = node.path;
|
|
1750
|
+
const ev = evaluation(path, node.outer, node.thisKind, path.scope, node.importedIds);
|
|
1751
|
+
// The values a parameter was not given are undefined.
|
|
1752
|
+
path.node.params.forEach((param, i) => destructure(param, node.argKinds[i] ?? P, ev));
|
|
1753
|
+
if (path.node._whatMovedProps) destructure(path.node._whatMovedProps, node.argKinds[0] ?? P, ev);
|
|
1754
|
+
const body = path.node.body;
|
|
1755
|
+
if (t.isBlockStatement(body)) evalStmt(body, ev);
|
|
1756
|
+
else evalExpr(body, ev);
|
|
1757
|
+
node.direct = ev.reads;
|
|
1758
|
+
node.edges = ev.edges;
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
function evalStmt(s, ev) {
|
|
1762
|
+
switch (s.type) {
|
|
1763
|
+
case 'BlockStatement':
|
|
1764
|
+
for (const x of s.body) evalStmt(x, ev);
|
|
1765
|
+
return;
|
|
1766
|
+
case 'ExpressionStatement':
|
|
1767
|
+
evalExpr(s.expression, ev);
|
|
1768
|
+
return;
|
|
1769
|
+
case 'ReturnStatement': case 'ThrowStatement':
|
|
1770
|
+
evalExpr(s.argument, ev);
|
|
1771
|
+
return;
|
|
1772
|
+
case 'VariableDeclaration':
|
|
1773
|
+
for (const d of s.declarations) {
|
|
1774
|
+
const kind = evalExpr(d.init, ev);
|
|
1775
|
+
// The declared names resolve from the declaration's own block.
|
|
1776
|
+
const scope = ev.scope;
|
|
1777
|
+
ev.scope = declaratorPaths.get(d)?.scope ?? scope;
|
|
1778
|
+
destructure(d.id, d.init == null ? P : kind, ev);
|
|
1779
|
+
ev.scope = scope;
|
|
1780
|
+
}
|
|
1781
|
+
return;
|
|
1782
|
+
case 'IfStatement':
|
|
1783
|
+
evalExpr(s.test, ev);
|
|
1784
|
+
evalStmt(s.consequent, ev);
|
|
1785
|
+
if (s.alternate) evalStmt(s.alternate, ev);
|
|
1786
|
+
return;
|
|
1787
|
+
case 'ForStatement':
|
|
1788
|
+
if (s.init) {
|
|
1789
|
+
if (t.isVariableDeclaration(s.init)) evalStmt(s.init, ev);
|
|
1790
|
+
else evalExpr(s.init, ev);
|
|
1791
|
+
}
|
|
1792
|
+
evalExpr(s.test, ev);
|
|
1793
|
+
evalExpr(s.update, ev);
|
|
1794
|
+
evalStmt(s.body, ev);
|
|
1795
|
+
return;
|
|
1796
|
+
case 'ForOfStatement': {
|
|
1797
|
+
const kind = evalExpr(s.right, ev);
|
|
1798
|
+
iterate(kind, ev);
|
|
1799
|
+
if (s.await) ev.reads = true;
|
|
1800
|
+
const target = t.isVariableDeclaration(s.left) ? s.left.declarations[0].id : s.left;
|
|
1801
|
+
destructure(target, kind === A || kind === P ? P : U, ev);
|
|
1802
|
+
evalStmt(s.body, ev);
|
|
1803
|
+
return;
|
|
1804
|
+
}
|
|
1805
|
+
case 'ForInStatement': {
|
|
1806
|
+
if (evalExpr(s.right, ev) === U) ev.reads = true;
|
|
1807
|
+
const target = t.isVariableDeclaration(s.left) ? s.left.declarations[0].id : s.left;
|
|
1808
|
+
destructure(target, P, ev);
|
|
1809
|
+
evalStmt(s.body, ev);
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
case 'WhileStatement': case 'DoWhileStatement':
|
|
1813
|
+
evalExpr(s.test, ev);
|
|
1814
|
+
evalStmt(s.body, ev);
|
|
1815
|
+
return;
|
|
1816
|
+
case 'SwitchStatement':
|
|
1817
|
+
evalExpr(s.discriminant, ev);
|
|
1818
|
+
for (const c of s.cases) {
|
|
1819
|
+
if (c.test) evalExpr(c.test, ev);
|
|
1820
|
+
for (const x of c.consequent) evalStmt(x, ev);
|
|
1821
|
+
}
|
|
1822
|
+
return;
|
|
1823
|
+
case 'TryStatement':
|
|
1824
|
+
evalStmt(s.block, ev);
|
|
1825
|
+
if (s.handler) {
|
|
1826
|
+
if (s.handler.param) destructure(s.handler.param, U, ev);
|
|
1827
|
+
evalStmt(s.handler.body, ev);
|
|
1828
|
+
}
|
|
1829
|
+
if (s.finalizer) evalStmt(s.finalizer, ev);
|
|
1830
|
+
return;
|
|
1831
|
+
case 'LabeledStatement':
|
|
1832
|
+
evalStmt(s.body, ev);
|
|
1833
|
+
return;
|
|
1834
|
+
case 'FunctionDeclaration': case 'EmptyStatement': case 'BreakStatement': case 'ContinueStatement': case 'DebuggerStatement':
|
|
1835
|
+
return;
|
|
1836
|
+
default:
|
|
1837
|
+
ev.reads = true;
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// Whether `start` reads, by a depth-first walk of the graph on a stack of its
|
|
1842
|
+
// own: a chain of thousands of helpers cannot overflow the JavaScript stack,
|
|
1843
|
+
// and a cycle is decided only once all of it has been seen. Reaching a reader
|
|
1844
|
+
// makes every node on the path to it a reader; exhausting the walk proves
|
|
1845
|
+
// every node it saw static, since each reaches only what the start reaches.
|
|
1846
|
+
function readsThrough(start) {
|
|
1847
|
+
if (start.reads !== undefined) return start.reads;
|
|
1848
|
+
const stack = [start];
|
|
1849
|
+
const nextEdge = [0];
|
|
1850
|
+
const seen = new Set(stack);
|
|
1851
|
+
const found = () => {
|
|
1852
|
+
for (const node of stack) node.reads = true;
|
|
1853
|
+
return true;
|
|
1854
|
+
};
|
|
1855
|
+
while (stack.length > 0) {
|
|
1856
|
+
const top = stack.length - 1;
|
|
1857
|
+
const node = stack[top];
|
|
1858
|
+
if (node.edges === null) expand(node);
|
|
1859
|
+
if (node.direct) return found();
|
|
1860
|
+
if (nextEdge[top] === node.edges.length) {
|
|
1861
|
+
stack.pop();
|
|
1862
|
+
nextEdge.pop();
|
|
1863
|
+
continue;
|
|
1864
|
+
}
|
|
1865
|
+
const edgeNode = node.edges[nextEdge[top]++];
|
|
1866
|
+
if (edgeNode.reads === true) return found();
|
|
1867
|
+
if (edgeNode.reads === false || seen.has(edgeNode)) continue;
|
|
1868
|
+
seen.add(edgeNode);
|
|
1869
|
+
stack.push(edgeNode);
|
|
1870
|
+
nextEdge.push(0);
|
|
1871
|
+
}
|
|
1872
|
+
for (const node of seen) node.reads = false;
|
|
1873
|
+
return false;
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
// Every call of the module, read before any JSX is lowered (see the plugin's
|
|
1877
|
+
// Program visitor), so that a function's body is read as its author wrote it
|
|
1878
|
+
// whether it comes before or after the JSX that calls it. What every
|
|
1879
|
+
// identifier names, and where each function and declaration stands, is
|
|
1880
|
+
// recorded first, so the bodies read see each of them.
|
|
1881
|
+
export function resolveCallees(program, callPaths, importedIds) {
|
|
1882
|
+
references = new WeakMap();
|
|
1883
|
+
functionPaths = new WeakMap();
|
|
1884
|
+
declaratorPaths = new WeakMap();
|
|
1885
|
+
decided = new WeakMap();
|
|
1886
|
+
declaredKinds = new WeakMap();
|
|
1887
|
+
opaqueBindings = new WeakMap();
|
|
1888
|
+
program.traverse({
|
|
1889
|
+
ReferencedIdentifier(path) {
|
|
1890
|
+
if (path.isIdentifier()) references.set(path.node, path.scope.getBinding(path.node.name) || null);
|
|
1891
|
+
},
|
|
1892
|
+
Function(path) {
|
|
1893
|
+
functionPaths.set(path.node, path);
|
|
1894
|
+
},
|
|
1895
|
+
VariableDeclarator(path) {
|
|
1896
|
+
declaratorPaths.set(path.node, path);
|
|
1897
|
+
},
|
|
1898
|
+
});
|
|
1899
|
+
scopeNamesCache = new WeakMap();
|
|
1900
|
+
try {
|
|
1901
|
+
for (const callPath of callPaths) {
|
|
1902
|
+
const ev = evaluation(null, null, U, callPath.scope, importedIds);
|
|
1903
|
+
evalExpr(callPath.node, ev);
|
|
1904
|
+
decided.set(callPath.node, finish(ev));
|
|
1905
|
+
}
|
|
1906
|
+
} finally {
|
|
1907
|
+
scopeNamesCache = null;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
// Each scope's own signal names, kept while resolveCallees reads the module,
|
|
1912
|
+
// before any JSX is lowered (lowering rewrites a row callback's parameters):
|
|
1913
|
+
// a module of thousands of functions reads its top-level bindings once, not
|
|
1914
|
+
// once per function.
|
|
1915
|
+
/** @type {WeakMap<object, { signals: string[], props: string[], propsObject: string[] | null }> | null} */
|
|
1916
|
+
let scopeNamesCache = null;
|
|
1917
|
+
|
|
1918
|
+
// A `when` prop's shape determines how the condition is formed:
|
|
1919
|
+
// - call expression → use as-is when={cond()}
|
|
1920
|
+
// - arrow w/ expression body → use the body when={() => x > 5}
|
|
1921
|
+
// - a proven local signal → invoke it: isOpen() when={isOpen}
|
|
1922
|
+
// - an import or a destructured prop, bare or as the root of a plain
|
|
1923
|
+
// member chain when={FLAG}, when={ns.open}
|
|
1924
|
+
// → resolve it at runtime, exactly as the Show
|
|
1925
|
+
// and Match components do for h():
|
|
1926
|
+
// typeof X === 'function' ? X() : X
|
|
1927
|
+
// Either one can hold a boolean or an accessor
|
|
1928
|
+
// and only the runtime knows which. Calling it
|
|
1929
|
+
// unconditionally threw on a boolean.
|
|
1930
|
+
// - anything else (member, literal, logical, etc.) when={user.isAdmin}
|
|
1931
|
+
// → use the raw expression. Do NOT invoke it:
|
|
1932
|
+
// non-functions would throw at runtime.
|
|
1933
|
+
export function buildWhenCondition(whenExpr, state) {
|
|
1934
|
+
if (t.isCallExpression(whenExpr)) return whenExpr;
|
|
1935
|
+
if (t.isArrowFunctionExpression(whenExpr) && t.isExpression(whenExpr.body)) return whenExpr.body;
|
|
1936
|
+
const fromProps = state.signalNames && state.signalNames.fromDestructuredProps;
|
|
1937
|
+
const isUnprovenBinding = name =>
|
|
1938
|
+
(fromProps && fromProps.has(name)) ||
|
|
1939
|
+
(state.importedIdentifiers && state.importedIdentifiers.has(name));
|
|
1940
|
+
if (t.isIdentifier(whenExpr)) {
|
|
1941
|
+
if (isUnprovenBinding(whenExpr.name)) return resolveAccessorAtRuntime(whenExpr);
|
|
1942
|
+
if (state.signalNames && isSignalIdentifier(whenExpr.name, state.signalNames)) {
|
|
1943
|
+
return t.callExpression(whenExpr, []);
|
|
1944
|
+
}
|
|
1945
|
+
return whenExpr;
|
|
1946
|
+
}
|
|
1947
|
+
const root = plainMemberChainRoot(whenExpr);
|
|
1948
|
+
// `props.when` may hold an accessor, as a destructured `when` may (which
|
|
1949
|
+
// common.js deferPlacedProps reads as `props.when`).
|
|
1950
|
+
const viaProps = root && state.signalNames && state.signalNames.propsObjects && state.signalNames.propsObjects.has(root.name);
|
|
1951
|
+
if (root && (isUnprovenBinding(root.name) || viaProps)) return resolveAccessorAtRuntime(whenExpr);
|
|
1952
|
+
return whenExpr;
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
// A reactive `each` becomes the list's accessor (see the <For> lowering), and
|
|
1956
|
+
// the value it evaluates to may be an accessor itself, as a plain `each` name
|
|
1957
|
+
// may: each={archived() ? done : open} picks one of two signals. Each name or
|
|
1958
|
+
// plain member chain the value can come from is read the way mapArray reads a
|
|
1959
|
+
// plain `each`; a proven local signal is called.
|
|
1960
|
+
export function buildEachValue(expr, state) {
|
|
1961
|
+
if (t.isConditionalExpression(expr)) {
|
|
1962
|
+
return t.conditionalExpression(expr.test, buildEachValue(expr.consequent, state), buildEachValue(expr.alternate, state));
|
|
1963
|
+
}
|
|
1964
|
+
if (t.isLogicalExpression(expr)) {
|
|
1965
|
+
return t.logicalExpression(expr.operator, buildEachValue(expr.left, state), buildEachValue(expr.right, state));
|
|
1966
|
+
}
|
|
1967
|
+
if (t.isIdentifier(expr)) {
|
|
1968
|
+
if (expr.name === 'undefined') return expr;
|
|
1969
|
+
const fromProps = state.signalNames && state.signalNames.fromDestructuredProps;
|
|
1970
|
+
const proven = state.signalNames && isSignalIdentifier(expr.name, state.signalNames) &&
|
|
1971
|
+
!(fromProps && fromProps.has(expr.name)) &&
|
|
1972
|
+
!(state.importedIdentifiers && state.importedIdentifiers.has(expr.name));
|
|
1973
|
+
return proven ? t.callExpression(expr, []) : resolveAccessorAtRuntime(expr);
|
|
1974
|
+
}
|
|
1975
|
+
return plainMemberChainRoot(expr) ? resolveAccessorAtRuntime(expr) : expr;
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
// The root identifier of `a.b.c` with no computed keys, else null. Reading
|
|
1979
|
+
// such a chain twice is a pure lookup, so the runtime check below may
|
|
1980
|
+
// evaluate it more than once.
|
|
1981
|
+
function plainMemberChainRoot(expr) {
|
|
1982
|
+
if (!t.isMemberExpression(expr)) return null;
|
|
1983
|
+
let node = expr;
|
|
1984
|
+
while (t.isMemberExpression(node)) {
|
|
1985
|
+
if (node.computed || !t.isIdentifier(node.property)) return null;
|
|
1986
|
+
node = node.object;
|
|
1987
|
+
}
|
|
1988
|
+
return t.isIdentifier(node) ? node : null;
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
function resolveAccessorAtRuntime(ref) {
|
|
1992
|
+
return t.conditionalExpression(
|
|
1993
|
+
t.binaryExpression('===', t.unaryExpression('typeof', ref), t.stringLiteral('function')),
|
|
1994
|
+
t.callExpression(t.cloneNode(ref), []),
|
|
1995
|
+
t.cloneNode(ref)
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
|