Skip to content

Commit b8c9b05

Browse files
committed
Address comments from PR #41
1 parent 9d30b42 commit b8c9b05

File tree

2 files changed

+99
-94
lines changed

2 files changed

+99
-94
lines changed

src/outputters/transform.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,16 @@ class Transformer {
341341
const moduleSymbol = this.typeChecker.getSymbolAtLocation(
342342
node.moduleSpecifier
343343
);
344-
if (!moduleSymbol) {
344+
if (!moduleSymbol || !moduleSymbol.exports) {
345345
return false;
346346
}
347-
// TODO(aomarks) Is there a better way to reliably identify the lit-localize
348-
// module that doesn't require this cast? We could export a const with a
349-
// known name and then look through `exports`, but it doesn't seem good to
350-
// polute the module like that.
351-
const file = (moduleSymbol.valueDeclaration as unknown) as {
352-
identifiers: Map<string, unknown>;
353-
};
354-
for (const id of file.identifiers.keys()) {
355-
if (id === '_LIT_LOCALIZE_MSG_') {
347+
const exports = moduleSymbol.exports.values();
348+
for (const xport of exports as typeof exports & {
349+
[Symbol.iterator](): Iterator<ts.Symbol>;
350+
}) {
351+
const type = this.typeChecker.getTypeAtLocation(xport.valueDeclaration);
352+
const props = this.typeChecker.getPropertiesOfType(type);
353+
if (props.some((prop) => prop.escapedName === '_LIT_LOCALIZE_MSG_')) {
356354
return true;
357355
}
358356
}

src/tests/transform.unit.test.ts

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7680
test('unchanged const', (t) => {
7781
const src = 'const foo = "foo";';
78-
checkTransform(t, src, src, []);
82+
checkTransform(t, src, src);
7983
});
8084

8185
test('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

8791
test('msg(string)', (t) => {
88-
checkTransform(t, 'msg("foo", "Hello World");', '"Hello World";', []);
92+
checkTransform(t, 'msg("foo", "Hello World");', '"Hello World";');
8993
});
9094

9195
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+
});
9599
});
96100

97101
test('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

115118
test('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

143147
test('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

166171
test('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

214221
test('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

237245
test('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

260269
test('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

Comments
 (0)