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
@@ -0,0 +1,774 @@
1
+ import { isFunctionNode, propertyName, unwrapChain } from "./ast.js";
2
+ import { DEFINED, enclosingFactory, helperParameters, literalArguments, memberKey, property, referenceOperands, } from "./mutation-references.js";
3
+ import { analyzeReturns, capturedResult, conditionArgument, markReturnedLocals } from "./mutation-returns.js";
4
+ import { arrayArguments, arrayPropertyReader, changesMember, } from "./mutation-arrays.js";
5
+ import { compareEdits, HelperReadTimes, visibleAt } from "./mutation-order.js";
6
+ const UNKNOWN = { targets: [], definite: false };
7
+ function functionKey(target) {
8
+ if (!target.bound && !target.factory)
9
+ return String(target.fn.start);
10
+ return JSON.stringify([
11
+ target.fn.start,
12
+ target.factory?.id,
13
+ ...(target.bound ?? []).map((capture) => [capture.call.start, capture.caller.id, capture.factory?.id]),
14
+ ]);
15
+ }
16
+ function alternatives(choices) {
17
+ const targets = new Map(choices.flatMap((choice) => choice.targets.map((target) => [functionKey(target), target])));
18
+ return { targets: [...targets.values()], definite: choices.every((choice) => choice.definite) };
19
+ }
20
+ function localFunction(target, context) {
21
+ let factory = context.factory;
22
+ while (factory && !target.scopes.includes(factory.fn))
23
+ factory = factory.context.factory;
24
+ return { targets: [factory ? { ...target, factory } : target], definite: true };
25
+ }
26
+ /** Calls with the same callback arguments share a walk of the helper body, including when it calls itself. */
27
+ export function mutationCalls(program, callsByOwner, methodWrites, suspensions, values, patterns) {
28
+ const calls = new Map();
29
+ const contexts = new Map();
30
+ const times = new Map();
31
+ const pending = [];
32
+ const sites = new Map();
33
+ const getterSites = new Map();
34
+ for (const [owner, entries] of callsByOwner)
35
+ for (const site of entries) {
36
+ sites.set(site.call, { site, owner });
37
+ if (site.accessor === 'get')
38
+ getterSites.set(site.call.callee, site);
39
+ }
40
+ const constantValue = (expression, scopes) => {
41
+ let value = unwrapChain(expression);
42
+ const seen = new Set();
43
+ while (value.type === 'Identifier' && !seen.has(value)) {
44
+ seen.add(value);
45
+ const alias = values.constantOf(value.name, scopes);
46
+ if (!alias)
47
+ break;
48
+ value = unwrapChain(alias.init);
49
+ scopes = alias.scopes;
50
+ }
51
+ return { value, scopes };
52
+ };
53
+ const objectWrites = new Map();
54
+ for (const write of methodWrites) {
55
+ const { value } = constantValue(write.object, write.scopes);
56
+ if (value.type !== 'ObjectExpression' && value.type !== 'ArrayExpression')
57
+ continue;
58
+ const writes = objectWrites.get(value);
59
+ if (writes)
60
+ writes.push(write);
61
+ else
62
+ objectWrites.set(value, [write]);
63
+ }
64
+ const writesAt = (container, key, time) => (objectWrites.get(container) ?? []).filter((write) => (key === null || write.name === null || write.name === key) &&
65
+ (write.owner !== program || visibleAt(write, time)));
66
+ const arrayChanges = new Map();
67
+ const helperEdits = methodWrites.filter((write) => write.owner !== program);
68
+ for (const { site, owner } of sites.values()) {
69
+ if (!site.arrayMethod)
70
+ continue;
71
+ const member = unwrapChain(site.call.callee);
72
+ const { value } = constantValue(member.object, site.scopes);
73
+ const edit = {
74
+ expression: site.call,
75
+ object: member.object,
76
+ args: arrayArguments(site.call, site.scopes),
77
+ name: site.arrayMethod,
78
+ scopes: site.scopes,
79
+ owner,
80
+ end: site.call.end,
81
+ unconditional: site.statement?.type === 'ExpressionStatement' && site.statement.expression === site.call,
82
+ };
83
+ if (value.type !== 'ArrayExpression') {
84
+ if (owner !== program)
85
+ helperEdits.push(edit);
86
+ continue;
87
+ }
88
+ const changes = arrayChanges.get(value) ?? [...(objectWrites.get(value) ?? [])];
89
+ changes.push(edit);
90
+ arrayChanges.set(value, changes);
91
+ }
92
+ for (const changes of arrayChanges.values())
93
+ changes.sort(compareEdits);
94
+ const arrayReaders = new Map();
95
+ const spreadLength = (source, scopes) => {
96
+ const { value } = constantValue(source, scopes);
97
+ if (value.type !== 'ArrayExpression' || arrayChanges.has(value) || objectWrites.has(value))
98
+ return null;
99
+ const elements = [...literalArguments(value.elements)];
100
+ return elements.some((element) => element?.type === 'SpreadElement') ? null : elements.length;
101
+ };
102
+ const memberSources = (value, scopes, key, time) => {
103
+ const changes = arrayChanges.get(value);
104
+ if (changes && value.type === 'ArrayExpression') {
105
+ let read = arrayReaders.get(value);
106
+ if (!read) {
107
+ read = arrayPropertyReader(value, scopes, program, changes, spreadLength);
108
+ arrayReaders.set(value, read);
109
+ }
110
+ const edited = read(key, time);
111
+ if (edited)
112
+ return edited;
113
+ }
114
+ const sources = Array.from(property({ value, path: [] }, key), (ref) => ({ ref, scopes }));
115
+ let accessorOnly = sources.every(({ ref }) => ref?.accessor);
116
+ for (const write of writesAt(value, key, time)) {
117
+ if (accessorOnly && write.assigned)
118
+ continue;
119
+ if (!write.assigned)
120
+ accessorOnly = false;
121
+ if (write.unconditional && time !== null && key !== null && key === write.name)
122
+ sources.length = 0;
123
+ sources.push({ scopes: write.scopes, ref: write.assigned && { value: write.assigned, path: [] } });
124
+ }
125
+ return sources;
126
+ };
127
+ // A spread reads its source immediately. An object contained in the copied slot is still shared.
128
+ const spreadTime = (value, scopes, invocationTime) => scopes.some((scope) => scope.type === 'FunctionDeclaration' || isFunctionNode(scope))
129
+ ? invocationTime
130
+ : value.start;
131
+ const reflectSources = (source, readTimes, invocationTime, seen = new Set()) => {
132
+ const { ref } = source;
133
+ if (ref.capture)
134
+ return [source];
135
+ let { value, scopes } = constantValue(ref.value, source.scopes);
136
+ let path = ref.path;
137
+ if (value.type === 'MemberExpression') {
138
+ const object = constantValue(value.object, scopes);
139
+ if (arrayChanges.has(object.value)) {
140
+ path = [memberKey(value), ...path];
141
+ readTimes = [spreadTime(value, scopes, invocationTime), ...readTimes];
142
+ ({ value, scopes } = object);
143
+ }
144
+ }
145
+ if (!path.length || value.type !== 'ArrayExpression' || seen.has(value))
146
+ return [source];
147
+ seen.add(value);
148
+ const [key, ...suffix] = path;
149
+ const [time, ...later] = readTimes;
150
+ const choices = [];
151
+ for (const { ref: child, scopes: at } of memberSources(value, scopes, key, time))
152
+ if (child)
153
+ choices.push({
154
+ source: { ...source, scopes: at, ref: { ...ref, value: child.value, path: [...child.path, ...suffix] } },
155
+ times: [...child.path.map(() => spreadTime(child.value, at, invocationTime)), ...later],
156
+ });
157
+ return choices.flatMap((choice) => reflectSources(choice.source, choice.times, invocationTime, new Set(seen)));
158
+ };
159
+ const bodyOperations = new Map();
160
+ for (const [owner, entries] of callsByOwner)
161
+ if (owner !== program)
162
+ bodyOperations.set(owner, [...entries]);
163
+ for (const edit of helperEdits) {
164
+ const entries = bodyOperations.get(edit.owner);
165
+ if (entries)
166
+ entries.push(edit);
167
+ else
168
+ bodyOperations.set(edit.owner, [edit]);
169
+ }
170
+ const endOf = (operation) => 'call' in operation ? operation.call.end : operation.end;
171
+ for (const entries of bodyOperations.values())
172
+ entries.sort((a, b) => endOf(a) - endOf(b));
173
+ const schedules = new Map();
174
+ const writeSchedule = (body) => {
175
+ let schedule = schedules.get(body);
176
+ if (schedule)
177
+ return schedule;
178
+ let awaitedAt = Infinity;
179
+ const straight = new Set();
180
+ if (body.type === 'BlockStatement') {
181
+ let unconditional = true;
182
+ for (const statement of body.body) {
183
+ if (statement.type === 'ExpressionStatement') {
184
+ if (unconditional)
185
+ straight.add(statement.expression);
186
+ if (statement.expression.type === 'AwaitExpression')
187
+ awaitedAt = Math.min(awaitedAt, statement.end);
188
+ }
189
+ else if (statement.type === 'VariableDeclaration') {
190
+ if (statement.declarations.some((decl) => decl.init?.type === 'AwaitExpression'))
191
+ awaitedAt = Math.min(awaitedAt, statement.end);
192
+ }
193
+ else
194
+ unconditional = false;
195
+ }
196
+ }
197
+ else
198
+ straight.add(body);
199
+ schedule = { straight, awaitedAt };
200
+ schedules.set(body, schedule);
201
+ return schedule;
202
+ };
203
+ let writeVersion = 0;
204
+ const observedMembers = [];
205
+ const readTimes = new HelperReadTimes();
206
+ const invocationSources = (frame, parent) => {
207
+ return (expression, at) => {
208
+ const source = constantValue(expression, at);
209
+ const name = source.value.type === 'Identifier' ? source.value.name : 'this';
210
+ const declared = source.value.type === 'ThisExpression'
211
+ ? source.scopes.findLast((scope) => scope.type === 'FunctionDeclaration' || scope.type === 'FunctionExpression')
212
+ : source.value.type === 'Identifier' && values.declarationOf(name, source.scopes);
213
+ const binding = declared && (declared === frame.fn ? frame : enclosingFactory(frame.context, declared));
214
+ if (!binding)
215
+ return { parameter: false, choices: [{ value: expression, scopes: at }] };
216
+ const ownerBody = binding.fn.body;
217
+ if (values.assignedIn(ownerBody, false).has(name) || values.assignedIn(ownerBody).has('arguments'))
218
+ return { parameter: true, choices: [] };
219
+ const choices = binding.parameters
220
+ .filter((param) => param.name === name && param.ref.rest === undefined)
221
+ .flatMap((param) => reflectSources(param, param.ref.path.map(() => binding.call.start), binding.call.start))
222
+ .filter((param) => !param.ref.path.length)
223
+ .flatMap((param) => {
224
+ const scopes = param.ref.capture?.scopes ?? param.scopes;
225
+ return parent ? parent(param.ref.value, scopes).choices : [{ value: param.ref.value, scopes }];
226
+ });
227
+ return { parameter: true, choices };
228
+ };
229
+ };
230
+ const invocationEdits = (frame, end, definite, active, record, parent) => {
231
+ const entry = { owner: frame.fn, position: frame.fn.start, end, repeated: active.has(frame.fn) };
232
+ record(entry);
233
+ const operations = bodyOperations.get(frame.fn);
234
+ if (!operations || entry.repeated || frame.fn.generator)
235
+ return [entry];
236
+ active.add(frame.fn);
237
+ const result = new Set([entry]);
238
+ const append = (step) => {
239
+ record(step);
240
+ result.add(step);
241
+ };
242
+ const children = new Map();
243
+ const sources = invocationSources(frame, parent);
244
+ const { straight, awaitedAt } = writeSchedule(frame.fn.body);
245
+ for (const operation of operations) {
246
+ const expression = 'call' in operation ? operation.call : operation.expression;
247
+ const when = expression.start >= awaitedAt ? (suspensions.find((at) => at >= end) ?? Infinity) : end;
248
+ const certain = definite && !frame.fn.async && straight.has(expression);
249
+ if ('call' in operation) {
250
+ const invocations = visit(operation, frame.context);
251
+ for (const child of invocations) {
252
+ const unique = certain && child.complete && invocations.length === 1;
253
+ // Identical forwarding calls can form a branching graph. Keep each possible source once when an edit repeats.
254
+ const key = JSON.stringify([
255
+ child.context.id,
256
+ when,
257
+ unique,
258
+ child.parameters.map((param) => [
259
+ param.name,
260
+ param.ref.path,
261
+ param.ref.rest,
262
+ param.ref.capture?.call.start,
263
+ sources(param.ref.value, param.ref.capture?.scopes ?? param.scopes).choices.map((choice) => choice.value.start),
264
+ ]),
265
+ ]);
266
+ const previous = children.get(key);
267
+ let edits;
268
+ let reusable = true;
269
+ if (previous?.version === writeVersion)
270
+ edits = previous.edits;
271
+ else {
272
+ const reads = new Map();
273
+ observedMembers.push(reads);
274
+ edits = invocationEdits(child, when, unique, active, record, sources);
275
+ observedMembers.pop();
276
+ reusable = !edits.some((step) => 'change' in step &&
277
+ [...(reads.get(step.container) ?? [])].some((key) => changesMember(step.change, key)));
278
+ }
279
+ for (const edit of edits) {
280
+ if (result.delete(edit)) {
281
+ if ('change' in edit)
282
+ edit.change.unconditional = false;
283
+ else
284
+ edit.repeated = true;
285
+ record(edit, true);
286
+ }
287
+ result.add(edit);
288
+ }
289
+ if (reusable)
290
+ children.set(key, { edits, version: writeVersion });
291
+ }
292
+ append({ owner: frame.fn, position: expression.end, end: when });
293
+ continue;
294
+ }
295
+ const write = operation;
296
+ const containers = sources(write.object, write.scopes);
297
+ if (!containers.parameter)
298
+ continue;
299
+ const unconditional = certain && containers.choices.length === 1;
300
+ const timing = { owner: program, end: when, unconditional };
301
+ const edits = [];
302
+ if ('args' in write) {
303
+ const args = arrayArguments(write.expression, write.scopes, (expr) => sources(expr, write.scopes).choices);
304
+ edits.push({ ...write, ...timing, args });
305
+ }
306
+ else {
307
+ const replacements = write.assigned
308
+ ? sources(write.assigned, write.scopes).choices
309
+ : [{ value: null, scopes: write.scopes }];
310
+ for (const replacement of replacements)
311
+ edits.push({
312
+ ...write,
313
+ ...timing,
314
+ unconditional: unconditional && replacements.length === 1,
315
+ assigned: replacement.value,
316
+ scopes: replacement.scopes,
317
+ });
318
+ }
319
+ for (const container of containers.choices) {
320
+ const { value } = constantValue(container.value, container.scopes);
321
+ if (value.type !== 'ArrayExpression' && (value.type !== 'ObjectExpression' || 'args' in write))
322
+ continue;
323
+ for (const change of edits)
324
+ append({ container: value, change });
325
+ }
326
+ append({ owner: frame.fn, position: expression.end, end: when });
327
+ }
328
+ active.delete(frame.fn);
329
+ return [...result];
330
+ };
331
+ const recordArgumentWrites = (invocation, unique) => {
332
+ const definite = unique &&
333
+ invocation.statement?.type === 'ExpressionStatement' &&
334
+ invocation.statement.expression === invocation.call;
335
+ let index = 0;
336
+ // Record each write before resolving the next callee, which may itself come from the edited slot.
337
+ invocationEdits(invocation, invocation.call.end, definite, new Set(), (step, repeated) => {
338
+ if (!('change' in step)) {
339
+ const time = step.end === invocation.call.end ? { end: step.end, step: index } : null;
340
+ readTimes.add(step.owner, invocation.call.start, step.position, time, !!step.repeated);
341
+ return;
342
+ }
343
+ const { container, change } = step;
344
+ writeVersion++;
345
+ change.step = ++index;
346
+ const assignments = objectWrites.get(container) ?? [];
347
+ const changes = arrayChanges.get(container) ?? ('args' in change ? [...assignments] : undefined);
348
+ if (changes) {
349
+ if (!repeated)
350
+ changes.push(change);
351
+ arrayChanges.set(container, changes.sort(compareEdits));
352
+ }
353
+ if (!('args' in change)) {
354
+ if (!repeated)
355
+ assignments.push(change);
356
+ objectWrites.set(container, assignments.sort(compareEdits));
357
+ }
358
+ arrayReaders.clear();
359
+ });
360
+ };
361
+ const returns = new Map();
362
+ const returnedBy = (fn) => {
363
+ let result = returns.get(fn);
364
+ if (!result) {
365
+ result = analyzeReturns(fn);
366
+ returns.set(fn, result);
367
+ }
368
+ return result;
369
+ };
370
+ const returnedFns = new Set();
371
+ let nextContext = 0;
372
+ let nextInvocation = 0;
373
+ const containers = new Map();
374
+ const contextFor = (owner, functions, factory, inputs, time) => {
375
+ let groups = contexts.get(owner);
376
+ if (!groups) {
377
+ groups = new Map();
378
+ contexts.set(owner, groups);
379
+ }
380
+ const signature = JSON.stringify([
381
+ factory?.id,
382
+ inputs?.size ? [time, ...[...inputs].map(([name, source]) => [name, source.value.start])] : null,
383
+ ...[...functions].map(([name, choice]) => [name, choice.definite, choice.targets.map(functionKey)]),
384
+ ]);
385
+ let context = groups.get(signature);
386
+ if (!context) {
387
+ const factories = new Map();
388
+ const remember = (frame) => {
389
+ for (const [call, ancestor] of frame.caller.factories)
390
+ factories.set(call, ancestor);
391
+ if (!factories.has(frame.call))
392
+ factories.set(frame.call, frame);
393
+ };
394
+ if (factory) {
395
+ remember(factory);
396
+ returnedFns.add(owner);
397
+ }
398
+ const ancestors = new Map();
399
+ for (const choice of functions.values())
400
+ for (const fn of choice.targets) {
401
+ if (fn.factory)
402
+ remember(fn.factory);
403
+ for (const capture of fn.bound ?? []) {
404
+ for (const [call, ancestor] of capture.caller.ancestors)
405
+ ancestors.set(call, ancestor);
406
+ if (!ancestors.has(capture.call))
407
+ ancestors.set(capture.call, capture.caller);
408
+ }
409
+ }
410
+ context = { owner, functions, id: nextContext++, ancestors, factory, factories };
411
+ if (inputs?.size)
412
+ containers.set(context, inputs);
413
+ groups.set(signature, context);
414
+ }
415
+ return context;
416
+ };
417
+ const activate = (context, time) => {
418
+ const previous = times.get(context);
419
+ if (previous === null || previous === time)
420
+ return;
421
+ times.set(context, previous === undefined ? time : null);
422
+ pending.push(context);
423
+ };
424
+ const root = contextFor(program, new Map());
425
+ const containerFor = (expr, scopes, context) => {
426
+ const source = constantValue(expr, scopes);
427
+ const { value } = source;
428
+ if (value.type === 'ObjectExpression' || value.type === 'ArrayExpression') {
429
+ if (!source.scopes.some((scope) => scope.type === 'FunctionDeclaration' || isFunctionNode(scope)))
430
+ return source;
431
+ }
432
+ else if (value.type === 'Identifier' || value.type === 'ThisExpression') {
433
+ const name = value.type === 'Identifier' ? value.name : 'this';
434
+ const declared = value.type === 'Identifier'
435
+ ? values.declarationOf(name, source.scopes)
436
+ : source.scopes.findLast((scope) => scope.type === 'FunctionExpression' || scope.type === 'FunctionDeclaration');
437
+ const binding = declared === context.owner ? context : declared && enclosingFactory(context, declared)?.context;
438
+ if (binding)
439
+ return containers.get(binding)?.get(name);
440
+ }
441
+ };
442
+ const callable = (expression, scopes, context, seen = new Set(), frame, path = []) => {
443
+ let value = unwrapChain(expression);
444
+ if (path.length && (value.type === 'Identifier' || value.type === 'ThisExpression')) {
445
+ const input = containerFor(value, scopes, context);
446
+ if (input)
447
+ return callable(input.value, input.scopes, root, seen, frame, path);
448
+ }
449
+ while (value.type === 'Identifier') {
450
+ const constant = values.constantOf(value.name, scopes);
451
+ if (!constant) {
452
+ const declared = values.declarationOf(value.name, scopes);
453
+ const pattern = declared && patterns.get(declared);
454
+ const options = pattern?.entries.get(value.name);
455
+ if (pattern && options && !seen.has(value)) {
456
+ seen.add(value);
457
+ const choices = [];
458
+ for (const { ref, fromDefault } of options) {
459
+ if (fromDefault && choices.length && choices.every((choice) => choice.definite))
460
+ continue;
461
+ const selection = ref.path.map((key) => ({ key, time: declared.start }));
462
+ choices.push(ref.rest === undefined
463
+ ? callable(ref.value, pattern.scopes, context, new Set(seen), frame, [...selection, ...path])
464
+ : UNKNOWN);
465
+ }
466
+ return alternatives(choices);
467
+ }
468
+ if (path.length)
469
+ return UNKNOWN;
470
+ const local = values.functionWithScopes(value.name, scopes);
471
+ if (local)
472
+ return localFunction(local, context);
473
+ const environment = declared === context.owner ? context : declared && enclosingFactory(context, declared)?.context;
474
+ if (environment)
475
+ return environment.functions.get(value.name) ?? UNKNOWN;
476
+ return UNKNOWN;
477
+ }
478
+ if (seen.has(value))
479
+ return UNKNOWN;
480
+ seen.add(value);
481
+ scopes = constant.scopes;
482
+ value = unwrapChain(constant.init);
483
+ }
484
+ if (value.type === 'MemberExpression' && !seen.has(value)) {
485
+ seen.add(value);
486
+ const time = context.owner === program ? value.start : readTimes.at(context.owner, value.start, times.get(context) ?? null);
487
+ const site = getterSites.get(value);
488
+ const step = { key: memberKey(value), time, read: site && { site, context } };
489
+ return callable(value.object, scopes, context, seen, frame, [step, ...path]);
490
+ }
491
+ if (path.length && (value.type === 'ObjectExpression' || value.type === 'ArrayExpression')) {
492
+ const [step, ...rest] = path;
493
+ if (!step.accessor) {
494
+ for (const reads of observedMembers) {
495
+ const keys = reads.get(value);
496
+ if (keys)
497
+ keys.add(step.key);
498
+ else
499
+ reads.set(value, new Set([step.key]));
500
+ }
501
+ }
502
+ const choices = [];
503
+ for (const { ref, scopes: at } of memberSources(value, scopes, step.key, step.time)) {
504
+ if (ref?.accessor && !step.accessor) {
505
+ choices.push(step.read ? produced(step.read.site, step.read.context, seen, rest) : UNKNOWN);
506
+ continue;
507
+ }
508
+ const selected = ref && (ref.path.length || ref.accessor === step.accessor ? ref.value : ref.counterpart);
509
+ if (ref && selected) {
510
+ const projection = ref.path.map((key) => ({
511
+ ...step,
512
+ key,
513
+ time: spreadTime(ref.value, at, frame?.statement ? frame.call.start : step.time),
514
+ }));
515
+ choices.push(callable(selected, at, context, new Set(seen), frame, [...projection, ...rest]));
516
+ }
517
+ else
518
+ choices.push(UNKNOWN);
519
+ }
520
+ return alternatives(choices);
521
+ }
522
+ if (value.type === 'CallExpression' && !seen.has(value)) {
523
+ const entry = sites.get(value);
524
+ if (!entry)
525
+ return UNKNOWN;
526
+ const { site, owner } = entry;
527
+ const evaluatedIn = owner === program ? root : (enclosingFactory(context, owner)?.context ?? context);
528
+ seen.add(value);
529
+ if (site.bindOwner && !path.length) {
530
+ // Re-entering the same bind site while forwarding a callback must reach a finite set of contexts.
531
+ const caller = site.statement ? root : (context.ancestors.get(value) ?? context);
532
+ const capture = { call: value, scopes: site.scopes, statement: site.statement, caller };
533
+ const original = callable(site.bindOwner, site.scopes, site.statement ? root : context, seen);
534
+ const targets = [];
535
+ let definite = original.definite;
536
+ for (const helper of original.targets) {
537
+ const { methods: options, complete } = methodChoices(helper, 'bind', caller, site.statement ? value.start : (times.get(caller) ?? null), seen);
538
+ if (options.includes(null)) {
539
+ const bound = helper.bound ?? [];
540
+ const previous = bound.findLast((entry) => entry.call === value);
541
+ if (previous?.repeated)
542
+ targets.push(helper);
543
+ else {
544
+ const next = previous ? { ...capture, caller: context, repeated: true } : capture;
545
+ targets.push({ ...helper, bound: [...bound, next] });
546
+ }
547
+ }
548
+ if (!complete || options.length !== 1 || options[0] !== null)
549
+ definite = false;
550
+ }
551
+ const inherited = { targets, definite };
552
+ if (definite)
553
+ return inherited;
554
+ const own = produced(site, evaluatedIn, seen);
555
+ return targets.length ? alternatives([inherited, own]) : own;
556
+ }
557
+ return produced(site, evaluatedIn, seen, path);
558
+ }
559
+ if (!path.length && isFunctionNode(value))
560
+ return localFunction({ fn: value, scopes }, context);
561
+ if (value.type === 'ConditionalExpression' || value.type === 'LogicalExpression') {
562
+ const test = unwrapChain(value.type === 'ConditionalExpression' ? value.test : value.left);
563
+ const branches = referenceOperands(value, conditionArgument(test, scopes, frame, values));
564
+ return alternatives(branches.map((expr) => callable(expr, scopes, context, new Set(seen), frame, path)));
565
+ }
566
+ if (value.type === 'SequenceExpression')
567
+ return callable(value.expressions[value.expressions.length - 1], scopes, context, seen, frame, path);
568
+ if (value.type === 'AssignmentExpression' && value.operator === '=')
569
+ return callable(value.right, scopes, context, seen, frame, path);
570
+ return UNKNOWN;
571
+ };
572
+ const produced = (site, context, seen, path = []) => {
573
+ const choices = [];
574
+ for (const frame of visit(site, context)) {
575
+ const { candidates, complete } = returnedBy(frame.fn);
576
+ if (!complete || !frame.complete)
577
+ choices.push(UNKNOWN);
578
+ for (const result of candidates) {
579
+ const choice = callable(result.expression, [...frame.parameterScopes, ...result.around], frame.context, new Set(seen), frame, path);
580
+ choices.push({
581
+ targets: choice.targets.map((target) => capturedResult(target, frame)),
582
+ definite: choice.definite,
583
+ });
584
+ }
585
+ }
586
+ return choices.length ? alternatives(choices) : UNKNOWN;
587
+ };
588
+ const methodChoices = (helper, name, caller, time, seen = new Set()) => {
589
+ let complete = true;
590
+ let possible = [null];
591
+ for (const write of methodWrites) {
592
+ if ((write.name !== null && write.name !== name) ||
593
+ (write.owner === program && time !== null && write.end > time))
594
+ continue;
595
+ const owners = callable(write.object, write.scopes, caller, new Set(seen));
596
+ if (!owners.targets.some((owner) => functionKey(owner) === functionKey(helper)))
597
+ continue;
598
+ if (write.name === name &&
599
+ write.unconditional &&
600
+ time !== null &&
601
+ owners.definite &&
602
+ owners.targets.length === 1) {
603
+ possible = [];
604
+ complete = true;
605
+ }
606
+ if (write.assigned) {
607
+ const replacement = callable(write.assigned, write.scopes, caller, new Set(seen));
608
+ possible.push(...replacement.targets);
609
+ complete &&= replacement.definite;
610
+ }
611
+ else
612
+ possible.push(null);
613
+ }
614
+ return { methods: possible, complete };
615
+ };
616
+ const targetsFor = (site, caller, time) => {
617
+ const { call, scopes } = site;
618
+ const callee = unwrapChain(call.callee);
619
+ const select = (choice, mode, methodOwner) => ({
620
+ targets: choice.targets.map((helper) => ({ helper, mode, methodOwner })),
621
+ complete: choice.definite,
622
+ });
623
+ if (callee.type !== 'MemberExpression')
624
+ return select(callable(callee, scopes, caller), 'direct');
625
+ const name = propertyName(callee);
626
+ const object = unwrapChain(callee.object);
627
+ if (site.accessor) {
628
+ const step = { key: memberKey(callee), accessor: site.accessor, time };
629
+ return select(callable(object, scopes, caller, new Set(), undefined, [step]), 'own', object);
630
+ }
631
+ if (name === 'apply' &&
632
+ object.type === 'Identifier' &&
633
+ object.name === 'Reflect' &&
634
+ !values.declarationOf('Reflect', scopes)) {
635
+ const args = {
636
+ type: 'ArrayExpression',
637
+ elements: [...literalArguments(call.arguments)],
638
+ start: call.start,
639
+ end: call.end,
640
+ };
641
+ return select(callable(args, scopes, caller, new Set(), undefined, [{ key: '0', time }]), 'reflect');
642
+ }
643
+ const own = select(callable(callee, scopes, caller), 'own', object);
644
+ if (name !== 'call' && name !== 'apply' && name !== 'bind')
645
+ return own;
646
+ const receivers = callable(site.bindOwner ?? object, scopes, caller);
647
+ let inheritedComplete = receivers.definite;
648
+ for (const helper of receivers.targets) {
649
+ const { methods, complete } = methodChoices(helper, name, caller, time);
650
+ inheritedComplete &&= complete;
651
+ for (const method of methods) {
652
+ if (method)
653
+ own.targets.push({ helper: method, mode: 'own', methodOwner: object });
654
+ else
655
+ own.targets.push({ helper, mode: name });
656
+ }
657
+ }
658
+ return { targets: own.targets, complete: own.complete || inheritedComplete };
659
+ };
660
+ const resolving = new Set();
661
+ const visit = (callSite, caller) => {
662
+ if (resolving.has(callSite.call))
663
+ return [];
664
+ resolving.add(callSite.call);
665
+ const found = [];
666
+ const { call, scopes, statement, runsLater } = callSite;
667
+ const time = statement ? call.start : (times.get(caller) ?? null);
668
+ const { targets: helpers, complete } = targetsFor(callSite, caller, time);
669
+ if (!helpers.length && !runsLater) {
670
+ for (const arg of call.arguments) {
671
+ if (arg.type === 'SpreadElement')
672
+ continue;
673
+ for (const callback of callable(arg, scopes, caller).targets)
674
+ activate(contextFor(callback.fn, new Map(), callback.factory), null);
675
+ }
676
+ }
677
+ for (const { helper, mode, methodOwner } of helpers) {
678
+ if (mode === 'bind')
679
+ continue;
680
+ let atSite = calls.get(call);
681
+ if (!atSite) {
682
+ atSite = [];
683
+ calls.set(call, atSite);
684
+ }
685
+ const key = functionKey(helper);
686
+ let invocation = atSite.find((entry) => entry.caller === caller && entry.key === key && entry.mode === mode);
687
+ const fresh = !invocation;
688
+ if (!invocation) {
689
+ const site = {
690
+ call,
691
+ scopes,
692
+ statement,
693
+ mode,
694
+ methodOwner,
695
+ bound: helper.bound,
696
+ key,
697
+ fn: helper.fn,
698
+ parameterScopes: [...helper.scopes, helper.fn],
699
+ };
700
+ const supplied = helperParameters(site, (expr, capture) => {
701
+ const at = capture?.scopes ?? scopes;
702
+ return (DEFINED.has(constantValue(expr, at).value.type) || callable(expr, at, capture?.caller ?? caller).definite);
703
+ });
704
+ const parameters = mode === 'reflect'
705
+ ? supplied.flatMap((source) => reflectSources(source, source.ref.path.map(() => time), time))
706
+ : supplied;
707
+ const choices = new Map();
708
+ for (const param of parameters) {
709
+ const choice = param.ref.path.length || param.ref.rest !== undefined
710
+ ? UNKNOWN
711
+ : callable(param.ref.value, param.ref.capture?.scopes ?? param.scopes, param.ref.capture?.caller ?? caller);
712
+ // A property selected by destructuring may also be absent, even when one candidate is a function.
713
+ const direct = param.fromDefault || (param.ref.capture?.call ?? call).arguments.includes(param.ref.value);
714
+ const possible = { targets: choice.targets, definite: direct && choice.definite };
715
+ const previous = choices.get(param.name);
716
+ if (previous)
717
+ previous.options.push(possible);
718
+ else
719
+ choices.set(param.name, { source: param, options: [possible] });
720
+ }
721
+ const functions = new Map();
722
+ const inputs = new Map();
723
+ const body = helper.fn.body;
724
+ for (const [name, { source, options }] of choices) {
725
+ const choice = alternatives(options);
726
+ if (choice.targets.length)
727
+ functions.set(name, choice);
728
+ if (options.length === 1 &&
729
+ source.ref.path.length === 0 &&
730
+ source.ref.rest === undefined &&
731
+ !values.assignedIn(body, false).has(name) &&
732
+ !values.assignedIn(body).has('arguments')) {
733
+ const input = containerFor(source.ref.value, source.ref.capture?.scopes ?? source.scopes, source.ref.capture?.caller ?? caller);
734
+ if (input)
735
+ inputs.set(name, input);
736
+ }
737
+ }
738
+ invocation = {
739
+ ...site,
740
+ id: nextInvocation++,
741
+ complete,
742
+ caller,
743
+ parameters,
744
+ context: contextFor(helper.fn, functions, helper.factory, inputs, time),
745
+ };
746
+ atSite.push(invocation);
747
+ }
748
+ found.push(invocation);
749
+ activate(invocation.context, helper.fn.async || helper.fn.generator ? null : time);
750
+ if (fresh && caller === root && helperEdits.length)
751
+ recordArgumentWrites(invocation, complete && helpers.length === 1);
752
+ }
753
+ resolving.delete(callSite.call);
754
+ return found;
755
+ };
756
+ activate(root, null);
757
+ for (const caller of pending)
758
+ for (const site of callsByOwner.get(caller.owner) ?? [])
759
+ visit(site, caller);
760
+ // Returning a local function alone does not run its body.
761
+ for (const invocations of calls.values())
762
+ for (const invocation of invocations) {
763
+ for (const { expression, around } of returnedBy(invocation.fn).candidates)
764
+ markReturnedLocals(expression, [...invocation.parameterScopes, ...around], invocation.fn, values, returnedFns);
765
+ }
766
+ const returnedContexts = new Map([...returnedFns].map((fn) => [fn, []]));
767
+ const callTimes = new Map();
768
+ for (const [context, time] of times) {
769
+ returnedContexts.get(context.owner)?.push(context);
770
+ const previous = callTimes.get(context.owner);
771
+ callTimes.set(context.owner, previous === undefined || previous === time ? time : null);
772
+ }
773
+ return { calls, callTimes, returnedContexts, memberSources, readTimes };
774
+ }