Skip to content

Commit 960041d

Browse files
committed
fix(@schematics/angular): Fix JSON parsing
fixes angular#10880
1 parent d00b4c1 commit 960041d

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

packages/schematics/angular/library/index.ts

+3-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 { strings } from '@angular-devkit/core';
8+
import { parseJson, strings } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicContext,
@@ -53,8 +53,8 @@ function updateJsonFile<T>(host: Tree, path: string, callback: UpdateJsonFn<T>):
5353
const source = host.read(path);
5454
if (source) {
5555
const sourceText = source.toString('utf-8');
56-
const json = JSON.parse(sourceText);
57-
callback(json);
56+
const json = parseJson(sourceText);
57+
callback(json as {} as T);
5858
host.overwrite(path, JSON.stringify(json, null, 2));
5959
}
6060

packages/schematics/angular/utility/config.ts

+4-4
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 { experimental } from '@angular-devkit/core';
8+
import { JsonParseMode, experimental, parseJson } from '@angular-devkit/core';
99
import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
1010

1111

@@ -495,9 +495,9 @@ export function getWorkspace(host: Tree): WorkspaceSchema {
495495
if (configBuffer === null) {
496496
throw new SchematicsException(`Could not find (${path})`);
497497
}
498-
const config = configBuffer.toString();
498+
const content = configBuffer.toString();
499499

500-
return JSON.parse(config);
500+
return parseJson(content, JsonParseMode.Loose) as {} as WorkspaceSchema;
501501
}
502502

503503
export function addProjectToWorkspace(
@@ -531,7 +531,7 @@ export function getConfig(host: Tree): CliConfig {
531531
throw new SchematicsException('Could not find .angular-cli.json');
532532
}
533533

534-
const config = JSON.parse(configBuffer.toString());
534+
const config = parseJson(configBuffer.toString(), JsonParseMode.Loose) as {} as CliConfig;
535535

536536
return config;
537537
}

packages/schematics/package_update/utility/npm.ts

+13-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 { JsonObject, logging } from '@angular-devkit/core';
8+
import { JsonObject, JsonParseMode, logging, parseJson } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicContext,
@@ -93,7 +93,7 @@ function _getNpmPackageJson(
9393
response.on('data', chunk => data += chunk);
9494
response.on('end', () => {
9595
try {
96-
const json = JSON.parse(data);
96+
const json = parseJson(data, JsonParseMode.Strict);
9797
subject.next(json as JsonObject);
9898
subject.complete();
9999
} catch (err) {
@@ -233,7 +233,10 @@ export function updatePackageJson(
233233
if (!packageJsonContent) {
234234
throw new SchematicsException('Could not find package.json.');
235235
}
236-
const packageJson = JSON.parse(packageJsonContent.toString());
236+
const packageJson = parseJson(packageJsonContent.toString(), JsonParseMode.Strict);
237+
if (packageJson === null || typeof packageJson !== 'object' || Array.isArray(packageJson)) {
238+
throw new SchematicsException('Could not parse package.json.');
239+
}
237240
const packages: { [name: string]: string } = {};
238241
for (const name of supportedPackages) {
239242
packages[name] = version;
@@ -251,17 +254,20 @@ export function updatePackageJson(
251254
if (!packageJsonContent) {
252255
throw new SchematicsException('Could not find package.json.');
253256
}
254-
const packageJson = JSON.parse(packageJsonContent.toString());
257+
const packageJson = parseJson(packageJsonContent.toString(), JsonParseMode.Strict);
258+
if (packageJson === null || typeof packageJson !== 'object' || Array.isArray(packageJson)) {
259+
throw new SchematicsException('Could not parse package.json.');
260+
}
255261

256262
for (const field of kPackageJsonDependencyFields) {
257263
const deps = packageJson[field];
258-
if (!deps) {
264+
if (!deps || typeof deps !== 'object' || Array.isArray(deps)) {
259265
continue;
260266
}
261267

262-
for (const depName of Object.keys(packageJson[field])) {
268+
for (const depName of Object.keys(deps)) {
263269
if (allVersions[depName]) {
264-
packageJson[field][depName] = allVersions[depName];
270+
deps[depName] = allVersions[depName];
265271
}
266272
}
267273
}

0 commit comments

Comments
 (0)