@@ -29,10 +29,12 @@ function checkTransform(
2929 t : ExecutionContext ,
3030 inputTs : string ,
3131 expectedJs : string ,
32- messages : Message [ ] ,
33- autoImport = true
32+ opts ?: {
33+ messages ?: Message [ ] ;
34+ autoImport ?: boolean ;
35+ }
3436) {
35- if ( autoImport ) {
37+ if ( opts ?. autoImport ?? true ) {
3638 // Rather than fuss with imports in all the test cases, this little hack
3739 // automatically imports for `msg` and `html` (assuming those strings aren't
3840 // used with any other meanings).
@@ -54,7 +56,9 @@ function checkTransform(
5456 // them here, so it's a waste of time.
5557 options . typeRoots = [ ] ;
5658 const result = compileTsFragment ( inputTs , options , cache , ( program ) => ( {
57- before : [ litLocalizeTransform ( makeMessageIdMap ( messages ) , program ) ] ,
59+ before : [
60+ litLocalizeTransform ( makeMessageIdMap ( opts ?. messages ?? [ ] ) , program ) ,
61+ ] ,
5862 } ) ) ;
5963
6064 let formattedExpected = prettier . format ( expectedJs , { parser : 'typescript' } ) ;
@@ -75,31 +79,30 @@ function checkTransform(
7579
7680test ( 'unchanged const' , ( t ) => {
7781 const src = 'const foo = "foo";' ;
78- checkTransform ( t , src , src , [ ] ) ;
82+ checkTransform ( t , src , src ) ;
7983} ) ;
8084
8185test ( 'unchanged html' , ( t ) => {
8286 const src =
8387 'const foo = "foo"; const bar = "bar"; html`Hello ${foo} and ${bar}!`;' ;
84- checkTransform ( t , src , src , [ ] ) ;
88+ checkTransform ( t , src , src ) ;
8589} ) ;
8690
8791test ( 'msg(string)' , ( t ) => {
88- checkTransform ( t , 'msg("foo", "Hello World");' , '"Hello World";' , [ ] ) ;
92+ checkTransform ( t , 'msg("foo", "Hello World");' , '"Hello World";' ) ;
8993} ) ;
9094
9195test ( 'msg(string) translated' , ( t ) => {
92- checkTransform ( t , 'msg("foo", "Hello World");' , '`Hola Mundo`;' , [
93- { name : 'foo' , contents : [ 'Hola Mundo' ] } ,
94- ] ) ;
96+ checkTransform ( t , 'msg("foo", "Hello World");' , '`Hola Mundo`;' , {
97+ messages : [ { name : 'foo' , contents : [ 'Hola Mundo' ] } ] ,
98+ } ) ;
9599} ) ;
96100
97101test ( 'html(msg(string))' , ( t ) => {
98102 checkTransform (
99103 t ,
100104 'html`<b>${msg("foo", "Hello World")}</b>`;' ,
101- 'html`<b>Hello World</b>`;' ,
102- [ ]
105+ 'html`<b>Hello World</b>`;'
103106 ) ;
104107} ) ;
105108
@@ -108,16 +111,15 @@ test('html(msg(string)) translated', (t) => {
108111 t ,
109112 'html`<b>${msg("foo", "Hello World")}</b>`;' ,
110113 'html`<b>Hola Mundo</b>`;' ,
111- [ { name : 'foo' , contents : [ 'Hola Mundo' ] } ]
114+ { messages : [ { name : 'foo' , contents : [ 'Hola Mundo' ] } ] }
112115 ) ;
113116} ) ;
114117
115118test ( 'html(msg(html))' , ( t ) => {
116119 checkTransform (
117120 t ,
118121 'html`<b>${msg("foo", html`Hello <i>World</i>`)}</b>`;' ,
119- 'html`<b>Hello <i>World</i></b>`;' ,
120- [ ]
122+ 'html`<b>Hello <i>World</i></b>`;'
121123 ) ;
122124} ) ;
123125
@@ -126,26 +128,27 @@ test('html(msg(html)) translated', (t) => {
126128 t ,
127129 'html`<b>${msg("foo", html`Hello <i>World</i>`)}</b>`;' ,
128130 'html`<b>Hola <i>Mundo</i></b>`;' ,
129- [
130- {
131- name : 'foo' ,
132- contents : [
133- 'Hola ' ,
134- { untranslatable : '<i>' } ,
135- 'Mundo' ,
136- { untranslatable : '</i>' } ,
137- ] ,
138- } ,
139- ]
131+ {
132+ messages : [
133+ {
134+ name : 'foo' ,
135+ contents : [
136+ 'Hola ' ,
137+ { untranslatable : '<i>' } ,
138+ 'Mundo' ,
139+ { untranslatable : '</i>' } ,
140+ ] ,
141+ } ,
142+ ] ,
143+ }
140144 ) ;
141145} ) ;
142146
143147test ( 'msg(fn(string), expr)' , ( t ) => {
144148 checkTransform (
145149 t ,
146150 'const name = "World";' + 'msg("foo", (name) => `Hello ${name}!`, name);' ,
147- 'const name = "World";' + '`Hello ${name}!`;' ,
148- [ ]
151+ 'const name = "World";' + '`Hello ${name}!`;'
149152 ) ;
150153} ) ;
151154
@@ -154,21 +157,22 @@ test('msg(fn(string), expr) translated', (t) => {
154157 t ,
155158 'const name = "World";' + 'msg("foo", (name) => `Hello ${name}!`, name);' ,
156159 'const name = "World";' + '`Hola ${name}!`;' ,
157- [
158- {
159- name : 'foo' ,
160- contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
161- } ,
162- ]
160+ {
161+ messages : [
162+ {
163+ name : 'foo' ,
164+ contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
165+ } ,
166+ ] ,
167+ }
163168 ) ;
164169} ) ;
165170
166171test ( 'msg(fn(string), string)' , ( t ) => {
167172 checkTransform (
168173 t ,
169174 'msg("foo", (name) => `Hello ${name}!`, "World");' ,
170- '`Hello World!`;' ,
171- [ ]
175+ '`Hello World!`;'
172176 ) ;
173177} ) ;
174178
@@ -177,12 +181,14 @@ test('msg(fn(string), string) translated', (t) => {
177181 t ,
178182 'msg("foo", (name) => `Hello ${name}!`, "World");' ,
179183 '`Hola World!`;' ,
180- [
181- {
182- name : 'foo' ,
183- contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
184- } ,
185- ]
184+ {
185+ messages : [
186+ {
187+ name : 'foo' ,
188+ contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
189+ } ,
190+ ] ,
191+ }
186192 ) ;
187193} ) ;
188194
@@ -191,8 +197,7 @@ test('msg(fn(html), expr)', (t) => {
191197 t ,
192198 'const name = "World";' +
193199 'msg("foo", (name) => html`Hello <b>${name}</b>!`, name);' ,
194- 'const name = "World";' + 'html`Hello <b>${name}</b>!`;' ,
195- [ ]
200+ 'const name = "World";' + 'html`Hello <b>${name}</b>!`;'
196201 ) ;
197202} ) ;
198203
@@ -202,21 +207,22 @@ test('msg(fn(html), expr) translated', (t) => {
202207 'const name = "World";' +
203208 'msg("foo", (name) => html`Hello <b>${name}</b>!`, name);' ,
204209 'const name = "World";' + 'html`Hola <b>${name}</b>!`;' ,
205- [
206- {
207- name : 'foo' ,
208- contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
209- } ,
210- ]
210+ {
211+ messages : [
212+ {
213+ name : 'foo' ,
214+ contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
215+ } ,
216+ ] ,
217+ }
211218 ) ;
212219} ) ;
213220
214221test ( 'msg(fn(html), string)' , ( t ) => {
215222 checkTransform (
216223 t ,
217224 'msg("foo", (name) => html`Hello <b>${name}</b>!`, "World");' ,
218- 'html`Hello <b>World</b>!`;' ,
219- [ ]
225+ 'html`Hello <b>World</b>!`;'
220226 ) ;
221227} ) ;
222228
@@ -225,21 +231,22 @@ test('msg(fn(html), string) translated', (t) => {
225231 t ,
226232 'msg("foo", (name) => html`Hello <b>${name}</b>!`, "World");' ,
227233 'html`Hola <b>World</b>!`;' ,
228- [
229- {
230- name : 'foo' ,
231- contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
232- } ,
233- ]
234+ {
235+ messages : [
236+ {
237+ name : 'foo' ,
238+ contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
239+ } ,
240+ ] ,
241+ }
234242 ) ;
235243} ) ;
236244
237245test ( 'msg(fn(html), html)' , ( t ) => {
238246 checkTransform (
239247 t ,
240248 'msg("foo", (name) => html`Hello <b>${name}</b>!`, html`<i>World</i>`);' ,
241- 'html`Hello <b><i>World</i></b>!`;' ,
242- [ ]
249+ 'html`Hello <b><i>World</i></b>!`;'
243250 ) ;
244251} ) ;
245252
@@ -248,21 +255,22 @@ test('msg(fn(html), html) translated', (t) => {
248255 t ,
249256 'msg("foo", (name) => html`Hello <b>${name}</b>!`, html`<i>World</i>`);' ,
250257 'html`Hola <b><i>World</i></b>!`;' ,
251- [
252- {
253- name : 'foo' ,
254- contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
255- } ,
256- ]
258+ {
259+ messages : [
260+ {
261+ name : 'foo' ,
262+ contents : [ 'Hola ' , { untranslatable : '<b>${name}</b>' } , '!' ] ,
263+ } ,
264+ ] ,
265+ }
257266 ) ;
258267} ) ;
259268
260269test ( 'msg(fn(string), msg(string))' , ( t ) => {
261270 checkTransform (
262271 t ,
263272 'msg("foo", (name) => `Hello ${name}!`, msg("bar", "World"));' ,
264- '`Hello World!`;' ,
265- [ ]
273+ '`Hello World!`;'
266274 ) ;
267275} ) ;
268276
@@ -271,16 +279,18 @@ test('msg(fn(string), msg(string)) translated', (t) => {
271279 t ,
272280 'msg("foo", (name) => `Hello ${name}!`, msg("bar", "World"));' ,
273281 '`Hola Mundo!`;' ,
274- [
275- {
276- name : 'foo' ,
277- contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
278- } ,
279- {
280- name : 'bar' ,
281- contents : [ 'Mundo' ] ,
282- } ,
283- ]
282+ {
283+ messages : [
284+ {
285+ name : 'foo' ,
286+ contents : [ 'Hola ' , { untranslatable : '${name}' } , '!' ] ,
287+ } ,
288+ {
289+ name : 'bar' ,
290+ contents : [ 'Mundo' ] ,
291+ } ,
292+ ] ,
293+ }
284294 ) ;
285295} ) ;
286296
@@ -292,8 +302,7 @@ test('import * as litLocalize', (t) => {
292302 litLocalize.msg("foo", "Hello World");
293303 ` ,
294304 '"Hello World";' ,
295- [ ] ,
296- false
305+ { autoImport : false }
297306 ) ;
298307} ) ;
299308
@@ -305,8 +314,7 @@ test('import {msg as foo}', (t) => {
305314 foo("foo", "Hello World");
306315 ` ,
307316 '"Hello World";' ,
308- [ ] ,
309- false
317+ { autoImport : false }
310318 ) ;
311319} ) ;
312320
@@ -317,7 +325,6 @@ test('exclude different msg function', (t) => {
317325 msg("foo", "Hello World");` ,
318326 `function msg(id, template) { return template; }
319327 msg("foo", "Hello World");` ,
320- [ ] ,
321- false
328+ { autoImport : false }
322329 ) ;
323330} ) ;
0 commit comments