Skip to content

Commit f728490

Browse files
josephperrottalxhub
authored andcommitted
fix(compiler): update type castings for JSON.parse usage (#40710)
Update usages of JSON.parse to be cast as specific types. PR Close #40710
1 parent b75d7cb commit f728490

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

modules/playground/e2e_test/example_test.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("//tools:defaults.bzl", "protractor_web_test_suite", "ts_library")
22

3-
def example_test(name, srcs, server, data = [], **kwargs):
3+
def example_test(name, srcs, server, data = [], deps = [], **kwargs):
44
ts_library(
55
name = "%s_lib" % name,
66
testonly = True,
@@ -12,7 +12,7 @@ def example_test(name, srcs, server, data = [], **kwargs):
1212
"@npm//@types/jasminewd2",
1313
"@npm//@types/selenium-webdriver",
1414
"@npm//protractor",
15-
],
15+
] + deps,
1616
)
1717

1818
protractor_web_test_suite(

modules/playground/e2e_test/sourcemap/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ example_test(
88
"//modules/playground/src/sourcemap:index.ts",
99
],
1010
server = "//modules/playground/src/sourcemap:devserver",
11+
deps = [
12+
"@npm//source-map",
13+
],
1114
)

modules/playground/e2e_test/sourcemap/sourcemap_spec.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {readFileSync} from 'fs';
910
import {$, browser} from 'protractor';
1011
import {logging} from 'selenium-webdriver';
11-
12-
const fs = require('fs');
13-
const sourceMap = require('source-map');
12+
import {RawSourceMap, SourceMapConsumer} from 'source-map';
1413

1514
describe('sourcemaps', function() {
1615
const URL = '/';
@@ -36,19 +35,18 @@ describe('sourcemaps', function() {
3635

3736

3837
const content =
39-
fs.readFileSync(require.resolve('../../src/sourcemap/index.js')).toString('utf8');
38+
readFileSync(require.resolve('../../src/sourcemap/index.js')).toString('utf8');
4039
const marker = '//# sourceMappingURL=data:application/json;base64,';
4140
const index = content.indexOf(marker);
4241
const sourceMapData =
4342
Buffer.from(content.substring(index + marker.length), 'base64').toString('utf8');
4443

45-
const decoder = new sourceMap.SourceMapConsumer(JSON.parse(sourceMapData));
44+
const decoder = new SourceMapConsumer(JSON.parse(sourceMapData) as RawSourceMap);
4645

4746
const originalPosition = decoder.originalPositionFor({line: errorLine, column: errorColumn});
48-
49-
const sourceCodeLines = fs.readFileSync(require.resolve('../../src/sourcemap/index.ts'), {
50-
encoding: 'UTF-8'
51-
}).split('\n');
47+
const sourceCodeLines = readFileSync(require.resolve('../../src/sourcemap/index.ts'), {
48+
encoding: 'UTF-8'
49+
}).split('\n');
5250
expect(sourceCodeLines[originalPosition.line - 1])
5351
.toMatch(/throw new Error\(\'Sourcemap test\'\)/);
5452
});

packages/compiler/src/aot/summary_serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class FromJsonDeserializer extends ValueTransformer {
461461
summaries: Summary<StaticSymbol>[],
462462
importAs: {symbol: StaticSymbol, importAs: StaticSymbol}[]
463463
} {
464-
const data: {moduleName: string|null, summaries: any[], symbols: any[]} = JSON.parse(json);
464+
const data = JSON.parse(json) as {moduleName: string | null, summaries: any[], symbols: any[]};
465465
const allImportAs: {symbol: StaticSymbol, importAs: StaticSymbol}[] = [];
466466
this.symbols = data.symbols.map(
467467
(serializedSymbol) => this.symbolCache.get(

packages/compiler/test/aot/test_util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export class MockAotCompilerHost implements AotCompilerHost {
427427
if (this.metadataVisible) {
428428
const metadataPath = modulePath.replace(DTS, '.metadata.json');
429429
if (this.tsHost.fileExists(metadataPath)) {
430-
let result = JSON.parse(this.tsHost.readFile(metadataPath));
430+
let result = JSON.parse(this.tsHost.readFile(metadataPath)) as {[key: string]: any}[];
431431
return Array.isArray(result) ? result : [result];
432432
}
433433
}

0 commit comments

Comments
 (0)