From 2a1d78d9e374f9460b253feceaddf30955cc194e Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 4 Feb 2015 17:02:26 -0800 Subject: [PATCH] Simplify the createProgram API. Allow the host parameter to be optional to simplify command line programs. --- src/compiler/program.ts | 3 ++- .../baselines/reference/APISample_compile.js | 8 +++---- .../reference/APISample_compile.types | 21 ++++++------------- tests/baselines/reference/APISample_linter.js | 2 +- .../reference/APISample_linter.types | 4 ++-- .../reference/APISample_transform.js | 2 +- .../reference/APISample_transform.types | 8 +++---- .../baselines/reference/APISample_watcher.js | 2 +- .../reference/APISample_watcher.types | 4 ++-- tests/cases/compiler/APISample_compile.ts | 3 +-- 10 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index a6ed7fb51ab..6976195cc04 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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 = {}; var errors: Diagnostic[] = []; var seenNoDefaultLib = options.noLib; var commonSourceDirectory: string; + host = host || createCompilerHost(options); forEach(rootNames, name => processRootFile(name, false)); if (!seenNoDefaultLib) { diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index c20c601823f..6df54cc1b64 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -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) { diff --git a/tests/baselines/reference/APISample_compile.types b/tests/baselines/reference/APISample_compile.types index 469dc8098aa..cc07d75b0c6 100644 --- a/tests/baselines/reference/APISample_compile.types +++ b/tests/baselines/reference/APISample_compile.types @@ -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 diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js index eed54d13542..1cf0ea77098 100644 --- a/tests/baselines/reference/APISample_linter.js +++ b/tests/baselines/reference/APISample_linter.js @@ -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; diff --git a/tests/baselines/reference/APISample_linter.types b/tests/baselines/reference/APISample_linter.types index 3af13c6b068..922a9ebea8e 100644 --- a/tests/baselines/reference/APISample_linter.types +++ b/tests/baselines/reference/APISample_linter.types @@ -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 diff --git a/tests/baselines/reference/APISample_transform.js b/tests/baselines/reference/APISample_transform.js index 59976015fb4..8d0352ed170 100644 --- a/tests/baselines/reference/APISample_transform.js +++ b/tests/baselines/reference/APISample_transform.js @@ -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; diff --git a/tests/baselines/reference/APISample_transform.types b/tests/baselines/reference/APISample_transform.types index e4f0e56910a..8c81d46e5bc 100644 --- a/tests/baselines/reference/APISample_transform.types +++ b/tests/baselines/reference/APISample_transform.types @@ -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 diff --git a/tests/baselines/reference/APISample_watcher.js b/tests/baselines/reference/APISample_watcher.js index 5879903340d..a4216e78ecd 100644 --- a/tests/baselines/reference/APISample_watcher.js +++ b/tests/baselines/reference/APISample_watcher.js @@ -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; diff --git a/tests/baselines/reference/APISample_watcher.types b/tests/baselines/reference/APISample_watcher.types index 67eff31dfb8..95f87b4978b 100644 --- a/tests/baselines/reference/APISample_watcher.types +++ b/tests/baselines/reference/APISample_watcher.types @@ -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 diff --git a/tests/cases/compiler/APISample_compile.ts b/tests/cases/compiler/APISample_compile.ts index af1eb9e0462..4731f856e2a 100644 --- a/tests/cases/compiler/APISample_compile.ts +++ b/tests/cases/compiler/APISample_compile.ts @@ -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()