jsdoc-builder 0.0.5 → 0.0.6

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.
Files changed (2) hide show
  1. package/dist/index.js +16 -14
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -5,46 +5,48 @@ const ts = require("typescript");
5
5
  const fs = require("fs");
6
6
  /**
7
7
  * Parses a TypeScript or JavaScript file and adds JSDoc comments to functions.
8
+ * Skips functions that already have JSDoc comments.
8
9
  * @param filePath - The path of the file to process.
9
10
  */
10
11
  function generateJSDoc(filePath) {
11
12
  const sourceCode = fs.readFileSync(filePath, "utf-8");
12
13
  const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true);
13
14
  const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
14
- // Visit function that processes each node
15
15
  function visit(node, context) {
16
- if (ts.isFunctionDeclaration(node) && node.name) {
16
+ if (ts.isFunctionDeclaration(node) && node.name && !hasJSDoc(node)) {
17
17
  const jsDoc = createJSDoc(node.name.text, node.parameters, node.type);
18
18
  ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, jsDoc.comment, true);
19
19
  return node;
20
20
  }
21
- // Check for arrow functions assigned to a constant
22
21
  if (ts.isVariableDeclaration(node) &&
23
22
  node.initializer &&
24
- ts.isArrowFunction(node.initializer)) {
23
+ ts.isArrowFunction(node.initializer) &&
24
+ !hasJSDoc(node.parent.parent) // VariableStatement에 JSDoc이 있는지 확인
25
+ ) {
25
26
  const jsDoc = createJSDoc(node.name.getText(), node.initializer.parameters, node.initializer.type);
26
- // Add JSDoc to the entire variable statement for clarity
27
- const parent = node.parent;
28
- if (ts.isVariableDeclarationList(parent) &&
29
- parent.declarations.length === 1) {
30
- ts.addSyntheticLeadingComment(parent.parent, ts.SyntaxKind.MultiLineCommentTrivia, jsDoc.comment, true);
31
- }
27
+ ts.addSyntheticLeadingComment(node.parent.parent, ts.SyntaxKind.MultiLineCommentTrivia, jsDoc.comment, true);
32
28
  return node;
33
29
  }
34
- return ts.visitEachChild(node, (child) => visit(child, context), context); // Pass context to visit
30
+ return ts.visitEachChild(node, (child) => visit(child, context), context);
35
31
  }
36
- // Create the transformer
37
32
  const transformer = (context) => {
38
33
  return (sourceFile) => {
39
- return ts.visitNode(sourceFile, (node) => visit(node, context)); // Pass context to visit
34
+ return ts.visitNode(sourceFile, (node) => visit(node, context));
40
35
  };
41
36
  };
42
- // Apply the transformer
43
37
  const result = ts.transform(sourceFile, [transformer]);
44
38
  const transformedSourceFile = result.transformed[0];
45
39
  const updatedCode = printer.printFile(transformedSourceFile);
46
40
  fs.writeFileSync(filePath, updatedCode, "utf-8");
47
41
  }
42
+ /**
43
+ * Checks if a node already has JSDoc comments.
44
+ * @param node - The TypeScript AST node to check.
45
+ * @returns Whether the node has JSDoc.
46
+ */
47
+ function hasJSDoc(node) {
48
+ return !!ts.getJSDocTags(node).length;
49
+ }
48
50
  /**
49
51
  * Creates a JSDoc comment.
50
52
  * @param functionName - The name of the function.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsdoc-builder",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "author": "dori",
5
5
  "description": "Generate JSDoc comments for JavaScript and TypeScript files.",
6
6
  "publishConfig": {