Skip to content

Commit ee3f2ce

Browse files
committed
regression(38485): allow using rawText property in processing a tagged template
1 parent f4872eb commit ee3f2ce

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/compiler/transformers/taggedTemplate.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,21 @@ namespace ts {
7171
*
7272
* @param node The ES6 template literal.
7373
*/
74-
function getRawLiteral(node: LiteralLikeNode, currentSourceFile: SourceFile) {
74+
function getRawLiteral(node: TemplateLiteralLikeNode, currentSourceFile: SourceFile) {
7575
// Find original source text, since we need to emit the raw strings of the tagged template.
7676
// The raw strings contain the (escaped) strings of what the user wrote.
7777
// Examples: `\n` is converted to "\\n", a template string with a newline to "\n".
78-
let text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node);
78+
let text = node.rawText;
79+
if (text === undefined) {
80+
text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node);
7981

80-
// text contains the original source, it will also contain quotes ("`"), dolar signs and braces ("${" and "}"),
81-
// thus we need to remove those characters.
82-
// First template piece starts with "`", others with "}"
83-
// Last template piece ends with "`", others with "${"
84-
const isLast = node.kind === SyntaxKind.NoSubstitutionTemplateLiteral || node.kind === SyntaxKind.TemplateTail;
85-
text = text.substring(1, text.length - (isLast ? 1 : 2));
82+
// text contains the original source, it will also contain quotes ("`"), dolar signs and braces ("${" and "}"),
83+
// thus we need to remove those characters.
84+
// First template piece starts with "`", others with "}"
85+
// Last template piece ends with "`", others with "${"
86+
const isLast = node.kind === SyntaxKind.NoSubstitutionTemplateLiteral || node.kind === SyntaxKind.TemplateTail;
87+
text = text.substring(1, text.length - (isLast ? 1 : 2));
88+
}
8689

8790
// Newline normalization:
8891
// ES6 Spec 11.8.6.1 - Static Semantics of TV's and TRV's

src/testRunner/unittests/transform.ts

+20
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ namespace ts {
5050
return (node: SourceFile) => visitNode(node, visitor);
5151
}
5252

53+
function createTaggedTemplateLiteral(): Transformer<SourceFile> {
54+
return sourceFile => updateSourceFileNode(sourceFile, [
55+
createStatement(
56+
createTaggedTemplate(
57+
createIdentifier("$tpl"),
58+
createNoSubstitutionTemplateLiteral("foo", "foo")))
59+
]);
60+
}
61+
5362
function transformSourceFile(sourceText: string, transformers: TransformerFactory<SourceFile>[]) {
5463
const transformed = transform(createSourceFile("source.ts", sourceText, ScriptTarget.ES2015), transformers);
5564
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, {
@@ -120,6 +129,17 @@ namespace ts {
120129
}).outputText;
121130
});
122131

132+
testBaseline("transformTaggedTemplateLiteral", () => {
133+
return transpileModule("", {
134+
transformers: {
135+
before: [createTaggedTemplateLiteral],
136+
},
137+
compilerOptions: {
138+
target: ScriptTarget.ES5
139+
}
140+
}).outputText;
141+
});
142+
123143
testBaseline("issue27854", () => {
124144
return transpileModule(`oldName<{ a: string; }>\` ... \`;`, {
125145
transformers: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
2+
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
3+
return cooked;
4+
};
5+
$tpl(__makeTemplateObject(["foo"], ["foo"]));

0 commit comments

Comments
 (0)