Skip to content

Commit c399230

Browse files
authored
Retain comments inside return statements (#17557)
* Retain comments inside return statements by including the return keyword in the parse tree * Revert "Retain comments inside return statements by including the return keyword in the parse tree" This reverts commit 5d2142e. * Readd test * Function for handling printing comments on a token
1 parent 73f941d commit c399230

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

src/compiler/comments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts {
88
setWriter(writer: EmitTextWriter): void;
99
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
1010
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
11-
emitTrailingCommentsOfPosition(pos: number): void;
11+
emitTrailingCommentsOfPosition(pos: number, prefixSpace?: boolean): void;
1212
emitLeadingCommentsOfPosition(pos: number): void;
1313
}
1414

@@ -306,7 +306,7 @@ namespace ts {
306306
}
307307
}
308308

309-
function emitTrailingCommentsOfPosition(pos: number) {
309+
function emitTrailingCommentsOfPosition(pos: number, prefixSpace?: boolean) {
310310
if (disabled) {
311311
return;
312312
}
@@ -315,7 +315,7 @@ namespace ts {
315315
performance.mark("beforeEmitTrailingCommentsOfPosition");
316316
}
317317

318-
forEachTrailingCommentToEmit(pos, emitTrailingCommentOfPosition);
318+
forEachTrailingCommentToEmit(pos, prefixSpace ? emitTrailingComment : emitTrailingCommentOfPosition);
319319

320320
if (extendedDiagnostics) {
321321
performance.measure("commentTime", "beforeEmitTrailingCommentsOfPosition");

src/compiler/emitter.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,20 @@ namespace ts {
15721572
write(";");
15731573
}
15741574

1575+
function emitTokenWithComment(token: SyntaxKind, pos: number, contextNode?: Node) {
1576+
const node = contextNode && getParseTreeNode(contextNode);
1577+
if (node && node.kind === contextNode.kind) {
1578+
pos = skipTrivia(currentSourceFile.text, pos);
1579+
}
1580+
pos = writeToken(token, pos, /*contextNode*/ contextNode);
1581+
if (node && node.kind === contextNode.kind) {
1582+
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ true);
1583+
}
1584+
return pos;
1585+
}
1586+
15751587
function emitReturnStatement(node: ReturnStatement) {
1576-
writeToken(SyntaxKind.ReturnKeyword, node.pos, /*contextNode*/ node);
1588+
emitTokenWithComment(SyntaxKind.ReturnKeyword, node.pos, /*contextNode*/ node);
15771589
emitExpressionWithPrefix(" ", node.expression);
15781590
write(";");
15791591
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [jsdocCastCommentEmit.ts]
2+
// allowJs: true
3+
// checkJs: true
4+
// outDir: out/
5+
// filename: input.js
6+
function f() {
7+
return /* @type {number} */ 42;
8+
}
9+
10+
//// [jsdocCastCommentEmit.js]
11+
// allowJs: true
12+
// checkJs: true
13+
// outDir: out/
14+
// filename: input.js
15+
function f() {
16+
return /* @type {number} */ 42;
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/jsdocCastCommentEmit.ts ===
2+
// allowJs: true
3+
// checkJs: true
4+
// outDir: out/
5+
// filename: input.js
6+
function f() {
7+
>f : Symbol(f, Decl(jsdocCastCommentEmit.ts, 0, 0))
8+
9+
return /* @type {number} */ 42;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/jsdocCastCommentEmit.ts ===
2+
// allowJs: true
3+
// checkJs: true
4+
// outDir: out/
5+
// filename: input.js
6+
function f() {
7+
>f : () => number
8+
9+
return /* @type {number} */ 42;
10+
>42 : 42
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// allowJs: true
2+
// checkJs: true
3+
// outDir: out/
4+
// filename: input.js
5+
function f() {
6+
return /* @type {number} */ 42;
7+
}

0 commit comments

Comments
 (0)