From 0bd50ca08c3990c5cb4dfc8e38b2e432241266d9 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sat, 17 Oct 2015 14:35:28 -0700 Subject: [PATCH] extract 'convertCompilerOptionsFromJson' to separate function --- src/compiler/commandLineParser.ts | 100 ++++++++++++++++-------------- src/compiler/types.ts | 2 +- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index acf0474b7bf..95583a19038 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -410,63 +410,19 @@ namespace ts { /** * Parse the contents of a config file (tsconfig.json). * @param json The contents of the config file to parse + * @param host Instance of ParseConfigHost used to enumerate files in folder. * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string): ParsedCommandLine { - let errors: Diagnostic[] = []; + let { options, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath); return { - options: getCompilerOptions(), + options, fileNames: getFileNames(), errors }; - function getCompilerOptions(): CompilerOptions { - let options: CompilerOptions = {}; - let optionNameMap: Map = {}; - forEach(optionDeclarations, option => { - optionNameMap[option.name] = option; - }); - let jsonOptions = json["compilerOptions"]; - if (jsonOptions) { - for (let id in jsonOptions) { - if (hasProperty(optionNameMap, id)) { - let opt = optionNameMap[id]; - let optType = opt.type; - let value = jsonOptions[id]; - let expectedType = typeof optType === "string" ? optType : "string"; - if (typeof value === expectedType) { - if (typeof optType !== "string") { - let key = value.toLowerCase(); - if (hasProperty(optType, key)) { - value = optType[key]; - } - else { - errors.push(createCompilerDiagnostic((opt).error)); - value = 0; - } - } - if (opt.isFilePath) { - value = normalizePath(combinePaths(basePath, value)); - if (value === "") { - value = "."; - } - } - options[opt.name] = value; - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType)); - } - } - else { - errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id)); - } - } - } - return options; - } - function getFileNames(): string[] { let fileNames: string[] = []; if (hasProperty(json, "files")) { @@ -501,4 +457,54 @@ namespace ts { return fileNames; } } + + export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string): { options: CompilerOptions, errors: Diagnostic[] } { + let options: CompilerOptions = {}; + let optionNameMap: Map = {}; + let errors: Diagnostic[] = []; + + if (!jsonOptions) { + return { options, errors }; + } + + forEach(optionDeclarations, option => { + optionNameMap[option.name] = option; + }); + + for (let id in jsonOptions) { + if (hasProperty(optionNameMap, id)) { + let opt = optionNameMap[id]; + let optType = opt.type; + let value = jsonOptions[id]; + let expectedType = typeof optType === "string" ? optType : "string"; + if (typeof value === expectedType) { + if (typeof optType !== "string") { + let key = value.toLowerCase(); + if (hasProperty(optType, key)) { + value = optType[key]; + } + else { + errors.push(createCompilerDiagnostic((opt).error)); + value = 0; + } + } + if (opt.isFilePath) { + value = normalizePath(combinePaths(basePath, value)); + if (value === "") { + value = "."; + } + } + options[opt.name] = value; + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, id, expectedType)); + } + } + else { + errors.push(createCompilerDiagnostic(Diagnostics.Unknown_compiler_option_0, id)); + } + } + + return { options, errors }; + } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index c44c6ad2cc0..2818ef04a48 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1305,7 +1305,7 @@ namespace ts { getCurrentDirectory(): string; } - export interface ParseConfigHost extends ModuleResolutionHost { + export interface ParseConfigHost { readDirectory(rootDir: string, extension: string, exclude: string[]): string[]; }