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.
Files changed (72) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +11 -0
  3. package/README.md +301 -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 +63 -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,543 @@
1
+ /**
2
+ * The `doc_coverage` method (spec 005, FR-001 to FR-004 and FR-011;
3
+ * research R1-R4): the documented-symbol surface walk, its wire record,
4
+ * and the serializer that emits it.
5
+ *
6
+ * The walk enumerates the exported source-level symbols of an analysis
7
+ * scope and reports, per symbol, whether a JSDoc comment is attached
8
+ * (FR-002) and the first summary line of its description (FR-003). The
9
+ * symbol surface (FR-002): exported function declarations (anonymous
10
+ * `export default function` included), exported variable declarations —
11
+ * a const with a function-like initializer routes to "function", any
12
+ * other const to "constant", let and var to "variable" — exported
13
+ * classes with their public method members (constructors, methods,
14
+ * accessors), and exported type, interface, and enum declarations. An
15
+ * `export default` naming an identifier emits the rows of the
16
+ * module-level declaration it names, under that declaration's own name,
17
+ * kind, and documentation.
18
+ *
19
+ * Boundary forms emit no row by design, never by silent omission
20
+ * (Principle II): non-exported declarations; export clauses and
21
+ * re-exports, whose rows belong to the declaration's own export
22
+ * spelling; default exports of expressions (arrows, literals, object
23
+ * literals), which name no declaration; `export =` and `module.exports`,
24
+ * the CommonJS boundaries; destructuring binding patterns, which bind
25
+ * many spellings to no single named symbol; namespaces and their
26
+ * members; body-less overload and ambient signatures, whose
27
+ * implementation signature is the symbol's documentation site; and
28
+ * non-public class members (private, protected, #private).
29
+ *
30
+ * Constitution alignment:
31
+ *
32
+ * - Principle I (Protocol Fidelity): rows serialize to the doc_coverage
33
+ * wire shape — name, package, kind, file, line, documented, and
34
+ * doc_snippet only when a summary line exists — in the canonical
35
+ * sorted-key order the protocol layer's own serializer produces, so
36
+ * its pass is a structural no-op (the serializeComplexityEntry
37
+ * discipline).
38
+ * - Principle III (TypeScript-Native Analysis): JSDoc attachment is read
39
+ * from the compiler's jsDoc arrays and the parent-hosting walk
40
+ * evidence.ts mirrors from the compiler's own
41
+ * `getNextJSDocCommentLocation` (nextJSDocHost — one shared hosting
42
+ * surface, so the doc and signal pipelines cannot drift), and comment
43
+ * text through the compiler's own `getTextOfJSDocComment` renderer —
44
+ * no custom doc parser. Verified against typescript 7.0.2: the
45
+ * renderer strips the ` * ` line decoration and per-line leading
46
+ * whitespace, preserves interior newlines, and keeps block tags out of
47
+ * the description — so FR-003's leading-`*` trim is discharged by the
48
+ * compiler surface itself, and stripping another asterisk here would
49
+ * mangle content asterisks the renderer deliberately preserves (a
50
+ * `**bold**` summary line survives verbatim).
51
+ * - Principle IV (Testability): the walk core is a pure
52
+ * PreparedAnalysis-to-SymbolRow[] function; the entry owns the
53
+ * program-context lifetime and closes it in a finally, exactly as
54
+ * computeComplexity does, so no native compiler server outlives one
55
+ * analysis.
56
+ * - Principle V (Analysis Safety): static analysis only — the walk reads
57
+ * the compiler's AST and never executes analyzed source.
58
+ */
59
+ import { getTextOfJSDocComment, isArrowFunction, isClassDeclaration, isConstructorDeclaration, isEnumDeclaration, isExportAssignment, isFunctionDeclaration, isFunctionExpression, isGetAccessorDeclaration, isIdentifier, isInterfaceDeclaration, isJSDoc, isMethodDeclaration, isPrivateIdentifier, isSetAccessorDeclaration, isTypeAliasDeclaration, isVariableDeclaration, isVariableStatement, ModifierFlags, NodeFlags, } from "typescript/unstable/ast";
60
+ import { prepareAnalysis } from "./analysis.js";
61
+ import { collectSources, propertyNameText } from "./extract.js";
62
+ import { compareFileLineName } from "./internal.js";
63
+ import { derivePackage } from "./models.js";
64
+ import { nextJSDocHost } from "./signals/evidence.js";
65
+ /**
66
+ * The `doc_coverage` method entry (FR-001 to FR-004; the analyze-shaped
67
+ * surface T018 established): the serialized SymbolRow array — one row
68
+ * per in-scope symbol of the FR-002 surface — assembled in the (file,
69
+ * line, name) total order and mapped through serializeSymbolRow so
70
+ * every element of the returned array is wire-final.
71
+ *
72
+ * The returned array — not a {symbols: [...]} wrapper — follows the
73
+ * computeComplexity split: the T009 server handler wraps the array into
74
+ * the doc_coverage result shape, exactly as it wraps the complexity
75
+ * entries. Both this entry and its payload are deterministic pure
76
+ * functions of the analyzed tree (Constitution Principle I).
77
+ *
78
+ * The program-mode context's lifetime is this handler's — closed in the
79
+ * finally whatever the walk outcome, exactly as analyze and
80
+ * computeComplexity close theirs; a parse-only context owns nothing to
81
+ * dispose.
82
+ *
83
+ * @param rootPath Absolute or relative project root; validated and
84
+ * realpath-resolved by prepareAnalysis (an invalid root throws
85
+ * InvalidRootPathError, which the server maps to -32602).
86
+ * @param patterns The raw patterns parameter value; shape-validated by
87
+ * the server (-32602) before analysis runs, normalized here.
88
+ * @param stderr Injected diagnostic sink (Testability principle);
89
+ * defaults to process.stderr. Stdout is never touched.
90
+ * @returns The serialized symbol rows, (file, line, name)-sorted.
91
+ * @throws InvalidRootPathError when the root does not exist, is not a
92
+ * directory, or cannot be accessed.
93
+ */
94
+ export async function docCoverage(rootPath, patterns, stderr = process.stderr) {
95
+ const prepared = await prepareAnalysis(rootPath, patterns, stderr);
96
+ try {
97
+ const rows = collectSymbolRows(prepared);
98
+ rows.sort(compareFileLineName);
99
+ return rows.map(serializeSymbolRow);
100
+ }
101
+ finally {
102
+ if (prepared.context.mode === "program") {
103
+ prepared.context.dispose();
104
+ }
105
+ }
106
+ }
107
+ /**
108
+ * Serializes a SymbolRow to its wire shape (FR-001, FR-003): exactly the
109
+ * six required fields, plus doc_snippet when set — omit-unset-optionals,
110
+ * the serializeSignal reasoning idiom — never null, never an empty
111
+ * string.
112
+ *
113
+ * Keys are emitted in the canonical sorted order the protocol layer's
114
+ * code-unit key sort produces (doc_snippet first when present — '_'
115
+ * sorts before 'u'), so that pass is a structural no-op: the
116
+ * serializeComplexityEntry discipline.
117
+ *
118
+ * Validation is the deliberate blend of the two house serializer
119
+ * patterns: a pure projection for fields whose validity is structural at
120
+ * this module's single walk construction site (the
121
+ * serializeComplexityEntry argument — no per-field revalidation of
122
+ * names, paths, lines, or booleans), plus the closed-set and
123
+ * never-empty guards the wire contract pins independently of
124
+ * construction (the AssertionKind and omit-unset-optionals patterns):
125
+ * kind membership in SymbolKind's set, and a present-but-empty
126
+ * doc_snippet fails loud rather than reaching the wire.
127
+ *
128
+ * @param row The symbol row to serialize.
129
+ * @returns A JSON-ready object with the wire fields in canonical order.
130
+ * @throws Error when kind is outside the closed set or doc_snippet is
131
+ * present but empty.
132
+ */
133
+ export function serializeSymbolRow(row) {
134
+ if (!SYMBOL_KINDS.has(row.kind)) {
135
+ throw new Error(`Invalid symbol kind: ${row.kind}`);
136
+ }
137
+ const serialized = {
138
+ documented: row.documented,
139
+ file: row.file,
140
+ kind: row.kind,
141
+ line: row.line,
142
+ name: row.name,
143
+ package: row.package,
144
+ };
145
+ if (row.doc_snippet !== undefined) {
146
+ if (row.doc_snippet.length === 0) {
147
+ throw new Error("Invalid doc_snippet: must be a non-empty string when present");
148
+ }
149
+ return { doc_snippet: row.doc_snippet, ...serialized };
150
+ }
151
+ return serialized;
152
+ }
153
+ /**
154
+ * The SymbolKind closed set (FR-001), held as a Set for membership so
155
+ * lookups consult own data only, never the prototype chain — the
156
+ * ASSERTION_KINDS and SIGNAL_SOURCES pattern. Constructed with the
157
+ * SymbolKind element type so a misspelled member is a compile error.
158
+ * Module-private: T025's public surface ships docCoverage and the row
159
+ * types, not the set.
160
+ */
161
+ const SYMBOL_KINDS = new Set([
162
+ "function",
163
+ "type",
164
+ "constant",
165
+ "variable",
166
+ "class",
167
+ "method",
168
+ ]);
169
+ /**
170
+ * The symbol-surface walk (FR-002): the rows for every documented-symbol
171
+ * surface member of the in-scope sources.
172
+ *
173
+ * Scope narrowing (FR-004): program mode can pull pattern-excluded
174
+ * in-root transitive imports into the program — the behavior
175
+ * analysis.ts documents for its own record narrowing — so the
176
+ * collectSources pairs are filtered by prepared.inScopePaths. The walk
177
+ * reports exactly the files the patterns select, never the imports they
178
+ * drag in: file-set parity with analyze stays structural (research R2;
179
+ * the parity T003's export widening was cut for).
180
+ *
181
+ * @param prepared The shared analysis preparation (root, context, scope).
182
+ * @returns The symbol rows; docCoverage sorts them into wire order.
183
+ */
184
+ function collectSymbolRows(prepared) {
185
+ const rows = [];
186
+ const inScope = new Set(prepared.inScopePaths);
187
+ const sources = collectSources(prepared.rootPath, prepared.context);
188
+ for (const [relPath, sourceFile] of sources) {
189
+ if (!inScope.has(relPath)) {
190
+ continue;
191
+ }
192
+ collectFileRows(relPath, sourceFile, rows);
193
+ }
194
+ return rows;
195
+ }
196
+ /**
197
+ * One file's rows, in two passes (FR-002's identifier-default rule).
198
+ *
199
+ * Pass one walks the module-level statements: every exported declaration
200
+ * emits its rows. Pass two resolves `export default` naming an
201
+ * identifier — the only default form that names a declaration — against
202
+ * an index of the file's module-level declarations, emitting the
203
+ * referenced declaration's rows under its own name, kind, and
204
+ * documentation (a default export with a declared name uses the name).
205
+ * The emitted set deduplicates: a declaration both exported and named by
206
+ * a default export produces one row set, and a declaration whose
207
+ * routing emits nothing (an overload signature) emits nothing in either
208
+ * pass — the body gate lives in the shared routing, not the export
209
+ * spelling.
210
+ *
211
+ * Determinism: both passes iterate statement order, and the final
212
+ * (file, line, name) sort makes append order immaterial to the wire.
213
+ *
214
+ * @param relPath Root-relative POSIX path of the file.
215
+ * @param sourceFile The parsed source.
216
+ * @param rows The accumulator rows are appended to.
217
+ */
218
+ function collectFileRows(relPath, sourceFile, rows) {
219
+ const emitted = new Set();
220
+ const declarations = indexModuleDeclarations(sourceFile);
221
+ const defaulted = [];
222
+ for (const statement of sourceFile.statements) {
223
+ if (isExportedDeclaration(statement)) {
224
+ emitSymbolRows(statement, relPath, sourceFile, rows, emitted);
225
+ continue;
226
+ }
227
+ if (isExportAssignment(statement) &&
228
+ !statement.isExportEquals &&
229
+ isIdentifier(statement.expression)) {
230
+ defaulted.push(statement.expression.text);
231
+ }
232
+ }
233
+ for (const name of defaulted) {
234
+ const declaration = declarations.get(name);
235
+ if (declaration !== undefined && !emitted.has(declaration)) {
236
+ emitSymbolRows(declaration, relPath, sourceFile, rows, emitted);
237
+ }
238
+ }
239
+ }
240
+ /**
241
+ * Name-to-declaration index of a file's module-level statements — the
242
+ * lookup surface `export default` naming an identifier resolves against.
243
+ * Names come from the declarations themselves (function, class, type
244
+ * alias, interface, enum, and identifier-bound variable declarations);
245
+ * the last statement of a duplicate name wins, matching the overload-set
246
+ * convention that the implementation signature follows its overloads —
247
+ * and the body gate in emitSymbolRows, not the index, decides what a
248
+ * looked-up overload set emits.
249
+ *
250
+ * @param sourceFile The parsed source.
251
+ * @returns The declarations keyed by declared name.
252
+ */
253
+ function indexModuleDeclarations(sourceFile) {
254
+ const declarations = new Map();
255
+ for (const statement of sourceFile.statements) {
256
+ if (isFunctionDeclaration(statement) && statement.name !== undefined) {
257
+ declarations.set(statement.name.text, statement);
258
+ continue;
259
+ }
260
+ if (isClassDeclaration(statement) && statement.name !== undefined) {
261
+ declarations.set(statement.name.text, statement);
262
+ continue;
263
+ }
264
+ if (isTypeAliasDeclaration(statement) ||
265
+ isInterfaceDeclaration(statement) ||
266
+ isEnumDeclaration(statement)) {
267
+ declarations.set(statement.name.text, statement);
268
+ continue;
269
+ }
270
+ if (isVariableStatement(statement)) {
271
+ for (const declaration of statement.declarationList.declarations) {
272
+ if (isIdentifier(declaration.name)) {
273
+ declarations.set(declaration.name.text, declaration);
274
+ }
275
+ }
276
+ }
277
+ }
278
+ return declarations;
279
+ }
280
+ /**
281
+ * Whether a statement is one of the declaration forms the FR-002 surface
282
+ * routes, carrying the Export modifier — the composite flag whose bits
283
+ * cover both `export` and `export default` (verified against
284
+ * typescript 7.0.2: ExportDefault is Export | Default), so one test
285
+ * admits both spellings. The export-modifier shape extends
286
+ * evidence.ts's hasExportModifier with the type, interface, and enum
287
+ * declaration forms the signal pipeline never reads.
288
+ *
289
+ * @param statement The module-level statement to test.
290
+ * @returns True when the statement is an exported routed declaration.
291
+ */
292
+ function isExportedDeclaration(statement) {
293
+ return ((isFunctionDeclaration(statement) ||
294
+ isClassDeclaration(statement) ||
295
+ isVariableStatement(statement) ||
296
+ isTypeAliasDeclaration(statement) ||
297
+ isInterfaceDeclaration(statement) ||
298
+ isEnumDeclaration(statement)) &&
299
+ (statement.modifierFlags & ModifierFlags.Export) !== 0);
300
+ }
301
+ /**
302
+ * FR-002's routing table: the rows one module-level declaration emits.
303
+ * Both file passes converge here — an exported statement and the
304
+ * declaration an `export default` names route identically, so a symbol's
305
+ * row never depends on which spelling exported it.
306
+ *
307
+ * Routing: a VariableStatement fans out to its declarations (a
308
+ * multi-declarator statement exports each); a function declaration with
309
+ * a body emits a "function" row — an anonymous one is grammatically only
310
+ * `export default function`, so FR-001's literal "default" names it —
311
+ * while a body-less overload or ambient signature emits none, the
312
+ * implementation signature being the symbol's documentation site; a
313
+ * variable declaration with an identifier name emits a row whose kind
314
+ * the declaration keyword and initializer shape decide (variableRowKind)
315
+ * — a destructuring pattern binds many spellings to no single symbol
316
+ * and emits none; a class declaration emits a "class" row plus its
317
+ * public method members' rows; a type alias, interface, or enum emits a
318
+ * single "type" row — interface and enum members are not symbols of the
319
+ * documentation surface. Any other node emits nothing.
320
+ *
321
+ * @param node The statement or declaration to route.
322
+ * @param relPath Root-relative POSIX path of the declaring file.
323
+ * @param sourceFile The declaring source (line math, member names).
324
+ * @param rows The accumulator rows are appended to.
325
+ * @param emitted The declarations that have emitted; the dedup surface
326
+ * for the identifier-default pass.
327
+ */
328
+ function emitSymbolRows(node, relPath, sourceFile, rows, emitted) {
329
+ if (isVariableStatement(node)) {
330
+ for (const declaration of node.declarationList.declarations) {
331
+ emitSymbolRows(declaration, relPath, sourceFile, rows, emitted);
332
+ }
333
+ return;
334
+ }
335
+ if (isFunctionDeclaration(node)) {
336
+ if (node.body !== undefined) {
337
+ emitted.add(node);
338
+ rows.push(symbolRow(relPath, sourceFile, node, node.name?.text ?? "default", "function"));
339
+ }
340
+ return;
341
+ }
342
+ if (isVariableDeclaration(node)) {
343
+ if (isIdentifier(node.name)) {
344
+ emitted.add(node);
345
+ rows.push(symbolRow(relPath, sourceFile, node, node.name.text, variableRowKind(node)));
346
+ }
347
+ return;
348
+ }
349
+ if (isClassDeclaration(node)) {
350
+ emitted.add(node);
351
+ rows.push(symbolRow(relPath, sourceFile, node, node.name?.text ?? "default", "class"));
352
+ emitClassMemberRows(node, relPath, sourceFile, rows);
353
+ return;
354
+ }
355
+ if (isTypeAliasDeclaration(node) ||
356
+ isInterfaceDeclaration(node) ||
357
+ isEnumDeclaration(node)) {
358
+ emitted.add(node);
359
+ rows.push(symbolRow(relPath, sourceFile, node, node.name.text, "type"));
360
+ }
361
+ }
362
+ /**
363
+ * Whether a class element is one of the method-like declarations.
364
+ *
365
+ * @param member The class element to test.
366
+ * @returns True when the element declares a method-like member.
367
+ */
368
+ function isClassMethodLike(member) {
369
+ return (isMethodDeclaration(member) ||
370
+ isConstructorDeclaration(member) ||
371
+ isGetAccessorDeclaration(member) ||
372
+ isSetAccessorDeclaration(member));
373
+ }
374
+ /**
375
+ * The public method surface of one exported class (FR-002): methods,
376
+ * constructors, and accessors emit "method" rows; every other class
377
+ * element — property declarations, index signatures, static blocks —
378
+ * emits none.
379
+ *
380
+ * Exclusion is the accessibility contract, not a heuristic: the
381
+ * private/protected modifier bits and #private names (PrivateIdentifier
382
+ * — which carries no modifier bit at all, verified against typescript
383
+ * 7.0.2, so the name's node kind is the only reliable test) mark
384
+ * members no consumer of the exported surface can see. Body-less
385
+ * members emit only when abstract: an overload signature has no
386
+ * documentation site of its own (its implementation is the symbol), but
387
+ * an abstract method is the declaration of the symbol itself. Static
388
+ * public methods are part of the exported surface and included. A
389
+ * getter/setter pair is two declarations and emits two rows, one per
390
+ * accessor line. Constructors have no name node — the literal
391
+ * "constructor" is the row name; other members take their source
392
+ * spelling through propertyNameText, the same identity surface
393
+ * ExtractionRecord uses, so doc rows and extraction records agree.
394
+ *
395
+ * @param cls The exported class declaration.
396
+ * @param relPath Root-relative POSIX path of the declaring file.
397
+ * @param sourceFile The declaring source (line math, member names).
398
+ * @param rows The accumulator rows are appended to.
399
+ */
400
+ function emitClassMemberRows(cls, relPath, sourceFile, rows) {
401
+ for (const member of cls.members) {
402
+ if (!isClassMethodLike(member)) {
403
+ continue;
404
+ }
405
+ if ((member.modifierFlags & ModifierFlags.NonPublicAccessibilityModifier) !== 0) {
406
+ continue;
407
+ }
408
+ if (!isConstructorDeclaration(member) && isPrivateIdentifier(member.name)) {
409
+ continue;
410
+ }
411
+ if (member.body === undefined &&
412
+ (member.modifierFlags & ModifierFlags.Abstract) === 0) {
413
+ continue;
414
+ }
415
+ const name = isConstructorDeclaration(member)
416
+ ? "constructor"
417
+ : propertyNameText(member.name, sourceFile);
418
+ rows.push(symbolRow(relPath, sourceFile, member, name, "method"));
419
+ }
420
+ }
421
+ /**
422
+ * FR-002's kind routing for one variable declaration, decided by the
423
+ * declaration keyword and the initializer's shape: let and var are
424
+ * reassignable bindings — "variable" — regardless of initializer (the
425
+ * table routes by keyword); a const with a function-like initializer
426
+ * (arrow or function expression, generator expressions included — one
427
+ * node kind) names a function — "function"; any other const is a value
428
+ * — "constant". The keyword comes from the declaration list's NodeFlags
429
+ * (Const = 2, verified against typescript 7.0.2), the same flag surface
430
+ * the compiler's own binder reads.
431
+ *
432
+ * @param declaration The variable declaration to classify.
433
+ * @returns The row kind: function, constant, or variable.
434
+ */
435
+ function variableRowKind(declaration) {
436
+ if ((declaration.parent.flags & NodeFlags.Const) === 0) {
437
+ return "variable";
438
+ }
439
+ const initializer = declaration.initializer;
440
+ if (initializer !== undefined &&
441
+ (isArrowFunction(initializer) || isFunctionExpression(initializer))) {
442
+ return "function";
443
+ }
444
+ return "constant";
445
+ }
446
+ /**
447
+ * Builds one symbol row: identity from the arguments, position from the
448
+ * node's first token, and the documentation fact from the JSDoc
449
+ * attached to the node (attachedJsDocs).
450
+ *
451
+ * The snippet owner is the LAST attached JSDoc — the compiler's own
452
+ * ownership convention (its filterOwnedJSDocTags resolves multiple
453
+ * comments on one node to the final one) — while `documented` reflects
454
+ * any attachment, so a tags-only final comment still reports
455
+ * documented: true with the snippet omitted (FR-003's tags-only edge).
456
+ *
457
+ * @param relPath Root-relative POSIX path of the declaring file.
458
+ * @param sourceFile The declaring source (line math).
459
+ * @param node The declaration the row describes.
460
+ * @param name The symbol's row name.
461
+ * @param kind The symbol's row kind.
462
+ * @returns The SymbolRow for the declaration.
463
+ */
464
+ function symbolRow(relPath, sourceFile, node, name, kind) {
465
+ const jsDocs = attachedJsDocs(node);
466
+ const owner = jsDocs.at(-1);
467
+ const docSnippet = owner !== undefined ? docSnippetOf(owner) : undefined;
468
+ return {
469
+ name,
470
+ package: derivePackage(relPath),
471
+ kind,
472
+ file: relPath,
473
+ line: sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1,
474
+ documented: jsDocs.length > 0,
475
+ ...(docSnippet !== undefined ? { doc_snippet: docSnippet } : {}),
476
+ };
477
+ }
478
+ /**
479
+ * The JSDoc comments attached to a declaration, read from the node's own
480
+ * jsDoc array — the same compiler surface evidence.ts reads — and then
481
+ * from each hosting parent up the compiler's parent-hosting chain
482
+ * (nextJSDocHost), innermost first. The hosting chain is what makes a
483
+ * variable declaration's statement comment its documentation: the
484
+ * compiler attaches a statement-leading comment to the statement and
485
+ * considers it hosted by the declaration. For every other routed shape
486
+ * — statements, class members, type declarations — the walk ends at the
487
+ * enclosing source file or class body (evidence.ts documents the same
488
+ * stop), so their rows read their own comments alone.
489
+ *
490
+ * @param node The declaration whose attached comments are read.
491
+ * @returns The attached JSDoc comments, in attachment order.
492
+ */
493
+ function attachedJsDocs(node) {
494
+ const docs = [];
495
+ let current = node;
496
+ while (current !== undefined) {
497
+ for (const attached of current.jsDoc ?? []) {
498
+ if (isJSDoc(attached)) {
499
+ docs.push(attached);
500
+ }
501
+ }
502
+ current = nextJSDocHost(current);
503
+ }
504
+ return docs;
505
+ }
506
+ /**
507
+ * The FR-003 snippet rule over one rendered JSDoc description: the first
508
+ * non-empty line that is not a block-tag line (a line whose first token
509
+ * begins with @), trimmed of surrounding whitespace; undefined when no
510
+ * line qualifies, so the row omits the key (never an empty string).
511
+ *
512
+ * The leading-`*` trim FR-003 names is discharged by the compiler's own
513
+ * renderer — getTextOfJSDocComment (typescript 7.0.2, verified) strips
514
+ * the ` * ` decoration and per-line leading whitespace and keeps block
515
+ * tags out of the description — so rendered lines carry no decoration
516
+ * left to strip, and stripping another asterisk here would mangle
517
+ * content asterisks the renderer preserves (a `**bold**` summary line
518
+ * must survive verbatim). The @ check is defensive for the same reason:
519
+ * the compiler parses tags — unknown tag names included — out of the
520
+ * description, so a rendered @-leading line is not expected on real
521
+ * output.
522
+ *
523
+ * @param doc The JSDoc comment whose description is scanned.
524
+ * @returns The first summary line, or undefined when none qualifies.
525
+ */
526
+ function docSnippetOf(doc) {
527
+ const text = getTextOfJSDocComment(doc.comment);
528
+ if (text === undefined) {
529
+ return undefined;
530
+ }
531
+ for (const rawLine of text.split("\n")) {
532
+ const line = rawLine.trim();
533
+ if (line.length === 0) {
534
+ continue;
535
+ }
536
+ if (line.startsWith("@")) {
537
+ continue;
538
+ }
539
+ return line;
540
+ }
541
+ return undefined;
542
+ }
543
+ //# sourceMappingURL=doc-coverage.js.map