@blundergoat/gruff-ts 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +123 -1
- package/CONTRIBUTING.md +32 -25
- package/README.md +55 -46
- package/SECURITY.md +18 -30
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -13
- package/docs/agent-hook.md +33 -24
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +173 -71
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -157
- package/docs/rules.md +140 -106
- package/package.json +10 -8
- package/src/analyser.ts +886 -282
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +85 -55
- package/src/blocks.ts +579 -364
- package/src/changed-regions.ts +414 -65
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +461 -302
- package/src/cli-program.ts +956 -166
- package/src/cli.ts +9 -2
- package/src/comment-rules.ts +277 -414
- package/src/comment-scanner.ts +363 -44
- package/src/complexity-metrics.ts +320 -0
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +778 -210
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +188 -61
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +521 -148
- package/src/doc-rules.ts +592 -147
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +240 -89
- package/src/findings.ts +48 -24
- package/src/fixture-purpose-rules.ts +226 -48
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +336 -106
- package/src/hook-contract.ts +708 -211
- package/src/init-config.ts +169 -114
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -419
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +33 -107
- package/src/parsed-script.ts +1206 -0
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +97 -71
- package/src/project-config-rules.ts +335 -146
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +284 -0
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +864 -195
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +255 -52
- package/src/security-flow-rules.ts +930 -109
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +502 -375
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +380 -148
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +422 -228
- package/src/text-scans.ts +67 -9
- package/src/types.ts +222 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -153
- package/src/test-fixtures.ts +0 -668
package/src/doc-rules.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
1
|
+
// Checks TypeScript and JavaScript documentation during `analyse` so public APIs and JSDoc tags stay accurate for reviewers.
|
|
2
|
+
//
|
|
3
|
+
// Covers `docs.missing-public-doc`, `docs.missing-file-overview`, `docs.missing-interface-doc`, and the `@param`/`@returns` tag rules.
|
|
4
|
+
// Emits findings in stable source order so terminal output, JSON reports, and baselines stay reproducible across runs.
|
|
5
5
|
import { commentTextAtLine, hasLeadingCommentBeforeLine } from "./comment-scanner.ts";
|
|
6
6
|
import { type SourceFile } from "./discovery.ts";
|
|
7
7
|
import { makeFinding } from "./findings.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { type DestructuredParameterSlot, documentedDefaultAndLaterExports, documentedExportConstants, documentedExportFunctions, documentedMethods, documentedPrivateCallables, type DocumentedExportFunction, type ParsedScript } from "./parsed-script.ts";
|
|
9
|
+
import { isTestPath } from "./project-rules.ts";
|
|
9
10
|
import { byteLine } from "./text-scans.ts";
|
|
10
11
|
import type { Finding } from "./types.ts";
|
|
11
12
|
|
|
@@ -17,15 +18,26 @@ export interface ExportedDeclaration {
|
|
|
17
18
|
line: number;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
interface
|
|
21
|
+
// One parsed `@param` tag from a docblock line, retaining its root name, optional first dotted key,
|
|
22
|
+
// full tag path (for stale-tag reporting), and description tail.
|
|
23
|
+
interface ParsedDocParamTag {
|
|
24
|
+
name: string;
|
|
25
|
+
fullName: string;
|
|
26
|
+
propertyKey: string | undefined;
|
|
27
|
+
tail: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Precomputed leading-comment + signature pair used by every docblock rule (stale-param,
|
|
31
|
+
// missing-param, missing-return) so the parser runs only once per source file.
|
|
32
|
+
interface DocumentedCallableBlock {
|
|
23
33
|
doc: string;
|
|
34
|
+
hasJsDoc: boolean;
|
|
24
35
|
name: string;
|
|
25
36
|
params: string[];
|
|
37
|
+
destructuredParameters: DestructuredParameterSlot[];
|
|
26
38
|
paramTags: string[];
|
|
39
|
+
parsedParamTags: ParsedDocParamTag[];
|
|
27
40
|
line: number;
|
|
28
|
-
returnType: string;
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
// Argument bundle shared by every docblock-related finding builder. `parameter` is optional
|
|
@@ -39,8 +51,25 @@ interface DocFindingInput {
|
|
|
39
51
|
parameter?: string;
|
|
40
52
|
}
|
|
41
53
|
|
|
42
|
-
//
|
|
43
|
-
//
|
|
54
|
+
// One `@returns` or `@param` tag in a docblock, for the bare-tag checks: its line offset from the
|
|
55
|
+
// docblock's first line and whether any description text follows the tag, its `{Type}` or its name.
|
|
56
|
+
interface DocTagOccurrence {
|
|
57
|
+
lineOffset: number;
|
|
58
|
+
isDescribed: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// One `@param` tag in a docblock, for the bare-tag checks: the occurrence plus the parameter it names.
|
|
62
|
+
interface DocParamTagOccurrence extends DocTagOccurrence {
|
|
63
|
+
name: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extracts source-ordered export matches for `class`, `interface`, `type`, `enum`, or `function` followed directly by a simple name.
|
|
68
|
+
*
|
|
69
|
+
* @param source - Original source text supplying one-based line numbers for each match.
|
|
70
|
+
* @param codeSource - Masked text aligned with `source` searched for export declarations.
|
|
71
|
+
* @returns Source-ordered export declaration records, or an empty list when nothing matches.
|
|
72
|
+
*/
|
|
44
73
|
export function exportedDeclarations(source: string, codeSource: string): ExportedDeclaration[] {
|
|
45
74
|
return [...codeSource.matchAll(/\bexport\s+(class|interface|type|enum|function)\s+([A-Za-z_$][A-Za-z0-9_$]*)/g)].map((match) => ({
|
|
46
75
|
kind: match[1] ?? "",
|
|
@@ -49,10 +78,15 @@ export function exportedDeclarations(source: string, codeSource: string): Export
|
|
|
49
78
|
}));
|
|
50
79
|
}
|
|
51
80
|
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
81
|
+
/**
|
|
82
|
+
* Check for the leading docblock required by the stable docs.missing-public-doc rule.
|
|
83
|
+
* Function checks are docs.missing-exported-function-doc, docs.missing-internal-function-doc. Interfaces use docs.missing-interface-doc.
|
|
84
|
+
*
|
|
85
|
+
* @param file - File path to attach to the finding.
|
|
86
|
+
* @param source - Text searched above the exported declaration for a docblock marker.
|
|
87
|
+
* @param declaration - Exported item to check. Functions and interfaces are skipped.
|
|
88
|
+
* @param findings - Collector for the advisory finding.
|
|
89
|
+
* @returns Nothing. Appends a finding only when an in-scope declaration lacks a leading docblock.
|
|
56
90
|
*/
|
|
57
91
|
export function pushMissingPublicDocFinding(file: SourceFile, source: string, declaration: ExportedDeclaration, findings: Finding[]): void {
|
|
58
92
|
if (declaration.kind === "function" || declaration.kind === "interface") {
|
|
@@ -76,8 +110,15 @@ export function pushMissingPublicDocFinding(file: SourceFile, source: string, de
|
|
|
76
110
|
);
|
|
77
111
|
}
|
|
78
112
|
|
|
79
|
-
|
|
80
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Examines source for a top-of-file overview at the first meaningful line after allowing for an optional shebang.
|
|
115
|
+
* Anchors the report at line 1 because the overview belongs at the top of the file.
|
|
116
|
+
*
|
|
117
|
+
* @param file - Source file whose display path anchors the finding.
|
|
118
|
+
* @param source - File content inspected for a leading overview comment.
|
|
119
|
+
* @param findings - Accumulator receiving the stable `docs.missing-file-overview` finding when absent.
|
|
120
|
+
* @returns Nothing. Appends a stable `docs.missing-file-overview` finding to `findings` when the overview is absent.
|
|
121
|
+
*/
|
|
81
122
|
export function analyseFileOverviewDoc(file: SourceFile, source: string, findings: Finding[]): void {
|
|
82
123
|
if (hasFileOverviewComment(source)) {
|
|
83
124
|
return;
|
|
@@ -97,9 +138,14 @@ export function analyseFileOverviewDoc(file: SourceFile, source: string, finding
|
|
|
97
138
|
);
|
|
98
139
|
}
|
|
99
140
|
|
|
100
|
-
|
|
101
|
-
*
|
|
102
|
-
*
|
|
141
|
+
/**
|
|
142
|
+
* Checks interface declarations, including unexported ones, for a leading maintainer comment.
|
|
143
|
+
*
|
|
144
|
+
* @param file - source file whose display path anchors the findings
|
|
145
|
+
* @param source - original text used to find line numbers and leading comments
|
|
146
|
+
* @param codeSource - masked text searched for interface declarations
|
|
147
|
+
* @param findings - accumulator for stable docs.missing-interface-doc findings
|
|
148
|
+
* @returns nothing. Appends a finding for each matched interface without a usable leading comment.
|
|
103
149
|
*/
|
|
104
150
|
export function analyseInterfaceDocs(file: SourceFile, source: string, codeSource: string, findings: Finding[]): void {
|
|
105
151
|
for (const declaration of interfaceDeclarations(source, codeSource)) {
|
|
@@ -123,8 +169,13 @@ export function analyseInterfaceDocs(file: SourceFile, source: string, codeSourc
|
|
|
123
169
|
}
|
|
124
170
|
}
|
|
125
171
|
|
|
126
|
-
|
|
127
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Finds interface headers that start a line, allowing leading indentation and a plain `export` prefix on that line.
|
|
174
|
+
*
|
|
175
|
+
* @param source - Original source text supplying line numbers for the interface documentation check.
|
|
176
|
+
* @param codeSource - Masked text searched for interface headers and same-line names.
|
|
177
|
+
* @returns Source-ordered interface declaration records for matched headers.
|
|
178
|
+
*/
|
|
128
179
|
export function interfaceDeclarations(source: string, codeSource: string): ExportedDeclaration[] {
|
|
129
180
|
return [...codeSource.matchAll(/^[ \t]*(?:export[ \t]+)?interface[ \t]+([A-Za-z_$][A-Za-z0-9_$]*)\b/gm)].map((match) => ({
|
|
130
181
|
kind: "interface",
|
|
@@ -133,114 +184,387 @@ export function interfaceDeclarations(source: string, codeSource: string): Expor
|
|
|
133
184
|
}));
|
|
134
185
|
}
|
|
135
186
|
|
|
136
|
-
|
|
137
|
-
*
|
|
138
|
-
*
|
|
187
|
+
/**
|
|
188
|
+
* The tag rules' `rules.<id>.options.everyCallable` switches, read once per file from the user's config.
|
|
189
|
+
* Both default to true; setting both false keeps the pack on documented exported function declarations.
|
|
190
|
+
*/
|
|
191
|
+
export interface DocTagRuleSwitches {
|
|
192
|
+
/** `docs.missing-param-tag`'s `everyCallable` option. */
|
|
193
|
+
isParamTagEveryCallable: boolean;
|
|
194
|
+
/** `docs.missing-return-tag`'s `everyCallable` option. */
|
|
195
|
+
isReturnTagEveryCallable: boolean;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Checks docblocks in stable order, reporting stale parameters, missing parameters, missing returns and bare tags in that order.
|
|
200
|
+
* Skips `docs.missing-param-tag`, `docs.missing-return-tag`, and extra stale-parameter checks on test files (`isTestPath`),
|
|
201
|
+
* while keeping fixture directories checked and enforcing `/** ... *\/` JSDoc blocks on `//` and `/* *\/` comments when `everyCallable` is enabled.
|
|
202
|
+
*
|
|
203
|
+
* @param file - source file whose display path anchors findings and controls test-path skipping
|
|
204
|
+
* @param findings - report accumulator that receives the docblock findings
|
|
205
|
+
* @param parsed - shared syntax tree providing the documented callables and signatures
|
|
206
|
+
* @param everyCallable - enables wider callable coverage, JSDoc enforcement on slash/block comments, and bare tag checks
|
|
207
|
+
* @returns nothing. Appends the docblock violations to findings.
|
|
139
208
|
*/
|
|
140
|
-
export function analyseDocRules(file: SourceFile,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
209
|
+
export function analyseDocRules(file: SourceFile, findings: Finding[], parsed: ParsedScript, everyCallable: DocTagRuleSwitches): void {
|
|
210
|
+
const isTestFile = isTestPath(file.displayPath);
|
|
211
|
+
for (const documentedExport of documentedBlocks(documentedExportFunctions(parsed))) {
|
|
212
|
+
if (documentedExport.hasJsDoc) {
|
|
213
|
+
pushStaleParamFindings(file, documentedExport, findings, everyCallable.isParamTagEveryCallable);
|
|
214
|
+
if (!isTestFile) {
|
|
215
|
+
pushMissingParamFindings(file, documentedExport, findings);
|
|
216
|
+
pushMissingReturnFinding(file, documentedExport, findings);
|
|
217
|
+
pushBareTagFindings(file, documentedExport, findings, everyCallable);
|
|
218
|
+
}
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (!isTestFile) {
|
|
222
|
+
if (everyCallable.isParamTagEveryCallable) {
|
|
223
|
+
pushMissingParamFindings(file, documentedExport, findings);
|
|
224
|
+
}
|
|
225
|
+
if (everyCallable.isReturnTagEveryCallable) {
|
|
226
|
+
pushMissingReturnFinding(file, documentedExport, findings);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
146
229
|
}
|
|
230
|
+
pushEveryCallableTagFindings(file, parsed, everyCallable, findings);
|
|
147
231
|
}
|
|
148
232
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Extends tag checks to documented arrows, function expressions, default and later exports, methods, and private and nested callables.
|
|
235
|
+
* Skips test files (`isTestPath`) and checks both `/** ... *\/` JSDoc blocks and non-JSDoc (`//` / `/* *\/`) leading comments on non-test source.
|
|
236
|
+
*
|
|
237
|
+
* @param file - source file whose display path anchors the findings and selects test-path skipping
|
|
238
|
+
* @param parsed - shared syntax tree used to find the additional documented callables
|
|
239
|
+
* @param everyCallable - switches that independently enable parameter and return tag checks
|
|
240
|
+
* @param findings - accumulator for violations of the tag contract
|
|
241
|
+
* @returns nothing. Appends findings for enabled checks and leaves the accumulator alone when both switches are off or the file is a test file.
|
|
242
|
+
*/
|
|
243
|
+
function pushEveryCallableTagFindings(file: SourceFile, parsed: ParsedScript, everyCallable: DocTagRuleSwitches, findings: Finding[]): void {
|
|
244
|
+
if ((!everyCallable.isParamTagEveryCallable && !everyCallable.isReturnTagEveryCallable) || isTestPath(file.displayPath)) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const callables = [...documentedExportConstants(parsed), ...documentedDefaultAndLaterExports(parsed), ...documentedMethods(parsed), ...documentedPrivateCallables(parsed)];
|
|
248
|
+
for (const block of documentedBlocks(callables)) {
|
|
249
|
+
if (block.hasJsDoc && everyCallable.isParamTagEveryCallable) {
|
|
250
|
+
pushStaleParamFindings(file, block, findings, true);
|
|
251
|
+
}
|
|
252
|
+
if (everyCallable.isParamTagEveryCallable) {
|
|
253
|
+
pushMissingParamFindings(file, block, findings);
|
|
254
|
+
}
|
|
255
|
+
if (everyCallable.isReturnTagEveryCallable) {
|
|
256
|
+
pushMissingReturnFinding(file, block, findings);
|
|
257
|
+
}
|
|
258
|
+
if (block.hasJsDoc) {
|
|
259
|
+
pushBareTagFindings(file, block, findings, everyCallable);
|
|
158
260
|
}
|
|
159
261
|
}
|
|
160
|
-
return blocks;
|
|
161
262
|
}
|
|
162
263
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
264
|
+
/**
|
|
265
|
+
* Keeps the parsed declaration details for each block, and extracts parameter tag names for the documentation checks.
|
|
266
|
+
*
|
|
267
|
+
* @param documentedCallables - Callable declarations and their documentation from the shared syntax tree.
|
|
268
|
+
* @returns One check record per input callable in the same order, or an empty array when there are none.
|
|
269
|
+
*/
|
|
270
|
+
function documentedBlocks(documentedCallables: DocumentedExportFunction[]): DocumentedCallableBlock[] {
|
|
271
|
+
return documentedCallables.map((documented) => {
|
|
272
|
+
const parsedParamTags = documented.hasJsDoc ? docParsedParamTags(documented.docText) : [];
|
|
273
|
+
return {
|
|
274
|
+
doc: documented.docText,
|
|
275
|
+
hasJsDoc: documented.hasJsDoc,
|
|
276
|
+
name: documented.name,
|
|
277
|
+
params: documented.parameterNames,
|
|
278
|
+
destructuredParameters: documented.destructuredParameters,
|
|
279
|
+
paramTags: parsedParamTags.map((tag) => tag.name),
|
|
280
|
+
parsedParamTags,
|
|
281
|
+
line: documented.line,
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Reports documented parameter names absent from the callable signature at `block`'s location.
|
|
288
|
+
* The caller checks for missing tags afterward so that a rename flags the old name first.
|
|
289
|
+
*
|
|
290
|
+
* @param file - Source file whose display path anchors each finding.
|
|
291
|
+
* @param block - Callable docblock and signature details being checked.
|
|
292
|
+
* @param findings - Accumulator receiving the stable `docs.stale-param-tag` findings.
|
|
293
|
+
* @param isNamingEveryStaleTag - Groups distinct stale names into one finding when true; otherwise emits one finding per stale tag.
|
|
294
|
+
* @returns Nothing. Appends stable findings to `findings`, or none if no tags are stale.
|
|
295
|
+
*/
|
|
296
|
+
function pushStaleParamFindings(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[], isNamingEveryStaleTag: boolean): void {
|
|
297
|
+
const staleTags = staleParamTagNames(block);
|
|
298
|
+
if (isNamingEveryStaleTag) {
|
|
299
|
+
pushEveryStaleParamFinding(file, block, findings, [...new Set(staleTags)]);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
for (const tag of staleTags) {
|
|
303
|
+
findings.push(docFinding({ ruleId: "docs.stale-param-tag", message: `Docblock for \`${block.name}\` has stale @param tag \`${tag}\`.`, file, line: block.line, symbol: block.name, parameter: tag }));
|
|
170
304
|
}
|
|
171
|
-
const doc = regexGroup(match, 1);
|
|
172
|
-
return {
|
|
173
|
-
doc,
|
|
174
|
-
name: regexGroup(match, 2),
|
|
175
|
-
params: parameterNames(regexGroup(match, 3)).map((parameter) => parameter.name),
|
|
176
|
-
paramTags: docParamTags(doc),
|
|
177
|
-
line: byteLine(source, matchStart),
|
|
178
|
-
returnType: regexGroup(match, 4).trim(),
|
|
179
|
-
};
|
|
180
305
|
}
|
|
181
306
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Evaluates all `@param` tags on one callable against named parameters and destructured parameter slots.
|
|
309
|
+
* Each destructured slot binds to at most one non-declared JSDoc object name, preferring names whose dotted keys match that slot.
|
|
310
|
+
*
|
|
311
|
+
* @param block - callable signature, destructured parameter slots, and parsed `@param` tags
|
|
312
|
+
* @returns Stale tag names (or dotted `obj.key` paths) in docblock order; empty when every tag matches the signature.
|
|
313
|
+
*/
|
|
314
|
+
function staleParamTagNames(block: DocumentedCallableBlock): string[] {
|
|
315
|
+
const declaredNames = new Set(block.params);
|
|
316
|
+
const boundSlots = bindDestructuredParamTags(block.parsedParamTags, declaredNames, block.destructuredParameters);
|
|
317
|
+
const stale: string[] = [];
|
|
318
|
+
for (const tag of block.parsedParamTags) {
|
|
319
|
+
if (declaredNames.has(tag.name)) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const slot = boundSlots.get(tag.name);
|
|
323
|
+
if (slot && (tag.propertyKey === undefined || slot.hasOpenKeys || slot.keys.includes(tag.propertyKey))) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
stale.push(tag.fullName);
|
|
327
|
+
}
|
|
328
|
+
return stale;
|
|
186
329
|
}
|
|
187
330
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
331
|
+
/**
|
|
332
|
+
* Maps non-declared JSDoc object names to available destructured parameter slots.
|
|
333
|
+
* Pass 1 binds root names whose dotted tags match explicit destructured keys so a stale tag listed earlier cannot steal a slot.
|
|
334
|
+
*
|
|
335
|
+
* @param tags - parsed `@param` tags in docblock order
|
|
336
|
+
* @param declaredNames - ordinary identifier parameter names in the signature
|
|
337
|
+
* @param slots - destructured parameter slots in signature order
|
|
338
|
+
* @returns Map from claimed JSDoc root parameter names to their bound destructured slots.
|
|
339
|
+
*/
|
|
340
|
+
function bindDestructuredParamTags(
|
|
341
|
+
tags: readonly ParsedDocParamTag[],
|
|
342
|
+
declaredNames: ReadonlySet<string>,
|
|
343
|
+
slots: readonly DestructuredParameterSlot[],
|
|
344
|
+
): Map<string, DestructuredParameterSlot> {
|
|
345
|
+
const bound = new Map<string, DestructuredParameterSlot>();
|
|
346
|
+
if (slots.length === 0) {
|
|
347
|
+
return bound;
|
|
348
|
+
}
|
|
349
|
+
const unclaimed = new Set<number>(slots.map((_slot, index) => index));
|
|
350
|
+
const candidateRoots = collectDestructuredCandidateRoots(tags, declaredNames);
|
|
351
|
+
bindMatchingKeyRoots(candidateRoots, slots, unclaimed, bound);
|
|
352
|
+
bindRemainingSlotRoots(candidateRoots, slots, unclaimed, bound);
|
|
353
|
+
return bound;
|
|
191
354
|
}
|
|
192
355
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
356
|
+
/**
|
|
357
|
+
* Collects distinct non-declared `@param` root names in docblock order alongside whether each root has a root-level tag and any dotted keys.
|
|
358
|
+
*
|
|
359
|
+
* @param tags - parsed `@param` tags in docblock order
|
|
360
|
+
* @param declaredNames - ordinary identifier parameter names already matched by name
|
|
361
|
+
* @returns Map preserving first-seen root order, mapping each candidate root to its root-tag presence and dotted keys.
|
|
362
|
+
*/
|
|
363
|
+
function collectDestructuredCandidateRoots(
|
|
364
|
+
tags: readonly ParsedDocParamTag[],
|
|
365
|
+
declaredNames: ReadonlySet<string>,
|
|
366
|
+
): Map<string, { hasRootTag: boolean; dottedKeys: string[] }> {
|
|
367
|
+
const roots = new Map<string, { hasRootTag: boolean; dottedKeys: string[] }>();
|
|
368
|
+
for (const tag of tags) {
|
|
369
|
+
if (declaredNames.has(tag.name)) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const entry = roots.get(tag.name) ?? { hasRootTag: false, dottedKeys: [] };
|
|
373
|
+
if (tag.propertyKey === undefined) {
|
|
374
|
+
entry.hasRootTag = true;
|
|
375
|
+
} else {
|
|
376
|
+
entry.dottedKeys.push(tag.propertyKey);
|
|
377
|
+
}
|
|
378
|
+
roots.set(tag.name, entry);
|
|
379
|
+
}
|
|
380
|
+
return roots;
|
|
197
381
|
}
|
|
198
382
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
383
|
+
/**
|
|
384
|
+
* First pass of destructured slot assignment: binds any candidate root whose dotted keys overlap an unclaimed slot's explicit keys.
|
|
385
|
+
*
|
|
386
|
+
* @param candidateRoots - ordered map of non-declared `@param` root names to their root-tag presence and dotted keys
|
|
387
|
+
* @param slots - destructured parameter slots from the signature
|
|
388
|
+
* @param unclaimed - mutable set of still-available slot indices
|
|
389
|
+
* @param bound - mutable destination map updated with matched root-to-slot bindings
|
|
390
|
+
* @returns Nothing. Updates `unclaimed` and `bound` in place.
|
|
391
|
+
*/
|
|
392
|
+
function bindMatchingKeyRoots(
|
|
393
|
+
candidateRoots: ReadonlyMap<string, { hasRootTag: boolean; dottedKeys: string[] }>,
|
|
394
|
+
slots: readonly DestructuredParameterSlot[],
|
|
395
|
+
unclaimed: Set<number>,
|
|
396
|
+
bound: Map<string, DestructuredParameterSlot>,
|
|
397
|
+
): void {
|
|
398
|
+
for (const [rootName, entry] of candidateRoots) {
|
|
399
|
+
if (entry.dottedKeys.length === 0) {
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
const slotIndex = [...unclaimed].find((index) => entry.dottedKeys.some((key) => slots[index]?.keys.includes(key) ?? false));
|
|
403
|
+
if (slotIndex !== undefined && slots[slotIndex]) {
|
|
404
|
+
unclaimed.delete(slotIndex);
|
|
405
|
+
bound.set(rootName, slots[slotIndex]);
|
|
205
406
|
}
|
|
206
407
|
}
|
|
207
408
|
}
|
|
208
409
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
410
|
+
/**
|
|
411
|
+
* Second pass of destructured slot assignment: binds unclaimed slots to unbound candidate roots that carry a root tag or match an open-key slot.
|
|
412
|
+
*
|
|
413
|
+
* @param candidateRoots - ordered map of non-declared `@param` root names to their root-tag presence and dotted keys
|
|
414
|
+
* @param slots - destructured parameter slots from the signature
|
|
415
|
+
* @param unclaimed - mutable set of still-available slot indices
|
|
416
|
+
* @param bound - mutable destination map updated with fallback root-to-slot bindings
|
|
417
|
+
* @returns Nothing. Updates `unclaimed` and `bound` in place.
|
|
418
|
+
*/
|
|
419
|
+
function bindRemainingSlotRoots(
|
|
420
|
+
candidateRoots: ReadonlyMap<string, { hasRootTag: boolean; dottedKeys: string[] }>,
|
|
421
|
+
slots: readonly DestructuredParameterSlot[],
|
|
422
|
+
unclaimed: Set<number>,
|
|
423
|
+
bound: Map<string, DestructuredParameterSlot>,
|
|
424
|
+
): void {
|
|
425
|
+
for (const [rootName, entry] of candidateRoots) {
|
|
426
|
+
if (bound.has(rootName) || unclaimed.size === 0) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
const slotIndex = [...unclaimed].find((index) => entry.hasRootTag || (slots[index]?.hasOpenKeys ?? false));
|
|
430
|
+
if (slotIndex !== undefined && slots[slotIndex]) {
|
|
431
|
+
unclaimed.delete(slotIndex);
|
|
432
|
+
bound.set(rootName, slots[slotIndex]);
|
|
214
433
|
}
|
|
215
434
|
}
|
|
216
435
|
}
|
|
217
436
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
437
|
+
/**
|
|
438
|
+
* Emits one `docs.stale-param-tag` finding per declaration naming each stale tag once in docblock order.
|
|
439
|
+
* One finding per tag would share the docblock's line and symbol, so its fingerprint would collapse them into the first.
|
|
440
|
+
*
|
|
441
|
+
* @param file - Source file whose display path anchors the finding.
|
|
442
|
+
* @param block - Callable docblock metadata providing the name and declaration line.
|
|
443
|
+
* @param findings - Accumulator receiving the finding, with `parameter` metadata holding the stale names joined by ", ".
|
|
444
|
+
* @param staleTags - Distinct stale `@param` names in docblock order.
|
|
445
|
+
* @returns Nothing. Appends one finding to `findings` when `staleTags` is non-empty.
|
|
446
|
+
*/
|
|
447
|
+
function pushEveryStaleParamFinding(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[], staleTags: string[]): void {
|
|
448
|
+
if (staleTags.length === 0) {
|
|
222
449
|
return;
|
|
223
450
|
}
|
|
224
|
-
|
|
451
|
+
const noun = staleTags.length === 1 ? "tag" : "tags";
|
|
452
|
+
const quoted = staleTags.map((tag) => `\`${tag}\``).join(", ");
|
|
453
|
+
findings.push(docFinding({ ruleId: "docs.stale-param-tag", message: `Docblock for \`${block.name}\` has stale @param ${noun} ${quoted}.`, file, line: block.line, symbol: block.name, parameter: staleTags.join(", ") }));
|
|
225
454
|
}
|
|
226
455
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
456
|
+
/**
|
|
457
|
+
* Appends one stable `docs.missing-param-tag` finding for all parameters in `block` lacking a JSDoc `@param` tag, in signature order.
|
|
458
|
+
* Enforces the contract that non-JSDoc (`//` or `/* *\/`) comments cannot satisfy parameter tags and receive an explicit JSDoc-block message.
|
|
459
|
+
*
|
|
460
|
+
* @param file - source file whose display path anchors the finding
|
|
461
|
+
* @param block - callable signature and leading comment metadata
|
|
462
|
+
* @param findings - report accumulator receiving the advisory finding
|
|
463
|
+
* @returns Nothing. Appends nothing when all declared parameters have JSDoc `@param` tags.
|
|
464
|
+
*/
|
|
465
|
+
function pushMissingParamFindings(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[]): void {
|
|
466
|
+
const missing = block.hasJsDoc ? block.params.filter((param) => !block.paramTags.includes(param)) : block.params;
|
|
467
|
+
if (missing.length === 0) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const quoted = missing.map((param) => `\`${param}\``).join(", ");
|
|
471
|
+
const message = block.hasJsDoc
|
|
472
|
+
? `Docblock for \`${block.name}\` is missing @param for ${quoted}.`
|
|
473
|
+
: `Comment for \`${block.name}\` must be a JSDoc block (\`/** ... */\`) with @param for ${quoted}.`;
|
|
474
|
+
findings.push(docFinding({ ruleId: "docs.missing-param-tag", message, file, line: block.line, symbol: block.name, parameter: missing.join(", ") }));
|
|
231
475
|
}
|
|
232
476
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
477
|
+
/**
|
|
478
|
+
* Appends one stable `docs.missing-return-tag` finding whenever a checked callable lacks a JSDoc `@returns` tag, including `void` callables.
|
|
479
|
+
* Enforces the contract that non-JSDoc (`//` or `/* *\/`) comments always report so slash or block comments cannot bypass return documentation.
|
|
480
|
+
*
|
|
481
|
+
* @param file - source file whose display path anchors the finding
|
|
482
|
+
* @param block - callable signature and leading comment metadata
|
|
483
|
+
* @param findings - report accumulator receiving the advisory finding
|
|
484
|
+
* @returns Nothing. Appends a finding unless `block` carries a JSDoc block containing `@return` or `@returns`.
|
|
485
|
+
*/
|
|
486
|
+
function pushMissingReturnFinding(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[]): void {
|
|
487
|
+
if (block.hasJsDoc && /@returns?\b/.test(block.doc)) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
const message = block.hasJsDoc
|
|
491
|
+
? `Docblock for \`${block.name}\` is missing @returns.`
|
|
492
|
+
: `Comment for \`${block.name}\` must be a JSDoc block (\`/** ... */\`) with @returns.`;
|
|
493
|
+
findings.push(docFinding({ ruleId: "docs.missing-return-tag", message, file, line: block.line, symbol: block.name }));
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Checks existing tags on declared parameters and on every checked callable's return tags.
|
|
498
|
+
* The description contract is satisfied by one described occurrence for each parameter or return, including description on continuation lines.
|
|
499
|
+
*
|
|
500
|
+
* @param file - source file whose display path anchors the findings
|
|
501
|
+
* @param block - doc text and signature used to check parameter and return tags
|
|
502
|
+
* @param findings - accumulator for bare-tag violations
|
|
503
|
+
* @param everyCallable - switches that independently enable the parameter and return checks
|
|
504
|
+
* @returns nothing. Appends violations for enabled checks only when matching tags exist and all their occurrences are bare.
|
|
505
|
+
*/
|
|
506
|
+
function pushBareTagFindings(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[], everyCallable: DocTagRuleSwitches): void {
|
|
507
|
+
if (everyCallable.isParamTagEveryCallable) {
|
|
508
|
+
pushBareParamTagFindings(file, block, findings);
|
|
509
|
+
}
|
|
510
|
+
if (everyCallable.isReturnTagEveryCallable) {
|
|
511
|
+
pushBareReturnTagFinding(file, block, findings);
|
|
238
512
|
}
|
|
239
513
|
}
|
|
240
514
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
515
|
+
/**
|
|
516
|
+
* Flags each declared parameter whose existing `@param` tags lack a description on every occurrence.
|
|
517
|
+
* Anchors on the first bare tag so each reported parameter keeps a distinct fingerprint.
|
|
518
|
+
* Tags naming no declared parameter are handled by `docs.stale-param-tag`.
|
|
519
|
+
*
|
|
520
|
+
* @param file - Source file whose display path anchors each finding.
|
|
521
|
+
* @param block - Callable docblock text, line, name, and declared parameter list.
|
|
522
|
+
* @param findings - Accumulator receiving `docs.missing-param-tag` findings.
|
|
523
|
+
* @returns Nothing. Appends one finding to `findings` per undescribed declared parameter.
|
|
524
|
+
*/
|
|
525
|
+
function pushBareParamTagFindings(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[]): void {
|
|
526
|
+
const tags = docParamTagOccurrences(block.doc);
|
|
527
|
+
for (const param of block.params) {
|
|
528
|
+
const bareTag = firstBareTag(tags.filter((tag) => tag.name === param));
|
|
529
|
+
if (bareTag) {
|
|
530
|
+
findings.push(docFinding({ ruleId: "docs.missing-param-tag", message: `Docblock for \`${block.name}\` has no description for @param \`${param}\`.`, file, line: block.line + bareTag.lineOffset, symbol: block.name, parameter: param }));
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Reports a stable `docs.missing-return-tag` finding on the first `@return` or `@returns` line when all existing return tags lack descriptions.
|
|
537
|
+
* Covers every checked callable including `void` so bare `@returns {void}` tags must still explain the contract.
|
|
538
|
+
*
|
|
539
|
+
* @param file - source file whose display path anchors the finding
|
|
540
|
+
* @param block - doc text and signature for the callable being inspected
|
|
541
|
+
* @param findings - report accumulator receiving the bare-tag finding
|
|
542
|
+
* @returns Nothing. Appends a finding only when return tags exist and every occurrence lacks a description.
|
|
543
|
+
*/
|
|
544
|
+
function pushBareReturnTagFinding(file: SourceFile, block: DocumentedCallableBlock, findings: Finding[]): void {
|
|
545
|
+
const bareTag = firstBareTag(docReturnTagOccurrences(block.doc));
|
|
546
|
+
if (bareTag) {
|
|
547
|
+
findings.push(docFinding({ ruleId: "docs.missing-return-tag", message: `Docblock for \`${block.name}\` has no description for @returns.`, file, line: block.line + bareTag.lineOffset, symbol: block.name }));
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Selects the first tag occurrence only when every entry lacks a description.
|
|
553
|
+
*
|
|
554
|
+
* @param tags - Parsed `@param` or `@returns` tag occurrences for one parameter or callable.
|
|
555
|
+
* @returns First entry in `tags` only when all entries are undescribed; returns `undefined` if `tags` is empty or any entry is described.
|
|
556
|
+
*/
|
|
557
|
+
function firstBareTag<T extends DocTagOccurrence>(tags: T[]): T | undefined {
|
|
558
|
+
return tags.some((tag) => tag.isDescribed) ? undefined : tags[0];
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Builds an advisory docblock finding whose stable fingerprint derives from the rule, file path, line, and symbol.
|
|
563
|
+
* Leaves the `parameter` metadata field unset under `exactOptionalPropertyTypes` when absent or empty.
|
|
564
|
+
*
|
|
565
|
+
* @param input - Rule identifier, message, source location, symbol, and optional parameter name.
|
|
566
|
+
* @returns Constructed advisory documentation finding.
|
|
567
|
+
*/
|
|
244
568
|
function docFinding(input: DocFindingInput): Finding {
|
|
245
569
|
return makeFinding({
|
|
246
570
|
ruleId: input.ruleId,
|
|
@@ -256,62 +580,164 @@ function docFinding(input: DocFindingInput): Finding {
|
|
|
256
580
|
});
|
|
257
581
|
}
|
|
258
582
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
583
|
+
/**
|
|
584
|
+
* Parses the first `@param` tag from each line of a docblock in stable order, preserving dotted property names for destructuring checks.
|
|
585
|
+
*
|
|
586
|
+
* @param doc - raw JSDoc comment text between its opening and closing markers
|
|
587
|
+
* @returns Parsed `@param` tags in source order; empty when no line yields a usable parameter name.
|
|
588
|
+
*/
|
|
589
|
+
function docParsedParamTags(doc: string): ParsedDocParamTag[] {
|
|
590
|
+
const tags: ParsedDocParamTag[] = [];
|
|
263
591
|
for (const line of doc.split(/\r?\n/)) {
|
|
264
|
-
const
|
|
265
|
-
if (
|
|
266
|
-
|
|
592
|
+
const parsedTag = docParamTagParts(line);
|
|
593
|
+
if (parsedTag) {
|
|
594
|
+
tags.push(parsedTag);
|
|
267
595
|
}
|
|
268
596
|
}
|
|
269
|
-
return
|
|
597
|
+
return tags;
|
|
270
598
|
}
|
|
271
599
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
600
|
+
/**
|
|
601
|
+
* Reads a line-leading `@param` tag from one docblock line, skipping an optional brace-delimited type and optional `[...]` brackets.
|
|
602
|
+
* Rejects prose mentions of `@param` and look-alike tags such as `@parameter`, and supports dotted property paths such as `options.timeout`.
|
|
603
|
+
*
|
|
604
|
+
* @param line - single line from a JSDoc comment body
|
|
605
|
+
* @returns Parsed root name, full path, optional first property key, and description tail, or undefined when unusable.
|
|
606
|
+
*/
|
|
607
|
+
function docParamTagParts(line: string): ParsedDocParamTag | undefined {
|
|
608
|
+
const marker = /^\s*(?:\/\*\*|\*)?\s*@param(?=\s|\{|$)/u.exec(line);
|
|
609
|
+
// A line that mentions @param in prose, or uses a look-alike tag such as @parameter, documents no parameter.
|
|
610
|
+
if (!marker) {
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
613
|
+
const rest = stripDocParamType(line.slice(marker[0].length).trim());
|
|
614
|
+
const bracketed = rest.startsWith("[");
|
|
615
|
+
const nameSource = bracketed ? rest.slice(1).trimStart() : rest;
|
|
616
|
+
const pathMatch = nameSource.match(/^([A-Za-z_$][A-Za-z0-9_$]*(?:\.[A-Za-z_$][A-Za-z0-9_$]*)*)/);
|
|
617
|
+
const fullName = pathMatch?.[1];
|
|
618
|
+
if (!fullName) {
|
|
277
619
|
return undefined;
|
|
278
620
|
}
|
|
279
|
-
const
|
|
280
|
-
|
|
621
|
+
const segments = fullName.split(".");
|
|
622
|
+
const rootName = segments[0] ?? fullName;
|
|
623
|
+
const tailAfterName = nameSource.slice(fullName.length);
|
|
624
|
+
const tail = bracketed ? stripOptionalParamBracketTail(tailAfterName) : tailAfterName;
|
|
625
|
+
return { name: rootName, fullName, propertyKey: segments[1], tail };
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Advances past any default value and closing `]` on a bracketed optional JSDoc parameter such as `[opts.timeout=5000] - wait`.
|
|
630
|
+
*
|
|
631
|
+
* @param tailAfterName - text immediately following the parameter name inside a `[...]` JSDoc parameter token
|
|
632
|
+
* @returns Remaining description tail after the closing `]`, or the input unchanged when no closing bracket exists.
|
|
633
|
+
*/
|
|
634
|
+
function stripOptionalParamBracketTail(tailAfterName: string): string {
|
|
635
|
+
const closeBracket = tailAfterName.indexOf("]");
|
|
636
|
+
return closeBracket === -1 ? tailAfterName : tailAfterName.slice(closeBracket + 1);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Inspects the first `@param` marker on each line of a docblock to track whether each tag has a description.
|
|
641
|
+
*
|
|
642
|
+
* @param doc - Raw JSDoc comment body between its opening and closing markers.
|
|
643
|
+
* @returns Tag occurrences with parameter name, line offset, and description presence for lines yielding a name.
|
|
644
|
+
*/
|
|
645
|
+
function docParamTagOccurrences(doc: string): DocParamTagOccurrence[] {
|
|
646
|
+
const lines = docContentLines(doc);
|
|
647
|
+
const tags: DocParamTagOccurrence[] = [];
|
|
648
|
+
for (const [index, line] of lines.entries()) {
|
|
649
|
+
const parts = docParamTagParts(line);
|
|
650
|
+
if (parts) {
|
|
651
|
+
tags.push({ name: parts.name, lineOffset: index, isDescribed: hasTagDescription(lines, index, parts.tail) });
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return tags;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Scans each line of a docblock in order for its first `@returns` or `@return` marker and checks for description text.
|
|
659
|
+
*
|
|
660
|
+
* @param doc - Raw JSDoc comment body between its opening and closing markers.
|
|
661
|
+
* @returns Occurrences in docblock order with line offset and whether description text follows on that line or before the next tag.
|
|
662
|
+
*/
|
|
663
|
+
function docReturnTagOccurrences(doc: string): DocTagOccurrence[] {
|
|
664
|
+
const lines = docContentLines(doc);
|
|
665
|
+
const tags: DocTagOccurrence[] = [];
|
|
666
|
+
for (const [index, line] of lines.entries()) {
|
|
667
|
+
const match = /@returns?\b/.exec(line);
|
|
668
|
+
if (match) {
|
|
669
|
+
tags.push({ lineOffset: index, isDescribed: hasTagDescription(lines, index, stripDocParamType(line.slice(match.index + match[0].length).trim())) });
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return tags;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Strips leading `*` markers and outer whitespace from each line of a docblock body.
|
|
677
|
+
* Because `doc` already starts after the opening marker and stops before the closing marker, index 0 is the first line.
|
|
678
|
+
*
|
|
679
|
+
* @param doc - Raw JSDoc comment body between its opening and closing markers.
|
|
680
|
+
* @returns Trimmed content lines without leading `*` prefixes.
|
|
681
|
+
*/
|
|
682
|
+
function docContentLines(doc: string): string[] {
|
|
683
|
+
return doc.split(/\r?\n/).map((line) => line.replace(/^\s*\*/, "").trim());
|
|
281
684
|
}
|
|
282
685
|
|
|
283
|
-
|
|
284
|
-
|
|
686
|
+
/**
|
|
687
|
+
* Checks whether a tag carries description text on its own line or on continuation lines before the next `@` tag.
|
|
688
|
+
* A leading `-` alone in the remainder does not count as a description.
|
|
689
|
+
*
|
|
690
|
+
* @param lines - Stripped, trimmed docblock content lines.
|
|
691
|
+
* @param index - Zero-based line index of the current tag in `lines`.
|
|
692
|
+
* @param tail - Text after the tag's name or type on the tag line.
|
|
693
|
+
* @returns True when non-empty description text appears in `tail` or before the next tag line.
|
|
694
|
+
*/
|
|
695
|
+
function hasTagDescription(lines: string[], index: number, tail: string): boolean {
|
|
696
|
+
if (tail.trim().replace(/^-\s*/, "") !== "") {
|
|
697
|
+
return true;
|
|
698
|
+
}
|
|
699
|
+
const nextTag = lines.findIndex((line, lineIndex) => lineIndex > index && line.startsWith("@"));
|
|
700
|
+
return lines.slice(index + 1, nextTag === -1 ? lines.length : nextTag).some((line) => line !== "");
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Strips a leading brace-delimited JSDoc type annotation from a tag tail, balancing nested object-type braces such as `{{ host: string }}`.
|
|
705
|
+
*
|
|
706
|
+
* @param rest - Trimmed text immediately after a `@param` or `@returns` marker.
|
|
707
|
+
* @returns Trimmed suffix after the matching closing brace, `rest` unchanged if no brace begins it, or an empty string if unclosed.
|
|
708
|
+
*/
|
|
285
709
|
function stripDocParamType(rest: string): string {
|
|
286
|
-
|
|
710
|
+
const openBrace = String.fromCharCode(123);
|
|
711
|
+
const closeBrace = String.fromCharCode(125);
|
|
712
|
+
// Without a leading `{`, the tag carries no JSDoc type block to strip.
|
|
713
|
+
if (!rest.startsWith(openBrace)) {
|
|
287
714
|
return rest;
|
|
288
715
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
.toLowerCase()
|
|
303
|
-
.replace(/[^a-z0-9 ]/g, " ")
|
|
304
|
-
.replace(/\s+/g, " ")
|
|
305
|
-
.trim();
|
|
306
|
-
if (!words) {
|
|
307
|
-
return true;
|
|
716
|
+
let depth = 0;
|
|
717
|
+
// Walk nested braces so inline object types like `@param {{ host: string }} opts` stop at the outer closing brace.
|
|
718
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
719
|
+
const character = rest[index];
|
|
720
|
+
if (character === openBrace) {
|
|
721
|
+
depth += 1;
|
|
722
|
+
} else if (character === closeBrace) {
|
|
723
|
+
depth -= 1;
|
|
724
|
+
// Reaching depth zero closes the outer JSDoc type block, leaving the parameter name and description after it.
|
|
725
|
+
if (depth === 0) {
|
|
726
|
+
return rest.slice(index + 1).trim();
|
|
727
|
+
}
|
|
728
|
+
}
|
|
308
729
|
}
|
|
309
|
-
return
|
|
730
|
+
return "";
|
|
310
731
|
}
|
|
311
732
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
733
|
+
/**
|
|
734
|
+
* Scans upward above a declaration line to check for a leading JSDoc block opener or `*` continuation line.
|
|
735
|
+
* Blank lines and `@`-prefixed lines are skipped; any other text ends the search without a match.
|
|
736
|
+
*
|
|
737
|
+
* @param source - Full source text containing the declaration.
|
|
738
|
+
* @param line - One-based line number of the declaration.
|
|
739
|
+
* @returns True when the upward scan reaches a JSDoc opener or continuation line before hitting other code or text.
|
|
740
|
+
*/
|
|
315
741
|
function hasDocCommentBeforeLine(source: string, line: number): boolean {
|
|
316
742
|
const lines = source.split(/\r?\n/);
|
|
317
743
|
let index = line - 2;
|
|
@@ -328,21 +754,35 @@ function hasDocCommentBeforeLine(source: string, line: number): boolean {
|
|
|
328
754
|
return false;
|
|
329
755
|
}
|
|
330
756
|
|
|
331
|
-
|
|
332
|
-
|
|
757
|
+
/**
|
|
758
|
+
* Checks whether a trimmed line starts a JSDoc opener or `*` continuation marker.
|
|
759
|
+
* Ordinary `//` comments are excluded here and checked by `hasLeadingCommentBeforeLine`.
|
|
760
|
+
*
|
|
761
|
+
* @param trimmedLine - Leading-and-trailing trimmed source line.
|
|
762
|
+
* @returns True when `trimmedLine` starts with a JSDoc opener or `*`.
|
|
763
|
+
*/
|
|
333
764
|
function isDocCommentLine(trimmedLine: string): boolean {
|
|
334
765
|
return trimmedLine.startsWith("/**") || trimmedLine.startsWith("*");
|
|
335
766
|
}
|
|
336
767
|
|
|
337
|
-
|
|
338
|
-
|
|
768
|
+
/**
|
|
769
|
+
* Determines whether a trimmed line stops the upward docblock scan after doc-comment lines have been ruled out.
|
|
770
|
+
* Blank lines and `@`-prefixed tag lines allow the search to continue; every other line is a boundary.
|
|
771
|
+
*
|
|
772
|
+
* @param trimmedLine - Leading-and-trailing trimmed source line.
|
|
773
|
+
* @returns True when `trimmedLine` is non-empty and does not start with `@`.
|
|
774
|
+
*/
|
|
339
775
|
function isDocCommentSearchBoundary(trimmedLine: string): boolean {
|
|
340
776
|
return trimmedLine !== "" && !trimmedLine.startsWith("@");
|
|
341
777
|
}
|
|
342
778
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
779
|
+
/**
|
|
780
|
+
* Reports whether the first content in `source` after blank lines and an optional shebang supplies overview text.
|
|
781
|
+
* An empty file, a shebang alone, an empty comment, or a block containing only tag lines leaves the overview missing.
|
|
782
|
+
*
|
|
783
|
+
* @param source - Full file text to inspect for a leading overview comment.
|
|
784
|
+
* @returns True when overview comment text is found at the first meaningful line after any shebang.
|
|
785
|
+
*/
|
|
346
786
|
function hasFileOverviewComment(source: string): boolean {
|
|
347
787
|
const lines = source.split(/\r?\n/);
|
|
348
788
|
let index = firstMeaningfulLineIndex(lines);
|
|
@@ -355,9 +795,14 @@ function hasFileOverviewComment(source: string): boolean {
|
|
|
355
795
|
return index !== undefined && commentTextAtLine(lines, index) !== undefined;
|
|
356
796
|
}
|
|
357
797
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
798
|
+
/**
|
|
799
|
+
* Finds the first non-blank line index at or after `start`.
|
|
800
|
+
* Comments count as meaningful so file-overview detection can inspect the comment itself.
|
|
801
|
+
*
|
|
802
|
+
* @param lines - Source lines split on line breaks.
|
|
803
|
+
* @param start - Zero-based starting index in `lines` (defaults to `0`).
|
|
804
|
+
* @returns Index of the first non-blank line at or after `start`, or `undefined` when none remains.
|
|
805
|
+
*/
|
|
361
806
|
function firstMeaningfulLineIndex(lines: string[], start = 0): number | undefined {
|
|
362
807
|
for (let index = start; index < lines.length; index += 1) {
|
|
363
808
|
if ((lines[index] ?? "").trim() !== "") {
|