diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index a83e742a12..b477c97fec 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -284,12 +284,13 @@ module ts { * Read tsconfig.json file * @param fileName The path to the config file */ - export function readConfigFile(fileName: string): any { + export function readConfigFile(fileName: string): { config?: any; error?: Diagnostic } { try { var text = sys.readFile(fileName); - return /\S/.test(text) ? JSON.parse(text) : {}; + return { config: /\S/.test(text) ? JSON.parse(text) : {} }; } catch (e) { + return { error: createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; } } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 89464e6b6e..15cc665e35 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -208,15 +208,15 @@ module ts { if (!cachedProgram) { if (configFileName) { - try { - var configObject = readConfigFile(configFileName); - } - catch (e) - { - reportDiagnostic(createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, configFileName, e.message)); + + let result = readConfigFile(configFileName); + if (result.error) { + reportDiagnostic(result.error); return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); } - var configParseResult = parseConfigFile(configObject, sys, getDirectoryPath(configFileName)); + + let configObject = result.config; + let configParseResult = parseConfigFile(configObject, sys, getDirectoryPath(configFileName)); if (configParseResult.errors.length > 0) { reportDiagnostics(configParseResult.errors); return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); @@ -233,7 +233,7 @@ module ts { compilerHost.getSourceFile = getSourceFile; } - var compileResult = compile(rootFileNames, compilerOptions, compilerHost); + let compileResult = compile(rootFileNames, compilerOptions, compilerHost); if (!compilerOptions.watch) { return sys.exit(compileResult.exitStatus); diff --git a/src/services/shims.ts b/src/services/shims.ts index 6caec3cd23..ddcc32b864 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -804,20 +804,17 @@ module ts { () => { let text = sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()); - try { - var json = /\S/.test(text) ? JSON.parse(text) : {}; - } - catch (e) { + let result = this.parseConfigFileText(fileName, text); + + if (result.error) { return { options: {}, files: [], - errors: realizeDiagnostic(createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message), '\r\n') - } + errors: [realizeDiagnostic(result.error, '/r/n')] + }; } - if (json) { - var configFile = parseConfigFile(json, this.host, getDirectoryPath(normalizeSlashes(fileName))); - } + var configFile = parseConfigFile(result.config, this.host, getDirectoryPath(normalizeSlashes(fileName))); return { options: configFile.options, @@ -827,6 +824,15 @@ module ts { }); } + private parseConfigFileText(fileName: string, jsonText: string): { config?: any; error?: Diagnostic } { + try { + return { config: /\S/.test(jsonText) ? JSON.parse(jsonText) : {} }; + } + catch (e) { + return { error: createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; + } + } + public getDefaultCompilationSettings(): string { return this.forwardJSONCall( "getDefaultCompilationSettings()",