diff --git a/packages/nx/src/utils/json.spec.ts b/packages/nx/src/utils/json.spec.ts index 6305d7f44b748..65c13e515e757 100644 --- a/packages/nx/src/utils/json.spec.ts +++ b/packages/nx/src/utils/json.spec.ts @@ -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(); + }); }); diff --git a/packages/nx/src/utils/json.ts b/packages/nx/src/utils/json.ts index 64e4fe1f64a44..65d7b3b6f3657 100644 --- a/packages/nx/src/utils/json.ts +++ b/packages/nx/src/utils/json.ts @@ -44,7 +44,9 @@ export function parseJson( options?: JsonParseOptions ): T { try { - return JSON.parse(input); + if (options?.expectComments !== true) { + return JSON.parse(input); + } } catch {} options = { allowTrailingComma: true, ...options };