Typescript 編譯器 API 允許我們處理抽象語法樹 (AST) 并生成源代碼。我們可以通過創建一個printer物件并使用printNode函式來做到這一點
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
printer.printNode(ts.EmitHint.Unspecified, node, sourceFile))
我面臨的問題是我的 AST 在不同的地方包含很多 jsDoc,我想忽略它們并列印沒有注釋的代碼。有沒有什么辦法可以在不手動遍歷 AST 和洗掉 jsDoc 物件的情況下做到這一點?
uj5u.com熱心網友回復:
基于查看列印機代碼(查看shouldWriteComment并遵循代碼),似乎不可能開箱即用。
您可以破解此問題的一種方法是將所有具有 jsdocs 的宣告的位置設定為-1or node.pos = node.getStart(sourceFile):
> ts.createPrinter({}).printNode(node, sourceFile)
'/** Test */\r\nfunction test() {\r\n test;\r\n}\r\n'
> node.pos = -1
> ts.createPrinter({}).printNode(node, sourceFile)
'function test() {\r\n test;\r\n}\r\n'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/382657.html
上一篇:型別轉換條目值?
下一篇:如何在打字稿中定義一個空物件
