From ebc0ed3346725f75c3df7cb02f971de4e16bc26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Wouts?= Date: Tue, 3 May 2022 15:31:41 +1000 Subject: [PATCH] fix: show problematic file path when encountering malformed JSON5 (#176) * fix: show problematic file path when encountering malformed JSON5 * Fix missing assert --- src/__tests__/tsconfig-loader.test.ts | 14 ++++++++++++++ src/tsconfig-loader.ts | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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) {