Skip to content

Commit ebc0ed3

Browse files
authored
fix: show problematic file path when encountering malformed JSON5 (#176)
* fix: show problematic file path when encountering malformed JSON5 * Fix missing assert
1 parent 9a3c0ed commit ebc0ed3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/__tests__/tsconfig-loader.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ describe("loadConfig", () => {
207207
expect(res).toStrictEqual(config);
208208
});
209209

210+
it("It should throw an error including the file path when encountering invalid JSON5", () => {
211+
expect(() =>
212+
loadTsconfig(
213+
"/root/dir1/tsconfig.json",
214+
(path) => path === "/root/dir1/tsconfig.json",
215+
(_) => `{
216+
"compilerOptions": {
217+
}`
218+
)
219+
).toThrowError(
220+
"/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12"
221+
);
222+
});
223+
210224
it("It should load a config with extends and overwrite all options", () => {
211225
const firstConfig = {
212226
extends: "../base-config.json",

src/tsconfig-loader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ export function loadTsconfig(
125125

126126
const configString = readFileSync(configFilePath);
127127
const cleanedJson = StripBom(configString);
128-
const config: Tsconfig = JSON5.parse(cleanedJson);
128+
let config: Tsconfig;
129+
try {
130+
config = JSON5.parse(cleanedJson);
131+
} catch (e) {
132+
throw new Error(`${configFilePath} is malformed ${e.message}`);
133+
}
129134
let extendedConfig = config.extends;
130135

131136
if (extendedConfig) {

0 commit comments

Comments
 (0)