Skip to content

Commit 402a8ee

Browse files
committed
Add support for comments in tsconfig.json, fix ivogabe#210
1 parent 83655b2 commit 402a8ee

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ module compile {
271271
tsConfigFileName = fileNameOrSettings;
272272
// load file and strip BOM, since JSON.parse fails to parse if there's a BOM present
273273
let tsConfigText = fs.readFileSync(fileNameOrSettings).toString();
274-
tsConfigContent = JSON.parse(tsConfigText.replace(/^\uFEFF/, ''));
274+
const typescript = (settings && settings.typescript) || ts;
275+
const tsConfig = tsApi.parseTsConfig(typescript, tsConfigFileName, tsConfigText);
276+
tsConfigContent = tsConfig.config || {};
277+
if (tsConfig.error) {
278+
console.log(tsConfig.error.messageText);
279+
}
275280
let newSettings: any = {};
276281
if (tsConfigContent.compilerOptions) {
277282
for (const key of Object.keys(tsConfigContent.compilerOptions)) {

lib/tsapi.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ export interface TypeScript15 {
1111
flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain15, newLine: string): string;
1212
transpile(input: string, compilerOptions?: ts.CompilerOptions, fileName?: string, diagnostics?: ts.Diagnostic[]): string;
1313
}
14+
export interface TypeScript17 {
15+
parseConfigFileTextToJson(fileName: string, jsonText: string): {
16+
config?: any;
17+
error?: ts.Diagnostic;
18+
};
19+
}
20+
21+
export function parseTsConfig(typescript: TypeScript14 | TypeScript15 | TypeScript17, fileName: string, content: string) {
22+
if ('parseConfigFileTextToJson' in typescript) {
23+
return (<TypeScript17> typescript).parseConfigFileTextToJson(fileName, content);
24+
} else {
25+
return {
26+
config: JSON.parse(content.replace(/^\uFEFF/, ''))
27+
};
28+
}
29+
}
30+
1431

1532
/*
1633
* In TS1.6+ ts.createProgram has an extra argument, `oldProgram`.

0 commit comments

Comments
 (0)