我正在使用编译器API来创建类型记录代码。在分配const printer = createPrinter()之后,我可以使用printer.printNode和printer.printList打印AST。但出于某种原因,printer.printFile总是打印空字符串。
const printer = createPrinter();
const sourceFile = createSourceFile(
'dummy.ts',
'',
ScriptTarget.ESNext,
false,
ScriptKind.TS
);
const classDeclaration = factory.createClassDeclaration(
undefined,
undefined,
'Foo',
undefined,
undefined,
[]
);
factory.updateSourceFile(sourceFile, factory.createNodeArray([classDeclaration]));
console.log(printer.printFile(sourceFile)); // '';为什么printer.printFile总是打印空字符串?
发布于 2022-04-19 05:58:28
我不知道为什么,但看起来factory.updateSourceFile返回更新的SourceFile。
const printer = createPrinter();
// const -> let
let sourceFile = createSourceFile(
'dummy.ts',
'',
ScriptTarget.ESNext,
false,
ScriptKind.TS
);
const classDeclaration = factory.createClassDeclaration(
undefined,
undefined,
'Foo',
undefined,
undefined,
[]
);
// assign the updated source file
sourceFile = factory.updateSourceFile(sourceFile, factory.createNodeArray([classDeclaration]));
console.log(printer.printFile(sourceFile));
// class Foo {
// }https://stackoverflow.com/questions/68627873
复制相似问题