fix(compiler): 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:38:25 -08:00 committed by Alex Rickabaugh
parent b75d7cb11f
commit f728490222
5 changed files with 14 additions and 13 deletions

View File

@ -1,6 +1,6 @@
load("//tools:defaults.bzl", "protractor_web_test_suite", "ts_library")
def example_test(name, srcs, server, data = [], **kwargs):
def example_test(name, srcs, server, data = [], deps = [], **kwargs):
ts_library(
name = "%s_lib" % name,
testonly = True,
@ -12,7 +12,7 @@ def example_test(name, srcs, server, data = [], **kwargs):
"@npm//@types/jasminewd2",
"@npm//@types/selenium-webdriver",
"@npm//protractor",
],
] + deps,
)
protractor_web_test_suite(

View File

@ -8,4 +8,7 @@ example_test(
"//modules/playground/src/sourcemap:index.ts",
],
server = "//modules/playground/src/sourcemap:devserver",
deps = [
"@npm//source-map",
],
)

View File

@ -6,11 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/
import {readFileSync} from 'fs';
import {$, browser} from 'protractor';
import {logging} from 'selenium-webdriver';
const fs = require('fs');
const sourceMap = require('source-map');
import {RawSourceMap, SourceMapConsumer} from 'source-map';
describe('sourcemaps', function() {
const URL = '/';
@ -36,19 +35,18 @@ describe('sourcemaps', function() {
const content =
fs.readFileSync(require.resolve('../../src/sourcemap/index.js')).toString('utf8');
readFileSync(require.resolve('../../src/sourcemap/index.js')).toString('utf8');
const marker = '//# sourceMappingURL=data:application/json;base64,';
const index = content.indexOf(marker);
const sourceMapData =
Buffer.from(content.substring(index + marker.length), 'base64').toString('utf8');
const decoder = new sourceMap.SourceMapConsumer(JSON.parse(sourceMapData));
const decoder = new SourceMapConsumer(JSON.parse(sourceMapData) as RawSourceMap);
const originalPosition = decoder.originalPositionFor({line: errorLine, column: errorColumn});
const sourceCodeLines = fs.readFileSync(require.resolve('../../src/sourcemap/index.ts'), {
encoding: 'UTF-8'
}).split('\n');
const sourceCodeLines = readFileSync(require.resolve('../../src/sourcemap/index.ts'), {
encoding: 'UTF-8'
}).split('\n');
expect(sourceCodeLines[originalPosition.line - 1])
.toMatch(/throw new Error\(\'Sourcemap test\'\)/);
});

View File

@ -461,7 +461,7 @@ class FromJsonDeserializer extends ValueTransformer {
summaries: Summary<StaticSymbol>[],
importAs: {symbol: StaticSymbol, importAs: StaticSymbol}[]
} {
const data: {moduleName: string|null, summaries: any[], symbols: any[]} = JSON.parse(json);
const data = JSON.parse(json) as {moduleName: string | null, summaries: any[], symbols: any[]};
const allImportAs: {symbol: StaticSymbol, importAs: StaticSymbol}[] = [];
this.symbols = data.symbols.map(
(serializedSymbol) => this.symbolCache.get(

View File

@ -427,7 +427,7 @@ export class MockAotCompilerHost implements AotCompilerHost {
if (this.metadataVisible) {
const metadataPath = modulePath.replace(DTS, '.metadata.json');
if (this.tsHost.fileExists(metadataPath)) {
let result = JSON.parse(this.tsHost.readFile(metadataPath));
let result = JSON.parse(this.tsHost.readFile(metadataPath)) as {[key: string]: any}[];
return Array.isArray(result) ? result : [result];
}
}