Skip to content

Commit

Permalink
fix(devkit): add options allow trailing commas for parsing json (#10538)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jun 1, 2022
1 parent 26edb49 commit 925a27b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
Expand Up @@ -32,17 +32,25 @@ 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,
disallowComments: false,
}
return json;
});
);
}

await formatFiles(tree);
Expand Down Expand Up @@ -73,7 +81,10 @@ async function collectTsConfigPaths(tree: Tree): Promise<string[]> {
false
);
targetTsConfigPaths.forEach((tsConfigPath) => {
const tsConfig = readJson(tree, tsConfigPath);
const tsConfig = readJson(tree, tsConfigPath, {
allowTrailingComma: true,
disallowComments: false,
});
if (tsConfig.compilerOptions?.target) {
uniqueTsConfigs.add(tsConfigPath);
}
Expand Down
Expand Up @@ -18,12 +18,20 @@ 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,
disallowComments: false,
}
);
} 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
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
@@ -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

1 comment on commit 925a27b

@vercel
Copy link

@vercel vercel bot commented on 925a27b Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.