/
locale_plugin.ts
62 lines (59 loc) 路 2.43 KB
/
locale_plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {NodePath, PluginObj} from '@babel/core';
import {LogicalExpression, MemberExpression, stringLiteral} from '@babel/types';
import {TranslatePluginOptions, isLocalize} from './source_file_utils';
/**
* This Babel plugin will replace the following code forms with a string literal containing the
* given `locale`.
*
* * `$localize.locale` -> `"locale"`
* * `$localize && $localize.locale` -> `"locale"`
* * `xxx && $localize && $localize.locale` -> `"xxx && locale"`
* * `$localize.locale || default` -> `"locale" || default`
*
* @param locale The name of the locale to inline into the code.
* @param options Additional options including the name of the `$localize` function.
*/
export function makeLocalePlugin(
locale: string, {localizeName = '$localize'}: TranslatePluginOptions = {}): PluginObj {
return {
visitor: {
MemberExpression(expression: NodePath<MemberExpression>) {
const obj = expression.get('object');
if (!isLocalize(obj, localizeName)) {
return;
}
const property = expression.get('property') as NodePath;
if (!property.isIdentifier() || property.node.name !== 'locale') {
return;
}
debugger;
// Check for the `$localize.locale` being guarded by a check on the existence of
// `$localize`.
const parent = expression.parentPath;
if (parent.isLogicalExpression() && parent.node.operator === '&&' &&
parent.get('right') === expression) {
const left = parent.get('left');
if (isLocalize(left, localizeName)) {
// Replace `$localize && $localize.locale` with `$localize.locale`
parent.replaceWith(expression);
} else if (
left.isLogicalExpression() && left.node.operator === '&&' &&
isLocalize(left.get('right'), localizeName)) {
// The `$localize` is part of a preceding logical AND.
// Replace XXX && $localize && $localize.locale` with `XXX && $localize.locale`
left.replaceWith(left.get('left'));
}
}
// Replace the `$localize.locale` with the string literal
expression.replaceWith(stringLiteral(locale));
}
}
};
}