Skip to content

Commit ebf8b9c

Browse files
committedJul 7, 2020
fix(@schematics/angular): provide more context on offending invalid JSON file
Closes: #18120 (cherry picked from commit 2039f28)
1 parent 519eb6c commit ebf8b9c

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed
 

‎packages/schematics/angular/migrations/update-10/solution-style-tsconfig.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonAstNode, JsonAstString, JsonParseMode, dirname, join, normalize, parseJsonAst, resolve } from '@angular-devkit/core';
8+
import { JsonAstNode, JsonAstString, JsonParseMode, dirname, join, logging, normalize, parseJsonAst, resolve } from '@angular-devkit/core';
99
import { DirEntry, Rule, chain } from '@angular-devkit/schematics';
1010
import { findPropertyInAstObject } from '../../utility/json-utils';
1111
import { getWorkspace } from '../../utility/workspace';
@@ -18,7 +18,7 @@ const SOLUTIONS_TS_CONFIG_HEADER = `/*
1818
*/
1919
`;
2020

21-
function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string, JsonAstString]> {
21+
function* visitExtendedJsonFiles(directory: DirEntry, logger: logging.LoggerApi): IterableIterator<[string, JsonAstString]> {
2222
for (const path of directory.subfiles) {
2323
if (!path.endsWith('.json')) {
2424
continue;
@@ -33,8 +33,16 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
3333
let jsonAst: JsonAstNode;
3434
try {
3535
jsonAst = parseJsonAst(content, JsonParseMode.Loose);
36-
} catch {
37-
throw new Error(`Invalid JSON AST Object (${path})`);
36+
} catch (error) {
37+
let jsonFilePath = `${join(directory.path, path)}`;
38+
jsonFilePath = jsonFilePath.startsWith('/') ? jsonFilePath.substr(1) : jsonFilePath;
39+
40+
const msg = error instanceof Error ? error.message : error;
41+
logger.warn(
42+
`Failed to parse "${jsonFilePath}" as JSON AST Object. ${msg}\n` +
43+
'If this is a TypeScript configuration file you will need to update the "extends" value manually.',
44+
);
45+
continue;
3846
}
3947

4048
if (jsonAst.kind !== 'object') {
@@ -54,12 +62,12 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
5462
continue;
5563
}
5664

57-
yield* visitExtendedJsonFiles(directory.dir(path));
65+
yield* visitExtendedJsonFiles(directory.dir(path), logger);
5866
}
5967
}
6068

6169
function updateTsconfigExtendsRule(): Rule {
62-
return host => {
70+
return (host, context) => {
6371
if (!host.exists('tsconfig.json')) {
6472
return;
6573
}
@@ -68,7 +76,7 @@ function updateTsconfigExtendsRule(): Rule {
6876
host.rename('tsconfig.json', 'tsconfig.base.json');
6977

7078
// Iterate over all tsconfig files and change the extends from 'tsconfig.json' 'tsconfig.base.json'
71-
for (const [tsconfigPath, extendsAst] of visitExtendedJsonFiles(host.root)) {
79+
for (const [tsconfigPath, extendsAst] of visitExtendedJsonFiles(host.root, context.logger)) {
7280
const tsConfigDir = dirname(normalize(tsconfigPath));
7381
if ('/tsconfig.json' !== resolve(tsConfigDir, normalize(extendsAst.value))) {
7482
// tsconfig extends doesn't refer to the workspace tsconfig path.

‎packages/schematics/angular/migrations/update-10/solution-style-tsconfig_spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,15 @@ describe('Migration to create "Solution Style" tsconfig', () => {
120120
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
121121
expect(readJsonFile(newTree, 'src/tsconfig.json').extends).toEqual('./../tsconfig.base.json');
122122
});
123+
124+
it('should show warning with full path when parsing invalid JSON', async () => {
125+
const logs: string[] = [];
126+
schematicRunner.logger.subscribe(m => logs.push(m.message));
127+
128+
tree.create('src/invalid/error.json', '{ invalid }');
129+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
130+
131+
expect(readJsonFile(newTree, 'src/tsconfig.tsc.json').extends).toEqual('./tsconfig.json');
132+
expect(logs.join('\n')).toContain('Failed to parse "src/invalid/error.json" as JSON AST Object. Invalid JSON character');
133+
});
123134
});

0 commit comments

Comments
 (0)
Please sign in to comment.