@@ -29,10 +29,12 @@ function checkTransform(
29
29
t : ExecutionContext ,
30
30
inputTs : string ,
31
31
expectedJs : string ,
32
- messages : Message [ ] ,
33
- autoImport = true
32
+ opts ?: {
33
+ messages ?: Message [ ] ;
34
+ autoImport ?: boolean ;
35
+ }
34
36
) {
35
- if ( autoImport ) {
37
+ if ( opts ?. autoImport ?? true ) {
36
38
// Rather than fuss with imports in all the test cases, this little hack
37
39
// automatically imports for `msg` and `html` (assuming those strings aren't
38
40
// used with any other meanings).
@@ -54,7 +56,9 @@ function checkTransform(
54
56
// them here, so it's a waste of time.
55
57
options . typeRoots = [ ] ;
56
58
const result = compileTsFragment ( inputTs , options , cache , ( program ) => ( {
57
- before : [ litLocalizeTransform ( makeMessageIdMap ( messages ) , program ) ] ,
59
+ before : [
60
+ litLocalizeTransform ( makeMessageIdMap ( opts ?. messages ?? [ ] ) , program ) ,
61
+ ] ,
58
62
} ) ) ;
59
63
60
64
let formattedExpected = prettier . format ( expectedJs , { parser : 'typescript' } ) ;
@@ -75,31 +79,30 @@ function checkTransform(
75
79
76
80
test ( 'unchanged const' , ( t ) => {
77
81
const src = 'const foo = "foo";' ;
78
- checkTransform ( t , src , src , [ ] ) ;
82
+ checkTransform ( t , src , src ) ;
79
83
} ) ;
80
84
81
85
test ( 'unchanged html' , ( t ) => {
82
86
const src =
83
87
'const foo = "foo"; const bar = "bar"; html`Hello ${foo} and ${bar}!`;' ;
84
- checkTransform ( t , src , src , [ ] ) ;
88
+ checkTransform ( t , src , src ) ;
85
89
} ) ;
86
90
87
91
test ( 'msg(string)' , ( t ) => {
88
- checkTransform ( t , 'msg("foo", "Hello World");' , '"Hello World";' , [ ] ) ;
92
+ checkTransform ( t , 'msg("foo", "Hello World");' , '"Hello World";' ) ;
89
93
} ) ;
90
94
91
95
test ( '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
+ } ) ;
95
99
} ) ;
96
100
97
101
test ( 'html(msg(string))' , ( t ) => {
98
102
checkTransform (
99
103
t ,
100
104
'html`<b>${msg("foo", "Hello World")}</b>`;' ,
101
- 'html`<b>Hello World</b>`;' ,
102
- [ ]
105
+ 'html`<b>Hello World</b>`;'
103
106
) ;
104
107
} ) ;
105
108
@@ -108,16 +111,15 @@ test('html(msg(string)) translated', (t) => {
108
111
t ,
109
112
'html`<b>${msg("foo", "Hello World")}</b>`;' ,
110
113
'html`<b>Hola Mundo</b>`;' ,
111
- [ { name : 'foo' , contents : [ 'Hola Mundo' ] } ]
114
+ { messages : [ { name : 'foo' , contents : [ 'Hola Mundo' ] } ] }
112
115
) ;
113
116
} ) ;
114
117
115
118
test ( 'html(msg(html))' , ( t ) => {
116
119
checkTransform (
117
120
t ,
118
121
'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>`;'
121
123
) ;
122
124
} ) ;
123
125
@@ -126,26 +128,27 @@ test('html(msg(html)) translated', (t) => {
126
128
t ,
127
129
'html`<b>${msg("foo", html`Hello <i>World</i>`)}</b>`;' ,
128
130
'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
+ }
140
144
) ;
141
145
} ) ;
142
146
143
147
test ( 'msg(fn(string), expr)' , ( t ) => {
144
148
checkTransform (
145
149
t ,
146
150
'const name = "World";' + 'msg("foo", (name) => `Hello ${name}!`, name);' ,
147
- 'const name = "World";' + '`Hello ${name}!`;' ,
148
- [ ]
151
+ 'const name = "World";' + '`Hello ${name}!`;'
149
152
) ;
150
153
} ) ;
151
154
@@ -154,21 +157,22 @@ test('msg(fn(string), expr) translated', (t) => {
154
157
t ,
155
158
'const name = "World";' + 'msg("foo", (name) => `Hello ${name}!`, name);' ,
156
159
'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
+ }
163
168
) ;
164
169
} ) ;
165
170
166
171
test ( 'msg(fn(string), string)' , ( t ) => {
167
172
checkTransform (
168
173
t ,
169
174
'msg("foo", (name) => `Hello ${name}!`, "World");' ,
170
- '`Hello World!`;' ,
171
- [ ]
175
+ '`Hello World!`;'
172
176
) ;
173
177
} ) ;
174
178
@@ -177,12 +181,14 @@ test('msg(fn(string), string) translated', (t) => {
177
181
t ,
178
182
'msg("foo", (name) => `Hello ${name}!`, "World");' ,
179
183
'`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
+ }
186
192
) ;
187
193
} ) ;
188
194
@@ -191,8 +197,7 @@ test('msg(fn(html), expr)', (t) => {
191
197
t ,
192
198
'const name = "World";' +
193
199
'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>!`;'
196
201
) ;
197
202
} ) ;
198
203
@@ -202,21 +207,22 @@ test('msg(fn(html), expr) translated', (t) => {
202
207
'const name = "World";' +
203
208
'msg("foo", (name) => html`Hello <b>${name}</b>!`, name);' ,
204
209
'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
+ }
211
218
) ;
212
219
} ) ;
213
220
214
221
test ( 'msg(fn(html), string)' , ( t ) => {
215
222
checkTransform (
216
223
t ,
217
224
'msg("foo", (name) => html`Hello <b>${name}</b>!`, "World");' ,
218
- 'html`Hello <b>World</b>!`;' ,
219
- [ ]
225
+ 'html`Hello <b>World</b>!`;'
220
226
) ;
221
227
} ) ;
222
228
@@ -225,21 +231,22 @@ test('msg(fn(html), string) translated', (t) => {
225
231
t ,
226
232
'msg("foo", (name) => html`Hello <b>${name}</b>!`, "World");' ,
227
233
'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
+ }
234
242
) ;
235
243
} ) ;
236
244
237
245
test ( 'msg(fn(html), html)' , ( t ) => {
238
246
checkTransform (
239
247
t ,
240
248
'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>!`;'
243
250
) ;
244
251
} ) ;
245
252
@@ -248,21 +255,22 @@ test('msg(fn(html), html) translated', (t) => {
248
255
t ,
249
256
'msg("foo", (name) => html`Hello <b>${name}</b>!`, html`<i>World</i>`);' ,
250
257
'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
+ }
257
266
) ;
258
267
} ) ;
259
268
260
269
test ( 'msg(fn(string), msg(string))' , ( t ) => {
261
270
checkTransform (
262
271
t ,
263
272
'msg("foo", (name) => `Hello ${name}!`, msg("bar", "World"));' ,
264
- '`Hello World!`;' ,
265
- [ ]
273
+ '`Hello World!`;'
266
274
) ;
267
275
} ) ;
268
276
@@ -271,16 +279,18 @@ test('msg(fn(string), msg(string)) translated', (t) => {
271
279
t ,
272
280
'msg("foo", (name) => `Hello ${name}!`, msg("bar", "World"));' ,
273
281
'`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
+ }
284
294
) ;
285
295
} ) ;
286
296
@@ -292,8 +302,7 @@ test('import * as litLocalize', (t) => {
292
302
litLocalize.msg("foo", "Hello World");
293
303
` ,
294
304
'"Hello World";' ,
295
- [ ] ,
296
- false
305
+ { autoImport : false }
297
306
) ;
298
307
} ) ;
299
308
@@ -305,8 +314,7 @@ test('import {msg as foo}', (t) => {
305
314
foo("foo", "Hello World");
306
315
` ,
307
316
'"Hello World";' ,
308
- [ ] ,
309
- false
317
+ { autoImport : false }
310
318
) ;
311
319
} ) ;
312
320
@@ -317,7 +325,6 @@ test('exclude different msg function', (t) => {
317
325
msg("foo", "Hello World");` ,
318
326
`function msg(id, template) { return template; }
319
327
msg("foo", "Hello World");` ,
320
- [ ] ,
321
- false
328
+ { autoImport : false }
322
329
) ;
323
330
} ) ;
0 commit comments