From f18549109254f5b5598a2af67e495a394ec08bfa Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 12 May 2020 17:42:46 +0000 Subject: [PATCH] Cherry-pick PR #38500 into release-3.9 Component commits: ee3f2ce362 regression(38485): allow using rawText property in processing a tagged template --- src/compiler/transformers/taggedTemplate.ts | 19 ++++++++++-------- src/testRunner/unittests/transform.ts | 20 +++++++++++++++++++ ...orrectly.transformTaggedTemplateLiteral.js | 5 +++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 tests/baselines/reference/transformApi/transformsCorrectly.transformTaggedTemplateLiteral.js diff --git a/src/compiler/transformers/taggedTemplate.ts b/src/compiler/transformers/taggedTemplate.ts index ea3e529b752dd..471fb65812f0d 100644 --- a/src/compiler/transformers/taggedTemplate.ts +++ b/src/compiler/transformers/taggedTemplate.ts @@ -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 diff --git a/src/testRunner/unittests/transform.ts b/src/testRunner/unittests/transform.ts index cce8c92ad4183..54d511233fa49 100644 --- a/src/testRunner/unittests/transform.ts +++ b/src/testRunner/unittests/transform.ts @@ -50,6 +50,15 @@ namespace ts { return (node: SourceFile) => visitNode(node, visitor); } + function createTaggedTemplateLiteral(): Transformer { + return sourceFile => updateSourceFileNode(sourceFile, [ + createStatement( + createTaggedTemplate( + createIdentifier("$tpl"), + createNoSubstitutionTemplateLiteral("foo", "foo"))) + ]); + } + function transformSourceFile(sourceText: string, transformers: TransformerFactory[]) { const transformed = transform(createSourceFile("source.ts", sourceText, ScriptTarget.ES2015), transformers); const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed }, { @@ -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: { diff --git a/tests/baselines/reference/transformApi/transformsCorrectly.transformTaggedTemplateLiteral.js b/tests/baselines/reference/transformApi/transformsCorrectly.transformTaggedTemplateLiteral.js new file mode 100644 index 0000000000000..e693443d134f9 --- /dev/null +++ b/tests/baselines/reference/transformApi/transformsCorrectly.transformTaggedTemplateLiteral.js @@ -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"]));