From bf0c520f2e9802ab43b18a41eaa8e6820787374d Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 28 Apr 2020 19:33:12 +0100 Subject: [PATCH] refactor(localize): simplify adding condition diagnostics (#36792) Previously the `missingTranslation` option had to be checked before calling `warn` or `error` on the `diagnostics` object. Now this boilerplate is hidden inside the `Diagnostics.add()` method, which will open it up to being used for other conditional diagnostics. PR Close #36792 --- packages/localize/src/tools/src/diagnostics.ts | 10 ++++++++++ .../src/tools/src/source_file_utils.ts | 18 +++++------------- .../localize/src/tools/src/translate/main.ts | 4 ++-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/localize/src/tools/src/diagnostics.ts b/packages/localize/src/tools/src/diagnostics.ts index 73f2945b68..695374f1f9 100644 --- a/packages/localize/src/tools/src/diagnostics.ts +++ b/packages/localize/src/tools/src/diagnostics.ts @@ -6,6 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ +/** + * How to handle potential diagnostics. + */ +export type DiagnosticHandlingStrategy = 'error'|'warning'|'ignore'; + /** * This class is used to collect and then report warnings and errors that occur during the execution * of the tools. @@ -15,6 +20,11 @@ export class Diagnostics { get hasErrors() { return this.messages.some(m => m.type === 'error'); } + add(type: DiagnosticHandlingStrategy, message: string) { + if (type !== 'ignore') { + this.messages.push({type, message}); + } + } warn(message: string) { this.messages.push({type: 'warning', message}); } diff --git a/packages/localize/src/tools/src/source_file_utils.ts b/packages/localize/src/tools/src/source_file_utils.ts index d906a03468..62cb32f9ee 100644 --- a/packages/localize/src/tools/src/source_file_utils.ts +++ b/packages/localize/src/tools/src/source_file_utils.ts @@ -8,7 +8,8 @@ import {ɵisMissingTranslationError, ɵmakeTemplateObject, ɵParsedTranslation, ɵSourceLocation, ɵtranslate} from '@angular/localize'; import {NodePath} from '@babel/traverse'; import * as t from '@babel/types'; -import {Diagnostics} from './diagnostics'; + +import {DiagnosticHandlingStrategy, Diagnostics} from './diagnostics'; /** * Is the given `expression` the global `$localize` identifier? @@ -299,15 +300,10 @@ export function isArrayOfExpressions(nodes: t.Node[]): nodes is t.Expression[] { /** Options that affect how the `makeEsXXXTranslatePlugin()` functions work. */ export interface TranslatePluginOptions { - missingTranslation?: MissingTranslationStrategy; + missingTranslation?: DiagnosticHandlingStrategy; localizeName?: string; } -/** - * How to handle missing translations. - */ -export type MissingTranslationStrategy = 'error'|'warning'|'ignore'; - /** * Translate the text of the given message, using the given translations. * @@ -316,16 +312,12 @@ export type MissingTranslationStrategy = 'error'|'warning'|'ignore'; export function translate( diagnostics: Diagnostics, translations: Record, messageParts: TemplateStringsArray, substitutions: readonly any[], - missingTranslation: MissingTranslationStrategy): [TemplateStringsArray, readonly any[]] { + missingTranslation: DiagnosticHandlingStrategy): [TemplateStringsArray, readonly any[]] { try { return ɵtranslate(translations, messageParts, substitutions); } catch (e) { if (ɵisMissingTranslationError(e)) { - if (missingTranslation === 'error') { - diagnostics.error(e.message); - } else if (missingTranslation === 'warning') { - diagnostics.warn(e.message); - } + diagnostics.add(missingTranslation, e.message); // Return the parsed message because this will have the meta blocks stripped return [ ɵmakeTemplateObject(e.parsedMessage.messageParts, e.parsedMessage.messageParts), diff --git a/packages/localize/src/tools/src/translate/main.ts b/packages/localize/src/tools/src/translate/main.ts index 5139a1076a..55fa300e62 100644 --- a/packages/localize/src/tools/src/translate/main.ts +++ b/packages/localize/src/tools/src/translate/main.ts @@ -84,7 +84,7 @@ if (require.main === module) { const translationFilePaths: string[] = options['t']; const outputPathFn = getOutputPathFn(options['o']); const diagnostics = new Diagnostics(); - const missingTranslation: MissingTranslationStrategy = options['m']; + const missingTranslation: DiagnosticHandlingStrategy = options['m']; const sourceLocale: string|undefined = options['l']; const translationFileLocales: string[] = options['target-locales'] || []; @@ -134,7 +134,7 @@ export interface TranslateFilesOptions { /** * How to handle missing translations. */ - missingTranslation: MissingTranslationStrategy; + missingTranslation: DiagnosticHandlingStrategy; /** * The locale of the source files. * If this is provided then a copy of the application will be created with no translation but just