@@ -3,6 +3,7 @@ import * as fs from "fs";
33
44/**
55 * Parses a TypeScript or JavaScript file and adds JSDoc comments to functions.
6+ * Skips functions that already have JSDoc comments.
67 * @param filePath - The path of the file to process.
78 */
89export function generateJSDoc ( filePath : string ) : void {
@@ -16,9 +17,8 @@ export function generateJSDoc(filePath: string): void {
1617
1718 const printer = ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed } ) ;
1819
19- // Visit function that processes each node
2020 function visit ( node : ts . Node , context : ts . TransformationContext ) : ts . Node {
21- if ( ts . isFunctionDeclaration ( node ) && node . name ) {
21+ if ( ts . isFunctionDeclaration ( node ) && node . name && ! hasJSDoc ( node ) ) {
2222 const jsDoc = createJSDoc ( node . name . text , node . parameters , node . type ) ;
2323 ts . addSyntheticLeadingComment (
2424 node ,
@@ -29,54 +29,54 @@ export function generateJSDoc(filePath: string): void {
2929 return node ;
3030 }
3131
32- // Check for arrow functions assigned to a constant
3332 if (
3433 ts . isVariableDeclaration ( node ) &&
3534 node . initializer &&
36- ts . isArrowFunction ( node . initializer )
35+ ts . isArrowFunction ( node . initializer ) &&
36+ ! hasJSDoc ( node . parent . parent ) // VariableStatement에 JSDoc이 있는지 확인
3737 ) {
3838 const jsDoc = createJSDoc (
3939 node . name . getText ( ) ,
4040 node . initializer . parameters ,
4141 node . initializer . type
4242 ) ;
4343
44- // Add JSDoc to the entire variable statement for clarity
45- const parent = node . parent ;
46- if (
47- ts . isVariableDeclarationList ( parent ) &&
48- parent . declarations . length === 1
49- ) {
50- ts . addSyntheticLeadingComment (
51- parent . parent ,
52- ts . SyntaxKind . MultiLineCommentTrivia ,
53- jsDoc . comment ,
54- true
55- ) ;
56- }
44+ ts . addSyntheticLeadingComment (
45+ node . parent . parent ,
46+ ts . SyntaxKind . MultiLineCommentTrivia ,
47+ jsDoc . comment ,
48+ true
49+ ) ;
5750 return node ;
5851 }
5952
60- return ts . visitEachChild ( node , ( child ) => visit ( child , context ) , context ) ; // Pass context to visit
53+ return ts . visitEachChild ( node , ( child ) => visit ( child , context ) , context ) ;
6154 }
6255
63- // Create the transformer
6456 const transformer : ts . TransformerFactory < ts . SourceFile > = ( context ) => {
6557 return ( sourceFile ) => {
6658 return ts . visitNode ( sourceFile , ( node ) =>
6759 visit ( node , context )
68- ) as ts . SourceFile ; // Pass context to visit
60+ ) as ts . SourceFile ;
6961 } ;
7062 } ;
7163
72- // Apply the transformer
7364 const result = ts . transform ( sourceFile , [ transformer ] ) ;
7465 const transformedSourceFile = result . transformed [ 0 ] as ts . SourceFile ;
7566 const updatedCode = printer . printFile ( transformedSourceFile ) ;
7667
7768 fs . writeFileSync ( filePath , updatedCode , "utf-8" ) ;
7869}
7970
71+ /**
72+ * Checks if a node already has JSDoc comments.
73+ * @param node - The TypeScript AST node to check.
74+ * @returns Whether the node has JSDoc.
75+ */
76+ function hasJSDoc ( node : ts . Node ) : boolean {
77+ return ! ! ts . getJSDocTags ( node ) . length ;
78+ }
79+
8080/**
8181 * Creates a JSDoc comment.
8282 * @param functionName - The name of the function.
0 commit comments