EmitHost is a just a simplified (as in narrowed down) version of CompilerHost (and is at runtime actually a CompilerHost for many use cases).

  1. emitFiles ->
  2. emitFile(jsFilePath, targetSourceFile) ->
  3. emitJavaScript(jsFilePath, targetSourceFile);

There is a lot of good comments in this function so we present it below :

  1. function emitSourceFile(sourceFile: SourceFile): void {
  2. currentSourceFile = sourceFile;
  3. exportFunctionForFile = undefined;
  4. emit(sourceFile);

The emit function handles comment emit + actual JavaScript emit. The actual JavaScript emit is the job of emitJavaScriptWorker function.

Recursion is done by simply calling other emitFoo function from these functions as needed e.g. from emitFunctionDeclaration :

  1. function emitFunctionDeclaration(node: FunctionLikeDeclaration) {
  2. if (nodeIsMissing(node.body)) {
  3. return emitOnlyPinnedOrTripleSlashComments(node);
  4. }
  5. // Methods will emit the comments as part of emitting method declaration
  6. emitLeadingComments(node);
  7. }
  8. // For targeting below es6, emit functions-like declaration including arrow function using function keyword.
  9. // When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead
  10. if (!shouldEmitAsArrowFunction(node)) {
  11. if (isES6ExportedDeclaration(node)) {
  12. write("export ");
  13. if (node.flags & NodeFlags.Default) {
  14. }
  15. }
  16. write("function");
  17. write("*");
  18. }
  19. write(" ");
  20. }
  21. if (shouldEmitFunctionName(node)) {
  22. emitDeclarationName(node);
  23. }
  24. emitSignatureAndBody(node);
  25. if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.FunctionDeclaration && node.parent === currentSourceFile && node.name) {
  26. emitExportMemberAssignments((<FunctionDeclaration>node).name);
  27. }
  28. if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) {
  29. emitTrailingComments(node);