Skip to content

Commit 69b972f

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/schematics): replace usage of deprecated JSON parser with jsonc-parser
1 parent bb5760c commit 69b972f

File tree

6 files changed

+26
-75
lines changed

6 files changed

+26
-75
lines changed

goldens/public-api/angular_devkit/schematics/tools/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
import { analytics } from '@angular-devkit/core';
1010
import { BaseException } from '@angular-devkit/core';
11-
import { InvalidJsonCharacterException } from '@angular-devkit/core';
1211
import { JsonObject } from '@angular-devkit/core';
1312
import { logging } from '@angular-devkit/core';
1413
import { Observable } from 'rxjs';
1514
import { Path } from '@angular-devkit/core';
1615
import { PathFragment } from '@angular-devkit/core';
1716
import { schema } from '@angular-devkit/core';
18-
import { UnexpectedEndOfInputException } from '@angular-devkit/core';
1917
import { Url } from 'url';
2018
import { virtualFs } from '@angular-devkit/core';
2119
import { workflow } from '@angular-devkit/schematics';
@@ -177,7 +175,7 @@ export interface FileSystemSchematicJsonDescription {
177175

178176
// @public (undocumented)
179177
export class InvalidCollectionJsonException extends BaseException {
180-
constructor(_name: string, path: string, jsonException?: UnexpectedEndOfInputException | InvalidJsonCharacterException);
178+
constructor(_name: string, path: string, jsonException?: Error);
181179
}
182180

183181
// @public

packages/angular_devkit/schematics/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
],
1515
"dependencies": {
1616
"@angular-devkit/core": "0.0.0",
17+
"jsonc-parser": "3.0.0",
1718
"ora": "5.4.1",
1819
"rxjs": "6.6.7"
1920
}

packages/angular_devkit/schematics/tools/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ts_library(
3131
"//packages/angular_devkit/schematics/tasks",
3232
"//packages/angular_devkit/schematics/tasks/node",
3333
"@npm//@types/node",
34+
"@npm//jsonc-parser",
3435
"@npm//rxjs",
3536
],
3637
)

packages/angular_devkit/schematics/tools/file-system-engine-host-base.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
BaseException,
11-
InvalidJsonCharacterException,
12-
JsonObject,
13-
UnexpectedEndOfInputException,
14-
normalize,
15-
virtualFs,
16-
} from '@angular-devkit/core';
9+
import { BaseException, JsonObject, normalize, virtualFs } from '@angular-devkit/core';
1710
import { NodeJsSyncHost } from '@angular-devkit/core/node';
1811
import { existsSync, statSync } from 'fs';
1912
import { dirname, isAbsolute, join, resolve } from 'path';
@@ -51,11 +44,7 @@ export class CollectionCannotBeResolvedException extends BaseException {
5144
}
5245
}
5346
export class InvalidCollectionJsonException extends BaseException {
54-
constructor(
55-
_name: string,
56-
path: string,
57-
jsonException?: UnexpectedEndOfInputException | InvalidJsonCharacterException,
58-
) {
47+
constructor(_name: string, path: string, jsonException?: Error) {
5948
let msg = `Collection JSON at path ${JSON.stringify(path)} is invalid.`;
6049

6150
if (jsonException) {
@@ -320,7 +309,7 @@ export abstract class FileSystemEngineHostBase implements FileSystemEngineHost {
320309
return transformedOptions;
321310
};
322311

323-
return (observableFrom(transform()) as unknown) as Observable<ResultT>;
312+
return observableFrom(transform()) as unknown as Observable<ResultT>;
324313
}
325314

326315
transformContext(context: FileSystemSchematicContext): FileSystemSchematicContext {

packages/angular_devkit/schematics/tools/file-system-utility.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
FileDoesNotExistException,
11-
JsonParseMode,
12-
JsonValue,
13-
parseJson,
14-
} from '@angular-devkit/core';
9+
import { JsonValue } from '@angular-devkit/core';
10+
import { FileDoesNotExistException } from '@angular-devkit/schematics';
1511
import { existsSync, readFileSync } from 'fs';
12+
import { ParseError, parse, printParseErrorCode } from 'jsonc-parser';
1613

17-
/**
18-
* Read a file and returns its content. This supports different file encoding.
19-
*/
20-
export function readFile(fileName: string): string {
21-
if (!existsSync(fileName)) {
22-
throw new FileDoesNotExistException(fileName);
14+
export function readJsonFile(path: string): JsonValue {
15+
if (!existsSync(path)) {
16+
throw new FileDoesNotExistException(path);
2317
}
24-
const buffer = readFileSync(fileName);
25-
let len = buffer.length;
26-
if (len >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
27-
// Big endian UTF-16 byte order mark detected. Since big endian is not supported by node.js,
28-
// flip all byte pairs and treat as little endian.
29-
len &= ~1;
30-
for (let i = 0; i < len; i += 2) {
31-
const temp = buffer[i];
32-
buffer[i] = buffer[i + 1];
33-
buffer[i + 1] = temp;
34-
}
3518

36-
return buffer.toString('utf16le', 2);
37-
}
38-
if (len >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
39-
// Little endian UTF-16 byte order mark detected
40-
return buffer.toString('utf16le', 2);
41-
}
42-
if (len >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
43-
// UTF-8 byte order mark detected
44-
return buffer.toString('utf8', 3);
45-
}
19+
const errors: ParseError[] = [];
20+
const content = parse(readFileSync(path, 'utf-8'), errors, { allowTrailingComma: true });
4621

47-
// Default is UTF-8 with no byte order mark
48-
return buffer.toString('utf8');
49-
}
22+
if (errors.length) {
23+
const { error, offset } = errors[0];
24+
throw new Error(
25+
`Failed to parse "${path}" as JSON AST Object. ${printParseErrorCode(
26+
error,
27+
)} at location: ${offset}.`,
28+
);
29+
}
5030

51-
export function readJsonFile(path: string): JsonValue {
52-
return parseJson(readFile(path), JsonParseMode.Loose);
31+
return content;
5332
}

packages/angular_devkit/schematics/tools/node-module-engine-host.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
BaseException,
11-
InvalidJsonCharacterException,
12-
UnexpectedEndOfInputException,
13-
} from '@angular-devkit/core';
9+
import { BaseException } from '@angular-devkit/core';
1410
import { dirname, join, resolve } from 'path';
1511
import { RuleFactory } from '../src';
1612
import { FileSystemCollectionDesc, FileSystemSchematicDesc } from './description';
@@ -19,7 +15,6 @@ import {
1915
CollectionCannotBeResolvedException,
2016
CollectionMissingSchematicsMapException,
2117
FileSystemEngineHostBase,
22-
InvalidCollectionJsonException,
2318
SchematicMissingFieldsException,
2419
} from './file-system-engine-host-base';
2520
import { readJsonFile } from './file-system-utility';
@@ -98,21 +93,9 @@ export class NodeModulesEngineHost extends FileSystemEngineHostBase {
9893

9994
protected _resolveCollectionPath(name: string, requester?: string): string {
10095
const collectionPath = this.resolve(name, requester);
96+
readJsonFile(collectionPath);
10197

102-
try {
103-
readJsonFile(collectionPath);
104-
105-
return collectionPath;
106-
} catch (e) {
107-
if (
108-
e instanceof InvalidJsonCharacterException ||
109-
e instanceof UnexpectedEndOfInputException
110-
) {
111-
throw new InvalidCollectionJsonException(name, collectionPath, e);
112-
} else {
113-
throw e;
114-
}
115-
}
98+
return collectionPath;
11699
}
117100

118101
protected _resolveReferenceString(refString: string, parentPath: string) {

0 commit comments

Comments
 (0)