2
2
3
3
/**
4
4
* @fileoverview AI-Powered JSDoc Generation Script
5
- *
5
+ *
6
6
* Automatically generates comprehensive JSDoc comments for JavaScript files
7
7
* using AI analysis. Integrates with git pre-commit hooks for seamless
8
8
* developer experience.
9
- *
9
+ *
10
10
* @module JSDocAI
11
11
* @since 1.0.0
12
12
*/
@@ -25,15 +25,15 @@ const __dirname = dirname(__filename);
25
25
*/
26
26
function getStagedJSFiles ( ) {
27
27
try {
28
- const output = execSync ( 'git diff --cached --name-only --diff-filter=ACM' , {
28
+ const output = execSync ( 'git diff --cached --name-only --diff-filter=ACM' , {
29
29
encoding : 'utf8' ,
30
30
cwd : join ( __dirname , '..' )
31
31
} ) ;
32
-
32
+
33
33
return output
34
34
. split ( '\n' )
35
- . filter ( file => file . trim ( ) && file . endsWith ( '.js' ) )
36
- . map ( file => file . trim ( ) ) ;
35
+ . filter ( ( file ) => file . trim ( ) && file . endsWith ( '.js' ) )
36
+ . map ( ( file ) => file . trim ( ) ) ;
37
37
} catch ( error ) {
38
38
console . log ( 'No staged files found or not in git repository' ) ;
39
39
return [ ] ;
@@ -54,35 +54,35 @@ function analyzeCodeStructure(code) {
54
54
imports : / i m p o r t \s + .* ?f r o m \s + [ ' " ` ] ( [ ^ ' " ` ] + ) [ ' " ` ] / g
55
55
} ;
56
56
57
- let analysis = " Analyze this JavaScript code and generate comprehensive JSDoc comments:\n\n" ;
58
-
57
+ let analysis = ' Analyze this JavaScript code and generate comprehensive JSDoc comments:\n\n' ;
58
+
59
59
// Detect patterns
60
60
const classes = [ ...code . matchAll ( patterns . classes ) ] ;
61
61
const functions = [ ...code . matchAll ( patterns . functions ) ] ;
62
62
const imports = [ ...code . matchAll ( patterns . imports ) ] ;
63
-
63
+
64
64
if ( classes . length > 0 ) {
65
- analysis += `Classes found: ${ classes . map ( m => m [ 1 ] ) . join ( ', ' ) } \n` ;
65
+ analysis += `Classes found: ${ classes . map ( ( m ) => m [ 1 ] ) . join ( ', ' ) } \n` ;
66
66
}
67
-
67
+
68
68
if ( functions . length > 0 ) {
69
- analysis += `Functions found: ${ functions . map ( m => m [ 1 ] ) . join ( ', ' ) } \n` ;
69
+ analysis += `Functions found: ${ functions . map ( ( m ) => m [ 1 ] ) . join ( ', ' ) } \n` ;
70
70
}
71
-
71
+
72
72
if ( imports . length > 0 ) {
73
- analysis += `Dependencies: ${ imports . map ( m => m [ 1 ] ) . join ( ', ' ) } \n` ;
73
+ analysis += `Dependencies: ${ imports . map ( ( m ) => m [ 1 ] ) . join ( ', ' ) } \n` ;
74
74
}
75
75
76
- analysis += " \nGenerate JSDoc with:\n" ;
77
- analysis += " - @fileoverview for file header\n" ;
78
- analysis += " - @param with accurate types for all parameters\n" ;
79
- analysis += " - @returns with specific return types\n" ;
80
- analysis += " - @throws for error conditions\n" ;
81
- analysis += " - @example for complex functions\n" ;
82
- analysis += " - @since version tags\n" ;
83
- analysis += " - @module declarations\n\n" ;
84
- analysis += " IMPORTANT: Only add JSDoc where missing. Preserve existing JSDoc comments.\n" ;
85
-
76
+ analysis += ' \nGenerate JSDoc with:\n' ;
77
+ analysis += ' - @fileoverview for file header\n' ;
78
+ analysis += ' - @param with accurate types for all parameters\n' ;
79
+ analysis += ' - @returns with specific return types\n' ;
80
+ analysis += ' - @throws for error conditions\n' ;
81
+ analysis += ' - @example for complex functions\n' ;
82
+ analysis += ' - @since version tags\n' ;
83
+ analysis += ' - @module declarations\n\n' ;
84
+ analysis += ' IMPORTANT: Only add JSDoc where missing. Preserve existing JSDoc comments.\n' ;
85
+
86
86
return analysis ;
87
87
}
88
88
@@ -95,11 +95,11 @@ async function generateJSDocForFile(filePath) {
95
95
try {
96
96
const absolutePath = join ( process . cwd ( ) , filePath ) ;
97
97
const code = readFileSync ( absolutePath , 'utf8' ) ;
98
-
98
+
99
99
// Skip if already has comprehensive JSDoc
100
100
const jsdocCount = ( code . match ( / \/ \* \* [ \s \S ] * ?\* \/ / g) || [ ] ) . length ;
101
101
const functionsCount = ( code . match ( / (?: f u n c t i o n | c l a s s | \w + \s * \( [ ^ ) ] * \) \s * { ) / g) || [ ] ) . length ;
102
-
102
+
103
103
if ( jsdocCount >= functionsCount * 0.8 ) {
104
104
console . log ( `✓ ${ filePath } already has good JSDoc coverage` ) ;
105
105
return false ;
@@ -108,19 +108,18 @@ async function generateJSDocForFile(filePath) {
108
108
const prompt = analyzeCodeStructure ( code ) ;
109
109
console . log ( `📝 Analysis for ${ filePath } :` ) ;
110
110
console . log ( prompt ) ;
111
-
111
+
112
112
// For demo purposes, just indicate what would be done
113
113
// In production, this would call Claude API or use local AI
114
114
console . log ( `\n🤖 AI JSDoc generation would be applied to ${ filePath } ` ) ;
115
115
console . log ( ` Found ${ functionsCount } functions/classes, ${ jsdocCount } have JSDoc` ) ;
116
- console . log ( ` 📋 Prompt ready for AI processing` ) ;
117
-
116
+ console . log ( ' 📋 Prompt ready for AI processing' ) ;
117
+
118
118
// For safety in demo, don't modify files
119
119
// Uncomment below to enable actual file modification:
120
120
// writeFileSync(absolutePath, enhancedCode);
121
-
121
+
122
122
return false ; // Return true when actually modifying files
123
-
124
123
} catch ( error ) {
125
124
console . error ( `✗ Error processing ${ filePath } :` , error . message ) ;
126
125
return false ;
@@ -134,16 +133,16 @@ async function generateJSDocForFile(filePath) {
134
133
*/
135
134
async function main ( targetFiles = null ) {
136
135
const files = targetFiles || getStagedJSFiles ( ) ;
137
-
136
+
138
137
if ( files . length === 0 ) {
139
138
console . log ( 'No JavaScript files to process' ) ;
140
139
return ;
141
140
}
142
141
143
142
console . log ( `🤖 Processing ${ files . length } JavaScript files for JSDoc enhancement...` ) ;
144
-
143
+
145
144
let modifiedCount = 0 ;
146
-
145
+
147
146
for ( const file of files ) {
148
147
const wasModified = await generateJSDocForFile ( file ) ;
149
148
if ( wasModified ) {
@@ -156,7 +155,7 @@ async function main(targetFiles = null) {
156
155
}
157
156
}
158
157
}
159
-
158
+
160
159
console . log ( `🚀 Enhanced ${ modifiedCount } /${ files . length } files with AI-generated JSDoc` ) ;
161
160
}
162
161
@@ -166,4 +165,4 @@ if (process.argv[1] === __filename) {
166
165
main ( targetFiles . length > 0 ? targetFiles : null ) . catch ( console . error ) ;
167
166
}
168
167
169
- export { main , generateJSDocForFile , analyzeCodeStructure , getStagedJSFiles } ;
168
+ export { main , generateJSDocForFile , analyzeCodeStructure , getStagedJSFiles } ;
0 commit comments