Skip to content

Commit

Permalink
refactor(ivy): i18n - create and use isLocalize() helper (#33314)
Browse files Browse the repository at this point in the history
PR Close #33314
  • Loading branch information
petebacondarwin authored and AndrewKushnir committed Oct 24, 2019
1 parent fde8363 commit f17072c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Expand Up @@ -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>,
Expand All @@ -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,
Expand Down
Expand Up @@ -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>,
Expand All @@ -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 =
Expand Down
Expand Up @@ -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> {
Expand Down

0 comments on commit f17072c

Please sign in to comment.