Skip to content

Commit f8424d0

Browse files
author
Paul van Brenk
committed
Minor clean up to make it more readable.
1 parent 5c44a0f commit f8424d0

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,13 @@ module ts {
284284
* Read tsconfig.json file
285285
* @param fileName The path to the config file
286286
*/
287-
export function readConfigFile(fileName: string): any {
287+
export function readConfigFile(fileName: string): { config?: any; error?: Diagnostic } {
288288
try {
289289
var text = sys.readFile(fileName);
290-
return /\S/.test(text) ? JSON.parse(text) : {};
290+
return { config: /\S/.test(text) ? JSON.parse(text) : {} };
291291
}
292292
catch (e) {
293+
return { error: createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) };
293294
}
294295
}
295296

src/compiler/tsc.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ module ts {
208208

209209
if (!cachedProgram) {
210210
if (configFileName) {
211-
try {
212-
var configObject = readConfigFile(configFileName);
213-
}
214-
catch (e)
215-
{
216-
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, configFileName, e.message));
211+
212+
let result = readConfigFile(configFileName);
213+
if (result.error) {
214+
reportDiagnostic(result.error);
217215
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
218216
}
219-
var configParseResult = parseConfigFile(configObject, sys, getDirectoryPath(configFileName));
217+
218+
let configObject = result.config;
219+
let configParseResult = parseConfigFile(configObject, sys, getDirectoryPath(configFileName));
220220
if (configParseResult.errors.length > 0) {
221221
reportDiagnostics(configParseResult.errors);
222222
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
@@ -233,7 +233,7 @@ module ts {
233233
compilerHost.getSourceFile = getSourceFile;
234234
}
235235

236-
var compileResult = compile(rootFileNames, compilerOptions, compilerHost);
236+
let compileResult = compile(rootFileNames, compilerOptions, compilerHost);
237237

238238
if (!compilerOptions.watch) {
239239
return sys.exit(compileResult.exitStatus);

src/services/shims.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -804,20 +804,17 @@ module ts {
804804
() => {
805805
let text = sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength());
806806

807-
try {
808-
var json = /\S/.test(text) ? JSON.parse(text) : {};
809-
}
810-
catch (e) {
807+
let result = this.parseConfigFileText(fileName, text);
808+
809+
if (result.error) {
811810
return {
812811
options: {},
813812
files: [],
814-
errors: realizeDiagnostic(createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message), '\r\n')
815-
}
813+
errors: [realizeDiagnostic(result.error, '/r/n')]
814+
};
816815
}
817816

818-
if (json) {
819-
var configFile = parseConfigFile(json, this.host, getDirectoryPath(normalizeSlashes(fileName)));
820-
}
817+
var configFile = parseConfigFile(result.config, this.host, getDirectoryPath(normalizeSlashes(fileName)));
821818

822819
return {
823820
options: configFile.options,
@@ -827,6 +824,15 @@ module ts {
827824
});
828825
}
829826

827+
private parseConfigFileText(fileName: string, jsonText: string): { config?: any; error?: Diagnostic } {
828+
try {
829+
return { config: /\S/.test(jsonText) ? JSON.parse(jsonText) : {} };
830+
}
831+
catch (e) {
832+
return { error: createCompilerDiagnostic(Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) };
833+
}
834+
}
835+
830836
public getDefaultCompilationSettings(): string {
831837
return this.forwardJSONCall(
832838
"getDefaultCompilationSettings()",

0 commit comments

Comments
 (0)