Skip to content

Commit 9b14db0

Browse files
committed
fix: generate logic
1 parent ea20a84 commit 9b14db0

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

src/index.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function generateJSDoc(filePath: string): void {
1616

1717
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
1818

19-
function visit(node: ts.Node): ts.Node {
19+
// Visit function that processes each node
20+
function visit(node: ts.Node, context: ts.TransformationContext): ts.Node {
2021
if (ts.isFunctionDeclaration(node) && node.name) {
2122
const jsDoc = createJSDoc(node.name.text, node.parameters, node.type);
2223
ts.addSyntheticLeadingComment(
@@ -56,32 +57,20 @@ export function generateJSDoc(filePath: string): void {
5657
return node;
5758
}
5859

59-
return ts.visitEachChild(node, visit, context);
60+
return ts.visitEachChild(node, (child) => visit(child, context), context); // Pass context to visit
6061
}
6162

62-
// Create a transformation context
63-
const context: ts.TransformationContext = {
64-
factory: ts.factory,
65-
startLexicalEnvironment: () => {},
66-
endLexicalEnvironment: () => [],
67-
suspendLexicalEnvironment: () => {},
68-
resumeLexicalEnvironment: () => {},
69-
requestEmitHelper: () => {},
70-
readEmitHelpers: () => undefined,
71-
enableSubstitution: () => {},
72-
isSubstitutionEnabled: () => false,
73-
onSubstituteNode: (hint, node) => node,
74-
enableEmitNotification: () => {},
75-
isEmitNotificationEnabled: () => false,
76-
onEmitNode: (hint, node, emitCallback) => emitCallback(hint, node),
77-
getCompilerOptions: () => ({} as ts.CompilerOptions),
78-
hoistFunctionDeclaration: () => {},
79-
hoistVariableDeclaration: () => {},
63+
// Create the transformer
64+
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
65+
return (sourceFile) => {
66+
return ts.visitNode(sourceFile, (node) =>
67+
visit(node, context)
68+
) as ts.SourceFile; // Pass context to visit
69+
};
8070
};
8171

82-
const result = ts.transform(sourceFile, [
83-
(context) => (rootNode) => ts.visitNode(rootNode, visit),
84-
]);
72+
// Apply the transformer
73+
const result = ts.transform(sourceFile, [transformer]);
8574
const transformedSourceFile = result.transformed[0] as ts.SourceFile;
8675
const updatedCode = printer.printFile(transformedSourceFile);
8776

0 commit comments

Comments
 (0)