Skip to content

Commit

Permalink
Merge pull request #38506 from typescript-bot/pick/38500/release-3.9
Browse files Browse the repository at this point in the history
馃 Pick PR #38500 (regression(38485): Unable to specif...) into release-3.9
  • Loading branch information
DanielRosenwasser committed May 19, 2020
2 parents 8037e26 + f185491 commit 7225809
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/compiler/transformers/taggedTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ namespace ts {
*
* @param node The ES6 template literal.
*/
function getRawLiteral(node: LiteralLikeNode, currentSourceFile: SourceFile) {
function getRawLiteral(node: TemplateLiteralLikeNode, currentSourceFile: SourceFile) {
// Find original source text, since we need to emit the raw strings of the tagged template.
// The raw strings contain the (escaped) strings of what the user wrote.
// Examples: `\n` is converted to "\\n", a template string with a newline to "\n".
let text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node);
let text = node.rawText;
if (text === undefined) {
text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node);

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

// Newline normalization:
// ES6 Spec 11.8.6.1 - Static Semantics of TV's and TRV's
Expand Down
20 changes: 20 additions & 0 deletions src/testRunner/unittests/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ namespace ts {
return (node: SourceFile) => visitNode(node, visitor);
}

function createTaggedTemplateLiteral(): Transformer<SourceFile> {
return sourceFile => updateSourceFileNode(sourceFile, [
createStatement(
createTaggedTemplate(
createIdentifier("$tpl"),
createNoSubstitutionTemplateLiteral("foo", "foo")))
]);
}

function transformSourceFile(sourceText: string, transformers: TransformerFactory<SourceFile>[]) {
const transformed = transform(createSourceFile("source.ts", sourceText, ScriptTarget.ES2015), transformers);
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, {
Expand Down Expand Up @@ -120,6 +129,17 @@ namespace ts {
}).outputText;
});

testBaseline("transformTaggedTemplateLiteral", () => {
return transpileModule("", {
transformers: {
before: [createTaggedTemplateLiteral],
},
compilerOptions: {
target: ScriptTarget.ES5
}
}).outputText;
});

testBaseline("issue27854", () => {
return transpileModule(`oldName<{ a: string; }>\` ... \`;`, {
transformers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
$tpl(__makeTemplateObject(["foo"], ["foo"]));

0 comments on commit 7225809

Please sign in to comment.