Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dc8d8e0

Browse files
alan-agius4filipesilva
authored andcommittedJun 26, 2020
fix(@schematics/angular): don't error out on blank JSON files during migrations
During the solution-style-tsconfig migration we are unexpectedly erroring out when encounter a blank JSON file. This PR fixes this behaviour and also prints the file path when an error occurs when parsing JSON contents. Closes: #18012
1 parent 2d65a49 commit dc8d8e0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed
 

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

+10-3
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 { JsonAstString, JsonParseMode, dirname, join, normalize, parseJsonAst, resolve } from '@angular-devkit/core';
8+
import { JsonAstNode, JsonAstString, JsonParseMode, dirname, join, 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';
@@ -25,11 +25,18 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
2525
}
2626

2727
const entry = directory.file(path);
28-
if (!entry) {
28+
const content = entry?.content.toString();
29+
if (!content) {
2930
continue;
3031
}
3132

32-
const jsonAst = parseJsonAst(entry.content.toString(), JsonParseMode.Loose);
33+
let jsonAst: JsonAstNode;
34+
try {
35+
jsonAst = parseJsonAst(content, JsonParseMode.Loose);
36+
} catch {
37+
throw new Error(`Invalid JSON AST Object (${path})`);
38+
}
39+
3340
if (jsonAst.kind !== 'object') {
3441
continue;
3542
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,10 @@ describe('Migration to create "Solution Style" tsconfig', () => {
114114
],
115115
});
116116
});
117+
118+
it('should not error out when a JSON file is a blank', async () => {
119+
tree.create('blank.json', '');
120+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
121+
expect(readJsonFile(newTree, 'src/tsconfig.json').extends).toEqual('./../tsconfig.base.json');
122+
});
117123
});

0 commit comments

Comments
 (0)
Please sign in to comment.