refactor(ivy): i18n - create and use `isLocalize()` helper (#33314)

PR Close #33314
This commit is contained in:
Pete Bacon Darwin 2019-10-23 22:49:02 +01:00 committed by Andrew Kushnir
parent fde8363e0d
commit f17072c7af
3 changed files with 18 additions and 5 deletions

View File

@ -9,7 +9,7 @@ import {ɵParsedTranslation} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {TaggedTemplateExpression} from '@babel/types';
import {Diagnostics} from '../../diagnostics';
import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isGlobalIdentifier, isNamedIdentifier, translate, unwrapMessagePartsFromTemplateLiteral} from './source_file_utils';
import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, unwrapMessagePartsFromTemplateLiteral} from './source_file_utils';
export function makeEs2015TranslatePlugin(
diagnostics: Diagnostics, translations: Record<string, ɵParsedTranslation>,
@ -20,7 +20,7 @@ export function makeEs2015TranslatePlugin(
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) {
try {
const tag = path.get('tag');
if (isNamedIdentifier(tag, localizeName) && isGlobalIdentifier(tag)) {
if (isLocalize(tag, localizeName)) {
const messageParts = unwrapMessagePartsFromTemplateLiteral(path.node.quasi.quasis);
const translated = translate(
diagnostics, translations, messageParts, path.node.quasi.expressions,

View File

@ -9,7 +9,7 @@ import {ɵParsedTranslation} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {CallExpression} from '@babel/types';
import {Diagnostics} from '../../diagnostics';
import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isGlobalIdentifier, isNamedIdentifier, translate, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from './source_file_utils';
import {TranslatePluginOptions, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from './source_file_utils';
export function makeEs5TranslatePlugin(
diagnostics: Diagnostics, translations: Record<string, ɵParsedTranslation>,
@ -20,7 +20,7 @@ export function makeEs5TranslatePlugin(
CallExpression(callPath: NodePath<CallExpression>) {
try {
const calleePath = callPath.get('callee');
if (isNamedIdentifier(calleePath, localizeName) && isGlobalIdentifier(calleePath)) {
if (isLocalize(calleePath, localizeName)) {
const messageParts = unwrapMessagePartsFromLocalizeCall(callPath);
const expressions = unwrapSubstitutionsFromLocalizeCall(callPath.node);
const translated =

View File

@ -11,8 +11,21 @@ import * as t from '@babel/types';
import {Diagnostics} from '../../diagnostics';
/**
* Is the given `expression` an identifier with the correct name
* Is the given `expression` the global `$localize` identifier?
*
* @param expression The expression to check.
* @param localizeName The configured name of `$localize`.
*/
export function isLocalize(
expression: NodePath, localizeName: string): expression is NodePath<t.Identifier> {
return isNamedIdentifier(expression, localizeName) && isGlobalIdentifier(expression);
}
/**
* Is the given `expression` an identifier with the correct `name`?
*
* @param expression The expression to check.
* @param name The name of the identifier we are looking for.
*/
export function isNamedIdentifier(
expression: NodePath, name: string): expression is NodePath<t.Identifier> {