test-inventory 0.12.0 → 0.12.2
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/CHANGELOG.md +145 -0
- package/README.md +3 -1
- package/dist/ast.d.ts +7 -2
- package/dist/ast.js +43 -3
- package/dist/case-titles.d.ts +5 -11
- package/dist/case-titles.js +69 -55
- package/dist/config-runs.d.ts +19 -2
- package/dist/config-runs.js +380 -91
- package/dist/config.d.ts +0 -5
- package/dist/config.js +0 -7
- package/dist/dependency-hash.js +1 -32
- package/dist/diagnostics.js +17 -2
- package/dist/diff.d.ts +2 -45
- package/dist/extract.d.ts +13 -3
- package/dist/extract.js +837 -89
- package/dist/frameworks/bun.js +47 -24
- package/dist/frameworks/common.d.ts +4 -2
- package/dist/frameworks/common.js +10 -11
- package/dist/frameworks/jasmine-registration.d.ts +47 -0
- package/dist/frameworks/jasmine-registration.js +632 -0
- package/dist/frameworks/jasmine.d.ts +7 -1
- package/dist/frameworks/jasmine.js +65 -70
- package/dist/frameworks/jest.d.ts +2 -2
- package/dist/frameworks/jest.js +126 -51
- package/dist/frameworks/test-functions.js +3 -1
- package/dist/frameworks/vitest.d.ts +2 -3
- package/dist/frameworks/vitest.js +80 -16
- package/dist/generator-steps.d.ts +56 -0
- package/dist/generator-steps.js +254 -0
- package/dist/model.d.ts +37 -5
- package/dist/mutation-arrays.d.ts +25 -0
- package/dist/mutation-arrays.js +229 -0
- package/dist/mutation-calls.d.ts +33 -0
- package/dist/mutation-calls.js +774 -0
- package/dist/mutation-order.d.ts +19 -0
- package/dist/mutation-order.js +55 -0
- package/dist/mutation-references.d.ts +109 -0
- package/dist/mutation-references.js +462 -0
- package/dist/mutation-returns.d.ts +13 -0
- package/dist/mutation-returns.js +71 -0
- package/dist/node-scripts.d.ts +6 -1
- package/dist/node-scripts.js +79 -11
- package/dist/options.d.ts +2 -0
- package/dist/options.js +6 -1
- package/dist/records.d.ts +5 -3
- package/dist/records.js +81 -28
- package/dist/scan-file.d.ts +18 -4
- package/dist/scan-file.js +44 -5
- package/dist/scan-source.js +2 -1
- package/dist/scan.js +30 -13
- package/dist/types.d.ts +45 -1
- package/dist/values.d.ts +72 -18
- package/dist/values.js +651 -172
- package/dist/vitest-concurrency.d.ts +16 -0
- package/dist/vitest-concurrency.js +55 -0
- package/package.json +6 -6
- package/schema/v1.json +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
export type ArrayReadTime = number | {
|
|
3
|
+
end: number;
|
|
4
|
+
step: number;
|
|
5
|
+
} | null;
|
|
6
|
+
interface TimedEdit {
|
|
7
|
+
end: number;
|
|
8
|
+
step?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function compareEdits(a: TimedEdit, b: TimedEdit): number;
|
|
11
|
+
export declare function visibleAt(change: TimedEdit, time: ArrayReadTime): boolean;
|
|
12
|
+
export declare function upperBound<T>(items: readonly T[], included: (item: T) => boolean): number;
|
|
13
|
+
/** Match a read inside a helper to the writes already made during the same module-level call. */
|
|
14
|
+
export declare class HelperReadTimes {
|
|
15
|
+
private readonly owners;
|
|
16
|
+
add(owner: Node, start: number, position: number, time: ArrayReadTime, repeated: boolean): void;
|
|
17
|
+
at(owner: Node, position: number, start: number | null): ArrayReadTime;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// An enclosing assignment follows the helper on its right-hand side even when their source ranges end together.
|
|
2
|
+
function stepOf(edit) {
|
|
3
|
+
return edit.step ?? Number.MAX_SAFE_INTEGER;
|
|
4
|
+
}
|
|
5
|
+
export function compareEdits(a, b) {
|
|
6
|
+
return a.end - b.end || stepOf(a) - stepOf(b);
|
|
7
|
+
}
|
|
8
|
+
export function visibleAt(change, time) {
|
|
9
|
+
if (time === null)
|
|
10
|
+
return true;
|
|
11
|
+
if (typeof time === 'number')
|
|
12
|
+
return change.end <= time;
|
|
13
|
+
return change.end < time.end || (change.end === time.end && stepOf(change) <= time.step);
|
|
14
|
+
}
|
|
15
|
+
export function upperBound(items, included) {
|
|
16
|
+
let lo = 0, hi = items.length;
|
|
17
|
+
while (lo < hi) {
|
|
18
|
+
const mid = lo + Math.floor((hi - lo) / 2);
|
|
19
|
+
if (included(items[mid]))
|
|
20
|
+
lo = mid + 1;
|
|
21
|
+
else
|
|
22
|
+
hi = mid;
|
|
23
|
+
}
|
|
24
|
+
return lo;
|
|
25
|
+
}
|
|
26
|
+
/** Match a read inside a helper to the writes already made during the same module-level call. */
|
|
27
|
+
export class HelperReadTimes {
|
|
28
|
+
owners = new Map();
|
|
29
|
+
add(owner, start, position, time, repeated) {
|
|
30
|
+
let calls = this.owners.get(owner);
|
|
31
|
+
if (!calls) {
|
|
32
|
+
calls = new Map();
|
|
33
|
+
this.owners.set(owner, calls);
|
|
34
|
+
}
|
|
35
|
+
const prior = calls.get(start);
|
|
36
|
+
if (prior === null)
|
|
37
|
+
return;
|
|
38
|
+
if (repeated || (prior && position === owner.start))
|
|
39
|
+
calls.set(start, null);
|
|
40
|
+
else if (prior)
|
|
41
|
+
prior.push({ position, time });
|
|
42
|
+
else
|
|
43
|
+
calls.set(start, [{ position, time }]);
|
|
44
|
+
}
|
|
45
|
+
at(owner, position, start) {
|
|
46
|
+
if (start === null)
|
|
47
|
+
return null;
|
|
48
|
+
const points = this.owners.get(owner)?.get(start);
|
|
49
|
+
if (points === undefined)
|
|
50
|
+
return start;
|
|
51
|
+
if (points === null)
|
|
52
|
+
return null;
|
|
53
|
+
return points[Math.max(0, upperBound(points, (point) => point.position <= position) - 1)].time;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { ArrayExpression, AssignmentTarget, BindingPattern, CallExpression, Expression, Node } from 'oxc-parser';
|
|
2
|
+
import type { mutationCalls } from './mutation-calls.ts';
|
|
3
|
+
import { type FunctionNode } from './ast.ts';
|
|
4
|
+
type Key = string | null;
|
|
5
|
+
interface Site {
|
|
6
|
+
scopes: readonly Node[];
|
|
7
|
+
/** The top-level declaration or expression being evaluated, when the relative order is known. */
|
|
8
|
+
statement: Node | null;
|
|
9
|
+
}
|
|
10
|
+
export interface MutationTarget extends Site {
|
|
11
|
+
owner: Node;
|
|
12
|
+
value: Expression;
|
|
13
|
+
}
|
|
14
|
+
export interface AliasBinding extends Site {
|
|
15
|
+
pattern: BindingPattern | AssignmentTarget;
|
|
16
|
+
source: Expression;
|
|
17
|
+
end: number;
|
|
18
|
+
replaces: boolean;
|
|
19
|
+
}
|
|
20
|
+
export type InvocationMode = 'direct' | 'call' | 'apply' | 'reflect' | 'own';
|
|
21
|
+
export interface HelperSite extends Site {
|
|
22
|
+
mode: InvocationMode;
|
|
23
|
+
methodOwner?: Expression;
|
|
24
|
+
bound?: CapturedCall[];
|
|
25
|
+
call: CallExpression;
|
|
26
|
+
fn: FunctionNode;
|
|
27
|
+
parameterScopes: readonly Node[];
|
|
28
|
+
}
|
|
29
|
+
export interface CapturedCall extends Site {
|
|
30
|
+
factory?: HelperInvocation;
|
|
31
|
+
repeated?: boolean;
|
|
32
|
+
call: CallExpression;
|
|
33
|
+
caller: CallbackContext;
|
|
34
|
+
}
|
|
35
|
+
export interface ScopedFunction {
|
|
36
|
+
factory?: HelperInvocation;
|
|
37
|
+
bound?: CapturedCall[];
|
|
38
|
+
fn: FunctionNode;
|
|
39
|
+
scopes: readonly Node[];
|
|
40
|
+
}
|
|
41
|
+
export interface CallbackChoice {
|
|
42
|
+
targets: ScopedFunction[];
|
|
43
|
+
/** Every possible value is a known function, so a parameter default cannot be used. */
|
|
44
|
+
definite: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface CallbackContext {
|
|
47
|
+
factory?: HelperInvocation;
|
|
48
|
+
factories: Map<CallExpression, HelperInvocation>;
|
|
49
|
+
owner: Node;
|
|
50
|
+
functions: Map<string, CallbackChoice>;
|
|
51
|
+
id: number;
|
|
52
|
+
ancestors: Map<CallExpression, CallbackContext>;
|
|
53
|
+
}
|
|
54
|
+
export interface ParameterSource extends Site {
|
|
55
|
+
name: string;
|
|
56
|
+
ref: Reference;
|
|
57
|
+
fromDefault: boolean;
|
|
58
|
+
}
|
|
59
|
+
export interface HelperInvocation extends HelperSite {
|
|
60
|
+
/** Every possible callee at this site is known in the calling context. */
|
|
61
|
+
complete: boolean;
|
|
62
|
+
id: number;
|
|
63
|
+
key: string;
|
|
64
|
+
caller: CallbackContext;
|
|
65
|
+
context: CallbackContext;
|
|
66
|
+
parameters: ParameterSource[];
|
|
67
|
+
}
|
|
68
|
+
/** The invocation whose local bindings a nested function captured. */
|
|
69
|
+
export declare function enclosingFactory(context: CallbackContext | undefined, owner: Node): HelperInvocation | undefined;
|
|
70
|
+
export interface Reference {
|
|
71
|
+
value: Expression;
|
|
72
|
+
path: Key[];
|
|
73
|
+
capture?: CapturedCall;
|
|
74
|
+
accessor?: 'get' | 'set';
|
|
75
|
+
counterpart?: Expression;
|
|
76
|
+
parts?: ArgumentPart[];
|
|
77
|
+
/** A rest binding is a shallow copy. Array rest starts at this index in the original array. */
|
|
78
|
+
rest?: number;
|
|
79
|
+
}
|
|
80
|
+
interface ArgumentPart {
|
|
81
|
+
list: Reference;
|
|
82
|
+
offset: number;
|
|
83
|
+
size: number | null;
|
|
84
|
+
}
|
|
85
|
+
export declare const DEFINED: Set<import("oxc-parser").FunctionType | "ArrowFunctionExpression" | "Program" | "Identifier" | "ThisExpression" | "ArrayExpression" | "ObjectExpression" | "Property" | "TemplateLiteral" | "TaggedTemplateExpression" | "TemplateElement" | "MemberExpression" | "CallExpression" | "NewExpression" | "MetaProperty" | "SpreadElement" | "UpdateExpression" | "UnaryExpression" | "BinaryExpression" | "LogicalExpression" | "ConditionalExpression" | "AssignmentExpression" | "ArrayPattern" | "ObjectPattern" | "RestElement" | "AssignmentPattern" | "SequenceExpression" | "Super" | "AwaitExpression" | "ChainExpression" | "ParenthesizedExpression" | "ExpressionStatement" | "Hashbang" | "BlockStatement" | "VariableDeclaration" | "VariableDeclarator" | "EmptyStatement" | "IfStatement" | "DoWhileStatement" | "WhileStatement" | "ForStatement" | "ForInStatement" | "ForOfStatement" | "ContinueStatement" | "BreakStatement" | "ReturnStatement" | "WithStatement" | "SwitchStatement" | "SwitchCase" | "LabeledStatement" | "ThrowStatement" | "TryStatement" | "CatchClause" | "DebuggerStatement" | "YieldExpression" | import("oxc-parser").ClassType | "ClassBody" | import("oxc-parser").MethodDefinitionType | import("oxc-parser").PropertyDefinitionType | "PrivateIdentifier" | "StaticBlock" | import("oxc-parser").AccessorPropertyType | "ImportExpression" | "ImportDeclaration" | "ImportSpecifier" | "ImportDefaultSpecifier" | "ImportNamespaceSpecifier" | "ImportAttribute" | "ExportNamedDeclaration" | "ExportDefaultDeclaration" | "ExportAllDeclaration" | "ExportSpecifier" | "V8IntrinsicExpression" | "Literal" | "JSXElement" | "JSXOpeningElement" | "JSXClosingElement" | "JSXFragment" | "JSXOpeningFragment" | "JSXClosingFragment" | "JSXNamespacedName" | "JSXMemberExpression" | "JSXExpressionContainer" | "JSXEmptyExpression" | "JSXAttribute" | "JSXSpreadAttribute" | "JSXIdentifier" | "JSXSpreadChild" | "JSXText" | "TSEnumDeclaration" | "TSEnumBody" | "TSEnumMember" | "TSTypeAnnotation" | "TSLiteralType" | "TSConditionalType" | "TSUnionType" | "TSIntersectionType" | "TSParenthesizedType" | "TSTypeOperator" | "TSArrayType" | "TSIndexedAccessType" | "TSTupleType" | "TSNamedTupleMember" | "TSOptionalType" | "TSRestType" | "TSAnyKeyword" | "TSStringKeyword" | "TSBooleanKeyword" | "TSNumberKeyword" | "TSNeverKeyword" | "TSIntrinsicKeyword" | "TSUnknownKeyword" | "TSNullKeyword" | "TSUndefinedKeyword" | "TSVoidKeyword" | "TSSymbolKeyword" | "TSThisType" | "TSObjectKeyword" | "TSBigIntKeyword" | "TSTypeReference" | "TSQualifiedName" | "TSTypeParameterInstantiation" | "TSTypeParameter" | "TSTypeParameterDeclaration" | "TSTypeAliasDeclaration" | "TSClassImplements" | "TSInterfaceDeclaration" | "TSInterfaceBody" | "TSPropertySignature" | "TSIndexSignature" | "TSCallSignatureDeclaration" | "TSMethodSignature" | "TSConstructSignatureDeclaration" | "TSInterfaceHeritage" | "TSTypePredicate" | "TSModuleDeclaration" | "TSModuleBlock" | "TSTypeLiteral" | "TSInferType" | "TSTypeQuery" | "TSImportType" | "TSFunctionType" | "TSConstructorType" | "TSMappedType" | "TSTemplateLiteralType" | "TSAsExpression" | "TSSatisfiesExpression" | "TSTypeAssertion" | "TSImportEqualsDeclaration" | "TSExternalModuleReference" | "TSNonNullExpression" | "Decorator" | "TSExportAssignment" | "TSNamespaceExportDeclaration" | "TSInstantiationExpression" | "TSJSDocNullableType" | "TSJSDocNonNullableType" | "TSJSDocUnknownType" | "TSParameterProperty">;
|
|
86
|
+
export declare function memberKey(member: Extract<Expression, {
|
|
87
|
+
type: 'MemberExpression';
|
|
88
|
+
}>): Key;
|
|
89
|
+
/** Which operand supplies a reference when the condition can be decided without reading mutable bindings. */
|
|
90
|
+
export declare function referenceOperands(value: Extract<Expression, {
|
|
91
|
+
type: 'ConditionalExpression' | 'LogicalExpression';
|
|
92
|
+
}>, evaluatedTest?: Expression): Expression[];
|
|
93
|
+
/** Possible references at a property, including a missing value when destructuring could use its default. */
|
|
94
|
+
export declare function property(ref: Reference, key: Key): Generator<Reference | null, void>;
|
|
95
|
+
/** References extracted once when a module evaluates a constant destructuring declaration. */
|
|
96
|
+
export declare function patternReferences(pattern: BindingPattern, source: Expression, scopes: readonly Node[]): {
|
|
97
|
+
entries: Map<string, {
|
|
98
|
+
ref: Reference;
|
|
99
|
+
fromDefault?: boolean;
|
|
100
|
+
}[]>;
|
|
101
|
+
scopes: readonly Node[];
|
|
102
|
+
};
|
|
103
|
+
/** Expand literal array spreads in argument order, preserving holes and spreads of unknown width. */
|
|
104
|
+
export declare function literalArguments(elements: Readonly<ArrayExpression['elements']>): Generator<ArrayExpression['elements'][number], void>;
|
|
105
|
+
/** Bind positional, rest and default references once for both callback discovery and write tracking. */
|
|
106
|
+
export declare function helperParameters(site: HelperSite, isDefined: (expression: Expression, capture?: CapturedCall) => boolean): ParameterSource[];
|
|
107
|
+
/** Follow the container of each write. Replacing a copy's property and mutating a shared child have different owners. */
|
|
108
|
+
export declare function changedReferences(declarations: AliasBinding[], targets: MutationTarget[], { calls, callTimes, returnedContexts, memberSources, readTimes }: ReturnType<typeof mutationCalls>, bindingKey: (name: string, scopes: readonly Node[]) => string | null): Set<string>;
|
|
109
|
+
export {};
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { isFunctionNode, propertyKey, propertyName, returnValues, unwrapChain } from "./ast.js";
|
|
2
|
+
/** The invocation whose local bindings a nested function captured. */
|
|
3
|
+
export function enclosingFactory(context, owner) {
|
|
4
|
+
for (let frame = context?.factory; frame; frame = frame.context.factory)
|
|
5
|
+
if (frame.fn === owner)
|
|
6
|
+
return frame;
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const direct = (value) => ({ value: unwrapChain(value), path: [] });
|
|
10
|
+
export const DEFINED = new Set([
|
|
11
|
+
'Literal',
|
|
12
|
+
'ArrayExpression',
|
|
13
|
+
'ObjectExpression',
|
|
14
|
+
'FunctionExpression',
|
|
15
|
+
'ArrowFunctionExpression',
|
|
16
|
+
'ClassExpression',
|
|
17
|
+
'TemplateLiteral',
|
|
18
|
+
]);
|
|
19
|
+
export function memberKey(member) {
|
|
20
|
+
const key = member.property;
|
|
21
|
+
return key.type === 'Literal' && typeof key.value === 'number' ? String(key.value) : propertyName(member);
|
|
22
|
+
}
|
|
23
|
+
/** Which operand supplies a reference when the condition can be decided without reading mutable bindings. */
|
|
24
|
+
export function referenceOperands(value, evaluatedTest) {
|
|
25
|
+
const conditional = value.type === 'ConditionalExpression';
|
|
26
|
+
const test = unwrapChain(evaluatedTest ?? (conditional ? value.test : value.left));
|
|
27
|
+
const present = !conditional && value.operator === '??';
|
|
28
|
+
let truth = null;
|
|
29
|
+
if (test.type === 'Literal')
|
|
30
|
+
truth = 'regex' in test || (present ? test.value !== null : Boolean(test.value));
|
|
31
|
+
else if (test.type === 'UnaryExpression' && test.operator === 'void')
|
|
32
|
+
truth = false;
|
|
33
|
+
else if (DEFINED.has(test.type) && (present || test.type !== 'TemplateLiteral'))
|
|
34
|
+
truth = true;
|
|
35
|
+
const first = conditional ? value.consequent : value.left;
|
|
36
|
+
const second = conditional ? value.alternate : value.right;
|
|
37
|
+
if (truth === null)
|
|
38
|
+
return [first, second];
|
|
39
|
+
const takeFirst = !conditional && value.operator === '&&' ? !truth : truth;
|
|
40
|
+
return [takeFirst ? first : second];
|
|
41
|
+
}
|
|
42
|
+
/** Possible references at a property, including a missing value when destructuring could use its default. */
|
|
43
|
+
export function* property(ref, key) {
|
|
44
|
+
for (const source of propertySources(ref, key)) {
|
|
45
|
+
yield source && ref.capture ? { ...source, capture: ref.capture } : source;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function* propertySources(ref, key) {
|
|
49
|
+
const value = ref.value;
|
|
50
|
+
if (ref.parts) {
|
|
51
|
+
let position = key === null ? null : Number(key);
|
|
52
|
+
for (const { list, offset, size } of ref.parts) {
|
|
53
|
+
const repeats = list.capture?.repeated && size !== null && size > 0;
|
|
54
|
+
if (position === null || size === null || position < size || repeats) {
|
|
55
|
+
const at = position !== null && repeats ? position % size : position;
|
|
56
|
+
yield* property(list, at === null ? null : String(at + offset));
|
|
57
|
+
if (position !== null && size !== null && position < size)
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
position = repeats || position === null || size === null ? null : position - size;
|
|
61
|
+
}
|
|
62
|
+
yield null;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (ref.path.length || (value.type !== 'ObjectExpression' && value.type !== 'ArrayExpression')) {
|
|
66
|
+
yield { value, path: [...ref.path, key] };
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (value.type === 'ObjectExpression') {
|
|
70
|
+
for (let index = value.properties.length - 1; index >= 0; index--) {
|
|
71
|
+
const entry = value.properties[index];
|
|
72
|
+
if (entry.type === 'SpreadElement') {
|
|
73
|
+
let mayMiss = false;
|
|
74
|
+
for (const source of property(direct(entry.argument), key)) {
|
|
75
|
+
if (!source || source.path.length)
|
|
76
|
+
mayMiss = true;
|
|
77
|
+
if (source)
|
|
78
|
+
yield source;
|
|
79
|
+
}
|
|
80
|
+
if (!mayMiss)
|
|
81
|
+
return;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const name = propertyKey(entry);
|
|
85
|
+
if (key !== null && name !== null && key !== name)
|
|
86
|
+
continue;
|
|
87
|
+
const child = direct(entry.value);
|
|
88
|
+
if (entry.kind !== 'init') {
|
|
89
|
+
child.accessor = entry.kind;
|
|
90
|
+
// Defining the other half of an accessor retains its previous getter or setter.
|
|
91
|
+
if (key !== null && key === name)
|
|
92
|
+
for (let prior = index - 1; prior >= 0; prior--) {
|
|
93
|
+
const earlier = value.properties[prior];
|
|
94
|
+
if (earlier.type === 'SpreadElement')
|
|
95
|
+
break;
|
|
96
|
+
const previousKey = propertyKey(earlier);
|
|
97
|
+
if (previousKey !== key && previousKey !== null)
|
|
98
|
+
continue;
|
|
99
|
+
if (earlier.kind === 'init')
|
|
100
|
+
break;
|
|
101
|
+
if (earlier.kind !== entry.kind) {
|
|
102
|
+
child.counterpart = earlier.value;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
yield child;
|
|
108
|
+
if (key !== null && key === name)
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const index = key === null ? null : Number(key);
|
|
114
|
+
let earliest = 0;
|
|
115
|
+
let afterSpread = false;
|
|
116
|
+
for (const item of value.elements) {
|
|
117
|
+
if (index !== null && earliest > index)
|
|
118
|
+
break;
|
|
119
|
+
if (item?.type === 'SpreadElement') {
|
|
120
|
+
yield* property(direct(item.argument), index === null || afterSpread ? null : String(index - earliest));
|
|
121
|
+
afterSpread = true;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (index === null || afterSpread || earliest === index)
|
|
125
|
+
yield item && direct(item);
|
|
126
|
+
if (!afterSpread && index === earliest)
|
|
127
|
+
return;
|
|
128
|
+
earliest++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
yield null;
|
|
133
|
+
}
|
|
134
|
+
/** Literal data cannot call a getter, evaluate a computed key or change an earlier declaration. */
|
|
135
|
+
function plainData(node) {
|
|
136
|
+
if (node.type === 'Literal')
|
|
137
|
+
return true;
|
|
138
|
+
if (node.type === 'ArrayExpression')
|
|
139
|
+
return node.elements.every((item) => !item || (item.type !== 'SpreadElement' && plainData(item)));
|
|
140
|
+
return (node.type === 'ObjectExpression' &&
|
|
141
|
+
node.properties.every((entry) => entry.type === 'Property' && entry.kind === 'init' && !entry.computed && plainData(entry.value)));
|
|
142
|
+
}
|
|
143
|
+
/** A top-level call can read consecutive declarations of literal data before anything can change their properties. */
|
|
144
|
+
function suppliedMember(ref, invocation) {
|
|
145
|
+
const { call, scopes, statement } = invocation;
|
|
146
|
+
const program = scopes[0];
|
|
147
|
+
if (statement?.type !== 'ExpressionStatement' ||
|
|
148
|
+
statement.expression !== call ||
|
|
149
|
+
program.type !== 'Program' ||
|
|
150
|
+
call.arguments.length !== 1 ||
|
|
151
|
+
call.arguments[0] !== ref.value)
|
|
152
|
+
return false;
|
|
153
|
+
let root = ref.value;
|
|
154
|
+
const path = [...ref.path];
|
|
155
|
+
while (root.type === 'MemberExpression') {
|
|
156
|
+
path.unshift(memberKey(root));
|
|
157
|
+
root = unwrapChain(root.object);
|
|
158
|
+
}
|
|
159
|
+
if (root.type !== 'Identifier')
|
|
160
|
+
return false;
|
|
161
|
+
for (let i = program.body.indexOf(statement) - 1; i >= 0; i--) {
|
|
162
|
+
const previous = program.body[i];
|
|
163
|
+
if (previous.type === 'FunctionDeclaration' || previous.type === 'EmptyStatement')
|
|
164
|
+
continue;
|
|
165
|
+
if (previous.type !== 'VariableDeclaration' ||
|
|
166
|
+
previous.kind !== 'const' ||
|
|
167
|
+
!previous.declarations.every((d) => d.id.type === 'Identifier' && d.init && plainData(d.init)))
|
|
168
|
+
break;
|
|
169
|
+
const initial = previous.declarations.find((d) => d.id.type === 'Identifier' && d.id.name === root.name)?.init;
|
|
170
|
+
if (!initial)
|
|
171
|
+
continue;
|
|
172
|
+
let current = direct(initial);
|
|
173
|
+
for (const key of path) {
|
|
174
|
+
if (key === null ||
|
|
175
|
+
key === '__proto__' ||
|
|
176
|
+
(current.value.type !== 'ObjectExpression' && current.value.type !== 'ArrayExpression'))
|
|
177
|
+
return false;
|
|
178
|
+
const next = property(current, key).next().value;
|
|
179
|
+
if (!next)
|
|
180
|
+
return false;
|
|
181
|
+
current = next;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
function bind(pattern, ref, add, defined) {
|
|
188
|
+
if (pattern.type === 'AssignmentPattern') {
|
|
189
|
+
if (ref)
|
|
190
|
+
bind(pattern.left, ref, add, defined);
|
|
191
|
+
if (!ref || ((ref.path.length || !DEFINED.has(ref.value.type)) && !defined?.(ref)))
|
|
192
|
+
bind(pattern.left, direct(pattern.right), (name, value) => {
|
|
193
|
+
add(name, value, true);
|
|
194
|
+
}, defined);
|
|
195
|
+
}
|
|
196
|
+
else if (!ref)
|
|
197
|
+
return;
|
|
198
|
+
else if (pattern.type === 'Identifier')
|
|
199
|
+
add(pattern.name, ref);
|
|
200
|
+
else if (pattern.type === 'ObjectPattern') {
|
|
201
|
+
for (const entry of pattern.properties) {
|
|
202
|
+
if (entry.type === 'RestElement')
|
|
203
|
+
bind(entry.argument, { ...ref, rest: 0 }, add, defined);
|
|
204
|
+
else
|
|
205
|
+
for (const child of property(ref, propertyKey(entry)))
|
|
206
|
+
bind(entry.value, child, add, defined);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (pattern.type === 'ArrayPattern') {
|
|
210
|
+
for (const [index, item] of pattern.elements.entries()) {
|
|
211
|
+
if (!item)
|
|
212
|
+
continue;
|
|
213
|
+
if (item.type === 'RestElement')
|
|
214
|
+
bind(item.argument, { ...ref, rest: index }, add, defined);
|
|
215
|
+
else
|
|
216
|
+
for (const child of property(ref, String(index)))
|
|
217
|
+
bind(item, child, add, defined);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/** References extracted once when a module evaluates a constant destructuring declaration. */
|
|
222
|
+
export function patternReferences(pattern, source, scopes) {
|
|
223
|
+
const entries = new Map();
|
|
224
|
+
bind(pattern, direct(source), (name, ref, fromDefault) => {
|
|
225
|
+
const options = entries.get(name);
|
|
226
|
+
const option = { ref, fromDefault };
|
|
227
|
+
if (options)
|
|
228
|
+
options.push(option);
|
|
229
|
+
else
|
|
230
|
+
entries.set(name, [option]);
|
|
231
|
+
});
|
|
232
|
+
return { entries, scopes };
|
|
233
|
+
}
|
|
234
|
+
/** Expand literal array spreads in argument order, preserving holes and spreads of unknown width. */
|
|
235
|
+
export function* literalArguments(elements) {
|
|
236
|
+
for (const element of elements) {
|
|
237
|
+
const spread = element?.type === 'SpreadElement' && unwrapChain(element.argument);
|
|
238
|
+
if (spread && spread.type === 'ArrayExpression')
|
|
239
|
+
yield* literalArguments(spread.elements);
|
|
240
|
+
else
|
|
241
|
+
yield element;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function argumentPart(list, offset) {
|
|
245
|
+
const array = list.value;
|
|
246
|
+
if (list.path.length || array.type !== 'ArrayExpression')
|
|
247
|
+
return { list, offset, size: null };
|
|
248
|
+
const elements = [...literalArguments(array.elements)];
|
|
249
|
+
if (elements.slice(0, offset).some((item) => item?.type === 'SpreadElement'))
|
|
250
|
+
return { list, offset, size: null };
|
|
251
|
+
const rest = elements.slice(offset);
|
|
252
|
+
return {
|
|
253
|
+
list: { ...list, value: { ...array, elements: rest } },
|
|
254
|
+
offset: 0,
|
|
255
|
+
size: rest.some((item) => item?.type === 'SpreadElement') ? null : rest.length,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/** Bind positional, rest and default references once for both callback discovery and write tracking. */
|
|
259
|
+
export function helperParameters(site, isDefined) {
|
|
260
|
+
const { call, fn, scopes, statement, parameterScopes } = site;
|
|
261
|
+
const elements = site.mode === 'reflect' ? [...literalArguments(call.arguments)] : call.arguments;
|
|
262
|
+
const args = direct({ type: 'ArrayExpression', elements, start: call.start, end: call.end });
|
|
263
|
+
const parameters = [];
|
|
264
|
+
const add = (name, ref, fromDefault = false) => {
|
|
265
|
+
parameters.push({
|
|
266
|
+
name,
|
|
267
|
+
ref,
|
|
268
|
+
fromDefault,
|
|
269
|
+
scopes: fromDefault ? parameterScopes : scopes,
|
|
270
|
+
statement: fromDefault ? null : statement,
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
const defined = (ref) => (!ref.path.length && isDefined(ref.value, ref.capture)) || suppliedMember(ref, site);
|
|
274
|
+
const bound = (site.bound ?? []).map((capture) => ({
|
|
275
|
+
...direct({
|
|
276
|
+
type: 'ArrayExpression',
|
|
277
|
+
elements: [...literalArguments(capture.call.arguments)],
|
|
278
|
+
start: capture.call.start,
|
|
279
|
+
end: capture.call.end,
|
|
280
|
+
}),
|
|
281
|
+
capture,
|
|
282
|
+
}));
|
|
283
|
+
const receiverIndex = site.mode === 'reflect' ? 1 : 0;
|
|
284
|
+
if (fn.type !== 'ArrowFunctionExpression') {
|
|
285
|
+
if (bound.length) {
|
|
286
|
+
for (const receiver of property(bound[0], '0'))
|
|
287
|
+
if (receiver)
|
|
288
|
+
add('this', receiver);
|
|
289
|
+
}
|
|
290
|
+
else if (site.methodOwner)
|
|
291
|
+
add('this', direct(site.methodOwner));
|
|
292
|
+
else if (site.mode !== 'direct') {
|
|
293
|
+
for (const receiver of property(args, String(receiverIndex)))
|
|
294
|
+
if (receiver)
|
|
295
|
+
add('this', receiver);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const lists = site.mode === 'apply' || site.mode === 'reflect' ? property(args, String(receiverIndex + 1)) : [args];
|
|
299
|
+
const offset = site.mode === 'call' ? 1 : 0;
|
|
300
|
+
for (const supplied of lists) {
|
|
301
|
+
const list = bound.length
|
|
302
|
+
? {
|
|
303
|
+
...args,
|
|
304
|
+
parts: [...bound.map((ref) => argumentPart(ref, 1)), ...(supplied ? [argumentPart(supplied, offset)] : [])],
|
|
305
|
+
}
|
|
306
|
+
: supplied;
|
|
307
|
+
const start = bound.length ? 0 : offset;
|
|
308
|
+
for (const [index, parameter] of fn.params.entries()) {
|
|
309
|
+
if (parameter.type === 'RestElement') {
|
|
310
|
+
if (list)
|
|
311
|
+
bind(parameter.argument, { ...list, rest: index + start }, add);
|
|
312
|
+
}
|
|
313
|
+
else if (list) {
|
|
314
|
+
for (const source of property(list, String(index + start)))
|
|
315
|
+
bind(parameter, source, add, defined);
|
|
316
|
+
}
|
|
317
|
+
else
|
|
318
|
+
bind(parameter, null, add, defined);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return parameters;
|
|
322
|
+
}
|
|
323
|
+
/** Follow the container of each write. Replacing a copy's property and mutating a shared child have different owners. */
|
|
324
|
+
export function changedReferences(declarations, targets, { calls, callTimes, returnedContexts, memberSources, readTimes }, bindingKey) {
|
|
325
|
+
const names = new Set();
|
|
326
|
+
const bindings = new Map();
|
|
327
|
+
const add = (name, scopes, source) => {
|
|
328
|
+
const key = bindingKey(name, scopes);
|
|
329
|
+
if (key === null)
|
|
330
|
+
return;
|
|
331
|
+
const previous = bindings.get(key);
|
|
332
|
+
if (previous)
|
|
333
|
+
previous.push(source);
|
|
334
|
+
else
|
|
335
|
+
bindings.set(key, [source]);
|
|
336
|
+
};
|
|
337
|
+
for (const { pattern, source, scopes, statement, end, replaces } of declarations) {
|
|
338
|
+
bind(pattern, direct(source), (name, ref) => {
|
|
339
|
+
add(name, scopes, { ref, scopes, statement, end: statement ? end : null, replaces });
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
for (const invocations of calls.values()) {
|
|
343
|
+
for (const invocation of invocations) {
|
|
344
|
+
for (const parameter of invocation.parameters) {
|
|
345
|
+
add(parameter.name, invocation.parameterScopes, {
|
|
346
|
+
...parameter,
|
|
347
|
+
end: null,
|
|
348
|
+
replaces: false,
|
|
349
|
+
invocation,
|
|
350
|
+
context: parameter.fromDefault ? invocation.context : invocation.caller,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const activeRoots = new Set();
|
|
356
|
+
const returning = [];
|
|
357
|
+
const follow = (ref, suffix, site, context, factory) => {
|
|
358
|
+
if (ref.capture) {
|
|
359
|
+
site = ref.capture;
|
|
360
|
+
context = ref.capture.caller;
|
|
361
|
+
factory = ref.capture.factory;
|
|
362
|
+
}
|
|
363
|
+
if (ref.rest !== undefined && suffix.length === 0)
|
|
364
|
+
return;
|
|
365
|
+
const owner = site.scopes.findLast((scope) => scope.type === 'FunctionDeclaration' || isFunctionNode(scope));
|
|
366
|
+
const frame = returning.findLast((entry) => entry.invocation.fn === owner);
|
|
367
|
+
const time = site.statement ? ref.value.start : frame ? frame.time : owner ? (callTimes.get(owner) ?? null) : null;
|
|
368
|
+
const slotTime = owner && !site.statement ? readTimes.at(owner, ref.value.start, time) : time;
|
|
369
|
+
const path = [...ref.path.map((key) => ({ key, time: slotTime })), ...suffix];
|
|
370
|
+
if (ref.rest && suffix[0].key !== null)
|
|
371
|
+
path[ref.path.length] = { ...suffix[0], key: String(Number(suffix[0].key) + ref.rest) };
|
|
372
|
+
let value = ref.value;
|
|
373
|
+
while (value.type === 'MemberExpression') {
|
|
374
|
+
path.unshift({ key: memberKey(value), time: slotTime });
|
|
375
|
+
value = unwrapChain(value.object);
|
|
376
|
+
}
|
|
377
|
+
if (value.type === 'Identifier' || value.type === 'ThisExpression') {
|
|
378
|
+
const key = bindingKey(value.type === 'ThisExpression' ? 'this' : value.name, site.scopes);
|
|
379
|
+
if (key === null)
|
|
380
|
+
return;
|
|
381
|
+
names.add(key);
|
|
382
|
+
const sources = bindings.get(key);
|
|
383
|
+
const active = returning.at(-1)?.active ?? activeRoots;
|
|
384
|
+
if (!sources || active.has(value))
|
|
385
|
+
return;
|
|
386
|
+
active.add(value);
|
|
387
|
+
// Only an unconditional module-level assignment rules out earlier top-level sources.
|
|
388
|
+
const readAt = time;
|
|
389
|
+
let replacedAt = 0;
|
|
390
|
+
if (typeof readAt === 'number') {
|
|
391
|
+
for (const source of sources)
|
|
392
|
+
if (source.replaces && source.end !== null && source.end <= readAt)
|
|
393
|
+
replacedAt = Math.max(replacedAt, source.end);
|
|
394
|
+
}
|
|
395
|
+
for (const source of sources) {
|
|
396
|
+
if (typeof readAt === 'number' && source.end !== null && (source.end < replacedAt || source.end > readAt))
|
|
397
|
+
continue;
|
|
398
|
+
const invocation = source.invocation;
|
|
399
|
+
if (invocation) {
|
|
400
|
+
if (context?.owner === invocation.fn &&
|
|
401
|
+
context !== invocation.context &&
|
|
402
|
+
!(ref.capture?.repeated && invocation.context.ancestors.has(ref.capture.call)))
|
|
403
|
+
continue;
|
|
404
|
+
const captured = factory?.fn === invocation.fn ? factory : enclosingFactory(context, invocation.fn);
|
|
405
|
+
if (captured && captured !== invocation)
|
|
406
|
+
continue;
|
|
407
|
+
const frame = returning.findLast((frame) => frame.invocation.fn === invocation.fn)?.invocation;
|
|
408
|
+
if (frame && frame !== invocation)
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
follow(source.ref, path, source, source.context ?? context, invocation === factory ? undefined : factory);
|
|
412
|
+
}
|
|
413
|
+
active.delete(value);
|
|
414
|
+
}
|
|
415
|
+
else if (value.type === 'CallExpression') {
|
|
416
|
+
for (const invocation of calls.get(value) ?? []) {
|
|
417
|
+
if (context?.owner === invocation.caller.owner && context !== invocation.caller)
|
|
418
|
+
continue;
|
|
419
|
+
if (returning.some((frame) => frame.invocation === invocation))
|
|
420
|
+
continue;
|
|
421
|
+
const returns = returnValues(invocation.fn, false, 'ignore');
|
|
422
|
+
if (!returns)
|
|
423
|
+
continue;
|
|
424
|
+
returning.push({ invocation, time, active: new Set() });
|
|
425
|
+
for (const result of returns)
|
|
426
|
+
follow(direct(result.expression), path, { scopes: [...invocation.parameterScopes, ...result.around], statement: null }, invocation.context, factory);
|
|
427
|
+
returning.pop();
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
else if (value.type === 'ConditionalExpression' || value.type === 'LogicalExpression') {
|
|
431
|
+
for (const source of referenceOperands(value))
|
|
432
|
+
follow(direct(source), path, site, context, factory);
|
|
433
|
+
}
|
|
434
|
+
else if (value.type === 'SequenceExpression') {
|
|
435
|
+
follow(direct(value.expressions[value.expressions.length - 1]), path, site, context, factory);
|
|
436
|
+
}
|
|
437
|
+
else if (value.type === 'AssignmentExpression' && value.operator === '=') {
|
|
438
|
+
follow(direct(value.right), path, site, context, factory);
|
|
439
|
+
}
|
|
440
|
+
else if (path.length && (value.type === 'ObjectExpression' || value.type === 'ArrayExpression')) {
|
|
441
|
+
const [{ key, time }, ...rest] = path;
|
|
442
|
+
const sources = value.type === 'ArrayExpression' && !ref.parts
|
|
443
|
+
? memberSources(value, site.scopes, key, time)
|
|
444
|
+
: Array.from(property({ value, path: [], parts: ref.parts }, key), (child) => ({
|
|
445
|
+
ref: child,
|
|
446
|
+
scopes: site.scopes,
|
|
447
|
+
}));
|
|
448
|
+
for (const child of sources)
|
|
449
|
+
if (child.ref)
|
|
450
|
+
follow(child.ref, rest, { ...site, scopes: child.scopes }, context, factory);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
for (const target of targets) {
|
|
454
|
+
const contexts = returnedContexts.get(target.owner);
|
|
455
|
+
if (contexts)
|
|
456
|
+
for (const context of contexts)
|
|
457
|
+
follow(direct(target.value), [], target, context);
|
|
458
|
+
else
|
|
459
|
+
follow(direct(target.value), [], target);
|
|
460
|
+
}
|
|
461
|
+
return names;
|
|
462
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Expression, Node } from 'oxc-parser';
|
|
2
|
+
import { type FunctionNode } from './ast.ts';
|
|
3
|
+
import type { HelperInvocation, ScopedFunction } from './mutation-references.ts';
|
|
4
|
+
import type { Values } from './values.ts';
|
|
5
|
+
export declare function analyzeReturns(fn: FunctionNode): {
|
|
6
|
+
candidates: import("./ast.ts").ReturnValue[];
|
|
7
|
+
complete: boolean;
|
|
8
|
+
};
|
|
9
|
+
/** Closures and bound arguments keep the closest factory whose bindings they use. */
|
|
10
|
+
export declare function capturedResult(target: ScopedFunction, frame: HelperInvocation): ScopedFunction;
|
|
11
|
+
export declare function conditionArgument(test: Expression, scopes: readonly Node[], frame: HelperInvocation | undefined, values: Values): Expression | undefined;
|
|
12
|
+
/** Mark functions whose creation alone cannot execute the writes in their bodies. */
|
|
13
|
+
export declare function markReturnedLocals(expression: Expression, scopes: readonly Node[], owner: FunctionNode, values: Values, out: Set<Node>, seen?: Set<Node>): void;
|