Skip to content

Commit

Permalink
fix(devkit): add options allow trailing commas for parsing json
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jun 1, 2022
1 parent 10e9992 commit d5af288
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ export default async function (tree: Tree) {
const tsConfigPaths = await collectTsConfigPaths(tree);

for (const tsConfigPath of tsConfigPaths) {
updateJson(tree, tsConfigPath, (json) => {
if (
!json.compilerOptions?.target ||
(json.compilerOptions?.target &&
!skipTargets.includes(json.compilerOptions.target.toLowerCase()))
) {
json.compilerOptions ??= {};
json.compilerOptions.target = 'es2020';
updateJson(
tree,
tsConfigPath,
(json) => {
if (
!json.compilerOptions?.target ||
(json.compilerOptions?.target &&
!skipTargets.includes(json.compilerOptions.target.toLowerCase()))
) {
json.compilerOptions ??= {};
json.compilerOptions.target = 'es2020';
}
return json;
},
{
allowTrailingComma: true,
}
return json;
});
);
}

await formatFiles(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ const allowedExt = ['.ts', '.js'];

function updateTsConfig(tree: Tree, tsConfigPath: string) {
try {
updateJson(tree, tsConfigPath, (json) => {
json.exclude = Array.from(
new Set([...(json.exclude || []), 'jest.config.ts'])
);
return json;
});
updateJson(
tree,
tsConfigPath,
(json) => {
json.exclude = Array.from(
new Set([...(json.exclude || []), 'jest.config.ts'])
);
return json;
},
{
allowTrailingComma: true,
}
);
} catch (e) {
logger.warn(
stripIndents`Nx Unable to update ${tsConfigPath}. Please manually ignore the jest.config.ts file.`
Expand Down
17 changes: 17 additions & 0 deletions packages/nx/src/utils/json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,21 @@ describe('parseJson', () => {
)
).toThrowError();
});

it('should handle trailing commas', () => {
expect(
parseJson(
`{
"nested": {
"test": 123,
},
"array": [1, 2, 3,]
}`,
{ allowTrailingComma: true }
)
).toEqual({
nested: { test: 123 },
array: [1, 2, 3],
});
});
});
24 changes: 10 additions & 14 deletions packages/nx/src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parse, printParseErrorCode, stripComments } from 'jsonc-parser';
import type { ParseError } from 'jsonc-parser';
import type { ParseError, ParseOptions } from 'jsonc-parser';

export { stripComments as stripJsonComments };

export interface JsonParseOptions {
export interface JsonParseOptions extends ParseOptions {
/**
* Expect JSON with javascript-style
* @default false
Expand All @@ -14,6 +14,11 @@ export interface JsonParseOptions {
* @default false
*/
disallowComments?: boolean;

/**
* Allow trailing commas in the JSON content
*/
allowTrailingComma?: boolean;
}

export interface JsonSerializeOptions {
Expand All @@ -37,20 +42,11 @@ export function parseJson<T extends object = any>(
options?: JsonParseOptions
): T {
try {
if (
options?.disallowComments === true ||
options?.expectComments !== true
) {
return JSON.parse(input);
}
} catch (error) {
if (options?.disallowComments === true) {
throw error;
}
}
return JSON.parse(input);
} catch {}

const errors: ParseError[] = [];
const result: T = parse(input, errors);
const result: T = parse(input, errors, options);

if (errors.length > 0) {
const { error, offset } = errors[0];
Expand Down

0 comments on commit d5af288

Please sign in to comment.