fix(bazel): update type castings for JSON.parse usage (#40710)

Update usages of JSON.parse to be cast as specific types.

PR Close #40710
This commit is contained in:
Joey Perrott 2021-02-04 14:40:21 -08:00 committed by Alex Rickabaugh
parent e721a5d258
commit 2c90391d4b
4 changed files with 24 additions and 11 deletions

View File

@ -91,7 +91,7 @@ function main(args: string[]): number {
const typeDefinitions = typeDefinitionsArg.split(',').filter(s => !!s); const typeDefinitions = typeDefinitionsArg.split(',').filter(s => !!s);
const srcs = srcsArg.split(',').filter(s => !!s); const srcs = srcsArg.split(',').filter(s => !!s);
const dataFiles: string[] = dataArg.split(',').filter(s => !!s); const dataFiles: string[] = dataArg.split(',').filter(s => !!s);
const modulesManifest = JSON.parse(modulesManifestArg); const modulesManifest = JSON.parse(modulesManifestArg) as Record<string, any>;
const dtsBundles: string[] = dtsBundleArg.split(',').filter(s => !!s); const dtsBundles: string[] = dtsBundleArg.split(',').filter(s => !!s);
/** /**
@ -226,7 +226,7 @@ function main(args: string[]): number {
let content = fs.readFileSync(src, 'utf-8'); let content = fs.readFileSync(src, 'utf-8');
// Modify package.json files as necessary for publishing // Modify package.json files as necessary for publishing
if (path.basename(src) === 'package.json') { if (path.basename(src) === 'package.json') {
const packageJson = JSON.parse(content); const packageJson = JSON.parse(content) as {[key: string]: string};
content = amendPackageJson(src, packageJson, false); content = amendPackageJson(src, packageJson, false);
const packageName = packageJson['name']; const packageName = packageJson['name'];
@ -439,7 +439,7 @@ export * from '${srcDirRelative(inputPath, typingsFile.replace(/\.d\.tsx?$/, '')
* @param typingsPath the typings bundle entrypoint * @param typingsPath the typings bundle entrypoint
*/ */
function rewireMetadata(metadataPath: string, typingsPath: string): string { function rewireMetadata(metadataPath: string, typingsPath: string): string {
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8')); const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8')) as {[key: string]: any};
let typingsRelativePath = let typingsRelativePath =
normalizeSeparators(path.relative(path.dirname(metadataPath), typingsPath)); normalizeSeparators(path.relative(path.dirname(metadataPath), typingsPath));

View File

@ -377,8 +377,9 @@ export function compile({
if (importedFilePath.indexOf('node_modules') >= 0) { if (importedFilePath.indexOf('node_modules') >= 0) {
const maybeMetadataFile = importedFilePath.replace(EXT, '') + '.metadata.json'; const maybeMetadataFile = importedFilePath.replace(EXT, '') + '.metadata.json';
if (fs.existsSync(maybeMetadataFile)) { if (fs.existsSync(maybeMetadataFile)) {
const moduleName = const moduleName = (JSON.parse(fs.readFileSync(maybeMetadataFile, {encoding: 'utf-8'})) as {
JSON.parse(fs.readFileSync(maybeMetadataFile, {encoding: 'utf-8'})).importAs; importAs: string
}).importAs;
if (moduleName) { if (moduleName) {
return moduleName; return moduleName;
} }

View File

@ -104,14 +104,20 @@ describe('@angular/common ng_package', () => {
describe('should have module resolution properties in the package.json file for', () => { describe('should have module resolution properties in the package.json file for', () => {
interface PackageJson {
main: string;
es2015: string;
module: string;
typings: string;
}
// https://github.com/angular/common-builds/blob/master/package.json // https://github.com/angular/common-builds/blob/master/package.json
it('/', () => { it('/', () => {
const actual = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'})); const actual = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'})) as PackageJson;
expect(actual['main']).toEqual('./bundles/common.umd.js'); expect(actual['main']).toEqual('./bundles/common.umd.js');
}); });
// https://github.com/angular/common-builds/blob/master/http/package.json // https://github.com/angular/common-builds/blob/master/http/package.json
it('/http', () => { it('/http', () => {
const actual = JSON.parse(fs.readFileSync('http/package.json', {encoding: 'utf-8'})); const actual = JSON.parse(fs.readFileSync('http/package.json', {encoding: 'utf-8'})) as PackageJson;
expect(actual['main']).toEqual('../bundles/common-http.umd.js'); expect(actual['main']).toEqual('../bundles/common-http.umd.js');
expect(actual['es2015']).toEqual('../fesm2015/http.js'); expect(actual['es2015']).toEqual('../fesm2015/http.js');
expect(actual['module']).toEqual('../fesm2015/http.js'); expect(actual['module']).toEqual('../fesm2015/http.js');
@ -119,12 +125,14 @@ describe('@angular/common ng_package', () => {
}); });
// https://github.com/angular/common-builds/blob/master/testing/package.json // https://github.com/angular/common-builds/blob/master/testing/package.json
it('/testing', () => { it('/testing', () => {
const actual = JSON.parse(fs.readFileSync('testing/package.json', {encoding: 'utf-8'})); const actual =
JSON.parse(fs.readFileSync('testing/package.json', {encoding: 'utf-8'})) as PackageJson;
expect(actual['main']).toEqual('../bundles/common-testing.umd.js'); expect(actual['main']).toEqual('../bundles/common-testing.umd.js');
}); });
// https://github.com/angular/common-builds/blob/master/http/testing/package.json // https://github.com/angular/common-builds/blob/master/http/testing/package.json
it('/http/testing', () => { it('/http/testing', () => {
const actual = JSON.parse(fs.readFileSync('http/testing/package.json', {encoding: 'utf-8'})); const actual =
JSON.parse(fs.readFileSync('http/testing/package.json', {encoding: 'utf-8'})) as PackageJson;
expect(actual['main']).toEqual('../../bundles/common-http-testing.umd.js'); expect(actual['main']).toEqual('../../bundles/common-http-testing.umd.js');
expect(actual['es2015']).toEqual('../../fesm2015/http/testing.js'); expect(actual['es2015']).toEqual('../../fesm2015/http/testing.js');
expect(actual['module']).toEqual('../../fesm2015/http/testing.js'); expect(actual['module']).toEqual('../../fesm2015/http/testing.js');
@ -132,7 +140,8 @@ describe('@angular/common ng_package', () => {
}); });
// https://github.com/angular/common-builds/blob/master/upgrade/package.json // https://github.com/angular/common-builds/blob/master/upgrade/package.json
it('/upgrade', () => { it('/upgrade', () => {
const actual = JSON.parse(fs.readFileSync('upgrade/package.json', {encoding: 'utf-8'})); const actual =
JSON.parse(fs.readFileSync('upgrade/package.json', {encoding: 'utf-8'})) as PackageJson;
expect(actual['main']).toEqual('../bundles/common-upgrade.umd.js'); expect(actual['main']).toEqual('../bundles/common-upgrade.umd.js');
expect(actual['es2015']).toEqual('../fesm2015/upgrade.js'); expect(actual['es2015']).toEqual('../fesm2015/upgrade.js');
expect(actual['module']).toEqual('../fesm2015/upgrade.js'); expect(actual['module']).toEqual('../fesm2015/upgrade.js');

View File

@ -58,8 +58,11 @@ describe('@angular/core ng_package', () => {
}); });
it('should contain metadata for ng update', () => { it('should contain metadata for ng update', () => {
interface PackageJson {
'ng-update': {packageGroup: string[];};
}
expect(shx.cat(packageJson)).not.toContain('NG_UPDATE_PACKAGE_GROUP'); expect(shx.cat(packageJson)).not.toContain('NG_UPDATE_PACKAGE_GROUP');
expect(JSON.parse(shx.cat(packageJson))['ng-update']['packageGroup']) expect((JSON.parse(shx.cat(packageJson)) as PackageJson)['ng-update'].packageGroup)
.toContain('@angular/core'); .toContain('@angular/core');
}); });
}); });