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
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export declare const PARSE_ERROR = -32700;
|
|
2
|
+
export declare const INVALID_REQUEST = -32600;
|
|
3
|
+
export declare const METHOD_NOT_FOUND = -32601;
|
|
4
|
+
export declare const INVALID_PARAMS = -32602;
|
|
5
|
+
export declare const INTERNAL_ERROR = -32603;
|
|
6
|
+
export declare const PROTOCOL_VERSION = "1.2.0";
|
|
7
|
+
export declare const ANALYZER_NAME = "reading-stone";
|
|
8
|
+
export declare const LANGUAGE = "typescript";
|
|
9
|
+
export type JsonRpcId = number | string | null;
|
|
10
|
+
export interface JsonRpcRequest {
|
|
11
|
+
jsonrpc: string;
|
|
12
|
+
id: JsonRpcId;
|
|
13
|
+
method: string;
|
|
14
|
+
params: object | null;
|
|
15
|
+
}
|
|
16
|
+
export interface JsonRpcSuccess {
|
|
17
|
+
jsonrpc: string;
|
|
18
|
+
id: JsonRpcId;
|
|
19
|
+
result: object | null;
|
|
20
|
+
}
|
|
21
|
+
export interface JsonRpcErrorBody {
|
|
22
|
+
code: number;
|
|
23
|
+
message: string;
|
|
24
|
+
data?: unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface JsonRpcError {
|
|
27
|
+
jsonrpc: string;
|
|
28
|
+
id: JsonRpcId;
|
|
29
|
+
error: JsonRpcErrorBody;
|
|
30
|
+
}
|
|
31
|
+
export type JsonRpcResponse = JsonRpcSuccess | JsonRpcError;
|
|
32
|
+
export interface Capabilities {
|
|
33
|
+
discover: boolean;
|
|
34
|
+
test_mapping: boolean;
|
|
35
|
+
classify_signals: boolean;
|
|
36
|
+
streaming: boolean;
|
|
37
|
+
doc_coverage: boolean;
|
|
38
|
+
cognitive_complexity: boolean;
|
|
39
|
+
}
|
|
40
|
+
export interface InitializeResult {
|
|
41
|
+
analyzer_name: string;
|
|
42
|
+
language: string;
|
|
43
|
+
language_version: string;
|
|
44
|
+
protocol_version: string;
|
|
45
|
+
capabilities: Capabilities;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The `doc_coverage` result shape (spec 005 FR-001): a single `symbols`
|
|
49
|
+
* key holding the serialized symbol rows — exactly one key, with an empty
|
|
50
|
+
* analysis a `{"symbols": []}` success rather than an error (US1 scenario
|
|
51
|
+
* 6). plan.md pins both new methods' result interfaces here in
|
|
52
|
+
* protocol.ts, following the `ClassifySignalsResult`/`TestMappingResult`
|
|
53
|
+
* one-key envelope pattern.
|
|
54
|
+
*
|
|
55
|
+
* The pin is load-bearing end to end: the `docCoverage` entry
|
|
56
|
+
* (src/doc-coverage.ts) returns this interface's `symbols` payload and
|
|
57
|
+
* the server handler constructs its wrapper against this interface, so
|
|
58
|
+
* the `{"symbols": [...]}` wire shape is compile-time enforced rather
|
|
59
|
+
* than merely declared (spec 005 FR-012).
|
|
60
|
+
*/
|
|
61
|
+
export interface DocCoverageResult {
|
|
62
|
+
/** The serialized SymbolRow wire records, in (file, line, name) order. */
|
|
63
|
+
readonly symbols: readonly Record<string, unknown>[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The `cognitive_complexity` result shape (spec 005 FR-005): a single
|
|
67
|
+
* `functions` key holding the serialized cognitive rows — exactly one key,
|
|
68
|
+
* with an empty analysis a `{"functions": []}` success rather than an error
|
|
69
|
+
* (US2 scenario 3). plan.md pins both new methods' result interfaces here in
|
|
70
|
+
* protocol.ts, following the `ClassifySignalsResult`/`TestMappingResult`
|
|
71
|
+
* one-key envelope pattern.
|
|
72
|
+
*
|
|
73
|
+
* The pin is load-bearing end to end: the `cognitiveComplexity` entry
|
|
74
|
+
* (src/cognitive.ts) returns this interface's `functions` payload and the
|
|
75
|
+
* server handler constructs its wrapper against this interface, so the
|
|
76
|
+
* `{"functions": [...]}` wire shape is compile-time enforced rather than
|
|
77
|
+
* merely declared (spec 005 FR-012).
|
|
78
|
+
*/
|
|
79
|
+
export interface CognitiveComplexityResult {
|
|
80
|
+
/** The serialized CognitiveRow wire records, in (file, line, name) order. */
|
|
81
|
+
readonly functions: readonly Record<string, unknown>[];
|
|
82
|
+
}
|
|
83
|
+
export declare function normalizeId(value: unknown): JsonRpcId;
|
|
84
|
+
export declare function successResponse(id: JsonRpcId, result: object | null): JsonRpcSuccess;
|
|
85
|
+
export declare function errorResponse(id: JsonRpcId, code: number, message: string, data?: unknown): JsonRpcError;
|
|
86
|
+
export declare function initializeResult(): InitializeResult;
|
|
87
|
+
export declare function shutdownResult(): Record<string, never>;
|
|
88
|
+
export declare function serialize(value: unknown): string;
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export const PARSE_ERROR = -32700;
|
|
2
|
+
export const INVALID_REQUEST = -32600;
|
|
3
|
+
export const METHOD_NOT_FOUND = -32601;
|
|
4
|
+
export const INVALID_PARAMS = -32602;
|
|
5
|
+
export const INTERNAL_ERROR = -32603;
|
|
6
|
+
export const PROTOCOL_VERSION = "1.2.0";
|
|
7
|
+
export const ANALYZER_NAME = "reading-stone";
|
|
8
|
+
export const LANGUAGE = "typescript";
|
|
9
|
+
export function normalizeId(value) {
|
|
10
|
+
if (typeof value === "string") {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
export function successResponse(id, result) {
|
|
19
|
+
return { jsonrpc: "2.0", id, result };
|
|
20
|
+
}
|
|
21
|
+
export function errorResponse(id, code, message, data) {
|
|
22
|
+
const body = data === null || data === undefined ? { code, message } : { code, message, data };
|
|
23
|
+
return { jsonrpc: "2.0", id, error: body };
|
|
24
|
+
}
|
|
25
|
+
export function initializeResult() {
|
|
26
|
+
return {
|
|
27
|
+
analyzer_name: ANALYZER_NAME,
|
|
28
|
+
language: LANGUAGE,
|
|
29
|
+
language_version: process.versions.node,
|
|
30
|
+
protocol_version: PROTOCOL_VERSION,
|
|
31
|
+
capabilities: {
|
|
32
|
+
discover: true,
|
|
33
|
+
// test_mapping flipped true with the src/server.ts dispatch
|
|
34
|
+
// registration in one protocol-state unit (T020, FR-014): no
|
|
35
|
+
// commit advertises a capability the dispatch table lacks.
|
|
36
|
+
test_mapping: true,
|
|
37
|
+
classify_signals: true,
|
|
38
|
+
streaming: false,
|
|
39
|
+
// doc_coverage flipped true with the src/server.ts dispatch
|
|
40
|
+
// registration in one protocol-state unit (T009, spec 005
|
|
41
|
+
// FR-013): no commit advertises a capability the dispatch table
|
|
42
|
+
// lacks. The capabilities object is the single source of truth
|
|
43
|
+
// for method availability.
|
|
44
|
+
doc_coverage: true,
|
|
45
|
+
// cognitive_complexity flipped true with the src/server.ts dispatch
|
|
46
|
+
// registration in one protocol-state unit (T017, spec 005
|
|
47
|
+
// FR-013): no commit advertises a capability the dispatch table
|
|
48
|
+
// lacks. With both v1.2.0 methods registered, the reserved -32601
|
|
49
|
+
// set is exactly analyze/stream — only streaming remains false.
|
|
50
|
+
cognitive_complexity: true,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export function shutdownResult() {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
function sortedValue(value) {
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
return value.map(sortedValue);
|
|
60
|
+
}
|
|
61
|
+
if (value !== null && typeof value === "object") {
|
|
62
|
+
const record = value;
|
|
63
|
+
const out = {};
|
|
64
|
+
for (const key of Object.keys(record).sort()) {
|
|
65
|
+
out[key] = sortedValue(record[key]);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
export function serialize(value) {
|
|
72
|
+
return JSON.stringify(sortedValue(value));
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { Writable } from "node:stream";
|
|
2
|
+
import { type Node } from "typescript/unstable/ast";
|
|
3
|
+
import type { TestFunction } from "./collect.js";
|
|
4
|
+
import type { AssertionKind } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* One detected assertion (data-model.md Section 5.2): the classified
|
|
7
|
+
* site of one matcher invocation, anchored at its assertion expression's
|
|
8
|
+
* outermost wrapper.
|
|
9
|
+
*
|
|
10
|
+
* `assertedExpression` and `argumentSubtree` are live compiler views
|
|
11
|
+
* kept for the inference stage (US4's provenance passes, T031): the
|
|
12
|
+
* asserted expression is the provenance subject, and the argument
|
|
13
|
+
* subtree is pass 4's scan domain.
|
|
14
|
+
*/
|
|
15
|
+
export interface AssertionSite {
|
|
16
|
+
/** The classified kind (Section 4's normative table). */
|
|
17
|
+
readonly kind: AssertionKind;
|
|
18
|
+
/**
|
|
19
|
+
* 1-based line of the assertion's anchor — the outermost wrapper of
|
|
20
|
+
* the matcher invocation (`await` and parenthesized wrappers unwrapped
|
|
21
|
+
* upward), the same anchor for every matcher invocation on one shared
|
|
22
|
+
* chain (the row's `assertion_location` line; no column on the wire).
|
|
23
|
+
*/
|
|
24
|
+
readonly line: number;
|
|
25
|
+
/**
|
|
26
|
+
* 0-based character of the assertion's anchor — the row-order
|
|
27
|
+
* tie-break; every matcher invocation on one shared chain reports the
|
|
28
|
+
* same anchor.
|
|
29
|
+
*/
|
|
30
|
+
readonly character: number;
|
|
31
|
+
/**
|
|
32
|
+
* The asserted expression — expect's FIRST argument, the provenance
|
|
33
|
+
* subject — or `null` when the expect call carried no arguments.
|
|
34
|
+
*/
|
|
35
|
+
readonly assertedExpression: Node | null;
|
|
36
|
+
/**
|
|
37
|
+
* Pass 4's scan domain: the invoked matcher invocation's full
|
|
38
|
+
* `CallExpression`, spanning BOTH the `expect(...)` arguments and the
|
|
39
|
+
* matcher's own arguments.
|
|
40
|
+
*/
|
|
41
|
+
readonly argumentSubtree: Node;
|
|
42
|
+
/** `true` for expect-rooted forms; `false` for node:assert forms (T028). */
|
|
43
|
+
readonly expectChain: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Collects every assertion inside one paired test function (FR-007's
|
|
47
|
+
* expect form; research R5).
|
|
48
|
+
*
|
|
49
|
+
* The scan domain is the paired test's callback subtree INCLUDING nested
|
|
50
|
+
* function expressions within it — a `Promise.resolve().then(() =>
|
|
51
|
+
* expect(x).toBe(1))` callback body is inside the test, so its chain
|
|
52
|
+
* collects; hooks and describe-scope statements are structurally outside
|
|
53
|
+
* (the walk starts at this test's own callback node, assumption 13). A
|
|
54
|
+
* test whose callback argument is a bare identifier reference (no
|
|
55
|
+
* subtree) collects nothing — the truthful outcome, never a crash and
|
|
56
|
+
* never a scan of the referenced function's body (US3-8).
|
|
57
|
+
*
|
|
58
|
+
* The walk is depth-bounded (F2; plan.md Scale/Scope): a callback
|
|
59
|
+
* subtree whose nesting passes the shared `MAPPING_WALK_DEPTH_BUDGET`
|
|
60
|
+
* discards only THIS test's sites — sibling tests keep theirs, the
|
|
61
|
+
* assertion stage being per test — with exactly one diagnostic written
|
|
62
|
+
* to `stderr` (a `RangeError` from lazy node materialization is treated
|
|
63
|
+
* identically). Pathological input degrades to a skip, never a crash
|
|
64
|
+
* (Constitution V).
|
|
65
|
+
*
|
|
66
|
+
* Sites emit in the deterministic AST-walk collection order: a post-order
|
|
67
|
+
* descent (children before the node itself), so the statements of a
|
|
68
|
+
* callback body arrive in source order and the matcher invocations of
|
|
69
|
+
* one shared chain arrive inner-first — the row sort's pinned final
|
|
70
|
+
* tie-break. Classification runs through the normative table
|
|
71
|
+
* (src/quality/matchers.ts): the matcher's kind under the modifier rule
|
|
72
|
+
* (`.rejects.` → `error_check`, else the matcher's row), and an unknown
|
|
73
|
+
* or custom matcher routes to `custom` — never an exception, never a drop.
|
|
74
|
+
*
|
|
75
|
+
* @param test The paired test function.
|
|
76
|
+
* @param stderr The diagnostic sink for depth-budget skips (the server's
|
|
77
|
+
* stderr; never stdout — protocol fidelity).
|
|
78
|
+
* @returns The assertion sites in AST-walk collection order — `[]` for
|
|
79
|
+
* a null callback, an assertion-free subtree, or a depth-skipped one.
|
|
80
|
+
*/
|
|
81
|
+
export declare function collectAssertions(test: TestFunction, stderr?: Writable): readonly AssertionSite[];
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
// Expect-chain assertion detection (T017; FR-007's expect form; research
|
|
2
|
+
// R5; data-model.md Section 5.2; Constitution Principles I, III, and V).
|
|
3
|
+
//
|
|
4
|
+
// `collectAssertions` walks the paired test's callback subtree and
|
|
5
|
+
// reconstructs every vitest/jest `expect()` chain invocation as an
|
|
6
|
+
// `AssertionSite` — the assertion side of the mapping pipeline. The walk
|
|
7
|
+
// is purely lexical (AST inspection through the compiler API): test
|
|
8
|
+
// files are untrusted input parsed inside the bounded hermetic host,
|
|
9
|
+
// never executed or imported (Constitution V). The walk is depth-bounded
|
|
10
|
+
// (F2; plan.md Scale/Scope): a pathological callback subtree discards
|
|
11
|
+
// only that test's sites with exactly one diagnostic — the shared
|
|
12
|
+
// `MAPPING_WALK_DEPTH_BUDGET`, never a request crash.
|
|
13
|
+
//
|
|
14
|
+
// Chain reconstruction (data-model Section 5.2): an invocation qualifies
|
|
15
|
+
// when its callee is a property access whose base chain — through
|
|
16
|
+
// intermediate property accesses and `await` wrappers — roots at a call
|
|
17
|
+
// whose callee is the identifier `expect`. The invoked property is the
|
|
18
|
+
// matcher name; the intermediate properties are the modifier chain; one
|
|
19
|
+
// `AssertionSite` is emitted per matcher invocation, so a chain with two
|
|
20
|
+
// matcher invocations (`expect(x).toBe(1).toBeNull()`) yields two sites
|
|
21
|
+
// sharing the chain's single anchor. An `expect(...)` chain with NO
|
|
22
|
+
// matcher invocation is not an assertion (US3-9): a bare `expect(value)`
|
|
23
|
+
// call and a dangling `expect(x).not` property access both collect
|
|
24
|
+
// nothing.
|
|
25
|
+
//
|
|
26
|
+
// Anchoring (US1-1; the landed rule, F5-reconciled): the assertion
|
|
27
|
+
// anchor is the OUTERMOST wrapper of the matcher invocation — `await`
|
|
28
|
+
// and parenthesized wrappers unwrapped upward through `node.parent`, so
|
|
29
|
+
// `await expect(p).resolves.toEqual(2)` anchors at the `await` and
|
|
30
|
+
// `(await expect(p)).toBe(2)` at the `(` — the leftmost spelling of the
|
|
31
|
+
// full assertion expression, while the invocation's own `getStart`
|
|
32
|
+
// descent through its callee covers the plain-chain form (every matcher
|
|
33
|
+
// invocation on one chain reports the SAME anchor). Never the matcher's
|
|
34
|
+
// own position; the row sort's final tie-break (AST-walk collection
|
|
35
|
+
// order) governs the relative order of one chain's sites.
|
|
36
|
+
//
|
|
37
|
+
// US3's node:assert forms (T028; data-model Sections 4.2 and 5.2) ride
|
|
38
|
+
// the SAME walk: the assert-binding table — built per call from the test
|
|
39
|
+
// file's import/require declarations over the closed module-specifier
|
|
40
|
+
// set — recognizes `assert(value)` bare truthiness calls, `assert.fn(...)`
|
|
41
|
+
// member calls on default/namespace/CJS carrier bindings, and bare
|
|
42
|
+
// named-binding calls, each emitting an `AssertionSite` with
|
|
43
|
+
// `expectChain: false` and classification through the pinned member
|
|
44
|
+
// table (src/quality/matchers.ts). The expect-chain surface above is
|
|
45
|
+
// unchanged.
|
|
46
|
+
import { isAwaitExpression, isCallExpression, isIdentifier, isImportDeclaration, isNamedImports, isNamespaceImport, isParenthesizedExpression, isPropertyAccessExpression, isStringLiteral, isVariableStatement, SyntaxKind, } from "typescript/unstable/ast";
|
|
47
|
+
import { MAPPING_WALK_DEPTH_BUDGET } from "../internal.js";
|
|
48
|
+
import { classifyAssertCall, classifyExpectChain } from "./matchers.js";
|
|
49
|
+
/**
|
|
50
|
+
* The assertion walk's depth sentinel (F2): raised when the callback
|
|
51
|
+
* subtree's nesting depth passes the shared `MAPPING_WALK_DEPTH_BUDGET`,
|
|
52
|
+
* caught by `collectAssertions` — never escapes this module (the
|
|
53
|
+
* extract.ts `AstDepthExceeded` discipline). A `RangeError` from lazy
|
|
54
|
+
* node materialization is caught at the same site and treated
|
|
55
|
+
* identically.
|
|
56
|
+
*/
|
|
57
|
+
class MappingWalkDepthExceeded extends Error {
|
|
58
|
+
constructor() {
|
|
59
|
+
super("ast depth exceeds budget");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The closed `node:assert` module-specifier set (data-model Section 4.2;
|
|
64
|
+
// research R5): a binding is an assert binding exactly when its import or
|
|
65
|
+
* require names one of these specifiers. Matched SYNTACTICALLY in both
|
|
66
|
+
* bootstrap modes — `node:assert` admits no in-root re-export, so the
|
|
67
|
+
* specifier text is the complete resolution (never a checker query, never
|
|
68
|
+
* a filesystem probe). `node:assert/strict` is in the set because it
|
|
69
|
+
* changes call SEMANTICS, not routing: the member name routes through the
|
|
70
|
+
* same table either way.
|
|
71
|
+
*/
|
|
72
|
+
const ASSERT_MODULE_SPECIFIERS = new Set([
|
|
73
|
+
"node:assert",
|
|
74
|
+
"node:assert/strict",
|
|
75
|
+
"assert",
|
|
76
|
+
]);
|
|
77
|
+
/**
|
|
78
|
+
* Builds the assert-binding table from one test file's top-level import
|
|
79
|
+
* and require declarations (T028; data-model Section 4.2).
|
|
80
|
+
*
|
|
81
|
+
* A statement-level scan — import declarations are top-level only, the
|
|
82
|
+
* import clause's shapes are fixed grammar, and the CJS form is the
|
|
83
|
+
* exact `const <identifier> = require(<specifier>)` declarator — so the
|
|
84
|
+
* build performs NO recursion at all and the shared
|
|
85
|
+
* `MAPPING_WALK_DEPTH_BUDGET` is satisfied vacuously (the
|
|
86
|
+
* `namedImportBindings` precedent in src/quality/pairing.ts: a walk with
|
|
87
|
+
* no descent has no depth to bound). A `require` nested inside a
|
|
88
|
+
* function body is deliberately NOT a file binding — it names a local of
|
|
89
|
+
* that scope, never the file's assertion vocabulary.
|
|
90
|
+
*
|
|
91
|
+
* @param sourceFile The test file's parsed source.
|
|
92
|
+
* @returns The file's assert bindings — empty sets when the file has no
|
|
93
|
+
* `node:assert` import or require.
|
|
94
|
+
*/
|
|
95
|
+
function buildAssertBindingTable(sourceFile) {
|
|
96
|
+
const carriers = new Set();
|
|
97
|
+
const named = new Map();
|
|
98
|
+
for (const statement of sourceFile.statements) {
|
|
99
|
+
if (isImportDeclaration(statement)) {
|
|
100
|
+
const clause = statement.importClause;
|
|
101
|
+
// The whole-clause `import type` form: TS 7's native AST spells the
|
|
102
|
+
// erased marker as the clause's phase modifier, not a boolean (the
|
|
103
|
+
// namedImportBindings precedent in src/quality/pairing.ts).
|
|
104
|
+
if (clause === undefined || clause.phaseModifier === SyntaxKind.TypeKeyword) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const specifier = isStringLiteral(statement.moduleSpecifier)
|
|
108
|
+
? statement.moduleSpecifier.text
|
|
109
|
+
: null;
|
|
110
|
+
if (specifier === null || !ASSERT_MODULE_SPECIFIERS.has(specifier)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (clause.name !== undefined) {
|
|
114
|
+
carriers.add(clause.name.text);
|
|
115
|
+
}
|
|
116
|
+
const bindings = clause.namedBindings;
|
|
117
|
+
if (bindings === undefined) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (isNamespaceImport(bindings)) {
|
|
121
|
+
carriers.add(bindings.name.text);
|
|
122
|
+
}
|
|
123
|
+
else if (isNamedImports(bindings)) {
|
|
124
|
+
for (const element of bindings.elements) {
|
|
125
|
+
if (element.isTypeOnly) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
named.set(element.name.text, element.propertyName?.text ?? element.name.text);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (isVariableStatement(statement)) {
|
|
134
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
135
|
+
// The CJS form is the exact declarator shape — an identifier name
|
|
136
|
+
// whose initializer is a require call naming the closed set. A
|
|
137
|
+
// destructuring pattern (`const { equal } = require(...)`) is not
|
|
138
|
+
// one of the four recognized binding forms.
|
|
139
|
+
if (!isIdentifier(declaration.name)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const initializer = declaration.initializer;
|
|
143
|
+
if (initializer === undefined || !isCallExpression(initializer)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const callee = initializer.expression;
|
|
147
|
+
if (!isIdentifier(callee) || callee.text !== "require") {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const argument = initializer.arguments[0];
|
|
151
|
+
if (argument === undefined || !isStringLiteral(argument)) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (ASSERT_MODULE_SPECIFIERS.has(argument.text)) {
|
|
155
|
+
carriers.add(declaration.name.text);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { carriers, named };
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The assert form one call expression takes under a binding table, or
|
|
164
|
+
* `null` when the call is not assert-rooted (T028; data-model Section
|
|
165
|
+
* 4.2).
|
|
166
|
+
*
|
|
167
|
+
* Recognition is import-gated and lexical — the local-name lookups never
|
|
168
|
+
* consult the checker, so a file-local function named `equal` (never
|
|
169
|
+
* imported) is not an assert call, while a same-named named import is
|
|
170
|
+
* (the documented conservative trade: a shadowed local import binding
|
|
171
|
+
* still recognizes, availability over precision at the site level).
|
|
172
|
+
* Nested property chains (`assert.strict.equal(...)`) do not recognize:
|
|
173
|
+
* the member-call form is exactly one property access whose base is a
|
|
174
|
+
* carrier identifier.
|
|
175
|
+
*
|
|
176
|
+
* @param call The candidate call expression.
|
|
177
|
+
* @param table The test file's assert bindings.
|
|
178
|
+
* @returns The classification member — `undefined` for the bare
|
|
179
|
+
* truthiness call, the member/property text otherwise — or `null` when
|
|
180
|
+
* the call is not assert-rooted.
|
|
181
|
+
*/
|
|
182
|
+
function assertFormOf(call, table) {
|
|
183
|
+
const callee = call.expression;
|
|
184
|
+
if (isIdentifier(callee)) {
|
|
185
|
+
if (table.carriers.has(callee.text)) {
|
|
186
|
+
// The bare truthiness call `assert(value)` — the undefined member.
|
|
187
|
+
return { member: undefined };
|
|
188
|
+
}
|
|
189
|
+
const imported = table.named.get(callee.text);
|
|
190
|
+
if (imported !== undefined) {
|
|
191
|
+
return { member: imported };
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
if (isPropertyAccessExpression(callee) &&
|
|
196
|
+
isIdentifier(callee.expression) &&
|
|
197
|
+
table.carriers.has(callee.expression.text)) {
|
|
198
|
+
return { member: callee.name.text };
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reconstructs the expect chain a matcher invocation hangs from
|
|
204
|
+
* (data-model Section 5.2), or answers `null` when the invocation is not
|
|
205
|
+
* expect-rooted.
|
|
206
|
+
*
|
|
207
|
+
* The walk descends from the invocation's callee toward the chain root,
|
|
208
|
+
* unwrapping every wrapper form the chain grammar admits:
|
|
209
|
+
*
|
|
210
|
+
* - an intermediate property access contributes its name to the modifier
|
|
211
|
+
* chain (`.not.`, `.resolves.`, `.rejects.`, and any other — the
|
|
212
|
+
* classification table decides which are meaningful; inert names are
|
|
213
|
+
* kept harmlessly);
|
|
214
|
+
* - parentheses and `await` wrappers are transparent — `(await
|
|
215
|
+
* expect(p)).toBe(2)` still roots at the `expect(p)` call, and the
|
|
216
|
+
* modifiers list reaching `classifyExpectChain` never sees an await
|
|
217
|
+
* (US3-10's awaited-chain parity, by construction);
|
|
218
|
+
* - a call rooted at the identifier `expect` is THE root;
|
|
219
|
+
* - a call rooted at another identifier (`something().it(...)`) is not
|
|
220
|
+
* an expect chain — the walk stops without a root;
|
|
221
|
+
* - a call rooted at a property access is an intermediate matcher
|
|
222
|
+
* invocation (`expect(v).toBe(1).toBeNull()`'s inner link): its
|
|
223
|
+
* callee property is inert context for THIS invocation (the inner
|
|
224
|
+
* invocation is itself a site the caller's walk collects), and the
|
|
225
|
+
* descent continues through that property's base;
|
|
226
|
+
* - anything else (an element access, an identifier, a literal) is not
|
|
227
|
+
* an expect chain.
|
|
228
|
+
*
|
|
229
|
+
* @param invocation The candidate matcher-invocation call expression.
|
|
230
|
+
* @returns The reconstructed chain, or `null` when not expect-rooted.
|
|
231
|
+
*/
|
|
232
|
+
function expectChainOf(invocation) {
|
|
233
|
+
const callee = invocation.expression;
|
|
234
|
+
if (!isPropertyAccessExpression(callee)) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const matcher = callee.name.text;
|
|
238
|
+
const modifiers = [];
|
|
239
|
+
let node = callee.expression;
|
|
240
|
+
while (true) {
|
|
241
|
+
if (isPropertyAccessExpression(node)) {
|
|
242
|
+
modifiers.push(node.name.text);
|
|
243
|
+
node = node.expression;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (isParenthesizedExpression(node) || isAwaitExpression(node)) {
|
|
247
|
+
node = node.expression;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (isCallExpression(node)) {
|
|
251
|
+
const innerCallee = node.expression;
|
|
252
|
+
if (isIdentifier(innerCallee)) {
|
|
253
|
+
if (innerCallee.text === "expect") {
|
|
254
|
+
return { rootCall: node, matcher, modifiers };
|
|
255
|
+
}
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
if (isPropertyAccessExpression(innerCallee)) {
|
|
259
|
+
// An intermediate matcher invocation's link property — inert
|
|
260
|
+
// chain context for this invocation; the inner invocation is
|
|
261
|
+
// collected as its own site by the subtree walk.
|
|
262
|
+
modifiers.push(innerCallee.name.text);
|
|
263
|
+
node = innerCallee.expression;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* The assertion anchor of one matcher invocation (US1-1): the invocation's
|
|
273
|
+
* own position, unwrapped upward through enclosing `await` and
|
|
274
|
+
* parenthesized wrappers — so `await expect(p).resolves.toEqual(2)`
|
|
275
|
+
* anchors at the `await` and `(await expect(p)).toBe(2)` at the `(`,
|
|
276
|
+
* the leftmost character of the full assertion expression, while the
|
|
277
|
+
* invocation's own `getStart` descent through its callee covers the
|
|
278
|
+
* parenthesized receiver form. Every matcher invocation on one shared
|
|
279
|
+
* chain reports the SAME anchor (their common leftmost spelling), the
|
|
280
|
+
* position the row's `assertion_location` line and the row-order
|
|
281
|
+
* character tie-break read.
|
|
282
|
+
*
|
|
283
|
+
* @param invocation The collected matcher-invocation call expression.
|
|
284
|
+
* @param sourceFile The invocation's source file — the coordinate base.
|
|
285
|
+
* @returns The anchor's 1-based line and 0-based character.
|
|
286
|
+
*/
|
|
287
|
+
function assertionAnchor(invocation, sourceFile) {
|
|
288
|
+
let outermost = invocation;
|
|
289
|
+
while (isAwaitExpression(outermost.parent) ||
|
|
290
|
+
isParenthesizedExpression(outermost.parent)) {
|
|
291
|
+
outermost = outermost.parent;
|
|
292
|
+
}
|
|
293
|
+
const position = sourceFile.getLineAndCharacterOfPosition(outermost.getStart(sourceFile));
|
|
294
|
+
return { line: position.line + 1, character: position.character };
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Collects every assertion inside one paired test function (FR-007's
|
|
298
|
+
* expect form; research R5).
|
|
299
|
+
*
|
|
300
|
+
* The scan domain is the paired test's callback subtree INCLUDING nested
|
|
301
|
+
* function expressions within it — a `Promise.resolve().then(() =>
|
|
302
|
+
* expect(x).toBe(1))` callback body is inside the test, so its chain
|
|
303
|
+
* collects; hooks and describe-scope statements are structurally outside
|
|
304
|
+
* (the walk starts at this test's own callback node, assumption 13). A
|
|
305
|
+
* test whose callback argument is a bare identifier reference (no
|
|
306
|
+
* subtree) collects nothing — the truthful outcome, never a crash and
|
|
307
|
+
* never a scan of the referenced function's body (US3-8).
|
|
308
|
+
*
|
|
309
|
+
* The walk is depth-bounded (F2; plan.md Scale/Scope): a callback
|
|
310
|
+
* subtree whose nesting passes the shared `MAPPING_WALK_DEPTH_BUDGET`
|
|
311
|
+
* discards only THIS test's sites — sibling tests keep theirs, the
|
|
312
|
+
* assertion stage being per test — with exactly one diagnostic written
|
|
313
|
+
* to `stderr` (a `RangeError` from lazy node materialization is treated
|
|
314
|
+
* identically). Pathological input degrades to a skip, never a crash
|
|
315
|
+
* (Constitution V).
|
|
316
|
+
*
|
|
317
|
+
* Sites emit in the deterministic AST-walk collection order: a post-order
|
|
318
|
+
* descent (children before the node itself), so the statements of a
|
|
319
|
+
* callback body arrive in source order and the matcher invocations of
|
|
320
|
+
* one shared chain arrive inner-first — the row sort's pinned final
|
|
321
|
+
* tie-break. Classification runs through the normative table
|
|
322
|
+
* (src/quality/matchers.ts): the matcher's kind under the modifier rule
|
|
323
|
+
* (`.rejects.` → `error_check`, else the matcher's row), and an unknown
|
|
324
|
+
* or custom matcher routes to `custom` — never an exception, never a drop.
|
|
325
|
+
*
|
|
326
|
+
* @param test The paired test function.
|
|
327
|
+
* @param stderr The diagnostic sink for depth-budget skips (the server's
|
|
328
|
+
* stderr; never stdout — protocol fidelity).
|
|
329
|
+
* @returns The assertion sites in AST-walk collection order — `[]` for
|
|
330
|
+
* a null callback, an assertion-free subtree, or a depth-skipped one.
|
|
331
|
+
*/
|
|
332
|
+
export function collectAssertions(test, stderr = process.stderr) {
|
|
333
|
+
const callback = test.callback;
|
|
334
|
+
if (callback === null) {
|
|
335
|
+
return [];
|
|
336
|
+
}
|
|
337
|
+
const sourceFile = callback.getSourceFile();
|
|
338
|
+
// The file's assert bindings, built once per call (a statement-level
|
|
339
|
+
// scan, recursion-free): the pipeline memoizes this function per test
|
|
340
|
+
// function, so the table is never rebuilt per pair or per assertion.
|
|
341
|
+
const assertBindings = buildAssertBindingTable(sourceFile);
|
|
342
|
+
// Per-test buffer (F2): one pathological callback discards only its own
|
|
343
|
+
// sites — sibling tests in the same file are unaffected (the assertion
|
|
344
|
+
// stage runs per test, so the skip unit is the test, not the file).
|
|
345
|
+
const sites = [];
|
|
346
|
+
try {
|
|
347
|
+
walkForChains(callback, sourceFile, assertBindings, sites, 0);
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
// Only the depth sentinel and a materialization RangeError are
|
|
351
|
+
// diagnosed skips; anything else escapes (a crash is never a silent
|
|
352
|
+
// skip — the buildProgramContext rethrow discipline).
|
|
353
|
+
if (!(error instanceof MappingWalkDepthExceeded || error instanceof RangeError)) {
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
stderr.write(`reading-stone: skipping assertions in ${test.relPath}:${test.line}: ast depth exceeds budget\n`);
|
|
357
|
+
return [];
|
|
358
|
+
}
|
|
359
|
+
return sites;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* The post-order subtree walk: descends into every child first, then
|
|
363
|
+
* processes the node — so nested function bodies collect before their
|
|
364
|
+
* enclosing statements complete, statements arrive in source order, and
|
|
365
|
+
* a shared chain's inner matcher invocation precedes its outer one (the
|
|
366
|
+
* deterministic collection order the row sort's final tie-break pins).
|
|
367
|
+
* Depth-bounded by the shared `MAPPING_WALK_DEPTH_BUDGET` (F2): the
|
|
368
|
+
* sentinel raised past the budget is caught by `collectAssertions`'s
|
|
369
|
+
* per-test skip.
|
|
370
|
+
*
|
|
371
|
+
* Both assertion families ride this one walk: a `CallExpression` is
|
|
372
|
+
* first probed for expect-chain rootedness, then — when not
|
|
373
|
+
* expect-rooted — for a node:assert form under the file's binding table
|
|
374
|
+
* (T028). A call can be at most one of the two (an expect chain roots at
|
|
375
|
+
* the identifier `expect`; an assert form roots at a table binding), so
|
|
376
|
+
* the order is a deterministic tie-break, never a conflict.
|
|
377
|
+
*
|
|
378
|
+
* @param node The subtree root (the test's callback).
|
|
379
|
+
* @param sourceFile The callback's source file — the anchor's coordinate
|
|
380
|
+
* base and the asserted nodes' own file.
|
|
381
|
+
* @param assertBindings The test file's assert bindings (T028).
|
|
382
|
+
* @param sites The collected sites, in walk order.
|
|
383
|
+
* @param depth The current nesting depth (0 at the callback root).
|
|
384
|
+
*/
|
|
385
|
+
function walkForChains(node, sourceFile, assertBindings, sites, depth) {
|
|
386
|
+
if (depth > MAPPING_WALK_DEPTH_BUDGET) {
|
|
387
|
+
throw new MappingWalkDepthExceeded();
|
|
388
|
+
}
|
|
389
|
+
node.forEachChild((child) => {
|
|
390
|
+
walkForChains(child, sourceFile, assertBindings, sites, depth + 1);
|
|
391
|
+
});
|
|
392
|
+
if (!isCallExpression(node)) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
const chain = expectChainOf(node);
|
|
396
|
+
if (chain !== null) {
|
|
397
|
+
const anchor = assertionAnchor(node, sourceFile);
|
|
398
|
+
sites.push({
|
|
399
|
+
kind: classifyExpectChain(chain.matcher, chain.modifiers),
|
|
400
|
+
line: anchor.line,
|
|
401
|
+
character: anchor.character,
|
|
402
|
+
assertedExpression: chain.rootCall.arguments[0] ?? null,
|
|
403
|
+
argumentSubtree: node,
|
|
404
|
+
expectChain: true,
|
|
405
|
+
});
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const assertForm = assertFormOf(node, assertBindings);
|
|
409
|
+
if (assertForm !== null) {
|
|
410
|
+
// The node:assert site (data-model Section 5.2): the same anchor
|
|
411
|
+
// rule (the outermost wrapper, `await` unwrapped upward), the first
|
|
412
|
+
// argument as the provenance subject, the assert call's own
|
|
413
|
+
// argument list as the pass-4 scan domain, and `expectChain: false`.
|
|
414
|
+
const anchor = assertionAnchor(node, sourceFile);
|
|
415
|
+
sites.push({
|
|
416
|
+
kind: classifyAssertCall(assertForm.member),
|
|
417
|
+
line: anchor.line,
|
|
418
|
+
character: anchor.character,
|
|
419
|
+
assertedExpression: node.arguments[0] ?? null,
|
|
420
|
+
argumentSubtree: node,
|
|
421
|
+
expectChain: false,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
//# sourceMappingURL=assertions.js.map
|