|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { transform } from "@swc/core"; |
| 3 | +import path from "node:path"; |
| 4 | +import url from "node:url"; |
| 5 | + |
| 6 | +const transformCode = async (code: string, options = {}) => { |
| 7 | + const result = await transform(code, { |
| 8 | + jsc: { |
| 9 | + parser: { |
| 10 | + syntax: "typescript", |
| 11 | + tsx: true, |
| 12 | + }, |
| 13 | + experimental: { |
| 14 | + plugins: [ |
| 15 | + [ |
| 16 | + path.join( |
| 17 | + path.dirname(url.fileURLToPath(import.meta.url)), |
| 18 | + "..", |
| 19 | + "swc_plugin_formatjs.wasm", |
| 20 | + ), |
| 21 | + options, |
| 22 | + ], |
| 23 | + ], |
| 24 | + }, |
| 25 | + }, |
| 26 | + }); |
| 27 | + return result.code; |
| 28 | +}; |
| 29 | + |
| 30 | +describe("formatjs swc plugin", () => { |
| 31 | + it("should transform FormattedMessage component", async () => { |
| 32 | + const input = ` |
| 33 | + import React from 'react'; |
| 34 | + import { FormattedMessage } from 'react-intl'; |
| 35 | +
|
| 36 | + export function Greeting() { |
| 37 | + return ( |
| 38 | + <FormattedMessage |
| 39 | + defaultMessage="Hello, {name}!" |
| 40 | + description="Greeting message" |
| 41 | + /> |
| 42 | + ); |
| 43 | + } |
| 44 | + `; |
| 45 | + |
| 46 | + const output = await transformCode(input); |
| 47 | + |
| 48 | + expect(output).toMatch(/id: \".+\"/); |
| 49 | + expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/); |
| 50 | + expect(output).not.toMatch(/description/); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should transform defineMessage function", async () => { |
| 54 | + const input = ` |
| 55 | + import { defineMessage } from 'react-intl'; |
| 56 | +
|
| 57 | + const message = defineMessage({ |
| 58 | + defaultMessage: "Welcome to {site}", |
| 59 | + description: "Welcome message" |
| 60 | + }); |
| 61 | + `; |
| 62 | + |
| 63 | + const output = await transformCode(input); |
| 64 | + |
| 65 | + expect(output).toMatch(/id: "[^"]+"/); |
| 66 | + expect(output).toMatch(/defaultMessage: "Welcome to \{site\}"/); |
| 67 | + expect(output).not.toMatch(/description/); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should transform multiple messages in defineMessages", async () => { |
| 71 | + const input = ` |
| 72 | + import { defineMessages } from 'react-intl'; |
| 73 | +
|
| 74 | + const messages = defineMessages({ |
| 75 | + greeting: { |
| 76 | + defaultMessage: "Hello", |
| 77 | + description: "Greeting" |
| 78 | + }, |
| 79 | + farewell: { |
| 80 | + defaultMessage: "Goodbye", |
| 81 | + description: "Farewell" |
| 82 | + } |
| 83 | + }); |
| 84 | + `; |
| 85 | + |
| 86 | + const output = await transformCode(input); |
| 87 | + |
| 88 | + const idMatches = output.match(/id:/g); |
| 89 | + expect(idMatches).toHaveLength(2); |
| 90 | + |
| 91 | + expect(output).toMatch(/defaultMessage: "Hello"/); |
| 92 | + expect(output).toMatch(/defaultMessage: "Goodbye"/); |
| 93 | + |
| 94 | + expect(output).not.toMatch(/description/); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should handle formatMessage calls", async () => { |
| 98 | + const input = ` |
| 99 | + import { useIntl } from 'react-intl'; |
| 100 | +
|
| 101 | + function MyComponent() { |
| 102 | + const intl = useIntl(); |
| 103 | + return intl.formatMessage({ |
| 104 | + defaultMessage: "Click here", |
| 105 | + description: "Button text" |
| 106 | + }); |
| 107 | + } |
| 108 | + `; |
| 109 | + |
| 110 | + const output = await transformCode(input); |
| 111 | + |
| 112 | + expect(output).toMatch(/id: "[^"]+"/); |
| 113 | + expect(output).toMatch(/defaultMessage: "Click here"/); |
| 114 | + expect(output).not.toMatch(/description/); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should preserve whitespace when option is enabled", async () => { |
| 118 | + const input = ` |
| 119 | + import { FormattedMessage } from 'react-intl'; |
| 120 | +
|
| 121 | + export function Greeting() { |
| 122 | + return ( |
| 123 | + <FormattedMessage |
| 124 | + defaultMessage="Hello, {name}!" |
| 125 | + description="Greeting message" |
| 126 | + /> |
| 127 | + ); |
| 128 | + } |
| 129 | + `; |
| 130 | + |
| 131 | + const output = await transformCode(input, { |
| 132 | + preserve_whitespace: true, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(output).toMatch(/defaultMessage: "Hello, {4}\{name\}!"/); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should use custom id interpolation pattern", async () => { |
| 139 | + const input = ` |
| 140 | + import { FormattedMessage } from 'react-intl'; |
| 141 | +
|
| 142 | + export function Greeting() { |
| 143 | + return ( |
| 144 | + <FormattedMessage |
| 145 | + defaultMessage="Hello, {name}!" |
| 146 | + description="Greeting message" |
| 147 | + /> |
| 148 | + ); |
| 149 | + } |
| 150 | + `; |
| 151 | + |
| 152 | + const output = await transformCode(input, { |
| 153 | + idInterpolationPattern: "[name]_[hash:base64:5]", |
| 154 | + }); |
| 155 | + |
| 156 | + expect(output).toMatch(/id: "file_[a-zA-Z0-9]{5}"/); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should handle additional component names", async () => { |
| 160 | + const input = ` |
| 161 | + import { CustomMessage } from './custom-intl'; |
| 162 | +
|
| 163 | + export function Greeting() { |
| 164 | + return ( |
| 165 | + <CustomMessage |
| 166 | + defaultMessage="Hello, {name}!" |
| 167 | + description="Greeting message" |
| 168 | + /> |
| 169 | + ); |
| 170 | + } |
| 171 | + `; |
| 172 | + |
| 173 | + const output = await transformCode(input, { |
| 174 | + additionalComponentNames: ["CustomMessage"], |
| 175 | + }); |
| 176 | + |
| 177 | + expect(output).toMatch(/id: "[^"]+"/); |
| 178 | + expect(output).toMatch(/defaultMessage: "Hello, \{name\}!"/); |
| 179 | + expect(output).not.toMatch(/description/); |
| 180 | + }); |
| 181 | +}); |
0 commit comments