@doccov/sdk 0.2.2 → 0.3.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/README.md CHANGED
@@ -1,68 +1,44 @@
1
1
  # @doccov/sdk
2
2
 
3
- Programmatic API for documentation coverage analysis and drift detection in TypeScript.
3
+ Programmatic API for documentation coverage analysis.
4
4
 
5
5
  ## Install
6
+
6
7
  ```bash
7
8
  npm install @doccov/sdk
8
9
  ```
9
10
 
10
- ## Minimal Usage
11
- ```ts
11
+ ## Usage
12
+
13
+ ```typescript
12
14
  import { DocCov } from '@doccov/sdk';
13
15
 
14
16
  const doccov = new DocCov();
15
- const { spec, diagnostics } = await doccov.analyzeFileWithDiagnostics('src/index.ts');
17
+ const { spec } = await doccov.analyzeFileWithDiagnostics('src/index.ts');
16
18
 
17
19
  console.log(`Coverage: ${spec.docs?.coverageScore}%`);
18
- console.log(`${spec.exports.length} exports analyzed`);
19
- ```
20
20
 
21
- ## Checking for Drift
22
- ```ts
21
+ // Check for drift
23
22
  for (const exp of spec.exports) {
24
- const drift = exp.docs?.drift ?? [];
25
- if (drift.length > 0) {
26
- console.log(`${exp.name}: ${drift.length} drift issues`);
27
- for (const d of drift) {
28
- console.log(` - ${d.issue}`);
29
- if (d.suggestion) {
30
- console.log(` Suggestion: ${d.suggestion}`);
31
- }
32
- }
23
+ if (exp.docs?.drift?.length) {
24
+ console.log(`${exp.name}: ${exp.docs.drift.length} drift issues`);
33
25
  }
34
26
  }
35
27
  ```
36
28
 
37
- ## Helper Functions
38
- - `analyze(code)` – analyze an in-memory string
39
- - `analyzeFile(path)` – analyze a single entry point
40
- - `analyzeWithDiagnostics` / `analyzeFileWithDiagnostics` – include TypeScript diagnostics
41
- - `extractPackageSpec` – lower-level hook used by the CLI
29
+ ## Exports
42
30
 
43
- ## Filters
44
- ```ts
45
- const { spec } = await doccov.analyzeFileWithDiagnostics('src/index.ts', {
46
- filters: {
47
- include: ['publicApi'],
48
- exclude: ['internalHelper'],
49
- },
50
- });
51
- ```
31
+ - `DocCov` - Main analysis class
32
+ - `runExample` / `runExamples` - Execute @example blocks
33
+ - `detectExampleRuntimeErrors` - Check for runtime failures
52
34
 
53
- ## Coverage Metadata
54
- Each export includes documentation health info:
55
- ```ts
56
- interface SpecDocsMetadata {
57
- coverageScore?: number; // 0-100
58
- missing?: ('description' | 'params' | 'returns' | 'examples')[];
59
- drift?: SpecDocDrift[]; // List of drift issues
60
- }
61
- ```
35
+ ## Documentation
36
+
37
+ - [SDK Overview](../../docs/sdk/overview.md)
38
+ - [DocCov Class](../../docs/sdk/doccov-class.md)
39
+ - [Example Runner](../../docs/sdk/example-runner.md)
40
+ - [Filtering](../../docs/sdk/filtering.md)
62
41
 
63
- ## See Also
64
- - [Spec helpers](../spec/README.md)
65
- - [CLI usage](../cli/README.md)
66
- - [Fixtures](../../tests/fixtures/README.md)
42
+ ## License
67
43
 
68
- MIT License
44
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,5 +1,74 @@
1
+ import { SpecDocDrift, SpecExport } from "@openpkg-ts/spec";
2
+ interface ExampleRunResult {
3
+ success: boolean;
4
+ stdout: string;
5
+ stderr: string;
6
+ exitCode: number;
7
+ duration: number;
8
+ }
9
+ interface RunExampleOptions {
10
+ /** Timeout in milliseconds (default: 5000) */
11
+ timeout?: number;
12
+ /** Working directory for execution */
13
+ cwd?: string;
14
+ }
15
+ interface RunExamplesWithPackageOptions extends RunExampleOptions {
16
+ /** Path to the local package to install */
17
+ packagePath: string;
18
+ /** Package manager to use (auto-detected if not specified) */
19
+ packageManager?: "npm" | "pnpm" | "bun";
20
+ /** Timeout for package installation in ms (default: 60000) */
21
+ installTimeout?: number;
22
+ }
23
+ interface RunExamplesWithPackageResult {
24
+ /** Results for each example by index */
25
+ results: Map<number, ExampleRunResult>;
26
+ /** Whether package installation succeeded */
27
+ installSuccess: boolean;
28
+ /** Error message if installation failed */
29
+ installError?: string;
30
+ /** Total duration including install */
31
+ totalDuration: number;
32
+ }
33
+ /**
34
+ * Run an example code snippet in an isolated Node process.
35
+ * Uses Node 22+ --experimental-strip-types for direct TS execution.
36
+ */
37
+ declare function runExample(code: string, options?: RunExampleOptions): Promise<ExampleRunResult>;
38
+ /**
39
+ * Run multiple examples and collect results
40
+ */
41
+ declare function runExamples(examples: string[], options?: RunExampleOptions): Promise<Map<number, ExampleRunResult>>;
42
+ /**
43
+ * Run multiple examples with a pre-installed local package.
44
+ * Creates a single temp directory, installs the package once,
45
+ * runs all examples, then cleans up.
46
+ */
47
+ declare function runExamplesWithPackage(examples: string[], options: RunExamplesWithPackageOptions): Promise<RunExamplesWithPackageResult>;
1
48
  import { OpenPkg } from "@openpkg-ts/spec";
2
49
  type OpenPkgSpec = OpenPkg;
50
+ /**
51
+ * Detect runtime errors in @example blocks.
52
+ * Results are provided externally after running examples via runExamples().
53
+ */
54
+ declare function detectExampleRuntimeErrors(entry: SpecExport, runtimeResults: Map<number, ExampleRunResult>): SpecDocDrift[];
55
+ /**
56
+ * Parse assertion comments from example code.
57
+ * Matches: // => expected_value
58
+ */
59
+ declare function parseAssertions(code: string): Array<{
60
+ lineNumber: number;
61
+ expected: string;
62
+ }>;
63
+ /**
64
+ * Check if code contains comments that are not assertion syntax.
65
+ * Used to determine if LLM fallback should be attempted.
66
+ */
67
+ declare function hasNonAssertionComments(code: string): boolean;
68
+ /**
69
+ * Detect assertion failures by comparing stdout to expected values.
70
+ */
71
+ declare function detectExampleAssertionFailures(entry: SpecExport, runtimeResults: Map<number, ExampleRunResult>): SpecDocDrift[];
3
72
  interface DocCovOptions {
4
73
  includePrivate?: boolean;
5
74
  followImports?: boolean;
@@ -13,9 +82,145 @@ interface FilterOptions {
13
82
  include?: string[];
14
83
  exclude?: string[];
15
84
  }
85
+ import { SpecDocDrift as SpecDocDrift2, SpecExport as SpecExport2 } from "@openpkg-ts/spec";
86
+ import * as TS from "typescript";
87
+ /**
88
+ * Represents a single parameter in a JSDoc patch
89
+ */
90
+ interface JSDocParam {
91
+ name: string;
92
+ type?: string;
93
+ description?: string;
94
+ optional?: boolean;
95
+ }
96
+ /**
97
+ * Represents a return type in a JSDoc patch
98
+ */
99
+ interface JSDocReturn {
100
+ type?: string;
101
+ description?: string;
102
+ }
103
+ /**
104
+ * Represents a generic tag in a JSDoc patch
105
+ */
106
+ interface JSDocTag {
107
+ name: string;
108
+ text: string;
109
+ }
110
+ /**
111
+ * A patchable representation of a JSDoc comment
112
+ */
113
+ interface JSDocPatch {
114
+ description?: string;
115
+ params?: JSDocParam[];
116
+ returns?: JSDocReturn;
117
+ examples?: string[];
118
+ deprecated?: string | false;
119
+ async?: boolean;
120
+ type?: string;
121
+ typeParams?: Array<{
122
+ name: string;
123
+ constraint?: string;
124
+ description?: string;
125
+ }>;
126
+ otherTags?: JSDocTag[];
127
+ }
128
+ /**
129
+ * Represents an edit to be applied to a source file
130
+ */
131
+ interface JSDocEdit {
132
+ filePath: string;
133
+ symbolName: string;
134
+ startLine: number;
135
+ endLine: number;
136
+ hasExisting: boolean;
137
+ existingJSDoc?: string;
138
+ newJSDoc: string;
139
+ indent: string;
140
+ }
141
+ /**
142
+ * Result of applying edits to source files
143
+ */
144
+ interface ApplyEditsResult {
145
+ filesModified: number;
146
+ editsApplied: number;
147
+ errors: Array<{
148
+ file: string;
149
+ error: string;
150
+ }>;
151
+ }
152
+ /**
153
+ * Parse a JSDoc comment string into a patchable structure
154
+ */
155
+ declare function parseJSDocToPatch(jsDocText: string): JSDocPatch;
156
+ /**
157
+ * Apply a partial patch to an existing JSDoc patch, preserving unmodified content
158
+ */
159
+ declare function applyPatchToJSDoc(existing: JSDocPatch, updates: Partial<JSDocPatch>): JSDocPatch;
160
+ /**
161
+ * Serialize a JSDocPatch back to a formatted comment string
162
+ */
163
+ declare function serializeJSDoc(patch: JSDocPatch, indent?: string): string;
164
+ /**
165
+ * Find the JSDoc location for a declaration in a source file
166
+ */
167
+ declare function findJSDocLocation(sourceFile: TS.SourceFile, symbolName: string, approximateLine?: number): {
168
+ startLine: number;
169
+ endLine: number;
170
+ declarationLine: number;
171
+ hasExisting: boolean;
172
+ existingJSDoc?: string;
173
+ indent: string;
174
+ } | null;
175
+ /**
176
+ * Apply a batch of edits to source files
177
+ */
178
+ declare function applyEdits(edits: JSDocEdit[]): Promise<ApplyEditsResult>;
179
+ /**
180
+ * Create a TypeScript source file from a file path
181
+ */
182
+ declare function createSourceFile(filePath: string): TS.SourceFile;
183
+ /**
184
+ * Types of fixes that can be generated
185
+ */
186
+ type FixType = "add-param" | "remove-param" | "update-param-type" | "update-param-optionality" | "update-return-type" | "update-assertion" | "add-template" | "update-template-constraint" | "add-deprecated" | "remove-deprecated" | "add-async" | "remove-async" | "update-property-type";
187
+ /**
188
+ * A fix suggestion with the patch to apply
189
+ */
190
+ interface FixSuggestion {
191
+ type: FixType;
192
+ driftType: SpecDocDrift2["type"];
193
+ target: string;
194
+ description: string;
195
+ patch: Partial<JSDocPatch>;
196
+ }
197
+ /**
198
+ * Check if a drift type can be fixed deterministically
199
+ */
200
+ declare function isFixableDrift(drift: SpecDocDrift2): boolean;
201
+ /**
202
+ * Generate a fix for a single drift issue
203
+ */
204
+ declare function generateFix(drift: SpecDocDrift2, exportEntry: SpecExport2, existingPatch?: JSDocPatch): FixSuggestion | null;
205
+ /**
206
+ * Generate all fixes for an export's drift issues
207
+ */
208
+ declare function generateFixesForExport(exportEntry: SpecExport2, existingPatch?: JSDocPatch): FixSuggestion[];
209
+ /**
210
+ * Merge multiple fix patches into a single patch
211
+ */
212
+ declare function mergeFixes(fixes: FixSuggestion[], basePatch?: JSDocPatch): JSDocPatch;
213
+ /**
214
+ * Get a summary of fixable vs non-fixable drifts
215
+ */
216
+ declare function categorizeDrifts(drifts: SpecDocDrift2[]): {
217
+ fixable: SpecDocDrift2[];
218
+ nonFixable: SpecDocDrift2[];
219
+ };
16
220
  interface Diagnostic {
17
221
  message: string;
18
222
  severity: "error" | "warning" | "info";
223
+ suggestion?: string;
19
224
  location?: {
20
225
  file: string;
21
226
  line?: number;
@@ -54,4 +259,4 @@ declare function analyze(code: string, options?: AnalyzeOptions): Promise<OpenPk
54
259
  declare function analyzeFile(filePath: string, options?: AnalyzeOptions): Promise<OpenPkgSpec>;
55
260
  /** @deprecated Use DocCov instead */
56
261
  declare const OpenPkg2: typeof DocCov;
57
- export { extractPackageSpec, analyzeFile, analyze, OpenPkgSpec, OpenPkgOptions, OpenPkg2 as OpenPkg, FilterOptions, DocCovOptions, DocCov, Diagnostic, AnalyzeOptions, AnalysisResult };
262
+ export { serializeJSDoc, runExamplesWithPackage, runExamples, runExample, parseJSDocToPatch, parseAssertions, mergeFixes, isFixableDrift, hasNonAssertionComments, generateFixesForExport, generateFix, findJSDocLocation, extractPackageSpec, detectExampleRuntimeErrors, detectExampleAssertionFailures, createSourceFile, categorizeDrifts, applyPatchToJSDoc, applyEdits, analyzeFile, analyze, RunExamplesWithPackageResult, RunExamplesWithPackageOptions, RunExampleOptions, OpenPkgSpec, OpenPkgOptions, OpenPkg2 as OpenPkg, JSDocTag, JSDocReturn, JSDocPatch, JSDocParam, JSDocEdit, FixType, FixSuggestion, FilterOptions, ExampleRunResult, DocCovOptions, DocCov, Diagnostic, ApplyEditsResult, AnalyzeOptions, AnalysisResult };