Skip to content

Commit

Permalink
fix: show problematic file path when encountering malformed JSON5 (#176)
Browse files Browse the repository at this point in the history
* fix: show problematic file path when encountering malformed JSON5

* Fix missing assert
  • Loading branch information
fwouts committed May 3, 2022
1 parent 9a3c0ed commit ebc0ed3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/__tests__/tsconfig-loader.test.ts
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/tsconfig-loader.ts
Expand Up @@ -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) {
Expand Down

0 comments on commit ebc0ed3

Please sign in to comment.