@doccov/sdk 0.2.1 → 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/LICENSE ADDED
@@ -0,0 +1,103 @@
1
+ Business Source License 1.1
2
+
3
+ Parameters
4
+
5
+ Licensor: Ryan Waits
6
+ Licensed Work: DocCov SDK
7
+ The Licensed Work is (c) 2024 Ryan Waits.
8
+ Additional Use Grant: You may make use of the Licensed Work, provided that
9
+ you do not use the Licensed Work for a Documentation
10
+ Coverage Service.
11
+
12
+ A "Documentation Coverage Service" is a commercial
13
+ offering that allows third parties to access the
14
+ functionality of the Licensed Work by analyzing
15
+ TypeScript/JavaScript code documentation coverage
16
+ as a service.
17
+
18
+ Change Date: 2028-11-26
19
+
20
+ Change License: Apache License 2.0
21
+
22
+ For information about alternative licensing arrangements for the Software,
23
+ please contact: licensing@doccov.dev
24
+
25
+ Notice
26
+
27
+ The Business Source License (this document, or the "License") is not an Open
28
+ Source license. However, the Licensed Work will eventually be made available
29
+ under an Open Source License, as stated in this License.
30
+
31
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
32
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
33
+
34
+ -----------------------------------------------------------------------------
35
+
36
+ Business Source License 1.1
37
+
38
+ Terms
39
+
40
+ The Licensor hereby grants you the right to copy, modify, create derivative
41
+ works, redistribute, and make non-production use of the Licensed Work. The
42
+ Licensor may make an Additional Use Grant, above, permitting limited
43
+ production use.
44
+
45
+ Effective on the Change Date, or the fourth anniversary of the first publicly
46
+ available distribution of a specific version of the Licensed Work under this
47
+ License, whichever comes first, the Licensor hereby grants you rights under
48
+ the terms of the Change License, and the rights granted in the paragraph
49
+ above terminate.
50
+
51
+ If your use of the Licensed Work does not comply with the requirements
52
+ currently in effect as described in this License, you must purchase a
53
+ commercial license from the Licensor, its affiliated entities, or authorized
54
+ resellers, or you must refrain from using the Licensed Work.
55
+
56
+ All copies of the original and modified Licensed Work, and derivative works
57
+ of the Licensed Work, are subject to this License. This License applies
58
+ separately for each version of the Licensed Work and the Change Date may vary
59
+ for each version of the Licensed Work released by Licensor.
60
+
61
+ You must conspicuously display this License on each original or modified copy
62
+ of the Licensed Work. If you receive the Licensed Work in original or
63
+ modified form from a third party, the terms and conditions set forth in this
64
+ License apply to your use of that work.
65
+
66
+ Any use of the Licensed Work in violation of this License will automatically
67
+ terminate your rights under this License for the current and all other
68
+ versions of the Licensed Work.
69
+
70
+ This License does not grant you any right in any trademark or logo of
71
+ Licensor or its affiliates (provided that you may use a trademark or logo of
72
+ Licensor as expressly required by this License).
73
+
74
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
75
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
76
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
77
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
78
+ TITLE.
79
+
80
+ MariaDB hereby grants you permission to use this License's text to license
81
+ your works, and to refer to it using the trademark "Business Source License",
82
+ as long as you comply with the Covenants of Licensor below.
83
+
84
+ Covenants of Licensor
85
+
86
+ In consideration of the right to use this License's text and the "Business
87
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
88
+ other recipients of the licensed work to be provided by Licensor:
89
+
90
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
91
+ or a license that is compatible with GPL Version 2.0 or a later version,
92
+ where "compatible" means that software provided under the Change License can
93
+ be included in a program with software provided under GPL Version 2.0 or a
94
+ later version. Licensor may specify additional Change Licenses without
95
+ limitation.
96
+
97
+ 2. To either: (a) specify an additional grant of rights to use that does not
98
+ impose any additional restriction on the right granted in this License, as
99
+ the Additional Use Grant; or (b) insert the text "None".
100
+
101
+ 3. To specify a Change Date.
102
+
103
+ 4. Not to modify this License in any other way.
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 };