Skip to content

fix(45336): removeComments: false generates incorrect code in specific case #46287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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))

}`;

Original file line number Diff line number Diff line change
@@ -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

}`;

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @removeComments: false

const a = 1;
const f1 = () =>
`${
// a
a
}a`;

const f2 = () =>
`${
// a
a
}`;