Skip to content

Commit 1f77a19

Browse files
committed
Address comments from PR #39
1 parent 6147f69 commit 1f77a19

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

src/outputters/runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {applyPatches, Patches} from '../patches';
1414
import {Locale} from '../locales';
1515
import {Config} from '../config';
1616
import {KnownError} from '../error';
17-
import {escapeStringLiteral} from '../typescript';
17+
import {escapeStringToEmbedInTemplateLiteral} from '../typescript';
1818
import * as fs from 'fs';
1919
import * as pathLib from 'path';
2020

@@ -444,7 +444,7 @@ function makeMessageString(
444444
const fragments = [];
445445
for (const content of contents) {
446446
if (typeof content === 'string') {
447-
fragments.push(escapeStringLiteral(content));
447+
fragments.push(escapeStringToEmbedInTemplateLiteral(content));
448448
} else {
449449
fragments.push(content.untranslatable);
450450
}

src/outputters/transform.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {Message} from '../messages';
1313
import {Locale} from '../locales';
1414
import {Config} from '../config';
1515
import * as ts from 'typescript';
16-
import {isLitExpression, isMsgCall, isStaticString} from '../program-analysis';
16+
import {isLitTemplate, isMsgCall, isStaticString} from '../program-analysis';
1717
import {KnownError} from '../error';
18-
import {escapeStringLiteral} from '../typescript';
18+
import {escapeStringToEmbedInTemplateLiteral} from '../typescript';
1919
import * as pathLib from 'path';
2020

2121
/**
@@ -92,7 +92,7 @@ class Transformer {
9292
if (isMsgCall(node)) {
9393
return this.replaceMsgCall(node);
9494
}
95-
if (isLitExpression(node)) {
95+
if (isLitTemplate(node)) {
9696
// If an html-tagged template literal embeds a msg call, we want to
9797
// collapse the result of that msg call into the parent template.
9898
return tagLit(
@@ -128,7 +128,7 @@ class Transformer {
128128
!(
129129
ts.isStringLiteral(arg1) ||
130130
ts.isTemplateLiteral(arg1) ||
131-
isLitExpression(arg1) ||
131+
isLitTemplate(arg1) ||
132132
ts.isArrowFunction(arg1)
133133
)
134134
) {
@@ -149,19 +149,19 @@ class Transformer {
149149
if (
150150
!ts.isStringLiteral(arg1.body) &&
151151
!ts.isTemplateLiteral(arg1.body) &&
152-
!isLitExpression(arg1.body)
152+
!isLitTemplate(arg1.body)
153153
) {
154154
throw new KnownError(
155155
'Expected function body to be a template or string'
156156
);
157157
}
158-
if (isLitExpression(arg1.body)) {
158+
if (isLitTemplate(arg1.body)) {
159159
isLitTagged = true;
160160
template = arg1.body.template;
161161
} else {
162162
template = arg1.body;
163163
}
164-
} else if (isLitExpression(arg1)) {
164+
} else if (isLitTemplate(arg1)) {
165165
isLitTagged = true;
166166
template = arg1.template;
167167
} else {
@@ -176,7 +176,7 @@ class Transformer {
176176
const templateLiteralBody = translation.contents
177177
.map((content) =>
178178
typeof content === 'string'
179-
? escapeStringLiteral(content)
179+
? escapeStringToEmbedInTemplateLiteral(content)
180180
: content.untranslatable
181181
)
182182
.join('');
@@ -299,7 +299,7 @@ class Transformer {
299299
fragments.push(expression.text);
300300
} else if (ts.isTemplateLiteral(expression)) {
301301
fragments.push(...this.recursivelyFlattenTemplate(expression, false));
302-
} else if (isLit && isLitExpression(expression)) {
302+
} else if (isLit && isLitTemplate(expression)) {
303303
fragments.push(
304304
...this.recursivelyFlattenTemplate(expression.template, true)
305305
);

src/program-analysis.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function extractMsg(
107107
};
108108
}
109109

110-
if (isLitExpression(contentsArg)) {
110+
if (isLitTemplate(contentsArg)) {
111111
if (ts.isNoSubstitutionTemplateLiteral(contentsArg.template)) {
112112
// E.g. msg('foo', html`bar <b>baz</b>`)
113113
return {
@@ -186,7 +186,7 @@ function functionTemplate(
186186
if (
187187
!ts.isTemplateExpression(body) &&
188188
!ts.isNoSubstitutionTemplateLiteral(body) &&
189-
!isLitExpression(body)
189+
!isLitTemplate(body)
190190
) {
191191
return createDiagnostic(
192192
file,
@@ -195,7 +195,7 @@ function functionTemplate(
195195
`or a lit-html template, without braces`
196196
);
197197
}
198-
const template = isLitExpression(body) ? body.template : body;
198+
const template = isLitTemplate(body) ? body.template : body;
199199
const parts: Array<string | {identifier: string}> = [];
200200
if (ts.isTemplateExpression(template)) {
201201
const spans = template.templateSpans;
@@ -220,8 +220,8 @@ function functionTemplate(
220220
// A NoSubstitutionTemplateLiteral. No spans.
221221
parts.push(template.text);
222222
}
223-
const isLitTemplate = isLitExpression(body);
224-
const contents = isLitTemplate
223+
const isLit = isLitTemplate(body);
224+
const contents = isLit
225225
? replaceExpressionsAndHtmlWithPlaceholders(parts)
226226
: parts.map((part) =>
227227
typeof part === 'string'
@@ -235,7 +235,7 @@ function functionTemplate(
235235
file,
236236
descStack: descStack.map((desc) => desc.text),
237237
params,
238-
isLitTemplate,
238+
isLitTemplate: isLit,
239239
};
240240
}
241241

@@ -480,7 +480,7 @@ export function isStaticString(
480480
/**
481481
* E.g. html`foo` or html`foo${bar}`
482482
*/
483-
export function isLitExpression(
483+
export function isLitTemplate(
484484
node: ts.Node
485485
): node is ts.TaggedTemplateExpression {
486486
return (

src/typescript.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ export function printDiagnostics(diagnostics: ts.Diagnostic[]): void {
8181
* Escape a string such that it can be safely embedded in a JavaScript template
8282
* literal (backtick string).
8383
*/
84-
export function escapeStringLiteral(unescaped: string): string {
84+
export function escapeStringToEmbedInTemplateLiteral(
85+
unescaped: string
86+
): string {
8587
return unescaped
8688
.replace(/\\/g, `\\\\`)
8789
.replace(/`/g, '\\`')

0 commit comments

Comments
 (0)