svelte-docinfo 0.1.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 +21 -0
- package/README.md +349 -0
- package/dist/analyze-core.d.ts +1386 -0
- package/dist/analyze-core.d.ts.map +1 -0
- package/dist/analyze-core.js +324 -0
- package/dist/analyze.d.ts +137 -0
- package/dist/analyze.d.ts.map +1 -0
- package/dist/analyze.js +148 -0
- package/dist/cli.d.ts +102 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +206 -0
- package/dist/concurrency.d.ts +38 -0
- package/dist/concurrency.d.ts.map +1 -0
- package/dist/concurrency.js +57 -0
- package/dist/declaration-build.d.ts +164 -0
- package/dist/declaration-build.d.ts.map +1 -0
- package/dist/declaration-build.js +15 -0
- package/dist/declaration-helpers.d.ts +169 -0
- package/dist/declaration-helpers.d.ts.map +1 -0
- package/dist/declaration-helpers.js +239 -0
- package/dist/dep-resolver.d.ts +166 -0
- package/dist/dep-resolver.d.ts.map +1 -0
- package/dist/dep-resolver.js +144 -0
- package/dist/diagnostics.d.ts +650 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +377 -0
- package/dist/discovery.d.ts +115 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +126 -0
- package/dist/exports.d.ts +105 -0
- package/dist/exports.d.ts.map +1 -0
- package/dist/exports.js +274 -0
- package/dist/files.d.ts +77 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +95 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/log.d.ts +32 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +11 -0
- package/dist/logo.d.ts +8 -0
- package/dist/logo.d.ts.map +1 -0
- package/dist/logo.js +9 -0
- package/dist/main.d.ts +11 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +18 -0
- package/dist/paths.d.ts +35 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +34 -0
- package/dist/postprocess.d.ts +145 -0
- package/dist/postprocess.d.ts.map +1 -0
- package/dist/postprocess.js +305 -0
- package/dist/session.d.ts +248 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +496 -0
- package/dist/source-config.d.ts +330 -0
- package/dist/source-config.d.ts.map +1 -0
- package/dist/source-config.js +356 -0
- package/dist/source.d.ts +126 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +68 -0
- package/dist/svelte.d.ts +179 -0
- package/dist/svelte.d.ts.map +1 -0
- package/dist/svelte.js +898 -0
- package/dist/tsdoc.d.ts +145 -0
- package/dist/tsdoc.d.ts.map +1 -0
- package/dist/tsdoc.js +262 -0
- package/dist/types.d.ts +3381 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +588 -0
- package/dist/typescript-exports.d.ts +91 -0
- package/dist/typescript-exports.d.ts.map +1 -0
- package/dist/typescript-exports.js +656 -0
- package/dist/typescript-extract-class.d.ts +29 -0
- package/dist/typescript-extract-class.d.ts.map +1 -0
- package/dist/typescript-extract-class.js +251 -0
- package/dist/typescript-extract-function.d.ts +47 -0
- package/dist/typescript-extract-function.d.ts.map +1 -0
- package/dist/typescript-extract-function.js +108 -0
- package/dist/typescript-extract-shared.d.ts +166 -0
- package/dist/typescript-extract-shared.d.ts.map +1 -0
- package/dist/typescript-extract-shared.js +528 -0
- package/dist/typescript-extract-type-properties.d.ts +34 -0
- package/dist/typescript-extract-type-properties.d.ts.map +1 -0
- package/dist/typescript-extract-type-properties.js +241 -0
- package/dist/typescript-extract-type.d.ts +48 -0
- package/dist/typescript-extract-type.d.ts.map +1 -0
- package/dist/typescript-extract-type.js +238 -0
- package/dist/typescript-program.d.ts +211 -0
- package/dist/typescript-program.d.ts.map +1 -0
- package/dist/typescript-program.js +290 -0
- package/dist/vite.d.ts +116 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +418 -0
- package/package.json +131 -0
- package/virtual-svelte-docinfo.d.ts +49 -0
package/dist/tsdoc.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TSDoc/JSDoc parsing helpers using the TypeScript compiler API.
|
|
3
|
+
*
|
|
4
|
+
* Provides `parseComment` for extracting JSDoc/TSDoc from TypeScript nodes.
|
|
5
|
+
* Primarily designed for build-time code generation but can be used at runtime.
|
|
6
|
+
*
|
|
7
|
+
* ## Design
|
|
8
|
+
*
|
|
9
|
+
* Pure extraction approach: extracts documentation as-is with minimal transformation,
|
|
10
|
+
* preserving source intent. Works around TypeScript compiler API quirks where needed.
|
|
11
|
+
*
|
|
12
|
+
* Supports both regular TypeScript and Svelte components (via svelte2tsx output).
|
|
13
|
+
*
|
|
14
|
+
* ## Tag support
|
|
15
|
+
*
|
|
16
|
+
* Supports a subset of standard TSDoc tags:
|
|
17
|
+
* `@param`, `@returns`, `@throws`, `@example`, `@deprecated`, `@see`, `@since`, `@default`, `@nodocs`.
|
|
18
|
+
*
|
|
19
|
+
* The `@nodocs` tag excludes exports from documentation and flat namespace validation.
|
|
20
|
+
* The declaration is still exported and usable, just not documented.
|
|
21
|
+
*
|
|
22
|
+
* Also supports `@mutates` (non-standard) for documenting mutations to parameters or external state.
|
|
23
|
+
* Uses same format as `@param`: `@mutates key - description of mutation`. The key is
|
|
24
|
+
* unvalidated — typically a parameter name, but compound paths (`this.foo`, `obj.field`)
|
|
25
|
+
* and external state references are accepted as-is.
|
|
26
|
+
*
|
|
27
|
+
* Only `@returns` is supported (not `@return`).
|
|
28
|
+
*
|
|
29
|
+
* The `@see` tag supports multiple formats: plain URLs (`https://...`), `{@link}` syntax, and module names.
|
|
30
|
+
* Relative/absolute path support in `@see` is TBD.
|
|
31
|
+
*
|
|
32
|
+
* ## Behavioral notes
|
|
33
|
+
*
|
|
34
|
+
* Due to TS Compiler API limitations:
|
|
35
|
+
* - TS API includes dash separator in `@param` tag text; we strip the leading `- ` as it's syntax, not content
|
|
36
|
+
* - `@throws` tags have `{Type}` stripped by TS API; fallback regex extracts first word as error type
|
|
37
|
+
* - TS API strips URL protocols from `@see` tag text; we use `getText()` to preserve original format including `{@link}` syntax
|
|
38
|
+
*
|
|
39
|
+
* @see `declaration-build.ts` for `DeclarationJsonBuild`
|
|
40
|
+
* @see `typescript-exports.ts` and `svelte.ts` as primary consumers
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
import ts from 'typescript';
|
|
45
|
+
import type { DeclarationJsonBuild, MemberJsonBuild } from './declaration-build.js';
|
|
46
|
+
/**
|
|
47
|
+
* Parsed JSDoc/TSDoc comment with structured metadata.
|
|
48
|
+
*
|
|
49
|
+
* Returned by `parseComment` — consumers typically pass this to `applyToDeclaration`
|
|
50
|
+
* to populate `DeclarationJsonBuild` fields.
|
|
51
|
+
*/
|
|
52
|
+
export interface TsdocParsedComment {
|
|
53
|
+
/** Comment text (excluding comment markers). */
|
|
54
|
+
text: string;
|
|
55
|
+
/** Parameter descriptions mapped by parameter name. */
|
|
56
|
+
params: Record<string, string>;
|
|
57
|
+
/** Return value description from `@returns`. */
|
|
58
|
+
returns?: string;
|
|
59
|
+
/** Thrown errors from `@throws`. */
|
|
60
|
+
throws?: Array<{
|
|
61
|
+
type?: string;
|
|
62
|
+
description: string;
|
|
63
|
+
}>;
|
|
64
|
+
/** Code examples from `@example`. */
|
|
65
|
+
examples?: Array<string>;
|
|
66
|
+
/** Deprecation message from `@deprecated`. */
|
|
67
|
+
deprecatedMessage?: string;
|
|
68
|
+
/** Related references from `@see`. */
|
|
69
|
+
seeAlso?: Array<string>;
|
|
70
|
+
/** Version information from `@since`. */
|
|
71
|
+
since?: string;
|
|
72
|
+
/** Default value from `@default` tag. */
|
|
73
|
+
defaultValue?: string;
|
|
74
|
+
/** Mutation documentation from `@mutates` (non-standard), mapped by parameter name. */
|
|
75
|
+
mutates?: Record<string, string>;
|
|
76
|
+
/** Whether to exclude from documentation. From `@nodocs` tag. */
|
|
77
|
+
nodocs?: boolean;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Parse JSDoc comment from a TypeScript node.
|
|
81
|
+
*
|
|
82
|
+
* Extracts and parses all JSDoc tags including:
|
|
83
|
+
*
|
|
84
|
+
* - `@param` - parameter descriptions
|
|
85
|
+
* - `@returns` - return value description
|
|
86
|
+
* - `@throws` - error documentation
|
|
87
|
+
* - `@example` - code examples
|
|
88
|
+
* - `@deprecated` - deprecation warnings
|
|
89
|
+
* - `@see` - related references
|
|
90
|
+
* - `@since` - version information
|
|
91
|
+
* - `@default` - default value
|
|
92
|
+
* - `@mutates` - mutation documentation (non-standard)
|
|
93
|
+
* - `@nodocs` - exclusion flag (non-standard)
|
|
94
|
+
*
|
|
95
|
+
* @param node - the TypeScript node to extract JSDoc from
|
|
96
|
+
* @param sourceFile - source file (used for extracting full `@see` tag text)
|
|
97
|
+
* @returns parsed comment with structured metadata, or undefined if no JSDoc found
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const tsdoc = parseComment(declarationNode, sourceFile);
|
|
102
|
+
* if (tsdoc) {
|
|
103
|
+
* console.log(tsdoc.text); // main comment text
|
|
104
|
+
* console.log(tsdoc.params); // {paramName: 'description'}
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare const parseComment: (node: ts.Node, sourceFile: ts.SourceFile) => TsdocParsedComment | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Apply parsed TSDoc metadata to a declaration.
|
|
111
|
+
*
|
|
112
|
+
* Consolidates the common pattern of assigning TSDoc fields to declarations,
|
|
113
|
+
* with conditional assignment for array fields (only if non-empty).
|
|
114
|
+
*
|
|
115
|
+
* @param declaration - declaration object to update
|
|
116
|
+
* @param tsdoc - parsed TSDoc comment (if available)
|
|
117
|
+
* @mutates declaration - adds docComment, deprecatedMessage, examples, seeAlso, throws, since, mutates, defaultValue fields
|
|
118
|
+
*/
|
|
119
|
+
export declare const applyToDeclaration: (declaration: DeclarationJsonBuild | MemberJsonBuild, tsdoc: TsdocParsedComment | undefined) => void;
|
|
120
|
+
/**
|
|
121
|
+
* Check if a TypeScript node has a `@module` JSDoc tag.
|
|
122
|
+
*
|
|
123
|
+
* Used to prevent `@module` comments from leaking into declaration `docComment` fields.
|
|
124
|
+
* Module-level comments are extracted separately by `extractModuleComment`.
|
|
125
|
+
*
|
|
126
|
+
* @param node - the TypeScript node to check
|
|
127
|
+
* @returns true if any JSDoc tag on the node is `@module`
|
|
128
|
+
*/
|
|
129
|
+
export declare const hasModuleTag: (node: ts.Node) => boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Clean raw JSDoc comment text by removing comment markers and leading asterisks.
|
|
132
|
+
*
|
|
133
|
+
* Transforms `/** ... *\/` style comments into clean text.
|
|
134
|
+
*
|
|
135
|
+
* @param commentText - the raw comment text including `/**` and `*\/` markers
|
|
136
|
+
* @returns cleaned comment text, or undefined if empty after cleaning
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* cleanComment('/** Hello world *\/') // => 'Hello world'
|
|
141
|
+
* cleanComment('/**\n * Line 1\n * Line 2\n *\/') // => 'Line 1\nLine 2'
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare const cleanComment: (commentText: string) => string | undefined;
|
|
145
|
+
//# sourceMappingURL=tsdoc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tsdoc.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/tsdoc.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,OAAO,KAAK,EAAC,oBAAoB,EAAE,eAAe,EAAC,MAAM,wBAAwB,CAAC;AAElF;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IAClC,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,gDAAgD;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oCAAoC;IACpC,MAAM,CAAC,EAAE,KAAK,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAC,CAAC,CAAC;IACrD,qCAAqC;IACrC,QAAQ,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACzB,8CAA8C;IAC9C,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sCAAsC;IACtC,OAAO,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACxB,yCAAyC;IACzC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yCAAyC;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uFAAuF;IACvF,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,iEAAiE;IACjE,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB;AAYD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,YAAY,GACxB,MAAM,EAAE,CAAC,IAAI,EACb,YAAY,EAAE,CAAC,UAAU,KACvB,kBAAkB,GAAG,SAoGvB,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,GAC9B,aAAa,oBAAoB,GAAG,eAAe,EACnD,OAAO,kBAAkB,GAAG,SAAS,KACnC,IAgCF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,EAAE,CAAC,IAAI,KAAG,OACsB,CAAC;AAEpE;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,YAAY,GAAI,aAAa,MAAM,KAAG,MAAM,GAAG,SAW3D,CAAC"}
|
package/dist/tsdoc.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TSDoc/JSDoc parsing helpers using the TypeScript compiler API.
|
|
3
|
+
*
|
|
4
|
+
* Provides `parseComment` for extracting JSDoc/TSDoc from TypeScript nodes.
|
|
5
|
+
* Primarily designed for build-time code generation but can be used at runtime.
|
|
6
|
+
*
|
|
7
|
+
* ## Design
|
|
8
|
+
*
|
|
9
|
+
* Pure extraction approach: extracts documentation as-is with minimal transformation,
|
|
10
|
+
* preserving source intent. Works around TypeScript compiler API quirks where needed.
|
|
11
|
+
*
|
|
12
|
+
* Supports both regular TypeScript and Svelte components (via svelte2tsx output).
|
|
13
|
+
*
|
|
14
|
+
* ## Tag support
|
|
15
|
+
*
|
|
16
|
+
* Supports a subset of standard TSDoc tags:
|
|
17
|
+
* `@param`, `@returns`, `@throws`, `@example`, `@deprecated`, `@see`, `@since`, `@default`, `@nodocs`.
|
|
18
|
+
*
|
|
19
|
+
* The `@nodocs` tag excludes exports from documentation and flat namespace validation.
|
|
20
|
+
* The declaration is still exported and usable, just not documented.
|
|
21
|
+
*
|
|
22
|
+
* Also supports `@mutates` (non-standard) for documenting mutations to parameters or external state.
|
|
23
|
+
* Uses same format as `@param`: `@mutates key - description of mutation`. The key is
|
|
24
|
+
* unvalidated — typically a parameter name, but compound paths (`this.foo`, `obj.field`)
|
|
25
|
+
* and external state references are accepted as-is.
|
|
26
|
+
*
|
|
27
|
+
* Only `@returns` is supported (not `@return`).
|
|
28
|
+
*
|
|
29
|
+
* The `@see` tag supports multiple formats: plain URLs (`https://...`), `{@link}` syntax, and module names.
|
|
30
|
+
* Relative/absolute path support in `@see` is TBD.
|
|
31
|
+
*
|
|
32
|
+
* ## Behavioral notes
|
|
33
|
+
*
|
|
34
|
+
* Due to TS Compiler API limitations:
|
|
35
|
+
* - TS API includes dash separator in `@param` tag text; we strip the leading `- ` as it's syntax, not content
|
|
36
|
+
* - `@throws` tags have `{Type}` stripped by TS API; fallback regex extracts first word as error type
|
|
37
|
+
* - TS API strips URL protocols from `@see` tag text; we use `getText()` to preserve original format including `{@link}` syntax
|
|
38
|
+
*
|
|
39
|
+
* @see `declaration-build.ts` for `DeclarationJsonBuild`
|
|
40
|
+
* @see `typescript-exports.ts` and `svelte.ts` as primary consumers
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
import ts from 'typescript';
|
|
45
|
+
/**
|
|
46
|
+
* Clean TSDoc tag description text by stripping leading `- ` separator.
|
|
47
|
+
* TSDoc syntax uses `- ` as a separator, but it shouldn't be part of the description content.
|
|
48
|
+
* Only strips the first `- ` to preserve markdown lists later in the text.
|
|
49
|
+
*
|
|
50
|
+
* @param text - the tag text to clean
|
|
51
|
+
* @returns cleaned text with leading `- ` removed if present
|
|
52
|
+
*/
|
|
53
|
+
const cleanTagDescription = (text) => text.trim().replace(/^-\s+/, '');
|
|
54
|
+
/**
|
|
55
|
+
* Parse JSDoc comment from a TypeScript node.
|
|
56
|
+
*
|
|
57
|
+
* Extracts and parses all JSDoc tags including:
|
|
58
|
+
*
|
|
59
|
+
* - `@param` - parameter descriptions
|
|
60
|
+
* - `@returns` - return value description
|
|
61
|
+
* - `@throws` - error documentation
|
|
62
|
+
* - `@example` - code examples
|
|
63
|
+
* - `@deprecated` - deprecation warnings
|
|
64
|
+
* - `@see` - related references
|
|
65
|
+
* - `@since` - version information
|
|
66
|
+
* - `@default` - default value
|
|
67
|
+
* - `@mutates` - mutation documentation (non-standard)
|
|
68
|
+
* - `@nodocs` - exclusion flag (non-standard)
|
|
69
|
+
*
|
|
70
|
+
* @param node - the TypeScript node to extract JSDoc from
|
|
71
|
+
* @param sourceFile - source file (used for extracting full `@see` tag text)
|
|
72
|
+
* @returns parsed comment with structured metadata, or undefined if no JSDoc found
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const tsdoc = parseComment(declarationNode, sourceFile);
|
|
77
|
+
* if (tsdoc) {
|
|
78
|
+
* console.log(tsdoc.text); // main comment text
|
|
79
|
+
* console.log(tsdoc.params); // {paramName: 'description'}
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export const parseComment = (node, sourceFile) => {
|
|
84
|
+
const tsdocComments = ts.getJSDocCommentsAndTags(node);
|
|
85
|
+
if (tsdocComments.length === 0)
|
|
86
|
+
return undefined;
|
|
87
|
+
let fullText = '';
|
|
88
|
+
const params = {};
|
|
89
|
+
let returns;
|
|
90
|
+
let throws;
|
|
91
|
+
let examples;
|
|
92
|
+
let deprecatedMessage;
|
|
93
|
+
let seeAlso;
|
|
94
|
+
let since;
|
|
95
|
+
let defaultValue;
|
|
96
|
+
let mutates;
|
|
97
|
+
let nodocs;
|
|
98
|
+
// Extract main comment text
|
|
99
|
+
for (const comment of tsdocComments) {
|
|
100
|
+
if (ts.isJSDoc(comment) && comment.comment) {
|
|
101
|
+
const text = typeof comment.comment === 'string'
|
|
102
|
+
? comment.comment
|
|
103
|
+
: comment.comment.map((c) => c.text).join('');
|
|
104
|
+
fullText += text + '\n';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Extract tags
|
|
108
|
+
const tags = ts.getJSDocTags(node);
|
|
109
|
+
for (const tag of tags) {
|
|
110
|
+
const tagText = typeof tag.comment === 'string' ? tag.comment : tag.comment?.map((c) => c.text).join('');
|
|
111
|
+
const tagName = tag.tagName.text;
|
|
112
|
+
if (tagName === 'param' && ts.isJSDocParameterTag(tag)) {
|
|
113
|
+
// Extract parameter name and description
|
|
114
|
+
const paramName = ts.isIdentifier(tag.name) ? tag.name.text : tag.name.getText();
|
|
115
|
+
if (paramName && tagText) {
|
|
116
|
+
params[paramName] = cleanTagDescription(tagText);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else if (tagName === 'returns' && tagText) {
|
|
120
|
+
returns = tagText.trim();
|
|
121
|
+
}
|
|
122
|
+
else if (tagName === 'throws' && tagText) {
|
|
123
|
+
// Try to extract error type and description
|
|
124
|
+
const match = /^\{?(\w+)\}?\s+(.+)/.exec(tagText);
|
|
125
|
+
if (match) {
|
|
126
|
+
(throws ??= []).push({ type: match[1], description: cleanTagDescription(match[2]) });
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
(throws ??= []).push({ description: cleanTagDescription(tagText) });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else if (tagName === 'example' && tagText) {
|
|
133
|
+
(examples ??= []).push(tagText.trim());
|
|
134
|
+
}
|
|
135
|
+
else if (tagName === 'deprecated') {
|
|
136
|
+
deprecatedMessage = tagText?.trim() ?? '';
|
|
137
|
+
}
|
|
138
|
+
else if (tagName === 'see') {
|
|
139
|
+
// The TS API strips 'https' from URLs in @see tags, so get full text from source
|
|
140
|
+
const fullTagText = tag.getText(sourceFile);
|
|
141
|
+
// Extract content after @see, handling JSDoc formatting artifacts
|
|
142
|
+
const seeContent = fullTagText
|
|
143
|
+
.replace(/^@see\s+/, '') // remove @see prefix
|
|
144
|
+
.replace(/\n\s*\*\s*/g, ' ') // remove JSDoc line continuations
|
|
145
|
+
.replace(/\s*\*\s*$/, '') // remove trailing asterisk artifacts
|
|
146
|
+
.trim();
|
|
147
|
+
if (seeContent) {
|
|
148
|
+
(seeAlso ??= []).push(seeContent);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else if (tagName === 'since' && tagText) {
|
|
152
|
+
since = tagText.trim();
|
|
153
|
+
}
|
|
154
|
+
else if (tagName === 'default' && tagText) {
|
|
155
|
+
defaultValue = tagText.trim();
|
|
156
|
+
}
|
|
157
|
+
else if (tagName === 'mutates' && tagText) {
|
|
158
|
+
// Extract parameter name and description (format: @mutates paramName - description)
|
|
159
|
+
const cleanedText = cleanTagDescription(tagText);
|
|
160
|
+
const match = /^(\w+)\s+-?\s*(.+)/.exec(cleanedText);
|
|
161
|
+
if (match) {
|
|
162
|
+
const paramName = match[1];
|
|
163
|
+
const description = match[2].trim();
|
|
164
|
+
(mutates ??= {})[paramName] = description;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (tagName === 'nodocs') {
|
|
168
|
+
nodocs = true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
fullText = fullText.trim();
|
|
172
|
+
return {
|
|
173
|
+
text: fullText,
|
|
174
|
+
params,
|
|
175
|
+
returns,
|
|
176
|
+
throws,
|
|
177
|
+
examples,
|
|
178
|
+
deprecatedMessage,
|
|
179
|
+
seeAlso,
|
|
180
|
+
since,
|
|
181
|
+
defaultValue,
|
|
182
|
+
mutates,
|
|
183
|
+
nodocs,
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Apply parsed TSDoc metadata to a declaration.
|
|
188
|
+
*
|
|
189
|
+
* Consolidates the common pattern of assigning TSDoc fields to declarations,
|
|
190
|
+
* with conditional assignment for array fields (only if non-empty).
|
|
191
|
+
*
|
|
192
|
+
* @param declaration - declaration object to update
|
|
193
|
+
* @param tsdoc - parsed TSDoc comment (if available)
|
|
194
|
+
* @mutates declaration - adds docComment, deprecatedMessage, examples, seeAlso, throws, since, mutates, defaultValue fields
|
|
195
|
+
*/
|
|
196
|
+
export const applyToDeclaration = (declaration, tsdoc) => {
|
|
197
|
+
if (!tsdoc)
|
|
198
|
+
return;
|
|
199
|
+
if (tsdoc.text) {
|
|
200
|
+
declaration.docComment = tsdoc.text;
|
|
201
|
+
}
|
|
202
|
+
if (tsdoc.deprecatedMessage !== undefined) {
|
|
203
|
+
declaration.deprecatedMessage = tsdoc.deprecatedMessage;
|
|
204
|
+
}
|
|
205
|
+
// Only assign arrays if they have content
|
|
206
|
+
if (tsdoc.examples?.length) {
|
|
207
|
+
declaration.examples = tsdoc.examples;
|
|
208
|
+
}
|
|
209
|
+
if (tsdoc.seeAlso?.length) {
|
|
210
|
+
declaration.seeAlso = tsdoc.seeAlso;
|
|
211
|
+
}
|
|
212
|
+
if (tsdoc.throws?.length) {
|
|
213
|
+
declaration.throws = tsdoc.throws;
|
|
214
|
+
}
|
|
215
|
+
if (tsdoc.since) {
|
|
216
|
+
declaration.since = tsdoc.since;
|
|
217
|
+
}
|
|
218
|
+
if (tsdoc.mutates && Object.keys(tsdoc.mutates).length > 0) {
|
|
219
|
+
declaration.mutates = tsdoc.mutates;
|
|
220
|
+
}
|
|
221
|
+
// `defaultValue` is schema-allowed on variable declarations/members only;
|
|
222
|
+
// `z.strictObject` would reject it on other kinds. Component props consume
|
|
223
|
+
// the parsed `defaultValue` directly in `svelte.ts` (not via this helper).
|
|
224
|
+
if (tsdoc.defaultValue !== undefined && declaration.kind === 'variable') {
|
|
225
|
+
declaration.defaultValue = tsdoc.defaultValue;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Check if a TypeScript node has a `@module` JSDoc tag.
|
|
230
|
+
*
|
|
231
|
+
* Used to prevent `@module` comments from leaking into declaration `docComment` fields.
|
|
232
|
+
* Module-level comments are extracted separately by `extractModuleComment`.
|
|
233
|
+
*
|
|
234
|
+
* @param node - the TypeScript node to check
|
|
235
|
+
* @returns true if any JSDoc tag on the node is `@module`
|
|
236
|
+
*/
|
|
237
|
+
export const hasModuleTag = (node) => ts.getJSDocTags(node).some((tag) => tag.tagName.text === 'module');
|
|
238
|
+
/**
|
|
239
|
+
* Clean raw JSDoc comment text by removing comment markers and leading asterisks.
|
|
240
|
+
*
|
|
241
|
+
* Transforms `/** ... *\/` style comments into clean text.
|
|
242
|
+
*
|
|
243
|
+
* @param commentText - the raw comment text including `/**` and `*\/` markers
|
|
244
|
+
* @returns cleaned comment text, or undefined if empty after cleaning
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* cleanComment('/** Hello world *\/') // => 'Hello world'
|
|
249
|
+
* cleanComment('/**\n * Line 1\n * Line 2\n *\/') // => 'Line 1\nLine 2'
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export const cleanComment = (commentText) => {
|
|
253
|
+
const text = commentText
|
|
254
|
+
.replace(/^\/\*\*/, '')
|
|
255
|
+
.replace(/\*\/$/, '')
|
|
256
|
+
.replace(/\r\n/g, '\n')
|
|
257
|
+
.split('\n')
|
|
258
|
+
.map((line) => line.replace(/^\s*\*\s?/, ''))
|
|
259
|
+
.join('\n')
|
|
260
|
+
.trim();
|
|
261
|
+
return text || undefined;
|
|
262
|
+
};
|