File tree 3 files changed +103
-4
lines changed
packages/babel-plugin-lingui-macro
3 files changed +103
-4
lines changed Original file line number Diff line number Diff line change @@ -256,16 +256,22 @@ export class MacroJs {
256
256
// parent would be an Expression with this identifier which we are interesting in
257
257
const currentPath = refPath . parentPath
258
258
259
+ const _ctx = createMacroJsContext (
260
+ ctx . isLinguiIdentifier ,
261
+ ctx . stripNonEssentialProps ,
262
+ ctx . stripMessageProp
263
+ )
264
+
259
265
// { t } = useLingui()
260
266
// t`Hello!`
261
267
if ( currentPath . isTaggedTemplateExpression ( ) ) {
262
- const tokens = tokenizeTemplateLiteral ( currentPath . node , ctx )
268
+ const tokens = tokenizeTemplateLiteral ( currentPath . node , _ctx )
263
269
264
270
const descriptor = createMessageDescriptorFromTokens (
265
271
tokens ,
266
272
currentPath . node . loc ,
267
- ctx . stripNonEssentialProps ,
268
- ctx . stripMessageProp
273
+ _ctx . stripNonEssentialProps ,
274
+ _ctx . stripMessageProp
269
275
)
270
276
271
277
const callExpr = t . callExpression (
@@ -285,7 +291,7 @@ export class MacroJs {
285
291
const descriptor = processDescriptor (
286
292
( currentPath . get ( "arguments" ) [ 0 ] as NodePath < ObjectExpression > )
287
293
. node ,
288
- ctx
294
+ _ctx
289
295
)
290
296
const callExpr = t . callExpression (
291
297
t . identifier ( uniqTIdentifier . name ) ,
Original file line number Diff line number Diff line change 1
1
// Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
3
+ exports [` correctly process indexed placeholders in few t calls 1` ] = `
4
+ import { useLingui } from "@lingui/react/macro";
5
+ function Home() {
6
+ const { t } = useLingui ();
7
+ const user = { name: " John " };
8
+ return (
9
+ <main >
10
+ <button onClick = { () => console .log (t \` Hello \$ { user .name } \` )}>Hello</button>
11
+
12
+ <button onClick = { () => console .log (t \` Bye \$ { user .name } \` )}>Bye</button>
13
+ </main>
14
+ );
15
+ }
16
+
17
+ ↓ ↓ ↓ ↓ ↓ ↓
18
+
19
+ import { useLingui as _useLingui } from "@lingui/react";
20
+ function Home() {
21
+ const { _: _t } = _useLingui ();
22
+ const user = {
23
+ name: " John " ,
24
+ };
25
+ return (
26
+ <main >
27
+ <button
28
+ onClick = { () =>
29
+ console .log (
30
+ _t (
31
+ /* i18n*/
32
+ {
33
+ id: " Y7riaK" ,
34
+ message: " Hello {0}" ,
35
+ values: {
36
+ 0: user.name ,
37
+ },
38
+ }
39
+ )
40
+ )
41
+ }
42
+ >
43
+ Hello
44
+ </button >
45
+
46
+ <button
47
+ onClick = { () =>
48
+ console .log (
49
+ _t (
50
+ /* i18n*/
51
+ {
52
+ id: " vqOLZ6" ,
53
+ message: " Bye {0}" ,
54
+ values: {
55
+ 0: user.name ,
56
+ },
57
+ }
58
+ )
59
+ )
60
+ }
61
+ >
62
+ Bye
63
+ </button >
64
+ </main >
65
+ );
66
+ }
67
+
68
+ ` ;
69
+
3
70
exports [` does not crash when no params 1` ] = `
4
71
import { useLingui } from "@lingui/react/macro";
5
72
function MyComponent() {
Original file line number Diff line number Diff line change @@ -201,6 +201,32 @@ function MyComponent2() {
201
201
const { t } = useLingui();
202
202
const b = t\`Text\`;
203
203
}` ,
204
+ } ,
205
+ {
206
+ name : "correctly process indexed placeholders in few t calls" ,
207
+ code : `
208
+ import { useLingui } from '@lingui/react/macro';
209
+
210
+ function Home() {
211
+ const {t} = useLingui();
212
+ const user = {name: 'John '}
213
+ return (
214
+ <main>
215
+ <button onClick={() =>
216
+ console.log(t\`Hello \${user.name}\`)
217
+ }>
218
+ Hello
219
+ </button>
220
+
221
+ <button onClick={() =>
222
+ console.log(t\`Bye \${user.name}\`)
223
+ }>
224
+ Bye
225
+ </button>
226
+ </main>
227
+ );
228
+ }
229
+ ` ,
204
230
} ,
205
231
{
206
232
name : "support configuring runtime module import using LinguiConfig.runtimeConfigModule" ,
You can’t perform that action at this time.
0 commit comments