11/* eslint-disable n/prefer-global/process, unicorn/no-process-exit, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-argument */
22import { readFileSync } from 'node:fs' ;
33import { execSync } from 'node:child_process' ;
4- import { Project } from 'ts-morph' ;
4+ import { Project , type JSDocableNode } from 'ts-morph' ;
55// Import index.ts to populate the test data via side effect
66// eslint-disable-next-line import-x/no-unassigned-import
77import './index.ts' ;
@@ -24,6 +24,49 @@ const sourceFile = project.createSourceFile('temp.d.ts', dtsContent, {overwrite:
2424
2525let examplesAdded = 0 ;
2626
27+ /**
28+ * Add example URLs to a JSDocable node (e.g., variable statement or type alias)
29+ */
30+ function addExamplesToNode ( node : JSDocableNode , urlExamples : string [ ] ) : void {
31+ const jsDoc = node . getJsDocs ( ) [ 0 ] ;
32+
33+ if ( jsDoc ) {
34+ // Add @example tags to existing JSDoc
35+ const existingTags = jsDoc . getTags ( ) ;
36+ const description = jsDoc . getDescription ( ) . trim ( ) ;
37+
38+ // Build new JSDoc content
39+ const newJsDocLines : string [ ] = [ ] ;
40+ if ( description ) {
41+ newJsDocLines . push ( description ) ;
42+ }
43+
44+ // Add existing tags (that aren't @example tags)
45+ for ( const tag of existingTags ) {
46+ if ( tag . getTagName ( ) !== 'example' ) {
47+ newJsDocLines . push ( tag . getText ( ) ) ;
48+ }
49+ }
50+
51+ // Add new @example tags
52+ for ( const url of urlExamples ) {
53+ newJsDocLines . push ( `@example ${ url } ` ) ;
54+ }
55+
56+ // Replace the JSDoc
57+ jsDoc . remove ( ) ;
58+ node . addJsDoc ( newJsDocLines . join ( '\n' ) ) ;
59+ } else {
60+ // Create new JSDoc with examples
61+ const jsDocLines : string [ ] = [ ] ;
62+ for ( const url of urlExamples ) {
63+ jsDocLines . push ( `@example ${ url } ` ) ;
64+ }
65+
66+ node . addJsDoc ( jsDocLines . join ( '\n' ) ) ;
67+ }
68+ }
69+
2770// Process each exported variable declaration (these are the function declarations)
2871for ( const statement of sourceFile . getVariableStatements ( ) ) {
2972 // Only process exported statements
@@ -43,45 +86,7 @@ for (const statement of sourceFile.getVariableStatements()) {
4386 const urlExamples = examples . filter ( ( url : string ) => url . startsWith ( 'http' ) ) ;
4487
4588 if ( urlExamples . length > 0 ) {
46- // Get or create JSDoc for this statement (not the declaration)
47- const jsDoc = statement . getJsDocs ( ) [ 0 ] ;
48-
49- if ( jsDoc ) {
50- // Add @example tags to existing JSDoc
51- const existingTags = jsDoc . getTags ( ) ;
52- const description = jsDoc . getDescription ( ) . trim ( ) ;
53-
54- // Build new JSDoc content
55- const newJsDocLines : string [ ] = [ ] ;
56- if ( description ) {
57- newJsDocLines . push ( description ) ;
58- }
59-
60- // Add existing tags (that aren't @example tags)
61- for ( const tag of existingTags ) {
62- if ( tag . getTagName ( ) !== 'example' ) {
63- newJsDocLines . push ( tag . getText ( ) ) ;
64- }
65- }
66-
67- // Add new @example tags
68- for ( const url of urlExamples ) {
69- newJsDocLines . push ( `@example ${ url } ` ) ;
70- }
71-
72- // Replace the JSDoc
73- jsDoc . remove ( ) ;
74- statement . addJsDoc ( newJsDocLines . join ( '\n' ) ) ;
75- } else {
76- // Create new JSDoc with examples
77- const jsDocLines : string [ ] = [ ] ;
78- for ( const url of urlExamples ) {
79- jsDocLines . push ( `@example ${ url } ` ) ;
80- }
81-
82- statement . addJsDoc ( jsDocLines . join ( '\n' ) ) ;
83- }
84-
89+ addExamplesToNode ( statement , urlExamples ) ;
8590 examplesAdded += urlExamples . length ;
8691 }
8792 }
@@ -103,38 +108,7 @@ for (const typeAlias of sourceFile.getTypeAliases()) {
103108 const urlExamples = examples . filter ( ( url : string ) => url . startsWith ( 'http' ) ) ;
104109
105110 if ( urlExamples . length > 0 ) {
106- const jsDoc = typeAlias . getJsDocs ( ) [ 0 ] ;
107-
108- if ( jsDoc ) {
109- const existingTags = jsDoc . getTags ( ) ;
110- const description = jsDoc . getDescription ( ) . trim ( ) ;
111-
112- const newJsDocLines : string [ ] = [ ] ;
113- if ( description ) {
114- newJsDocLines . push ( description ) ;
115- }
116-
117- for ( const tag of existingTags ) {
118- if ( tag . getTagName ( ) !== 'example' ) {
119- newJsDocLines . push ( tag . getText ( ) ) ;
120- }
121- }
122-
123- for ( const url of urlExamples ) {
124- newJsDocLines . push ( `@example ${ url } ` ) ;
125- }
126-
127- jsDoc . remove ( ) ;
128- typeAlias . addJsDoc ( newJsDocLines . join ( '\n' ) ) ;
129- } else {
130- const jsDocLines : string [ ] = [ ] ;
131- for ( const url of urlExamples ) {
132- jsDocLines . push ( `@example ${ url } ` ) ;
133- }
134-
135- typeAlias . addJsDoc ( jsDocLines . join ( '\n' ) ) ;
136- }
137-
111+ addExamplesToNode ( typeAlias , urlExamples ) ;
138112 examplesAdded += urlExamples . length ;
139113 }
140114 }
0 commit comments