-
Notifications
You must be signed in to change notification settings - Fork 0
/
localization-util.js
185 lines (179 loc) · 5.05 KB
/
localization-util.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const esprima = require('esprima');
const {
AssignmentExpression,
AssignmentPattern,
FunctionExpression,
ExpressionStatement,
BlockStatement,
CallExpression,
IfStatement,
BinaryExpression,
StaticMemberExpression,
ComputedMemberExpression,
Identifier,
ForInStatement,
MemberExpression,
ClassDeclaration,
ClassBody,
VariableDeclaration,
VariableDeclarator,
ThisExpression,
ArrowFunctionExpression,
LogicalExpression,
ReturnStatement,
ObjectExpression,
Property,
SpreadElement,
Literal,
ArrayExpression,
ForStatement,
UpdateExpression,
NewExpression,
UnaryExpression,
FunctionDeclaration,
TemplateLiteral,
ConditionalExpression,
SwitchStatement,
SwitchCase,
TryStatement,
CatchClause,
RestElement,
ObjectPattern,
BreakStatement,
ForOfStatement,
AwaitExpression,
WhileStatement,
ThrowStatement,
SequenceExpression,
ContinueStatement,
ArrayPattern
} = esprima.Syntax;
function isGettextCall(callExpression) {
return callExpression.callee.type === Identifier &&
callExpression.callee.name === GETTEXT_FUNCTION_NAME;
}
const GETTEXT_FUNCTION_NAME = '__';
function findTranslationCalls(expression) {
if (expression === null) {
return [];
}
switch (expression.type) {
case ExpressionStatement:
return findTranslationCalls(expression.expression);
case VariableDeclaration:
return expression.declarations
.map(findTranslationCalls)
.flat();
case VariableDeclarator:
return findTranslationCalls(expression.init);
case AssignmentPattern:
case AssignmentExpression:
return findTranslationCalls(expression.right);
case ArrowFunctionExpression:
case FunctionDeclaration:
case FunctionExpression:
return expression.params
.map(findTranslationCalls)
.concat(findTranslationCalls(expression.body))
.flat();
case ClassBody:
case BlockStatement:
return expression.body
.map(findTranslationCalls)
.flat();
case NewExpression:
return findTranslationCalls(expression.callee)
.concat(expression.arguments.map(findTranslationCalls))
.flat();
case CallExpression:
if (isGettextCall(expression)) {
if (expression.arguments.length !== 1) {
throw new Error(`Invalid call to gettext: expected exactly one argument: ${expression}`);
}
return [expression];
} else {
return findTranslationCalls(expression.callee)
.concat(expression.arguments.map(findTranslationCalls))
.flat();
}
case WhileStatement:
return [expression.test, expression.body]
.map(findTranslationCalls)
.flat();
case ConditionalExpression:
case IfStatement:
return [expression.test, expression.consequent, expression.alternate]
.map(findTranslationCalls)
.flat();
case ForStatement:
return [expression.init, expression.test, expression.update, expression.body]
.map(findTranslationCalls)
.flat();
case ForOfStatement:
case ForInStatement:
return [expression.right, expression.body]
.map(findTranslationCalls)
.flat();
case SwitchStatement:
return expression.cases
.map(findTranslationCalls)
.concat(findTranslationCalls(expression.discriminant))
.flat();
case SwitchCase:
return expression.consequent
.map(findTranslationCalls)
.flat();
case ClassDeclaration:
return findTranslationCalls(expression.body);
case LogicalExpression:
case BinaryExpression:
return [expression.left, expression.right]
.map(findTranslationCalls)
.flat();
case ObjectPattern:
case ObjectExpression:
return expression.properties
.map(findTranslationCalls)
.flat();
case Property:
return findTranslationCalls(expression.value);
case ThrowStatement:
case ReturnStatement:
case AwaitExpression:
case RestElement:
case SpreadElement:
return findTranslationCalls(expression.argument);
case ArrayExpression:
return expression.elements
.map(findTranslationCalls)
.flat();
case TryStatement:
case CatchClause:
return findTranslationCalls(expression.block);
case TemplateLiteral:
case SequenceExpression:
return expression.expressions
.map(findTranslationCalls)
.flat();
case MemberExpression:
case ComputedMemberExpression:
case StaticMemberExpression:
return findTranslationCalls(expression.object);
case ThisExpression:
case Identifier:
case Literal:
case UpdateExpression:
case UnaryExpression:
case BreakStatement:
case ContinueStatement:
case ArrayPattern:
return [];
default:
console.debug(`Unknown expression type: ${expression.type}`);
console.debug(expression);
return [];
}
}
module.exports.findTranslationCalls = findTranslationCalls;
module.exports.contract = (s, size) => s.slice(size, -size);
module.exports.shave = (s, size) => module.exports.contract(s, size).trim();