-
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathwasm.test.ts
More file actions
656 lines (542 loc) · 17.2 KB
/
wasm.test.ts
File metadata and controls
656 lines (542 loc) · 17.2 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import { describe, expect, it } from "vitest";
import { transform } from "@swc/core";
import path from "node:path";
import url from "node:url";
const transformCode = async (code: string, options = {}) => {
const result = await transform(code, {
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
experimental: {
plugins: [
[
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
"..",
"swc_plugin_formatjs.wasm",
),
options,
],
],
},
},
});
return result.code;
};
describe("formatjs swc plugin", () => {
it("should transform FormattedMessage component", async () => {
const input = `
import React from 'react';
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, {name}!"
description="Greeting message"
/>
);
}
`;
const output = await transformCode(input);
expect(output).toMatch(/id: \".+\"/);
expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/);
expect(output).not.toMatch(/description/);
});
it("should transform defineMessage function", async () => {
const input = `
import { defineMessage } from 'react-intl';
const message = defineMessage({
defaultMessage: "Welcome to {site}",
description: "Welcome message"
});
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "Welcome to \{site\}"/);
expect(output).not.toMatch(/description/);
});
it("should transform multiple messages in defineMessages", async () => {
const input = `
import { defineMessages } from 'react-intl';
const messages = defineMessages({
greeting: {
defaultMessage: "Hello",
description: "Greeting"
},
farewell: {
defaultMessage: "Goodbye",
description: "Farewell"
}
});
`;
const output = await transformCode(input);
const idMatches = output.match(/id:/g);
expect(idMatches).toHaveLength(2);
expect(output).toMatch(/defaultMessage: "Hello"/);
expect(output).toMatch(/defaultMessage: "Goodbye"/);
expect(output).not.toMatch(/description/);
});
it("should handle string concatenation in defaultMessage", async () => {
const input = `
import { defineMessage } from 'react-intl';
const message = defineMessage({
defaultMessage: "Foo " + "Bar",
description: "foobar"
});
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "Foo Bar"/);
expect(output).not.toMatch(/description/);
});
it("should handle multiple string concatenations", async () => {
const input = `
import { defineMessage } from 'react-intl';
const message = defineMessage({
defaultMessage: "This is " + "a very " + "long message",
description: "multi concat"
});
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "This is a very long message"/);
expect(output).not.toMatch(/description/);
});
it("should handle string concatenation in FormattedMessage JSX", async () => {
const input = `
import React from 'react';
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage={"Hello " + "World"}
description="jsx concat"
/>
);
}
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "Hello World"/);
expect(output).not.toMatch(/description/);
});
it("should handle statically evaluate-able variables", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';
const part1 = "Hello";
const part2 = "world";
const message = defineMessage({
defaultMessage: part1 + part2,
description: "static vars"
});
function Greeting() {
const message2 = formatMessage({
defaultMessage: part1 + part2,
description: "static vars in function"
});
const templateMessage = formatMessage({
defaultMessage: \`~\${part1}, \${part2}!\`,
description: "static string"
});
return (<FormattedMessage defaultMessage={part1 + part2} />);
}
`;
const output = await transformCode(input);
expect(output).toMatchSnapshot();
});
it("should throw error on non-statically evaluate-able variables", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';
const part1 = "Hello, ";
const message = defineMessage({
defaultMessage: part1 + part2,
description: "static vars"
});
`;
expect(transformCode(input)).rejects.toThrow(
"[React Intl] Messages must be statically evaluate-able for extraction.",
);
});
it("should transform to ast when enabled", async () => {
const input = `
import { defineMessage, formatMessage, FormattedMessage } from 'react-intl';
const helloWorldMessage = formatMessage({
defaultMessage: "Hello, world!",
});
const helloWorld = defineMessage({
defaultMessage: "Hello, world!",
description: "A simple greeting",
});
export function Greeting() {
return (
<FormattedMessage defaultMessage="Hello, world!" />
);
}
`;
const code = await transformCode(input, { ast: true });
expect(code).toMatchSnapshot();
});
it("should handle formatMessage calls", async () => {
const input = `
import { useIntl } from 'react-intl';
function MyComponent() {
const intl = useIntl();
return intl.formatMessage({
defaultMessage: "Click here",
description: "Button text"
});
}
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "Click here"/);
expect(output).not.toMatch(/description/);
});
it("should preserve whitespace when option is enabled", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, {name}!"
description="Greeting message"
/>
);
}
`;
const output = await transformCode(input, {
preserve_whitespace: true,
});
expect(output).toMatch(/defaultMessage: "Hello, {4}\{name\}!"/);
});
it("should use custom id interpolation pattern", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, {name}!"
description="Greeting message"
/>
);
}
`;
const output = await transformCode(input, {
idInterpolationPattern: "[name]_[hash:base64:5]",
});
expect(output).toMatch(/id: "file_[a-zA-Z0-9]{5}"/);
});
it("should handle additional component names", async () => {
const input = `
import { CustomMessage } from './custom-intl';
export function Greeting() {
return (
<CustomMessage
defaultMessage="Hello, {name}!"
description="Greeting message"
/>
);
}
`;
const output = await transformCode(input, {
additionalComponentNames: ["CustomMessage"],
});
expect(output).toMatch(/id: "[^"]+"/);
expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/);
expect(output).not.toMatch(/description/);
});
it("should be able to use sha1 and sha512 hashing in interpolation", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello!"
description="Greeting message"
/>
);
}
`;
const md5output = await transformCode(input, {
idInterpolationPattern: "[md5:contenthash:base64:6]",
});
const sha1output = await transformCode(input, {
idInterpolationPattern: "[sha1:contenthash:base64:6]",
});
const sha512output = await transformCode(input, {
idInterpolationPattern: "[sha512:contenthash:base64:6]",
});
expect(md5output).toMatch(/id: "[a-zA-Z0-9]{6}"/);
expect(sha1output).toMatch(/id: "[a-zA-Z0-9]{6}"/);
expect(sha512output).toMatch(/id: "[a-zA-Z0-9]{6}"/);
expect(md5output).not.toMatch(sha512output);
expect(sha1output).not.toMatch(sha512output);
});
it("should be able to use object description", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello!"
description={{ text: "Greeting message" }}
/>
);
}
`;
const output = await transformCode(input);
expect(output).toMatch(/id: "zL\/jyT"/);
});
it("should generate same id even if order of keys is different in two description objects with same keys", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello!"
description={{ text: "Greeting message", image: "https://example.com/image.png" }}
/>
);
}
`;
const input2 = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello!"
description={{ image: "https://example.com/image.png", text: "Greeting message" }}
/>
);
}
`;
const output = await transformCode(input);
const output2 = await transformCode(input2);
expect(output).toMatch(output2);
});
it("should generate same id even if description is an external variable", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
const description = {
text: "Hello description",
img: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
};
<FormattedMessage
defaultMessage="Hello message {name}"
description={description}
values={{
name: value.value,
}}
/>
`;
const input2 = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello message {name}"
description={{
text: "Hello description",
img: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
}}
values={{
name: value.value,
}}
/>
);
}
`;
const output = await transformCode(input);
const output2 = await transformCode(input2);
const id1 = output.match(/id: "([^"]+)"/)?.[1];
const id2 = output2.match(/id: "([^"]+)"/)?.[1];
expect(id1).toBe(id2);
});
it("should generate same id after react compiler optimizations", async () => {
const input = `
"use client";
import { c as _c } from "react/compiler-runtime";
import { FormattedMessage } from "react-intl";
interface ClientProps {
value: {
value: string;
};
}
export function Client(t0) {
const $ = _c(3);
const { value } = t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = {
text: "Hello",
img: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
};
$[0] = t1;
} else {
t1 = $[0];
}
let t2;
if ($[1] !== value.value) {
t2 = (
<FormattedMessage
defaultMessage="Hello {name}"
description={t1}
values={{ name: value.value }}
/>
);
$[1] = value.value;
$[2] = t2;
} else {
t2 = $[2];
}
return t2;
}
`;
const input2 = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello {name}"
description={{
text: "Hello",
img: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
}}
values={{
name: value.value,
}}
/>
);
}
`;
const output = await transformCode(input);
const output2 = await transformCode(input2);
const id1 = output.match(/id: "([^"]+)"/)?.[1];
const id2 = output2.match(/id: "([^"]+)"/)?.[1];
expect(id1).toBe(id2);
});
it("should be able to use different encodings in interpolation", async () => {
const input = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, World!"
description="Greeting message"
/>
);
}
`;
const hexOutput = await transformCode(input, {
idInterpolationPattern: "[sha512:contenthash:hex:9]",
});
const base64UrlOutput = await transformCode(input, {
idInterpolationPattern: "[sha512:contenthash:base64url:12]",
});
expect(hexOutput).toMatch(/id: "[0-9a-f]{9}"/);
expect(base64UrlOutput).toMatch(/id: "[a-zA-Z0-9-_]{12}"/);
});
it("should quote plural keys correctly when ast enabled", async () => {
const input = `
import { formatMessage } from 'react-intl';
formatMessage(
{
defaultMessage: \`
You did {count, plural,
=0 {nothing}
=1 {1 click}
other {# clicks}
}
\`,
},
{ count }
)`;
const code = await transformCode(input, { ast: true });
expect(code).toMatchSnapshot();
});
it("should not break on JSX outside formatjs calls", async () => {
const input = `
import React from 'react';
const Loading = () => <div>Loading...</div>;
function App() {
return (
<React.Suspense fallback={<Loading />}>
<div>Content</div>
</React.Suspense>
);
}
`;
const output = await transformCode(input);
// Build should succeed; no formatjs ids should be generated
expect(output).toBeTruthy();
expect(output).not.toMatch(/id:/);
// The original JSX structure should be preserved
expect(output).toMatch(/React\.Suspense/);
expect(output).toMatch(/Loading/);
});
it("should not break on conditional JSX rendering outside formatjs calls", async () => {
const input = `
import React from 'react';
function App({ show }) {
return show ? <div>Hello</div> : <span>World</span>;
}
`;
const output = await transformCode(input);
// Build should succeed; no formatjs ids should be generated
expect(output).toBeTruthy();
expect(output).not.toMatch(/id:/);
// The original JSX structure should be preserved
expect(output).toMatch(/Hello/);
expect(output).toMatch(/World/);
});
it("should generate same id even if description is an template literal string", async () => {
const input1 = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, World!"
description="Greeting message"
/>
);
}
`;
const input2 = `
import { FormattedMessage } from 'react-intl';
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, World!"
description={\`Greeting message\`}
/>
);
}
`;
const input3 = `
import { FormattedMessage } from 'react-intl';
const description = \`Greeting message\`;
export function Greeting() {
return (
<FormattedMessage
defaultMessage="Hello, World!"
description={description}
/>
);
}
`;
const code1 = await transformCode(input1, {
idInterpolationPattern: "[sha512:contenthash:base64:6]",
});
const code2 = await transformCode(input2, {
idInterpolationPattern: "[sha512:contenthash:base64:6]",
});
const code3 = await transformCode(input3, {
idInterpolationPattern: "[sha512:contenthash:base64:6]",
});
expect(code1).toMatch(/id: "Ae\/S0P"/);
expect(code2).toMatch(/id: "Ae\/S0P"/);
expect(code3).toMatch(/id: "Ae\/S0P"/);
});
});