Simplify the API for emitting code from the Program instance.

This commit is contained in:
Cyrus Najmabadi 2015-02-04 16:53:14 -08:00
parent b6d083fa40
commit bb307f8163
19 changed files with 263 additions and 152 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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();

View File

@ -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;

View File

@ -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
}

View File

@ -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

View File

@ -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[] = [];

View File

@ -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;

View File

@ -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,

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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())

View File

@ -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,