From cbdb5e208e3411cdb021e23f3602f8478ddd48c2 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Thu, 4 Feb 2021 14:42:42 -0800 Subject: [PATCH] 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 --- dev-infra/format/formatters/buildifier.ts | 2 +- dev-infra/ng-dev.js | 2 +- dev-infra/release/build/build.spec.ts | 2 +- dev-infra/release/publish/actions.ts | 6 ++++-- dev-infra/release/publish/external-commands.ts | 2 +- .../release/publish/test/cut-next-prerelease.spec.ts | 2 +- dev-infra/release/versioning/version-branches.ts | 3 ++- dev-infra/ts-circular-dependencies/index.ts | 2 +- modules/playground/e2e_test/sourcemap/sourcemap_spec.ts | 1 - packages/bazel/test/ng_package/common_package.spec.ts | 9 ++++++--- .../benchpress/src/webdriver/chrome_driver_extension.ts | 4 +++- .../benchpress/src/webdriver/ios_driver_extension.ts | 4 +++- .../benchpress/test/reporter/json_file_reporter_spec.ts | 2 +- tools/ts-api-guardian/lib/main.ts | 3 ++- 14 files changed, 27 insertions(+), 17 deletions(-) diff --git a/dev-infra/format/formatters/buildifier.ts b/dev-infra/format/formatters/buildifier.ts index 8edb68a71a..9f52e50cd0 100644 --- a/dev-infra/format/formatters/buildifier.ts +++ b/dev-infra/format/formatters/buildifier.ts @@ -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: { diff --git a/dev-infra/ng-dev.js b/dev-infra/ng-dev.js index 3f2e24082c..757a4b279e 100755 --- a/dev-infra/ng-dev.js +++ b/dev-infra/ng-dev.js @@ -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: { diff --git a/dev-infra/release/build/build.spec.ts b/dev-infra/release/build/build.spec.ts index 9f6ca0fb80..16cb0d5b66 100644 --- a/dev-infra/release/build/build.spec.ts +++ b/dev-infra/release/build/build.spec.ts @@ -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'}, diff --git a/dev-infra/release/publish/actions.ts b/dev-infra/release/publish/actions.ts index 72d907026f..2ffd33b4b4 100644 --- a/dev-infra/release/publish/actions.ts +++ b/dev-infra/release/publish/actions.ts @@ -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}`); diff --git a/dev-infra/release/publish/external-commands.ts b/dev-infra/release/publish/external-commands.ts index 49698607e6..3c9cfbe8f7 100644 --- a/dev-infra/release/publish/external-commands.ts +++ b/dev-infra/release/publish/external-commands.ts @@ -64,7 +64,7 @@ export async function invokeReleaseBuildCommand(): Promise { 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); diff --git a/dev-infra/release/publish/test/cut-next-prerelease.spec.ts b/dev-infra/release/publish/test/cut-next-prerelease.spec.ts index ab37350788..3802d6653e 100644 --- a/dev-infra/release/publish/test/cut-next-prerelease.spec.ts +++ b/dev-infra/release/publish/test/cut-next-prerelease.spec.ts @@ -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.'); }); diff --git a/dev-infra/release/versioning/version-branches.ts b/dev-infra/release/versioning/version-branches.ts index 7a934d3a76..5db34f82c3 100644 --- a/dev-infra/release/versioning/version-branches.ts +++ b/dev-infra/release/versioning/version-branches.ts @@ -35,7 +35,8 @@ export async function getVersionOfBranch( repo: GithubRepoWithApi, branchName: string): Promise { 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}.`); diff --git a/dev-infra/ts-circular-dependencies/index.ts b/dev-infra/ts-circular-dependencies/index.ts index 2800c31ba4..f6eae504f9 100644 --- a/dev-infra/ts-circular-dependencies/index.ts +++ b/dev-infra/ts-circular-dependencies/index.ts @@ -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; diff --git a/modules/playground/e2e_test/sourcemap/sourcemap_spec.ts b/modules/playground/e2e_test/sourcemap/sourcemap_spec.ts index adcd571012..28f1ca4e1a 100644 --- a/modules/playground/e2e_test/sourcemap/sourcemap_spec.ts +++ b/modules/playground/e2e_test/sourcemap/sourcemap_spec.ts @@ -42,7 +42,6 @@ describe('sourcemaps', function() { Buffer.from(content.substring(index + marker.length), 'base64').toString('utf8'); const decoder = new SourceMapConsumer(JSON.parse(sourceMapData) as RawSourceMap); - const originalPosition = decoder.originalPositionFor({line: errorLine, column: errorColumn}); const sourceCodeLines = readFileSync(require.resolve('../../src/sourcemap/index.ts'), { encoding: 'UTF-8' diff --git a/packages/bazel/test/ng_package/common_package.spec.ts b/packages/bazel/test/ng_package/common_package.spec.ts index 5035f4c392..9eb5c0b7ba 100644 --- a/packages/bazel/test/ng_package/common_package.spec.ts +++ b/packages/bazel/test/ng_package/common_package.spec.ts @@ -112,12 +112,14 @@ describe('@angular/common ng_package', () => { } // https://github.com/angular/common-builds/blob/master/package.json it('/', () => { - const actual = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'})) as PackageJson; + const actual = + JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'})) as PackageJson; expect(actual['main']).toEqual('./bundles/common.umd.js'); }); // https://github.com/angular/common-builds/blob/master/http/package.json it('/http', () => { - const actual = JSON.parse(fs.readFileSync('http/package.json', {encoding: 'utf-8'})) as PackageJson; + 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['es2015']).toEqual('../fesm2015/http.js'); expect(actual['module']).toEqual('../fesm2015/http.js'); @@ -132,7 +134,8 @@ describe('@angular/common ng_package', () => { // https://github.com/angular/common-builds/blob/master/http/testing/package.json it('/http/testing', () => { const actual = - JSON.parse(fs.readFileSync('http/testing/package.json', {encoding: 'utf-8'})) as PackageJson; + 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['es2015']).toEqual('../../fesm2015/http/testing.js'); expect(actual['module']).toEqual('../../fesm2015/http/testing.js'); diff --git a/packages/benchpress/src/webdriver/chrome_driver_extension.ts b/packages/benchpress/src/webdriver/chrome_driver_extension.ts index ab9f29afb4..9fecaa33a3 100644 --- a/packages/benchpress/src/webdriver/chrome_driver_extension.ts +++ b/packages/benchpress/src/webdriver/chrome_driver_extension.ts @@ -86,7 +86,9 @@ export class ChromeDriverExtension extends WebDriverExtension { .then((entries) => { const events: PerfLogEvent[] = []; entries.forEach((entry: any) => { - const message = (JSON.parse(entry['message']) as {message: any})['message']; + const message = + (JSON.parse(entry['message']) as + {message: {method: string, params: PerfLogEvent}})['message']; if (message['method'] === 'Tracing.dataCollected') { events.push(message['params']); } diff --git a/packages/benchpress/src/webdriver/ios_driver_extension.ts b/packages/benchpress/src/webdriver/ios_driver_extension.ts index df85000561..5da139a7e8 100644 --- a/packages/benchpress/src/webdriver/ios_driver_extension.ts +++ b/packages/benchpress/src/webdriver/ios_driver_extension.ts @@ -44,7 +44,9 @@ export class IOsDriverExtension extends WebDriverExtension { .then((entries) => { const records: any[] = []; entries.forEach((entry: any) => { - const message = (JSON.parse(entry['message']) as {message: any})['message']; + const message = + (JSON.parse(entry['message']) as + {message: {method: string, params: PerfLogEvent}})['message']; if (message['method'] === 'Timeline.eventRecorded') { records.push(message['params']['record']); } diff --git a/packages/benchpress/test/reporter/json_file_reporter_spec.ts b/packages/benchpress/test/reporter/json_file_reporter_spec.ts index a7943a4a48..6fc2657ba7 100644 --- a/packages/benchpress/test/reporter/json_file_reporter_spec.ts +++ b/packages/benchpress/test/reporter/json_file_reporter_spec.ts @@ -50,7 +50,7 @@ import {Injector, JsonFileReporter, MeasureValues, Options, SampleDescription} f [mv(0, 0, {'a': 3, 'b': 6}), mv(1, 1, {'a': 5, 'b': 9})]); const regExp = /somePath\/someId_\d+\.json/; expect(loggedFile['filename'].match(regExp) != null).toBe(true); - const parsedContent = JSON.parse(loggedFile['content']) as any; + const parsedContent = JSON.parse(loggedFile['content']) as {[key: string]: any}; expect(parsedContent).toEqual({ 'description': { 'id': 'someId', diff --git a/tools/ts-api-guardian/lib/main.ts b/tools/ts-api-guardian/lib/main.ts index d7428ef87c..777bce97c1 100644 --- a/tools/ts-api-guardian/lib/main.ts +++ b/tools/ts-api-guardian/lib/main.ts @@ -86,7 +86,8 @@ export function discoverAllEntrypoints(dirPath: string) { // Get all typings file locations from package.json files for (const packageJson of packageJsons) { - const packageJsonObj = JSON.parse(fs.readFileSync(packageJson, {encoding: 'utf8'})) as any; + const packageJsonObj = + JSON.parse(fs.readFileSync(packageJson, {encoding: 'utf8'})) as {typings: string}; const typings = packageJsonObj.typings; if (typings) { entryPoints.push(path.join(path.dirname(packageJson), typings));