fix(dev-infra): 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:42:42 -08:00
committed by Alex Rickabaugh
parent 2c90391d4b
commit cbdb5e208e
14 changed files with 27 additions and 17 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ describe('ng-dev release build', () => {
expect(writeSpy).toHaveBeenCalledTimes(1);
const jsonText = writeSpy.calls.mostRecent().args[0] as string;
const parsed = JSON.parse(jsonText);
const parsed = JSON.parse(jsonText) as releaseConfig.BuiltPackage[];
expect(parsed).toEqual([
{name: '@angular/pkg1', outputPath: 'dist/pkg1'},
+4 -2
View File
@@ -82,7 +82,8 @@ export abstract class ReleaseAction {
/** Updates the version in the project top-level `package.json` file. */
protected async updateProjectVersion(newVersion: semver.SemVer) {
const pkgJsonPath = join(this.projectDir, packageJsonPath);
const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'));
const pkgJson =
JSON.parse(await fs.readFile(pkgJsonPath, 'utf8')) as {version: string, [key: string]: any};
pkgJson.version = newVersion.format();
// Write the `package.json` file. Note that we add a trailing new line
// to avoid unnecessary diff. IDEs usually add a trailing new line.
@@ -558,7 +559,8 @@ export abstract class ReleaseAction {
private async _verifyPackageVersions(version: semver.SemVer, packages: BuiltPackage[]) {
for (const pkg of packages) {
const {version: packageJsonVersion} =
JSON.parse(await fs.readFile(join(pkg.outputPath, 'package.json'), 'utf8'));
JSON.parse(await fs.readFile(join(pkg.outputPath, 'package.json'), 'utf8')) as
{version: string, [key: string]: any};
if (version.compare(packageJsonVersion) !== 0) {
error(red('The built package version does not match the version being released.'));
error(` Release Version: ${version.version}`);
@@ -64,7 +64,7 @@ export async function invokeReleaseBuildCommand(): Promise<BuiltPackage[]> {
info(green(' ✓ Built release output for all packages.'));
// The `ng-dev release build` command prints a JSON array to stdout
// that represents the built release packages and their output paths.
return JSON.parse(stdout.trim());
return JSON.parse(stdout.trim()) as BuiltPackage[];
} catch (e) {
spinner.stop();
error(e);
@@ -49,7 +49,7 @@ describe('cut next pre-release action', () => {
await expectStagingAndPublishWithoutCherryPick(action, 'master', '10.2.0-next.0', 'next');
const pkgJsonContents = readFileSync(join(action.testTmpDir, packageJsonPath), 'utf8');
const pkgJson = JSON.parse(pkgJsonContents);
const pkgJson = JSON.parse(pkgJsonContents) as {version: string, [key: string]: any};
expect(pkgJson.version).toBe('10.2.0-next.0', 'Expected version to not have changed.');
});
@@ -35,7 +35,8 @@ export async function getVersionOfBranch(
repo: GithubRepoWithApi, branchName: string): Promise<semver.SemVer> {
const {data} = await repo.api.repos.getContents(
{owner: repo.owner, repo: repo.name, path: '/package.json', ref: branchName});
const {version} = JSON.parse(Buffer.from(data.content, 'base64').toString());
const {version} = JSON.parse(Buffer.from(data.content, 'base64').toString()) as
{version: string, [key: string]: any};
const parsedVersion = semver.parse(version);
if (parsedVersion === null) {
throw Error(`Invalid version detected in following branch: ${branchName}.`);