forked from vimeo/babel-plugin-transform-i18n
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (147 loc) · 6.65 KB
/
index.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
/* eslint-disable no-console */
module.exports = function i18nPlugin({ types: t }) {
function getNodeForValue(
value,
tokens,
leftDelimiter = '{',
rightDelimiter = '}',
) {
if (
value.match(new RegExp(`^${leftDelimiter}.*?${rightDelimiter}$`)) &&
tokens[value]
) {
return tokens[value];
}
return t.stringLiteral(value);
}
return {
visitor: {
CallExpression(path, state) {
const { debug, delimiter } = state.opts;
const leftDelimiter =
delimiter || state.opts.leftDelimiter || '{';
const rightDelimiter =
delimiter || state.opts.rightDelimiter || '}';
const pattern = new RegExp(
`(${leftDelimiter}.*?${rightDelimiter})`,
'g',
);
const translationFunctionName = state.opts.functionName || 't';
const dictionary = state.opts.dictionary || {};
if (
t.isIdentifier(path.node.callee) &&
path.node.callee.name === translationFunctionName
) {
const firstArgument = path.node.arguments[0];
if (!firstArgument || !firstArgument.extra) {
return;
}
const key = firstArgument.extra.rawValue;
if (dictionary[key] === '') {
if (debug) {
console.warn(`Found empty string for key: ${key}`);
}
path.replaceWith(t.stringLiteral(''));
return;
}
if (!dictionary[key]) {
if (debug) {
console.warn(
`Found invalid value for key: ${key} => ${dictionary[key]}`,
);
}
path.replaceWith(t.stringLiteral(key));
return;
}
if (
path.node.arguments.length > 1 &&
t.isObjectExpression(path.node.arguments[1])
) {
const tokens = path.node.arguments[1].properties.reduce(
(previous, current) => ({
...previous,
[`${leftDelimiter}${current.key.name}${rightDelimiter}`]:
current.value,
}),
{},
);
const replacementNode = dictionary[key]
.split(pattern)
.filter((component) => component !== '')
.reduce((previous, current, i) => {
let previousNode = previous;
if (i === 0) {
previousNode = getNodeForValue(
previous,
tokens,
leftDelimiter,
rightDelimiter,
);
}
const currentNode = getNodeForValue(
current,
tokens,
leftDelimiter,
rightDelimiter,
);
if (t.isStringLiteral(currentNode)) {
// If the previous node is a StringLiteral, return a combined StringLiteral
if (t.isStringLiteral(previousNode)) {
return t.stringLiteral(
`${previousNode.value}${currentNode.value}`,
);
}
// If the previous node is a BinaryExpression with a StringLiteral on the right side,
// update the BinaryExpression to have a combined StringLiteral on the right
if (
t.isBinaryExpression(previousNode) &&
t.isStringLiteral(previous.right)
) {
previousNode.right = t.stringLiteral(
`${previousNode.right.value}${currentNode.value}`,
);
return previousNode;
}
}
return t.binaryExpression(
'+',
previousNode,
currentNode,
);
}, '');
try {
path.replaceWith(replacementNode);
} catch (replaceWithError) {
if (debug) {
console.warn(
'replaceWith',
replacementNode,
replaceWithError,
);
}
try {
path.replaceWithSourceString(replacementNode);
} catch (replaceWithSourceStringError) {
if (debug) {
console.warn(
'replaceWithSourceString',
replacementNode,
replaceWithSourceStringError,
);
}
}
}
return;
}
try {
path.replaceWith(t.stringLiteral(dictionary[key]));
} catch (err) {
if (debug) {
console.warn(dictionary[key], err);
}
}
}
},
},
};
};