Skip to content

fix(@schematics/angular): don't error out on blank JSON files during migrations #18015

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

Merged
merged 1 commit into from
Jun 26, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { JsonAstString, JsonParseMode, dirname, join, normalize, parseJsonAst, resolve } from '@angular-devkit/core';
import { JsonAstNode, JsonAstString, JsonParseMode, dirname, join, normalize, parseJsonAst, resolve } from '@angular-devkit/core';
import { DirEntry, Rule, chain } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from '../../utility/json-utils';
import { getWorkspace } from '../../utility/workspace';
Expand All @@ -25,11 +25,18 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
}

const entry = directory.file(path);
if (!entry) {
const content = entry?.content.toString();
if (!content) {
continue;
}

const jsonAst = parseJsonAst(entry.content.toString(), JsonParseMode.Loose);
let jsonAst: JsonAstNode;
try {
jsonAst = parseJsonAst(content, JsonParseMode.Loose);
} catch {
throw new Error(`Invalid JSON AST Object (${path})`);
}

if (jsonAst.kind !== 'object') {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ describe('Migration to create "Solution Style" tsconfig', () => {
],
});
});

it('should not error out when a JSON file is a blank', async () => {
tree.create('blank.json', '');
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
expect(readJsonFile(newTree, 'src/tsconfig.json').extends).toEqual('./../tsconfig.base.json');
});
});