Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(devkit): add options allow trailing commas for parsing json #10538

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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,
leosvelperez marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
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