Skip to content

Commit

Permalink
fix(devkit): respect expectComments when parsing json (#21584)
Browse files Browse the repository at this point in the history
  • Loading branch information
llwt authored and jaysoo committed Feb 23, 2024
1 parent c4cacf1 commit de9b6d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/nx/src/utils/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ describe('parseJson', () => {
array: [1, 2, 3],
});
});

it('should not call JSON.parse when expectComments is true', () => {
jest.spyOn(JSON, 'parse');

expect(
parseJson(
`{
/* a comment */
"foo": "bar"
}`,
{ expectComments: true }
)
).toEqual({ foo: 'bar' });

expect(JSON.parse).not.toHaveBeenCalled();
});
});
4 changes: 3 additions & 1 deletion packages/nx/src/utils/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function parseJson<T extends object = any>(
options?: JsonParseOptions
): T {
try {
return JSON.parse(input);
if (options?.expectComments !== true) {
return JSON.parse(input);
}
} catch {}

options = { allowTrailingComma: true, ...options };
Expand Down

0 comments on commit de9b6d2

Please sign in to comment.