From de9b6d271d025f639dc90f8bd642fafbfe6e16ed Mon Sep 17 00:00:00 2001 From: Steven Nance Date: Thu, 22 Feb 2024 20:08:07 +0100 Subject: [PATCH] fix(devkit): respect expectComments when parsing json (#21584) --- packages/nx/src/utils/json.spec.ts | 16 ++++++++++++++++ packages/nx/src/utils/json.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 };