首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >printer.printFile打印空字符串

printer.printFile打印空字符串
EN

Stack Overflow用户
提问于 2021-08-02 20:58:44
回答 1查看 115关注 0票数 0

我正在使用编译器API来创建类型记录代码。在分配const printer = createPrinter()之后,我可以使用printer.printNodeprinter.printList打印AST。但出于某种原因,printer.printFile总是打印空字符串。

代码语言:javascript
复制
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总是打印空字符串?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-19 05:58:28

我不知道为什么,但看起来factory.updateSourceFile返回更新的SourceFile

代码语言:javascript
复制
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 {
// }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68627873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档