Skip to content

Commit

Permalink
refactor(localize): remove deprecated canParse method from Translat…
Browse files Browse the repository at this point in the history
…ionParsers

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.
  • Loading branch information
alan-agius4 committed Aug 26, 2022
1 parent 46f5021 commit 7b87c1c
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 197 deletions.
10 changes: 0 additions & 10 deletions goldens/public-api/localize/tools/index.md
Expand Up @@ -23,8 +23,6 @@ import { ɵSourceMessage } from '@angular/localize';
export class ArbTranslationParser implements TranslationParser<ArbJsonObject> {
// (undocumented)
analyze(_filePath: string, contents: string): ParseAnalysis<ArbJsonObject>;
// @deprecated (undocumented)
canParse(filePath: string, contents: string): ArbJsonObject | false;
// (undocumented)
parse(_filePath: string, contents: string, arb?: ArbJsonObject): ParsedTranslationBundle;
}
Expand Down Expand Up @@ -96,8 +94,6 @@ export class MessageExtractor {
export class SimpleJsonTranslationParser implements TranslationParser<SimpleJsonFile> {
// (undocumented)
analyze(filePath: string, contents: string): ParseAnalysis<SimpleJsonFile>;
// @deprecated (undocumented)
canParse(filePath: string, contents: string): SimpleJsonFile | false;
// (undocumented)
parse(_filePath: string, contents: string, json?: SimpleJsonFile): ParsedTranslationBundle;
}
Expand Down Expand Up @@ -131,8 +127,6 @@ export function unwrapSubstitutionsFromLocalizeCall(call: NodePath<t.CallExpress
export class Xliff1TranslationParser implements TranslationParser<XmlTranslationParserHint> {
// (undocumented)
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
// @deprecated (undocumented)
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
// (undocumented)
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
}
Expand All @@ -148,8 +142,6 @@ export class Xliff1TranslationSerializer implements TranslationSerializer {
export class Xliff2TranslationParser implements TranslationParser<XmlTranslationParserHint> {
// (undocumented)
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
// @deprecated (undocumented)
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
// (undocumented)
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
}
Expand All @@ -172,8 +164,6 @@ export class XmbTranslationSerializer implements TranslationSerializer {
export class XtbTranslationParser implements TranslationParser<XmlTranslationParserHint> {
// (undocumented)
analyze(filePath: string, contents: string): ParseAnalysis<XmlTranslationParserHint>;
// @deprecated (undocumented)
canParse(filePath: string, contents: string): XmlTranslationParserHint | false;
// (undocumented)
parse(filePath: string, contents: string, hint?: XmlTranslationParserHint): ParsedTranslationBundle;
}
Expand Down
Expand Up @@ -53,14 +53,6 @@ export interface ArbLocation {
* ```
*/
export class ArbTranslationParser implements TranslationParser<ArbJsonObject> {
/**
* @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<ArbJsonObject> {
const diagnostics = new Diagnostics();
if (!contents.includes('"@@locale"')) {
Expand Down
Expand Up @@ -34,14 +34,6 @@ interface SimpleJsonFile {
* @publicApi used by CLI
*/
export class SimpleJsonTranslationParser implements TranslationParser<SimpleJsonFile> {
/**
* @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<SimpleJsonFile> {
const diagnostics = new Diagnostics();
// For this to be parsable, the extension must be `.json` and the contents must include "locale"
Expand Down
Expand Up @@ -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 result = parser.analyze(filePath, content);
* if (analysis.canParse) {
* return parser.parse(filePath, content, analysis.hint);
* }
* ```
*/
export interface TranslationParser<Hint = true> {
/**
* 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.
*
Expand Down
Expand Up @@ -24,25 +24,17 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
* @publicApi used by CLI
*/
export class Xliff1TranslationParser implements TranslationParser<XmlTranslationParserHint> {
/**
* @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<XmlTranslationParserHint> {
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 {
Expand Down Expand Up @@ -89,28 +81,6 @@ export class Xliff1TranslationParser implements TranslationParser<XmlTranslation

return bundle;
}

private extractBundleDeprecated(filePath: string, contents: string) {
const hint = this.canParse(filePath, contents);
if (!hint) {
throw new Error(`Unable to parse "${filePath}" as XLIFF 1.2 format.`);
}
const bundle = this.extractBundle(hint);
if (bundle.diagnostics.hasErrors) {
const message =
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 1.2 format`);
throw new Error(message);
}
return bundle;
}
}

class XliffFileElementVisitor extends BaseVisitor {
override visitElement(fileElement: Element): any {
if (fileElement.name === 'file') {
return {fileElement, locale: getAttribute(fileElement, 'target-language')};
}
}
}

class XliffTranslationVisitor extends BaseVisitor {
Expand Down
Expand Up @@ -23,25 +23,17 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
* @publicApi used by CLI
*/
export class Xliff2TranslationParser implements TranslationParser<XmlTranslationParserHint> {
/**
* @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<XmlTranslationParserHint> {
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 {
Expand All @@ -67,20 +59,6 @@ export class Xliff2TranslationParser implements TranslationParser<XmlTranslation
}
return bundle;
}

private extractBundleDeprecated(filePath: string, contents: string) {
const hint = this.canParse(filePath, contents);
if (!hint) {
throw new Error(`Unable to parse "${filePath}" as XLIFF 2.0 format.`);
}
const bundle = this.extractBundle(hint);
if (bundle.diagnostics.hasErrors) {
const message =
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XLIFF 2.0 format`);
throw new Error(message);
}
return bundle;
}
}


Expand Down
Expand Up @@ -25,14 +25,6 @@ import {addErrorsToBundle, addParseDiagnostic, addParseError, canParseXml, getAt
* @publicApi used by CLI
*/
export class XtbTranslationParser implements TranslationParser<XmlTranslationParserHint> {
/**
* @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<XmlTranslationParserHint> {
const extension = extname(filePath);
if (extension !== '.xtb' && extension !== '.xmb') {
Expand All @@ -45,11 +37,11 @@ export class XtbTranslationParser implements TranslationParser<XmlTranslationPar

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 XMB/XTB format.`);
}

return this.extractBundle(hint);
}

private extractBundle({element, errors}: XmlTranslationParserHint): ParsedTranslationBundle {
Expand All @@ -65,20 +57,6 @@ export class XtbTranslationParser implements TranslationParser<XmlTranslationPar
visitAll(bundleVisitor, element.children, bundle);
return bundle;
}

private extractBundleDeprecated(filePath: string, contents: string) {
const hint = this.canParse(filePath, contents);
if (!hint) {
throw new Error(`Unable to parse "${filePath}" as XMB/XTB format.`);
}
const bundle = this.extractBundle(hint);
if (bundle.diagnostics.hasErrors) {
const message =
bundle.diagnostics.formatDiagnostics(`Failed to parse "${filePath}" as XMB/XTB format`);
throw new Error(message);
}
return bundle;
}
}

class XtbVisitor extends BaseVisitor {
Expand Down
Expand Up @@ -219,11 +219,6 @@ runInEachFileSystem(() => {
private _canParse: (filePath: string) => boolean, private _locale?: string,
private _translations: Record<string, ɵParsedTranslation> = {}) {}

canParse(filePath: string, fileContents: string) {
const result = this.analyze(filePath, fileContents);
return result.canParse && result.hint;
}

analyze(filePath: string, fileContents: string): ParseAnalysis<true> {
const diagnostics = new Diagnostics();
diagnostics.warn('This is a mock failure warning.');
Expand Down
Expand Up @@ -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();
});
});

Expand Down
Expand Up @@ -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();
});
});

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7b87c1c

Please sign in to comment.