reading-stone 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +11 -0
- package/README.md +301 -2
- package/dist/analysis.d.ts +341 -0
- package/dist/analysis.js +392 -0
- package/dist/cognitive.d.ts +163 -0
- package/dist/cognitive.js +380 -0
- package/dist/complexity.d.ts +102 -0
- package/dist/complexity.js +163 -0
- package/dist/coverage.d.ts +78 -0
- package/dist/coverage.js +635 -0
- package/dist/detector.d.ts +41 -0
- package/dist/detector.js +2134 -0
- package/dist/discovery.d.ts +330 -0
- package/dist/discovery.js +930 -0
- package/dist/doc-coverage.d.ts +155 -0
- package/dist/doc-coverage.js +543 -0
- package/dist/extract.d.ts +209 -0
- package/dist/extract.js +498 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +63 -0
- package/dist/internal.d.ts +75 -0
- package/dist/internal.js +63 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.js +39 -0
- package/dist/models.d.ts +185 -0
- package/dist/models.js +170 -0
- package/dist/program.d.ts +207 -0
- package/dist/program.js +662 -0
- package/dist/protocol.d.ts +88 -0
- package/dist/protocol.js +74 -0
- package/dist/quality/assertions.d.ts +81 -0
- package/dist/quality/assertions.js +425 -0
- package/dist/quality/collect.d.ts +66 -0
- package/dist/quality/collect.js +412 -0
- package/dist/quality/inference.d.ts +86 -0
- package/dist/quality/inference.js +603 -0
- package/dist/quality/matchers.d.ts +129 -0
- package/dist/quality/matchers.js +220 -0
- package/dist/quality/pairing.d.ts +133 -0
- package/dist/quality/pairing.js +770 -0
- package/dist/quality/pipeline.d.ts +59 -0
- package/dist/quality/pipeline.js +202 -0
- package/dist/quality/types.d.ts +127 -0
- package/dist/quality/types.js +92 -0
- package/dist/safeio.d.ts +107 -0
- package/dist/safeio.js +314 -0
- package/dist/server.d.ts +19 -0
- package/dist/server.js +451 -0
- package/dist/signals/adapter.d.ts +103 -0
- package/dist/signals/adapter.js +323 -0
- package/dist/signals/caller-index.d.ts +117 -0
- package/dist/signals/caller-index.js +147 -0
- package/dist/signals/caller.d.ts +76 -0
- package/dist/signals/caller.js +108 -0
- package/dist/signals/evidence.d.ts +233 -0
- package/dist/signals/evidence.js +578 -0
- package/dist/signals/interface.d.ts +68 -0
- package/dist/signals/interface.js +87 -0
- package/dist/signals/jsdoc.d.ts +27 -0
- package/dist/signals/jsdoc.js +233 -0
- package/dist/signals/naming.d.ts +26 -0
- package/dist/signals/naming.js +160 -0
- package/dist/signals/routing.d.ts +163 -0
- package/dist/signals/routing.js +416 -0
- package/dist/signals/types.d.ts +146 -0
- package/dist/signals/types.js +99 -0
- package/dist/signals/visibility.d.ts +23 -0
- package/dist/signals/visibility.js +82 -0
- package/dist/taxonomy.d.ts +89 -0
- package/dist/taxonomy.js +217 -0
- package/package.json +61 -4
package/dist/detector.js
ADDED
|
@@ -0,0 +1,2134 @@
|
|
|
1
|
+
import { isArrowFunction, isBinaryExpression, isBlock, isCallExpression, isClassDeclaration, isClassExpression, isConstructorDeclaration, isDecorator, isDeleteExpression, isElementAccessExpression, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isImportDeclaration, isMethodDeclaration, isNamespaceImport, isNewExpression, isParenthesizedExpression, isPostfixUnaryExpression, isPrefixUnaryExpression, isPropertyAccessExpression, isPropertyDeclaration, isReturnStatement, isSetAccessorDeclaration, isStringLiteral, isThisExpression, isThrowStatement, isVariableDeclaration, isVariableStatement, isYieldExpression, ModifierFlags, NodeFlags, SyntaxKind, } from "typescript/unstable/ast";
|
|
2
|
+
import { TypeFlags } from "typescript/unstable/sync";
|
|
3
|
+
import { collectBindingNames, isExtractedFunctionLike, isSchedulingCall, TIMER_GLOBAL_NAMES, } from "./extract.js";
|
|
4
|
+
import { compareFileLineName } from "./internal.js";
|
|
5
|
+
import { derivePackage } from "./models.js";
|
|
6
|
+
import { resolveSideEffectType } from "./taxonomy.js";
|
|
7
|
+
/**
|
|
8
|
+
* The shared empty name set for scopes that bind nothing (the module
|
|
9
|
+
* frame's parameter set; the defensive fallback when the chain/ancestor
|
|
10
|
+
* zip runs short). Never mutated: `DetectionScope.declares` only grows a
|
|
11
|
+
* walk-entered frame's own live set.
|
|
12
|
+
*/
|
|
13
|
+
const EMPTY_NAMES = new Set();
|
|
14
|
+
/**
|
|
15
|
+
* The built-in constructors whose instances are error values (FR-013's
|
|
16
|
+
* "built-in Error subclasses"): the ECMAScript error constructors plus
|
|
17
|
+
* `AggregateError`. A module-scope constant initialized with `new` of
|
|
18
|
+
* any of these — or of a module-scope class that transitively extends
|
|
19
|
+
* one — is an Error-instance constant.
|
|
20
|
+
*/
|
|
21
|
+
const BUILTIN_ERROR_CONSTRUCTORS = new Set([
|
|
22
|
+
"Error",
|
|
23
|
+
"TypeError",
|
|
24
|
+
"RangeError",
|
|
25
|
+
"SyntaxError",
|
|
26
|
+
"ReferenceError",
|
|
27
|
+
"EvalError",
|
|
28
|
+
"URIError",
|
|
29
|
+
"AggregateError",
|
|
30
|
+
]);
|
|
31
|
+
/**
|
|
32
|
+
* The nine mutating array methods of the param-write split (FR-015):
|
|
33
|
+
* calls of these names on parameter receivers are `SliceMutation` —
|
|
34
|
+
* checker-proven array types decide in program mode (the element-write
|
|
35
|
+
* split's test), the receiver name and shape decide in parse-only (the
|
|
36
|
+
* disposition table's documented type-blind name-form surface).
|
|
37
|
+
*/
|
|
38
|
+
const ARRAY_MUTATOR_METHODS = new Set([
|
|
39
|
+
"push",
|
|
40
|
+
"pop",
|
|
41
|
+
"shift",
|
|
42
|
+
"unshift",
|
|
43
|
+
"splice",
|
|
44
|
+
"sort",
|
|
45
|
+
"reverse",
|
|
46
|
+
"fill",
|
|
47
|
+
"copyWithin",
|
|
48
|
+
]);
|
|
49
|
+
/**
|
|
50
|
+
* The membership mutators (FR-015): calls of these names fire
|
|
51
|
+
* `ContainerMutation` on any receiver — `set` is deliberately absent;
|
|
52
|
+
* it follows the parameter Map/WeakMap split instead.
|
|
53
|
+
*/
|
|
54
|
+
const MEMBERSHIP_MUTATOR_METHODS = new Set([
|
|
55
|
+
"add",
|
|
56
|
+
"delete",
|
|
57
|
+
"clear",
|
|
58
|
+
]);
|
|
59
|
+
/** `WriterOutput`'s name-form methods (the disposition table). */
|
|
60
|
+
const WRITER_OUTPUT_METHODS = new Set([
|
|
61
|
+
"write",
|
|
62
|
+
"writeLine",
|
|
63
|
+
"append",
|
|
64
|
+
]);
|
|
65
|
+
/**
|
|
66
|
+
* `HTTPResponseWrite`'s name-form methods — checked before
|
|
67
|
+
* `WriterOutput`'s families so `res.write(...)` is HTTP, never a
|
|
68
|
+
* generic writer (research R6.4's precedence).
|
|
69
|
+
*/
|
|
70
|
+
const HTTP_RESPONSE_METHODS = new Set([
|
|
71
|
+
"send",
|
|
72
|
+
"json",
|
|
73
|
+
"jsonp",
|
|
74
|
+
"writeHead",
|
|
75
|
+
"write",
|
|
76
|
+
"end",
|
|
77
|
+
]);
|
|
78
|
+
/** `HTTPResponseWrite`'s name-form receiver spellings. */
|
|
79
|
+
const HTTP_RESPONSE_RECEIVER_NAMES = new Set(["res", "response"]);
|
|
80
|
+
/**
|
|
81
|
+
* `HTTPResponseWrite`'s checker-mode receiver type names (Node's
|
|
82
|
+
* `ServerResponse`; the fetch/express-style `Response`).
|
|
83
|
+
*/
|
|
84
|
+
const HTTP_RESPONSE_TYPE_NAMES = new Set([
|
|
85
|
+
"ServerResponse",
|
|
86
|
+
"Response",
|
|
87
|
+
]);
|
|
88
|
+
/** `ChannelClose`'s name-form receiver spellings (the MessagePort family). */
|
|
89
|
+
const CHANNEL_CLOSE_RECEIVER_NAMES = new Set([
|
|
90
|
+
"port",
|
|
91
|
+
"channel",
|
|
92
|
+
"worker",
|
|
93
|
+
]);
|
|
94
|
+
/** `ChannelClose`'s checker-mode receiver type names. */
|
|
95
|
+
const CHANNEL_CLOSE_TYPE_NAMES = new Set([
|
|
96
|
+
"MessagePort",
|
|
97
|
+
"MessageChannel",
|
|
98
|
+
"BroadcastChannel",
|
|
99
|
+
"Worker",
|
|
100
|
+
]);
|
|
101
|
+
/**
|
|
102
|
+
* The fs module specifiers whose imported bindings count as fs receivers
|
|
103
|
+
* (the disposition table's FileSystemWrite/Delete/Meta rows: "on
|
|
104
|
+
* `fs`/`node:fs/promises` receivers or default-imported fs bindings").
|
|
105
|
+
* Both the default-import form (`import fsp from "node:fs"`) and the
|
|
106
|
+
* namespace form (`import * as fsp from "node:fs"`) bind the module's
|
|
107
|
+
* API surface, so both count — excluding the namespace form would be a
|
|
108
|
+
* false negative with no precision gain.
|
|
109
|
+
*/
|
|
110
|
+
const FS_MODULE_SPECIFIERS = new Set([
|
|
111
|
+
"fs",
|
|
112
|
+
"node:fs",
|
|
113
|
+
"fs/promises",
|
|
114
|
+
"node:fs/promises",
|
|
115
|
+
]);
|
|
116
|
+
/**
|
|
117
|
+
* `FileSystemWrite`'s name-form methods (the disposition table): the
|
|
118
|
+
* write family — `writeFile`/`appendFile`/`rename`/`truncate`/
|
|
119
|
+
* `createWriteStream` — plus their `Sync` variants (the promises forms
|
|
120
|
+
* spell identically to the base names) and the low-level fd-based
|
|
121
|
+
* `write`, research R6.4's pinned precedence carve-out — `.write` on an
|
|
122
|
+
* fs receiver is FileSystemWrite, never WriterOutput.
|
|
123
|
+
*/
|
|
124
|
+
const FILE_SYSTEM_WRITE_METHODS = new Set([
|
|
125
|
+
"write",
|
|
126
|
+
"writeFile",
|
|
127
|
+
"writeFileSync",
|
|
128
|
+
"appendFile",
|
|
129
|
+
"appendFileSync",
|
|
130
|
+
"rename",
|
|
131
|
+
"renameSync",
|
|
132
|
+
"truncate",
|
|
133
|
+
"truncateSync",
|
|
134
|
+
"createWriteStream",
|
|
135
|
+
]);
|
|
136
|
+
/**
|
|
137
|
+
* `FileSystemDelete`'s name-form methods (the disposition table):
|
|
138
|
+
* `unlink`/`rm`/`rmdir` plus their `Sync` variants.
|
|
139
|
+
*/
|
|
140
|
+
const FILE_SYSTEM_DELETE_METHODS = new Set([
|
|
141
|
+
"unlink",
|
|
142
|
+
"unlinkSync",
|
|
143
|
+
"rm",
|
|
144
|
+
"rmSync",
|
|
145
|
+
"rmdir",
|
|
146
|
+
"rmdirSync",
|
|
147
|
+
]);
|
|
148
|
+
/**
|
|
149
|
+
* `FileSystemMeta`'s name-form methods (the disposition table):
|
|
150
|
+
* `chmod`/`chown`/`utimes`/`lutimes`/`mkdir` plus their `Sync` variants.
|
|
151
|
+
* `stat`/`lstat` are reads and are deliberately absent.
|
|
152
|
+
*/
|
|
153
|
+
const FILE_SYSTEM_META_METHODS = new Set([
|
|
154
|
+
"chmod",
|
|
155
|
+
"chmodSync",
|
|
156
|
+
"chown",
|
|
157
|
+
"chownSync",
|
|
158
|
+
"utimes",
|
|
159
|
+
"utimesSync",
|
|
160
|
+
"lutimes",
|
|
161
|
+
"lutimesSync",
|
|
162
|
+
"mkdir",
|
|
163
|
+
"mkdirSync",
|
|
164
|
+
]);
|
|
165
|
+
/**
|
|
166
|
+
* `DatabaseWrite`'s name-form methods (the disposition table): the
|
|
167
|
+
* MongoDB-style write names on any receiver — the documented
|
|
168
|
+
* false-positive surface of the partial tier.
|
|
169
|
+
*/
|
|
170
|
+
const DATABASE_WRITE_METHODS = new Set([
|
|
171
|
+
"insertOne",
|
|
172
|
+
"insertMany",
|
|
173
|
+
"updateOne",
|
|
174
|
+
"updateMany",
|
|
175
|
+
"deleteOne",
|
|
176
|
+
"deleteMany",
|
|
177
|
+
"findOneAndUpdate",
|
|
178
|
+
"bulkWrite",
|
|
179
|
+
]);
|
|
180
|
+
/** `DatabaseTransaction`'s name-form methods (the disposition table). */
|
|
181
|
+
const DATABASE_TRANSACTION_METHODS = new Set([
|
|
182
|
+
"beginTransaction",
|
|
183
|
+
"commit",
|
|
184
|
+
"rollback",
|
|
185
|
+
"transact",
|
|
186
|
+
]);
|
|
187
|
+
/**
|
|
188
|
+
* `LogWrite`'s name-form methods (the disposition table): the logger
|
|
189
|
+
* level names. `console.*` is excluded by the receiver — the stdio
|
|
190
|
+
* family owns it.
|
|
191
|
+
*/
|
|
192
|
+
const LOG_WRITE_METHODS = new Set([
|
|
193
|
+
"info",
|
|
194
|
+
"warn",
|
|
195
|
+
"error",
|
|
196
|
+
"debug",
|
|
197
|
+
"log",
|
|
198
|
+
"trace",
|
|
199
|
+
]);
|
|
200
|
+
/** `LogWrite`'s name-form receiver spellings (the disposition table). */
|
|
201
|
+
const LOG_RECEIVER_NAMES = new Set(["log", "logger"]);
|
|
202
|
+
/** `LogWrite`'s checker-mode receiver type names. */
|
|
203
|
+
const LOG_TYPE_NAMES = new Set(["Logger"]);
|
|
204
|
+
/**
|
|
205
|
+
* `StdoutWrite`'s console methods (the disposition table):
|
|
206
|
+
* `console.log`/`info`/`debug`/`trace` — detected.
|
|
207
|
+
*/
|
|
208
|
+
const CONSOLE_STDOUT_METHODS = new Set([
|
|
209
|
+
"log",
|
|
210
|
+
"info",
|
|
211
|
+
"debug",
|
|
212
|
+
"trace",
|
|
213
|
+
]);
|
|
214
|
+
/**
|
|
215
|
+
* `StderrWrite`'s console methods (the disposition table):
|
|
216
|
+
* `console.error`/`warn` — detected.
|
|
217
|
+
*/
|
|
218
|
+
const CONSOLE_STDERR_METHODS = new Set(["error", "warn"]);
|
|
219
|
+
/**
|
|
220
|
+
* `ResourceManagement`'s disposal name-forms (the disposition table):
|
|
221
|
+
* `.close()`/`.dispose()` on resource receivers (file handles,
|
|
222
|
+
* connections) — deliberately checked below `ChannelClose`'s receivers
|
|
223
|
+
* (research R6.4's pinned precedence: the MessagePort family's
|
|
224
|
+
* `.close()` is a channel close, never a resource release).
|
|
225
|
+
*/
|
|
226
|
+
const RESOURCE_DISPOSAL_METHODS = new Set(["close", "dispose"]);
|
|
227
|
+
/**
|
|
228
|
+
* `FinalizerRegistration`'s checker-mode receiver type names (the
|
|
229
|
+
* disposition table: `.register(...)` on FinalizationRegistry
|
|
230
|
+
* receivers, typed in program mode — the parse-only surface is the
|
|
231
|
+
* construction form alone, a documented best-effort boundary).
|
|
232
|
+
*/
|
|
233
|
+
const FINALIZER_TYPE_NAMES = new Set(["FinalizationRegistry"]);
|
|
234
|
+
/**
|
|
235
|
+
* Detects the P0 and P1 side effects of every extraction record
|
|
236
|
+
* (FR-011 through FR-017; research R6).
|
|
237
|
+
*
|
|
238
|
+
* Each record is walked exactly once by a fresh `RecordWalker` (no
|
|
239
|
+
* cross-record state), then `SentinelError` declarations attach per
|
|
240
|
+
* file, then every record's effects are sorted into FR-020's documented
|
|
241
|
+
* `(line, column, canonical type name)` order — dual emissions at one
|
|
242
|
+
* construct and sentinel attachments make emission order alone
|
|
243
|
+
* under-specified, so the total order is applied here where every
|
|
244
|
+
* record's list is complete.
|
|
245
|
+
*
|
|
246
|
+
* @param records The extraction records (any order; the result map
|
|
247
|
+
* preserves the given record order).
|
|
248
|
+
* @param context The analysis context the records came from: its mode
|
|
249
|
+
* decides the typed splits' behavior — program mode consults the live
|
|
250
|
+
* checker, parse-only takes each split's documented type-blind default.
|
|
251
|
+
* @returns Each record's effects, sorted by `(line, column, type)` —
|
|
252
|
+
* empty for a record whose body contains no pinned constructs.
|
|
253
|
+
*/
|
|
254
|
+
export function detectEffects(records, context) {
|
|
255
|
+
const pending = new Map();
|
|
256
|
+
// The checker is live only in program mode; parse-only walks are
|
|
257
|
+
// purely lexical, and the typed splits take their documented
|
|
258
|
+
// type-blind defaults (FR-015).
|
|
259
|
+
const checker = context.mode === "program" ? context.checker : undefined;
|
|
260
|
+
for (const record of records) {
|
|
261
|
+
const walker = new RecordWalker(record, checker);
|
|
262
|
+
walker.walk();
|
|
263
|
+
pending.set(record, walker.pending);
|
|
264
|
+
}
|
|
265
|
+
attachSentinelEffects(records, pending);
|
|
266
|
+
const effects = new Map();
|
|
267
|
+
for (const record of records) {
|
|
268
|
+
const list = pending.get(record) ?? [];
|
|
269
|
+
list.sort(comparePendingEffects);
|
|
270
|
+
effects.set(record, list.map((entry) => entry.effect));
|
|
271
|
+
}
|
|
272
|
+
return effects;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* FR-020's response assembly: turns the detection result into the
|
|
276
|
+
* `(file, line, name)`-sorted `FunctionRecord[]` — the analyze method's
|
|
277
|
+
* record payload (research R8's record-level total order). Extraction
|
|
278
|
+
* already emits in that order; sorting here makes the order structural
|
|
279
|
+
* rather than inherited, so the wire guarantee holds whatever order the
|
|
280
|
+
* records arrive in.
|
|
281
|
+
*
|
|
282
|
+
* Each record's `package` is derived through `derivePackage` and its
|
|
283
|
+
* `side_effects` are the detector's attribution for that record (empty
|
|
284
|
+
* for records the detection walk left clean — never fabricated).
|
|
285
|
+
*
|
|
286
|
+
* @param records The extraction records, any order.
|
|
287
|
+
* @param effects The detection result — `detectEffects`' per-record map.
|
|
288
|
+
* @returns The assembled records, sorted by `(file, line, name)`.
|
|
289
|
+
*/
|
|
290
|
+
export function assembleFunctionRecords(records, effects) {
|
|
291
|
+
const assembled = records.map((record) => ({
|
|
292
|
+
name: record.name,
|
|
293
|
+
package: derivePackage(record.relPath),
|
|
294
|
+
file: record.relPath,
|
|
295
|
+
line: record.line,
|
|
296
|
+
side_effects: effects.get(record) ?? [],
|
|
297
|
+
}));
|
|
298
|
+
assembled.sort(compareFileLineName);
|
|
299
|
+
return assembled;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* FR-020's within-record effect order: `(line, column, canonical type
|
|
303
|
+
* name)` with codepoint comparison for the type name and `0` on
|
|
304
|
+
* full-key ties so the platform-default stable sort keeps emission
|
|
305
|
+
* order (declaration order for same-position same-type pairs).
|
|
306
|
+
*
|
|
307
|
+
* @param a Left effect.
|
|
308
|
+
* @param b Right effect.
|
|
309
|
+
* @returns The three-key comparison result.
|
|
310
|
+
*/
|
|
311
|
+
function comparePendingEffects(a, b) {
|
|
312
|
+
if (a.line !== b.line) {
|
|
313
|
+
return a.line - b.line;
|
|
314
|
+
}
|
|
315
|
+
if (a.column !== b.column) {
|
|
316
|
+
return a.column - b.column;
|
|
317
|
+
}
|
|
318
|
+
if (a.effect.type !== b.effect.type) {
|
|
319
|
+
return a.effect.type < b.effect.type ? -1 : 1;
|
|
320
|
+
}
|
|
321
|
+
return 0;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* The one detail payload every FR-017 ambiguity effect carries
|
|
325
|
+
* (`{"confidence": "ambiguous"}`) — shared so the wire shape is
|
|
326
|
+
* structurally uniform across every ambiguity rule (the wire test pins
|
|
327
|
+
* the exact object on every `detail`-bearing effect).
|
|
328
|
+
*/
|
|
329
|
+
const AMBIGUOUS_DETAIL = Object.freeze({
|
|
330
|
+
confidence: "ambiguous",
|
|
331
|
+
});
|
|
332
|
+
/**
|
|
333
|
+
* Constructs one pending effect through the canonical-type resolver
|
|
334
|
+
* (FR-018): `resolveSideEffectType` passes canonical names through, so a
|
|
335
|
+
* miss is a programming error in this module's rules and fails loud
|
|
336
|
+
* (Constitution Principle I) rather than emitting an invalid type.
|
|
337
|
+
* `location` is the construct's root-relative `file:line:col` from the
|
|
338
|
+
* node's no-trivia start (R6.6); `target` and `detail` are included only
|
|
339
|
+
* when the rule pins one (the omit-unset-optionals rule, models.md).
|
|
340
|
+
*
|
|
341
|
+
* @param type The canonical side-effect type name.
|
|
342
|
+
* @param description Deterministic description template.
|
|
343
|
+
* @param relPath Root-relative POSIX path of the declaring file.
|
|
344
|
+
* @param node The construct node — the location's anchor.
|
|
345
|
+
* @param sourceFile The declaring source, for position math.
|
|
346
|
+
* @param target The R6.6 target, when the rule pins one.
|
|
347
|
+
* @param detail The opaque detail payload, when the rule pins one.
|
|
348
|
+
* @returns The pending effect, with its numeric sort keys.
|
|
349
|
+
* @throws Error when `type` resolves to no canonical side-effect type.
|
|
350
|
+
*/
|
|
351
|
+
function constructEffect(type, description, relPath, node, sourceFile, target, detail) {
|
|
352
|
+
const canonical = resolveSideEffectType(type);
|
|
353
|
+
if (canonical === undefined) {
|
|
354
|
+
throw new Error(`Invalid side effect type: ${type}`);
|
|
355
|
+
}
|
|
356
|
+
const position = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
|
|
357
|
+
const location = `${relPath}:${position.line + 1}:${position.character + 1}`;
|
|
358
|
+
const effect = {
|
|
359
|
+
type: canonical,
|
|
360
|
+
description,
|
|
361
|
+
location,
|
|
362
|
+
...(target === undefined ? {} : { target }),
|
|
363
|
+
...(detail === undefined ? {} : { detail }),
|
|
364
|
+
};
|
|
365
|
+
return {
|
|
366
|
+
effect,
|
|
367
|
+
line: position.line + 1,
|
|
368
|
+
column: position.character + 1,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Attaches each file's `SentinelError` declarations to its first record
|
|
373
|
+
* (FR-013; research R6 decision 2). The first record is identified by
|
|
374
|
+
* minimal `(line, position)` across the file's records — before any
|
|
375
|
+
* global ordering is assumed — so the attachment is a pure function of
|
|
376
|
+
* the file's source layout. A file whose records list is empty (no
|
|
377
|
+
* functions declared) never appears here and attaches nothing: its
|
|
378
|
+
* sentinels go unreported, exactly the documented no-record case.
|
|
379
|
+
*
|
|
380
|
+
* @param records The extraction records, any order.
|
|
381
|
+
* @param pending The per-record pending-effect lists to append to.
|
|
382
|
+
*/
|
|
383
|
+
function attachSentinelEffects(records, pending) {
|
|
384
|
+
const firstPerFile = new Map();
|
|
385
|
+
for (const record of records) {
|
|
386
|
+
const sourceFile = record.node.getSourceFile();
|
|
387
|
+
const position = record.node.getStart(sourceFile);
|
|
388
|
+
const known = firstPerFile.get(record.relPath);
|
|
389
|
+
if (known === undefined ||
|
|
390
|
+
record.line < known.line ||
|
|
391
|
+
(record.line === known.line && position < known.position)) {
|
|
392
|
+
firstPerFile.set(record.relPath, {
|
|
393
|
+
record,
|
|
394
|
+
sourceFile,
|
|
395
|
+
line: record.line,
|
|
396
|
+
position,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
for (const first of firstPerFile.values()) {
|
|
401
|
+
const list = pending.get(first.record);
|
|
402
|
+
if (list === undefined) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
for (const sentinel of moduleScopeSentinelDeclarations(first.sourceFile)) {
|
|
406
|
+
list.push(constructEffect("SentinelError", sentinel.description, first.record.relPath, sentinel.node, first.sourceFile));
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Collects the module-scope Error sentinels of one file (FR-013):
|
|
412
|
+
* Error-subclass class declarations and Error-instance constants,
|
|
413
|
+
* declared directly under the `SourceFile` (module scope — sentinels
|
|
414
|
+
* nested in functions or classes are not module-scope and never match).
|
|
415
|
+
*
|
|
416
|
+
* Resolution is two-pass so project-local subclasses resolve
|
|
417
|
+
* transitively: the first pass maps every module-scope class name to
|
|
418
|
+
* its `extends` clause's identifier spelling; the second classifies
|
|
419
|
+
* declarations against that map — `class A extends Error` directly,
|
|
420
|
+
* `class B extends A` through the chain (with a cycle guard), and
|
|
421
|
+
* `const e = new B(...)` instance constants through the same
|
|
422
|
+
* `isErrorSubclassName` test. Heritage forms other than a single
|
|
423
|
+
* identifier (`extends lib.Base`, `extends (mixins())`) and non-`new`
|
|
424
|
+
* initializers (`const E = Error` — a class reference, not an instance)
|
|
425
|
+
* resolve to no module-scope class and never match — the documented
|
|
426
|
+
* boundary.
|
|
427
|
+
*
|
|
428
|
+
* @param sourceFile The file whose top-level statements are scanned.
|
|
429
|
+
* @returns The sentinel declarations, in statement order.
|
|
430
|
+
*/
|
|
431
|
+
function moduleScopeSentinelDeclarations(sourceFile) {
|
|
432
|
+
const classParents = new Map();
|
|
433
|
+
for (const statement of sourceFile.statements) {
|
|
434
|
+
if (isClassDeclaration(statement) && statement.name !== undefined) {
|
|
435
|
+
const parent = extendsClauseIdentifier(statement);
|
|
436
|
+
if (parent !== null) {
|
|
437
|
+
classParents.set(statement.name.text, parent);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const sentinels = [];
|
|
442
|
+
for (const statement of sourceFile.statements) {
|
|
443
|
+
if (isClassDeclaration(statement)) {
|
|
444
|
+
if (statement.name !== undefined &&
|
|
445
|
+
isErrorSubclassName(statement.name.text, classParents)) {
|
|
446
|
+
sentinels.push({
|
|
447
|
+
node: statement,
|
|
448
|
+
description: `declares module-scope error subclass ${statement.name.text}`,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (isVariableStatement(statement)) {
|
|
454
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
455
|
+
const name = isIdentifier(declaration.name) ? declaration.name.text : null;
|
|
456
|
+
const constructed = newedConstructorName(declaration.initializer);
|
|
457
|
+
if (name !== null &&
|
|
458
|
+
constructed !== null &&
|
|
459
|
+
isErrorSubclassName(constructed, classParents)) {
|
|
460
|
+
sentinels.push({
|
|
461
|
+
node: statement,
|
|
462
|
+
description: `declares module-scope error constant ${name}`,
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return sentinels;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* The identifier spelling of a class declaration's `extends` clause, or
|
|
472
|
+
* `null` when it has none (or the clause is not a single identifier).
|
|
473
|
+
* Only the `extends` clause is consulted — `implements` contributes no
|
|
474
|
+
* inheritance.
|
|
475
|
+
*
|
|
476
|
+
* @param node The class declaration.
|
|
477
|
+
* @returns The extended name as written, or `null`.
|
|
478
|
+
*/
|
|
479
|
+
function extendsClauseIdentifier(node) {
|
|
480
|
+
if (node.heritageClauses === undefined) {
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
for (const clause of node.heritageClauses) {
|
|
484
|
+
if (clause.token !== SyntaxKind.ExtendsKeyword) {
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
for (const type of clause.types) {
|
|
488
|
+
if (isIdentifier(type.expression)) {
|
|
489
|
+
return type.expression.text;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The constructor name of a `new X(...)` initializer, when the
|
|
497
|
+
* initializer is a `new` expression over a single identifier — the form
|
|
498
|
+
* whose instances resolve against the module's classes and the built-in
|
|
499
|
+
* error constructors.
|
|
500
|
+
*
|
|
501
|
+
* @param initializer The variable-declaration initializer, if any.
|
|
502
|
+
* @returns The constructed name, or `null` for every other shape.
|
|
503
|
+
*/
|
|
504
|
+
function newedConstructorName(initializer) {
|
|
505
|
+
if (initializer === undefined || !isNewExpression(initializer)) {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
return isIdentifier(initializer.expression) ? initializer.expression.text : null;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Whether a name resolves to an error-constructor chain: a built-in
|
|
512
|
+
* error constructor, or a module-scope class whose `extends` chain
|
|
513
|
+
* (followed through `classParents`, with a cycle guard) terminates at
|
|
514
|
+
* one. Unresolvable names — imports, non-class locals, unresolvable
|
|
515
|
+
* heritage — end the walk as non-errors.
|
|
516
|
+
*
|
|
517
|
+
* @param name The candidate class or constructor name.
|
|
518
|
+
* @param classParents Module-scope class name → extended name.
|
|
519
|
+
* @returns Whether the name is an Error subclass.
|
|
520
|
+
*/
|
|
521
|
+
function isErrorSubclassName(name, classParents) {
|
|
522
|
+
const seen = new Set();
|
|
523
|
+
let current = name;
|
|
524
|
+
while (current !== undefined && !seen.has(current)) {
|
|
525
|
+
if (BUILTIN_ERROR_CONSTRUCTORS.has(current)) {
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
seen.add(current);
|
|
529
|
+
current = classParents.get(current);
|
|
530
|
+
}
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Collects one file's module-scope import bindings: every top-level
|
|
535
|
+
* import declaration with a string-literal module specifier contributes
|
|
536
|
+
* its default-binding name (if any) and its namespace-binding name (if
|
|
537
|
+
* any). A pure function of the file's top-level statements, so the
|
|
538
|
+
* per-record recomputation in `RecordWalker`'s constructor is
|
|
539
|
+
* deterministic; records of one file share the same bounded statement
|
|
540
|
+
* list, keeping the repeated scan trivial.
|
|
541
|
+
*
|
|
542
|
+
* @param sourceFile The file whose import declarations are scanned.
|
|
543
|
+
* @returns The default- and namespace-binding maps, name → specifier.
|
|
544
|
+
*/
|
|
545
|
+
function scanImportBindings(sourceFile) {
|
|
546
|
+
const defaultSpecifiers = new Map();
|
|
547
|
+
const namespaceSpecifiers = new Map();
|
|
548
|
+
for (const statement of sourceFile.statements) {
|
|
549
|
+
if (!isImportDeclaration(statement)) {
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
const specifier = isStringLiteral(statement.moduleSpecifier)
|
|
553
|
+
? statement.moduleSpecifier.text
|
|
554
|
+
: null;
|
|
555
|
+
const clause = statement.importClause;
|
|
556
|
+
if (specifier === null || clause === undefined) {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (clause.name !== undefined) {
|
|
560
|
+
defaultSpecifiers.set(clause.name.text, specifier);
|
|
561
|
+
}
|
|
562
|
+
if (clause.namedBindings !== undefined && isNamespaceImport(clause.namedBindings)) {
|
|
563
|
+
namespaceSpecifiers.set(clause.namedBindings.name.text, specifier);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return { defaultSpecifiers, namespaceSpecifiers };
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* One scope frame of the detection walk: the parameter names declared
|
|
570
|
+
* in it and the binding names visible through it. Two flavors:
|
|
571
|
+
*
|
|
572
|
+
* - **Derived** frames mirror an extractor scope-chain frame — the
|
|
573
|
+
* chain's name set is hoisting-complete by the time extraction
|
|
574
|
+
* returns, so the frame is fixed and `declares` is a no-op.
|
|
575
|
+
* - **Entered** frames are created when the walk descends into an
|
|
576
|
+
* anonymous function-like below the record — parameters seed a live,
|
|
577
|
+
* growing name set that collects the body's declarations as the walk
|
|
578
|
+
* proceeds (the later tiers' write classification reads it, so a
|
|
579
|
+
* local inside a callback never misresolves to an outer scope).
|
|
580
|
+
*
|
|
581
|
+
* Parameter resolution is the walker's: it scans the stack innermost
|
|
582
|
+
* frame outward and asks the first frame that binds the name whether it
|
|
583
|
+
* bound it as a parameter — shadowing resolved by scope order.
|
|
584
|
+
*/
|
|
585
|
+
class DetectionScope {
|
|
586
|
+
params;
|
|
587
|
+
fixed;
|
|
588
|
+
growing;
|
|
589
|
+
constructor(params, fixed, growing) {
|
|
590
|
+
this.params = params;
|
|
591
|
+
this.fixed = fixed;
|
|
592
|
+
this.growing = growing;
|
|
593
|
+
}
|
|
594
|
+
/** A chain-derived frame: `names` is complete; nothing grows. */
|
|
595
|
+
static derived(params, names) {
|
|
596
|
+
return new DetectionScope(params, names, undefined);
|
|
597
|
+
}
|
|
598
|
+
/** A walk-entered frame: parameters seed a live, growing set. */
|
|
599
|
+
static entered(params) {
|
|
600
|
+
return new DetectionScope(params, EMPTY_NAMES, new Set(params));
|
|
601
|
+
}
|
|
602
|
+
/** Whether the frame makes `name` visible (as parameter or local). */
|
|
603
|
+
binds(name) {
|
|
604
|
+
return this.fixed.has(name) || (this.growing?.has(name) ?? false);
|
|
605
|
+
}
|
|
606
|
+
/** Records a declaration into the frame, when it grows. */
|
|
607
|
+
declares(name) {
|
|
608
|
+
this.growing?.add(name);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Type guard for the function-like union.
|
|
613
|
+
*
|
|
614
|
+
* @param node The node to test.
|
|
615
|
+
* @returns Whether the node is one of the seven function-like forms.
|
|
616
|
+
*/
|
|
617
|
+
function isFunctionLikeNode(node) {
|
|
618
|
+
return (isFunctionDeclaration(node) ||
|
|
619
|
+
isMethodDeclaration(node) ||
|
|
620
|
+
isConstructorDeclaration(node) ||
|
|
621
|
+
isGetAccessorDeclaration(node) ||
|
|
622
|
+
isSetAccessorDeclaration(node) ||
|
|
623
|
+
isArrowFunction(node) ||
|
|
624
|
+
isFunctionExpression(node));
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* The innermost function-like enclosing a node, by parent climb. The
|
|
628
|
+
* detection walk never descends into extracted function-likes, so for
|
|
629
|
+
* any visited node this is the record root or an anonymous function
|
|
630
|
+
* entered during the walk — never a nested record's own scope (the
|
|
631
|
+
* same invariant `ancestorFunctionLikes` relies on, innermost-first
|
|
632
|
+
* here because the yield rule wants the immediately enclosing
|
|
633
|
+
* function only).
|
|
634
|
+
*
|
|
635
|
+
* @param node The node to climb from (exclusive).
|
|
636
|
+
* @returns The innermost enclosing function-like, or `undefined` at
|
|
637
|
+
* module level.
|
|
638
|
+
*/
|
|
639
|
+
function enclosingFunctionLike(node) {
|
|
640
|
+
for (let current = node.parent; current !== undefined; current = current.parent) {
|
|
641
|
+
if (isFunctionLikeNode(current)) {
|
|
642
|
+
return current;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return undefined;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Whether a function-like is a synchronous generator (`function*`,
|
|
649
|
+
* method-with-asterisk): carries the asterisk token and no `async`
|
|
650
|
+
* modifier. Async generators yield `AsyncGeneratorYield` (the P2 arm
|
|
651
|
+
* below) and plain functions carry no asterisk.
|
|
652
|
+
*
|
|
653
|
+
* @param node The function-like, when one encloses the yield.
|
|
654
|
+
* @returns Whether the function is a synchronous generator.
|
|
655
|
+
*/
|
|
656
|
+
function isSyncGenerator(node) {
|
|
657
|
+
return (node !== undefined &&
|
|
658
|
+
node.asteriskToken !== undefined &&
|
|
659
|
+
(node.modifierFlags & ModifierFlags.Async) === 0);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Whether a function-like is an async generator (`async function*`,
|
|
663
|
+
* async-generator methods): carries the asterisk token and the `async`
|
|
664
|
+
* modifier — the `AsyncGeneratorYield` arm of the yield rule (the
|
|
665
|
+
* disposition table's P2 row; arrows can never qualify, they carry no
|
|
666
|
+
* asterisk).
|
|
667
|
+
*
|
|
668
|
+
* @param node The function-like, when one encloses the yield.
|
|
669
|
+
* @returns Whether the function is an async generator.
|
|
670
|
+
*/
|
|
671
|
+
function isAsyncGenerator(node) {
|
|
672
|
+
return (node !== undefined &&
|
|
673
|
+
node.asteriskToken !== undefined &&
|
|
674
|
+
(node.modifierFlags & ModifierFlags.Async) !== 0);
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* The function-likes lexically enclosing (and including) a node,
|
|
678
|
+
* outermost-first. Each corresponds 1:1 to a function frame of the
|
|
679
|
+
* record's scope chain — the correspondence `buildInitialScopes` zips
|
|
680
|
+
* to recover each frame's parameter names from the ancestor node.
|
|
681
|
+
*
|
|
682
|
+
* @param node The node to climb from (inclusive).
|
|
683
|
+
* @returns The enclosing function-likes, outermost-first.
|
|
684
|
+
*/
|
|
685
|
+
function ancestorFunctionLikes(node) {
|
|
686
|
+
const ancestors = [];
|
|
687
|
+
for (let current = node; current !== undefined; current = current.parent) {
|
|
688
|
+
if (isFunctionLikeNode(current)) {
|
|
689
|
+
ancestors.push(current);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
ancestors.reverse();
|
|
693
|
+
return ancestors;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Collects the parameter binding names of a function-like's parameter
|
|
697
|
+
* list, recursing through destructuring patterns so every bound spelling
|
|
698
|
+
* resolves — through the same `collectBindingNames` `extract.ts` uses
|
|
699
|
+
* for its scope frames, so the two walks never disagree on what a
|
|
700
|
+
* parameter binds.
|
|
701
|
+
*
|
|
702
|
+
* @param parameters The parameter list.
|
|
703
|
+
* @returns The bound parameter names.
|
|
704
|
+
*/
|
|
705
|
+
function paramNames(parameters) {
|
|
706
|
+
const names = new Set();
|
|
707
|
+
for (const parameter of parameters) {
|
|
708
|
+
collectBindingNames(parameter.name, names);
|
|
709
|
+
}
|
|
710
|
+
return names;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Whether a call is `Promise.reject(...)` (FR-012's third rejection
|
|
714
|
+
* form): a property access `Promise.reject` as the whole callee.
|
|
715
|
+
*
|
|
716
|
+
* @param node The call expression.
|
|
717
|
+
* @returns Whether the callee is `Promise.reject`.
|
|
718
|
+
*/
|
|
719
|
+
function isPromiseRejectCall(node) {
|
|
720
|
+
const callee = node.expression;
|
|
721
|
+
return (isPropertyAccessExpression(callee) &&
|
|
722
|
+
callee.name.text === "reject" &&
|
|
723
|
+
isIdentifier(callee.expression) &&
|
|
724
|
+
callee.expression.text === "Promise");
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Whether a call is `process.exit(...)` (FR-012's terminal form): a
|
|
728
|
+
* property access `process.exit` as the whole callee. Indirections
|
|
729
|
+
* (`const die = process.exit; die(1)`) and renamed imports are outside
|
|
730
|
+
* the strict tier's name-form — a documented boundary, never a guess.
|
|
731
|
+
*
|
|
732
|
+
* @param node The call expression.
|
|
733
|
+
* @returns Whether the callee is `process.exit`.
|
|
734
|
+
*/
|
|
735
|
+
function isProcessExitCall(node) {
|
|
736
|
+
const callee = node.expression;
|
|
737
|
+
return (isPropertyAccessExpression(callee) &&
|
|
738
|
+
callee.name.text === "exit" &&
|
|
739
|
+
isIdentifier(callee.expression) &&
|
|
740
|
+
callee.expression.text === "process");
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Whether a function-like node is a scheduling callback (research
|
|
744
|
+
* R6.5): an argument of the imported `extract.ts` scheduling predicate.
|
|
745
|
+
* Everything it lexically contains executes after the enclosing function
|
|
746
|
+
* returns — the deferral boundary `DeferredReturnMutation` attributes.
|
|
747
|
+
*
|
|
748
|
+
* @param node The function-like node.
|
|
749
|
+
* @returns Whether the node is an argument of a scheduling call.
|
|
750
|
+
*/
|
|
751
|
+
function isSchedulingCallbackArgument(node) {
|
|
752
|
+
const parent = node.parent;
|
|
753
|
+
return (isCallExpression(parent) &&
|
|
754
|
+
parent.arguments.some((argument) => argument === node) &&
|
|
755
|
+
isSchedulingCall(parent));
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Whether a call is one of FR-016's five spawn forms: the three bare
|
|
759
|
+
* timer globals (`extract.ts`'s exported `TIMER_GLOBAL_NAMES` — the one
|
|
760
|
+
* shared set) and Node's `process.nextTick` (with `new Promise(...)`
|
|
761
|
+
* matched at the `NewExpression` dispatch). Promise continuations and
|
|
762
|
+
* event registration schedule callbacks but spawn nothing — they are
|
|
763
|
+
* deliberately absent from the spawn set.
|
|
764
|
+
*
|
|
765
|
+
* @param node The call expression.
|
|
766
|
+
* @returns Whether the call spawns an asynchronous task.
|
|
767
|
+
*/
|
|
768
|
+
function isSpawnCall(node) {
|
|
769
|
+
const callee = node.expression;
|
|
770
|
+
if (isIdentifier(callee)) {
|
|
771
|
+
return TIMER_GLOBAL_NAMES.has(callee.text);
|
|
772
|
+
}
|
|
773
|
+
return (isPropertyAccessExpression(callee) &&
|
|
774
|
+
callee.name.text === "nextTick" &&
|
|
775
|
+
isIdentifier(callee.expression) &&
|
|
776
|
+
callee.expression.text === "process");
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Whether a `new` expression is FR-016's fifth spawn form — `new
|
|
780
|
+
* Promise(...)` over the bare global identifier. Every other
|
|
781
|
+
* construction is inert here (constructor calls as a whole are T018's
|
|
782
|
+
* ambiguity surface, never fabricated by the P1 rules).
|
|
783
|
+
*
|
|
784
|
+
* @param node The new expression.
|
|
785
|
+
* @returns Whether the construction spawns a task.
|
|
786
|
+
*/
|
|
787
|
+
function isPromiseConstruction(node) {
|
|
788
|
+
return isIdentifier(node.expression) && node.expression.text === "Promise";
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Whether an expression is spelled `process.stdout` or
|
|
792
|
+
* `process.stderr` — `WriterOutput`'s documented exclusion: the stdio
|
|
793
|
+
* streams belong to StdoutWrite/StderrWrite (the stdio families below),
|
|
794
|
+
* never WriterOutput (research R6.4).
|
|
795
|
+
*
|
|
796
|
+
* @param receiver The expression to test.
|
|
797
|
+
* @returns Whether the expression is a process stdio stream.
|
|
798
|
+
*/
|
|
799
|
+
function isProcessStdioReceiver(receiver) {
|
|
800
|
+
return (isPropertyAccessExpression(receiver) &&
|
|
801
|
+
(receiver.name.text === "stdout" || receiver.name.text === "stderr") &&
|
|
802
|
+
isIdentifier(receiver.expression) &&
|
|
803
|
+
receiver.expression.text === "process");
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Whether a `new` expression is `new WebAssembly.Instance(...)` — one of
|
|
807
|
+
* `CgoCall`'s two WebAssembly forms (the disposition table: the closest
|
|
808
|
+
* foreign-function surface).
|
|
809
|
+
*
|
|
810
|
+
* @param node The new expression.
|
|
811
|
+
* @returns Whether the construction instantiates WebAssembly.
|
|
812
|
+
*/
|
|
813
|
+
function isWebAssemblyInstanceConstruction(node) {
|
|
814
|
+
return (isPropertyAccessExpression(node.expression) &&
|
|
815
|
+
node.expression.name.text === "Instance" &&
|
|
816
|
+
isIdentifier(node.expression.expression) &&
|
|
817
|
+
node.expression.expression.text === "WebAssembly");
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Whether a property-form callee is spelled `WebAssembly.instantiate` —
|
|
821
|
+
* `CgoCall`'s call form (the disposition table).
|
|
822
|
+
*
|
|
823
|
+
* @param callee The property-access callee.
|
|
824
|
+
* @returns Whether the callee instantiates WebAssembly.
|
|
825
|
+
*/
|
|
826
|
+
function isWebAssemblyInstantiateCallee(callee) {
|
|
827
|
+
return (callee.name.text === "instantiate" &&
|
|
828
|
+
isIdentifier(callee.expression) &&
|
|
829
|
+
callee.expression.text === "WebAssembly");
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Whether a dot-property write target is spelled `process.env.X` — the
|
|
833
|
+
* `EnvVarMutation` form (the disposition table). Only the dot form is
|
|
834
|
+
* pinned; `process.env[k]` element writes are a documented boundary.
|
|
835
|
+
*
|
|
836
|
+
* @param target The written property-access target.
|
|
837
|
+
* @returns Whether the target path runs through `process.env`.
|
|
838
|
+
*/
|
|
839
|
+
function isProcessEnvTarget(target) {
|
|
840
|
+
const base = target.expression;
|
|
841
|
+
return (isPropertyAccessExpression(base) &&
|
|
842
|
+
base.name.text === "env" &&
|
|
843
|
+
isIdentifier(base.expression) &&
|
|
844
|
+
base.expression.text === "process");
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Type guard for the decoratable member union.
|
|
848
|
+
*
|
|
849
|
+
* @param node The node to test.
|
|
850
|
+
* @returns Whether the node is a method, property, or accessor
|
|
851
|
+
* declaration.
|
|
852
|
+
*/
|
|
853
|
+
function isDecoratableMember(node) {
|
|
854
|
+
return (isMethodDeclaration(node) ||
|
|
855
|
+
isPropertyDeclaration(node) ||
|
|
856
|
+
isGetAccessorDeclaration(node) ||
|
|
857
|
+
isSetAccessorDeclaration(node));
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Whether a checker type is array-like (FR-015's `SliceMutation` arm):
|
|
861
|
+
* tuple types and references to `Array` — plain and `readonly` element
|
|
862
|
+
* types alike, both are Array references — with unions recursing into
|
|
863
|
+
* their constituents. The type authority is the checker (Constitution
|
|
864
|
+
* Principle III): this is a shape test on its answer, never a
|
|
865
|
+
* re-derivation of TypeScript semantics.
|
|
866
|
+
*
|
|
867
|
+
* @param type The checker type, when one is available.
|
|
868
|
+
* @returns Whether the type is array-like.
|
|
869
|
+
*/
|
|
870
|
+
function isArrayLikeType(type) {
|
|
871
|
+
if (type === undefined) {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
if (type.isTupleType()) {
|
|
875
|
+
return true;
|
|
876
|
+
}
|
|
877
|
+
if (type.isTypeReference()) {
|
|
878
|
+
return type.getTarget().getSymbol()?.name === "Array";
|
|
879
|
+
}
|
|
880
|
+
if (type.isUnionType()) {
|
|
881
|
+
return type.getTypes().some((constituent) => isArrayLikeType(constituent));
|
|
882
|
+
}
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* The declared name of a checker type's symbol (`Map`, `WeakSet`, a
|
|
887
|
+
* project class), when it has one — the typed half of the receiver
|
|
888
|
+
* rules. `undefined` for intrinsics, literals, and unresolved shapes,
|
|
889
|
+
* which fall back to the name-form rules.
|
|
890
|
+
*
|
|
891
|
+
* @param type The checker type, when one is available.
|
|
892
|
+
* @returns The type symbol's name, or `undefined`.
|
|
893
|
+
*/
|
|
894
|
+
function typeSymbolName(type) {
|
|
895
|
+
return type?.getSymbol()?.name;
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* The R6.6 target of a `throw`: the constructor name for
|
|
899
|
+
* `throw new X(...)` — the identifier or property-access spelling as
|
|
900
|
+
* written — and omitted for every other thrown expression.
|
|
901
|
+
*
|
|
902
|
+
* @param expression The thrown expression, if any.
|
|
903
|
+
* @param sourceFile The declaring source, for the text slice.
|
|
904
|
+
* @returns The constructor spelling, or `undefined`.
|
|
905
|
+
*/
|
|
906
|
+
function thrownConstructorName(expression, sourceFile) {
|
|
907
|
+
if (expression !== undefined &&
|
|
908
|
+
isNewExpression(expression) &&
|
|
909
|
+
(isIdentifier(expression.expression) ||
|
|
910
|
+
isPropertyAccessExpression(expression.expression))) {
|
|
911
|
+
return expression.expression.getText(sourceFile);
|
|
912
|
+
}
|
|
913
|
+
return undefined;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Whether an operator-token kind is one of the sixteen assignment
|
|
917
|
+
* operators (simple, compound, and logical-assignment) — the
|
|
918
|
+
* `FirstAssignment`..`LastAssignment` kind range, which excludes every
|
|
919
|
+
* comparison operator.
|
|
920
|
+
*
|
|
921
|
+
* @param kind The operator token's syntax kind.
|
|
922
|
+
* @returns Whether the operator writes.
|
|
923
|
+
*/
|
|
924
|
+
function isAssignmentOperator(kind) {
|
|
925
|
+
return kind >= SyntaxKind.FirstAssignment && kind <= SyntaxKind.LastAssignment;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* One record's P0/P1 detection walk (FR-009 attribution; research R6):
|
|
929
|
+
* a fresh pre-order visitor over the record's own function-like node,
|
|
930
|
+
* with a scope stack seeded from the record's chain and grown as the
|
|
931
|
+
* walk descends into anonymous function-likes. Effects accumulate into
|
|
932
|
+
* {@link pending} — unsorted; `detectEffects` applies FR-020's order.
|
|
933
|
+
*/
|
|
934
|
+
class RecordWalker {
|
|
935
|
+
pending = [];
|
|
936
|
+
record;
|
|
937
|
+
sourceFile;
|
|
938
|
+
scopes;
|
|
939
|
+
/** The live checker in program mode; `undefined` in parse-only. */
|
|
940
|
+
checker;
|
|
941
|
+
/**
|
|
942
|
+
* The declaring file's module-scope import bindings — the fs-receiver
|
|
943
|
+
* and MonkeyPatch namespace rules resolve receiver identifiers against
|
|
944
|
+
* them (research R6.4), with the scope stack settling shadows.
|
|
945
|
+
*/
|
|
946
|
+
imports;
|
|
947
|
+
/**
|
|
948
|
+
* Scope-stack indices of the deferred-callback frames the walk is
|
|
949
|
+
* inside (research R6.5): a write to a function-scope binding
|
|
950
|
+
* declared outside one of these frames, from inside it, is a
|
|
951
|
+
* `DeferredReturnMutation`. Seeded from a `deferred` record's own
|
|
952
|
+
* ancestors; grown when the walk enters a scheduling callback
|
|
953
|
+
* argument; popped when it leaves.
|
|
954
|
+
*/
|
|
955
|
+
boundaries;
|
|
956
|
+
constructor(record, checker) {
|
|
957
|
+
this.record = record;
|
|
958
|
+
this.sourceFile = record.node.getSourceFile();
|
|
959
|
+
this.checker = checker;
|
|
960
|
+
this.imports = scanImportBindings(this.sourceFile);
|
|
961
|
+
this.scopes = buildInitialScopes(record);
|
|
962
|
+
this.boundaries = initialDeferredBoundaries(record);
|
|
963
|
+
}
|
|
964
|
+
/** Walks the record's own function-like node. */
|
|
965
|
+
walk() {
|
|
966
|
+
const root = asFunctionLike(this.record.node);
|
|
967
|
+
// The record's own expression-bodied arrow (FR-011): the body IS
|
|
968
|
+
// the returned value, so the implicit return reports at the
|
|
969
|
+
// expression's start.
|
|
970
|
+
if (isArrowFunction(root) && !isBlock(root.body)) {
|
|
971
|
+
this.emit("ReturnValue", "arrow expression body returns a value", root.body);
|
|
972
|
+
}
|
|
973
|
+
this.walkChildren(root);
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Emits one effect for this record at `node`'s location, through the
|
|
977
|
+
* canonical-type resolver.
|
|
978
|
+
*/
|
|
979
|
+
emit(type, description, node, target) {
|
|
980
|
+
this.pending.push(constructEffect(type, description, this.record.relPath, node, this.sourceFile, target));
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Emits one FR-017 ambiguity effect: `CallbackInvocation` carrying
|
|
984
|
+
* `detail: {"confidence": "ambiguous"}`, the carrier the wire contract
|
|
985
|
+
* reserves for detection that cannot resolve the invoked function.
|
|
986
|
+
* The description names the unresolved construct (research R7's
|
|
987
|
+
* decision text); it is a deterministic template like every other.
|
|
988
|
+
*/
|
|
989
|
+
emitAmbiguous(description, node) {
|
|
990
|
+
this.pending.push(constructEffect("CallbackInvocation", description, this.record.relPath, node, this.sourceFile, undefined, AMBIGUOUS_DETAIL));
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* The dispatch: nested extracted function-likes stop the walk; other
|
|
994
|
+
* function-likes are entered anonymously with their own scope; each
|
|
995
|
+
* pinned construct family emits and continues the descent — constructs
|
|
996
|
+
* nest (`return promise.then(...)`, writes inside callbacks), so no
|
|
997
|
+
* match short-circuits the traversal.
|
|
998
|
+
*/
|
|
999
|
+
visit(node) {
|
|
1000
|
+
if (isFunctionDeclaration(node)) {
|
|
1001
|
+
// The declaration binds its name into the enclosing scope (a
|
|
1002
|
+
// body-less overload binds too), then a body-bearing declaration
|
|
1003
|
+
// stops the walk — its record owns that body.
|
|
1004
|
+
if (node.name !== undefined) {
|
|
1005
|
+
this.declareName(node.name.text);
|
|
1006
|
+
}
|
|
1007
|
+
if (node.body === undefined) {
|
|
1008
|
+
this.walkChildren(node);
|
|
1009
|
+
}
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
if (isFunctionLikeNode(node)) {
|
|
1013
|
+
if (isExtractedFunctionLike(node)) {
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
this.visitAnonymousFunctionLike(node);
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
if (isReturnStatement(node)) {
|
|
1020
|
+
// FR-011: only a value-carrying return matches; bare `return;`
|
|
1021
|
+
// reports nothing.
|
|
1022
|
+
if (node.expression !== undefined) {
|
|
1023
|
+
this.emit("ReturnValue", "return statement returns a value", node);
|
|
1024
|
+
}
|
|
1025
|
+
this.walkChildren(node);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (isThrowStatement(node)) {
|
|
1029
|
+
this.visitThrow(node);
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
if (isCallExpression(node)) {
|
|
1033
|
+
this.visitCall(node);
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
if (isNewExpression(node)) {
|
|
1037
|
+
this.visitNew(node);
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
if (isYieldExpression(node)) {
|
|
1041
|
+
this.visitYield(node);
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1044
|
+
if (isBinaryExpression(node)) {
|
|
1045
|
+
if (isAssignmentOperator(node.operatorToken.kind)) {
|
|
1046
|
+
this.emitWrite(node, node.left);
|
|
1047
|
+
}
|
|
1048
|
+
this.walkChildren(node);
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
if (isPostfixUnaryExpression(node) || isPrefixUnaryExpression(node)) {
|
|
1052
|
+
if (node.operator === SyntaxKind.PlusPlusToken ||
|
|
1053
|
+
node.operator === SyntaxKind.MinusMinusToken) {
|
|
1054
|
+
this.emitWrite(node, node.operand);
|
|
1055
|
+
}
|
|
1056
|
+
this.walkChildren(node);
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (isDeleteExpression(node)) {
|
|
1060
|
+
this.emitWrite(node, node.expression);
|
|
1061
|
+
this.walkChildren(node);
|
|
1062
|
+
return;
|
|
1063
|
+
}
|
|
1064
|
+
if (isVariableStatement(node)) {
|
|
1065
|
+
this.visitVariableStatement(node);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
if (isClassDeclaration(node) || isClassExpression(node)) {
|
|
1069
|
+
this.visitClassLike(node);
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
this.declareBinding(node);
|
|
1073
|
+
this.walkChildren(node);
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* FR-012's dual emission for `throw`: `ErrorReturn` + `ErrorSignal`
|
|
1077
|
+
* at the throw statement, both targeting the constructor name for
|
|
1078
|
+
* `throw new X(...)` (omitted otherwise). Async functions need no
|
|
1079
|
+
* case of their own — every throw dual-emits identically, which is
|
|
1080
|
+
* the rejection-channel rule.
|
|
1081
|
+
*/
|
|
1082
|
+
visitThrow(node) {
|
|
1083
|
+
const thrown = thrownConstructorName(node.expression, this.sourceFile);
|
|
1084
|
+
this.emit("ErrorReturn", "throws an error to the caller", node, thrown);
|
|
1085
|
+
this.emit("ErrorSignal", "signals an error", node, thrown);
|
|
1086
|
+
this.walkChildren(node);
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* `ResourceManagement`'s declaration arm (the disposition table): a
|
|
1090
|
+
* `using`/`await using` statement — on the native compiler
|
|
1091
|
+
* (typescript 7.0.2) both parse as a VariableStatement whose
|
|
1092
|
+
* declaration list carries `NodeFlags.Using` (the `await using` form
|
|
1093
|
+
* additionally carries `AwaitUsing`; one bit test covers both). The
|
|
1094
|
+
* declared names bind normally through the declaration-list descent,
|
|
1095
|
+
* exactly as a `const` statement's do.
|
|
1096
|
+
*/
|
|
1097
|
+
visitVariableStatement(node) {
|
|
1098
|
+
if ((node.declarationList.flags & NodeFlags.Using) !== 0) {
|
|
1099
|
+
this.emit("ResourceManagement", "declares a using-scoped resource", node);
|
|
1100
|
+
}
|
|
1101
|
+
this.walkChildren(node);
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* `DescriptorEffect` (the disposition table): every decorator on a
|
|
1105
|
+
* class-like inside a walked body — the class's own and its
|
|
1106
|
+
* decoratable members' — collected at the class level because the
|
|
1107
|
+
* walk stops at extracted method bodies before reaching their
|
|
1108
|
+
* decorator nodes, and a decorator executes at class-definition time
|
|
1109
|
+
* (inside the enclosing record), never in the decorated method's own
|
|
1110
|
+
* frame. A class declaration still binds its name into the enclosing
|
|
1111
|
+
* scope (`declareBinding`'s half of this branch); class expressions
|
|
1112
|
+
* bind their names only inside themselves. Decorator factory
|
|
1113
|
+
* arguments on member decorators are not descended (the member's
|
|
1114
|
+
* extracted-body boundary) — a documented boundary.
|
|
1115
|
+
*/
|
|
1116
|
+
visitClassLike(node) {
|
|
1117
|
+
for (const modifier of node.modifiers ?? []) {
|
|
1118
|
+
if (isDecorator(modifier)) {
|
|
1119
|
+
this.emit("DescriptorEffect", "applies a decorator to a class", modifier);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
for (const member of node.members) {
|
|
1123
|
+
if (!isDecoratableMember(member)) {
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
for (const modifier of member.modifiers ?? []) {
|
|
1127
|
+
if (isDecorator(modifier)) {
|
|
1128
|
+
this.emit("DescriptorEffect", "applies a decorator to a class member", modifier);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
this.declareBinding(node);
|
|
1133
|
+
this.walkChildren(node);
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* The named-call forms: `Promise.reject(...)` dual-emits
|
|
1137
|
+
* `ErrorReturn` + `ErrorSignal` (the rejection channel), and
|
|
1138
|
+
* `process.exit(...)` emits `ErrorSignal` + `ProcessExit` — never
|
|
1139
|
+
* `ErrorReturn`, the terminal carve-out. Everything else falls to
|
|
1140
|
+
* the P1 call families; unmatched calls — including those whose
|
|
1141
|
+
* callee or arguments nest detectable constructs — simply descend.
|
|
1142
|
+
*/
|
|
1143
|
+
visitCall(node) {
|
|
1144
|
+
if (isPromiseRejectCall(node)) {
|
|
1145
|
+
this.emit("ErrorReturn", "returns a rejected promise", node);
|
|
1146
|
+
this.emit("ErrorSignal", "signals a rejected promise", node);
|
|
1147
|
+
}
|
|
1148
|
+
else if (isProcessExitCall(node)) {
|
|
1149
|
+
this.emit("ErrorSignal", "signals an error through process exit", node);
|
|
1150
|
+
this.emit("ProcessExit", "terminates the process", node);
|
|
1151
|
+
}
|
|
1152
|
+
else {
|
|
1153
|
+
this.visitP1Call(node);
|
|
1154
|
+
}
|
|
1155
|
+
this.walkChildren(node);
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* The bare-callee call families (FR-015/FR-016 plus the P2/P3 rows
|
|
1159
|
+
* that key on the callee alone): the spawn forms — emitted as the
|
|
1160
|
+
* `AsyncTaskSpawn` alias, which the canonical-type resolver maps to
|
|
1161
|
+
* `GoroutineSpawn` before the effect is constructed (FR-018) — the
|
|
1162
|
+
* dynamic `import(...)` and `require(...)` module side effects (the
|
|
1163
|
+
* callee of the former is the `import` keyword token itself), then
|
|
1164
|
+
* the bare-identifier channel/stream globals (`postMessage`, the
|
|
1165
|
+
* Worker/window global form; `pipeline`, the node:stream/promises
|
|
1166
|
+
* form). The unclaimed callee forms then fall to FR-017's ambiguity
|
|
1167
|
+
* remainder: computed-member calls (`obj[key]()`, a syntactic form
|
|
1168
|
+
* reported in both modes), `eval(...)`, and — program mode only —
|
|
1169
|
+
* bare callees the checker resolves to no declaration (unknown
|
|
1170
|
+
* globals). Everything else reports nothing: research R7's carve-out
|
|
1171
|
+
* treats lexically-bound bare callees (parameters, locally-defined
|
|
1172
|
+
* functions, imported symbols) as resolved, their effects belonging
|
|
1173
|
+
* to their own records, and the parse-only mode boundary leaves
|
|
1174
|
+
* unresolvable bare callees unreported rather than flooding the mode.
|
|
1175
|
+
*/
|
|
1176
|
+
visitP1Call(node) {
|
|
1177
|
+
if (isSpawnCall(node)) {
|
|
1178
|
+
this.emit("AsyncTaskSpawn", "spawns an asynchronous task", node);
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
if (node.expression.kind === SyntaxKind.ImportKeyword) {
|
|
1182
|
+
this.emit("ImportSideEffect", "dynamically imports a module", node);
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
const callee = node.expression;
|
|
1186
|
+
if (isPropertyAccessExpression(callee)) {
|
|
1187
|
+
this.visitMethodCall(node, callee);
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
if (isElementAccessExpression(callee)) {
|
|
1191
|
+
// A syntactic ambiguity form (research R7): no name-form family
|
|
1192
|
+
// can match a computed callee — there is no property name to key
|
|
1193
|
+
// on — so the dynamic dispatch itself is the reportable fact.
|
|
1194
|
+
this.emitAmbiguous("calls through a computed member", node);
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1197
|
+
if (isIdentifier(callee)) {
|
|
1198
|
+
if (callee.text === "postMessage") {
|
|
1199
|
+
this.emit("ChannelSend", "sends a message to a channel", node);
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
if (callee.text === "pipeline") {
|
|
1203
|
+
this.emit("StreamOutput", "pipes a stream", node);
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
1206
|
+
if (callee.text === "require") {
|
|
1207
|
+
this.emit("ImportSideEffect", "requires a module", node);
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
// `eval` is one of R7's always-forms: it resolves to a lib
|
|
1211
|
+
// declaration in program mode, so the syntactic check must
|
|
1212
|
+
// precede the symbol-resolution rule.
|
|
1213
|
+
if (callee.text === "eval") {
|
|
1214
|
+
this.emitAmbiguous("evaluates a string dynamically through eval", node);
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
if (this.isUnresolvedCallee(callee)) {
|
|
1218
|
+
this.emitAmbiguous(`calls the unresolved global ${callee.text}`, node);
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* Whether a bare-identifier callee resolves to no declaration — the
|
|
1224
|
+
* program-mode half of FR-017's type-based ambiguity (unknown
|
|
1225
|
+
* globals; research R7). Parse-only has no symbol resolution, and R7
|
|
1226
|
+
* documents that boundary explicitly: unresolvable bare callees stay
|
|
1227
|
+
* unreported there instead of turning every non-lexical helper call
|
|
1228
|
+
* into noise. A callee the checker resolves — a parameter, a
|
|
1229
|
+
* locally-defined function, an import, a known global — keeps the
|
|
1230
|
+
* zero-false-positive carve-out.
|
|
1231
|
+
*/
|
|
1232
|
+
isUnresolvedCallee(callee) {
|
|
1233
|
+
if (this.checker === undefined) {
|
|
1234
|
+
return false;
|
|
1235
|
+
}
|
|
1236
|
+
return this.checker.getSymbolAtLocation(callee) === undefined;
|
|
1237
|
+
}
|
|
1238
|
+
/**
|
|
1239
|
+
* One property-form call through the name-form rule families in
|
|
1240
|
+
* research R6.4's precedence order — a ladder of family helpers,
|
|
1241
|
+
* each returning whether it claimed the call: the stdio streams
|
|
1242
|
+
* first (console, process.std*), then the receiver-specific
|
|
1243
|
+
* communication families (HTTP responses, channels), the fs family,
|
|
1244
|
+
* and the metaprogramming/Atomics receivers whose method names the
|
|
1245
|
+
* generic rules below would otherwise steal (`Reflect.set` vs the
|
|
1246
|
+
* membership `set`, `Atomics.add` vs the membership `add`); then the
|
|
1247
|
+
* stream, disposal, logging, and writer name-forms, the FR-015
|
|
1248
|
+
* param-write split, the membership mutators, and the P2/P4
|
|
1249
|
+
* database, cancellation, clock, and registration name-forms. The
|
|
1250
|
+
* first match wins; every family matches on closed method-name lists
|
|
1251
|
+
* or pinned receiver spellings, so an unmatched call reports nothing
|
|
1252
|
+
* until `visitReceiverAmbiguity`'s remainder.
|
|
1253
|
+
*/
|
|
1254
|
+
visitMethodCall(node, callee) {
|
|
1255
|
+
const method = callee.name.text;
|
|
1256
|
+
const receiver = callee.expression;
|
|
1257
|
+
if (this.visitStdioCall(node, method, callee)) {
|
|
1258
|
+
return;
|
|
1259
|
+
}
|
|
1260
|
+
if (this.visitCommunicationCall(node, method, receiver)) {
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
if (this.visitFileSystemCall(node, method, receiver)) {
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
if (this.visitMetaprogrammingCall(node, callee)) {
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
if (this.visitAtomicsCall(node, receiver)) {
|
|
1270
|
+
return;
|
|
1271
|
+
}
|
|
1272
|
+
if (this.visitStreamCall(node, method)) {
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1275
|
+
if (this.visitDisposalCall(node, method)) {
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
if (this.visitWriterCall(node, method, receiver)) {
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
if (this.visitSliceMutationCall(node, method, receiver)) {
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
if (this.visitMembershipCall(node, method, receiver)) {
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
if (this.visitDatabaseCall(node, method)) {
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
if (this.visitContextCancellationCall(node, method, callee)) {
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
if (this.visitTimeDependencyCall(node, method, callee)) {
|
|
1294
|
+
return;
|
|
1295
|
+
}
|
|
1296
|
+
if (this.visitRegistrationCall(node, method, callee)) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
// FR-017's receiver ambiguity — the final remainder, after every
|
|
1300
|
+
// name-form family (a claimed call is never ambiguous; research
|
|
1301
|
+
// R7's precedence carve-out).
|
|
1302
|
+
this.visitReceiverAmbiguity(node, receiver);
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* FR-017's receiver-based ambiguity remainder for property-form calls
|
|
1306
|
+
* (research R7), replacing the earlier parameter-property callback
|
|
1307
|
+
* arm: the pinned tests contract `resolve()` inside a Promise
|
|
1308
|
+
* executor and `parts.join("-")` to zero effects, so resolved
|
|
1309
|
+
* callback invocations are never re-reported — their effects belong
|
|
1310
|
+
* to their own records.
|
|
1311
|
+
*
|
|
1312
|
+
* Parse-only reports the one syntactic form: a receiver the lexical
|
|
1313
|
+
* scope stack cannot resolve. Every lexically-resolvable receiver —
|
|
1314
|
+
* parameter, local, import, module binding — is treated as resolved
|
|
1315
|
+
* (R7's documented parse-only boundary).
|
|
1316
|
+
*
|
|
1317
|
+
* Program mode reports the type-based form instead, which subsumes
|
|
1318
|
+
* the syntactic one: a receiver whose checker type is `any` or the
|
|
1319
|
+
* error type. An undeclared receiver types as error (which carries
|
|
1320
|
+
* the Any flag, exactly as in classic ts), while a checker-resolved
|
|
1321
|
+
* global receiver (`JSON.parse`, declared globals) keeps the
|
|
1322
|
+
* zero-false-positive carve-out.
|
|
1323
|
+
*/
|
|
1324
|
+
visitReceiverAmbiguity(node, receiver) {
|
|
1325
|
+
let base = receiver;
|
|
1326
|
+
while (isParenthesizedExpression(base)) {
|
|
1327
|
+
base = base.expression;
|
|
1328
|
+
}
|
|
1329
|
+
if (!isIdentifier(base)) {
|
|
1330
|
+
// Property chains (`this.items.push`), `this`/`super`, and every
|
|
1331
|
+
// non-identifier receiver carry no name-form ambiguity surface.
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
if (this.checker === undefined) {
|
|
1335
|
+
if (this.bindingScopeIndex(base.text) < 0) {
|
|
1336
|
+
this.emitAmbiguous(`calls a method on the unresolvable receiver ${base.text}`, node);
|
|
1337
|
+
}
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
const type = this.typeAt(base);
|
|
1341
|
+
if (type !== undefined && (type.flags & TypeFlags.Any) !== 0) {
|
|
1342
|
+
this.emitAmbiguous(`calls a method on the any-typed receiver ${base.text}`, node);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* The receiver-specific communication families (research R6.4):
|
|
1347
|
+
* `HTTPResponseWrite` on `res`/`response`-spelled or checker-typed
|
|
1348
|
+
* response receivers, `ChannelSend` on any `postMessage`, and
|
|
1349
|
+
* `ChannelClose` on port/channel/worker-spelled or MessagePort-family
|
|
1350
|
+
* receivers — pinned above the fs family and above
|
|
1351
|
+
* `ResourceManagement`'s `.close()`.
|
|
1352
|
+
*
|
|
1353
|
+
* @returns Whether a communication family claimed the call.
|
|
1354
|
+
*/
|
|
1355
|
+
visitCommunicationCall(node, method, receiver) {
|
|
1356
|
+
if (HTTP_RESPONSE_METHODS.has(method) && this.isHttpResponseReceiver(receiver)) {
|
|
1357
|
+
this.emit("HTTPResponseWrite", "writes to an HTTP response", node);
|
|
1358
|
+
return true;
|
|
1359
|
+
}
|
|
1360
|
+
if (method === "postMessage") {
|
|
1361
|
+
this.emit("ChannelSend", "sends a message to a channel", node);
|
|
1362
|
+
return true;
|
|
1363
|
+
}
|
|
1364
|
+
if (method === "close" && this.isChannelReceiver(receiver)) {
|
|
1365
|
+
this.emit("ChannelClose", "closes a channel", node);
|
|
1366
|
+
return true;
|
|
1367
|
+
}
|
|
1368
|
+
return false;
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* The `AtomicOp` row on the `Atomics` global — pinned above the
|
|
1372
|
+
* membership mutators, whose `add` the receiver would otherwise
|
|
1373
|
+
* steal (research R6.4).
|
|
1374
|
+
*
|
|
1375
|
+
* @returns Whether the call is an Atomics operation.
|
|
1376
|
+
*/
|
|
1377
|
+
visitAtomicsCall(node, receiver) {
|
|
1378
|
+
if (this.isAtomicsReceiver(receiver)) {
|
|
1379
|
+
this.emit("AtomicOp", "performs an atomic shared-memory operation", node);
|
|
1380
|
+
return true;
|
|
1381
|
+
}
|
|
1382
|
+
return false;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* The `StreamOutput` name-form: `.pipe` on any receiver (the
|
|
1386
|
+
* disposition table's streams row).
|
|
1387
|
+
*
|
|
1388
|
+
* @returns Whether the call is a pipe.
|
|
1389
|
+
*/
|
|
1390
|
+
visitStreamCall(node, method) {
|
|
1391
|
+
if (method === "pipe") {
|
|
1392
|
+
this.emit("StreamOutput", "pipes a stream", node);
|
|
1393
|
+
return true;
|
|
1394
|
+
}
|
|
1395
|
+
return false;
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* `ResourceManagement`'s disposal name-form (the disposition table):
|
|
1399
|
+
* `.close()`/`.dispose()` on resource receivers — deliberately below
|
|
1400
|
+
* `ChannelClose`'s receivers (research R6.4's pinned precedence: the
|
|
1401
|
+
* MessagePort family's `.close()` is a channel close, never a
|
|
1402
|
+
* resource release).
|
|
1403
|
+
*
|
|
1404
|
+
* @returns Whether the call is a disposal form.
|
|
1405
|
+
*/
|
|
1406
|
+
visitDisposalCall(node, method) {
|
|
1407
|
+
if (RESOURCE_DISPOSAL_METHODS.has(method)) {
|
|
1408
|
+
this.emit("ResourceManagement", "releases a resource", node);
|
|
1409
|
+
return true;
|
|
1410
|
+
}
|
|
1411
|
+
return false;
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* The output name-form families: `LogWrite` on log/logger-named or
|
|
1415
|
+
* Logger-typed receivers, then `WriterOutput`'s writer-like methods —
|
|
1416
|
+
* excluding the process stdio streams and HTTP response receivers the
|
|
1417
|
+
* earlier tiers own (research R6.4's precedence).
|
|
1418
|
+
*
|
|
1419
|
+
* @returns Whether an output family claimed the call.
|
|
1420
|
+
*/
|
|
1421
|
+
visitWriterCall(node, method, receiver) {
|
|
1422
|
+
if (LOG_WRITE_METHODS.has(method) && this.isLogReceiver(receiver)) {
|
|
1423
|
+
this.emit("LogWrite", "writes a log record", node);
|
|
1424
|
+
return true;
|
|
1425
|
+
}
|
|
1426
|
+
if (WRITER_OUTPUT_METHODS.has(method) &&
|
|
1427
|
+
!isProcessStdioReceiver(receiver) &&
|
|
1428
|
+
!this.isHttpResponseReceiver(receiver)) {
|
|
1429
|
+
this.emit("WriterOutput", "writes through a writer-like receiver", node);
|
|
1430
|
+
return true;
|
|
1431
|
+
}
|
|
1432
|
+
return false;
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* The `SliceMutation` method arm of the FR-015 param-write split: the
|
|
1436
|
+
* nine mutating array methods on a parameter receiver — array-typed
|
|
1437
|
+
* in program mode, type-blind in parse-only
|
|
1438
|
+
* (`isArrayTypedParamReceiver`). A non-array parameter's mutator
|
|
1439
|
+
* call falls through to the later tiers, exactly as its element
|
|
1440
|
+
* writes do.
|
|
1441
|
+
*
|
|
1442
|
+
* @returns Whether the call is a parameter slice mutation.
|
|
1443
|
+
*/
|
|
1444
|
+
visitSliceMutationCall(node, method, receiver) {
|
|
1445
|
+
if (ARRAY_MUTATOR_METHODS.has(method) &&
|
|
1446
|
+
this.isParamReceiver(receiver) &&
|
|
1447
|
+
this.isArrayTypedParamReceiver(receiver)) {
|
|
1448
|
+
this.emit("SliceMutation", "calls a mutating array method on a parameter", node);
|
|
1449
|
+
return true;
|
|
1450
|
+
}
|
|
1451
|
+
return false;
|
|
1452
|
+
}
|
|
1453
|
+
/**
|
|
1454
|
+
* The database name-forms (the disposition table's partial rows): the
|
|
1455
|
+
* MongoDB-style write names and the transaction names, on any
|
|
1456
|
+
* receiver — the documented false-positive surface of the partial
|
|
1457
|
+
* tier.
|
|
1458
|
+
*
|
|
1459
|
+
* @returns Whether a database name-form claimed the call.
|
|
1460
|
+
*/
|
|
1461
|
+
visitDatabaseCall(node, method) {
|
|
1462
|
+
if (DATABASE_WRITE_METHODS.has(method)) {
|
|
1463
|
+
this.emit("DatabaseWrite", "writes to a database", node);
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
if (DATABASE_TRANSACTION_METHODS.has(method)) {
|
|
1467
|
+
this.emit("DatabaseTransaction", "manages a database transaction", node);
|
|
1468
|
+
return true;
|
|
1469
|
+
}
|
|
1470
|
+
return false;
|
|
1471
|
+
}
|
|
1472
|
+
/**
|
|
1473
|
+
* The registration name-forms: `WebAssembly.instantiate` (`CgoCall`)
|
|
1474
|
+
* and the typed `.register(...)` on FinalizationRegistry receivers
|
|
1475
|
+
* (`FinalizerRegistration` — program mode only; parse-only reports
|
|
1476
|
+
* the construction form alone).
|
|
1477
|
+
*
|
|
1478
|
+
* @returns Whether a registration form claimed the call.
|
|
1479
|
+
*/
|
|
1480
|
+
visitRegistrationCall(node, method, callee) {
|
|
1481
|
+
if (isWebAssemblyInstantiateCallee(callee)) {
|
|
1482
|
+
this.emit("CgoCall", "instantiates WebAssembly", node);
|
|
1483
|
+
return true;
|
|
1484
|
+
}
|
|
1485
|
+
const receiver = callee.expression;
|
|
1486
|
+
if (method === "register" && this.isFinalizerRegistryReceiver(receiver)) {
|
|
1487
|
+
this.emit("FinalizerRegistration", "registers a finalizer", node);
|
|
1488
|
+
return true;
|
|
1489
|
+
}
|
|
1490
|
+
return false;
|
|
1491
|
+
}
|
|
1492
|
+
/**
|
|
1493
|
+
* The P3 stdio families (the disposition table): `console.log`/
|
|
1494
|
+
* `info`/`debug`/`trace` → `StdoutWrite`, `console.error`/`warn` →
|
|
1495
|
+
* `StderrWrite`, and `.write` on the process stdio streams. Matched
|
|
1496
|
+
* before WriterOutput so the writer name-form never claims them
|
|
1497
|
+
* (research R6.4's pinned precedence).
|
|
1498
|
+
*
|
|
1499
|
+
* @returns Whether a stdio family claimed the call.
|
|
1500
|
+
*/
|
|
1501
|
+
visitStdioCall(node, method, callee) {
|
|
1502
|
+
const receiver = callee.expression;
|
|
1503
|
+
if (isIdentifier(receiver) && receiver.text === "console") {
|
|
1504
|
+
if (CONSOLE_STDOUT_METHODS.has(method)) {
|
|
1505
|
+
this.emit("StdoutWrite", "writes to standard output", node);
|
|
1506
|
+
return true;
|
|
1507
|
+
}
|
|
1508
|
+
if (CONSOLE_STDERR_METHODS.has(method)) {
|
|
1509
|
+
this.emit("StderrWrite", "writes to standard error", node);
|
|
1510
|
+
return true;
|
|
1511
|
+
}
|
|
1512
|
+
return false;
|
|
1513
|
+
}
|
|
1514
|
+
if (method === "write" &&
|
|
1515
|
+
isPropertyAccessExpression(receiver) &&
|
|
1516
|
+
isProcessStdioReceiver(receiver)) {
|
|
1517
|
+
const toStdout = receiver.name.text === "stdout";
|
|
1518
|
+
this.emit(toStdout ? "StdoutWrite" : "StderrWrite", toStdout ? "writes to standard output" : "writes to standard error", node);
|
|
1519
|
+
return true;
|
|
1520
|
+
}
|
|
1521
|
+
return false;
|
|
1522
|
+
}
|
|
1523
|
+
/**
|
|
1524
|
+
* The fs families (the disposition table's FileSystemWrite/Delete/Meta
|
|
1525
|
+
* rows): the write/delete/meta name lists on fs receivers — an
|
|
1526
|
+
* identifier spelled `fs` (name-form, receiver-blind), an identifier
|
|
1527
|
+
* whose default or namespace import resolves to an fs module
|
|
1528
|
+
* specifier (shadow-checked through the scope stack), or a
|
|
1529
|
+
* `.promises` chain over either. Reads (`readFile`, `stat`, …) claim
|
|
1530
|
+
* nothing and fall through.
|
|
1531
|
+
*
|
|
1532
|
+
* @returns Whether an fs family claimed the call.
|
|
1533
|
+
*/
|
|
1534
|
+
visitFileSystemCall(node, method, receiver) {
|
|
1535
|
+
if (!this.isFileSystemReceiver(receiver)) {
|
|
1536
|
+
return false;
|
|
1537
|
+
}
|
|
1538
|
+
if (FILE_SYSTEM_WRITE_METHODS.has(method)) {
|
|
1539
|
+
this.emit("FileSystemWrite", "writes a file through the fs API", node);
|
|
1540
|
+
return true;
|
|
1541
|
+
}
|
|
1542
|
+
if (FILE_SYSTEM_DELETE_METHODS.has(method)) {
|
|
1543
|
+
this.emit("FileSystemDelete", "deletes a filesystem entry", node);
|
|
1544
|
+
return true;
|
|
1545
|
+
}
|
|
1546
|
+
if (FILE_SYSTEM_META_METHODS.has(method)) {
|
|
1547
|
+
this.emit("FileSystemMeta", "changes filesystem metadata", node);
|
|
1548
|
+
return true;
|
|
1549
|
+
}
|
|
1550
|
+
return false;
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
* Whether a method receiver is an fs binding: the `fs` spelling (any
|
|
1554
|
+
* shape — the canonical name-form), an import-tracked fs binding
|
|
1555
|
+
* (default or namespace import from an fs module specifier, excluded
|
|
1556
|
+
* when a function-scope frame shadows the name), or a `.promises`
|
|
1557
|
+
* property chain over either form.
|
|
1558
|
+
*/
|
|
1559
|
+
isFileSystemReceiver(receiver) {
|
|
1560
|
+
let base = receiver;
|
|
1561
|
+
while (isParenthesizedExpression(base)) {
|
|
1562
|
+
base = base.expression;
|
|
1563
|
+
}
|
|
1564
|
+
if (isIdentifier(base)) {
|
|
1565
|
+
return base.text === "fs" || this.isImportedFsBinding(base.text);
|
|
1566
|
+
}
|
|
1567
|
+
if (isPropertyAccessExpression(base) && base.name.text === "promises") {
|
|
1568
|
+
const root = base.expression;
|
|
1569
|
+
return (isIdentifier(root) &&
|
|
1570
|
+
(root.text === "fs" || this.isImportedFsBinding(root.text)));
|
|
1571
|
+
}
|
|
1572
|
+
return false;
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* Whether a name is an import-tracked fs binding: bound at module
|
|
1576
|
+
* scope by a default or namespace import from an fs module specifier,
|
|
1577
|
+
* and not shadowed by any function-scope frame the scope stack sees.
|
|
1578
|
+
*/
|
|
1579
|
+
isImportedFsBinding(name) {
|
|
1580
|
+
const specifier = this.imports.defaultSpecifiers.get(name) ??
|
|
1581
|
+
this.imports.namespaceSpecifiers.get(name);
|
|
1582
|
+
return (specifier !== undefined &&
|
|
1583
|
+
FS_MODULE_SPECIFIERS.has(specifier) &&
|
|
1584
|
+
this.bindingScopeIndex(name) <= 0);
|
|
1585
|
+
}
|
|
1586
|
+
/**
|
|
1587
|
+
* The metaprogramming forms (the disposition table):
|
|
1588
|
+
* `Reflect.set`/`Reflect.deleteProperty`/`Object.defineProperty`,
|
|
1589
|
+
* matched before the membership mutators so `Reflect.set` is never a
|
|
1590
|
+
* `ContainerMutation` (research R6.4's receiver-specific-first rule).
|
|
1591
|
+
*
|
|
1592
|
+
* @returns Whether a metaprogramming form claimed the call.
|
|
1593
|
+
*/
|
|
1594
|
+
visitMetaprogrammingCall(node, callee) {
|
|
1595
|
+
const receiver = callee.expression;
|
|
1596
|
+
if (!isIdentifier(receiver)) {
|
|
1597
|
+
return false;
|
|
1598
|
+
}
|
|
1599
|
+
const method = callee.name.text;
|
|
1600
|
+
if ((receiver.text === "Reflect" &&
|
|
1601
|
+
(method === "set" || method === "deleteProperty")) ||
|
|
1602
|
+
(receiver.text === "Object" && method === "defineProperty")) {
|
|
1603
|
+
this.emit("MetaprogrammingMutation", "mutates an object through metaprogramming", node);
|
|
1604
|
+
return true;
|
|
1605
|
+
}
|
|
1606
|
+
return false;
|
|
1607
|
+
}
|
|
1608
|
+
/** Whether a receiver is the `Atomics` global (the `AtomicOp` row). */
|
|
1609
|
+
isAtomicsReceiver(receiver) {
|
|
1610
|
+
return isIdentifier(receiver) && receiver.text === "Atomics";
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Whether a receiver is logger-like (the `LogWrite` row): the
|
|
1614
|
+
* name-form spellings `log`/`logger` (both modes), or — with the
|
|
1615
|
+
* checker — a type declared as a `Logger`.
|
|
1616
|
+
*/
|
|
1617
|
+
isLogReceiver(receiver) {
|
|
1618
|
+
if (isIdentifier(receiver) && LOG_RECEIVER_NAMES.has(receiver.text)) {
|
|
1619
|
+
return true;
|
|
1620
|
+
}
|
|
1621
|
+
const name = typeSymbolName(this.typeAt(receiver));
|
|
1622
|
+
return name !== undefined && LOG_TYPE_NAMES.has(name);
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* The cancellation forms (the `ContextCancellation` row): `.abort()`
|
|
1626
|
+
* — the name-form reading of "AbortController receivers" (the method
|
|
1627
|
+
* name is cancellation-specific, and receiver-blind matching keeps
|
|
1628
|
+
* parse-only symmetric with program mode) — and the static
|
|
1629
|
+
* `AbortSignal.throwIfAborted()`.
|
|
1630
|
+
*
|
|
1631
|
+
* @returns Whether a cancellation form claimed the call.
|
|
1632
|
+
*/
|
|
1633
|
+
visitContextCancellationCall(node, method, callee) {
|
|
1634
|
+
const receiver = callee.expression;
|
|
1635
|
+
if (method === "abort") {
|
|
1636
|
+
this.emit("ContextCancellation", "aborts an AbortController", node);
|
|
1637
|
+
return true;
|
|
1638
|
+
}
|
|
1639
|
+
if (method === "throwIfAborted" &&
|
|
1640
|
+
isIdentifier(receiver) &&
|
|
1641
|
+
receiver.text === "AbortSignal") {
|
|
1642
|
+
this.emit("ContextCancellation", "throws when the signal is aborted", node);
|
|
1643
|
+
return true;
|
|
1644
|
+
}
|
|
1645
|
+
return false;
|
|
1646
|
+
}
|
|
1647
|
+
/**
|
|
1648
|
+
* The clock forms (the `TimeDependency` row): `Date.now()`,
|
|
1649
|
+
* `performance.now()`, `process.hrtime()`, and the
|
|
1650
|
+
* `process.hrtime.bigint()` chain.
|
|
1651
|
+
*
|
|
1652
|
+
* @returns Whether a clock form claimed the call.
|
|
1653
|
+
*/
|
|
1654
|
+
visitTimeDependencyCall(node, method, callee) {
|
|
1655
|
+
const receiver = callee.expression;
|
|
1656
|
+
if (method === "now" &&
|
|
1657
|
+
isIdentifier(receiver) &&
|
|
1658
|
+
(receiver.text === "Date" || receiver.text === "performance")) {
|
|
1659
|
+
this.emit("TimeDependency", "reads the current time", node);
|
|
1660
|
+
return true;
|
|
1661
|
+
}
|
|
1662
|
+
if (method === "hrtime" && isIdentifier(receiver) && receiver.text === "process") {
|
|
1663
|
+
this.emit("TimeDependency", "reads the current time", node);
|
|
1664
|
+
return true;
|
|
1665
|
+
}
|
|
1666
|
+
if (method === "bigint" &&
|
|
1667
|
+
isPropertyAccessExpression(receiver) &&
|
|
1668
|
+
receiver.name.text === "hrtime" &&
|
|
1669
|
+
isIdentifier(receiver.expression) &&
|
|
1670
|
+
receiver.expression.text === "process") {
|
|
1671
|
+
this.emit("TimeDependency", "reads the current time", node);
|
|
1672
|
+
return true;
|
|
1673
|
+
}
|
|
1674
|
+
return false;
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* Whether a receiver is checker-typed as a FinalizationRegistry (the
|
|
1678
|
+
* `.register(...)` arm of the `FinalizerRegistration` row — program
|
|
1679
|
+
* mode only; parse-only reports the construction form alone).
|
|
1680
|
+
*/
|
|
1681
|
+
isFinalizerRegistryReceiver(receiver) {
|
|
1682
|
+
const name = typeSymbolName(this.typeAt(receiver));
|
|
1683
|
+
return name !== undefined && FINALIZER_TYPE_NAMES.has(name);
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* The membership mutators (FR-015): `add`/`delete`/`clear` on any
|
|
1687
|
+
* receiver and `set` on non-parameter receivers are
|
|
1688
|
+
* `ContainerMutation`; `set` on a parameter follows the Map/WeakMap
|
|
1689
|
+
* split — `ContainerMutation` when the checker proves a WeakMap,
|
|
1690
|
+
* `MapMutation` otherwise, with parse-only taking the `MapMutation`
|
|
1691
|
+
* default (the documented type-blind surface).
|
|
1692
|
+
*
|
|
1693
|
+
* @returns Whether a membership form claimed the call.
|
|
1694
|
+
*/
|
|
1695
|
+
visitMembershipCall(node, method, receiver) {
|
|
1696
|
+
if (method === "set") {
|
|
1697
|
+
if (this.isParamReceiver(receiver)) {
|
|
1698
|
+
if (typeSymbolName(this.typeAt(receiver)) === "WeakMap") {
|
|
1699
|
+
this.emit("ContainerMutation", "mutates membership of a WeakMap parameter", node);
|
|
1700
|
+
}
|
|
1701
|
+
else {
|
|
1702
|
+
this.emit("MapMutation", "mutates a parameter through Map.set", node);
|
|
1703
|
+
}
|
|
1704
|
+
return true;
|
|
1705
|
+
}
|
|
1706
|
+
this.emit("ContainerMutation", "mutates membership of a container", node);
|
|
1707
|
+
return true;
|
|
1708
|
+
}
|
|
1709
|
+
if (MEMBERSHIP_MUTATOR_METHODS.has(method)) {
|
|
1710
|
+
this.emit("ContainerMutation", "mutates membership of a container", node);
|
|
1711
|
+
return true;
|
|
1712
|
+
}
|
|
1713
|
+
return false;
|
|
1714
|
+
}
|
|
1715
|
+
/**
|
|
1716
|
+
* The construction families: FR-016's fifth spawn form — `new
|
|
1717
|
+
* Promise(...)` emits through the `AsyncTaskSpawn` alias (resolved to
|
|
1718
|
+
* `GoroutineSpawn`; the alias never appears on the wire) — plus the
|
|
1719
|
+
* P3/P4 construction rows: `new Date()` (`TimeDependency`), `new
|
|
1720
|
+
* FinalizationRegistry(...)` (`FinalizerRegistration`), and `new
|
|
1721
|
+
* WebAssembly.Instance(...)` (`CgoCall`), then FR-017's `new
|
|
1722
|
+
* Function(...)` ambiguity form. Every other construction is inert
|
|
1723
|
+
* here; each argument's constructs descend normally.
|
|
1724
|
+
*/
|
|
1725
|
+
visitNew(node) {
|
|
1726
|
+
if (isPromiseConstruction(node)) {
|
|
1727
|
+
this.emit("AsyncTaskSpawn", "spawns an asynchronous task", node);
|
|
1728
|
+
}
|
|
1729
|
+
else if (isIdentifier(node.expression) && node.expression.text === "Date") {
|
|
1730
|
+
this.emit("TimeDependency", "reads the current time", node);
|
|
1731
|
+
}
|
|
1732
|
+
else if (isIdentifier(node.expression) &&
|
|
1733
|
+
node.expression.text === "FinalizationRegistry") {
|
|
1734
|
+
this.emit("FinalizerRegistration", "creates a finalization registry", node);
|
|
1735
|
+
}
|
|
1736
|
+
else if (isWebAssemblyInstanceConstruction(node)) {
|
|
1737
|
+
this.emit("CgoCall", "constructs a WebAssembly instance", node);
|
|
1738
|
+
}
|
|
1739
|
+
else if (isIdentifier(node.expression) && node.expression.text === "Function") {
|
|
1740
|
+
// FR-017's second always-form: `new Function(...)` builds a
|
|
1741
|
+
// function from a dynamic string — the executed effects cannot
|
|
1742
|
+
// be resolved statically. It resolves to a lib declaration in
|
|
1743
|
+
// program mode, so the match is syntactic, the `eval` precedent.
|
|
1744
|
+
// No other construction is ambiguous: `new Set<string>()` and
|
|
1745
|
+
// kin resolve to declarations and stay clean (the carve-out).
|
|
1746
|
+
this.emitAmbiguous("constructs a function dynamically through new Function", node);
|
|
1747
|
+
}
|
|
1748
|
+
this.walkChildren(node);
|
|
1749
|
+
}
|
|
1750
|
+
/**
|
|
1751
|
+
* The yield rule (FR-016 plus the P2 sibling): `yield`/`yield*`
|
|
1752
|
+
* whose innermost enclosing function-like — found by the parent
|
|
1753
|
+
* climb, the record root or an anonymous function entered during the
|
|
1754
|
+
* walk — is a synchronous generator emits `GeneratorYield`; an async
|
|
1755
|
+
* generator emits `AsyncGeneratorYield`. The yield's own expression
|
|
1756
|
+
* may nest writes, so the descent continues.
|
|
1757
|
+
*/
|
|
1758
|
+
visitYield(node) {
|
|
1759
|
+
const enclosing = enclosingFunctionLike(node);
|
|
1760
|
+
if (isSyncGenerator(enclosing)) {
|
|
1761
|
+
this.emit("GeneratorYield", node.asteriskToken === undefined
|
|
1762
|
+
? "yields a value in a generator"
|
|
1763
|
+
: "delegates to another iterable in a generator", node);
|
|
1764
|
+
}
|
|
1765
|
+
else if (isAsyncGenerator(enclosing)) {
|
|
1766
|
+
this.emit("AsyncGeneratorYield", node.asteriskToken === undefined
|
|
1767
|
+
? "yields a value in an async generator"
|
|
1768
|
+
: "delegates to another iterable in an async generator", node);
|
|
1769
|
+
}
|
|
1770
|
+
this.walkChildren(node);
|
|
1771
|
+
}
|
|
1772
|
+
/**
|
|
1773
|
+
* Whether a method receiver is a parameter binding (FR-015's splits
|
|
1774
|
+
* are lexically scoped to parameters): a bare identifier the scope
|
|
1775
|
+
* stack resolves to a parameter. Property chains (`this.items.push`)
|
|
1776
|
+
* and non-identifier receivers never are.
|
|
1777
|
+
*/
|
|
1778
|
+
isParamReceiver(receiver) {
|
|
1779
|
+
let base = receiver;
|
|
1780
|
+
while (isParenthesizedExpression(base)) {
|
|
1781
|
+
base = base.expression;
|
|
1782
|
+
}
|
|
1783
|
+
return isIdentifier(base) && this.isParameter(base.text);
|
|
1784
|
+
}
|
|
1785
|
+
/**
|
|
1786
|
+
* Whether a parameter method receiver counts as array-typed for the
|
|
1787
|
+
* `SliceMutation` method arm (FR-015): parse-only keeps the documented
|
|
1788
|
+
* type-blind default — the receiver name and shape decide — while
|
|
1789
|
+
* program mode asks the checker through the same `isArrayLikeType`
|
|
1790
|
+
* test the element-write split applies, so one row's two arms cannot
|
|
1791
|
+
* disagree. A non-array parameter's mutator call falls through to the
|
|
1792
|
+
* later tiers exactly as its element writes do.
|
|
1793
|
+
*/
|
|
1794
|
+
isArrayTypedParamReceiver(receiver) {
|
|
1795
|
+
if (this.checker === undefined) {
|
|
1796
|
+
return true;
|
|
1797
|
+
}
|
|
1798
|
+
let base = receiver;
|
|
1799
|
+
while (isParenthesizedExpression(base)) {
|
|
1800
|
+
base = base.expression;
|
|
1801
|
+
}
|
|
1802
|
+
return isArrayLikeType(this.typeAt(base));
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Whether a method receiver is HTTP-response-like: the name-form
|
|
1806
|
+
* spellings `res`/`response` (both modes), or — with the checker — a
|
|
1807
|
+
* type declared as `ServerResponse` or the fetch/express-style
|
|
1808
|
+
* `Response`.
|
|
1809
|
+
*/
|
|
1810
|
+
isHttpResponseReceiver(receiver) {
|
|
1811
|
+
if (isIdentifier(receiver) && HTTP_RESPONSE_RECEIVER_NAMES.has(receiver.text)) {
|
|
1812
|
+
return true;
|
|
1813
|
+
}
|
|
1814
|
+
const name = typeSymbolName(this.typeAt(receiver));
|
|
1815
|
+
return name !== undefined && HTTP_RESPONSE_TYPE_NAMES.has(name);
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Whether a method receiver is channel-like: the name-form spellings
|
|
1819
|
+
* port/channel/worker (both modes), or — with the checker — a type
|
|
1820
|
+
* declared as one of the MessagePort family.
|
|
1821
|
+
*/
|
|
1822
|
+
isChannelReceiver(receiver) {
|
|
1823
|
+
if (isIdentifier(receiver) && CHANNEL_CLOSE_RECEIVER_NAMES.has(receiver.text)) {
|
|
1824
|
+
return true;
|
|
1825
|
+
}
|
|
1826
|
+
const name = typeSymbolName(this.typeAt(receiver));
|
|
1827
|
+
return name !== undefined && CHANNEL_CLOSE_TYPE_NAMES.has(name);
|
|
1828
|
+
}
|
|
1829
|
+
/** The checker's type at a node, when program mode provides one. */
|
|
1830
|
+
typeAt(node) {
|
|
1831
|
+
return this.checker?.getTypeAtLocation(node);
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* An anonymous (unassigned) function-like inside the record: its
|
|
1835
|
+
* constructs attribute to this record (FR-009), so the walk enters it
|
|
1836
|
+
* under a fresh scope seeded with its parameters — an arrow's
|
|
1837
|
+
* expression body is itself an implicit return (FR-011). A
|
|
1838
|
+
* scheduling callback argument additionally opens a deferred-callback
|
|
1839
|
+
* boundary (research R6.5): everything it lexically contains executes
|
|
1840
|
+
* after the enclosing function returned, so writes it carries to
|
|
1841
|
+
* outer function-scope bindings attribute as deferred while the walk
|
|
1842
|
+
* is inside it.
|
|
1843
|
+
*/
|
|
1844
|
+
visitAnonymousFunctionLike(node) {
|
|
1845
|
+
this.scopes.push(DetectionScope.entered(paramNames(node.parameters)));
|
|
1846
|
+
const deferredBoundary = isSchedulingCallbackArgument(node);
|
|
1847
|
+
if (deferredBoundary) {
|
|
1848
|
+
this.boundaries.push(this.scopes.length - 1);
|
|
1849
|
+
}
|
|
1850
|
+
if (isArrowFunction(node) && !isBlock(node.body)) {
|
|
1851
|
+
this.emit("ReturnValue", "arrow expression body returns a value", node.body);
|
|
1852
|
+
}
|
|
1853
|
+
this.walkChildren(node);
|
|
1854
|
+
this.scopes.pop();
|
|
1855
|
+
if (deferredBoundary) {
|
|
1856
|
+
this.boundaries.pop();
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Classifies one write (FR-014/FR-015 plus the P2/P3 property rows)
|
|
1861
|
+
* and emits when it targets `this`, a parameter, `process.env.X`, a
|
|
1862
|
+
* MonkeyPatch form, a module-scope binding, or an enclosing
|
|
1863
|
+
* function-scope binding. The location anchor is the write construct
|
|
1864
|
+
* (assignment, increment/decrement, or `delete` expression); the
|
|
1865
|
+
* classified target is the written expression, with parentheses
|
|
1866
|
+
* unwrapped. Dot forms take the written path as written (`this.size`,
|
|
1867
|
+
* `p.label`, `process.env.DEBUG`, `NS.prop` — R6.6); element forms
|
|
1868
|
+
* take the base identifier; bare identifiers take the binding name.
|
|
1869
|
+
* Chained member writes other than the MonkeyPatch forms and element
|
|
1870
|
+
* writes on non-parameter bases target nothing the tiers pin — the
|
|
1871
|
+
* documented method-form boundary.
|
|
1872
|
+
*/
|
|
1873
|
+
emitWrite(locationNode, written) {
|
|
1874
|
+
let target = written;
|
|
1875
|
+
while (isParenthesizedExpression(target)) {
|
|
1876
|
+
target = target.expression;
|
|
1877
|
+
}
|
|
1878
|
+
if (isPropertyAccessExpression(target)) {
|
|
1879
|
+
if (isThisExpression(target.expression)) {
|
|
1880
|
+
this.emit("ReceiverMutation", "mutates a member of this", locationNode, target.getText(this.sourceFile));
|
|
1881
|
+
return;
|
|
1882
|
+
}
|
|
1883
|
+
if (isIdentifier(target.expression) && this.isParameter(target.expression.text)) {
|
|
1884
|
+
this.visitParamDotWrite(locationNode, target);
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
// `EnvVarMutation` (the P3 row): writes to `process.env.X`, in
|
|
1888
|
+
// every write form — assignment, compound, increment/decrement,
|
|
1889
|
+
// and `delete` all flow through here.
|
|
1890
|
+
if (isProcessEnvTarget(target)) {
|
|
1891
|
+
this.emit("EnvVarMutation", "mutates a process environment variable", locationNode, target.getText(this.sourceFile));
|
|
1892
|
+
return;
|
|
1893
|
+
}
|
|
1894
|
+
// `MonkeyPatch` (the P2 row; research R6.4 — precedence over
|
|
1895
|
+
// GlobalMutation for these forms): `X.prototype.*` writes and
|
|
1896
|
+
// property writes on imported namespace objects.
|
|
1897
|
+
const patched = this.monkeyPatchTarget(target);
|
|
1898
|
+
if (patched !== undefined) {
|
|
1899
|
+
this.emit("MonkeyPatch", patched.prototype
|
|
1900
|
+
? "patches a prototype property"
|
|
1901
|
+
: "patches a property of an imported namespace", locationNode, patched.text);
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
return;
|
|
1905
|
+
}
|
|
1906
|
+
if (isElementAccessExpression(target)) {
|
|
1907
|
+
this.visitElementWrite(locationNode, target);
|
|
1908
|
+
return;
|
|
1909
|
+
}
|
|
1910
|
+
if (isIdentifier(target)) {
|
|
1911
|
+
this.classifyIdentifierWrite(locationNode, target.text);
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* The MonkeyPatch form of a dot-property write, when it is one: an
|
|
1916
|
+
* `X.prototype.*` target (any `X` — the write is a prototype patch
|
|
1917
|
+
* regardless of what the constructor binding is), or a property
|
|
1918
|
+
* write on an imported namespace object (`import * as NS`; the scope
|
|
1919
|
+
* stack excludes function-scope shadows, so a local or parameter of
|
|
1920
|
+
* the same name keeps its own classification). Research R6.4: both
|
|
1921
|
+
* forms take precedence over GlobalMutation.
|
|
1922
|
+
*
|
|
1923
|
+
* @param target The written property-access target.
|
|
1924
|
+
* @returns The written path and which MonkeyPatch arm matched, or
|
|
1925
|
+
* `undefined` when the write is neither form.
|
|
1926
|
+
*/
|
|
1927
|
+
monkeyPatchTarget(target) {
|
|
1928
|
+
const base = target.expression;
|
|
1929
|
+
if (isPropertyAccessExpression(base) && base.name.text === "prototype") {
|
|
1930
|
+
return { text: target.getText(this.sourceFile), prototype: true };
|
|
1931
|
+
}
|
|
1932
|
+
if (isIdentifier(base) &&
|
|
1933
|
+
this.imports.namespaceSpecifiers.has(base.text) &&
|
|
1934
|
+
this.bindingScopeIndex(base.text) <= 0) {
|
|
1935
|
+
return { text: target.getText(this.sourceFile), prototype: false };
|
|
1936
|
+
}
|
|
1937
|
+
return undefined;
|
|
1938
|
+
}
|
|
1939
|
+
/**
|
|
1940
|
+
* The parameter dot-write rules (FR-014/FR-015): `p.length = …` on a
|
|
1941
|
+
* checker-proven array-typed parameter is the `SliceMutation`
|
|
1942
|
+
* truncation carve-out (never `PointerArgMutation`); every other
|
|
1943
|
+
* dot-property write on a parameter is `PointerArgMutation`. The
|
|
1944
|
+
* carve-out is typed — in parse-only, or on a non-array checker
|
|
1945
|
+
* type, the P0 dot-write rule stands.
|
|
1946
|
+
*/
|
|
1947
|
+
visitParamDotWrite(locationNode, target) {
|
|
1948
|
+
if (target.name.text === "length" &&
|
|
1949
|
+
isArrayLikeType(this.typeAt(target.expression))) {
|
|
1950
|
+
this.emit("SliceMutation", "truncates an array-typed parameter", locationNode, target.getText(this.sourceFile));
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
this.emit("PointerArgMutation", "mutates a property of a parameter", locationNode, target.getText(this.sourceFile));
|
|
1954
|
+
}
|
|
1955
|
+
/**
|
|
1956
|
+
* The element-write rules (FR-014/FR-015): `this[k] = v` is
|
|
1957
|
+
* `ReceiverMutation`; `p[k] = v` on a parameter follows the split —
|
|
1958
|
+
* `SliceMutation` for checker-proven array types, `MapMutation` for
|
|
1959
|
+
* everything else (records, objects, `any`, unresolved, and the
|
|
1960
|
+
* whole parse-only mode — the documented type-blind default). The
|
|
1961
|
+
* target is the base identifier (R6.6). Element writes on other
|
|
1962
|
+
* bases report nothing at P1.
|
|
1963
|
+
*/
|
|
1964
|
+
visitElementWrite(locationNode, target) {
|
|
1965
|
+
if (isThisExpression(target.expression)) {
|
|
1966
|
+
this.emit("ReceiverMutation", "mutates an element of this", locationNode, "this");
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
if (isIdentifier(target.expression) && this.isParameter(target.expression.text)) {
|
|
1970
|
+
if (isArrayLikeType(this.typeAt(target.expression))) {
|
|
1971
|
+
this.emit("SliceMutation", "mutates an element of an array-typed parameter", locationNode, target.expression.text);
|
|
1972
|
+
}
|
|
1973
|
+
else {
|
|
1974
|
+
this.emit("MapMutation", "mutates a computed key of a parameter", locationNode, target.expression.text);
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
/**
|
|
1979
|
+
* The identifier-write classification (FR-014; research R5): a write
|
|
1980
|
+
* to a module-scope binding from inside a function is
|
|
1981
|
+
* `GlobalMutation`; a write to an enclosing function-scope binding is
|
|
1982
|
+
* a closure capture — `DeferredReturnMutation` exactly when a
|
|
1983
|
+
* deferred-callback boundary separates the write from the declaring
|
|
1984
|
+
* scope (the mutation executes after that function returned), and a
|
|
1985
|
+
* synchronous `ClosureCaptureMutation` otherwise (P4, never
|
|
1986
|
+
* `GlobalMutation`); a write to the innermost scope's own binding is
|
|
1987
|
+
* local state; an unresolved name is the documented tracker boundary
|
|
1988
|
+
* (enum, namespace, catch-clause bindings) and reports nothing.
|
|
1989
|
+
*/
|
|
1990
|
+
classifyIdentifierWrite(locationNode, name) {
|
|
1991
|
+
const index = this.bindingScopeIndex(name);
|
|
1992
|
+
if (index < 0 || index === this.scopes.length - 1) {
|
|
1993
|
+
return;
|
|
1994
|
+
}
|
|
1995
|
+
if (index === 0) {
|
|
1996
|
+
this.emit("GlobalMutation", "writes a module-scope binding", locationNode, name);
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
if (this.boundaries.some((boundary) => index < boundary)) {
|
|
2000
|
+
this.emit("DeferredReturnMutation", "mutates an enclosing binding from a deferred callback", locationNode, name);
|
|
2001
|
+
return;
|
|
2002
|
+
}
|
|
2003
|
+
this.emit("ClosureCaptureMutation", "mutates an enclosing function-scope binding from a closure", locationNode, name);
|
|
2004
|
+
}
|
|
2005
|
+
/**
|
|
2006
|
+
* The innermost scope-stack index whose frame binds `name`, or `-1`
|
|
2007
|
+
* when no frame does — the write classification's scope address
|
|
2008
|
+
* (research R5): index 0 is the module frame, the last index is the
|
|
2009
|
+
* innermost function scope, and the frames between are the enclosing
|
|
2010
|
+
* closure scopes.
|
|
2011
|
+
*/
|
|
2012
|
+
bindingScopeIndex(name) {
|
|
2013
|
+
for (let index = this.scopes.length - 1; index >= 0; index -= 1) {
|
|
2014
|
+
if (this.scopes[index]?.binds(name)) {
|
|
2015
|
+
return index;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
return -1;
|
|
2019
|
+
}
|
|
2020
|
+
/**
|
|
2021
|
+
* Whether a name resolves to a parameter: the innermost scope that
|
|
2022
|
+
* binds the name decides — its `params` set says parameter, anything
|
|
2023
|
+
* else (local, module binding) says not. Unresolved names are never
|
|
2024
|
+
* parameters (the documented tracker boundary for enum, namespace,
|
|
2025
|
+
* and catch-clause bindings).
|
|
2026
|
+
*/
|
|
2027
|
+
isParameter(name) {
|
|
2028
|
+
const index = this.bindingScopeIndex(name);
|
|
2029
|
+
return this.scopes[index]?.params.has(name) ?? false;
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* Records one binding name into the innermost scope — the
|
|
2033
|
+
* walk-entered frames' local collection (chain-derived frames are
|
|
2034
|
+
* already hoisting-complete, so `declares` is their no-op). Function
|
|
2035
|
+
* declarations bind in the visit dispatch; variables and classes
|
|
2036
|
+
* here.
|
|
2037
|
+
*/
|
|
2038
|
+
declareBinding(node) {
|
|
2039
|
+
if (isVariableDeclaration(node)) {
|
|
2040
|
+
const names = new Set();
|
|
2041
|
+
collectBindingNames(node.name, names);
|
|
2042
|
+
for (const name of names) {
|
|
2043
|
+
this.declareName(name);
|
|
2044
|
+
}
|
|
2045
|
+
return;
|
|
2046
|
+
}
|
|
2047
|
+
if (isClassDeclaration(node) && node.name !== undefined) {
|
|
2048
|
+
this.declareName(node.name.text);
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
/** One name into the innermost scope, when the walk is inside one. */
|
|
2052
|
+
declareName(name) {
|
|
2053
|
+
this.scopes[this.scopes.length - 1]?.declares(name);
|
|
2054
|
+
}
|
|
2055
|
+
/** The default descent: every child, in source order. */
|
|
2056
|
+
walkChildren(node) {
|
|
2057
|
+
node.forEachChild((child) => {
|
|
2058
|
+
this.visit(child);
|
|
2059
|
+
return undefined;
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* Builds a record's initial scope stack from its chain (research R5):
|
|
2065
|
+
* the module frame first, then one frame per chain function scope, its
|
|
2066
|
+
* parameter names recovered from the corresponding ancestor
|
|
2067
|
+
* function-like node (the 1:1 chain/ancestor correspondence — every
|
|
2068
|
+
* chain function frame was pushed by the extractor entering exactly one
|
|
2069
|
+
* lexically enclosing function-like). The zip is defensive on length:
|
|
2070
|
+
* a short ancestor list degrades that frame's parameter set to empty
|
|
2071
|
+
* rather than crashing the walk.
|
|
2072
|
+
*
|
|
2073
|
+
* @param record The record whose scopes are built.
|
|
2074
|
+
* @returns The scope stack, innermost last.
|
|
2075
|
+
*/
|
|
2076
|
+
function buildInitialScopes(record) {
|
|
2077
|
+
const ancestors = ancestorFunctionLikes(record.node);
|
|
2078
|
+
const scopes = [];
|
|
2079
|
+
let ancestorIndex = 0;
|
|
2080
|
+
for (const frame of record.scopeChain) {
|
|
2081
|
+
if (scopes.length === 0) {
|
|
2082
|
+
// The outermost frame is the module scope: no parameters.
|
|
2083
|
+
scopes.push(DetectionScope.derived(EMPTY_NAMES, frame.names));
|
|
2084
|
+
continue;
|
|
2085
|
+
}
|
|
2086
|
+
const ancestor = ancestors[ancestorIndex];
|
|
2087
|
+
scopes.push(DetectionScope.derived(ancestor === undefined ? EMPTY_NAMES : paramNames(ancestor.parameters), frame.names));
|
|
2088
|
+
ancestorIndex += 1;
|
|
2089
|
+
}
|
|
2090
|
+
return scopes;
|
|
2091
|
+
}
|
|
2092
|
+
/**
|
|
2093
|
+
* The deferred-callback boundaries a record is born inside (research
|
|
2094
|
+
* R6.5): for a `deferred` record — one whose declaration sits inside a
|
|
2095
|
+
* scheduling callback — every ancestor function-like that is a
|
|
2096
|
+
* scheduling-call argument contributes its scope-frame index, using the
|
|
2097
|
+
* same chain/ancestor correspondence `buildInitialScopes` zips (the
|
|
2098
|
+
* k-th function-like ancestor is the (k+1)-th scope frame). A
|
|
2099
|
+
* non-deferred record starts with none: the walk itself grows the
|
|
2100
|
+
* boundary set only when it enters a scheduling callback argument.
|
|
2101
|
+
*
|
|
2102
|
+
* @param record The record whose walk the boundaries seed.
|
|
2103
|
+
* @returns The initial boundary scope indices, ascending.
|
|
2104
|
+
*/
|
|
2105
|
+
function initialDeferredBoundaries(record) {
|
|
2106
|
+
if (!record.deferred) {
|
|
2107
|
+
return [];
|
|
2108
|
+
}
|
|
2109
|
+
const boundaries = [];
|
|
2110
|
+
const ancestors = ancestorFunctionLikes(record.node);
|
|
2111
|
+
for (const [index, ancestor] of ancestors.entries()) {
|
|
2112
|
+
if (isSchedulingCallbackArgument(ancestor)) {
|
|
2113
|
+
boundaries.push(index + 1);
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
return boundaries;
|
|
2117
|
+
}
|
|
2118
|
+
/**
|
|
2119
|
+
* Narrows a record's node to the function-like union — the extraction
|
|
2120
|
+
* contract guarantees every record's node is a body-owning function-like,
|
|
2121
|
+
* so anything else is an extractor bug and fails loud (Constitution
|
|
2122
|
+
* Principle I) rather than walking an ill-shaped tree.
|
|
2123
|
+
*
|
|
2124
|
+
* @param node The record's node.
|
|
2125
|
+
* @returns The node, narrowed.
|
|
2126
|
+
* @throws Error when the node is not a function-like.
|
|
2127
|
+
*/
|
|
2128
|
+
function asFunctionLike(node) {
|
|
2129
|
+
if (!isFunctionLikeNode(node)) {
|
|
2130
|
+
throw new Error(`extraction record node is not a function-like: ${node.constructor.name}`);
|
|
2131
|
+
}
|
|
2132
|
+
return node;
|
|
2133
|
+
}
|
|
2134
|
+
//# sourceMappingURL=detector.js.map
|