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
This commit is contained in:
parent
ee35e223a7
commit
bf0c520f2e
|
@ -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});
|
||||
}
|
||||
|
|
|
@ -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<string, ɵParsedTranslation>,
|
||||
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),
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue