Skip to content

Commit

Permalink
fixup! feat(ivy): i18n - inline current locale at compile-time
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Oct 24, 2019
1 parent f3197bf commit 8e8c140
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {NodePath, PluginObj} from '@babel/core';
import {LogicalExpression, MemberExpression, stringLiteral} from '@babel/types';
import {MemberExpression, stringLiteral} from '@babel/types';

import {TranslatePluginOptions, isLocalize} from './source_file_utils';

Expand Down Expand Up @@ -35,6 +35,10 @@ export function makeLocalePlugin(
if (!property.isIdentifier({name: 'locale'})) {
return;
}
if (expression.parentPath.isAssignmentExpression() &&
expression.parentPath.get('left') === expression) {
return;
}
// Check for the `$localize.locale` being guarded by a check on the existence of
// `$localize`.
const parent = expression.parentPath;
Expand All @@ -53,7 +57,6 @@ export function makeLocalePlugin(
left.replaceWith(left.get('left'));
}
}

// Replace the `$localize.locale` with the string literal
expression.replaceWith(stringLiteral(locale));
}
Expand Down
Expand Up @@ -83,4 +83,16 @@ describe('makeLocalePlugin', () => {
const output = transformSync(input, {plugins: [makeLocalePlugin('fr')]}) !;
expect(output.code).toEqual('const {\n locale\n} = $localize;\nconst a = locale;');
});

it('should ignore `$localize.locale` on LHS of an assignment', () => {
const input = 'let a;\na = $localize.locale = "de";';
const output = transformSync(input, {plugins: [makeLocalePlugin('fr')]}) !;
expect(output.code).toEqual('let a;\na = $localize.locale = "de";');
});

it('should handle `$localize.locale on RHS of an assignment', () => {
const input = 'let a;\na = $localize.locale;';
const output = transformSync(input, {plugins: [makeLocalePlugin('fr')]}) !;
expect(output.code).toEqual('let a;\na = "fr";');
});
});

0 comments on commit 8e8c140

Please sign in to comment.