Skip to content

Codebase Compiler Emitter

Nathan Shively-Sanders edited this page Sep 2, 2022 · 1 revision

Emitter

The emitter is a tree based syntax emitter. It works by going through the TypeScript AST for a program and emitting source code as it is pipelined.

The emitter itself is "dumb" in the sense that it doesn't contain logic outside of printing whatever AST it is given. So, it's possible that a bug in emission is actually that the AST isn't set up the way that you'd like it.

Outfile

Creating a single file which represents many is done by creating a SyntaxKind.Bundle. Printing happens in function writeBundle(. There are prepends which I don't understand, and then each sourcefile is is printed.

Printer

The printer is a part of the emitter, you create one with createPrinter, then start calling print with an AST node on it. This adds the node via a pipeline:

const enum PipelinePhase {
  Notification,
  Substitution,
  Comments,
  SourceMaps,
  Emit,
}

With the word to start emitting through the AST in [pipelineEmitWithHint][4]. There is a hint option which can be used to force the emit type.

Post Processing via Transformers

The process of changing your AST into the expected JS or TS happens the emitter compiler transformers. There is a full step

Emitting a declaration file is a multi-step process. It goes through the above emitter of its AST, but then also goes through a

Clone this wiki locally