-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathplugin.js
More file actions
185 lines (166 loc) · 5.89 KB
/
plugin.js
File metadata and controls
185 lines (166 loc) · 5.89 KB
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 _ = require('lodash');
const {
i18nextImportStatement,
kImportStatement,
} = require('./frozen-asts');
const {
getUniqueKeyFromFreeText,
LutManager,
} = require('./lut');
const {
isBlacklistedForJsxAttribute,
handleConditionalExpressions,
handleURLInlitterals,
handleBlackListKey,
handleBlackListVariable,
handleBlackListValue,
} = require('./plugin-helpers');
const handleStringLiteral = (path, table, key) => {
const { value } = path.node;
if (!table[key]) table[key] = {};
if (!table[key].pairs) table[key].pairs = [];
if (handleURLInlitterals(value)) return
if (handleBlackListKey(key)) return;
if(handleBlackListValue(value)) return
table[key].pairs.push({ path, value });
};
const extractValueAndUpdateTable = (t, table, path, key) => {
if (t.isStringLiteral(path.node)) {
handleStringLiteral(path, table, key);
} else if (t.isArrayExpression(path.node)) {
path.get('elements').forEach((element) => {
if (t.isStringLiteral(element.node)) handleStringLiteral(element, table, key);
});
}
};
module.exports = ({ types: t }) => ({
name: 'i18ize-react',
visitor: {
Program: {
enter() {
this.state = {};
this.alreadyImportedK = false;
this.alreadyImportedi18n = false;
LutManager.resetGetUniqueKeyFromFreeTextNumCalls();
},
exit(programPath) {
Object.keys(this.state).forEach((key) => {
if (this.state[key].valid && this.state[key].pairs) {
this.state[key].pairs.forEach(({ path, value }) => {
// TODO: OPTIMIZATION: Use quasi quotes to optimize this
const kValue = getUniqueKeyFromFreeText(value);
path.replaceWithSourceString(`i18n.t(k.${kValue})`);
});
}
});
// Do not add imports if there is no replaceable text
// in this file
if (LutManager.getUniqueKeyFromFreeTextNumCalls > 0) {
if (!this.alreadyImportedK) programPath.node.body.unshift(_.cloneDeep(kImportStatement));
if (!this.alreadyImportedi18n) {
programPath.node.body
.unshift(_.cloneDeep(i18nextImportStatement));
}
}
},
},
ImportDeclaration: {
enter(path) {
// For idempotence
if (path.node.source.value.match(/i18n\/keys/)) {
this.alreadyImportedK = true;
}
if (path.node.source.value.match(/^i18next$/)) {
this.alreadyImportedi18n = true;
}
},
},
Identifier: {
enter(path) {
console.log("> Identifier")
// Only extract the value of identifiers
// who are children of some JSX element
if (path.findParent(p => p.isJSXElement())) {
this.state[path.node.name] = _.merge(this.state[path.node.name], { valid: true });
}
},
},
TemplateLiteral: {
enter(path) {
console.log("> TemplateLiteral")
// Only extract the value of identifiers
// who are children of some JSX element
const firstJsxParent = path.findParent(p => p.isJSXElement());
if (!firstJsxParent) return;
// Ignore CSS strings
if (_.get(firstJsxParent, 'node.openingElement.name.name') === 'style') return;
if (isBlacklistedForJsxAttribute(path)) return;
const { expressions, quasis } = path.node;
expressions.forEach((expression) => {
const key = expression.name;
this.state[key] = _.merge(this.state[key], { valid: true });
});
quasis.forEach((templateElement, index) => {
const coreValue = templateElement.value.raw.trim();
if (coreValue.length) {
const qPath = path.get('quasis')[index];
const kValue = getUniqueKeyFromFreeText(coreValue);
// TODO: OPTIMIZATION: Use quasi quotes to optimize this
// TODO: Replace the path instead of modifying the raw
qPath.node.value.raw = qPath.node.value.raw.replace(coreValue, `\${i18n.t(k.${kValue})}`);
qPath.node.value.cooked = qPath.node.value.cooked.replace(coreValue, `\${i18n.t(k.${kValue})}`);
}
});
},
},
AssignmentExpression: {
enter(path) {
console.log("> AssignmentExpression")
// TODO: Explore the reason behind crash
const key = _.get(path, 'node.left.name', _.get(path, 'node.left.property.name'));
if (!key) return;
if (handleBlackListVariable(key)) return;
extractValueAndUpdateTable(t, this.state, path.get('right'), key);
},
},
ObjectProperty: {
enter(path) {
console.log("> ObjectProperty")
const key = _.get(path, 'node.key.name');
if (!key) return;
// Check for blacklist
if (isBlacklistedForJsxAttribute(path)) return;
if(handleBlackListKey(key)) return
extractValueAndUpdateTable(t, this.state, path.get('value'), key);
},
},
VariableDeclarator: {
enter(path) {
console.log("> VariableDeclarator")
// TODO: Explore the reason behind crash
const key = _.get(path, 'node.id.name');
if (!key) return;
// Check for blacklist
if (isBlacklistedForJsxAttribute(path)) return;
if (handleBlackListVariable(key)) return;
extractValueAndUpdateTable(t, this.state, path.get('init'), key);
},
},
JSXText: {
enter(path) {
console.log("> JSXText")
const coreValue = _.get(path, 'node.value', '').trim();
if (!coreValue.length) return;
const kValue = getUniqueKeyFromFreeText(coreValue);
// TODO: OPTIMIZATION: Use quasi quotes to optimize this
path.node.value = path.node.value.replace(coreValue, `{i18n.t(k.${kValue})}`);
},
},
StringLiteral: {
enter(path) {
console.log("> StringLiteral")
handleConditionalExpressions(path);
},
},
},
});