Skip to content

Commit d36fd3d

Browse files
alan-agius4AndrewKushnir
authored andcommitted
refactor(localize): remove deprecated canParse method from TranslationParsers (#47275)
This change removed the deprecated `canParse` method from all the TranslationParsers. BREAKING CHANGE: - `canParse` method has been removed from all translation parsers in `@angular/localize/tools`. `analyze` should be used instead. - the `hint` parameter in the`parse` methods is now mandatory. PR Close #47275
1 parent 93d7d91 commit d36fd3d

File tree

14 files changed

+1764
-1928
lines changed

14 files changed

+1764
-1928
lines changed

goldens/public-api/localize/tools/index.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import { ɵSourceMessage } from '@angular/localize';
2323
export class ArbTranslationParser implements TranslationParser<ArbJsonObject> {
2424
// (undocumented)
2525
analyze(_filePath: string, contents: string): ParseAnalysis<ArbJsonObject>;
26-
// @deprecated (undocumented)
27-
canParse(filePath: string, contents: string): ArbJsonObject | false;
2826
// (undocumented)
2927
parse(_filePath: string, contents: string, arb?: ArbJsonObject): ParsedTranslationBundle;
3028
}
@@ -96,8 +94,6 @@ export class MessageExtractor {
9694
export class SimpleJsonTranslationParser implements TranslationParser<SimpleJsonFile> {
9795
// (undocumented)
9896
analyze(filePath: string, contents: string): ParseAnalysis<SimpleJsonFile>;
99-
// @deprecated (undocumented)
100-
canParse(filePath: string, contents: string): SimpleJsonFile | false;
10197
// (undocumented)
10298
parse(_filePath: string, contents: string, json?: SimpleJsonFile): ParsedTranslationBundle;
10399
}
@@ -131,10 +127,8 @@ export function unwrapSubstitutionsFromLocalizeCall(call: NodePath<t.CallExpress
131127
export class Xliff1TranslationParser implements TranslationParser<XmlTranslationParserHint> {
132128
// (undocumented)
133129
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
134-
// @deprecated (undocumented)
135-
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
136130
// (undocumented)
137-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
131+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint): ParsedTranslationBundle;
138132
}
139133

140134
// @public
@@ -148,10 +142,8 @@ export class Xliff1TranslationSerializer implements TranslationSerializer {
148142
export class Xliff2TranslationParser implements TranslationParser<XmlTranslationParserHint> {
149143
// (undocumented)
150144
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
151-
// @deprecated (undocumented)
152-
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
153145
// (undocumented)
154-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
146+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint): ParsedTranslationBundle;
155147
}
156148

157149
// @public
@@ -172,10 +164,8 @@ export class XmbTranslationSerializer implements TranslationSerializer {
172164
export class XtbTranslationParser implements TranslationParser<XmlTranslationParserHint> {
173165
// (undocumented)
174166
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
175-
// @deprecated (undocumented)
176-
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
177167
// (undocumented)
178-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
168+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint): ParsedTranslationBundle;
179169
}
180170

181171
// (No @packageDocumentation comment for this package)

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ export interface ArbLocation {
5353
* ```
5454
*/
5555
export class ArbTranslationParser implements TranslationParser<ArbJsonObject> {
56-
/**
57-
* @deprecated
58-
*/
59-
canParse(filePath: string, contents: string): ArbJsonObject|false {
60-
const result = this.analyze(filePath, contents);
61-
return result.canParse && result.hint;
62-
}
63-
6456
analyze(_filePath: string, contents: string): ParseAnalysis<ArbJsonObject> {
6557
const diagnostics = new Diagnostics();
6658
if (!contents.includes('"@@locale"')) {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ interface SimpleJsonFile {
3434
* @publicApi used by CLI
3535
*/
3636
export class SimpleJsonTranslationParser implements TranslationParser<SimpleJsonFile> {
37-
/**
38-
* @deprecated
39-
*/
40-
canParse(filePath: string, contents: string): SimpleJsonFile|false {
41-
const result = this.analyze(filePath, contents);
42-
return result.canParse && result.hint;
43-
}
44-
4537
analyze(filePath: string, contents: string): ParseAnalysis<SimpleJsonFile> {
4638
const diagnostics = new Diagnostics();
4739
// For this to be parsable, the extension must be `.json` and the contents must include "locale"

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,21 @@ export interface ParsedTranslationBundle {
4444
/**
4545
* Implement this interface to provide a class that can parse the contents of a translation file.
4646
*
47-
* The `canParse()` method can return a hint that can be used by the `parse()` method to speed up
48-
* parsing. This allows the parser to do significant work to determine if the file can be parsed
47+
* The `analyze()` method can return a hint that can be used by the `parse()` method to speed
48+
* up parsing. This allows the parser to do significant work to determine if the file can be parsed
4949
* without duplicating the work when it comes to actually parsing the file.
5050
*
5151
* Example usage:
5252
*
5353
* ```
5454
* const parser: TranslationParser = getParser();
55-
* const result = parser.canParse(filePath, content);
56-
* if (result) {
57-
* return parser.parse(filePath, content, result);
55+
* const analysis = parser.analyze(filePath, content);
56+
* if (analysis.canParse) {
57+
* return parser.parse(filePath, content, analysis.hint);
5858
* }
5959
* ```
6060
*/
6161
export interface TranslationParser<Hint = true> {
62-
/**
63-
* Can this parser parse the given file?
64-
*
65-
* @deprecated Use `analyze()` instead
66-
*
67-
* @param filePath The absolute path to the translation file.
68-
* @param contents The contents of the translation file.
69-
* @returns A hint, which can be used in doing the actual parsing, if the file can be parsed by
70-
* this parser; false otherwise.
71-
*/
72-
canParse(filePath: string, contents: string): Hint|false;
73-
7462
/**
7563
* Analyze the file to see if this parser can parse the given file.
7664
*
@@ -89,22 +77,10 @@ export interface TranslationParser<Hint = true> {
8977
* @param filePath The absolute path to the translation file.
9078
* @param contents The contents of the translation file.
9179
* @param hint A value that can be used by the parser to speed up parsing of the file. This will
92-
* have been provided as the return result from calling `canParse()`.
80+
* have been provided as the return result from calling `analyze()`.
9381
* @returns The translation bundle parsed from the file.
9482
* @throws No errors. If there was a problem with parsing the bundle will contain errors
9583
* in the `diagnostics` property.
9684
*/
9785
parse(filePath: string, contents: string, hint: Hint): ParsedTranslationBundle;
98-
/**
99-
* Parses the given file, extracting the target locale and translations.
100-
*
101-
* @deprecated This overload is kept for backward compatibility. Going forward use the Hint
102-
* returned from `canParse()` so that this method can avoid duplicating effort.
103-
*
104-
* @param filePath The absolute path to the translation file.
105-
* @param contents The contents of the translation file.
106-
* @returns The translation bundle parsed from the file.
107-
* @throws An error if there was a problem parsing this file.
108-
*/
109-
parse(filePath: string, contents: string): ParsedTranslationBundle;
11086
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function getInnerRange(element: Element): LexerRange {
5959
}
6060

6161
/**
62-
* This "hint" object is used to pass information from `canParse()` to `parse()` for
62+
* This "hint" object is used to pass information from `analyze()` to `parse()` for
6363
* `TranslationParser`s that expect XML contents.
6464
*
6565
* This saves the `parse()` method from having to re-parse the XML.

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,13 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
2424
* @publicApi used by CLI
2525
*/
2626
export class Xliff1TranslationParser implements TranslationParser<XmlTranslationParserHint> {
27-
/**
28-
* @deprecated
29-
*/
30-
canParse(filePath: string, contents: string): XmlTranslationParserHint|false {
31-
const result = this.analyze(filePath, contents);
32-
return result.canParse && result.hint;
33-
}
34-
3527
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint> {
3628
return canParseXml(filePath, contents, 'xliff', {version: '1.2'});
3729
}
3830

39-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint):
31+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint):
4032
ParsedTranslationBundle {
41-
if (hint) {
42-
return this.extractBundle(hint);
43-
} else {
44-
return this.extractBundleDeprecated(filePath, contents);
45-
}
33+
return this.extractBundle(hint);
4634
}
4735

4836
private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle {
@@ -89,28 +77,6 @@ export class Xliff1TranslationParser implements TranslationParser<XmlTranslation
8977

9078
return bundle;
9179
}
92-
93-
private extractBundleDeprecated(filePath: string, contents: string) {
94-
const hint = this.canParse(filePath, contents);
95-
if (!hint) {
96-
throw new Error(`Unable to parse "${filePath}" as XLIFF 1.2 format.`);
97-
}
98-
const bundle = this.extractBundle(hint);
99-
if (bundle.diagnostics.hasErrors) {
100-
const message =
101-
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 1.2 format`);
102-
throw new Error(message);
103-
}
104-
return bundle;
105-
}
106-
}
107-
108-
class XliffFileElementVisitor extends BaseVisitor {
109-
override visitElement(fileElement: Element): any {
110-
if (fileElement.name === 'file') {
111-
return {fileElement, locale: getAttribute(fileElement, 'target-language')};
112-
}
113-
}
11480
}
11581

11682
class XliffTranslationVisitor extends BaseVisitor {

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,13 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
2323
* @publicApi used by CLI
2424
*/
2525
export class Xliff2TranslationParser implements TranslationParser<XmlTranslationParserHint> {
26-
/**
27-
* @deprecated
28-
*/
29-
canParse(filePath: string, contents: string): XmlTranslationParserHint|false {
30-
const result = this.analyze(filePath, contents);
31-
return result.canParse && result.hint;
32-
}
33-
3426
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint> {
3527
return canParseXml(filePath, contents, 'xliff', {version: '2.0'});
3628
}
3729

38-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint):
30+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint):
3931
ParsedTranslationBundle {
40-
if (hint) {
41-
return this.extractBundle(hint);
42-
} else {
43-
return this.extractBundleDeprecated(filePath, contents);
44-
}
32+
return this.extractBundle(hint);
4533
}
4634

4735
private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle {
@@ -67,20 +55,6 @@ export class Xliff2TranslationParser implements TranslationParser<XmlTranslation
6755
}
6856
return bundle;
6957
}
70-
71-
private extractBundleDeprecated(filePath: string, contents: string) {
72-
const hint = this.canParse(filePath, contents);
73-
if (!hint) {
74-
throw new Error(`Unable to parse "${filePath}" as XLIFF 2.0 format.`);
75-
}
76-
const bundle = this.extractBundle(hint);
77-
if (bundle.diagnostics.hasErrors) {
78-
const message =
79-
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 2.0 format`);
80-
throw new Error(message);
81-
}
82-
return bundle;
83-
}
8458
}
8559

8660

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
2525
* @publicApi used by CLI
2626
*/
2727
export class XtbTranslationParser implements TranslationParser<XmlTranslationParserHint> {
28-
/**
29-
* @deprecated
30-
*/
31-
canParse(filePath: string, contents: string): XmlTranslationParserHint|false {
32-
const result = this.analyze(filePath, contents);
33-
return result.canParse && result.hint;
34-
}
35-
3628
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint> {
3729
const extension = extname(filePath);
3830
if (extension !== '.xtb' && extension !== '.xmb') {
@@ -43,13 +35,9 @@ export class XtbTranslationParser implements TranslationParser<XmlTranslationPar
4335
return canParseXml(filePath, contents, 'translationbundle', {});
4436
}
4537

46-
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint):
38+
parse(filePath: string, contents: string, hint: XmlTranslationParserHint):
4739
ParsedTranslationBundle {
48-
if (hint) {
49-
return this.extractBundle(hint);
50-
} else {
51-
return this.extractBundleDeprecated(filePath, contents);
52-
}
40+
return this.extractBundle(hint);
5341
}
5442

5543
private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle {
@@ -65,20 +53,6 @@ export class XtbTranslationParser implements TranslationParser<XmlTranslationPar
6553
visitAll(bundleVisitor, element.children, bundle);
6654
return bundle;
6755
}
68-
69-
private extractBundleDeprecated(filePath: string, contents: string) {
70-
const hint = this.canParse(filePath, contents);
71-
if (!hint) {
72-
throw new Error(`Unable to parse "${filePath}" as XMB/XTB format.`);
73-
}
74-
const bundle = this.extractBundle(hint);
75-
if (bundle.diagnostics.hasErrors) {
76-
const message =
77-
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XMB/XTB format`);
78-
throw new Error(message);
79-
}
80-
return bundle;
81-
}
8256
}
8357

8458
class XtbVisitor extends BaseVisitor {

packages/localize/tools/test/translate/translation_files/translation_loader_spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ runInEachFileSystem(() => {
4141
jsonParser = new SimpleJsonTranslationParser();
4242
});
4343

44-
it('should call `canParse()` and `parse()` for each file', () => {
44+
it('should call `analyze()` and `parse()` for each file', () => {
4545
const diagnostics = new Diagnostics();
4646
const parser = new MockTranslationParser(alwaysCanParse, 'fr');
4747
const loader = new TranslationLoader(fs, [parser], 'error', diagnostics);
@@ -219,11 +219,6 @@ runInEachFileSystem(() => {
219219
private _canParse: (filePath: string) => boolean, private _locale?: string,
220220
private _translations: Record<string, ɵParsedTranslation> = {}) {}
221221

222-
canParse(filePath: string, fileContents: string) {
223-
const result = this.analyze(filePath, fileContents);
224-
return result.canParse && result.hint;
225-
}
226-
227222
analyze(filePath: string, fileContents: string): ParseAnalysis<true> {
228223
const diagnostics = new Diagnostics();
229224
diagnostics.warn('This is a mock failure warning.');

packages/localize/tools/test/translate/translation_files/translation_parsers/arb_translation_parser_spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {ɵmakeTemplateObject} from '@angular/localize';
9+
910
import {ArbTranslationParser} from '../../../../src/translate/translation_files/translation_parsers/arb_translation_parser';
1011

1112
describe('SimpleArbTranslationParser', () => {
12-
describe('canParse()', () => {
13+
describe('analyze()', () => {
1314
it('should return true if the file extension is `.json` and contains `@@locale` property',
1415
() => {
1516
const parser = new ArbTranslationParser();
16-
expect(parser.canParse('/some/file.xlf', '')).toBe(false);
17-
expect(parser.canParse('/some/file.json', 'xxx')).toBe(false);
18-
expect(parser.canParse('/some/file.json', '{ "someKey": "someValue" }')).toBe(false);
19-
expect(parser.canParse('/some/file.json', '{ "@@locale": "en", "someKey": "someValue" }'))
20-
.toBeTruthy();
17+
expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse();
18+
expect(parser.analyze('/some/file.json', 'xxx').canParse).toBeFalse();
19+
expect(parser.analyze('/some/file.json', '{ "someKey": "someValue" }').canParse)
20+
.toBeFalse();
21+
expect(parser.analyze('/some/file.json', '{ "@@locale": "en", "someKey": "someValue" }')
22+
.canParse)
23+
.toBeTrue();
2124
});
2225
});
2326

0 commit comments

Comments
 (0)