diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts index 52963c530ab77..07d79f50fe21a 100644 --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -4147,7 +4147,8 @@ namespace ts { // NoSubstitutionTemplateLiterals are directly emitted via emitLiteral() Debug.assert(node.templateSpans.length !== 0); - return node.head.text.length !== 0 || node.templateSpans[0].literal.text.length === 0; + const span = node.templateSpans[0]; + return node.head.text.length !== 0 || span.literal.text.length === 0 || !!length(getLeadingCommentRangesOfNode(span.expression, currentSourceFile)); } /** diff --git a/tests/baselines/reference/templateStringWithCommentsInArrowFunction.js b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.js new file mode 100644 index 0000000000000..df8a47410f554 --- /dev/null +++ b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.js @@ -0,0 +1,27 @@ +//// [templateStringWithCommentsInArrowFunction.ts] +const a = 1; +const f1 = () => + `${ + // a + a + }a`; + +const f2 = () => + `${ + // a + a + }`; + + +//// [templateStringWithCommentsInArrowFunction.js] +var a = 1; +var f1 = function () { + return "" + + // a + a + "a"; +}; +var f2 = function () { + return "" + + // a + a; +}; diff --git a/tests/baselines/reference/templateStringWithCommentsInArrowFunction.symbols b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.symbols new file mode 100644 index 0000000000000..1b9b224345964 --- /dev/null +++ b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.symbols @@ -0,0 +1,24 @@ +=== tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts === +const a = 1; +>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5)) + +const f1 = () => +>f1 : Symbol(f1, Decl(templateStringWithCommentsInArrowFunction.ts, 1, 5)) + + `${ + // a + a +>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5)) + + }a`; + +const f2 = () => +>f2 : Symbol(f2, Decl(templateStringWithCommentsInArrowFunction.ts, 7, 5)) + + `${ + // a + a +>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5)) + + }`; + diff --git a/tests/baselines/reference/templateStringWithCommentsInArrowFunction.types b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.types new file mode 100644 index 0000000000000..7f2b2198c602b --- /dev/null +++ b/tests/baselines/reference/templateStringWithCommentsInArrowFunction.types @@ -0,0 +1,31 @@ +=== tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts === +const a = 1; +>a : 1 +>1 : 1 + +const f1 = () => +>f1 : () => string +>() => `${ // a a }a` : () => string + + `${ +>`${ // a a }a` : string + + // a + a +>a : 1 + + }a`; + +const f2 = () => +>f2 : () => string +>() => `${ // a a }` : () => string + + `${ +>`${ // a a }` : string + + // a + a +>a : 1 + + }`; + diff --git a/tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts b/tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts new file mode 100644 index 0000000000000..c344bea1fb734 --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts @@ -0,0 +1,14 @@ +// @removeComments: false + +const a = 1; +const f1 = () => + `${ + // a + a + }a`; + +const f2 = () => + `${ + // a + a + }`;