fix(compiler-cli): update type castings for JSON.parse usage (#40710) · angular/angular@b75d7cb · GitHub
Skip to content

Commit b75d7cb

Browse files
josephperrottalxhub
authored andcommitted
fix(compiler-cli): update type castings for JSON.parse usage (#40710)
Update usages of JSON.parse to be cast as specific types. PR Close #40710
1 parent efd4149 commit b75d7cb

24 files changed

Lines changed: 159 additions & 101 deletions

packages/compiler-cli/ngcc/src/packages/entry_point.ts

Lines changed: 1 addition & 1 deletion

packages/compiler-cli/ngcc/src/packages/entry_point_manifest.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {EntryPointWithDependencies} from '../dependencies/dependency_host';
1313

1414
import {NGCC_VERSION} from './build_marker';
1515
import {NgccConfiguration} from './configuration';
16-
import {getEntryPointInfo, isEntryPoint} from './entry_point';
16+
import {getEntryPointInfo, isEntryPoint, PackageJsonFormatProperties} from './entry_point';
1717

1818
/**
1919
* Manages reading and writing a manifest file that contains a list of all the entry-points that
@@ -197,3 +197,9 @@ export interface EntryPointManifestFile {
197197
lockFileHash: string;
198198
entryPointPaths: EntryPointPaths[];
199199
}
200+
201+
202+
/** The JSON format of the entrypoint properties. */
203+
export type NewEntryPointPropertiesMap = {
204+
[Property in PackageJsonFormatProperties as `${Property}_ivy_ngcc`]?: string;
205+
};

packages/compiler-cli/ngcc/src/writing/cleaning/cleaning_strategies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import {absoluteFrom, AbsoluteFsPath, FileSystem, PathSegment} from '../../../../src/ngtsc/file_system';
99
import {cleanPackageJson} from '../../packages/build_marker';
10+
import {EntryPointPackageJson} from '../../packages/entry_point';
1011
import {NGCC_BACKUP_EXTENSION} from '../in_place_file_writer';
1112
import {NGCC_DIRECTORY} from '../new_entry_point_file_writer';
1213

@@ -30,7 +31,7 @@ export class PackageJsonCleaner implements CleaningStrategy {
3031
return basename === 'package.json';
3132
}
3233
clean(path: AbsoluteFsPath, _basename: PathSegment): void {
33-
const packageJson = JSON.parse(this.fs.readFile(path));
34+
const packageJson = JSON.parse(this.fs.readFile(path)) as EntryPointPackageJson;
3435
if (cleanPackageJson(packageJson)) {
3536
this.fs.writeFile(path, `${JSON.stringify(packageJson, null, 2)}\n`);
3637
}

packages/compiler-cli/ngcc/src/writing/new_entry_point_file_writer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ export class NewEntryPointFileWriter extends InPlaceFileWriter {
103103
const sourceMapPath = (originalSrcPath + '.map') as AbsoluteFsPath;
104104
if (this.fs.exists(sourceMapPath)) {
105105
try {
106-
const sourceMap = JSON.parse(this.fs.readFile(sourceMapPath));
106+
const sourceMap =
107+
JSON.parse(this.fs.readFile(sourceMapPath)) as {sourceRoot: string, [key: string]: any};
107108
const newSourceMapPath = (newSrcPath + '.map') as AbsoluteFsPath;
108109
const relativePath =
109110
this.fs.relative(this.fs.dirname(newSourceMapPath), this.fs.dirname(sourceMapPath));
110-
sourceMap['sourceRoot'] = this.fs.join(relativePath, sourceMap['sourceRoot'] || '.');
111+
sourceMap.sourceRoot = this.fs.join(relativePath, sourceMap.sourceRoot || '.');
111112
this.fs.ensureDir(this.fs.dirname(newSourceMapPath));
112113
this.fs.writeFile(newSourceMapPath, JSON.stringify(sourceMap));
113114
} catch (e) {

packages/compiler-cli/ngcc/src/writing/package_json_updater.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ export class DirectPackageJsonUpdater implements PackageJsonUpdater {
130130
// Read and parse the `package.json` content.
131131
// NOTE: We are not using `preExistingParsedJson` (even if specified) to avoid corrupting the
132132
// content on disk in case `preExistingParsedJson` is outdated.
133-
const parsedJson =
134-
this.fs.exists(packageJsonPath) ? JSON.parse(this.fs.readFile(packageJsonPath)) : {};
133+
const parsedJson = this.fs.exists(packageJsonPath) ?
134+
JSON.parse(this.fs.readFile(packageJsonPath)) as JsonObject :
135+
{};
135136

136137
// Apply all changes to both the canonical representation (read from disk) and any pre-existing,
137138
// in-memory representation.

packages/compiler-cli/ngcc/test/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ts_library(
2222
"//packages/compiler-cli/src/ngtsc/logging/testing",
2323
"//packages/compiler-cli/src/ngtsc/partial_evaluator",
2424
"//packages/compiler-cli/src/ngtsc/reflection",
25+
"//packages/compiler-cli/src/ngtsc/sourcemaps",
2526
"//packages/compiler-cli/src/ngtsc/testing",
2627
"//packages/compiler-cli/src/ngtsc/transform",
2728
"//packages/compiler-cli/src/ngtsc/translator",

packages/compiler-cli/ngcc/test/entry_point_finder/directory_walker_entry_point_finder_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ runInEachFileSystem(() => {
210210

211211
// Modify the manifest to prove that we use it to find the entry-points
212212
const manifestPath = _Abs('/sub_entry_points/node_modules/__ngcc_entry_points__.json');
213-
const manifestFile: EntryPointManifestFile = JSON.parse(fs.readFile(manifestPath));
213+
const manifestFile = JSON.parse(fs.readFile(manifestPath)) as EntryPointManifestFile;
214214
manifestFile.entryPointPaths.pop();
215215
fs.writeFile(manifestPath, JSON.stringify(manifestFile));
216216

packages/compiler-cli/ngcc/test/entry_point_finder/targeted_entry_point_finder_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {ModuleResolver} from '../../src/dependencies/module_resolver';
1616
import {TargetedEntryPointFinder} from '../../src/entry_point_finder/targeted_entry_point_finder';
1717
import {NGCC_VERSION} from '../../src/packages/build_marker';
1818
import {NgccConfiguration, ProcessedNgccPackageConfig} from '../../src/packages/configuration';
19-
import {EntryPoint} from '../../src/packages/entry_point';
19+
import {EntryPoint, EntryPointPackageJson} from '../../src/packages/entry_point';
2020
import {PathMappings} from '../../src/path_mappings';
2121

2222
runInEachFileSystem(() => {
@@ -555,7 +555,7 @@ runInEachFileSystem(() => {
555555

556556
// Add a build marker to the package.json
557557
const packageJsonPath = _Abs(`${targetPath}/package.json`);
558-
const packageJson = JSON.parse(fs.readFile(packageJsonPath));
558+
const packageJson = JSON.parse(fs.readFile(packageJsonPath)) as EntryPointPackageJson;
559559
packageJson.__processed_by_ivy_ngcc__ = {
560560
esm5: NGCC_VERSION,
561561
};
@@ -579,7 +579,7 @@ runInEachFileSystem(() => {
579579

580580
// Add build markers to the package.json
581581
const packageJsonPath = _Abs(`${targetPath}/package.json`);
582-
const packageJson = JSON.parse(fs.readFile(packageJsonPath));
582+
const packageJson = JSON.parse(fs.readFile(packageJsonPath)) as EntryPointPackageJson;
583583
packageJson.__processed_by_ivy_ngcc__ = {
584584
fesm2015: NGCC_VERSION,
585585
esm5: NGCC_VERSION,
@@ -625,7 +625,7 @@ runInEachFileSystem(() => {
625625

626626
// Add build markers to the package.json
627627
const packageJsonPath = _Abs(`${targetPath}/package.json`);
628-
const packageJson = JSON.parse(fs.readFile(packageJsonPath));
628+
const packageJson = JSON.parse(fs.readFile(packageJsonPath)) as EntryPointPackageJson;
629629
packageJson.__processed_by_ivy_ngcc__ = {
630630
esm5: NGCC_VERSION,
631631
};
@@ -651,7 +651,7 @@ runInEachFileSystem(() => {
651651

652652
// Add build markers to the package.json
653653
const packageJsonPath = _Abs(`${targetPath}/package.json`);
654-
const packageJson = JSON.parse(fs.readFile(packageJsonPath));
654+
const packageJson = JSON.parse(fs.readFile(packageJsonPath)) as EntryPointPackageJson;
655655
packageJson.__processed_by_ivy_ngcc__ = {
656656
fesm2015: NGCC_VERSION,
657657
};

packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ runInEachFileSystem(() => {
6363
function setupAngularCoreEsm5() {
6464
const pkgPath = _('/node_modules/@angular/core');
6565
const pkgJsonPath = fs.join(pkgPath, 'package.json');
66-
const pkgJson = JSON.parse(fs.readFile(pkgJsonPath));
66+
const pkgJson = JSON.parse(fs.readFile(pkgJsonPath)) as EntryPointPackageJson;
6767

6868
fs.ensureDir(fs.join(pkgPath, 'fesm5'));
6969
fs.writeFile(
@@ -1390,12 +1390,13 @@ runInEachFileSystem(() => {
13901390
{basePath: '/node_modules', propertiesToConsider: ['main'], logger: new MockLogger()});
13911391
// Check that common/testing ES5 was processed
13921392
let commonTesting =
1393-
JSON.parse(fs.readFile(_('/node_modules/@angular/common/testing/package.json')));
1393+
JSON.parse(fs.readFile(_('/node_modules/@angular/common/testing/package.json'))) as
1394+
EntryPointPackageJson;
13941395
expect(hasBeenProcessed(commonTesting, 'main')).toBe(true);
13951396
expect(hasBeenProcessed(commonTesting, 'esm2015')).toBe(false);
13961397
// Modify the manifest to test that is has no effect
1397-
let manifest: EntryPointManifestFile =
1398-
JSON.parse(fs.readFile(_('/node_modules/__ngcc_entry_points__.json')));
1398+
let manifest = JSON.parse(fs.readFile(_('/node_modules/__ngcc_entry_points__.json'))) as
1399+
EntryPointManifestFile;
13991400
manifest.entryPointPaths =
14001401
manifest.entryPointPaths.filter(paths => paths[1] !== '@angular/common/testing');
14011402
fs.writeFile(_('/node_modules/__ngcc_entry_points__.json'), JSON.stringify(manifest));
@@ -1409,12 +1410,14 @@ runInEachFileSystem(() => {
14091410
});
14101411
// Check that common/testing ES2015 is now processed, despite the manifest not listing it
14111412
commonTesting =
1412-
JSON.parse(fs.readFile(_('/node_modules/@angular/common/testing/package.json')));
1413+
JSON.parse(fs.readFile(_('/node_modules/@angular/common/testing/package.json'))) as
1414+
EntryPointPackageJson;
14131415
expect(hasBeenProcessed(commonTesting, 'main')).toBe(true);
14141416
expect(hasBeenProcessed(commonTesting, 'esm2015')).toBe(true);
14151417
// Check that the newly computed manifest has written to disk, containing the path that we
14161418
// had removed earlier.
1417-
manifest = JSON.parse(fs.readFile(_('/node_modules/__ngcc_entry_points__.json')));
1419+
manifest = JSON.parse(fs.readFile(_('/node_modules/__ngcc_entry_points__.json'))) as
1420+
EntryPointManifestFile;
14181421
expect(manifest.entryPointPaths).toContain([
14191422
'@angular/common',
14201423
'@angular/common/testing',
@@ -2248,7 +2251,8 @@ runInEachFileSystem(() => {
22482251

22492252
function loadPackage(
22502253
packageName: string, basePath: AbsoluteFsPath = _('/node_modules')): EntryPointPackageJson {
2251-
return JSON.parse(fs.readFile(fs.resolve(basePath, packageName, 'package.json')));
2254+
return JSON.parse(fs.readFile(fs.resolve(basePath, packageName, 'package.json'))) as
2255+
EntryPointPackageJson;
22522256
}
22532257

22542258
function initMockFileSystem(fs: FileSystem, testFiles: Folder) {

packages/compiler-cli/ngcc/test/packages/build_marker_spec.ts

Lines changed: 37 additions & 36 deletions

0 commit comments

Comments
 (0)