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-09 10:48:43 -08:00
committed by Alex Rickabaugh
parent 2c90391d4b
commit cbdb5e208e
14 changed files with 27 additions and 17 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ export class Buildifier extends Formatter {
commandFlags: `${BAZEL_WARNING_FLAG} --lint=warn --mode=check --format=json`,
callback:
(_: string, code: number, stdout: string) => {
return code !== 0 || !JSON.parse(stdout)['success'];
return code !== 0 || !(JSON.parse(stdout) as {success: string}).success;
},
},
format: {
+1 -1
View File
@@ -2391,7 +2391,7 @@ class Buildifier extends Formatter {
check: {
commandFlags: `${BAZEL_WARNING_FLAG} --lint=warn --mode=check --format=json`,
callback: (_, code, stdout) => {
return code !== 0 || !JSON.parse(stdout)['success'];
return code !== 0 || !JSON.parse(stdout).success;
},
},
format: {
+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}.`);
+1 -1
View File
@@ -93,7 +93,7 @@ export function main(
info(yellow(` Please rerun with "--warnings" to inspect unresolved imports.`));
}
const expected: Golden = JSON.parse(readFileSync(goldenFile, 'utf8'));
const expected = JSON.parse(readFileSync(goldenFile, 'utf8')) as Golden;
const {fixedCircularDeps, newCircularDeps} = compareGoldens(actual, expected);
const isMatching = fixedCircularDeps.length === 0 && newCircularDeps.length === 0;