emitFiles

Defined in emitter.ts here is the function signature:

  1. // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
  2. export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile?: SourceFile): EmitResult {

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

The most interesting call stack from emitFiles is the following:

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

emitJavaScript

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

  1. function emitJavaScript(jsFilePath: string, root?: SourceFile) {
  2. let writer = createTextWriter(newLine);
  3. let write = writer.write;
  4. let writeTextOfNode = writer.writeTextOfNode;
  5. let writeLine = writer.writeLine;
  6. let increaseIndent = writer.increaseIndent;
  7. let decreaseIndent = writer.decreaseIndent;
  8. let currentSourceFile: SourceFile;
  9. // name of an exporter function if file is a System external module
  10. // System.register([...], function (<exporter>) {...})
  11. // exporting in System modules looks like:
  12. // export var x; ... x = 1
  13. // =>
  14. // var x;... exporter("x", x = 1)
  15. let exportFunctionForFile: string;
  16. let generatedNameSet: Map<string> = {};
  17. let nodeToGeneratedName: string[] = [];
  18. let computedPropertyNamesToGeneratedNames: string[];
  19. let extendsEmitted = false;
  20. let decorateEmitted = false;
  21. let paramEmitted = false;
  22. let awaiterEmitted = false;
  23. let tempFlags = 0;
  24. let tempVariables: Identifier[];
  25. let tempParameters: Identifier[];
  26. let externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[];
  27. let exportSpecifiers: Map<ExportSpecifier[]>;
  28. let exportEquals: ExportAssignment;
  29. let hasExportStars: boolean;
  30. /** Write emitted output to disk */
  31. let writeEmittedFiles = writeJavaScriptFile;
  32. let detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number }[];
  33. let writeComment = writeCommentRange;
  34. /** Emit a node */
  35. let emit = emitNodeWithoutSourceMap;
  36. /** Called just before starting emit of a node */
  37. let emitStart = function (node: Node) { };
  38. /** Called once the emit of the node is done */
  39. let emitEnd = function (node: Node) { };
  40. /** Emit the text for the given token that comes after startPos
  41. * This by default writes the text provided with the given tokenKind
  42. * but if optional emitFn callback is provided the text is emitted using the callback instead of default text
  43. * @param tokenKind the kind of the token to search and emit
  44. * @param startPos the position in the source to start searching for the token
  45. * @param emitFn if given will be invoked to emit the text instead of actual token emit */
  46. let emitToken = emitTokenText;
  47. /** Called to before starting the lexical scopes as in function/class in the emitted code because of node
  48. * @param scopeDeclaration node that starts the lexical scope
  49. * @param scopeName Optional name of this scope instead of deducing one from the declaration node */
  50. let scopeEmitStart = function(scopeDeclaration: Node, scopeName?: string) { };
  51. /** Called after coming out of the scope */
  52. let scopeEmitEnd = function() { };
  53. /** Sourcemap data that will get encoded */
  54. let sourceMapData: SourceMapData;
  55. if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) {
  56. initializeEmitterWithSourceMaps();
  57. }
  58. if (root) {
  59. // Do not call emit directly. It does not set the currentSourceFile.
  60. emitSourceFile(root);
  61. }
  62. else {
  63. forEach(host.getSourceFiles(), sourceFile => {
  64. if (!isExternalModuleOrDeclarationFile(sourceFile)) {
  65. emitSourceFile(sourceFile);
  66. }
  67. });
  68. }
  69. writeLine();
  70. writeEmittedFiles(writer.getText(), /*writeByteOrderMark*/ compilerOptions.emitBOM);
  71. return;
  72. /// BUNCH OF LOCAL FUNCTIONS
  73. }

Basically it sets up a bunch of locals (these function form the bulk of emitter.ts) and then hands off to a local function emitSourceFile which kicks off the emit. The emitSourceFile function just sets up the currentSourceFile and in turn hands off to a local emit function.

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

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

emitJavaScriptWorker

The complete function:

  1. function emitJavaScriptWorker(node: Node) {
  2. // Check if the node can be emitted regardless of the ScriptTarget
  3. switch (node.kind) {
  4. case SyntaxKind.Identifier:
  5. return emitIdentifier(<Identifier>node);
  6. case SyntaxKind.Parameter:
  7. return emitParameter(<ParameterDeclaration>node);
  8. case SyntaxKind.MethodDeclaration:
  9. case SyntaxKind.MethodSignature:
  10. return emitMethod(<MethodDeclaration>node);
  11. case SyntaxKind.GetAccessor:
  12. case SyntaxKind.SetAccessor:
  13. return emitAccessor(<AccessorDeclaration>node);
  14. case SyntaxKind.ThisKeyword:
  15. return emitThis(node);
  16. case SyntaxKind.SuperKeyword:
  17. return emitSuper(node);
  18. case SyntaxKind.NullKeyword:
  19. return write("null");
  20. case SyntaxKind.TrueKeyword:
  21. return write("true");
  22. case SyntaxKind.FalseKeyword:
  23. return write("false");
  24. case SyntaxKind.NumericLiteral:
  25. case SyntaxKind.StringLiteral:
  26. case SyntaxKind.RegularExpressionLiteral:
  27. case SyntaxKind.NoSubstitutionTemplateLiteral:
  28. case SyntaxKind.TemplateHead:
  29. case SyntaxKind.TemplateMiddle:
  30. case SyntaxKind.TemplateTail:
  31. return emitLiteral(<LiteralExpression>node);
  32. case SyntaxKind.TemplateExpression:
  33. return emitTemplateExpression(<TemplateExpression>node);
  34. case SyntaxKind.TemplateSpan:
  35. return emitTemplateSpan(<TemplateSpan>node);
  36. case SyntaxKind.JsxElement:
  37. case SyntaxKind.JsxSelfClosingElement:
  38. return emitJsxElement(<JsxElement|JsxSelfClosingElement>node);
  39. case SyntaxKind.JsxText:
  40. return emitJsxText(<JsxText>node);
  41. case SyntaxKind.JsxExpression:
  42. return emitJsxExpression(<JsxExpression>node);
  43. case SyntaxKind.QualifiedName:
  44. return emitQualifiedName(<QualifiedName>node);
  45. case SyntaxKind.ObjectBindingPattern:
  46. return emitObjectBindingPattern(<BindingPattern>node);
  47. case SyntaxKind.ArrayBindingPattern:
  48. return emitArrayBindingPattern(<BindingPattern>node);
  49. case SyntaxKind.BindingElement:
  50. return emitBindingElement(<BindingElement>node);
  51. case SyntaxKind.ArrayLiteralExpression:
  52. return emitArrayLiteral(<ArrayLiteralExpression>node);
  53. case SyntaxKind.ObjectLiteralExpression:
  54. return emitObjectLiteral(<ObjectLiteralExpression>node);
  55. case SyntaxKind.PropertyAssignment:
  56. return emitPropertyAssignment(<PropertyDeclaration>node);
  57. case SyntaxKind.ShorthandPropertyAssignment:
  58. return emitShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
  59. case SyntaxKind.ComputedPropertyName:
  60. return emitComputedPropertyName(<ComputedPropertyName>node);
  61. case SyntaxKind.PropertyAccessExpression:
  62. return emitPropertyAccess(<PropertyAccessExpression>node);
  63. case SyntaxKind.ElementAccessExpression:
  64. return emitIndexedAccess(<ElementAccessExpression>node);
  65. case SyntaxKind.CallExpression:
  66. return emitCallExpression(<CallExpression>node);
  67. case SyntaxKind.NewExpression:
  68. return emitNewExpression(<NewExpression>node);
  69. case SyntaxKind.TaggedTemplateExpression:
  70. return emitTaggedTemplateExpression(<TaggedTemplateExpression>node);
  71. case SyntaxKind.TypeAssertionExpression:
  72. return emit((<TypeAssertion>node).expression);
  73. case SyntaxKind.AsExpression:
  74. return emit((<AsExpression>node).expression);
  75. case SyntaxKind.ParenthesizedExpression:
  76. return emitParenExpression(<ParenthesizedExpression>node);
  77. case SyntaxKind.FunctionDeclaration:
  78. case SyntaxKind.FunctionExpression:
  79. case SyntaxKind.ArrowFunction:
  80. return emitFunctionDeclaration(<FunctionLikeDeclaration>node);
  81. case SyntaxKind.DeleteExpression:
  82. return emitDeleteExpression(<DeleteExpression>node);
  83. case SyntaxKind.TypeOfExpression:
  84. return emitTypeOfExpression(<TypeOfExpression>node);
  85. case SyntaxKind.VoidExpression:
  86. return emitVoidExpression(<VoidExpression>node);
  87. case SyntaxKind.AwaitExpression:
  88. return emitAwaitExpression(<AwaitExpression>node);
  89. case SyntaxKind.PrefixUnaryExpression:
  90. return emitPrefixUnaryExpression(<PrefixUnaryExpression>node);
  91. case SyntaxKind.PostfixUnaryExpression:
  92. return emitPostfixUnaryExpression(<PostfixUnaryExpression>node);
  93. case SyntaxKind.BinaryExpression:
  94. return emitBinaryExpression(<BinaryExpression>node);
  95. case SyntaxKind.ConditionalExpression:
  96. return emitConditionalExpression(<ConditionalExpression>node);
  97. case SyntaxKind.SpreadElementExpression:
  98. return emitSpreadElementExpression(<SpreadElementExpression>node);
  99. case SyntaxKind.YieldExpression:
  100. return emitYieldExpression(<YieldExpression>node);
  101. case SyntaxKind.OmittedExpression:
  102. return;
  103. case SyntaxKind.Block:
  104. case SyntaxKind.ModuleBlock:
  105. return emitBlock(<Block>node);
  106. case SyntaxKind.VariableStatement:
  107. return emitVariableStatement(<VariableStatement>node);
  108. case SyntaxKind.EmptyStatement:
  109. return write(";");
  110. case SyntaxKind.ExpressionStatement:
  111. return emitExpressionStatement(<ExpressionStatement>node);
  112. case SyntaxKind.IfStatement:
  113. return emitIfStatement(<IfStatement>node);
  114. case SyntaxKind.DoStatement:
  115. return emitDoStatement(<DoStatement>node);
  116. case SyntaxKind.WhileStatement:
  117. return emitWhileStatement(<WhileStatement>node);
  118. case SyntaxKind.ForStatement:
  119. return emitForStatement(<ForStatement>node);
  120. case SyntaxKind.ForOfStatement:
  121. case SyntaxKind.ForInStatement:
  122. return emitForInOrForOfStatement(<ForInStatement>node);
  123. case SyntaxKind.ContinueStatement:
  124. case SyntaxKind.BreakStatement:
  125. return emitBreakOrContinueStatement(<BreakOrContinueStatement>node);
  126. case SyntaxKind.ReturnStatement:
  127. return emitReturnStatement(<ReturnStatement>node);
  128. case SyntaxKind.WithStatement:
  129. return emitWithStatement(<WithStatement>node);
  130. case SyntaxKind.SwitchStatement:
  131. return emitSwitchStatement(<SwitchStatement>node);
  132. case SyntaxKind.CaseClause:
  133. case SyntaxKind.DefaultClause:
  134. return emitCaseOrDefaultClause(<CaseOrDefaultClause>node);
  135. case SyntaxKind.LabeledStatement:
  136. return emitLabelledStatement(<LabeledStatement>node);
  137. case SyntaxKind.ThrowStatement:
  138. return emitThrowStatement(<ThrowStatement>node);
  139. case SyntaxKind.TryStatement:
  140. return emitTryStatement(<TryStatement>node);
  141. case SyntaxKind.CatchClause:
  142. return emitCatchClause(<CatchClause>node);
  143. case SyntaxKind.DebuggerStatement:
  144. return emitDebuggerStatement(node);
  145. case SyntaxKind.VariableDeclaration:
  146. return emitVariableDeclaration(<VariableDeclaration>node);
  147. case SyntaxKind.ClassExpression:
  148. return emitClassExpression(<ClassExpression>node);
  149. case SyntaxKind.ClassDeclaration:
  150. return emitClassDeclaration(<ClassDeclaration>node);
  151. case SyntaxKind.InterfaceDeclaration:
  152. return emitInterfaceDeclaration(<InterfaceDeclaration>node);
  153. case SyntaxKind.EnumDeclaration:
  154. return emitEnumDeclaration(<EnumDeclaration>node);
  155. case SyntaxKind.EnumMember:
  156. return emitEnumMember(<EnumMember>node);
  157. case SyntaxKind.ModuleDeclaration:
  158. return emitModuleDeclaration(<ModuleDeclaration>node);
  159. case SyntaxKind.ImportDeclaration:
  160. return emitImportDeclaration(<ImportDeclaration>node);
  161. case SyntaxKind.ImportEqualsDeclaration:
  162. return emitImportEqualsDeclaration(<ImportEqualsDeclaration>node);
  163. case SyntaxKind.ExportDeclaration:
  164. return emitExportDeclaration(<ExportDeclaration>node);
  165. case SyntaxKind.ExportAssignment:
  166. return emitExportAssignment(<ExportAssignment>node);
  167. case SyntaxKind.SourceFile:
  168. return emitSourceFileNode(<SourceFile>node);
  169. }
  170. }

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