嘿,
我正在尝试构建一些可以在函数之上添加注释的东西。不幸的是,似乎ts.setSyntheticLeadingComments不允许我替换现有的评论。
我努力了:
ts.setSyntheticLeadingComments(node, [])ts.setSyntheticLeadingComments(node, undefined)node = ts.setSyntheticLeadingComments(node, [])但这些都不起作用。最终,我的目标是能够用新评论替换我本来生成的现有评论。
任何想法 ?谢谢
const transformFactory = (context: ts.TransformationContext) => (
rootNode: ts.SourceFile
): ts.SourceFile => {
const visit = (node: ts.Node) => {
node = ts.visitEachChild(node, visit, context);
ts.setSyntheticLeadingComments(node, []);
return node;
};
return ts.visitNode(rootNode, visit);
};
const sourceFile = ts.createSourceFile(
path,
source,
ts.ScriptTarget.ESNext,
true,
ts.ScriptKind.TS
);
const result = ts.transform(sourceFile, [transformFactory]);
const resultPrinter = ts.createPrinter({ removeComments: false });
console.log(resultPrinter.printFile(result.transformed[0]));
Run Code Online (Sandbox Code Playgroud)
尝试以下转换器,看看如何完全不删除任何注释
使用ts.createPrinter(..., { substituteNode(hint, …