reading-stone 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +11 -0
  3. package/README.md +274 -2
  4. package/dist/analysis.d.ts +341 -0
  5. package/dist/analysis.js +392 -0
  6. package/dist/cognitive.d.ts +163 -0
  7. package/dist/cognitive.js +380 -0
  8. package/dist/complexity.d.ts +102 -0
  9. package/dist/complexity.js +163 -0
  10. package/dist/coverage.d.ts +78 -0
  11. package/dist/coverage.js +635 -0
  12. package/dist/detector.d.ts +41 -0
  13. package/dist/detector.js +2134 -0
  14. package/dist/discovery.d.ts +330 -0
  15. package/dist/discovery.js +930 -0
  16. package/dist/doc-coverage.d.ts +155 -0
  17. package/dist/doc-coverage.js +543 -0
  18. package/dist/extract.d.ts +209 -0
  19. package/dist/extract.js +498 -0
  20. package/dist/index.d.ts +64 -0
  21. package/dist/index.js +54 -0
  22. package/dist/internal.d.ts +75 -0
  23. package/dist/internal.js +63 -0
  24. package/dist/main.d.ts +8 -0
  25. package/dist/main.js +39 -0
  26. package/dist/models.d.ts +185 -0
  27. package/dist/models.js +170 -0
  28. package/dist/program.d.ts +207 -0
  29. package/dist/program.js +662 -0
  30. package/dist/protocol.d.ts +88 -0
  31. package/dist/protocol.js +74 -0
  32. package/dist/quality/assertions.d.ts +81 -0
  33. package/dist/quality/assertions.js +425 -0
  34. package/dist/quality/collect.d.ts +66 -0
  35. package/dist/quality/collect.js +412 -0
  36. package/dist/quality/inference.d.ts +86 -0
  37. package/dist/quality/inference.js +603 -0
  38. package/dist/quality/matchers.d.ts +129 -0
  39. package/dist/quality/matchers.js +220 -0
  40. package/dist/quality/pairing.d.ts +133 -0
  41. package/dist/quality/pairing.js +770 -0
  42. package/dist/quality/pipeline.d.ts +59 -0
  43. package/dist/quality/pipeline.js +202 -0
  44. package/dist/quality/types.d.ts +127 -0
  45. package/dist/quality/types.js +92 -0
  46. package/dist/safeio.d.ts +107 -0
  47. package/dist/safeio.js +314 -0
  48. package/dist/server.d.ts +19 -0
  49. package/dist/server.js +451 -0
  50. package/dist/signals/adapter.d.ts +103 -0
  51. package/dist/signals/adapter.js +323 -0
  52. package/dist/signals/caller-index.d.ts +117 -0
  53. package/dist/signals/caller-index.js +147 -0
  54. package/dist/signals/caller.d.ts +76 -0
  55. package/dist/signals/caller.js +108 -0
  56. package/dist/signals/evidence.d.ts +233 -0
  57. package/dist/signals/evidence.js +578 -0
  58. package/dist/signals/interface.d.ts +68 -0
  59. package/dist/signals/interface.js +87 -0
  60. package/dist/signals/jsdoc.d.ts +27 -0
  61. package/dist/signals/jsdoc.js +233 -0
  62. package/dist/signals/naming.d.ts +26 -0
  63. package/dist/signals/naming.js +160 -0
  64. package/dist/signals/routing.d.ts +163 -0
  65. package/dist/signals/routing.js +416 -0
  66. package/dist/signals/types.d.ts +146 -0
  67. package/dist/signals/types.js +99 -0
  68. package/dist/signals/visibility.d.ts +23 -0
  69. package/dist/signals/visibility.js +82 -0
  70. package/dist/taxonomy.d.ts +89 -0
  71. package/dist/taxonomy.js +217 -0
  72. package/package.json +61 -4
@@ -0,0 +1,108 @@
1
+ /**
2
+ * The distinct-module bucket extractor (FR-004; research R7; data-model.md
3
+ * Section 7) — the pure half of US3's caller signal, and one of the five
4
+ * extractors the adapter fans out per (function record, canonical effect
5
+ * type) pair (FR-008).
6
+ *
7
+ * `buildCallerIndex` (src/signals/caller-index.ts) answers "how many
8
+ * distinct in-scope modules call this function?" through the checker's own
9
+ * cross-module reference machinery; this rule maps that count to FR-004's
10
+ * pinned buckets:
11
+ *
12
+ * | distinct calling modules | `caller_count` weight |
13
+ * |--------------------------|-----------------------|
14
+ * | 0 | — (no signal emitted) |
15
+ * | exactly 1 | +5 |
16
+ * | 2–3 | +10 |
17
+ * | 4 or more | +15 |
18
+ *
19
+ * The table is pinned arithmetic, not tuning — the same posture as the
20
+ * confidence formula (FR-009): a bucket boundary or weight that drifts from
21
+ * data-model.md Section 7 is a bug.
22
+ *
23
+ * Evidence seam (research R12): the rule is a pure function of the count and
24
+ * the pair's identity fields — no compiler interaction, no I/O, no
25
+ * `ProgramContext` (the imports above are type-only, which is the structural
26
+ * proof). The checker machinery that produces the count lives entirely in
27
+ * caller-index.ts; the adapter passes `RecordEvidence.callerModuleCount`
28
+ * once the evidence snapshot lands (T019). The seam is what makes the rule
29
+ * exhaustively table-testable with hand-built counts
30
+ * (tests/signals/caller.test.ts, T008) — no compiler fixtures for the
31
+ * weights, exactly like the sibling pure rules (interface, visibility,
32
+ * naming, jsdoc).
33
+ *
34
+ * Zero-caller neutrality (FR-004; US3-4; spec assumption 3): a count of 0
35
+ * emits NO signal. Weight 0 carries no evidence — a 0-weight row would add
36
+ * wire noise without shifting the confidence arithmetic — and the reference
37
+ * analyzer's extractor likewise returns nothing for zero-caller counts. The
38
+ * same value carries parse-only degradation (research R7): without a
39
+ * checker the index is empty, so no `caller_count` signal exists at all —
40
+ * degradation reduces precision, never availability.
41
+ *
42
+ * Determinism (Constitution Principle I; research R1/R15): the emitted
43
+ * `reasoning` is a fixed template naming the count — a pure function of the
44
+ * matched rule and its token, stable per analyzer version and pinned by the
45
+ * test suite exactly like effect `description` strings.
46
+ */
47
+ /**
48
+ * Maps one (function record, canonical effect type) pair's distinct in-scope
49
+ * calling-module count to FR-004's `caller_count` bucket.
50
+ *
51
+ * The identity fields pass through verbatim — `functionName` is the record's
52
+ * unqualified name, `packageName` its `derivePackage` value — and the rule is
53
+ * effect-type-blind: unlike naming and documentation evidence (FR-005/
54
+ * FR-006), real usage agrees with every effect type equally, so no routing
55
+ * gate applies and `effectType` rides along as identity only.
56
+ *
57
+ * @param callerModuleCount The number of distinct in-scope modules whose
58
+ * call sites resolve to the record — `buildCallerIndex`'s per-record value
59
+ * (0 in parse-only mode), which the adapter passes as
60
+ * `RecordEvidence.callerModuleCount` once the evidence snapshot lands.
61
+ * @param functionName The record's unqualified name — the signal's
62
+ * `function` identity field.
63
+ * @param packageName The record's dotted module path — the signal's
64
+ * `package` identity field.
65
+ * @param effectType The pair's canonical effect type — the signal's
66
+ * `side_effect_type` identity field.
67
+ * @returns The `caller_count` signal for the count's bucket — weight +5 for
68
+ * exactly 1 distinct module, +10 for 2–3, +15 for 4 or more — or
69
+ * `undefined` for 0 callers: neutral evidence, never a 0-weight row.
70
+ * @throws Error when `callerModuleCount` is not a non-negative integer — a
71
+ * value the caller index cannot produce; an upstream bug fails loud rather
72
+ * than silently dropping the signal.
73
+ */
74
+ export function callerSignal(callerModuleCount, functionName, packageName, effectType) {
75
+ // The index produces Set.size values — non-negative integers by
76
+ // construction — so an out-of-domain count is an upstream bug, not a
77
+ // runtime condition to absorb. Failing loud (the models.ts serializer
78
+ // posture) beats silently suppressing the caller signal, which would be a
79
+ // Principle II silent false negative.
80
+ if (!Number.isInteger(callerModuleCount) || callerModuleCount < 0) {
81
+ throw new Error(`Invalid caller module count: ${callerModuleCount} (must be a non-negative integer)`);
82
+ }
83
+ // US3-4: zero callers is neutral evidence — no signal, never a 0-weight row.
84
+ if (callerModuleCount === 0) {
85
+ return undefined;
86
+ }
87
+ // FR-004's bucket table, transcribed from data-model.md Section 7.
88
+ let weight;
89
+ if (callerModuleCount === 1) {
90
+ weight = 5;
91
+ }
92
+ else if (callerModuleCount <= 3) {
93
+ weight = 10;
94
+ }
95
+ else {
96
+ weight = 15;
97
+ }
98
+ const plural = callerModuleCount === 1 ? "" : "s";
99
+ return {
100
+ function: functionName,
101
+ package: packageName,
102
+ side_effect_type: effectType,
103
+ source: "caller_count",
104
+ weight,
105
+ reasoning: `called from ${callerModuleCount} distinct in-scope module${plural}`,
106
+ };
107
+ }
108
+ //# sourceMappingURL=caller.js.map
@@ -0,0 +1,233 @@
1
+ import { type Node } from "typescript/unstable/ast";
2
+ import type { ExtractionRecord } from "../extract.js";
3
+ import type { ProgramContext } from "../program.js";
4
+ /**
5
+ * The per-record evidence snapshot (FR-002 through FR-006 inputs; research
6
+ * R5, R12; data-model.md Section 5.1) — every classification input the pure
7
+ * extractors read besides the effect type, computed once per function
8
+ * record and never per (record, effect type) pair: evidence is
9
+ * effect-type-independent by construction, so one snapshot serves every
10
+ * signal a record's effects produce (plan.md's performance goal).
11
+ *
12
+ * This module computes BOTH halves. The mode-independent half —
13
+ * `exportedFunction`, `exportedReceiver`, `docText`, `docTags` — reads the
14
+ * parsed AST alone, identical in program and parse-only modes. The
15
+ * checker-resolved half — `exportedReturnType`, `interfaceMember`,
16
+ * `callerModuleCount` — rides the program-mode checker while the context is
17
+ * live (`buildRecordEvidence` runs all its checker traffic before
18
+ * returning), and carries the documented degradation values in parse-only
19
+ * mode (research R7's boundary: degradation reduces precision, never
20
+ * availability). The caller index is `buildCallerIndex` (T009) run once
21
+ * per snapshot over the same records and in-scope set.
22
+ *
23
+ * Constitution Principle III throughout: every value comes from the
24
+ * compiler's own surfaces — `getJSDocTags`/`getTextOfJSDocComment` for
25
+ * documentation (never a custom regex doc parser), `modifierFlags` for
26
+ * export modifiers, the AST parent chain for the CJS assignment shapes,
27
+ * `getSymbolAtLocation`/`getAliasedSymbol`/`getMembers` for heritage and
28
+ * return-type resolution, and the compiler's own `escapeLeadingUnderscores`
29
+ * for the member-table key spelling. Principle II where the checker cannot
30
+ * decide: the CJS binding match is name-based within the file, and every
31
+ * unresolved reference — an invalid `extends`-an-interface clause, an
32
+ * unresolvable type name, an unknown alias target — is absence of evidence,
33
+ * never an error. Principle V: nothing here executes or imports analyzed
34
+ * code — every read is off the parsed AST or a checker query.
35
+ *
36
+ * API-safety record (typescript 7.0.2, verified by probe):
37
+ * `checker.getAliasedSymbol` PANICS the native compiler server when handed
38
+ * a non-alias symbol ("Should only get alias here"), and the heritage
39
+ * clause-type node itself resolves nothing — only its `.expression` does.
40
+ * Both facts are load-bearing below: the alias gate in
41
+ * `resolveThroughAliases` is a hard precondition, never a stylistic
42
+ * choice, and the heritage walk queries the clause type's expression.
43
+ *
44
+ * Exportedness contract (research R5; data-model.md Section 5.1):
45
+ *
46
+ * 1. **ESM** — an `export` modifier on the declaration itself, on its root
47
+ * variable statement, or an export assignment above it. `export default`
48
+ * qualifies through both of the first two routes: the ExportDefault
49
+ * modifier flag is a composite that includes the Export bit, and the
50
+ * anonymous-expression form sits directly under the ExportAssignment.
51
+ * `export =` fires through the same assignment shape.
52
+ * 2. **CJS** (spec edge case 5) — a `module.exports.<name> = X`,
53
+ * `module.exports = X`, or `exports.<name> = X` assignment whose
54
+ * right-hand side is the identifier of the binding the record is rooted
55
+ * at, in the same file — and only when the walk ended at module scope:
56
+ * the right-hand side resolves in the module frame, so a function-local
57
+ * namesake of a targeted binding is a different binding sharing the
58
+ * spelling, never an export. The targeted names are scanned once per
59
+ * file and memoized for the build.
60
+ * 3. **The value-position chain** crosses exactly the shapes extract.ts's
61
+ * name-giving table reads (variable binding, property assignment,
62
+ * object-literal passthrough, export assignment) because a record only
63
+ * exists at those shapes — a wrapped initializer (parens, `as`,
64
+ * `satisfies`) is never extracted, so walk and extraction coincide by
65
+ * construction. Object literals are crossed so an object-literal member
66
+ * rooted at an exported binding is itself exported (R5's rooted-object
67
+ * rule); every other parent — class bodies, blocks, source files — ends
68
+ * the walk, so a nested function never inherits a module-level export.
69
+ * 4. **The component split** — class-member records (methods, constructors,
70
+ * accessors, and class-property initializers) never take
71
+ * `exportedFunction`; they take `exportedReceiver` when the enclosing
72
+ * class-like satisfies the same closed exported-value rule, class
73
+ * expressions through their root binding included.
74
+ *
75
+ * Documentation contract (data-model.md Section 5.1): `docTags` is the
76
+ * compiler's own `getJSDocTags` list — bare tag names in document order,
77
+ * parent-node inheritance included (variable-declaration unification); the
78
+ * JSDoc nodes behind `docText` come from mirroring the compiler's hosting
79
+ * walk (see `nextJSDocHost`), which stops at object literals and class
80
+ * bodies exactly as the compiler does — a documented statement does not
81
+ * document its object-literal members, and a documented class does not
82
+ * document its methods.
83
+ *
84
+ * Interface-heritage contract (research R6; program mode only): a
85
+ * class-member record's `interfaceMember` is true exactly when a heritage
86
+ * base of the enclosing class-like resolves through the checker to an
87
+ * interface whose DECLARED member table carries the record's escaped name.
88
+ * The route: `checker.getSymbolAtLocation` on each clause type's
89
+ * EXPRESSION (verified — the clause-type node itself resolves nothing),
90
+ * aliases followed to the base symbol, the base counted only when one of
91
+ * its declarations is an `InterfaceDeclaration` — the declaration-kind
92
+ * gate that keeps a resolvable CLASS base from firing, through either
93
+ * heritage token — and membership read from `getMembers()`, the declared
94
+ * table, never apparent-member lookup (which would find `Object`-prototype
95
+ * members and manufacture +30 signals on every `toString`-alike; research
96
+ * R6's pinned hazard). An invalid `extends`-an-interface clause resolves
97
+ * through no checker route and contributes nothing. Structural
98
+ * satisfaction (an object literal contextually typed as an interface) is
99
+ * out: no enclosing class, no declared contract.
100
+ *
101
+ * Return-type contract (research R5; program mode only): `exportedReturnType`
102
+ * is true exactly when the record node's own explicit return-type annotation
103
+ * contains at least one type reference resolving through the checker
104
+ * (aliases followed) to a symbol with at least one exported or ambient
105
+ * declaration — exported is the export modifier on the declaration (the
106
+ * same ESM signal as `exportedFunction`'s modifier route), ambient is a
107
+ * `.d.ts` declaration (the platform libraries, whose members carry no
108
+ * export modifier — verified). Primitives, literal types, and
109
+ * object-literal types are not identifier-based type references; an
110
+ * unresolved name resolves to no symbol or the checker's error symbols,
111
+ * which carry no declarations — none of them fire.
112
+ */
113
+ /**
114
+ * One flattened JSDoc tag (data-model.md Section 5.1): the bare tag name
115
+ * (`returns`, never `@returns`) and the tag's comment text as
116
+ * `getTextOfJSDocComment` renders it — the empty string when the tag carries
117
+ * no comment. Document order is preserved by the `docTags` array.
118
+ */
119
+ export interface DocTag {
120
+ /** The bare tag name — `param`, `returns` — never spelled with `@`. */
121
+ readonly tag: string;
122
+ /** The tag's comment text; the empty string when the tag has no comment. */
123
+ readonly text: string;
124
+ }
125
+ /**
126
+ * The per-record evidence snapshot (data-model.md Section 5.1) — the pure
127
+ * extractors' only input besides the effect type (research R12's seam).
128
+ *
129
+ * Immutable value record (convention pack AP-004): `readonly` fields, never
130
+ * mutated after construction. Keyed externally by the record reference this
131
+ * snapshot carries, so lookups and fan-out can use either.
132
+ */
133
+ export interface RecordEvidence {
134
+ /** The extracted function-like this snapshot describes — the map key by reference. */
135
+ readonly record: ExtractionRecord;
136
+ /**
137
+ * The record's value is reachable from module scope: an ESM export form
138
+ * (modifier on the declaration, its root variable statement, or its export
139
+ * assignment — `export default` included) or a same-file CJS assignment
140
+ * targeting its binding. Never true for class members — they take
141
+ * `exportedReceiver`.
142
+ */
143
+ readonly exportedFunction: boolean;
144
+ /**
145
+ * The annotated return type contains a type reference resolving to an
146
+ * exported or ambient symbol (research R5). Checker-resolved: false
147
+ * whenever no checker is live — always in parse-only mode.
148
+ */
149
+ readonly exportedReturnType: boolean;
150
+ /**
151
+ * The record is a direct member of a class-like that is itself exported —
152
+ * the same closed rule as `exportedFunction`, applied to the enclosing
153
+ * class (class expressions through their root binding included).
154
+ */
155
+ readonly exportedReceiver: boolean;
156
+ /**
157
+ * The flattened JSDoc text — description and tag comment texts,
158
+ * single-space joined — from the declaration's own JSDoc through the
159
+ * compiler's hosting walk; the empty string when undocumented.
160
+ */
161
+ readonly docText: string;
162
+ /** The JSDoc tags in document order, bare tag names, inheritance included. */
163
+ readonly docTags: readonly DocTag[];
164
+ /**
165
+ * A declared member of an interface in the enclosing class's heritage
166
+ * carries the record's name (research R6). Checker-resolved: false
167
+ * whenever no checker is live — always in parse-only mode.
168
+ */
169
+ readonly interfaceMember: boolean;
170
+ /**
171
+ * The number of distinct in-scope modules with at least one qualifying
172
+ * call site (research R7) — `buildCallerIndex`'s count for the record,
173
+ * wired here and never recomputed; 0 in parse-only mode (the empty
174
+ * index).
175
+ */
176
+ readonly callerModuleCount: number;
177
+ }
178
+ /**
179
+ * Builds the per-record evidence snapshot over one prepared analysis
180
+ * (research R12's seam).
181
+ *
182
+ * Both halves compute here, each once per record: the mode-independent
183
+ * components (both exportedness components and both documentation fields)
184
+ * from the parsed AST, and the checker-resolved components
185
+ * (`exportedReturnType`, `interfaceMember`) through the live program-mode
186
+ * checker. The caller index is `buildCallerIndex` (T009) run once per
187
+ * build, over the same records and in-scope set — its count lands on the
188
+ * snapshot verbatim, never recomputed per record. In parse-only mode there
189
+ * is no checker: the checker-resolved components carry their degradation
190
+ * values (false/0) and the caller index is empty, so degradation reduces
191
+ * precision, never availability.
192
+ *
193
+ * Every value is computed once per record, and the result is a pure
194
+ * function of the live inputs: repeated calls over the same context,
195
+ * records, and in-scope set produce equal snapshots. An empty record list
196
+ * yields the empty map.
197
+ *
198
+ * @param context The bootstrapped analysis context — the program-mode arm
199
+ * must still be live (every checker query and the caller index's
200
+ * `getSignatureUsage` traffic round-trip to the native compiler server);
201
+ * the parse-only arm owns nothing to keep alive.
202
+ * @param records The extracted function-likes, as `prepareAnalysis`
203
+ * returned them — the same instances the caller will look up by.
204
+ * @param inScopePaths The pattern-filtered, test-excluded source list from
205
+ * `PreparedAnalysis.inScopePaths` — the identical file set the caller index
206
+ * filters call sites by (US3-7's parity).
207
+ * @param rootPath The realpath-resolved analysis root from
208
+ * `PreparedAnalysis.rootPath` — the base the caller index relativizes
209
+ * call-site file names against.
210
+ * @returns Every record mapped to its evidence snapshot, keyed by the
211
+ * record reference.
212
+ */
213
+ export declare function buildRecordEvidence(context: ProgramContext, records: readonly ExtractionRecord[], inScopePaths: readonly string[], rootPath: string): ReadonlyMap<ExtractionRecord, RecordEvidence>;
214
+ /**
215
+ * The next node up whose attached JSDoc the compiler considers hosted by
216
+ * `node` — a faithful mirror of the compiler's own
217
+ * `getNextJSDocCommentLocation` (typescript/dist/ast/jsdoc.js): the
218
+ * comment-bearing owners are property assignments, export assignments,
219
+ * property declarations, variable declarations, satisfies expressions,
220
+ * return statements, variable statements, and expression statements, plus a
221
+ * variable declaration list only when `node` is its first declaration.
222
+ * Every other parent — object literals, class bodies, source files — ends
223
+ * the walk.
224
+ *
225
+ * Exported for doc-coverage's symbol walk — a pure widening, the
226
+ * collectSources precedent: both pipelines read JSDoc attachment through
227
+ * this one hosting surface, so their semantics cannot drift.
228
+ *
229
+ * @param node The node whose hosting parent to find.
230
+ * @returns The parent that hosts JSDoc for `node`, or undefined when the
231
+ * walk ends.
232
+ */
233
+ export declare function nextJSDocHost(node: Node): Node | undefined;