@native-sdk/cli 0.4.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +187 -6
- package/build/ts_run.mjs +63 -0
- package/build.zig +213 -1
- package/build.zig.zon +1 -0
- package/package.json +15 -10
- package/packages/core/package-lock.json +43 -0
- package/packages/core/package.json +37 -0
- package/packages/core/rt/rt.zig +1579 -0
- package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
- package/packages/core/sdk/core.ts +694 -0
- package/packages/core/sdk/events.ts +137 -0
- package/packages/core/sdk/text.ts +519 -0
- package/packages/core/src/checker.ts +1869 -0
- package/packages/core/src/cli.ts +68 -0
- package/packages/core/src/devhost.ts +291 -0
- package/packages/core/src/devhost_resolver.mjs +22 -0
- package/packages/core/src/diagnostics.ts +417 -0
- package/packages/core/src/emitter.ts +8815 -0
- package/packages/core/src/infer.ts +1515 -0
- package/packages/core/src/modules.ts +173 -0
- package/packages/core/src/ownership.ts +740 -0
- package/packages/core/src/text_polyfill.ts +337 -0
- package/packages/core/src/text_tables.ts +408 -0
- package/packages/core/src/transpile.ts +146 -0
- package/packages/core/src/typed_ast.ts +536 -0
- package/packages/core/src/types.ts +815 -0
- package/skill-data/native-ui/SKILL.md +14 -0
- package/skill-data/ts-core/SKILL.md +493 -0
- package/skill-data/zig/SKILL.md +10 -0
- package/src/app_runner/root.zig +24 -0
- package/src/app_runner/ts_core_main.zig +231 -0
- package/src/primitives/app_manifest/comptime_scene.zig +341 -0
- package/src/primitives/app_manifest/root.zig +5 -0
- package/src/primitives/canvas/commands.zig +14 -7
- package/src/primitives/canvas/lazy_tls.zig +73 -0
- package/src/primitives/canvas/render_generic_resources.zig +22 -15
- package/src/primitives/canvas/root.zig +6 -0
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/text_atlas.zig +40 -32
- package/src/primitives/canvas/text_layout_cache.zig +29 -22
- package/src/primitives/canvas/text_measure_cache.zig +56 -41
- package/src/primitives/canvas/text_spans.zig +39 -26
- package/src/primitives/canvas/tokens.zig +54 -0
- package/src/primitives/canvas/ui.zig +125 -0
- package/src/primitives/canvas/ui_markup.zig +22 -3
- package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
- package/src/primitives/canvas/ui_markup_contract.zig +68 -14
- package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
- package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
- package/src/primitives/canvas/ui_markup_view.zig +105 -1
- package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
- package/src/primitives/canvas/ui_schema.zig +8 -0
- package/src/primitives/canvas/ui_schema_tests.zig +5 -4
- package/src/primitives/canvas/widget_invalidation.zig +14 -7
- package/src/primitives/canvas/widget_render.zig +25 -15
- package/src/primitives/canvas/widget_render_controls.zig +15 -9
- package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
- package/src/root.zig +7 -0
- package/src/runtime/canvas_frame.zig +57 -36
- package/src/runtime/canvas_images.zig +9 -3
- package/src/runtime/canvas_widget_control_tests.zig +5 -6
- package/src/runtime/canvas_widget_runtime.zig +7 -4
- package/src/runtime/effects.zig +490 -2
- package/src/runtime/effects_host_tests.zig +594 -0
- package/src/runtime/root.zig +18 -0
- package/src/runtime/session_replay.zig +7 -1
- package/src/runtime/tests.zig +2 -0
- package/src/runtime/ts_core_host.zig +1505 -0
- package/src/runtime/ts_core_host_tests.zig +1408 -0
- package/src/runtime/ts_ui_app.zig +469 -0
- package/src/runtime/ui_app.zig +58 -0
- package/src/runtime/view_canvas.zig +24 -17
- package/src/runtime/view_widget_tree.zig +2 -2
- package/src/tooling/codesign.zig +212 -105
- package/src/tooling/doctor.zig +78 -0
- package/src/tooling/manifest.zig +47 -0
- package/src/tooling/package.zig +124 -12
- package/src/tooling/raw_manifest.zig +15 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +683 -28
- package/src/tooling/ts_core.zig +486 -0
- package/src/tooling/verbs.zig +17 -0
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
// Array ownership: the local-mutation rule, shared by the checker (the
|
|
2
|
+
// teaching layer) and the emitter (the layer-3 re-derivation).
|
|
3
|
+
//
|
|
4
|
+
// An array is LOCALLY OWNED at a mutation site iff it is a function-local
|
|
5
|
+
// binding created by the function's own array construction (an array
|
|
6
|
+
// literal, or a fresh copy from `.slice()`/`.map()`/`.filter()`/`.concat()`/
|
|
7
|
+
// `.toSorted()`) and the binding has not ESCAPED before the mutation: not
|
|
8
|
+
// returned, not passed to another function (the deliberate v1 conservatism —
|
|
9
|
+
// a callee could alias it), not stored into a structure, not aliased by a
|
|
10
|
+
// second binding. Reads never end ownership: method calls, element and
|
|
11
|
+
// `.length` access, `for...of` iteration, and spreads into fresh literals
|
|
12
|
+
// (which copy) all leave the array yours. Parameters, model/msg data, and
|
|
13
|
+
// module-level consts are never locally owned.
|
|
14
|
+
//
|
|
15
|
+
// Escapes gate by POSITION: a mutation is legal only when no escape sits
|
|
16
|
+
// before it. An escape inside a loop gates the whole loop body (the loop's
|
|
17
|
+
// second iteration would mutate after the first iteration's escape), so an
|
|
18
|
+
// escape's effective position is the start of its outermost enclosing loop.
|
|
19
|
+
|
|
20
|
+
import { ts, TypedAst, lineColumn } from "./typed_ast.ts";
|
|
21
|
+
|
|
22
|
+
/// Every JS array method that mutates its receiver (the checker's gate set).
|
|
23
|
+
export const mutatingArrayMethods = new Set([
|
|
24
|
+
"push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
/// The mutating methods v1 supports on locally-owned arrays (`copyWithin`
|
|
28
|
+
/// stays out everywhere — splice/fill cover its uses).
|
|
29
|
+
export const ownedMutatingMethods = new Set([
|
|
30
|
+
"push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill",
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/// Methods that change the array's LENGTH. These force the builder lowering
|
|
34
|
+
/// (a growable slice plus a live fill count) and may not run against the
|
|
35
|
+
/// array a loop is iterating — JS walks the live array there.
|
|
36
|
+
export const lengthChangingMethods = new Set(["push", "pop", "shift", "unshift", "splice"]);
|
|
37
|
+
|
|
38
|
+
/// Methods that can GROW the array (the emitted slice variable is reassigned
|
|
39
|
+
/// by rt.frameGrow, so its binding must be `var`).
|
|
40
|
+
export const growingMethods = new Set(["push", "unshift", "splice"]);
|
|
41
|
+
|
|
42
|
+
export type OwnershipVerdict =
|
|
43
|
+
| { readonly owned: true; readonly decl: ts.VariableDeclaration }
|
|
44
|
+
| { readonly owned: false; readonly why: "not-local"; readonly detail: string }
|
|
45
|
+
| { readonly owned: false; readonly why: "escaped"; readonly detail: string };
|
|
46
|
+
|
|
47
|
+
export function unwrapExpr(e: ts.Expression): ts.Expression {
|
|
48
|
+
while (
|
|
49
|
+
ts.isParenthesizedExpression(e) ||
|
|
50
|
+
ts.isAsExpression(e) ||
|
|
51
|
+
ts.isSatisfiesExpression(e) ||
|
|
52
|
+
ts.isNonNullExpression(e)
|
|
53
|
+
) {
|
|
54
|
+
e = e.expression;
|
|
55
|
+
}
|
|
56
|
+
return e;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// The owning constructors: expressions that create a FRESH array this
|
|
60
|
+
/// function is the only holder of.
|
|
61
|
+
export function isOwningInitializer(init: ts.Expression): boolean {
|
|
62
|
+
const e = unwrapExpr(init);
|
|
63
|
+
if (ts.isArrayLiteralExpression(e)) return true;
|
|
64
|
+
return (
|
|
65
|
+
ts.isCallExpression(e) &&
|
|
66
|
+
ts.isPropertyAccessExpression(e.expression) &&
|
|
67
|
+
// `.split` is the bytes method: a fresh array of views, the caller's own.
|
|
68
|
+
["slice", "map", "filter", "concat", "toSorted", "split"].includes(e.expression.name.text)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function enclosingFunctionOf(node: ts.Node): ts.Node | null {
|
|
73
|
+
let cur: ts.Node | undefined = node.parent;
|
|
74
|
+
while (cur && !ts.isSourceFile(cur)) {
|
|
75
|
+
if (
|
|
76
|
+
ts.isFunctionDeclaration(cur) ||
|
|
77
|
+
ts.isArrowFunction(cur) ||
|
|
78
|
+
ts.isFunctionExpression(cur) ||
|
|
79
|
+
ts.isMethodDeclaration(cur) ||
|
|
80
|
+
ts.isConstructorDeclaration(cur)
|
|
81
|
+
) {
|
|
82
|
+
return cur;
|
|
83
|
+
}
|
|
84
|
+
cur = cur.parent;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// A `readonly T[]` parameter type (the borrow shape): the type operator
|
|
90
|
+
/// spelling or a ReadonlyArray reference. Bytes keep their own discipline.
|
|
91
|
+
function isReadonlySliceType(node: ts.TypeNode): boolean {
|
|
92
|
+
if (ts.isTypeOperatorNode(node) && node.operator === ts.SyntaxKind.ReadonlyKeyword && ts.isArrayTypeNode(node.type)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && node.typeName.text === "ReadonlyArray";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Whether passing `arg` into `call` BORROWS instead of escaping: the
|
|
99
|
+
/// resolved callee's parameter is declared `readonly T[]`, and the callee
|
|
100
|
+
/// provably cannot mutate or alias the value out — every use of the
|
|
101
|
+
/// parameter in its body is a read, or another borrowing pass (checked
|
|
102
|
+
/// coinductively, so mutual/self recursion over borrowed slices stays
|
|
103
|
+
/// legal: an in-progress parameter is assumed safe until a real escape
|
|
104
|
+
/// disproves it).
|
|
105
|
+
function borrowedByCall(
|
|
106
|
+
tast: TypedAst,
|
|
107
|
+
call: ts.CallExpression,
|
|
108
|
+
arg: ts.Node,
|
|
109
|
+
inProgress: Set<ts.ParameterDeclaration>,
|
|
110
|
+
): boolean {
|
|
111
|
+
const idx = (call.arguments as readonly ts.Node[]).indexOf(arg);
|
|
112
|
+
if (idx < 0) return false;
|
|
113
|
+
if (call.arguments.slice(0, idx + 1).some((a) => ts.isSpreadElement(a))) return false;
|
|
114
|
+
const callee = unwrapExpr(call.expression);
|
|
115
|
+
if (!ts.isIdentifier(callee)) return false;
|
|
116
|
+
const d = tast.declarationOf(callee);
|
|
117
|
+
let params: readonly ts.ParameterDeclaration[] | null = null;
|
|
118
|
+
let body: ts.Node | null = null;
|
|
119
|
+
if (d && ts.isFunctionDeclaration(d) && d.body) {
|
|
120
|
+
params = d.parameters;
|
|
121
|
+
body = d.body;
|
|
122
|
+
} else if (d && ts.isVariableDeclaration(d)) {
|
|
123
|
+
const fnValue = constFunctionValue(d);
|
|
124
|
+
if (fnValue) {
|
|
125
|
+
params = fnValue.parameters;
|
|
126
|
+
body = fnValue.body;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!params || !body) return false;
|
|
130
|
+
const p = params[idx];
|
|
131
|
+
if (!p || p.dotDotDotToken || !p.type || !isReadonlySliceType(p.type)) return false;
|
|
132
|
+
if (inProgress.has(p)) return true; // coinductive: recursion over a borrow stays a borrow
|
|
133
|
+
inProgress.add(p);
|
|
134
|
+
try {
|
|
135
|
+
return paramStaysBorrowed(tast, p, body, inProgress);
|
|
136
|
+
} finally {
|
|
137
|
+
inProgress.delete(p);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// Every use of a readonly-slice parameter inside the callee body must keep
|
|
142
|
+
/// the borrow: reads (member/element access, iteration, spreads into fresh
|
|
143
|
+
/// literals) and further borrowing passes. A return, a store, an alias, or
|
|
144
|
+
/// a pass into a non-borrowing position aliases the caller's array out.
|
|
145
|
+
function paramStaysBorrowed(
|
|
146
|
+
tast: TypedAst,
|
|
147
|
+
param: ts.ParameterDeclaration,
|
|
148
|
+
body: ts.Node,
|
|
149
|
+
inProgress: Set<ts.ParameterDeclaration>,
|
|
150
|
+
): boolean {
|
|
151
|
+
let safe = true;
|
|
152
|
+
const visit = (n: ts.Node): void => {
|
|
153
|
+
if (!safe) return;
|
|
154
|
+
if (ts.isIdentifier(n) && n !== param.name && tast.declarationOf(n) === param) {
|
|
155
|
+
let cur: ts.Node = n;
|
|
156
|
+
let p: ts.Node = cur.parent;
|
|
157
|
+
while (
|
|
158
|
+
ts.isParenthesizedExpression(p) ||
|
|
159
|
+
ts.isAsExpression(p) ||
|
|
160
|
+
ts.isSatisfiesExpression(p) ||
|
|
161
|
+
ts.isNonNullExpression(p)
|
|
162
|
+
) {
|
|
163
|
+
cur = p;
|
|
164
|
+
p = p.parent;
|
|
165
|
+
}
|
|
166
|
+
const read =
|
|
167
|
+
(ts.isPropertyAccessExpression(p) && p.expression === cur) ||
|
|
168
|
+
(ts.isElementAccessExpression(p) && p.expression === cur) ||
|
|
169
|
+
(ts.isForOfStatement(p) && p.expression === cur) ||
|
|
170
|
+
(ts.isSpreadElement(p) && ts.isArrayLiteralExpression(p.parent));
|
|
171
|
+
if (read) return;
|
|
172
|
+
if (ts.isCallExpression(p) && (p.arguments as readonly ts.Node[]).includes(cur)) {
|
|
173
|
+
if (!borrowedByCall(tast, p, cur, inProgress)) safe = false;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
safe = false;
|
|
177
|
+
}
|
|
178
|
+
ts.forEachChild(n, visit);
|
|
179
|
+
};
|
|
180
|
+
visit(body);
|
|
181
|
+
return safe;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Classify one identifier occurrence: null when the use is a read that
|
|
185
|
+
/// keeps ownership, else a short past-tense description of the escape.
|
|
186
|
+
/// `fn` is the owning function: a `return` there is TERMINAL — execution
|
|
187
|
+
/// ends, so it can never precede a later mutation dynamically (the
|
|
188
|
+
/// early-exit `if (...) return out;` shape stays legal) — while a `return`
|
|
189
|
+
/// inside a nested callback stores the value into the produced array and is
|
|
190
|
+
/// a real escape.
|
|
191
|
+
function escapeKindOf(n: ts.Identifier, fn: ts.Node, tast: TypedAst): string | null {
|
|
192
|
+
// A reference inside a STORED function value (a const-bound helper, not
|
|
193
|
+
// an inline call-argument callback) escapes at the capture: the closure
|
|
194
|
+
// retains the reference past this statement, so JS would show it every
|
|
195
|
+
// later mutation while the hoisted native helper cannot.
|
|
196
|
+
{
|
|
197
|
+
let walk: ts.Node = n;
|
|
198
|
+
while (walk !== fn && walk.parent) {
|
|
199
|
+
const up: ts.Node = walk.parent;
|
|
200
|
+
if ((ts.isArrowFunction(up) || ts.isFunctionExpression(up)) && up !== fn && !inCallArgumentPosition(up)) {
|
|
201
|
+
return "was captured by a stored function value";
|
|
202
|
+
}
|
|
203
|
+
if (up === fn) break;
|
|
204
|
+
walk = up;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
let cur: ts.Node = n;
|
|
208
|
+
let p: ts.Node = cur.parent;
|
|
209
|
+
while (
|
|
210
|
+
ts.isParenthesizedExpression(p) ||
|
|
211
|
+
ts.isAsExpression(p) ||
|
|
212
|
+
ts.isSatisfiesExpression(p) ||
|
|
213
|
+
ts.isNonNullExpression(p)
|
|
214
|
+
) {
|
|
215
|
+
cur = p;
|
|
216
|
+
p = p.parent;
|
|
217
|
+
}
|
|
218
|
+
// Reads: method calls / .length (property access), element reads and
|
|
219
|
+
// writes, iteration, and spreads into fresh array literals (a copy).
|
|
220
|
+
if (ts.isPropertyAccessExpression(p) && p.expression === cur) return null;
|
|
221
|
+
if (ts.isElementAccessExpression(p) && p.expression === cur) return null;
|
|
222
|
+
if (ts.isForOfStatement(p) && p.expression === cur) return null;
|
|
223
|
+
if (ts.isSpreadElement(p) && ts.isArrayLiteralExpression(p.parent)) return null;
|
|
224
|
+
if (ts.isReturnStatement(p)) {
|
|
225
|
+
return enclosingFunctionOf(p) === fn ? null : "was returned from a callback";
|
|
226
|
+
}
|
|
227
|
+
if (ts.isCallExpression(p) && (p.arguments as readonly ts.Node[]).includes(cur)) {
|
|
228
|
+
// Passing into a `readonly T[]` parameter BORROWS: the callee provably
|
|
229
|
+
// cannot mutate or alias the array out, so ownership survives the call.
|
|
230
|
+
if (borrowedByCall(tast, p, cur, new Set())) return null;
|
|
231
|
+
return "was passed to a call";
|
|
232
|
+
}
|
|
233
|
+
if (ts.isVariableDeclaration(p) && p.initializer === cur) return "was aliased by another binding";
|
|
234
|
+
if (ts.isBinaryExpression(p) && p.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
235
|
+
// The RHS stores the reference; the LHS is a rebinding, reported as the
|
|
236
|
+
// reassignment verdict by arrayOwnership (never owned) instead.
|
|
237
|
+
return p.right === cur ? "was stored through an assignment" : null;
|
|
238
|
+
}
|
|
239
|
+
if (ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p)) return "was stored in an object";
|
|
240
|
+
if (ts.isSpreadAssignment(p)) return "was spread into an object";
|
|
241
|
+
if (ts.isArrayLiteralExpression(p)) return "was stored in an array";
|
|
242
|
+
return "escaped as a value";
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// An escape inside a loop gates the whole loop: its effective position is
|
|
246
|
+
/// the start of its outermost enclosing loop (bounded by the function).
|
|
247
|
+
function effectivePos(n: ts.Node, fn: ts.Node): number {
|
|
248
|
+
let pos = n.getStart();
|
|
249
|
+
let cur: ts.Node | undefined = n.parent;
|
|
250
|
+
while (cur && cur !== fn) {
|
|
251
|
+
if (
|
|
252
|
+
ts.isForStatement(cur) ||
|
|
253
|
+
ts.isForOfStatement(cur) ||
|
|
254
|
+
ts.isForInStatement(cur) ||
|
|
255
|
+
ts.isWhileStatement(cur) ||
|
|
256
|
+
ts.isDoStatement(cur)
|
|
257
|
+
) {
|
|
258
|
+
pos = Math.min(pos, cur.getStart());
|
|
259
|
+
}
|
|
260
|
+
cur = cur.parent;
|
|
261
|
+
}
|
|
262
|
+
return pos;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// Whether the binding is ever ASSIGNED after its declaration.
|
|
266
|
+
function isReassignedBinding(tast: TypedAst, decl: ts.VariableDeclaration, fn: ts.Node): boolean {
|
|
267
|
+
let found = false;
|
|
268
|
+
const visit = (n: ts.Node): void => {
|
|
269
|
+
if (found) return;
|
|
270
|
+
if (ts.isBinaryExpression(n) && n.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
271
|
+
const target = unwrapExpr(n.left);
|
|
272
|
+
if (ts.isIdentifier(target) && tast.declarationOf(target) === decl) {
|
|
273
|
+
found = true;
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
ts.forEachChild(n, visit);
|
|
278
|
+
};
|
|
279
|
+
visit(fn);
|
|
280
|
+
return found;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/// Flow-sensitive ownership for a reassigned `let`: the binding stays owned
|
|
284
|
+
/// when EVERY assignment installs a fresh owning constructor — the binding
|
|
285
|
+
/// can then never hold a shared array, whatever the control flow. One mixed
|
|
286
|
+
/// assignment (an alias, a helper result) and the binding never owns.
|
|
287
|
+
export function everyAssignmentOwning(
|
|
288
|
+
tast: TypedAst,
|
|
289
|
+
decl: ts.VariableDeclaration,
|
|
290
|
+
fn: ts.Node,
|
|
291
|
+
owningInit: (init: ts.Expression) => boolean = isOwningInitializer,
|
|
292
|
+
): boolean {
|
|
293
|
+
let allOwning = true;
|
|
294
|
+
const visit = (n: ts.Node): void => {
|
|
295
|
+
if (!allOwning) return;
|
|
296
|
+
if (ts.isBinaryExpression(n) && n.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
297
|
+
const target = unwrapExpr(n.left);
|
|
298
|
+
if (ts.isIdentifier(target) && tast.declarationOf(target) === decl && !owningInit(n.right)) {
|
|
299
|
+
allOwning = false;
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
ts.forEachChild(n, visit);
|
|
304
|
+
};
|
|
305
|
+
visit(fn);
|
|
306
|
+
return allOwning;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/// The first escape of `decl` whose effective position precedes `sitePos`,
|
|
310
|
+
/// described for the teaching diagnostic — or null when the binding is still
|
|
311
|
+
/// owned at the site.
|
|
312
|
+
function escapeBefore(
|
|
313
|
+
tast: TypedAst,
|
|
314
|
+
decl: ts.VariableDeclaration,
|
|
315
|
+
fn: ts.Node,
|
|
316
|
+
sitePos: number,
|
|
317
|
+
): string | null {
|
|
318
|
+
let found: string | null = null;
|
|
319
|
+
let foundPos = Infinity;
|
|
320
|
+
const visit = (n: ts.Node): void => {
|
|
321
|
+
if (ts.isIdentifier(n) && n !== decl.name && tast.declarationOf(n) === decl) {
|
|
322
|
+
const kind = escapeKindOf(n, fn, tast);
|
|
323
|
+
if (kind !== null) {
|
|
324
|
+
const pos = effectivePos(n, fn);
|
|
325
|
+
if (pos < sitePos && pos < foundPos) {
|
|
326
|
+
const file = n.getSourceFile();
|
|
327
|
+
const { line } = lineColumn(file, n.getStart());
|
|
328
|
+
foundPos = pos;
|
|
329
|
+
found = `${kind} (line ${line})`;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
ts.forEachChild(n, visit);
|
|
334
|
+
};
|
|
335
|
+
visit(fn);
|
|
336
|
+
return found;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export type ValueStepVerdict = { readonly ok: true } | { readonly ok: false; readonly why: string };
|
|
340
|
+
|
|
341
|
+
/// A const-bound function VALUE (`const helper = (x) => ...` /
|
|
342
|
+
/// `const helper = function (x) {...}`) declared inside a function — the
|
|
343
|
+
/// shape that hoists to a module-level declaration. Named function
|
|
344
|
+
/// expressions and non-const bindings stay taught.
|
|
345
|
+
export function constFunctionValue(decl: ts.VariableDeclaration): ts.ArrowFunction | ts.FunctionExpression | null {
|
|
346
|
+
if (!ts.isIdentifier(decl.name) || !decl.initializer) return null;
|
|
347
|
+
if (!ts.isVariableDeclarationList(decl.parent) || (decl.parent.flags & ts.NodeFlags.Const) === 0) return null;
|
|
348
|
+
if (!enclosingFunctionOf(decl)) return null; // module-level function values stay taught
|
|
349
|
+
const init = unwrapExpr(decl.initializer);
|
|
350
|
+
if (ts.isArrowFunction(init) && !init.asteriskToken) return init;
|
|
351
|
+
if (ts.isFunctionExpression(init) && !init.name && !init.asteriskToken) return init;
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/// Whether a function value sits in a call's ARGUMENT position (the inline
|
|
356
|
+
/// callback home), reached through literal wrappers.
|
|
357
|
+
export function inCallArgumentPosition(fn: ts.Node): boolean {
|
|
358
|
+
let cur: ts.Node = fn;
|
|
359
|
+
for (;;) {
|
|
360
|
+
const p: ts.Node = cur.parent;
|
|
361
|
+
if (!p) return false;
|
|
362
|
+
if (ts.isCallExpression(p) && (p.arguments as readonly ts.Node[]).includes(cur as ts.Expression)) return true;
|
|
363
|
+
if (
|
|
364
|
+
ts.isParenthesizedExpression(p) ||
|
|
365
|
+
ts.isPropertyAssignment(p) ||
|
|
366
|
+
ts.isObjectLiteralExpression(p) ||
|
|
367
|
+
ts.isArrayLiteralExpression(p) ||
|
|
368
|
+
ts.isAsExpression(p) ||
|
|
369
|
+
ts.isSatisfiesExpression(p)
|
|
370
|
+
) {
|
|
371
|
+
cur = p;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/// Legality of hoisting a const-bound function value to module level.
|
|
379
|
+
/// Everything the hoisted declaration cannot represent teaches: captures of
|
|
380
|
+
/// enclosing locals (module consts and other hoistable helpers are fine),
|
|
381
|
+
/// missing signature annotations, and uses beyond a direct call or an
|
|
382
|
+
/// array-method callback argument.
|
|
383
|
+
export function functionValueLegality(
|
|
384
|
+
decl: ts.VariableDeclaration,
|
|
385
|
+
fn: ts.ArrowFunction | ts.FunctionExpression,
|
|
386
|
+
tast: TypedAst,
|
|
387
|
+
): ValueStepVerdict {
|
|
388
|
+
const name = (decl.name as ts.Identifier).text;
|
|
389
|
+
if (fn.typeParameters && fn.typeParameters.length > 0) {
|
|
390
|
+
return { ok: false, why: `\`${name}\` declares a type parameter (generic helpers are module-level function declarations)` };
|
|
391
|
+
}
|
|
392
|
+
if (!fn.type) {
|
|
393
|
+
return { ok: false, why: `\`${name}\` has no return type annotation (a hoisted helper spells its full signature)` };
|
|
394
|
+
}
|
|
395
|
+
for (const p of fn.parameters) {
|
|
396
|
+
if (!ts.isIdentifier(p.name) || !p.type) {
|
|
397
|
+
return { ok: false, why: `\`${name}\` has a parameter without a name and type annotation` };
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// A helper inside a GENERIC function must not mention its type
|
|
401
|
+
// parameters: the hoisted declaration is one concrete fn, while the
|
|
402
|
+
// enclosing generic instantiates per call site.
|
|
403
|
+
for (const typeNode of [fn.type, ...fn.parameters.map((p) => p.type)]) {
|
|
404
|
+
if (!typeNode) continue;
|
|
405
|
+
let mentioned: string | null = null;
|
|
406
|
+
const scan = (tn: ts.Node): void => {
|
|
407
|
+
if (mentioned) return;
|
|
408
|
+
if (ts.isTypeReferenceNode(tn) && ts.isIdentifier(tn.typeName)) {
|
|
409
|
+
const d = tast.declarationOf(tn.typeName);
|
|
410
|
+
if (d && ts.isTypeParameterDeclaration(d)) mentioned = tn.typeName.text;
|
|
411
|
+
}
|
|
412
|
+
ts.forEachChild(tn, scan);
|
|
413
|
+
};
|
|
414
|
+
scan(typeNode);
|
|
415
|
+
if (mentioned) {
|
|
416
|
+
return { ok: false, why: `\`${name}\`'s signature references type parameter \`${mentioned}\` (hoisted helpers are concrete — make it a module-level generic function)` };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const captured = capturedOuterLocal(fn, tast);
|
|
420
|
+
if (captured !== null) {
|
|
421
|
+
return { ok: false, why: `\`${name}\` captures \`${captured}\` from the enclosing function (pass it as a parameter)` };
|
|
422
|
+
}
|
|
423
|
+
const badUse = illegalFunctionValueUse(decl, tast);
|
|
424
|
+
if (badUse !== null) return { ok: false, why: badUse };
|
|
425
|
+
return { ok: true };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// The first identifier the function value CLOSES OVER: declared in an
|
|
429
|
+
/// enclosing function (not module scope, not the value's own scope). Other
|
|
430
|
+
/// hoistable helpers are exempt — they become module-level names too.
|
|
431
|
+
function capturedOuterLocal(fn: ts.ArrowFunction | ts.FunctionExpression, tast: TypedAst): string | null {
|
|
432
|
+
let found: string | null = null;
|
|
433
|
+
const file = fn.getSourceFile();
|
|
434
|
+
const visit = (n: ts.Node): void => {
|
|
435
|
+
if (found !== null) return;
|
|
436
|
+
if (ts.isIdentifier(n)) {
|
|
437
|
+
const d = tast.declarationOf(n);
|
|
438
|
+
if (
|
|
439
|
+
d &&
|
|
440
|
+
(ts.isVariableDeclaration(d) || ts.isParameter(d) || ts.isBindingElement(d)) &&
|
|
441
|
+
enclosingFunctionOf(d) !== null &&
|
|
442
|
+
!(d.getSourceFile() === file && d.pos >= fn.pos && d.end <= fn.end)
|
|
443
|
+
) {
|
|
444
|
+
if (ts.isVariableDeclaration(d) && constFunctionValue(d) !== null) return; // hoists too (self-recursion included)
|
|
445
|
+
found = n.text;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
ts.forEachChild(n, visit);
|
|
449
|
+
};
|
|
450
|
+
visit(fn.body);
|
|
451
|
+
return found;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/// The first USE of the binding beyond a direct call (`helper(x)`) or an
|
|
455
|
+
/// array-method callback argument (`xs.map(helper)`), described for the
|
|
456
|
+
/// teaching diagnostic; null when every use is legal.
|
|
457
|
+
function illegalFunctionValueUse(decl: ts.VariableDeclaration, tast: TypedAst): string | null {
|
|
458
|
+
const fn = enclosingFunctionOf(decl);
|
|
459
|
+
if (!fn) return "the binding is not function-local";
|
|
460
|
+
const name = (decl.name as ts.Identifier).text;
|
|
461
|
+
let found: string | null = null;
|
|
462
|
+
const visit = (n: ts.Node): void => {
|
|
463
|
+
if (found !== null) return;
|
|
464
|
+
if (ts.isIdentifier(n) && n !== decl.name && tast.declarationOf(n) === decl) {
|
|
465
|
+
let cur: ts.Node = n;
|
|
466
|
+
let p: ts.Node = cur.parent;
|
|
467
|
+
while (ts.isParenthesizedExpression(p)) {
|
|
468
|
+
cur = p;
|
|
469
|
+
p = p.parent;
|
|
470
|
+
}
|
|
471
|
+
const directCall = ts.isCallExpression(p) && p.expression === cur && !p.questionDotToken;
|
|
472
|
+
const callbackArg =
|
|
473
|
+
ts.isCallExpression(p) &&
|
|
474
|
+
(p.arguments as readonly ts.Node[]).includes(cur) &&
|
|
475
|
+
ts.isPropertyAccessExpression(p.expression);
|
|
476
|
+
if (!directCall && !callbackArg) {
|
|
477
|
+
found = `\`${name}\` is used as a value beyond a direct call or an array-method callback (call it, or inline the logic)`;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
ts.forEachChild(n, visit);
|
|
481
|
+
};
|
|
482
|
+
visit(fn);
|
|
483
|
+
return found;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/// Legality of a `++`/`--` or assignment used AS A VALUE (`arr[i++]`,
|
|
487
|
+
/// `const n = ++count`, `const z = (y = 5)`). The lowering splits the step
|
|
488
|
+
/// into its own statement and reads the variable, which is JS-order-exact
|
|
489
|
+
/// only when BOTH hold:
|
|
490
|
+
///
|
|
491
|
+
/// - the step cannot be skipped or repeated: it does not sit in a
|
|
492
|
+
/// short-circuit right operand, a ternary branch, a loop condition or
|
|
493
|
+
/// incrementor slot, or a case label (all positions JS may not reach);
|
|
494
|
+
/// - the stepped variable's ONLY mention in the evaluation unit (the
|
|
495
|
+
/// enclosing statement's expression, or the callback body) is the step
|
|
496
|
+
/// itself — another read would observe the mid-expression order the
|
|
497
|
+
/// split statement cannot reproduce.
|
|
498
|
+
///
|
|
499
|
+
/// Number-typed identifier locals only: the value read back after the
|
|
500
|
+
/// statement equals the expression's JS value for every operator form.
|
|
501
|
+
export function valuePositionStep(expr: ts.Expression, target: ts.Expression, tast: TypedAst): ValueStepVerdict {
|
|
502
|
+
const t = unwrapExpr(target);
|
|
503
|
+
if (!ts.isIdentifier(t)) {
|
|
504
|
+
return { ok: false, why: "the stepped target is not a plain local variable" };
|
|
505
|
+
}
|
|
506
|
+
const decl = tast.declarationOf(t);
|
|
507
|
+
if (!decl || !ts.isVariableDeclaration(decl)) {
|
|
508
|
+
return { ok: false, why: `\`${t.text}\` is not a local variable` };
|
|
509
|
+
}
|
|
510
|
+
const flags = tast.typeOf(t).flags;
|
|
511
|
+
if ((flags & (ts.TypeFlags.Number | ts.TypeFlags.NumberLiteral)) === 0) {
|
|
512
|
+
return { ok: false, why: `\`${t.text}\` is not number-typed (the split-statement value is pinned for numbers only)` };
|
|
513
|
+
}
|
|
514
|
+
let cur: ts.Node = expr;
|
|
515
|
+
let scope: ts.Node | null = null;
|
|
516
|
+
for (;;) {
|
|
517
|
+
const p: ts.Node = cur.parent;
|
|
518
|
+
if (!p || ts.isSourceFile(p)) {
|
|
519
|
+
return { ok: false, why: "the step sits at module level" };
|
|
520
|
+
}
|
|
521
|
+
if (ts.isConditionalExpression(p) && (p.whenTrue === cur || p.whenFalse === cur)) {
|
|
522
|
+
return { ok: false, why: "the step sits in a ternary branch JS may skip" };
|
|
523
|
+
}
|
|
524
|
+
if (ts.isBinaryExpression(p) && p.right === cur) {
|
|
525
|
+
const op = p.operatorToken.kind;
|
|
526
|
+
if (
|
|
527
|
+
op === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
528
|
+
op === ts.SyntaxKind.BarBarToken ||
|
|
529
|
+
op === ts.SyntaxKind.QuestionQuestionToken ||
|
|
530
|
+
op === ts.SyntaxKind.AmpersandAmpersandEqualsToken ||
|
|
531
|
+
op === ts.SyntaxKind.BarBarEqualsToken ||
|
|
532
|
+
op === ts.SyntaxKind.QuestionQuestionEqualsToken
|
|
533
|
+
) {
|
|
534
|
+
return { ok: false, why: "the step sits in a short-circuit right operand JS may skip" };
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if ((ts.isWhileStatement(p) || ts.isDoStatement(p)) && p.expression === cur) {
|
|
538
|
+
return { ok: false, why: "the step sits in a loop condition JS re-evaluates per iteration" };
|
|
539
|
+
}
|
|
540
|
+
if (ts.isForStatement(p) && (p.condition === cur || p.incrementor === cur)) {
|
|
541
|
+
return { ok: false, why: "the step sits in a for-loop head slot (the incrementor takes plain steps; the condition re-evaluates)" };
|
|
542
|
+
}
|
|
543
|
+
if (ts.isCaseClause(p) && p.expression === cur) {
|
|
544
|
+
return { ok: false, why: "the step sits in a case label JS evaluates only when earlier cases miss" };
|
|
545
|
+
}
|
|
546
|
+
if (
|
|
547
|
+
(ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p) || ts.isCallExpression(p)) &&
|
|
548
|
+
ts.isOptionalChain(p)
|
|
549
|
+
) {
|
|
550
|
+
// Conservative: a chain hop may short-circuit past the step.
|
|
551
|
+
return { ok: false, why: "the step rides an optional chain JS may short-circuit" };
|
|
552
|
+
}
|
|
553
|
+
if ((ts.isArrowFunction(p) || ts.isFunctionExpression(p)) && (p as ts.ArrowFunction).body === cur) {
|
|
554
|
+
scope = cur;
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
if (isStatementBoundary(p)) {
|
|
558
|
+
scope = cur;
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
cur = p;
|
|
562
|
+
}
|
|
563
|
+
let count = 0;
|
|
564
|
+
const visit = (n: ts.Node): void => {
|
|
565
|
+
if (ts.isIdentifier(n) && n !== decl.name && tast.declarationOf(n) === decl) count += 1;
|
|
566
|
+
ts.forEachChild(n, visit);
|
|
567
|
+
};
|
|
568
|
+
visit(scope);
|
|
569
|
+
if (count !== 1) {
|
|
570
|
+
return {
|
|
571
|
+
ok: false,
|
|
572
|
+
why: `\`${t.text}\` is mentioned ${count} times in the same evaluation unit (the split statement would lose JS's mid-expression order)`,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
return { ok: true };
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function isStatementBoundary(p: ts.Node): boolean {
|
|
579
|
+
return (
|
|
580
|
+
ts.isExpressionStatement(p) ||
|
|
581
|
+
ts.isReturnStatement(p) ||
|
|
582
|
+
ts.isIfStatement(p) ||
|
|
583
|
+
ts.isSwitchStatement(p) ||
|
|
584
|
+
ts.isVariableStatement(p) ||
|
|
585
|
+
ts.isForOfStatement(p) ||
|
|
586
|
+
ts.isForStatement(p) ||
|
|
587
|
+
ts.isBlock(p) ||
|
|
588
|
+
ts.isSourceFile(p)
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/// Method names of a data class whose bodies write the receiver: an
|
|
593
|
+
/// assignment/compound/step into `this.field`, or a call of another
|
|
594
|
+
/// mutating method on `this` (computed to a fixpoint). These emit with a
|
|
595
|
+
/// pointer receiver, and their call sites require a locally-owned instance.
|
|
596
|
+
export function mutatingMethodNames(decl: ts.ClassDeclaration): Set<string> {
|
|
597
|
+
const bodies = new Map<string, ts.Node>();
|
|
598
|
+
for (const m of decl.members) {
|
|
599
|
+
if (ts.isMethodDeclaration(m) && m.name && ts.isIdentifier(m.name) && m.body) {
|
|
600
|
+
bodies.set(m.name.text, m.body);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const isThisField = (e: ts.Expression): boolean => {
|
|
604
|
+
const u = unwrapExpr(e);
|
|
605
|
+
return ts.isPropertyAccessExpression(u) && u.expression.kind === ts.SyntaxKind.ThisKeyword;
|
|
606
|
+
};
|
|
607
|
+
const mutating = new Set<string>();
|
|
608
|
+
const writesThis = (body: ts.Node): boolean => {
|
|
609
|
+
let found = false;
|
|
610
|
+
const visit = (n: ts.Node): void => {
|
|
611
|
+
if (found) return;
|
|
612
|
+
if (
|
|
613
|
+
ts.isBinaryExpression(n) &&
|
|
614
|
+
compoundAndPlainAssignmentOps.has(n.operatorToken.kind) &&
|
|
615
|
+
isThisField(n.left)
|
|
616
|
+
) {
|
|
617
|
+
found = true;
|
|
618
|
+
} else if (
|
|
619
|
+
(ts.isPrefixUnaryExpression(n) || ts.isPostfixUnaryExpression(n)) &&
|
|
620
|
+
(n.operator === ts.SyntaxKind.PlusPlusToken || n.operator === ts.SyntaxKind.MinusMinusToken) &&
|
|
621
|
+
isThisField(n.operand)
|
|
622
|
+
) {
|
|
623
|
+
found = true;
|
|
624
|
+
} else if (
|
|
625
|
+
ts.isCallExpression(n) &&
|
|
626
|
+
ts.isPropertyAccessExpression(n.expression) &&
|
|
627
|
+
n.expression.expression.kind === ts.SyntaxKind.ThisKeyword &&
|
|
628
|
+
mutating.has(n.expression.name.text)
|
|
629
|
+
) {
|
|
630
|
+
found = true;
|
|
631
|
+
}
|
|
632
|
+
ts.forEachChild(n, visit);
|
|
633
|
+
};
|
|
634
|
+
visit(body);
|
|
635
|
+
return found;
|
|
636
|
+
};
|
|
637
|
+
for (;;) {
|
|
638
|
+
let grew = false;
|
|
639
|
+
for (const [name, body] of bodies) {
|
|
640
|
+
if (!mutating.has(name) && writesThis(body)) {
|
|
641
|
+
mutating.add(name);
|
|
642
|
+
grew = true;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (!grew) break;
|
|
646
|
+
}
|
|
647
|
+
return mutating;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/// Assignment operators that WRITE their target (plain and compound forms).
|
|
651
|
+
export const compoundAndPlainAssignmentOps = new Set<ts.SyntaxKind>([
|
|
652
|
+
ts.SyntaxKind.EqualsToken,
|
|
653
|
+
ts.SyntaxKind.PlusEqualsToken,
|
|
654
|
+
ts.SyntaxKind.MinusEqualsToken,
|
|
655
|
+
ts.SyntaxKind.AsteriskEqualsToken,
|
|
656
|
+
ts.SyntaxKind.SlashEqualsToken,
|
|
657
|
+
ts.SyntaxKind.PercentEqualsToken,
|
|
658
|
+
ts.SyntaxKind.AsteriskAsteriskEqualsToken,
|
|
659
|
+
ts.SyntaxKind.AmpersandEqualsToken,
|
|
660
|
+
ts.SyntaxKind.BarEqualsToken,
|
|
661
|
+
ts.SyntaxKind.CaretEqualsToken,
|
|
662
|
+
ts.SyntaxKind.LessThanLessThanEqualsToken,
|
|
663
|
+
ts.SyntaxKind.GreaterThanGreaterThanEqualsToken,
|
|
664
|
+
ts.SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken,
|
|
665
|
+
ts.SyntaxKind.QuestionQuestionEqualsToken,
|
|
666
|
+
ts.SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
667
|
+
ts.SyntaxKind.BarBarEqualsToken,
|
|
668
|
+
]);
|
|
669
|
+
|
|
670
|
+
/// The ownership verdict for mutating a CLASS INSTANCE at `site` (a field
|
|
671
|
+
/// write or a mutating-method call through `base`). Exactly the array rule
|
|
672
|
+
/// with `new ClassName(...)` as the owning constructor: your own fresh
|
|
673
|
+
/// instance is yours to mutate until it escapes; parameters, model data,
|
|
674
|
+
/// and aliased bindings teach.
|
|
675
|
+
export function instanceOwnership(tast: TypedAst, base: ts.Expression, site: ts.Node): OwnershipVerdict {
|
|
676
|
+
return ownershipOf(tast, base, site, (init) => {
|
|
677
|
+
const e = unwrapExpr(init);
|
|
678
|
+
return ts.isNewExpression(e) && ts.isIdentifier(e.expression) && e.expression.text !== "Uint8Array";
|
|
679
|
+
}, "instance");
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/// The ownership verdict for mutating `base` at `site`. `base` is the
|
|
683
|
+
/// receiver expression of a mutating method call or an indexed write.
|
|
684
|
+
export function arrayOwnership(tast: TypedAst, base: ts.Expression, site: ts.Node): OwnershipVerdict {
|
|
685
|
+
return ownershipOf(tast, base, site, isOwningInitializer, "array");
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function ownershipOf(
|
|
689
|
+
tast: TypedAst,
|
|
690
|
+
base: ts.Expression,
|
|
691
|
+
site: ts.Node,
|
|
692
|
+
owningInit: (init: ts.Expression) => boolean,
|
|
693
|
+
what: "array" | "instance",
|
|
694
|
+
): OwnershipVerdict {
|
|
695
|
+
const b = unwrapExpr(base);
|
|
696
|
+
if (!ts.isIdentifier(b)) {
|
|
697
|
+
return {
|
|
698
|
+
owned: false,
|
|
699
|
+
why: "not-local",
|
|
700
|
+
detail: `the ${what} is reached through a structure, not a local binding this function created`,
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
const decl = tast.declarationOf(b);
|
|
704
|
+
if (decl && ts.isParameter(decl)) {
|
|
705
|
+
return { owned: false, why: "not-local", detail: `\`${b.text}\` is a parameter — the caller's ${what}` };
|
|
706
|
+
}
|
|
707
|
+
if (!decl || !ts.isVariableDeclaration(decl)) {
|
|
708
|
+
return { owned: false, why: "not-local", detail: `\`${b.text}\` is not a local binding` };
|
|
709
|
+
}
|
|
710
|
+
const fn = enclosingFunctionOf(decl);
|
|
711
|
+
if (!fn) {
|
|
712
|
+
return {
|
|
713
|
+
owned: false,
|
|
714
|
+
why: "not-local",
|
|
715
|
+
detail: `\`${b.text}\` is module-level data, shared by every dispatch`,
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
if (!decl.initializer) {
|
|
719
|
+
return { owned: false, why: "not-local", detail: `\`${b.text}\` was declared without a creating initializer` };
|
|
720
|
+
}
|
|
721
|
+
if (!owningInit(decl.initializer)) {
|
|
722
|
+
const e = unwrapExpr(decl.initializer);
|
|
723
|
+
const detail = ts.isIdentifier(e)
|
|
724
|
+
? `\`${b.text}\` aliases \`${e.text}\` instead of owning fresh storage${what === "array" ? " — copy with `.slice()` to own it" : ""}`
|
|
725
|
+
: `\`${b.text}\` was not created by this function's own ${what === "array" ? "array construction — copy with `.slice()` to own it" : "`new` expression"}`;
|
|
726
|
+
return { owned: false, why: "not-local", detail };
|
|
727
|
+
}
|
|
728
|
+
if (isReassignedBinding(tast, decl, fn) && !everyAssignmentOwning(tast, decl, fn, owningInit)) {
|
|
729
|
+
return {
|
|
730
|
+
owned: false,
|
|
731
|
+
why: "not-local",
|
|
732
|
+
detail: `\`${b.text}\` is reassigned from a value this function does not own — a binding stays yours only while every assignment installs a fresh ${what === "array" ? "array construction (a literal or a copy)" : "\`new\` expression"}`,
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
const escape = escapeBefore(tast, decl, fn, site.getStart());
|
|
736
|
+
if (escape !== null) {
|
|
737
|
+
return { owned: false, why: "escaped", detail: `\`${b.text}\` ${escape}` };
|
|
738
|
+
}
|
|
739
|
+
return { owned: true, decl };
|
|
740
|
+
}
|