Skip to content

Commit 44deb84

Browse files
authored
fix(45336): add a blank line before the comment expression to avoid disrupting return statement (#46287)
1 parent 68ff738 commit 44deb84

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

src/compiler/transformers/es2015.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4147,7 +4147,8 @@ namespace ts {
41474147
// NoSubstitutionTemplateLiterals are directly emitted via emitLiteral()
41484148
Debug.assert(node.templateSpans.length !== 0);
41494149

4150-
return node.head.text.length !== 0 || node.templateSpans[0].literal.text.length === 0;
4150+
const span = node.templateSpans[0];
4151+
return node.head.text.length !== 0 || span.literal.text.length === 0 || !!length(getLeadingCommentRangesOfNode(span.expression, currentSourceFile));
41514152
}
41524153

41534154
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [templateStringWithCommentsInArrowFunction.ts]
2+
const a = 1;
3+
const f1 = () =>
4+
`${
5+
// a
6+
a
7+
}a`;
8+
9+
const f2 = () =>
10+
`${
11+
// a
12+
a
13+
}`;
14+
15+
16+
//// [templateStringWithCommentsInArrowFunction.js]
17+
var a = 1;
18+
var f1 = function () {
19+
return "" +
20+
// a
21+
a + "a";
22+
};
23+
var f2 = function () {
24+
return "" +
25+
// a
26+
a;
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts ===
2+
const a = 1;
3+
>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5))
4+
5+
const f1 = () =>
6+
>f1 : Symbol(f1, Decl(templateStringWithCommentsInArrowFunction.ts, 1, 5))
7+
8+
`${
9+
// a
10+
a
11+
>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5))
12+
13+
}a`;
14+
15+
const f2 = () =>
16+
>f2 : Symbol(f2, Decl(templateStringWithCommentsInArrowFunction.ts, 7, 5))
17+
18+
`${
19+
// a
20+
a
21+
>a : Symbol(a, Decl(templateStringWithCommentsInArrowFunction.ts, 0, 5))
22+
23+
}`;
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/es6/templates/templateStringWithCommentsInArrowFunction.ts ===
2+
const a = 1;
3+
>a : 1
4+
>1 : 1
5+
6+
const f1 = () =>
7+
>f1 : () => string
8+
>() => `${ // a a }a` : () => string
9+
10+
`${
11+
>`${ // a a }a` : string
12+
13+
// a
14+
a
15+
>a : 1
16+
17+
}a`;
18+
19+
const f2 = () =>
20+
>f2 : () => string
21+
>() => `${ // a a }` : () => string
22+
23+
`${
24+
>`${ // a a }` : string
25+
26+
// a
27+
a
28+
>a : 1
29+
30+
}`;
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @removeComments: false
2+
3+
const a = 1;
4+
const f1 = () =>
5+
`${
6+
// a
7+
a
8+
}a`;
9+
10+
const f2 = () =>
11+
`${
12+
// a
13+
a
14+
}`;

0 commit comments

Comments
 (0)