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

+159
-101
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export function getEntryPointFormat(
256256
function loadPackageJson(
257257
fs: ReadonlyFileSystem, packageJsonPath: AbsoluteFsPath): EntryPointPackageJson|null {
258258
try {
259-
return JSON.parse(fs.readFile(packageJsonPath));
259+
return JSON.parse(fs.readFile(packageJsonPath)) as EntryPointPackageJson;
260260
} catch {
261261
return null;
262262
}

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
Original file line numberDiff line numberDiff line change
@@ -85,56 +85,57 @@ runInEachFileSystem(() => {
8585
const COMMON_PACKAGE_PATH = _('/node_modules/@angular/common/package.json');
8686
const fs = getFileSystem();
8787
const pkgUpdater = new DirectPackageJsonUpdater(fs);
88-
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
88+
// TODO: Determine the correct/best type for the `pkg` type.
89+
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
8990
expect(pkg.__processed_by_ivy_ngcc__).toBeUndefined();
9091
expect(pkg.scripts).toBeUndefined();
9192

9293
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015', 'fesm5']);
93-
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
94-
expect(pkg.__processed_by_ivy_ngcc__.fesm2015).toBe('0.0.0-PLACEHOLDER');
95-
expect(pkg.__processed_by_ivy_ngcc__.fesm5).toBe('0.0.0-PLACEHOLDER');
96-
expect(pkg.__processed_by_ivy_ngcc__.esm2015).toBeUndefined();
97-
expect(pkg.__processed_by_ivy_ngcc__.esm5).toBeUndefined();
98-
expect(pkg.scripts.prepublishOnly).toBeDefined();
94+
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
95+
expect(pkg.__processed_by_ivy_ngcc__!.fesm2015).toBe('0.0.0-PLACEHOLDER');
96+
expect(pkg.__processed_by_ivy_ngcc__!.fesm5).toBe('0.0.0-PLACEHOLDER');
97+
expect(pkg.__processed_by_ivy_ngcc__!.esm2015).toBeUndefined();
98+
expect(pkg.__processed_by_ivy_ngcc__!.esm5).toBeUndefined();
99+
expect(pkg.scripts!.prepublishOnly).toBeDefined();
99100

100101
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['esm2015', 'esm5']);
101-
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
102-
expect(pkg.__processed_by_ivy_ngcc__.fesm2015).toBe('0.0.0-PLACEHOLDER');
103-
expect(pkg.__processed_by_ivy_ngcc__.fesm5).toBe('0.0.0-PLACEHOLDER');
104-
expect(pkg.__processed_by_ivy_ngcc__.esm2015).toBe('0.0.0-PLACEHOLDER');
105-
expect(pkg.__processed_by_ivy_ngcc__.esm5).toBe('0.0.0-PLACEHOLDER');
106-
expect(pkg.scripts.prepublishOnly).toBeDefined();
102+
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
103+
expect(pkg.__processed_by_ivy_ngcc__!.fesm2015).toBe('0.0.0-PLACEHOLDER');
104+
expect(pkg.__processed_by_ivy_ngcc__!.fesm5).toBe('0.0.0-PLACEHOLDER');
105+
expect(pkg.__processed_by_ivy_ngcc__!.esm2015).toBe('0.0.0-PLACEHOLDER');
106+
expect(pkg.__processed_by_ivy_ngcc__!.esm5).toBe('0.0.0-PLACEHOLDER');
107+
expect(pkg.scripts!.prepublishOnly).toBeDefined();
107108
});
108109

109110
it('should update the packageJson object in-place', () => {
110111
const COMMON_PACKAGE_PATH = _('/node_modules/@angular/common/package.json');
111112
const fs = getFileSystem();
112113
const pkgUpdater = new DirectPackageJsonUpdater(fs);
113-
const pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
114+
const pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
114115
expect(pkg.__processed_by_ivy_ngcc__).toBeUndefined();
115116
expect(pkg.scripts).toBeUndefined();
116117

117118
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015', 'fesm5']);
118-
expect(pkg.__processed_by_ivy_ngcc__.fesm2015).toBe('0.0.0-PLACEHOLDER');
119-
expect(pkg.__processed_by_ivy_ngcc__.fesm5).toBe('0.0.0-PLACEHOLDER');
120-
expect(pkg.__processed_by_ivy_ngcc__.esm2015).toBeUndefined();
121-
expect(pkg.__processed_by_ivy_ngcc__.esm5).toBeUndefined();
122-
expect(pkg.scripts.prepublishOnly).toBeDefined();
119+
expect(pkg.__processed_by_ivy_ngcc__!.fesm2015).toBe('0.0.0-PLACEHOLDER');
120+
expect(pkg.__processed_by_ivy_ngcc__!.fesm5).toBe('0.0.0-PLACEHOLDER');
121+
expect(pkg.__processed_by_ivy_ngcc__!.esm2015).toBeUndefined();
122+
expect(pkg.__processed_by_ivy_ngcc__!.esm5).toBeUndefined();
123+
expect(pkg.scripts!.prepublishOnly).toBeDefined();
123124

124125
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['esm2015', 'esm5']);
125-
expect(pkg.__processed_by_ivy_ngcc__.fesm2015).toBe('0.0.0-PLACEHOLDER');
126-
expect(pkg.__processed_by_ivy_ngcc__.fesm5).toBe('0.0.0-PLACEHOLDER');
127-
expect(pkg.__processed_by_ivy_ngcc__.esm2015).toBe('0.0.0-PLACEHOLDER');
128-
expect(pkg.__processed_by_ivy_ngcc__.esm5).toBe('0.0.0-PLACEHOLDER');
129-
expect(pkg.scripts.prepublishOnly).toBeDefined();
126+
expect(pkg.__processed_by_ivy_ngcc__!.fesm2015).toBe('0.0.0-PLACEHOLDER');
127+
expect(pkg.__processed_by_ivy_ngcc__!.fesm5).toBe('0.0.0-PLACEHOLDER');
128+
expect(pkg.__processed_by_ivy_ngcc__!.esm2015).toBe('0.0.0-PLACEHOLDER');
129+
expect(pkg.__processed_by_ivy_ngcc__!.esm5).toBe('0.0.0-PLACEHOLDER');
130+
expect(pkg.scripts!.prepublishOnly).toBeDefined();
130131
});
131132

132133
it('should one perform one write operation for all updated properties', () => {
133134
const COMMON_PACKAGE_PATH = _('/node_modules/@angular/common/package.json');
134135
const fs = getFileSystem();
135136
const pkgUpdater = new DirectPackageJsonUpdater(fs);
136137
const writeFileSpy = spyOn(fs, 'writeFile');
137-
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
138+
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
138139

139140
markAsProcessed(
140141
pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015', 'fesm5', 'esm2015', 'esm5']);
@@ -146,34 +147,34 @@ runInEachFileSystem(() => {
146147
const fs = getFileSystem();
147148
const pkgUpdater = new DirectPackageJsonUpdater(fs);
148149
const prepublishOnly = 'existing script';
149-
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
150+
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
150151
pkg.scripts = {prepublishOnly};
151152

152153
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015']);
153-
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
154-
expect(pkg.scripts.prepublishOnly).toContain('This is not allowed');
155-
expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBe(prepublishOnly);
154+
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
155+
expect(pkg.scripts!.prepublishOnly).toContain('This is not allowed');
156+
expect(pkg.scripts!.prepublishOnly__ivy_ngcc_bak).toBe(prepublishOnly);
156157
});
157158

158159
it(`should not keep backup of overwritten 'prepublishOnly' script`, () => {
159160
const COMMON_PACKAGE_PATH = _('/node_modules/@angular/common/package.json');
160161
const fs = getFileSystem();
161162
const pkgUpdater = new DirectPackageJsonUpdater(fs);
162-
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
163+
let pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
163164

164165
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015']);
165166

166-
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
167-
expect(pkg.scripts.prepublishOnly).toContain('This is not allowed');
168-
expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBeUndefined();
167+
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
168+
expect(pkg.scripts!.prepublishOnly).toContain('This is not allowed');
169+
expect(pkg.scripts!.prepublishOnly__ivy_ngcc_bak).toBeUndefined();
169170

170171
// Running again, now that there is `prepublishOnly` script (created by `ngcc`), it should
171172
// still not back it up as `prepublishOnly__ivy_ngcc_bak`.
172173
markAsProcessed(pkgUpdater, pkg, COMMON_PACKAGE_PATH, ['fesm2015']);
173174

174-
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH));
175-
expect(pkg.scripts.prepublishOnly).toContain('This is not allowed');
176-
expect(pkg.scripts.prepublishOnly__ivy_ngcc_bak).toBeUndefined();
175+
pkg = JSON.parse(fs.readFile(COMMON_PACKAGE_PATH)) as EntryPointPackageJson;
176+
expect(pkg.scripts!.prepublishOnly).toContain('This is not allowed');
177+
expect(pkg.scripts!.prepublishOnly__ivy_ngcc_bak).toBeUndefined();
177178
});
178179
});
179180

0 commit comments

Comments
 (0)