diff --git a/src/__tests__/tsconfig-loader.test.ts b/src/__tests__/tsconfig-loader.test.ts index 0569a89..2564142 100644 --- a/src/__tests__/tsconfig-loader.test.ts +++ b/src/__tests__/tsconfig-loader.test.ts @@ -207,6 +207,20 @@ describe("loadConfig", () => { expect(res).toStrictEqual(config); }); + it("It should throw an error including the file path when encountering invalid JSON5", () => { + expect(() => + loadTsconfig( + "/root/dir1/tsconfig.json", + (path) => path === "/root/dir1/tsconfig.json", + (_) => `{ + "compilerOptions": { + }` + ) + ).toThrowError( + "/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12" + ); + }); + it("It should load a config with extends and overwrite all options", () => { const firstConfig = { extends: "../base-config.json", diff --git a/src/tsconfig-loader.ts b/src/tsconfig-loader.ts index 93b6326..74b7ba9 100644 --- a/src/tsconfig-loader.ts +++ b/src/tsconfig-loader.ts @@ -125,7 +125,12 @@ export function loadTsconfig( const configString = readFileSync(configFilePath); const cleanedJson = StripBom(configString); - const config: Tsconfig = JSON5.parse(cleanedJson); + let config: Tsconfig; + try { + config = JSON5.parse(cleanedJson); + } catch (e) { + throw new Error(`${configFilePath} is malformed ${e.message}`); + } let extendedConfig = config.extends; if (extendedConfig) {