/
jsx-lexer.test.js
394 lines (349 loc) · 12.9 KB
/
jsx-lexer.test.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { assert } from 'chai'
import JsxLexer from '../../src/lexers/jsx-lexer.js'
describe('JsxLexer', () => {
describe('<Interpolate>', () => {
it('extracts keys from i18nKey attributes', (done) => {
const Lexer = new JsxLexer()
const content = '<Interpolate i18nKey="first" />'
assert.deepEqual(Lexer.extract(content), [{ key: 'first' }])
done()
})
})
describe('<Trans>', () => {
it('extracts keys from i18nKey attributes from closing tags', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans i18nKey="first" count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts default value from string literal `defaults` prop', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans i18nKey="first" defaults="test-value">should be ignored</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'test-value' },
])
done()
})
it('extracts default value from interpolated expression statement `defaults` prop', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans i18nKey="first" defaults={"test-value"}>should be ignored</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'test-value' },
])
done()
})
it('extracts keys from user-defined key attributes from closing tags', (done) => {
const Lexer = new JsxLexer({ attr: 'myIntlKey' })
const content = '<Trans myIntlKey="first" count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts keys from i18nKey attributes from self-closing tags', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans i18nKey="first" count={count} />'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', count: '{count}' },
])
done()
})
it('extracts keys from user-defined key attributes from self-closing tags', (done) => {
const Lexer = new JsxLexer({ attr: 'myIntlKey' })
const content = '<Trans myIntlKey="first" count={count} />'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', count: '{count}' },
])
done()
})
it('extracts custom attributes', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans customAttribute="Youpi">Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'Yo', defaultValue: 'Yo', customAttribute: 'Youpi' },
])
done()
})
it('extracts boolean attributes', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans ordinal customTrue={true} customFalse={false}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{
key: 'Yo',
defaultValue: 'Yo',
ordinal: true,
customTrue: true,
customFalse: false,
},
])
done()
})
it('extracts keys from Trans elements without an i18nKey', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'Yo', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts keys from Trans elements and ignores values of expressions and spaces', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>{{ key: property }}</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: '{{key}}', defaultValue: '{{key}}', count: '{count}' },
])
done()
})
it('extracts keys from single line comments', (done) => {
const Lexer = new JsxLexer()
const content = `
// i18n.t('commentKey1')
i18n.t('commentKey' + i)
// i18n.t('commentKey2')
i18n.t(\`commentKey\${i}\`)
// Irrelevant comment
// i18n.t('commentKey3')
`
assert.deepEqual(Lexer.extract(content), [
{ key: 'commentKey1' },
{ key: 'commentKey2' },
{ key: 'commentKey3' },
])
done()
})
it('extracts keys from multiline comments', (done) => {
const Lexer = new JsxLexer()
const content = `
/*
i18n.t('commentKey1')
i18n.t('commentKey2')
*/
i18n.t(\`commentKey\${i}\`)
// Irrelevant comment
/* i18n.t('commentKey3') */
`
assert.deepEqual(Lexer.extract(content), [
{ key: 'commentKey1' },
{ key: 'commentKey2' },
{ key: 'commentKey3' },
])
done()
})
it('invalid interpolation gets stripped', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>before{{ key1, key2 }}after</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'beforeafter', defaultValue: 'beforeafter', count: '{count}' },
])
done()
})
it("doesn't add a blank key for self-closing or empty tags", (done) => {
const Lexer = new JsxLexer()
const emptyTag = '<Trans count={count}></Trans>'
assert.deepEqual(Lexer.extract(emptyTag), [])
const selfClosing = '<Trans count={count}/>'
assert.deepEqual(Lexer.extract(selfClosing), [])
done()
})
it('erases tags from content', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans>a<b test={"</b>"}>c<c>z</c></b>{d}<br stuff={y}/></Trans>'
assert.equal(
Lexer.extract(content)[0].defaultValue,
'a<1>c<1>z</1></1>{d}<3></3>'
)
done()
})
it('erases comment expressions', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>{/* some comment */}Some Content</Trans>'
assert.equal(Lexer.extract(content)[0].defaultValue, 'Some Content')
done()
})
it('handles jsx fragments', (done) => {
const Lexer = new JsxLexer()
const content = '<><Trans i18nKey="first" /></>'
assert.deepEqual(Lexer.extract(content), [{ key: 'first' }])
done()
})
it('interpolates literal string values', (done) => {
const Lexer = new JsxLexer()
const content = `<Trans>Some{' '}Interpolated {'Content'}</Trans>`
assert.equal(
Lexer.extract(content)[0].defaultValue,
'Some Interpolated Content'
)
done()
})
it('uses the ns (namespace) prop', (done) => {
const Lexer = new JsxLexer()
const content = `<Trans ns="foo">bar</Trans>`
assert.deepEqual(Lexer.extract(content), [
{ key: 'bar', defaultValue: 'bar', namespace: 'foo' },
])
done()
})
it('uses the ns (namespace) prop with curly braces syntax', (done) => {
const Lexer = new JsxLexer()
const content = `<Trans ns={'foo'}>bar</Trans>`
assert.deepEqual(Lexer.extract(content), [
{ key: 'bar', defaultValue: 'bar', namespace: 'foo' },
])
done()
})
it('emits a `warning` event if the component attribute is a JSX spread attribute', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans defaults="bar" {...spread} />'
Lexer.on('warning', (message) => {
assert.equal(
message,
'Component attribute is a JSX spread attribute : spread'
)
done()
})
assert.deepEqual(Lexer.extract(content), [{ defaultValue: 'bar' }])
})
})
describe('supports TypeScript', () => {
it('supports basic tsx syntax', () => {
const Lexer = new JsxLexer()
const content = '<Interpolate i18nKey="first" someVar={foo() as bar} />'
assert.deepEqual(Lexer.extract(content), [{ key: 'first' }])
})
describe('<Interpolate>', () => {
it('extracts keys from i18nKey attributes', (done) => {
const Lexer = new JsxLexer()
const content = '<Interpolate i18nKey="first" />'
assert.deepEqual(Lexer.extract(content), [{ key: 'first' }])
done()
})
})
describe('<Trans>', () => {
it('extracts keys from i18nKey attributes from closing tags', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans i18nKey="first" count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts keys from user-defined key attributes from closing tags', (done) => {
const Lexer = new JsxLexer({ attr: 'myIntlKey' })
const content = '<Trans myIntlKey="first" count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts keys from i18nKey attributes from self-closing tags', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans i18nKey="first" count={count} />'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', count: '{count}' },
])
done()
})
it('does not extract variable identifier from i18nKey as key', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans i18nKey={variable} />'
assert.deepEqual(Lexer.extract(content), [])
done()
})
it('extracts keys from user-defined key attributes from self-closing tags', (done) => {
const Lexer = new JsxLexer({ attr: 'myIntlKey' })
const content = '<Trans myIntlKey="first" count={count} />'
assert.deepEqual(Lexer.extract(content), [
{ key: 'first', count: '{count}' },
])
done()
})
it('extracts keys from Trans elements without an i18nKey', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>Yo</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'Yo', defaultValue: 'Yo', count: '{count}' },
])
done()
})
it('extracts keys from Trans elements and ignores values of expressions and spaces', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans count={count}>{{ key: property }}</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: '{{key}}', defaultValue: '{{key}}', count: '{count}' },
])
done()
})
it('strips invalid interpolation', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans count={count}>before{{ key1, key2 }}after</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: 'beforeafter', defaultValue: 'beforeafter', count: '{count}' },
])
done()
})
it("doesn't add a blank key for self-closing or empty tags", (done) => {
const Lexer = new JsxLexer()
const emptyTag = '<Trans count={count}></Trans>'
assert.deepEqual(Lexer.extract(emptyTag), [])
const selfClosing = '<Trans count={count}/>'
assert.deepEqual(Lexer.extract(selfClosing), [])
done()
})
it('erases tags from content', (done) => {
const Lexer = new JsxLexer()
const content =
'<Trans>a<b test={"</b>"}>c<c>z</c></b>{d}<br stuff={y}/></Trans>'
assert.equal(
Lexer.extract(content)[0].defaultValue,
'a<1>c<1>z</1></1>{d}<3></3>'
)
done()
})
it('erases comment expressions', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>{/* some comment */}Some Content</Trans>'
assert.equal(Lexer.extract(content)[0].defaultValue, 'Some Content')
done()
})
it('erases typecasts', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>{{ key: property } as any}</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: '{{key}}', defaultValue: '{{key}}' },
])
done()
})
it('keeps self-closing tags untouched when transSupportBasicHtmlNodes is true', (done) => {
const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true })
const content = '<Trans>a<br />b</Trans>'
assert.equal(Lexer.extract(content)[0].defaultValue, 'a<br />b')
done()
})
it('keeps empty tag untouched when transSupportBasicHtmlNodes is true', (done) => {
const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true })
const content = '<Trans>a<strong></strong>b</Trans>'
assert.equal(
Lexer.extract(content)[0].defaultValue,
'a<strong></strong>b'
)
done()
})
it('supports the shouldUnescape options', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans shouldUnescape>I&apros;m Cielquan</Trans>'
assert.equal(
Lexer.extract(content)[0].defaultValue,
'I&apros;m Cielquan'
)
done()
})
})
})
})