diff --git a/goldens/public-api/localize/tools/index.md b/goldens/public-api/localize/tools/index.md index abfb7df8897a3..15b2c0bebe65b 100644 --- a/goldens/public-api/localize/tools/index.md +++ b/goldens/public-api/localize/tools/index.md @@ -23,8 +23,6 @@ import { ɵSourceMessage } from '@angular/localize'; export class ArbTranslationParser implements TranslationParser { // (undocumented) analyze(_filePath: string, contents: string): ParseAnalysis; - // @deprecated (undocumented) - canParse(filePath: string, contents: string): ArbJsonObject | false; // (undocumented) parse(_filePath: string, contents: string, arb?: ArbJsonObject): ParsedTranslationBundle; } @@ -96,8 +94,6 @@ export class MessageExtractor { export class SimpleJsonTranslationParser implements TranslationParser { // (undocumented) analyze(filePath: string, contents: string): ParseAnalysis; - // @deprecated (undocumented) - canParse(filePath: string, contents: string): SimpleJsonFile | false; // (undocumented) parse(_filePath: string, contents: string, json?: SimpleJsonFile): ParsedTranslationBundle; } @@ -131,8 +127,6 @@ export function unwrapSubstitutionsFromLocalizeCall(call: NodePath { // (undocumented) analyze(filePath: string, contents: string): ParseAnalysis; - // @deprecated (undocumented) - canParse(filePath: string, contents: string): XmlTranslationParserHint | false; // (undocumented) parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle; } @@ -148,8 +142,6 @@ export class Xliff1TranslationSerializer implements TranslationSerializer { export class Xliff2TranslationParser implements TranslationParser { // (undocumented) analyze(filePath: string, contents: string): ParseAnalysis; - // @deprecated (undocumented) - canParse(filePath: string, contents: string): XmlTranslationParserHint | false; // (undocumented) parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle; } @@ -172,8 +164,6 @@ export class XmbTranslationSerializer implements TranslationSerializer { export class XtbTranslationParser implements TranslationParser { // (undocumented) analyze(filePath: string, contents: string): ParseAnalysis; - // @deprecated (undocumented) - canParse(filePath: string, contents: string): XmlTranslationParserHint | false; // (undocumented) parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle; } diff --git a/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.ts b/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.ts index 036030a6642e6..a85691d3fed70 100644 --- a/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.ts +++ b/packages/localize/tools/src/translate/translation_files/translation_parsers/arb_translation_parser.ts @@ -53,14 +53,6 @@ export interface ArbLocation { * ``` */ export class ArbTranslationParser implements TranslationParser { - /** - * @deprecated - */ - canParse(filePath: string, contents: string): ArbJsonObject|false { - const result = this.analyze(filePath, contents); - return result.canParse && result.hint; - } - analyze(_filePath: string, contents: string): ParseAnalysis { const diagnostics = new Diagnostics(); if (!contents.includes('"@@locale"')) { diff --git a/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts b/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts index 47dbfd9e1084a..ad5acced7e687 100644 --- a/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts +++ b/packages/localize/tools/src/translate/translation_files/translation_parsers/simple_json_translation_parser.ts @@ -34,14 +34,6 @@ interface SimpleJsonFile { * @publicApi used by CLI */ export class SimpleJsonTranslationParser implements TranslationParser { - /** - * @deprecated - */ - canParse(filePath: string, contents: string): SimpleJsonFile|false { - const result = this.analyze(filePath, contents); - return result.canParse && result.hint; - } - analyze(filePath: string, contents: string): ParseAnalysis { const diagnostics = new Diagnostics(); // For this to be parsable, the extension must be `.json` and the contents must include "locale" diff --git a/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parser.ts b/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parser.ts index 22145ebf33d50..76480d40608d7 100644 --- a/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parser.ts +++ b/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_parser.ts @@ -44,33 +44,21 @@ export interface ParsedTranslationBundle { /** * Implement this interface to provide a class that can parse the contents of a translation file. * - * The `canParse()` method can return a hint that can be used by the `parse()` method to speed up - * parsing. This allows the parser to do significant work to determine if the file can be parsed + * The `parser.analyze()` method can return a hint that can be used by the `parse()` method to speed + * up parsing. This allows the parser to do significant work to determine if the file can be parsed * without duplicating the work when it comes to actually parsing the file. * * Example usage: * * ``` * const parser: TranslationParser = getParser(); - * const result = parser.canParse(filePath, content); - * if (result) { - * return parser.parse(filePath, content, result); + * const analysis = parser.analyze(filePath, content); + * if (analysis.canParse) { + * return parser.parse(filePath, content, analysis.hint); * } * ``` */ export interface TranslationParser { - /** - * Can this parser parse the given file? - * - * @deprecated Use `analyze()` instead - * - * @param filePath The absolute path to the translation file. - * @param contents The contents of the translation file. - * @returns A hint, which can be used in doing the actual parsing, if the file can be parsed by - * this parser; false otherwise. - */ - canParse(filePath: string, contents: string): Hint|false; - /** * Analyze the file to see if this parser can parse the given file. * @@ -89,7 +77,7 @@ export interface TranslationParser { * @param filePath The absolute path to the translation file. * @param contents The contents of the translation file. * @param hint A value that can be used by the parser to speed up parsing of the file. This will - * have been provided as the return result from calling `canParse()`. + * have been provided as the return result from calling `analyze()`. * @returns The translation bundle parsed from the file. * @throws No errors. If there was a problem with parsing the bundle will contain errors * in the `diagnostics` property. @@ -99,7 +87,7 @@ export interface TranslationParser { * Parses the given file, extracting the target locale and translations. * * @deprecated This overload is kept for backward compatibility. Going forward use the Hint - * returned from `canParse()` so that this method can avoid duplicating effort. + * returned from `analyze()` so that this method can avoid duplicating effort. * * @param filePath The absolute path to the translation file. * @param contents The contents of the translation file. diff --git a/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.ts b/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.ts index c310da8459a7d..b9cb20424ba69 100644 --- a/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.ts +++ b/packages/localize/tools/src/translate/translation_files/translation_parsers/translation_utils.ts @@ -59,7 +59,7 @@ function getInnerRange(element: Element): LexerRange { } /** - * This "hint" object is used to pass information from `canParse()` to `parse()` for + * This "hint" object is used to pass information from `analyze()` to `parse()` for * `TranslationParser`s that expect XML contents. * * This saves the `parse()` method from having to re-parse the XML. diff --git a/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.ts b/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.ts index c70a56578aff5..d2e87f424b9a9 100644 --- a/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.ts +++ b/packages/localize/tools/src/translate/translation_files/translation_parsers/xliff1_translation_parser.ts @@ -24,25 +24,17 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt * @publicApi used by CLI */ export class Xliff1TranslationParser implements TranslationParser { - /** - * @deprecated - */ - canParse(filePath: string, contents: string): XmlTranslationParserHint|false { - const result = this.analyze(filePath, contents); - return result.canParse && result.hint; - } - analyze(filePath: string, contents: string): ParseAnalysis { return canParseXml(filePath, contents, 'xliff', {version: '1.2'}); } parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle { - if (hint) { - return this.extractBundle(hint); - } else { - return this.extractBundleDeprecated(filePath, contents); + if (!hint) { + throw new Error(`Unable to parse "${filePath}" as XLIFF 1.2 format.`); } + + return this.extractBundle(hint); } private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle { @@ -89,28 +81,6 @@ export class Xliff1TranslationParser implements TranslationParser { - /** - * @deprecated - */ - canParse(filePath: string, contents: string): XmlTranslationParserHint|false { - const result = this.analyze(filePath, contents); - return result.canParse && result.hint; - } - analyze(filePath: string, contents: string): ParseAnalysis { return canParseXml(filePath, contents, 'xliff', {version: '2.0'}); } parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle { - if (hint) { - return this.extractBundle(hint); - } else { - return this.extractBundleDeprecated(filePath, contents); + if (!hint) { + throw new Error(`Unable to parse "${filePath}" as XLIFF 2.0 format.`); } + + return this.extractBundle(hint); } private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle { @@ -67,20 +59,6 @@ export class Xliff2TranslationParser implements TranslationParser { - /** - * @deprecated - */ - canParse(filePath: string, contents: string): XmlTranslationParserHint|false { - const result = this.analyze(filePath, contents); - return result.canParse && result.hint; - } - analyze(filePath: string, contents: string): ParseAnalysis { const extension = extname(filePath); if (extension !== '.xtb' && extension !== '.xmb') { @@ -45,11 +37,11 @@ export class XtbTranslationParser implements TranslationParser { jsonParser = new SimpleJsonTranslationParser(); }); - it('should call `canParse()` and `parse()` for each file', () => { + it('should call `analyze()` and `parse()` for each file', () => { const diagnostics = new Diagnostics(); const parser = new MockTranslationParser(alwaysCanParse, 'fr'); const loader = new TranslationLoader(fs, [parser], 'error', diagnostics); @@ -219,11 +219,6 @@ runInEachFileSystem(() => { private _canParse: (filePath: string) => boolean, private _locale?: string, private _translations: Record = {}) {} - canParse(filePath: string, fileContents: string) { - const result = this.analyze(filePath, fileContents); - return result.canParse && result.hint; - } - analyze(filePath: string, fileContents: string): ParseAnalysis { const diagnostics = new Diagnostics(); diagnostics.warn('This is a mock failure warning.'); diff --git a/packages/localize/tools/test/translate/translation_files/translation_parsers/arb_translation_parser_spec.ts b/packages/localize/tools/test/translate/translation_files/translation_parsers/arb_translation_parser_spec.ts index 54778105a95f1..649ce4f2fe66a 100644 --- a/packages/localize/tools/test/translate/translation_files/translation_parsers/arb_translation_parser_spec.ts +++ b/packages/localize/tools/test/translate/translation_files/translation_parsers/arb_translation_parser_spec.ts @@ -6,18 +6,21 @@ * found in the LICENSE file at https://angular.io/license */ import {ɵmakeTemplateObject} from '@angular/localize'; + import {ArbTranslationParser} from '../../../../src/translate/translation_files/translation_parsers/arb_translation_parser'; describe('SimpleArbTranslationParser', () => { - describe('canParse()', () => { + describe('analyze()', () => { it('should return true if the file extension is `.json` and contains `@@locale` property', () => { const parser = new ArbTranslationParser(); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.json', 'xxx')).toBe(false); - expect(parser.canParse('/some/file.json', '{ "someKey": "someValue" }')).toBe(false); - expect(parser.canParse('/some/file.json', '{ "@@locale": "en", "someKey": "someValue" }')) - .toBeTruthy(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', 'xxx').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '{ "someKey": "someValue" }').canParse) + .toBeFalse(); + expect(parser.analyze('/some/file.json', '{ "@@locale": "en", "someKey": "someValue" }') + .canParse) + .toBeTrue(); }); }); diff --git a/packages/localize/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts b/packages/localize/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts index 20567a5465ef8..8f22f6cbf7bd6 100644 --- a/packages/localize/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts +++ b/packages/localize/tools/test/translate/translation_files/translation_parsers/simple_json_spec.ts @@ -6,20 +6,22 @@ * found in the LICENSE file at https://angular.io/license */ import {ɵmakeTemplateObject} from '@angular/localize'; + import {SimpleJsonTranslationParser} from '../../../../src/translate/translation_files/translation_parsers/simple_json_translation_parser'; import {ParsedTranslationBundle} from '../../../../src/translate/translation_files/translation_parsers/translation_parser'; describe('SimpleJsonTranslationParser', () => { - describe('canParse()', () => { + describe('analyze()', () => { it('should return true if the file extension is `.json` and contains top level `locale` and `translations` properties', () => { const parser = new SimpleJsonTranslationParser(); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.json', '{}')).toBe(false); - expect(parser.canParse('/some/file.json', '{ "translations" : {} }')).toBe(false); - expect(parser.canParse('/some/file.json', '{ "locale" : "fr" }')).toBe(false); - expect(parser.canParse('/some/file.json', '{ "locale" : "fr", "translations" : {}}')) - .toBeTruthy(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '{}').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '{ "translations" : {} }').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '{ "locale" : "fr" }').canParse).toBeFalse(); + expect( + parser.analyze('/some/file.json', '{ "locale" : "fr", "translations" : {}}').canParse) + .toBeTrue(); }); }); @@ -51,11 +53,12 @@ describe('SimpleJsonTranslationParser', () => { const doParse: (fileName: string, contents: string) => ParsedTranslationBundle = withHint ? (fileName, contents) => { const parser = new SimpleJsonTranslationParser(); - const hint = parser.canParse(fileName, contents); - if (!hint) { + const analysis = parser.analyze(fileName, contents); + if (!analysis.canParse) { throw new Error('expected contents to be valid'); } - return parser.parse(fileName, contents, hint); + + return parser.parse(fileName, contents, analysis.hint); } : (fileName, contents) => { const parser = new SimpleJsonTranslationParser(); return parser.parse(fileName, contents); diff --git a/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff1_translation_parser_spec.ts b/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff1_translation_parser_spec.ts index 4eea42a389cb5..ac8d61c049148 100644 --- a/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff1_translation_parser_spec.ts +++ b/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff1_translation_parser_spec.ts @@ -6,29 +6,35 @@ * found in the LICENSE file at https://angular.io/license */ import {ɵcomputeMsgId, ɵmakeParsedTranslation} from '@angular/localize'; + import {ParseAnalysis, ParsedTranslationBundle} from '../../../../src/translate/translation_files/translation_parsers/translation_parser'; import {Xliff1TranslationParser} from '../../../../src/translate/translation_files/translation_parsers/xliff1_translation_parser'; describe( 'Xliff1TranslationParser', () => { - describe('canParse()', () => { + describe('analyze()', () => { it('should return true only if the file contains an element with version="1.2" attribute', () => { const parser = new Xliff1TranslationParser(); - expect(parser.canParse( - '/some/file.xlf', - '')) - .toBeTruthy(); - expect(parser.canParse( - '/some/file.json', - '')) - .toBeTruthy(); - expect(parser.canParse('/some/file.xliff', '')).toBeTruthy(); - expect(parser.canParse('/some/file.json', '')).toBeTruthy(); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.json', '')).toBe(false); + expect(parser + .analyze( + '/some/file.xlf', + '') + .canParse) + .toBeTrue(); + expect(parser + .analyze( + '/some/file.json', + '') + .canParse) + .toBeTrue(); + expect(parser.analyze('/some/file.xliff', '').canParse) + .toBeTrue(); + expect(parser.analyze('/some/file.json', '').canParse).toBeTrue(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '').canParse).toBeFalse(); }); }); @@ -92,11 +98,11 @@ describe( const doParse: (fileName: string, XLIFF: string) => ParsedTranslationBundle = withHint ? (fileName, XLIFF) => { const parser = new Xliff1TranslationParser(); - const hint = parser.canParse(fileName, XLIFF); - if (!hint) { + const analysis = parser.analyze(fileName, XLIFF); + if (!analysis.canParse) { throw new Error('expected XLIFF to be valid'); } - return parser.parse(fileName, XLIFF, hint); + return parser.parse(fileName, XLIFF, analysis.hint); } : (fileName, XLIFF) => { const parser = new Xliff1TranslationParser(); return parser.parse(fileName, XLIFF); diff --git a/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff2_translation_parser_spec.ts b/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff2_translation_parser_spec.ts index bb9000d09bd89..9d8924de3ea85 100644 --- a/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff2_translation_parser_spec.ts +++ b/packages/localize/tools/test/translate/translation_files/translation_parsers/xliff2_translation_parser_spec.ts @@ -6,28 +6,33 @@ * found in the LICENSE file at https://angular.io/license */ import {ɵcomputeMsgId, ɵmakeParsedTranslation} from '@angular/localize'; + import {ParseAnalysis, ParsedTranslationBundle} from '../../../../src/translate/translation_files/translation_parsers/translation_parser'; import {Xliff2TranslationParser} from '../../../../src/translate/translation_files/translation_parsers/xliff2_translation_parser'; describe('Xliff2TranslationParser', () => { - describe('canParse()', () => { + describe('analyze()', () => { it('should return true if the file contains an element with version="2.0" attribute', () => { const parser = new Xliff2TranslationParser(); - expect(parser.canParse( - '/some/file.xlf', - '')) - .toBeTruthy(); - expect(parser.canParse( - '/some/file.json', - '')) - .toBeTruthy(); - expect(parser.canParse('/some/file.xliff', '')).toBeTruthy(); - expect(parser.canParse('/some/file.json', '')).toBeTruthy(); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.xlf', '')).toBe(false); - expect(parser.canParse('/some/file.json', '')).toBe(false); + expect(parser + .analyze( + '/some/file.xlf', + '') + .canParse) + .toBeTrue(); + expect(parser + .analyze( + '/some/file.json', + '') + .canParse) + .toBeTrue(); + expect(parser.analyze('/some/file.xliff', '').canParse).toBeTrue(); + expect(parser.analyze('/some/file.json', '').canParse).toBeTrue(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xlf', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.json', '').canParse).toBeFalse(); }); }); @@ -95,11 +100,11 @@ describe('Xliff2TranslationParser', () => { const doParse: (fileName: string, XLIFF: string) => ParsedTranslationBundle = withHint ? (fileName, XLIFF) => { const parser = new Xliff2TranslationParser(); - const hint = parser.canParse(fileName, XLIFF); - if (!hint) { + const analysis = parser.analyze(fileName, XLIFF); + if (!analysis.canParse) { throw new Error('expected XLIFF to be valid'); } - return parser.parse(fileName, XLIFF, hint); + return parser.parse(fileName, XLIFF, analysis.hint); } : (fileName, XLIFF) => { const parser = new Xliff2TranslationParser(); return parser.parse(fileName, XLIFF); diff --git a/packages/localize/tools/test/translate/translation_files/translation_parsers/xtb_translation_parser_spec.ts b/packages/localize/tools/test/translate/translation_files/translation_parsers/xtb_translation_parser_spec.ts index bc91e87d8e80b..e1d214fc4882b 100644 --- a/packages/localize/tools/test/translate/translation_files/translation_parsers/xtb_translation_parser_spec.ts +++ b/packages/localize/tools/test/translate/translation_files/translation_parsers/xtb_translation_parser_spec.ts @@ -6,21 +6,24 @@ * found in the LICENSE file at https://angular.io/license */ import {ɵcomputeMsgId, ɵmakeParsedTranslation} from '@angular/localize'; + import {ParseAnalysis, ParsedTranslationBundle} from '../../../../src/translate/translation_files/translation_parsers/translation_parser'; import {XtbTranslationParser} from '../../../../src/translate/translation_files/translation_parsers/xtb_translation_parser'; describe('XtbTranslationParser', () => { - describe('canParse()', () => { + describe('analyze()', () => { it('should return true if the file extension is `.xtb` or `.xmb` and it contains the `` tag', () => { const parser = new XtbTranslationParser(); - expect(parser.canParse('/some/file.xtb', '')).toBeTruthy(); - expect(parser.canParse('/some/file.xmb', '')).toBeTruthy(); - expect(parser.canParse('/some/file.xtb', '')).toBeTruthy(); - expect(parser.canParse('/some/file.xmb', '')).toBeTruthy(); - expect(parser.canParse('/some/file.json', '')).toBe(false); - expect(parser.canParse('/some/file.xmb', '')).toBe(false); - expect(parser.canParse('/some/file.xtb', '')).toBe(false); + expect(parser.analyze('/some/file.xtb', '').canParse).toBeTrue(); + expect(parser.analyze('/some/file.xmb', '').canParse).toBeTrue(); + expect(parser.analyze('/some/file.xtb', '').canParse) + .toBeTrue(); + expect(parser.analyze('/some/file.xmb', '').canParse) + .toBeTrue(); + expect(parser.analyze('/some/file.json', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xmb', '').canParse).toBeFalse(); + expect(parser.analyze('/some/file.xtb', '').canParse).toBeFalse(); }); }); @@ -73,11 +76,11 @@ describe('XtbTranslationParser', () => { const doParse: (fileName: string, XTB: string) => ParsedTranslationBundle = withHint ? (fileName, XTB) => { const parser = new XtbTranslationParser(); - const hint = parser.canParse(fileName, XTB); - if (!hint) { + const analysis = parser.analyze(fileName, XTB); + if (!analysis.canParse) { throw new Error('expected XTB to be valid'); } - return parser.parse(fileName, XTB, hint); + return parser.parse(fileName, XTB, analysis.hint); } : (fileName, XTB) => { const parser = new XtbTranslationParser(); return parser.parse(fileName, XTB);