Skip to content

Commit 4b469c9

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

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

packages/localize/schematics/ng-add/index_spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,20 @@ export { renderModule, renderModuleFactory } from '@angular/platform-server';`;
177177
it('should add package to `devDependencies` by default', async () => {
178178
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
179179
const packageJsonText = host.readContent('/package.json');
180-
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize'])
181-
.toBe('~0.0.0-PLACEHOLDER');
182-
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize']).toBeUndefined();
180+
const packageJsonObj = JSON.parse(packageJsonText) as
181+
{devDependencies: {[key: string]: string}, dependencies: {[key: string]: string}};
182+
expect(packageJsonObj.devDependencies?.['@angular/localize']).toBe('~0.0.0-PLACEHOLDER');
183+
expect(packageJsonObj.dependencies?.['@angular/localize']).toBeUndefined();
183184
});
184185

185186
it('should add package to `dependencies` if `useAtRuntime` is `true`', async () => {
186187
host = await schematicRunner
187188
.runSchematicAsync('ng-add', {...defaultOptions, useAtRuntime: true}, host)
188189
.toPromise();
189190
const packageJsonText = host.readContent('/package.json');
190-
expect(JSON.parse(packageJsonText).dependencies?.['@angular/localize'])
191-
.toBe('~0.0.0-PLACEHOLDER');
192-
expect(JSON.parse(packageJsonText).devDependencies?.['@angular/localize']).toBeUndefined();
191+
const packageJsonObj = JSON.parse(packageJsonText) as
192+
{devDependencies: {[key: string]: string}, dependencies: {[key: string]: string}};
193+
expect(packageJsonObj.dependencies?.['@angular/localize']).toBe('~0.0.0-PLACEHOLDER');
194+
expect(packageJsonObj.devDependencies?.['@angular/localize']).toBeUndefined();
193195
});
194196
});

packages/localize/src/tools/src/extract/translation_files/format_options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ export function validateOptions(name: string, validOptions: ValidOptions, option
4040
* @param optionString The string to parse.
4141
*/
4242
export function parseFormatOptions(optionString: string = '{}'): FormatOptions {
43-
return JSON.parse(optionString);
44-
}
43+
return JSON.parse(optionString) as FormatOptions;
44+
}

packages/localize/src/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class ArbTranslationParser implements TranslationParser<ArbJsonObject> {
9393
}
9494

9595
private tryParseArbFormat(contents: string): ArbJsonObject {
96-
const json = JSON.parse(contents);
96+
const json = JSON.parse(contents) as ArbJsonObject;
9797
if (typeof json['@@locale'] !== 'string') {
9898
throw new Error('Missing @@locale property.');
9999
}

packages/localize/src/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import {Diagnostics} from '../../../diagnostics';
1111

1212
import {ParseAnalysis, ParsedTranslationBundle, TranslationParser} from './translation_parser';
1313

14+
interface SimpleJsonFile {
15+
locale: string;
16+
translations: {[messageId: string]: string};
17+
}
18+
1419
/**
1520
* A translation parser that can parse JSON that has the form:
1621
*
@@ -27,16 +32,16 @@ import {ParseAnalysis, ParsedTranslationBundle, TranslationParser} from './trans
2732
* @see SimpleJsonTranslationSerializer
2833
* @publicApi used by CLI
2934
*/
30-
export class SimpleJsonTranslationParser implements TranslationParser<Object> {
35+
export class SimpleJsonTranslationParser implements TranslationParser<SimpleJsonFile> {
3136
/**
3237
* @deprecated
3338
*/
34-
canParse(filePath: string, contents: string): Object|false {
39+
canParse(filePath: string, contents: string): SimpleJsonFile|false {
3540
const result = this.analyze(filePath, contents);
3641
return result.canParse && result.hint;
3742
}
3843

39-
analyze(filePath: string, contents: string): ParseAnalysis<Object> {
44+
analyze(filePath: string, contents: string): ParseAnalysis<SimpleJsonFile> {
4045
const diagnostics = new Diagnostics();
4146
// For this to be parsable, the extension must be `.json` and the contents must include "locale"
4247
// and "translations" keys.
@@ -46,7 +51,7 @@ export class SimpleJsonTranslationParser implements TranslationParser<Object> {
4651
return {canParse: false, diagnostics};
4752
}
4853
try {
49-
const json = JSON.parse(contents);
54+
const json = JSON.parse(contents) as SimpleJsonFile;
5055
if (json.locale === undefined) {
5156
diagnostics.warn('Required "locale" property missing.');
5257
return {canParse: false, diagnostics};
@@ -70,8 +75,8 @@ export class SimpleJsonTranslationParser implements TranslationParser<Object> {
7075
}
7176
}
7277

73-
parse(_filePath: string, contents: string, json?: Object): ParsedTranslationBundle {
74-
const {locale: parsedLocale, translations} = json || JSON.parse(contents);
78+
parse(_filePath: string, contents: string, json?: SimpleJsonFile): ParsedTranslationBundle {
79+
const {locale: parsedLocale, translations} = json || JSON.parse(contents) as SimpleJsonFile;
7580
const parsedTranslations: Record<ɵMessageId, ɵParsedTranslation> = {};
7681
for (const messageId in translations) {
7782
const targetMessage = translations[messageId];

0 commit comments

Comments
 (0)