Simplify the createProgram API.

Allow the host parameter to be optional to simplify command line programs.
This commit is contained in:
Cyrus Najmabadi 2015-02-04 17:02:26 -08:00
parent bb307f8163
commit 2a1d78d9e3
10 changed files with 23 additions and 34 deletions

View file

@ -73,13 +73,14 @@ module ts {
};
}
export function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program {
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program {
var program: Program;
var files: SourceFile[] = [];
var filesByName: Map<SourceFile> = {};
var errors: Diagnostic[] = [];
var seenNoDefaultLib = options.noLib;
var commonSourceDirectory: string;
host = host || createCompilerHost(options);
forEach(rootNames, name => processRootFile(name, false));
if (!seenNoDefaultLib) {

View file

@ -14,8 +14,7 @@ declare var console: any;
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 program = ts.createProgram(fileNames, options);
var result = program.emit();
var allDiagnostics = program.getDiagnostics()
@ -1426,7 +1425,7 @@ declare module "typescript" {
}
declare module "typescript" {
function createCompilerHost(options: CompilerOptions): CompilerHost;
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
}
declare module "typescript" {
var servicesVersion: string;
@ -1926,8 +1925,7 @@ declare module "typescript" {
*/
var ts = require("typescript");
function compile(fileNames, options) {
var host = ts.createCompilerHost(options);
var program = ts.createProgram(fileNames, options, host);
var program = ts.createProgram(fileNames, options);
var result = program.emit();
var allDiagnostics = program.getDiagnostics().concat(program.getTypeCheckerDiagnostics()).concat(result.diagnostics);
allDiagnostics.forEach(function (diagnostic) {

View file

@ -22,23 +22,14 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
>ts : unknown
>CompilerOptions : ts.CompilerOptions
var host = ts.createCompilerHost(options);
>host : ts.CompilerHost
>ts.createCompilerHost(options) : ts.CompilerHost
>ts.createCompilerHost : (options: ts.CompilerOptions) => ts.CompilerHost
>ts : typeof ts
>createCompilerHost : (options: ts.CompilerOptions) => ts.CompilerHost
>options : ts.CompilerOptions
var program = ts.createProgram(fileNames, options, host);
var program = ts.createProgram(fileNames, options);
>program : ts.Program
>ts.createProgram(fileNames, options, host) : ts.Program
>ts.createProgram : (rootNames: string[], options: ts.CompilerOptions, host: ts.CompilerHost) => ts.Program
>ts.createProgram(fileNames, options) : ts.Program
>ts.createProgram : (rootNames: string[], options: ts.CompilerOptions, host?: ts.CompilerHost) => ts.Program
>ts : typeof ts
>createProgram : (rootNames: string[], options: ts.CompilerOptions, host: ts.CompilerHost) => ts.Program
>createProgram : (rootNames: string[], options: ts.CompilerOptions, host?: ts.CompilerHost) => ts.Program
>fileNames : string[]
>options : ts.CompilerOptions
>host : ts.CompilerHost
var result = program.emit();
>result : ts.EmitResult
@ -4577,8 +4568,8 @@ declare module "typescript" {
>CompilerOptions : CompilerOptions
>CompilerHost : CompilerHost
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host: CompilerHost) => Program
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host?: CompilerHost) => Program
>rootNames : string[]
>options : CompilerOptions
>CompilerOptions : CompilerOptions

View file

@ -1456,7 +1456,7 @@ declare module "typescript" {
}
declare module "typescript" {
function createCompilerHost(options: CompilerOptions): CompilerHost;
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
}
declare module "typescript" {
var servicesVersion: string;

View file

@ -4715,8 +4715,8 @@ declare module "typescript" {
>CompilerOptions : CompilerOptions
>CompilerHost : CompilerHost
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host: CompilerHost) => Program
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host?: CompilerHost) => Program
>rootNames : string[]
>options : CompilerOptions
>CompilerOptions : CompilerOptions

View file

@ -1456,7 +1456,7 @@ declare module "typescript" {
}
declare module "typescript" {
function createCompilerHost(options: CompilerOptions): CompilerHost;
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
}
declare module "typescript" {
var servicesVersion: string;

View file

@ -139,9 +139,9 @@ function transform(contents: string, compilerOptions: ts.CompilerOptions = {}) {
var program = ts.createProgram(["file.ts"], compilerOptions, compilerHost);
>program : ts.Program
>ts.createProgram(["file.ts"], compilerOptions, compilerHost) : ts.Program
>ts.createProgram : (rootNames: string[], options: ts.CompilerOptions, host: ts.CompilerHost) => ts.Program
>ts.createProgram : (rootNames: string[], options: ts.CompilerOptions, host?: ts.CompilerHost) => ts.Program
>ts : typeof ts
>createProgram : (rootNames: string[], options: ts.CompilerOptions, host: ts.CompilerHost) => ts.Program
>createProgram : (rootNames: string[], options: ts.CompilerOptions, host?: ts.CompilerHost) => ts.Program
>["file.ts"] : string[]
>compilerOptions : ts.CompilerOptions
>compilerHost : { getSourceFile: (fileName: any, target: any) => ts.SourceFile; writeFile: (name: any, text: any, writeByteOrderMark: any) => void; getDefaultLibFileName: () => string; useCaseSensitiveFileNames: () => boolean; getCanonicalFileName: (fileName: any) => any; getCurrentDirectory: () => string; getNewLine: () => string; }
@ -4656,8 +4656,8 @@ declare module "typescript" {
>CompilerOptions : CompilerOptions
>CompilerHost : CompilerHost
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host: CompilerHost) => Program
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host?: CompilerHost) => Program
>rootNames : string[]
>options : CompilerOptions
>CompilerOptions : CompilerOptions

View file

@ -1494,7 +1494,7 @@ declare module "typescript" {
}
declare module "typescript" {
function createCompilerHost(options: CompilerOptions): CompilerHost;
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
}
declare module "typescript" {
var servicesVersion: string;

View file

@ -4841,8 +4841,8 @@ declare module "typescript" {
>CompilerOptions : CompilerOptions
>CompilerHost : CompilerHost
function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host: CompilerHost) => Program
function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program;
>createProgram : (rootNames: string[], options: CompilerOptions, host?: CompilerHost) => Program
>rootNames : string[]
>options : CompilerOptions
>CompilerOptions : CompilerOptions

View file

@ -14,8 +14,7 @@ declare var console: any;
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 program = ts.createProgram(fileNames, options);
var result = program.emit();
var allDiagnostics = program.getDiagnostics()