Skip to content

Commit 2c90391

Browse files
josephperrottalxhub
authored andcommitted
fix(bazel): update type castings for JSON.parse usage (#40710)
Update usages of JSON.parse to be cast as specific types. PR Close #40710
1 parent e721a5d commit 2c90391

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

packages/bazel/src/ng_package/packager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function main(args: string[]): number {
9191
const typeDefinitions = typeDefinitionsArg.split(',').filter(s => !!s);
9292
const srcs = srcsArg.split(',').filter(s => !!s);
9393
const dataFiles: string[] = dataArg.split(',').filter(s => !!s);
94-
const modulesManifest = JSON.parse(modulesManifestArg);
94+
const modulesManifest = JSON.parse(modulesManifestArg) as Record<string, any>;
9595
const dtsBundles: string[] = dtsBundleArg.split(',').filter(s => !!s);
9696

9797
/**
@@ -226,7 +226,7 @@ function main(args: string[]): number {
226226
let content = fs.readFileSync(src, 'utf-8');
227227
// Modify package.json files as necessary for publishing
228228
if (path.basename(src) === 'package.json') {
229-
const packageJson = JSON.parse(content);
229+
const packageJson = JSON.parse(content) as {[key: string]: string};
230230
content = amendPackageJson(src, packageJson, false);
231231

232232
const packageName = packageJson['name'];
@@ -439,7 +439,7 @@ export * from '${srcDirRelative(inputPath, typingsFile.replace(/\.d\.tsx?$/, '')
439439
* @param typingsPath the typings bundle entrypoint
440440
*/
441441
function rewireMetadata(metadataPath: string, typingsPath: string): string {
442-
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8'));
442+
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8')) as {[key: string]: any};
443443

444444
let typingsRelativePath =
445445
normalizeSeparators(path.relative(path.dirname(metadataPath), typingsPath));

packages/bazel/src/ngc-wrapped/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,9 @@ export function compile({
377377
if (importedFilePath.indexOf('node_modules') >= 0) {
378378
const maybeMetadataFile = importedFilePath.replace(EXT, '') + '.metadata.json';
379379
if (fs.existsSync(maybeMetadataFile)) {
380-
const moduleName =
381-
JSON.parse(fs.readFileSync(maybeMetadataFile, {encoding: 'utf-8'})).importAs;
380+
const moduleName = (JSON.parse(fs.readFileSync(maybeMetadataFile, {encoding: 'utf-8'})) as {
381+
importAs: string
382+
}).importAs;
382383
if (moduleName) {
383384
return moduleName;
384385
}

packages/bazel/test/ng_package/common_package.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,44 @@ describe('@angular/common ng_package', () => {
104104

105105

106106
describe('should have module resolution properties in the package.json file for', () => {
107+
interface PackageJson {
108+
main: string;
109+
es2015: string;
110+
module: string;
111+
typings: string;
112+
}
107113
// https://github.com/angular/common-builds/blob/master/package.json
108114
it('/', () => {
109-
const actual = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'}));
115+
const actual = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'})) as PackageJson;
110116
expect(actual['main']).toEqual('./bundles/common.umd.js');
111117
});
112118
// https://github.com/angular/common-builds/blob/master/http/package.json
113119
it('/http', () => {
114-
const actual = JSON.parse(fs.readFileSync('http/package.json', {encoding: 'utf-8'}));
120+
const actual = JSON.parse(fs.readFileSync('http/package.json', {encoding: 'utf-8'})) as PackageJson;
115121
expect(actual['main']).toEqual('../bundles/common-http.umd.js');
116122
expect(actual['es2015']).toEqual('../fesm2015/http.js');
117123
expect(actual['module']).toEqual('../fesm2015/http.js');
118124
expect(actual['typings']).toEqual('./http.d.ts');
119125
});
120126
// https://github.com/angular/common-builds/blob/master/testing/package.json
121127
it('/testing', () => {
122-
const actual = JSON.parse(fs.readFileSync('testing/package.json', {encoding: 'utf-8'}));
128+
const actual =
129+
JSON.parse(fs.readFileSync('testing/package.json', {encoding: 'utf-8'})) as PackageJson;
123130
expect(actual['main']).toEqual('../bundles/common-testing.umd.js');
124131
});
125132
// https://github.com/angular/common-builds/blob/master/http/testing/package.json
126133
it('/http/testing', () => {
127-
const actual = JSON.parse(fs.readFileSync('http/testing/package.json', {encoding: 'utf-8'}));
134+
const actual =
135+
JSON.parse(fs.readFileSync('http/testing/package.json', {encoding: 'utf-8'})) as PackageJson;
128136
expect(actual['main']).toEqual('../../bundles/common-http-testing.umd.js');
129137
expect(actual['es2015']).toEqual('../../fesm2015/http/testing.js');
130138
expect(actual['module']).toEqual('../../fesm2015/http/testing.js');
131139
expect(actual['typings']).toEqual('./testing.d.ts');
132140
});
133141
// https://github.com/angular/common-builds/blob/master/upgrade/package.json
134142
it('/upgrade', () => {
135-
const actual = JSON.parse(fs.readFileSync('upgrade/package.json', {encoding: 'utf-8'}));
143+
const actual =
144+
JSON.parse(fs.readFileSync('upgrade/package.json', {encoding: 'utf-8'})) as PackageJson;
136145
expect(actual['main']).toEqual('../bundles/common-upgrade.umd.js');
137146
expect(actual['es2015']).toEqual('../fesm2015/upgrade.js');
138147
expect(actual['module']).toEqual('../fesm2015/upgrade.js');

packages/bazel/test/ng_package/core_package.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ describe('@angular/core ng_package', () => {
5858
});
5959

6060
it('should contain metadata for ng update', () => {
61+
interface PackageJson {
62+
'ng-update': {packageGroup: string[];};
63+
}
6164
expect(shx.cat(packageJson)).not.toContain('NG_UPDATE_PACKAGE_GROUP');
62-
expect(JSON.parse(shx.cat(packageJson))['ng-update']['packageGroup'])
65+
expect((JSON.parse(shx.cat(packageJson)) as PackageJson)['ng-update'].packageGroup)
6366
.toContain('@angular/core');
6467
});
6568
});

0 commit comments

Comments
 (0)