-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathplugin.js
More file actions
157 lines (144 loc) · 4.94 KB
/
plugin.js
File metadata and controls
157 lines (144 loc) · 4.94 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
const {
i18nextImportStatement,
kImportStatement,
} = require('./frozen-asts');
const {
getUniqueKeyFromFreeText,
LutManager,
} = require('./lut');
const {
isBlacklistedForJsxAttribute,
handleConditionalExpressions,
} = require('./plugin-helpers');
const cloneDeep = (obj) => JSON.parse(JSON.stringify(obj));
const handleStringLiteral = (path, table, key) => {
const { value } = path.node;
if (!table[key]) table[key] = {};
if (!table[key].pairs) table[key].pairs = [];
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 }) => {
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) {
// Only extract the value of identifiers
// who are children of some JSX element
if (path.findParent(p => p.isJSXElement())) {
this.state[path.node.name] = { ...this.state[path.node.name], valid: true };
}
},
},
TemplateLiteral: {
enter(path) {
// 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 (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] = { ...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: 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) {
const key = path.node.left.name || (path.node.left.property && path.node.left.property.name);
if (!key) return;
extractValueAndUpdateTable(t, this.state, path.get('right'), key);
},
},
ObjectProperty: {
enter(path) {
const key = path.node.key && path.node.key.name;
if (!key) return;
// Check for blacklist
if (isBlacklistedForJsxAttribute(path)) return;
extractValueAndUpdateTable(t, this.state, path.get('value'), key);
},
},
VariableDeclarator: {
enter(path) {
const key = path.node.id && path.node.id.name;
if (!key) return;
// Check for blacklist
if (isBlacklistedForJsxAttribute(path)) return;
extractValueAndUpdateTable(t, this.state, path.get('init'), key);
},
},
JSXText: {
enter(path) {
const coreValue = (path.node.value || '').trim();
if (!coreValue.length) return;
const kValue = getUniqueKeyFromFreeText(coreValue);
path.node.value = path.node.value.replace(coreValue, `{i18n.t(k.${kValue})}`);
},
},
StringLiteral: {
enter(path) {
handleConditionalExpressions(path);
},
},
},
});