From bb307f81639b2bc29a86aef5f92492e38e88ba67 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 4 Feb 2015 16:53:14 -0800 Subject: [PATCH] Simplify the API for emitting code from the Program instance. --- src/compiler/emitter.ts | 3 +- src/compiler/program.ts | 24 +++++-- src/compiler/tsc.ts | 2 +- src/compiler/types.ts | 24 +++++-- src/compiler/utilities.ts | 17 +---- src/harness/fourslash.ts | 2 +- src/harness/harness.ts | 2 +- src/harness/projectsRunner.ts | 2 +- src/services/services.ts | 7 +- .../baselines/reference/APISample_compile.js | 24 +++++-- .../reference/APISample_compile.types | 64 +++++++++++-------- tests/baselines/reference/APISample_linter.js | 20 ++++-- .../reference/APISample_linter.types | 56 ++++++++++------ .../reference/APISample_transform.js | 24 +++++-- .../reference/APISample_transform.types | 64 +++++++++++-------- .../baselines/reference/APISample_watcher.js | 20 ++++-- .../reference/APISample_watcher.types | 56 ++++++++++------ tests/cases/compiler/APISample_compile.ts | 2 +- tests/cases/compiler/APISample_transform.ts | 2 +- 19 files changed, 263 insertions(+), 152 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index a4521977802..cb0b8faab5b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1504,8 +1504,9 @@ module ts { return diagnostics; } + // @internal // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature - export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile?: SourceFile): EmitResult { + export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult { var compilerOptions = host.getCompilerOptions(); var languageVersion = compilerOptions.target || ScriptTarget.ES3; var sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ddd9cb4ed3a..a6ed7fb51ab 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -90,7 +90,6 @@ module ts { var diagnosticsProducingTypeChecker: TypeChecker; var noDiagnosticsTypeChecker: TypeChecker; - var emitHost: EmitHost; program = { getSourceFile: getSourceFile, @@ -105,7 +104,7 @@ module ts { getTypeChecker, getDiagnosticsProducingTypeChecker, getCommonSourceDirectory: () => commonSourceDirectory, - emitFiles: invokeEmitter, + emit, isEmitBlocked, getCurrentDirectory: host.getCurrentDirectory, getEmitResolver: () => getDiagnosticsProducingTypeChecker().getEmitResolver(), @@ -116,10 +115,22 @@ module ts { }; return program; - function getEmitHost() { - return emitHost || (emitHost = createEmitHostFromProgram(program)); + function getEmitHost(writeFileCallback?: WriteFileCallback) { + var compilerHost = program.getCompilerHost(); + return { + getCanonicalFileName: compilerHost.getCanonicalFileName, + getCommonSourceDirectory: program.getCommonSourceDirectory, + getCompilerOptions: program.getCompilerOptions, + getCurrentDirectory: compilerHost.getCurrentDirectory, + getNewLine: compilerHost.getNewLine, + getSourceFile: program.getSourceFile, + getSourceFiles: program.getSourceFiles, + isEmitBlocked: program.isEmitBlocked, + writeFile: writeFileCallback || compilerHost.writeFile, + }; } + function isEmitBlocked(sourceFile?: SourceFile): boolean { if (options.noEmitOnError) { return getDiagnostics(sourceFile).length !== 0 || getTypeCheckerDiagnostics(sourceFile).length !== 0; @@ -143,9 +154,10 @@ module ts { return ts.getDeclarationDiagnostics(getEmitHost(), resolver, targetSourceFile); } - function invokeEmitter(targetSourceFile?: SourceFile) { + function emit(targetSourceFile?: SourceFile, writeFileCallback?: WriteFileCallback) { var resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(); - return emitFiles(resolver, getEmitHost(), targetSourceFile); + var host = getEmitHost(writeFileCallback); + return emitFiles(resolver, host, targetSourceFile); } function getSourceFile(fileName: string) { diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 2c31c008b21..05f2305acf7 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -346,7 +346,7 @@ module ts { } else { var emitStart = new Date().getTime(); - var emitOutput = program.emitFiles(); + var emitOutput = program.emit(); var emitErrors = emitOutput.diagnostics; exitStatus = emitOutput.emitResultStatus; var reportStart = new Date().getTime(); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 58898605959..b04a5944bfc 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -927,10 +927,27 @@ module ts { getCurrentDirectory(): string; } + export interface WriteFileCallback { + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + } + export interface Program extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; getCompilerHost(): CompilerHost; - getEmitResolver(): EmitResolver; + + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; + + isEmitBlocked(sourceFile?: SourceFile): boolean; // These will merge with the below diagnostics function in a followup checkin. getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; @@ -951,9 +968,6 @@ module ts { getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; - emitFiles(targetSourceFile?: SourceFile): EmitResult; - isEmitBlocked(sourceFile?: SourceFile): boolean; - // For testing purposes only. Should not be used by any other consumers (including the // language service). /* @internal */ getDiagnosticsProducingTypeChecker(): TypeChecker; @@ -1671,7 +1685,7 @@ module ts { getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile; getDefaultLibFileName(options: CompilerOptions): string; getCancellationToken? (): CancellationToken; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 69c64ba4880..9df23b22dd0 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -31,7 +31,7 @@ module ts { getCanonicalFileName(fileName: string): string; getNewLine(): string; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; } // Pool writers to avoid needing to allocate them for every symbol we write. @@ -843,21 +843,6 @@ module ts { return false; } - export function createEmitHostFromProgram(program: Program): EmitHost { - var compilerHost = program.getCompilerHost(); - return { - getCanonicalFileName: compilerHost.getCanonicalFileName, - getCommonSourceDirectory: program.getCommonSourceDirectory, - getCompilerOptions: program.getCompilerOptions, - getCurrentDirectory: compilerHost.getCurrentDirectory, - getNewLine: compilerHost.getNewLine, - getSourceFile: program.getSourceFile, - getSourceFiles: program.getSourceFiles, - isEmitBlocked: program.isEmitBlocked, - writeFile: compilerHost.writeFile, - }; - } - export function textSpanEnd(span: TextSpan) { return span.start + span.length } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index b3c9c118e97..dd55e702d09 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2216,7 +2216,7 @@ module FourSlash { if (errors.length > 0) { throw new Error('Error compiling ' + fileName + ': ' + errors.map(e => e.messageText).join('\r\n')); } - program.emitFiles(); + program.emit(); result = result || ''; // Might have an empty fourslash file // Compile and execute the test diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 1b7a7b31dbd..7b68ac80efa 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1086,7 +1086,7 @@ module Harness { // only emit if there weren't parse errors var emitResult: ts.EmitResult; if (!isEmitBlocked) { - emitResult = program.emitFiles(); + emitResult = program.emit(); } var errors: HarnessDiagnostic[] = []; diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 8daa4a9afdc..327ebc3520e 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -131,7 +131,7 @@ class ProjectRunner extends RunnerBase { var sourceMapData: ts.SourceMapData[] = null; if (!errors.length) { errors = program.getTypeCheckerDiagnostics(); - var emitResult = program.emitFiles(); + var emitResult = program.emit(); errors = ts.concatenate(errors, emitResult.diagnostics); sourceMapData = emitResult.sourceMaps; diff --git a/src/services/services.ts b/src/services/services.ts index 771263919e5..7311e5f7596 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4664,12 +4664,7 @@ module ts { }); } - // Get an emit host from our program, but override the writeFile functionality to - // call our local writer function. - var emitHost = createEmitHostFromProgram(program); - emitHost.writeFile = writeFile; - - var emitOutput = emitFiles(program.getEmitResolver(), emitHost, sourceFile); + var emitOutput = program.emit(sourceFile, writeFile); return { outputFiles, diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index fd741d793b4..c20c601823f 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -16,7 +16,7 @@ import ts = require("typescript"); export function compile(fileNames: string[], options: ts.CompilerOptions): void { var host = ts.createCompilerHost(options); var program = ts.createProgram(fileNames, options, host); - var result = program.emitFiles(); + var result = program.emit(); var allDiagnostics = program.getDiagnostics() .concat(program.getTypeCheckerDiagnostics()) @@ -737,10 +737,24 @@ declare module "typescript" { getSourceFile(fileName: string): SourceFile; getCurrentDirectory(): string; } + interface WriteFileCallback { + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + } interface Program extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; getCompilerHost(): CompilerHost; - getEmitResolver(): EmitResolver; + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; + isEmitBlocked(sourceFile?: SourceFile): boolean; getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; getTypeCheckerGlobalDiagnostics(): Diagnostic[]; getDiagnostics(sourceFile?: SourceFile): Diagnostic[]; @@ -748,8 +762,6 @@ declare module "typescript" { getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[]; getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; - emitFiles(targetSourceFile?: SourceFile): EmitResult; - isEmitBlocked(sourceFile?: SourceFile): boolean; } interface SourceMapSpan { emittedLine: number; @@ -1338,7 +1350,7 @@ declare module "typescript" { getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile; getDefaultLibFileName(options: CompilerOptions): string; getCancellationToken?(): CancellationToken; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; @@ -1916,7 +1928,7 @@ var ts = require("typescript"); function compile(fileNames, options) { var host = ts.createCompilerHost(options); var program = ts.createProgram(fileNames, options, host); - var result = program.emitFiles(); + var result = program.emit(); var allDiagnostics = program.getDiagnostics().concat(program.getTypeCheckerDiagnostics()).concat(result.diagnostics); allDiagnostics.forEach(function (diagnostic) { var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start); diff --git a/tests/baselines/reference/APISample_compile.types b/tests/baselines/reference/APISample_compile.types index 5aeb7f096e1..469dc8098aa 100644 --- a/tests/baselines/reference/APISample_compile.types +++ b/tests/baselines/reference/APISample_compile.types @@ -40,12 +40,12 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void >options : ts.CompilerOptions >host : ts.CompilerHost - var result = program.emitFiles(); + var result = program.emit(); >result : ts.EmitResult ->program.emitFiles() : ts.EmitResult ->program.emitFiles : (targetSourceFile?: ts.SourceFile) => ts.EmitResult +>program.emit() : ts.EmitResult +>program.emit : (targetSourceFile?: ts.SourceFile, writeFile?: ts.WriteFileCallback) => ts.EmitResult >program : ts.Program ->emitFiles : (targetSourceFile?: ts.SourceFile) => ts.EmitResult +>emit : (targetSourceFile?: ts.SourceFile, writeFile?: ts.WriteFileCallback) => ts.EmitResult var allDiagnostics = program.getDiagnostics() >allDiagnostics : ts.Diagnostic[] @@ -2240,6 +2240,16 @@ declare module "typescript" { getCurrentDirectory(): string; >getCurrentDirectory : () => string + } + interface WriteFileCallback { +>WriteFileCallback : WriteFileCallback + + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; +>fileName : string +>data : string +>writeByteOrderMark : boolean +>onError : (message: string) => void +>message : string } interface Program extends ScriptReferenceHost { >Program : Program @@ -2253,9 +2263,28 @@ declare module "typescript" { >getCompilerHost : () => CompilerHost >CompilerHost : CompilerHost - getEmitResolver(): EmitResolver; ->getEmitResolver : () => EmitResolver ->EmitResolver : EmitResolver + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; +>emit : (targetSourceFile?: SourceFile, writeFile?: WriteFileCallback) => EmitResult +>targetSourceFile : SourceFile +>SourceFile : SourceFile +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback +>EmitResult : EmitResult + + isEmitBlocked(sourceFile?: SourceFile): boolean; +>isEmitBlocked : (sourceFile?: SourceFile) => boolean +>sourceFile : SourceFile +>SourceFile : SourceFile getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; >getTypeCheckerDiagnostics : (sourceFile?: SourceFile) => Diagnostic[] @@ -2289,17 +2318,6 @@ declare module "typescript" { getCommonSourceDirectory(): string; >getCommonSourceDirectory : () => string - - emitFiles(targetSourceFile?: SourceFile): EmitResult; ->emitFiles : (targetSourceFile?: SourceFile) => EmitResult ->targetSourceFile : SourceFile ->SourceFile : SourceFile ->EmitResult : EmitResult - - isEmitBlocked(sourceFile?: SourceFile): boolean; ->isEmitBlocked : (sourceFile?: SourceFile) => boolean ->sourceFile : SourceFile ->SourceFile : SourceFile } interface SourceMapSpan { >SourceMapSpan : SourceMapSpan @@ -4255,13 +4273,9 @@ declare module "typescript" { >getCancellationToken : () => CancellationToken >CancellationToken : CancellationToken - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; ->writeFile : (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) => void ->fileName : string ->data : string ->writeByteOrderMark : boolean ->onError : (message: string) => void ->message : string + writeFile: WriteFileCallback; +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback getCurrentDirectory(): string; >getCurrentDirectory : () => string diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js index b32f7036038..eed54d13542 100644 --- a/tests/baselines/reference/APISample_linter.js +++ b/tests/baselines/reference/APISample_linter.js @@ -767,10 +767,24 @@ declare module "typescript" { getSourceFile(fileName: string): SourceFile; getCurrentDirectory(): string; } + interface WriteFileCallback { + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + } interface Program extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; getCompilerHost(): CompilerHost; - getEmitResolver(): EmitResolver; + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; + isEmitBlocked(sourceFile?: SourceFile): boolean; getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; getTypeCheckerGlobalDiagnostics(): Diagnostic[]; getDiagnostics(sourceFile?: SourceFile): Diagnostic[]; @@ -778,8 +792,6 @@ declare module "typescript" { getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[]; getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; - emitFiles(targetSourceFile?: SourceFile): EmitResult; - isEmitBlocked(sourceFile?: SourceFile): boolean; } interface SourceMapSpan { emittedLine: number; @@ -1368,7 +1380,7 @@ declare module "typescript" { getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile; getDefaultLibFileName(options: CompilerOptions): string; getCancellationToken?(): CancellationToken; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; diff --git a/tests/baselines/reference/APISample_linter.types b/tests/baselines/reference/APISample_linter.types index 386830d2c1c..3af13c6b068 100644 --- a/tests/baselines/reference/APISample_linter.types +++ b/tests/baselines/reference/APISample_linter.types @@ -2378,6 +2378,16 @@ declare module "typescript" { getCurrentDirectory(): string; >getCurrentDirectory : () => string + } + interface WriteFileCallback { +>WriteFileCallback : WriteFileCallback + + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; +>fileName : string +>data : string +>writeByteOrderMark : boolean +>onError : (message: string) => void +>message : string } interface Program extends ScriptReferenceHost { >Program : Program @@ -2391,9 +2401,28 @@ declare module "typescript" { >getCompilerHost : () => CompilerHost >CompilerHost : CompilerHost - getEmitResolver(): EmitResolver; ->getEmitResolver : () => EmitResolver ->EmitResolver : EmitResolver + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; +>emit : (targetSourceFile?: SourceFile, writeFile?: WriteFileCallback) => EmitResult +>targetSourceFile : SourceFile +>SourceFile : SourceFile +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback +>EmitResult : EmitResult + + isEmitBlocked(sourceFile?: SourceFile): boolean; +>isEmitBlocked : (sourceFile?: SourceFile) => boolean +>sourceFile : SourceFile +>SourceFile : SourceFile getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; >getTypeCheckerDiagnostics : (sourceFile?: SourceFile) => Diagnostic[] @@ -2427,17 +2456,6 @@ declare module "typescript" { getCommonSourceDirectory(): string; >getCommonSourceDirectory : () => string - - emitFiles(targetSourceFile?: SourceFile): EmitResult; ->emitFiles : (targetSourceFile?: SourceFile) => EmitResult ->targetSourceFile : SourceFile ->SourceFile : SourceFile ->EmitResult : EmitResult - - isEmitBlocked(sourceFile?: SourceFile): boolean; ->isEmitBlocked : (sourceFile?: SourceFile) => boolean ->sourceFile : SourceFile ->SourceFile : SourceFile } interface SourceMapSpan { >SourceMapSpan : SourceMapSpan @@ -4393,13 +4411,9 @@ declare module "typescript" { >getCancellationToken : () => CancellationToken >CancellationToken : CancellationToken - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; ->writeFile : (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) => void ->fileName : string ->data : string ->writeByteOrderMark : boolean ->onError : (message: string) => void ->message : string + writeFile: WriteFileCallback; +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback getCurrentDirectory(): string; >getCurrentDirectory : () => string diff --git a/tests/baselines/reference/APISample_transform.js b/tests/baselines/reference/APISample_transform.js index cdccf5e83f4..59976015fb4 100644 --- a/tests/baselines/reference/APISample_transform.js +++ b/tests/baselines/reference/APISample_transform.js @@ -51,7 +51,7 @@ function transform(contents: string, compilerOptions: ts.CompilerOptions = {}) { // Type check and get semantic errors errors = program.getTypeCheckerDiagnostics(); // Generate output - program.emitFiles(); + program.emit(); } return { outputs: outputs, @@ -767,10 +767,24 @@ declare module "typescript" { getSourceFile(fileName: string): SourceFile; getCurrentDirectory(): string; } + interface WriteFileCallback { + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + } interface Program extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; getCompilerHost(): CompilerHost; - getEmitResolver(): EmitResolver; + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; + isEmitBlocked(sourceFile?: SourceFile): boolean; getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; getTypeCheckerGlobalDiagnostics(): Diagnostic[]; getDiagnostics(sourceFile?: SourceFile): Diagnostic[]; @@ -778,8 +792,6 @@ declare module "typescript" { getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[]; getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; - emitFiles(targetSourceFile?: SourceFile): EmitResult; - isEmitBlocked(sourceFile?: SourceFile): boolean; } interface SourceMapSpan { emittedLine: number; @@ -1368,7 +1380,7 @@ declare module "typescript" { getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile; getDefaultLibFileName(options: CompilerOptions): string; getCancellationToken?(): CancellationToken; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; @@ -1975,7 +1987,7 @@ function transform(contents, compilerOptions) { // Type check and get semantic errors errors = program.getTypeCheckerDiagnostics(); // Generate output - program.emitFiles(); + program.emit(); } return { outputs: outputs, diff --git a/tests/baselines/reference/APISample_transform.types b/tests/baselines/reference/APISample_transform.types index e689bba7527..e4f0e56910a 100644 --- a/tests/baselines/reference/APISample_transform.types +++ b/tests/baselines/reference/APISample_transform.types @@ -171,11 +171,11 @@ function transform(contents: string, compilerOptions: ts.CompilerOptions = {}) { >getTypeCheckerDiagnostics : (sourceFile?: ts.SourceFile) => ts.Diagnostic[] // Generate output - program.emitFiles(); ->program.emitFiles() : ts.EmitResult ->program.emitFiles : (targetSourceFile?: ts.SourceFile) => ts.EmitResult + program.emit(); +>program.emit() : ts.EmitResult +>program.emit : (targetSourceFile?: ts.SourceFile, writeFile?: ts.WriteFileCallback) => ts.EmitResult >program : ts.Program ->emitFiles : (targetSourceFile?: ts.SourceFile) => ts.EmitResult +>emit : (targetSourceFile?: ts.SourceFile, writeFile?: ts.WriteFileCallback) => ts.EmitResult } return { >{ outputs: outputs, errors: errors.map(function (e) { return e.file.fileName + "(" + e.file.getLineAndCharacterFromPosition(e.start).line + "): " + e.messageText; }) } : { outputs: any[]; errors: string[]; } @@ -2319,6 +2319,16 @@ declare module "typescript" { getCurrentDirectory(): string; >getCurrentDirectory : () => string + } + interface WriteFileCallback { +>WriteFileCallback : WriteFileCallback + + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; +>fileName : string +>data : string +>writeByteOrderMark : boolean +>onError : (message: string) => void +>message : string } interface Program extends ScriptReferenceHost { >Program : Program @@ -2332,9 +2342,28 @@ declare module "typescript" { >getCompilerHost : () => CompilerHost >CompilerHost : CompilerHost - getEmitResolver(): EmitResolver; ->getEmitResolver : () => EmitResolver ->EmitResolver : EmitResolver + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; +>emit : (targetSourceFile?: SourceFile, writeFile?: WriteFileCallback) => EmitResult +>targetSourceFile : SourceFile +>SourceFile : SourceFile +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback +>EmitResult : EmitResult + + isEmitBlocked(sourceFile?: SourceFile): boolean; +>isEmitBlocked : (sourceFile?: SourceFile) => boolean +>sourceFile : SourceFile +>SourceFile : SourceFile getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; >getTypeCheckerDiagnostics : (sourceFile?: SourceFile) => Diagnostic[] @@ -2368,17 +2397,6 @@ declare module "typescript" { getCommonSourceDirectory(): string; >getCommonSourceDirectory : () => string - - emitFiles(targetSourceFile?: SourceFile): EmitResult; ->emitFiles : (targetSourceFile?: SourceFile) => EmitResult ->targetSourceFile : SourceFile ->SourceFile : SourceFile ->EmitResult : EmitResult - - isEmitBlocked(sourceFile?: SourceFile): boolean; ->isEmitBlocked : (sourceFile?: SourceFile) => boolean ->sourceFile : SourceFile ->SourceFile : SourceFile } interface SourceMapSpan { >SourceMapSpan : SourceMapSpan @@ -4334,13 +4352,9 @@ declare module "typescript" { >getCancellationToken : () => CancellationToken >CancellationToken : CancellationToken - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; ->writeFile : (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) => void ->fileName : string ->data : string ->writeByteOrderMark : boolean ->onError : (message: string) => void ->message : string + writeFile: WriteFileCallback; +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback getCurrentDirectory(): string; >getCurrentDirectory : () => string diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index 411cd4f9d51..5879903340d 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -805,10 +805,24 @@ declare module "typescript" { getSourceFile(fileName: string): SourceFile; getCurrentDirectory(): string; } + interface WriteFileCallback { + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + } interface Program extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; getCompilerHost(): CompilerHost; - getEmitResolver(): EmitResolver; + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; + isEmitBlocked(sourceFile?: SourceFile): boolean; getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; getTypeCheckerGlobalDiagnostics(): Diagnostic[]; getDiagnostics(sourceFile?: SourceFile): Diagnostic[]; @@ -816,8 +830,6 @@ declare module "typescript" { getDeclarationDiagnostics(sourceFile: SourceFile): Diagnostic[]; getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; - emitFiles(targetSourceFile?: SourceFile): EmitResult; - isEmitBlocked(sourceFile?: SourceFile): boolean; } interface SourceMapSpan { emittedLine: number; @@ -1406,7 +1418,7 @@ declare module "typescript" { getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile; getDefaultLibFileName(options: CompilerOptions): string; getCancellationToken?(): CancellationToken; - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; + writeFile: WriteFileCallback; getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; diff --git a/tests/baselines/reference/APISample_watcher.types b/tests/baselines/reference/APISample_watcher.types index c26d8e241e3..67eff31dfb8 100644 --- a/tests/baselines/reference/APISample_watcher.types +++ b/tests/baselines/reference/APISample_watcher.types @@ -2504,6 +2504,16 @@ declare module "typescript" { getCurrentDirectory(): string; >getCurrentDirectory : () => string + } + interface WriteFileCallback { +>WriteFileCallback : WriteFileCallback + + (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; +>fileName : string +>data : string +>writeByteOrderMark : boolean +>onError : (message: string) => void +>message : string } interface Program extends ScriptReferenceHost { >Program : Program @@ -2517,9 +2527,28 @@ declare module "typescript" { >getCompilerHost : () => CompilerHost >CompilerHost : CompilerHost - getEmitResolver(): EmitResolver; ->getEmitResolver : () => EmitResolver ->EmitResolver : EmitResolver + /** + * Emits the javascript and declaration files. If targetSourceFile is not specified, then + * the javascript and declaration files will be produced for all the files in this program. + * If targetSourceFile is specified, then only the javascript and declaration for that + * specific file will be generated. + * + * If writeFile is not specified then the writeFile callback from getCompilerHost() will be + * used for writing the javascript and declaration files. Otherwise, the writeFile parameter + * will be invoked when writing the javascript and declaration files. + */ + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult; +>emit : (targetSourceFile?: SourceFile, writeFile?: WriteFileCallback) => EmitResult +>targetSourceFile : SourceFile +>SourceFile : SourceFile +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback +>EmitResult : EmitResult + + isEmitBlocked(sourceFile?: SourceFile): boolean; +>isEmitBlocked : (sourceFile?: SourceFile) => boolean +>sourceFile : SourceFile +>SourceFile : SourceFile getTypeCheckerDiagnostics(sourceFile?: SourceFile): Diagnostic[]; >getTypeCheckerDiagnostics : (sourceFile?: SourceFile) => Diagnostic[] @@ -2553,17 +2582,6 @@ declare module "typescript" { getCommonSourceDirectory(): string; >getCommonSourceDirectory : () => string - - emitFiles(targetSourceFile?: SourceFile): EmitResult; ->emitFiles : (targetSourceFile?: SourceFile) => EmitResult ->targetSourceFile : SourceFile ->SourceFile : SourceFile ->EmitResult : EmitResult - - isEmitBlocked(sourceFile?: SourceFile): boolean; ->isEmitBlocked : (sourceFile?: SourceFile) => boolean ->sourceFile : SourceFile ->SourceFile : SourceFile } interface SourceMapSpan { >SourceMapSpan : SourceMapSpan @@ -4519,13 +4537,9 @@ declare module "typescript" { >getCancellationToken : () => CancellationToken >CancellationToken : CancellationToken - writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void): void; ->writeFile : (fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) => void ->fileName : string ->data : string ->writeByteOrderMark : boolean ->onError : (message: string) => void ->message : string + writeFile: WriteFileCallback; +>writeFile : WriteFileCallback +>WriteFileCallback : WriteFileCallback getCurrentDirectory(): string; >getCurrentDirectory : () => string diff --git a/tests/cases/compiler/APISample_compile.ts b/tests/cases/compiler/APISample_compile.ts index 5278fb16682..af1eb9e0462 100644 --- a/tests/cases/compiler/APISample_compile.ts +++ b/tests/cases/compiler/APISample_compile.ts @@ -16,7 +16,7 @@ import ts = require("typescript"); export function compile(fileNames: string[], options: ts.CompilerOptions): void { var host = ts.createCompilerHost(options); var program = ts.createProgram(fileNames, options, host); - var result = program.emitFiles(); + var result = program.emit(); var allDiagnostics = program.getDiagnostics() .concat(program.getTypeCheckerDiagnostics()) diff --git a/tests/cases/compiler/APISample_transform.ts b/tests/cases/compiler/APISample_transform.ts index 284e8a4dc5d..eccdb1ccecc 100644 --- a/tests/cases/compiler/APISample_transform.ts +++ b/tests/cases/compiler/APISample_transform.ts @@ -51,7 +51,7 @@ function transform(contents: string, compilerOptions: ts.CompilerOptions = {}) { // Type check and get semantic errors errors = program.getTypeCheckerDiagnostics(); // Generate output - program.emitFiles(); + program.emit(); } return { outputs: outputs,