test-inventory 0.12.1 → 0.12.3

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +141 -0
  2. package/README.md +74 -49
  3. package/dist/ast.d.ts +7 -2
  4. package/dist/ast.js +43 -3
  5. package/dist/case-titles.d.ts +5 -11
  6. package/dist/case-titles.js +69 -55
  7. package/dist/config-runs.d.ts +19 -2
  8. package/dist/config-runs.js +379 -90
  9. package/dist/config.d.ts +0 -5
  10. package/dist/config.js +0 -7
  11. package/dist/dependency-hash.js +1 -32
  12. package/dist/diagnostics.js +17 -2
  13. package/dist/diff.d.ts +2 -45
  14. package/dist/extract.d.ts +13 -3
  15. package/dist/extract.js +837 -89
  16. package/dist/frameworks/bun.js +47 -24
  17. package/dist/frameworks/common.d.ts +4 -2
  18. package/dist/frameworks/common.js +10 -11
  19. package/dist/frameworks/jasmine-registration.d.ts +47 -0
  20. package/dist/frameworks/jasmine-registration.js +632 -0
  21. package/dist/frameworks/jasmine.d.ts +7 -1
  22. package/dist/frameworks/jasmine.js +65 -70
  23. package/dist/frameworks/jest.d.ts +2 -2
  24. package/dist/frameworks/jest.js +126 -51
  25. package/dist/frameworks/vitest.d.ts +2 -3
  26. package/dist/frameworks/vitest.js +80 -16
  27. package/dist/generator-steps.d.ts +56 -0
  28. package/dist/generator-steps.js +254 -0
  29. package/dist/model.d.ts +37 -5
  30. package/dist/mutation-arrays.d.ts +25 -0
  31. package/dist/mutation-arrays.js +229 -0
  32. package/dist/mutation-calls.d.ts +33 -0
  33. package/dist/mutation-calls.js +774 -0
  34. package/dist/mutation-order.d.ts +19 -0
  35. package/dist/mutation-order.js +55 -0
  36. package/dist/mutation-references.d.ts +109 -0
  37. package/dist/mutation-references.js +462 -0
  38. package/dist/mutation-returns.d.ts +13 -0
  39. package/dist/mutation-returns.js +71 -0
  40. package/dist/node-scripts.d.ts +4 -1
  41. package/dist/node-scripts.js +77 -11
  42. package/dist/options.d.ts +2 -0
  43. package/dist/options.js +6 -1
  44. package/dist/records.d.ts +5 -3
  45. package/dist/records.js +81 -28
  46. package/dist/scan-file.d.ts +18 -4
  47. package/dist/scan-file.js +44 -5
  48. package/dist/scan-source.js +2 -1
  49. package/dist/scan.js +30 -13
  50. package/dist/types.d.ts +45 -1
  51. package/dist/values.d.ts +72 -18
  52. package/dist/values.js +651 -172
  53. package/dist/vitest-concurrency.d.ts +16 -0
  54. package/dist/vitest-concurrency.js +55 -0
  55. package/package.json +2 -2
  56. package/schema/v1.json +1 -0
package/dist/values.js CHANGED
@@ -1,4 +1,10 @@
1
- import { childNodes, isFunctionNode, propertyKey, propertyName, unwrap } from "./ast.js";
1
+ import { childNodes, isFunctionNode, propertyKey, propertyName, unwrap, unwrapChain, } from "./ast.js";
2
+ import { changedReferences, memberKey, patternReferences, } from "./mutation-references.js";
3
+ import { mutationCalls } from "./mutation-calls.js";
4
+ /** Only strings and values with an iterator can be spread into a call or bound to an array pattern. */
5
+ export function nonIterable(value) {
6
+ return typeof value !== 'string' && (value === null || typeof value !== 'object' || !(Symbol.iterator in value));
7
+ }
2
8
  const UNRESOLVED = { ok: false, importedFrom: null };
3
9
  function isPrimitive(value) {
4
10
  return typeof value !== 'object' || value === null;
@@ -7,6 +13,81 @@ function isPrimitive(value) {
7
13
  export function isKey(value) {
8
14
  return typeof value === 'string' || typeof value === 'number';
9
15
  }
16
+ export function isValueOperation(node) {
17
+ return (node.type === 'TemplateLiteral' ||
18
+ node.type === 'BinaryExpression' ||
19
+ node.type === 'LogicalExpression' ||
20
+ node.type === 'ConditionalExpression' ||
21
+ node.type === 'UnaryExpression');
22
+ }
23
+ export function usesRightOperand(operator, left) {
24
+ if (operator === '&&')
25
+ return Boolean(left);
26
+ if (operator === '||')
27
+ return !left;
28
+ return left === null || left === undefined;
29
+ }
30
+ /** Read operands in evaluation order, including only the selected side of a short-circuit expression. */
31
+ export function operationValue(expression, read) {
32
+ switch (expression.type) {
33
+ case 'TemplateLiteral': {
34
+ // Only tagged templates can lack a cooked value, so these strings are always present.
35
+ let text = expression.quasis[0].value.cooked;
36
+ for (const [index, part] of expression.expressions.entries()) {
37
+ const resolved = read(part);
38
+ if (!resolved.ok)
39
+ return { ok: false, importedFrom: resolved.importedFrom };
40
+ if (!isPrimitive(resolved.value))
41
+ return UNRESOLVED;
42
+ text += String(resolved.value) + expression.quasis[index + 1].value.cooked;
43
+ }
44
+ return { ok: true, value: text };
45
+ }
46
+ case 'BinaryExpression': {
47
+ if (!BINARY.has(expression.operator))
48
+ return UNRESOLVED;
49
+ const left = read(expression.left);
50
+ if (!left.ok)
51
+ return { ok: false, importedFrom: left.importedFrom };
52
+ const right = read(expression.right);
53
+ if (!right.ok)
54
+ return { ok: false, importedFrom: right.importedFrom };
55
+ if (!isPrimitive(left.value) || !isPrimitive(right.value))
56
+ return UNRESOLVED;
57
+ return binary(expression.operator, left.value, right.value);
58
+ }
59
+ case 'LogicalExpression': {
60
+ const left = read(expression.left);
61
+ if (!left.ok)
62
+ return left.truthy && expression.operator === '&&' ? read(expression.right) : left;
63
+ return usesRightOperand(expression.operator, left.value) ? read(expression.right) : left;
64
+ }
65
+ case 'ConditionalExpression': {
66
+ const test = read(expression.test);
67
+ if (!test.ok)
68
+ return test.truthy ? read(expression.consequent) : test;
69
+ return read(test.value ? expression.consequent : expression.alternate);
70
+ }
71
+ case 'UnaryExpression': {
72
+ if (expression.operator === 'void')
73
+ return { ok: true, value: undefined };
74
+ if (expression.operator !== '!' && expression.operator !== '-' && expression.operator !== '+')
75
+ return UNRESOLVED;
76
+ const argument = read(expression.argument);
77
+ if (!argument.ok) {
78
+ if (argument.truthy && expression.operator === '!')
79
+ return { ok: true, value: false };
80
+ return { ok: false, importedFrom: argument.importedFrom };
81
+ }
82
+ if (expression.operator === '!')
83
+ return { ok: true, value: !argument.value };
84
+ if (!isPrimitive(argument.value))
85
+ return UNRESOLVED;
86
+ const number = Number(argument.value);
87
+ return Number.isNaN(number) ? UNRESOLVED : { ok: true, value: expression.operator === '-' ? -number : number };
88
+ }
89
+ }
90
+ }
10
91
  export function isScopeNode(node) {
11
92
  switch (node.type) {
12
93
  case 'Program':
@@ -56,34 +137,142 @@ export function bindingNames(pattern, names = []) {
56
137
  export class Values {
57
138
  bindingsByScope = new Map();
58
139
  cache = new Map();
140
+ fileIterators = new WeakMap();
141
+ iteratorMethods = new WeakMap();
142
+ iteratorSpreads = new WeakMap();
59
143
  pending = new Set();
60
144
  /** The functions being read at one of their calls, innermost last. */
61
145
  calls = [];
62
146
  imports;
63
147
  assigned = new WeakMap();
148
+ rebound = new WeakMap();
64
149
  changed = new WeakMap();
65
150
  /** Without `imports`, a value imported from another module is not worked out. */
66
151
  constructor(imports = null) {
67
152
  this.imports = imports;
68
153
  }
154
+ /** Saved instances belong to the invocation that evaluated their initializer. */
155
+ recordIterator(node, scopes, value) {
156
+ const call = this.calls.findLast((entry) => scopes.includes(entry.fn));
157
+ if (value) {
158
+ const instances = call ? (call.iterators ??= new WeakMap()) : this.fileIterators;
159
+ instances.set(node, value);
160
+ }
161
+ else
162
+ (call ? call.iterators : this.fileIterators)?.delete(node);
163
+ }
164
+ /** Follow only bindings that preserve the iterator object itself. A copy of its elements is reusable. */
165
+ iteratorOf(node, scopes) {
166
+ let expression = unwrap(node);
167
+ const seen = new Set();
168
+ while (expression.type === 'Identifier') {
169
+ if (seen.has(expression))
170
+ return;
171
+ seen.add(expression);
172
+ const constant = this.constantOf(expression.name, scopes, true);
173
+ if (!constant) {
174
+ const declaration = this.declarationOf(expression.name, scopes);
175
+ const bound = declaration && this.resolveParameter(expression.name, declaration);
176
+ return bound && !bound.ok && bound.iterator ? bound : undefined;
177
+ }
178
+ expression = unwrap(constant.init);
179
+ scopes = constant.scopes;
180
+ }
181
+ if (expression.type !== 'CallExpression')
182
+ return;
183
+ const call = this.calls.findLast((entry) => scopes.includes(entry.fn));
184
+ return (call ? call.iterators : this.fileIterators)?.get(expression);
185
+ }
186
+ /** A suspended helper still sees its own arguments and the parameters of the enclosing calls that created it. */
187
+ captureBindings(read) {
188
+ const bindings = this.calls.slice();
189
+ return (value) => {
190
+ const caller = this.calls;
191
+ this.calls = bindings;
192
+ try {
193
+ return read(value);
194
+ }
195
+ finally {
196
+ this.calls = caller;
197
+ }
198
+ };
199
+ }
200
+ /** Branches may resume the same suspension with different values. Keep their constant caches independent. */
201
+ withGeneratorLocals(locals, read) {
202
+ const binding = this.calls[this.calls.length - 1];
203
+ this.calls.push({ ...binding, locals, cache: new Map() });
204
+ try {
205
+ return read();
206
+ }
207
+ finally {
208
+ this.calls.pop();
209
+ }
210
+ }
211
+ completedOperand(node, scopes) {
212
+ for (let i = this.calls.length - 1; i >= 0; i--) {
213
+ const call = this.calls[i];
214
+ if (call.locals && scopes.includes(call.fn) && call.fn.start <= node.start && node.end <= call.fn.end)
215
+ return call.locals.operands.get(node);
216
+ }
217
+ return undefined;
218
+ }
219
+ bindCatch(clause, value, scopes, locals) {
220
+ const names = new Map();
221
+ const steps = new Map();
222
+ locals.catches.set(clause, names);
223
+ this.bind(clause.param, value, names, scopes, steps);
224
+ for (const name of this.assignedIn(clause.body))
225
+ names.delete(name);
226
+ return steps;
227
+ }
228
+ /** Read arguments in the calling scope, up to the first spread whose length is unknown. */
229
+ callArguments(call, scopes, expandSpread, read) {
230
+ const args = [];
231
+ let complete = true;
232
+ for (const item of call.arguments) {
233
+ if (item.type !== 'SpreadElement') {
234
+ args.push((expandSpread ? this.iteratorOf(item, scopes) : undefined) ?? read(item));
235
+ continue;
236
+ }
237
+ const consumed = expandSpread && this.iteratorSpreads.get(item.argument);
238
+ if (consumed) {
239
+ for (const value of consumed.values)
240
+ args.push(value);
241
+ if (consumed.complete)
242
+ continue;
243
+ complete = false;
244
+ break;
245
+ }
246
+ const list = expandSpread && read(item.argument);
247
+ if (!list || !list.ok || !(Array.isArray(list.value) || typeof list.value === 'string')) {
248
+ complete = false;
249
+ break;
250
+ }
251
+ for (const value of list.value)
252
+ args.push({ ok: true, value });
253
+ }
254
+ return { args, complete };
255
+ }
69
256
  /**
70
257
  * Runs `read` with a function's parameters bound to the arguments of one call, worked out where the call is, so that
71
258
  * titles and values built from them resolve, as in `test(\`${side} rejects a zero quantity\`)` inside
72
259
  * `validatesQuantity('buy')`. `args` stop at a spread argument, after which no position is known, and `complete` is
73
260
  * false then. A missing argument takes the parameter's default value, worked out in `scopes`, the scopes around the
74
- * function. A rest parameter, as `...cycles`, holds the arguments left.
261
+ * function. A rest parameter, as `...cycles`, holds the arguments left. When collecting parameter side effects,
262
+ * `steps` records conditional defaults and destructuring failures in the order the caller visits the parameters.
75
263
  */
76
- withArguments(fn, args, scopes, read, complete = true) {
264
+ withArguments(fn, args, scopes, read, complete = true, steps) {
77
265
  const parameters = new Map();
78
266
  for (const [index, parameter] of fn.params.entries()) {
79
267
  if (parameter.type === 'RestElement') {
80
268
  const rest = args.slice(index);
81
269
  const unknown = complete ? rest.find((item) => !item.ok) : UNRESOLVED;
82
270
  const value = unknown ?? { ok: true, value: rest.map((item) => item.value) };
83
- this.bind(parameter.argument, value, parameters, [...scopes, fn]);
271
+ this.bind(parameter.argument, value, parameters, [...scopes, fn], steps);
84
272
  }
85
273
  else {
86
- this.bind(parameter, args.at(index), parameters, [...scopes, fn]);
274
+ const value = args.at(index) ?? (!complete && steps ? UNRESOLVED : undefined);
275
+ this.bind(parameter, value, parameters, [...scopes, fn], steps);
87
276
  }
88
277
  }
89
278
  // A parameter the function assigns to may hold another value by the time it is read.
@@ -102,11 +291,12 @@ export class Values {
102
291
  names.delete(name);
103
292
  return this.withBound(declarator, names, read);
104
293
  }
105
- assignedIn(body) {
106
- let names = this.assigned.get(body);
294
+ assignedIn(body, includeMemberWrites = true) {
295
+ const cache = includeMemberWrites ? this.assigned : this.rebound;
296
+ let names = cache.get(body);
107
297
  if (!names) {
108
- names = assignedNames(body);
109
- this.assigned.set(body, names);
298
+ names = assignedNames(body, includeMemberWrites);
299
+ cache.set(body, names);
110
300
  }
111
301
  return names;
112
302
  }
@@ -123,55 +313,81 @@ export class Values {
123
313
  * Binds the names of a parameter or a loop variable to the parts of `value` they take, as destructuring does. A
124
314
  * missing `value`, after a spread argument, leaves them unknown, unless a default gives them one.
125
315
  */
126
- bind(pattern, value, names, scopes) {
316
+ bind(pattern, value, names, scopes, steps) {
317
+ const unknown = value && !value.ok && !value.iterator ? value : UNRESOLVED;
127
318
  switch (pattern.type) {
128
319
  case 'Identifier':
320
+ steps?.set(pattern, 'initialize');
129
321
  if (value)
130
322
  names.set(pattern.name, () => value);
131
323
  return;
132
324
  case 'AssignmentPattern': {
133
325
  const useDefault = !value || (value.ok && value.value === undefined);
326
+ steps?.set(pattern, value && !value.ok && !value.defined ? null : useDefault);
134
327
  if (!useDefault) {
135
- this.bind(pattern.left, value, names, scopes);
328
+ this.bind(pattern.left, value, names, scopes, steps);
136
329
  return;
137
330
  }
138
331
  const fallback = pattern.right;
139
332
  if (pattern.left.type === 'Identifier') {
333
+ steps?.set(pattern.left, 'initialize');
140
334
  names.set(pattern.left.name, () => this.resolve(fallback, scopes));
141
335
  return;
142
336
  }
143
- this.bind(pattern.left, this.resolve(fallback, scopes), names, scopes);
337
+ this.bind(pattern.left, this.resolve(fallback, scopes), names, scopes, steps);
144
338
  return;
145
339
  }
146
- case 'ArrayPattern':
340
+ case 'ArrayPattern': {
341
+ if (steps && (!value || (value.ok && nonIterable(value.value)))) {
342
+ steps.set(pattern, 'throw');
343
+ return;
344
+ }
345
+ if (value && !value.ok)
346
+ steps?.set(pattern, null);
347
+ const iterable = value?.ok ? value.value : null;
348
+ const list = typeof iterable === 'string' ? Array.from(iterable) : Array.isArray(iterable) ? iterable : null;
147
349
  for (const [index, element] of pattern.elements.entries()) {
148
350
  if (!element || !value)
149
351
  continue;
150
- const list = value.ok && Array.isArray(value.value) ? value.value : null;
151
- const part = (rest) => list ? { ok: true, value: rest ? list.slice(index) : list[index] } : value.ok ? UNRESOLVED : value;
352
+ const part = (rest) => list ? { ok: true, value: rest ? list.slice(index) : list[index] } : unknown;
152
353
  if (element.type === 'RestElement')
153
- this.bind(element.argument, part(true), names, scopes);
354
+ this.bind(element.argument, part(true), names, scopes, steps);
154
355
  else
155
- this.bind(element, part(false), names, scopes);
356
+ this.bind(element, part(false), names, scopes, steps);
156
357
  }
157
358
  return;
359
+ }
158
360
  case 'ObjectPattern': {
361
+ if (steps && (!value || (value.ok && (value.value === null || value.value === undefined)))) {
362
+ steps.set(pattern, 'throw');
363
+ return;
364
+ }
159
365
  if (!value)
160
366
  return;
161
- const object = value.ok && isPlainObject(value.value) ? value.value : null;
367
+ if (!value.ok)
368
+ steps?.set(pattern, null);
369
+ const object = value.ok && value.value !== null && value.value !== undefined
370
+ ? Object(value.value)
371
+ : null;
162
372
  const taken = [];
163
373
  for (const property of pattern.properties) {
164
374
  if (property.type === 'RestElement') {
165
375
  const rest = object && Object.fromEntries(Object.entries(object).filter(([key]) => !taken.includes(key)));
166
- this.bind(property.argument, rest ? { ok: true, value: rest } : value.ok ? UNRESOLVED : value, names, scopes);
376
+ this.bind(property.argument, rest ? { ok: true, value: rest } : unknown, names, scopes, steps);
167
377
  continue;
168
378
  }
169
379
  const key = this.propertyKey(property.key, property.computed, scopes);
170
380
  const name = key.ok && isKey(key.value) ? String(key.value) : null;
171
381
  if (name !== null)
172
382
  taken.push(name);
173
- const part = object && name !== null ? { ok: true, value: object[name] } : value.ok ? UNRESOLVED : value;
174
- this.bind(property.value, part, names, scopes);
383
+ let part = unknown;
384
+ if (object && name !== null) {
385
+ const item = object[name];
386
+ // An inherited method suppresses a default even though the scan cannot use it as a literal value.
387
+ part =
388
+ typeof item === 'function' ? { ok: false, importedFrom: null, defined: true } : { ok: true, value: item };
389
+ }
390
+ this.bind(property.value, part, names, scopes, steps);
175
391
  }
176
392
  return;
177
393
  }
@@ -180,69 +396,32 @@ export class Values {
180
396
  /** `scopes` lists the scope nodes around the expression, outermost first. */
181
397
  resolve(node, scopes) {
182
398
  const expression = unwrap(node);
399
+ const completed = this.completedOperand(expression, scopes);
400
+ if (completed)
401
+ return completed;
402
+ if (isValueOperation(expression))
403
+ return operationValue(expression, (part) => this.resolve(part, scopes));
183
404
  switch (expression.type) {
184
405
  case 'Literal':
185
406
  return 'regex' in expression || 'bigint' in expression ? UNRESOLVED : { ok: true, value: expression.value };
186
- case 'TemplateLiteral': {
187
- // Only tagged templates can lack a cooked value, so these strings are always present.
188
- let text = expression.quasis[0].value.cooked;
189
- for (const [index, part] of expression.expressions.entries()) {
190
- const resolved = this.resolve(part, scopes);
191
- if (!resolved.ok)
192
- return resolved;
193
- if (!isPrimitive(resolved.value))
194
- return UNRESOLVED;
195
- text += String(resolved.value) + expression.quasis[index + 1].value.cooked;
196
- }
197
- return { ok: true, value: text };
198
- }
199
- case 'BinaryExpression': {
200
- if (!BINARY.has(expression.operator))
201
- return UNRESOLVED;
202
- const left = this.resolve(expression.left, scopes);
203
- if (!left.ok)
204
- return left;
205
- const right = this.resolve(expression.right, scopes);
206
- if (!right.ok)
207
- return right;
208
- if (!isPrimitive(left.value) || !isPrimitive(right.value))
209
- return UNRESOLVED;
210
- return binary(expression.operator, left.value, right.value);
211
- }
212
- case 'LogicalExpression': {
213
- const left = this.resolve(expression.left, scopes);
214
- if (!left.ok)
215
- return left;
216
- const useLeft = expression.operator === '&&'
217
- ? !left.value
218
- : expression.operator === '||'
219
- ? Boolean(left.value)
220
- : left.value !== null && left.value !== undefined;
221
- return useLeft ? left : this.resolve(expression.right, scopes);
222
- }
223
- case 'ConditionalExpression': {
224
- const test = this.resolve(expression.test, scopes);
225
- if (!test.ok)
226
- return test;
227
- return this.resolve(test.value ? expression.consequent : expression.alternate, scopes);
228
- }
229
407
  case 'CallExpression':
230
- return this.call(expression, scopes);
231
- case 'UnaryExpression': {
232
- if (expression.operator !== '!' && expression.operator !== '-')
233
- return UNRESOLVED;
234
- const argument = this.resolve(expression.argument, scopes);
235
- if (!argument.ok)
236
- return argument;
237
- if (expression.operator === '!')
238
- return { ok: true, value: !argument.value };
239
- return typeof argument.value === 'number' ? { ok: true, value: -argument.value } : UNRESOLVED;
240
- }
408
+ return (this.iteratorMethods.get(expression)?.result ??
409
+ this.builtinCall(expression, scopes, (operand) => this.resolve(operand, scopes)));
241
410
  case 'ArrayExpression': {
242
411
  const items = [];
243
412
  for (const element of expression.elements) {
244
413
  if (element === null)
245
414
  return UNRESOLVED;
415
+ if (element.type === 'SpreadElement') {
416
+ const spread = this.iteratorSpreads.get(element.argument);
417
+ if (spread) {
418
+ if (spread.outcome !== 'next' || !spread.complete || !spread.values.every((item) => item.ok))
419
+ return UNRESOLVED;
420
+ for (const item of spread.values)
421
+ items.push(item.value);
422
+ continue;
423
+ }
424
+ }
246
425
  const resolved = this.resolve(element.type === 'SpreadElement' ? element.argument : element, scopes);
247
426
  if (!resolved.ok)
248
427
  return resolved;
@@ -260,6 +439,7 @@ export class Values {
260
439
  }
261
440
  case 'ObjectExpression': {
262
441
  const object = {};
442
+ const define = (key, value) => Object.defineProperty(object, key, { value, enumerable: true, configurable: true, writable: true });
263
443
  for (const property of expression.properties) {
264
444
  if (property.type === 'SpreadElement') {
265
445
  const spread = this.resolve(property.argument, scopes);
@@ -267,7 +447,8 @@ export class Values {
267
447
  return spread;
268
448
  if (!isPlainObject(spread.value))
269
449
  return UNRESOLVED;
270
- Object.assign(object, spread.value);
450
+ for (const [key, value] of Object.entries(spread.value))
451
+ define(key, value);
271
452
  continue;
272
453
  }
273
454
  if (property.kind !== 'init' || property.method)
@@ -280,7 +461,13 @@ export class Values {
280
461
  const value = this.resolve(property.value, scopes);
281
462
  if (!value.ok)
282
463
  return value;
283
- object[String(key.value)] = value.value;
464
+ // Only an ordinary __proto__: initializer sets the prototype. Computed keys and spreads define own data.
465
+ if (key.value === '__proto__' && !property.computed && !property.shorthand) {
466
+ if (value.value === null || typeof value.value === 'object')
467
+ Object.setPrototypeOf(object, value.value);
468
+ }
469
+ else
470
+ define(String(key.value), value.value);
284
471
  }
285
472
  return { ok: true, value: object };
286
473
  }
@@ -290,9 +477,14 @@ export class Values {
290
477
  const namespace = this.namespaceMember(expression, scopes);
291
478
  if (namespace)
292
479
  return namespace;
293
- const own = this.ownProperty(expression, scopes);
480
+ if (propertyName(expression) === 'name') {
481
+ const name = this.functionName(expression.object, scopes);
482
+ if (name)
483
+ return name;
484
+ }
485
+ const own = this.dataProperty(expression, scopes);
294
486
  if (own)
295
- return own;
487
+ return own.value ? this.resolveConstant(own.value, own.scopes) : { ok: true, value: undefined };
296
488
  const object = this.resolve(expression.object, scopes);
297
489
  if (!object.ok)
298
490
  return object;
@@ -323,7 +515,7 @@ export class Values {
323
515
  return this.lookup(name, scopes)?.binding.declaration ?? null;
324
516
  }
325
517
  /**
326
- * The function a name is declared as, with `function name() {}` or a variable holding one; null for any other name.
518
+ * The local function a name refers to, including one reached through a constant alias.
327
519
  * A parameter is not its function, though the function declares it.
328
520
  */
329
521
  functionOf(name, scopes) {
@@ -331,18 +523,31 @@ export class Values {
331
523
  }
332
524
  /** `functionOf()`, with the scopes around the function's declaration, in which its body is read. */
333
525
  functionWithScopes(name, scopes) {
334
- const found = this.lookup(name, scopes);
335
- const declaration = found?.binding.declaration;
336
- // A class is bound like a function, but has no body to walk.
337
- const declared = found?.binding.kind === 'function' && declaration?.type === 'FunctionDeclaration';
338
- const init = declaration?.type === 'VariableDeclarator' && declaration.init ? unwrap(declaration.init) : null;
339
- const fn = declared ? declaration : isFunctionNode(init) ? init : null;
340
- return fn && found ? { fn, scopes: found.scopes } : null;
526
+ const seen = new Set();
527
+ let found = this.lookup(name, scopes);
528
+ while (found) {
529
+ const { binding } = found;
530
+ const declaration = binding.declaration;
531
+ if (binding.kind === 'function' && declaration.type === 'FunctionDeclaration')
532
+ return { fn: declaration, scopes: found.scopes };
533
+ if (declaration.type !== 'VariableDeclarator' || !declaration.init)
534
+ return null;
535
+ const init = unwrap(declaration.init);
536
+ if (isFunctionNode(init))
537
+ return { fn: init, scopes: found.scopes };
538
+ if (binding.kind !== 'const' || init.type !== 'Identifier' || seen.has(declaration))
539
+ return null;
540
+ seen.add(declaration);
541
+ found = this.lookup(init.name, binding.scopes);
542
+ }
543
+ return null;
341
544
  }
342
545
  /** The value a `const` name is declared with, and the scopes around that declaration; null for any other name. */
343
- constantOf(name, scopes) {
546
+ constantOf(name, scopes, unchanged = false) {
344
547
  const binding = this.lookup(name, scopes)?.binding;
345
- return binding?.kind === 'const' ? { init: binding.init, scopes: binding.scopes } : null;
548
+ if (binding?.kind !== 'const' || (unchanged && this.changedAtLoad(binding.scopes[0], binding, name)))
549
+ return null;
550
+ return { init: binding.init, scopes: binding.scopes };
346
551
  }
347
552
  /** The name a module exports that a name imported from it stands for, `default` for a default import; null otherwise. */
348
553
  importedName(name, scopes) {
@@ -362,11 +567,60 @@ export class Values {
362
567
  const binding = this.lookup(name, scopes)?.binding;
363
568
  if (binding?.kind !== 'other')
364
569
  return false;
365
- return this.calls.findLast((item) => item.fn === binding.declaration)?.parameters.has(name) ?? false;
570
+ return this.boundValue(name, binding.declaration) !== undefined;
366
571
  }
367
- /** Whether a name refers to a function or class declared in the file. */
368
- isFunction(name, scopes) {
369
- return this.lookup(name, scopes)?.binding.kind === 'function';
572
+ /** A local function's name. Bun reads the declared name and receives undefined for an unnamed function. */
573
+ functionName(expression, scopes, bun) {
574
+ let node = unwrap(expression);
575
+ let inferred = '';
576
+ let modified = false;
577
+ const seen = new Set();
578
+ while (node.type === 'Identifier' || node.type === 'MemberExpression') {
579
+ if (node.type === 'MemberExpression') {
580
+ const own = this.dataProperty(node, scopes);
581
+ if (!own?.value || seen.has(own.value))
582
+ return null;
583
+ seen.add(own.value);
584
+ inferred = bun && (own.computed || (own.staticField && !bun.inferStaticFieldNames)) ? '' : own.name;
585
+ node = unwrap(own.value);
586
+ scopes = own.scopes;
587
+ continue;
588
+ }
589
+ const found = this.lookup(node.name, scopes);
590
+ if (!found || seen.has(found.binding.declaration))
591
+ return null;
592
+ seen.add(found.binding.declaration);
593
+ modified ||= this.changedAtLoad(found.scopes[0], found.binding, node.name);
594
+ inferred = node.name;
595
+ scopes = found.scopes;
596
+ const binding = found.binding;
597
+ if (binding.kind === 'function')
598
+ node = binding.declaration;
599
+ else if (binding.kind === 'const')
600
+ node = unwrap(binding.init);
601
+ else
602
+ return null;
603
+ }
604
+ if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
605
+ if (modified)
606
+ return UNRESOLVED;
607
+ const declared = { ok: true, value: node.id?.name ?? inferred };
608
+ if (bun)
609
+ return declared;
610
+ const member = this.classProperty(node.body, 'name', false, scopes);
611
+ if (member === null)
612
+ return declared;
613
+ if (member === 'unknown')
614
+ return UNRESOLVED;
615
+ return member.value ? this.resolveConstant(member.value, scopes) : { ok: true, value: undefined };
616
+ }
617
+ if (node.type === 'FunctionDeclaration' ||
618
+ node.type === 'FunctionExpression' ||
619
+ node.type === 'ArrowFunctionExpression') {
620
+ const name = node.type === 'ArrowFunctionExpression' ? inferred : (node.id?.name ?? inferred);
621
+ return modified ? UNRESOLVED : { ok: true, value: bun && name === '' ? undefined : name };
622
+ }
623
+ return null;
370
624
  }
371
625
  propertyKey(key, computed, scopes) {
372
626
  if (computed)
@@ -380,7 +634,7 @@ export class Values {
380
634
  * `JSON.stringify()` with one argument, `String()`, and a string's `toUpperCase()`, `toLowerCase()` and `trim()` and
381
635
  * an array's `join()`.
382
636
  */
383
- call(expression, scopes) {
637
+ builtinCall(expression, scopes, read) {
384
638
  const callee = unwrap(expression.callee);
385
639
  if (expression.optional || expression.arguments.some((item) => item.type === 'SpreadElement'))
386
640
  return UNRESOLVED;
@@ -389,12 +643,12 @@ export class Values {
389
643
  callee.name === 'String' &&
390
644
  args.length === 1 &&
391
645
  this.isGlobal('String', scopes)) {
392
- const value = this.resolve(args[0], scopes);
646
+ const value = read(args[0]);
393
647
  return value.ok && isPrimitive(value.value)
394
648
  ? { ok: true, value: String(value.value) }
395
649
  : value.ok
396
650
  ? UNRESOLVED
397
- : value;
651
+ : { ok: false, importedFrom: value.importedFrom };
398
652
  }
399
653
  if (callee.type !== 'MemberExpression' || callee.computed)
400
654
  return UNRESOLVED;
@@ -403,9 +657,9 @@ export class Values {
403
657
  if (target.type === 'Identifier' && (target.name === 'Object' || target.name === 'JSON')) {
404
658
  if (!this.isGlobal(target.name, scopes) || args.length !== 1)
405
659
  return UNRESOLVED;
406
- const value = this.resolve(args[0], scopes);
660
+ const value = read(args[0]);
407
661
  if (!value.ok)
408
- return value;
662
+ return { ok: false, importedFrom: value.importedFrom };
409
663
  if (target.name === 'JSON')
410
664
  return method === 'stringify' ? { ok: true, value: JSON.stringify(value.value) } : UNRESOLVED;
411
665
  if (!isPlainObject(value.value) && !Array.isArray(value.value))
@@ -416,9 +670,9 @@ export class Values {
416
670
  return { ok: true, value: Object.values(value.value) };
417
671
  return method === 'entries' ? { ok: true, value: Object.entries(value.value) } : UNRESOLVED;
418
672
  }
419
- const object = this.resolve(callee.object, scopes);
673
+ const object = read(callee.object);
420
674
  if (!object.ok)
421
- return object;
675
+ return { ok: false, importedFrom: object.importedFrom };
422
676
  const text = object.value;
423
677
  if (typeof text === 'string' && args.length === 0 && STRING_METHODS.has(method)) {
424
678
  return {
@@ -428,9 +682,9 @@ export class Values {
428
682
  }
429
683
  if (!Array.isArray(text) || method !== 'join' || args.length > 1 || !text.every(isPrimitive))
430
684
  return UNRESOLVED;
431
- const separator = args.length === 0 ? { ok: true, value: ',' } : this.resolve(args[0], scopes);
685
+ const separator = args.length === 0 ? { ok: true, value: ',' } : read(args[0]);
432
686
  if (!separator.ok)
433
- return separator;
687
+ return { ok: false, importedFrom: separator.importedFrom };
434
688
  return typeof separator.value === 'string' ? { ok: true, value: text.join(separator.value) } : UNRESOLVED;
435
689
  }
436
690
  /** Whether a name refers to a global, as no scope around the use declares it. */
@@ -449,7 +703,7 @@ export class Values {
449
703
  const required = binding.imported === 'default' && binding.declaration.type === 'VariableDeclarator';
450
704
  if (binding.imported !== null && !required)
451
705
  return null;
452
- if (required && this.changedAtLoad(scopes[0]).has(object.name))
706
+ if (required && this.changedAtLoad(scopes[0], binding, object.name))
453
707
  return { ok: false, importedFrom: binding.source };
454
708
  const key = expression.computed
455
709
  ? this.resolve(expression.property, scopes)
@@ -458,40 +712,107 @@ export class Values {
458
712
  return { ok: false, importedFrom: binding.source };
459
713
  return this.imported(binding.source, String(key.value));
460
714
  }
461
- /**
462
- * A property of an object written out in a constant the module keeps, worked out alone, as `TITLES.fill` in
463
- * `const TITLES = { fill: 'fills a limit order', format: (venue) => venue }`, whatever its other values are. Null when
464
- * a spread, a key the scan can't work out, or a getter or method of that name may hold it.
465
- */
466
- ownProperty(expression, scopes) {
467
- const object = unwrap(expression.object);
468
- if (object.type !== 'Identifier')
469
- return null;
470
- const binding = this.lookup(object.name, scopes)?.binding;
471
- if (binding?.kind !== 'const' || this.changedAtLoad(binding.scopes[0]).has(object.name))
472
- return null;
473
- const init = unwrap(binding.init);
474
- if (init.type !== 'ObjectExpression')
475
- return null;
715
+ /** A local data property, in the scope where its value was defined. */
716
+ dataProperty(expression, scopes) {
476
717
  const key = expression.computed
477
718
  ? this.resolve(expression.property, scopes)
478
719
  : { ok: true, value: propertyName(expression) };
479
720
  if (!key.ok || !isKey(key.value))
480
721
  return null;
481
- let found = null;
482
- for (const property of init.properties) {
722
+ const name = String(key.value);
723
+ let object = unwrap(expression.object);
724
+ let inferredName = '';
725
+ let prototype = false;
726
+ let inherited = false;
727
+ const seen = new Set();
728
+ for (;;) {
729
+ if (object.type === 'Identifier') {
730
+ const found = this.lookup(object.name, scopes);
731
+ if (!found ||
732
+ this.changedAtLoad(found.scopes[0], found.binding, object.name) ||
733
+ seen.has(found.binding.declaration))
734
+ return null;
735
+ seen.add(found.binding.declaration);
736
+ inferredName = object.name;
737
+ const binding = found.binding;
738
+ if (binding.kind === 'const')
739
+ object = unwrap(binding.init);
740
+ else if (binding.declaration.type === 'ClassDeclaration')
741
+ object = binding.declaration;
742
+ else
743
+ return null;
744
+ scopes = found.scopes;
745
+ continue;
746
+ }
747
+ if (object.type === 'MemberExpression') {
748
+ if (inherited || prototype || propertyName(object) !== 'prototype')
749
+ return null;
750
+ prototype = true;
751
+ object = unwrap(object.object);
752
+ continue;
753
+ }
754
+ if (object.type !== 'ClassDeclaration' && object.type !== 'ClassExpression')
755
+ break;
756
+ if (object.decorators.length)
757
+ return null;
758
+ const member = this.classProperty(object.body, name, prototype, scopes);
759
+ if (member === 'unknown')
760
+ return null;
761
+ if (member)
762
+ return member;
763
+ // Each class installs its own prototype.constructor before it installs any computed methods.
764
+ if (prototype && name === 'constructor')
765
+ return { name: object.id?.name ?? inferredName, computed: false, value: object, scopes };
766
+ if (!object.superClass)
767
+ return null;
768
+ inherited = true;
769
+ object = unwrap(object.superClass);
770
+ inferredName = '';
771
+ }
772
+ if (object.type !== 'ObjectExpression' || prototype || inherited)
773
+ return null;
774
+ for (let index = object.properties.length - 1; index >= 0; index--) {
775
+ const property = object.properties[index];
483
776
  if (property.type === 'SpreadElement')
484
777
  return null;
485
- const name = this.propertyKey(property.key, property.computed, binding.scopes);
486
- if (!name.ok || !isKey(name.value))
778
+ const candidate = this.propertyKey(property.key, property.computed, scopes);
779
+ if (!candidate.ok || !isKey(candidate.value))
487
780
  return null;
488
- if (String(name.value) !== String(key.value))
781
+ if (String(candidate.value) === name)
782
+ return property.kind === 'init' ? { name, computed: property.computed, value: property.value, scopes } : null;
783
+ }
784
+ return null;
785
+ }
786
+ /** Methods are installed first. Static fields and blocks can then replace them, in source order. */
787
+ classProperty(body, name, prototype, scopes) {
788
+ let method = null;
789
+ let field = null;
790
+ for (const member of body.body) {
791
+ if (member.type === 'StaticBlock') {
792
+ field = 'unknown';
489
793
  continue;
490
- if (property.kind !== 'init' || property.method)
491
- return null;
492
- found = property.value;
794
+ }
795
+ if (!('key' in member) || member.static === prototype || member.key.type === 'PrivateIdentifier')
796
+ continue;
797
+ const key = this.propertyKey(member.key, member.computed, scopes);
798
+ if (key.ok && isKey(key.value) && String(key.value) !== name)
799
+ continue;
800
+ const known = key.ok && isKey(key.value) && !member.decorators.length;
801
+ if (member.type === 'MethodDefinition') {
802
+ if (member.kind === 'constructor')
803
+ continue;
804
+ method =
805
+ known && member.kind === 'method'
806
+ ? { name, computed: member.computed, value: member.value, scopes }
807
+ : 'unknown';
808
+ }
809
+ else if (!prototype && member.type === 'PropertyDefinition' && !member.declare) {
810
+ field = known ? { name, computed: member.computed, value: member.value, scopes, staticField: true } : 'unknown';
811
+ }
812
+ else if (member.type === 'AccessorProperty')
813
+ method = 'unknown';
493
814
  }
494
- return found && this.resolve(found, binding.scopes);
815
+ return field ?? method;
495
816
  }
496
817
  imported(source, name) {
497
818
  const resolved = name !== null && this.imports ? this.imports(source, name) : UNRESOLVED;
@@ -536,7 +857,7 @@ export class Values {
536
857
  const binding = this.lookup(name, scopes)?.binding;
537
858
  if (binding?.kind === 'import')
538
859
  return resolved.arrayLength ?? null;
539
- if (binding?.kind !== 'const' || seen.has(name) || this.changedAtLoad(binding.scopes[0]).has(name))
860
+ if (binding?.kind !== 'const' || seen.has(name) || this.changedAtLoad(binding.scopes[0], binding, name))
540
861
  return null;
541
862
  return this.lengthOf(binding.init, binding.scopes, new Set([...seen, name]));
542
863
  }
@@ -547,7 +868,7 @@ export class Values {
547
868
  const binding = this.lookup(expression.name, scopes)?.binding;
548
869
  if (binding?.kind !== 'const' || seen.has(expression.name))
549
870
  return null;
550
- if (this.changedAtLoad(binding.scopes[0]).has(expression.name))
871
+ if (this.changedAtLoad(binding.scopes[0], binding, expression.name))
551
872
  return null;
552
873
  return this.keyCount(binding.init, binding.scopes, new Set([...seen, expression.name]));
553
874
  }
@@ -570,13 +891,13 @@ export class Values {
570
891
  const { binding } = found;
571
892
  switch (binding.kind) {
572
893
  case 'const':
573
- if (this.changedAtLoad(binding.scopes[0]).has(name))
894
+ if (this.changedAtLoad(binding.scopes[0], binding, name))
574
895
  return UNRESOLVED;
575
896
  return this.resolveConstant(binding.init, binding.scopes);
576
897
  case 'enum':
577
898
  return { ok: true, value: this.enumMembers(binding.node, binding.scopes) };
578
899
  case 'import':
579
- if (this.changedAtLoad(scopes[0]).has(name))
900
+ if (this.changedAtLoad(scopes[0], binding, name))
580
901
  return { ok: false, importedFrom: binding.source };
581
902
  return this.imported(binding.source, binding.imported);
582
903
  case 'function':
@@ -587,17 +908,27 @@ export class Values {
587
908
  }
588
909
  /** A parameter of a function read at one of its calls: the argument given there. */
589
910
  resolveParameter(name, fn) {
590
- const call = this.calls.findLast((item) => item.fn === fn);
591
- return call?.parameters.get(name)?.() ?? UNRESOLVED;
911
+ return this.boundValue(name, fn)?.() ?? UNRESOLVED;
912
+ }
913
+ boundValue(name, declaration) {
914
+ for (let i = this.calls.length - 1; i >= 0; i--) {
915
+ const call = this.calls[i];
916
+ if (call.fn === declaration)
917
+ return call.parameters.get(name);
918
+ const caught = call.locals?.catches.get(declaration);
919
+ if (caught)
920
+ return caught.get(name);
921
+ }
922
+ return undefined;
592
923
  }
593
924
  /** Names the module changes while it loads, as in `WATCHED.push('MSFT')`, whose declared value isn't the one used. */
594
- changedAtLoad(program) {
925
+ changedAtLoad(program, binding, name) {
595
926
  let names = this.changed.get(program);
596
927
  if (!names) {
597
- names = changedNames(program);
928
+ names = changedNames(program, this);
598
929
  this.changed.set(program, names);
599
930
  }
600
- return names;
931
+ return names.has(mutationKey(binding.declaration, name));
601
932
  }
602
933
  resolveConstant(init, scopes) {
603
934
  const cache = this.calls.at(-1)?.cache ?? this.cache;
@@ -704,16 +1035,16 @@ const MUTATING_METHODS = new Set([
704
1035
  const OBJECT_MUTATORS = new Set(['assign', 'defineProperty', 'defineProperties', 'setPrototypeOf']);
705
1036
  /** The identifier a member chain such as `ROUTING.venues[0]` starts from; null for any other expression. */
706
1037
  function rootName(node) {
707
- let current = unwrap(node);
1038
+ let current = unwrapChain(node);
708
1039
  while (current.type === 'MemberExpression')
709
- current = unwrap(current.object);
1040
+ current = unwrapChain(current.object);
710
1041
  return current.type === 'Identifier' ? current.name : null;
711
1042
  }
712
1043
  /**
713
1044
  * Names assigned to or changed anywhere in `body`, its functions included: `x = ...`, `x++`, `[x] = ...`,
714
1045
  * `for (x of ...)`, `x.push(...)`.
715
1046
  */
716
- export function assignedNames(body) {
1047
+ export function assignedNames(body, includeMemberWrites = true) {
717
1048
  const names = new Set();
718
1049
  const targets = (target) => {
719
1050
  switch (target?.type) {
@@ -744,9 +1075,12 @@ export function assignedNames(body) {
744
1075
  if (node.type === 'ForOfStatement' || node.type === 'ForInStatement')
745
1076
  targets(node.left);
746
1077
  // A parameter whose value the function changes, as in `list.push(...)`, no longer holds the argument's value.
747
- const changed = mutatedName(node);
748
- if (changed !== null)
749
- names.add(changed);
1078
+ if (includeMemberWrites) {
1079
+ const object = mutatedObject(node);
1080
+ const changed = object && rootName(object);
1081
+ if (changed !== null)
1082
+ names.add(changed);
1083
+ }
750
1084
  for (const child of childNodes(node))
751
1085
  walk(child);
752
1086
  };
@@ -783,65 +1117,210 @@ function calleeNames(callee) {
783
1117
  names.push(current.name);
784
1118
  return names;
785
1119
  }
786
- /** The name a mutation changes the value of, as in `WATCHED.push('MSFT')` or `ROUTING.market = 'NYSE'`; or null. */
787
- function mutatedName(node) {
1120
+ /** The object a write or a mutating call acts on. */
1121
+ function mutatedObject(node) {
788
1122
  switch (node.type) {
789
1123
  case 'AssignmentExpression':
790
- return node.left.type === 'MemberExpression' ? rootName(node.left) : null;
1124
+ return node.left.type === 'MemberExpression' ? node.left.object : null;
791
1125
  case 'UpdateExpression':
792
- return node.argument.type === 'MemberExpression' ? rootName(node.argument) : null;
793
- case 'UnaryExpression':
794
- return node.operator === 'delete' ? rootName(node.argument) : null;
1126
+ return node.argument.type === 'MemberExpression' ? node.argument.object : null;
1127
+ case 'UnaryExpression': {
1128
+ const target = unwrapChain(node.argument);
1129
+ return node.operator === 'delete' && target.type === 'MemberExpression' ? target.object : null;
1130
+ }
795
1131
  case 'CallExpression': {
796
- const callee = unwrap(node.callee);
1132
+ const callee = unwrapChain(node.callee);
797
1133
  if (callee.type !== 'MemberExpression')
798
1134
  return null;
799
1135
  const method = propertyName(callee) ?? '';
800
1136
  if (MUTATING_METHODS.has(method))
801
- return rootName(callee.object);
1137
+ return callee.object;
802
1138
  const target = node.arguments.at(0);
803
- return OBJECT_MUTATORS.has(method) && rootName(callee.object) === 'Object' && target ? rootName(target) : null;
1139
+ return OBJECT_MUTATORS.has(method) &&
1140
+ rootName(callee.object) === 'Object' &&
1141
+ target &&
1142
+ target.type !== 'SpreadElement'
1143
+ ? target
1144
+ : null;
804
1145
  }
805
1146
  default:
806
1147
  return null;
807
1148
  }
808
1149
  }
1150
+ /** The immediately preceding initializer or assignment can identify a mutable function alias. */
1151
+ function previousAliasValue(object, previous) {
1152
+ if (object.type !== 'Identifier')
1153
+ return object;
1154
+ if (previous?.type === 'VariableDeclaration')
1155
+ previous = previous.declarations[previous.declarations.length - 1];
1156
+ if (previous?.type === 'VariableDeclarator') {
1157
+ if (previous.id.type === 'Identifier' && previous.id.name === object.name && previous.init)
1158
+ return previous.init;
1159
+ previous = previous.init;
1160
+ }
1161
+ else if (previous?.type === 'ExpressionStatement')
1162
+ previous = previous.expression;
1163
+ return previous?.type === 'AssignmentExpression' &&
1164
+ previous.operator === '=' &&
1165
+ previous.left.type === 'Identifier' &&
1166
+ previous.left.name === object.name
1167
+ ? previous.right
1168
+ : object;
1169
+ }
809
1170
  /**
810
1171
  * Names whose value the module changes while it loads, as describe callbacks and helpers run then: a member assigned
811
1172
  * or deleted, a mutating method such as `push()`, or `Object.assign()`, also through an alias such as
812
1173
  * `const all = SIDES`. The callbacks of tests, hooks and steps run after the runner has read titles and tables, so
813
1174
  * what they change doesn't count.
814
1175
  */
815
- function changedNames(program) {
816
- const names = new Set();
817
- const aliases = new Map();
818
- const walk = (node, later) => {
819
- if (!later) {
820
- const name = mutatedName(node);
821
- if (name !== null)
822
- names.add(name);
1176
+ function changedNames(program, values) {
1177
+ const targets = [];
1178
+ const declarations = [];
1179
+ const callsByOwner = new Map();
1180
+ const methodWrites = [];
1181
+ const suspensions = [];
1182
+ const accessors = { get: new Set(), set: new Set() };
1183
+ const accesses = [];
1184
+ const writeOnly = new Set();
1185
+ const patterns = new Map();
1186
+ const addSite = (owner, site) => {
1187
+ const sites = callsByOwner.get(owner);
1188
+ if (sites)
1189
+ sites.push(site);
1190
+ else
1191
+ callsByOwner.set(owner, [site]);
1192
+ };
1193
+ const walk = (node, ancestors, statement, owner, before = null) => {
1194
+ const scopes = isScopeNode(node) ? [...ancestors, node] : ancestors;
1195
+ if (node.type === 'FunctionDeclaration' || isFunctionNode(node)) {
1196
+ owner = node;
1197
+ statement = null;
1198
+ }
1199
+ else if (node.type === 'ClassBody')
1200
+ statement = null;
1201
+ if (owner === program) {
1202
+ if (node.type === 'ForOfStatement' && node.await)
1203
+ suspensions.push(node.right.end);
1204
+ if (node.type === 'AwaitExpression')
1205
+ suspensions.push(node.end);
1206
+ if (node.type === 'VariableDeclaration' && node.kind === 'await using')
1207
+ suspensions.push(scopes[scopes.length - 1].end);
1208
+ }
1209
+ const target = mutatedObject(node);
1210
+ if (target) {
1211
+ targets.push({ value: target, scopes, statement, owner });
1212
+ const member = node.type === 'AssignmentExpression' && node.operator === '='
1213
+ ? node.left
1214
+ : node.type === 'UnaryExpression'
1215
+ ? unwrapChain(node.argument)
1216
+ : null;
1217
+ if (member?.type === 'MemberExpression') {
1218
+ writeOnly.add(member);
1219
+ if (node.type === 'AssignmentExpression')
1220
+ accesses.push({ member, assigned: node.right, site: { scopes, statement, runsLater: true }, owner });
1221
+ const name = memberKey(member);
1222
+ const unconditional = statement?.type === 'ExpressionStatement' && statement.expression === node;
1223
+ methodWrites.push({
1224
+ expression: node,
1225
+ name,
1226
+ object: unconditional ? previousAliasValue(member.object, before) : member.object,
1227
+ assigned: node.type === 'AssignmentExpression' ? node.right : null,
1228
+ scopes,
1229
+ owner,
1230
+ end: node.end,
1231
+ unconditional,
1232
+ });
1233
+ }
823
1234
  }
824
- if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier' && node.init?.type === 'Identifier') {
825
- aliases.set(node.id.name, node.init.name);
1235
+ if (node.type === 'Property' && node.kind !== 'init')
1236
+ accessors[node.kind].add(propertyKey(node));
1237
+ if (node.type === 'MemberExpression' && !writeOnly.has(node))
1238
+ accesses.push({ member: node, site: { scopes, statement, runsLater: false }, owner });
1239
+ if (node.type === 'VariableDeclaration' && node.kind === 'const')
1240
+ for (const declaration of node.declarations)
1241
+ if (declaration.init && (declaration.id.type === 'ObjectPattern' || declaration.id.type === 'ArrayPattern'))
1242
+ patterns.set(declaration, patternReferences(declaration.id, declaration.init, scopes));
1243
+ if (node.type === 'VariableDeclarator' && node.init)
1244
+ declarations.push({ pattern: node.id, source: node.init, scopes, statement, end: node.end, replaces: false });
1245
+ if (node.type === 'AssignmentExpression' && node.operator === '=')
1246
+ declarations.push({
1247
+ pattern: node.left,
1248
+ source: node.right,
1249
+ scopes,
1250
+ statement,
1251
+ end: node.end,
1252
+ replaces: statement?.type === 'ExpressionStatement' && statement.expression === node,
1253
+ });
1254
+ if (node.type === 'IfStatement' && node.test.type === 'Literal' && typeof node.test.value === 'boolean') {
1255
+ const branch = node.test.value ? node.consequent : node.alternate;
1256
+ if (branch)
1257
+ walk(branch, scopes, statement, owner);
1258
+ return;
826
1259
  }
827
1260
  if (node.type === 'CallExpression') {
828
1261
  const callee = calleeNames(node.callee);
829
- const runsLater = !later && callee.some((name) => RUNS_LATER.has(name)) && !callee.some((name) => RUNS_NOW.has(name));
830
- walk(node.callee, later);
1262
+ const runsLater = callee.some((name) => RUNS_LATER.has(name)) && !callee.some((name) => RUNS_NOW.has(name));
1263
+ const member = unwrapChain(node.callee);
1264
+ const bindOwner = member.type === 'MemberExpression' && propertyName(member) === 'bind'
1265
+ ? previousAliasValue(member.object, before)
1266
+ : undefined;
1267
+ const method = member.type === 'MemberExpression' ? propertyName(member) : null;
1268
+ const arrayMethod = method && MUTATING_METHODS.has(method) ? method : undefined;
1269
+ addSite(owner, { call: node, scopes, statement, runsLater, bindOwner, arrayMethod });
1270
+ walk(node.callee, scopes, statement, owner);
831
1271
  for (const argument of node.arguments)
832
- walk(argument, later || (runsLater && isFunctionNode(argument)));
1272
+ if (!runsLater || !isFunctionNode(argument))
1273
+ walk(argument, scopes, statement, owner);
833
1274
  return;
834
1275
  }
835
- for (const child of childNodes(node))
836
- walk(child, later);
1276
+ const sequence = node === program ||
1277
+ node.type === 'BlockStatement' ||
1278
+ node.type === 'VariableDeclaration' ||
1279
+ node.type === 'SequenceExpression';
1280
+ let previous = before;
1281
+ for (const child of childNodes(node)) {
1282
+ const top = node === program && (child.type === 'VariableDeclaration' || child.type === 'ExpressionStatement');
1283
+ const prior = sequence
1284
+ ? previous
1285
+ : node.type === 'ExpressionStatement' || node.type === 'VariableDeclarator'
1286
+ ? before
1287
+ : null;
1288
+ walk(child, scopes, top ? child : statement, owner, prior);
1289
+ previous = child;
1290
+ }
837
1291
  };
838
- walk(program, false);
839
- for (const name of [...names]) {
840
- for (let target = aliases.get(name); target !== undefined && !names.has(target); target = aliases.get(target)) {
841
- names.add(target);
1292
+ walk(program, [], null, program);
1293
+ if (!targets.length)
1294
+ return new Set();
1295
+ if (accessors.get.size || accessors.set.size)
1296
+ for (const { member, owner, site, assigned } of accesses) {
1297
+ const accessor = assigned ? 'set' : 'get';
1298
+ const keys = accessors[accessor];
1299
+ const key = memberKey(member);
1300
+ if (!keys.size || (key !== null && !keys.has(key) && !keys.has(null)))
1301
+ continue;
1302
+ const call = {
1303
+ type: 'CallExpression',
1304
+ callee: member,
1305
+ arguments: assigned ? [assigned] : [],
1306
+ optional: false,
1307
+ start: member.start,
1308
+ end: member.end,
1309
+ };
1310
+ addSite(owner, { ...site, call, accessor });
842
1311
  }
843
- }
844
- return names;
1312
+ const graph = mutationCalls(program, callsByOwner, methodWrites, suspensions.sort((a, b) => a - b), values, patterns);
1313
+ return changedReferences(declarations, targets, graph, (identifier, scopes) => {
1314
+ const declaration = identifier === 'this'
1315
+ ? (scopes.findLast((scope) => scope.type === 'FunctionDeclaration' || scope.type === 'FunctionExpression') ??
1316
+ null)
1317
+ : values.declarationOf(identifier, scopes);
1318
+ return declaration && mutationKey(declaration, identifier);
1319
+ });
1320
+ }
1321
+ /** A declaration can bind several names in a parameter list or destructuring pattern. */
1322
+ function mutationKey(declaration, name) {
1323
+ return `${declaration.start}:${name}`;
845
1324
  }
846
1325
  function isPlainObject(value) {
847
1326
  return typeof value === 'object' && value !== null && !Array.isArray(value);