Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 86dce41

Browse filesBrowse files
authored
Move synthetic comments from arrow body expressions to return statement (microsoft#24135)
1 parent 9484653 commit 86dce41
Copy full SHA for 86dce41

6 files changed

+45Lines changed: 45 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/factory.ts‎

Copy file name to clipboardExpand all lines: src/compiler/factory.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,15 @@ namespace ts {
29002900
return setSyntheticTrailingComments(node, append<SynthesizedComment>(getSyntheticTrailingComments(node), { kind, pos: -1, end: -1, hasTrailingNewLine, text }));
29012901
}
29022902

2903+
export function moveSyntheticComments<T extends Node>(node: T, original: Node): T {
2904+
setSyntheticLeadingComments(node, getSyntheticLeadingComments(original));
2905+
setSyntheticTrailingComments(node, getSyntheticTrailingComments(original));
2906+
const emit = getOrCreateEmitNode(original);
2907+
emit.leadingComments = undefined;
2908+
emit.trailingComments = undefined;
2909+
return node;
2910+
}
2911+
29032912
/**
29042913
* Gets the constant value to emit for an expression.
29052914
*/
Collapse file

‎src/compiler/transformers/es2015.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/es2015.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,7 @@ namespace ts {
18851885
const expression = visitNode(body, visitor, isExpression);
18861886
const returnStatement = createReturn(expression);
18871887
setTextRange(returnStatement, body);
1888+
moveSyntheticComments(returnStatement, body);
18881889
setEmitFlags(returnStatement, EmitFlags.NoTokenSourceMaps | EmitFlags.NoTrailingSourceMap | EmitFlags.NoTrailingComments);
18891890
statements.push(returnStatement);
18901891

Collapse file

‎src/harness/unittests/transform.ts‎

Copy file name to clipboardExpand all lines: src/harness/unittests/transform.ts
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,35 @@ namespace ts {
270270
program.emit(program.getSourceFiles()[1], (p, s, bom) => host.writeFile(p, s, bom), /*cancellationToken*/ undefined, /*onlyDts*/ true, opts.transformers);
271271
return fs.readFileSync("/.src/index.d.ts").toString();
272272
}
273+
274+
// https://github.com/Microsoft/TypeScript/issues/24096
275+
testBaseline("transformAddCommentToArrowReturnValue", () => {
276+
return transpileModule(`const foo = () =>
277+
void 0
278+
`, {
279+
transformers: {
280+
before: [addSyntheticComment],
281+
},
282+
compilerOptions: {
283+
target: ScriptTarget.ES5,
284+
newLine: NewLineKind.CarriageReturnLineFeed,
285+
}
286+
}).outputText;
287+
288+
function addSyntheticComment(context: TransformationContext) {
289+
return (sourceFile: SourceFile): SourceFile => {
290+
return visitNode(sourceFile, rootTransform, isSourceFile);
291+
};
292+
function rootTransform<T extends Node>(node: T): VisitResult<T> {
293+
if (isVoidExpression(node)) {
294+
setEmitFlags(node, EmitFlags.NoLeadingComments);
295+
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.SingleLineCommentTrivia, text: "// comment!", pos: -1, end: -1, hasTrailingNewLine: true }]);
296+
return node;
297+
}
298+
return visitEachChild(node, rootTransform, context);
299+
}
300+
}
301+
});
273302
});
274303
}
275304

Collapse file

‎tests/baselines/reference/api/tsserverlibrary.d.ts‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/api/tsserverlibrary.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,7 @@ declare namespace ts {
38603860
function getSyntheticTrailingComments(node: Node): SynthesizedComment[] | undefined;
38613861
function setSyntheticTrailingComments<T extends Node>(node: T, comments: SynthesizedComment[]): T;
38623862
function addSyntheticTrailingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean): T;
3863+
function moveSyntheticComments<T extends Node>(node: T, original: Node): T;
38633864
/**
38643865
* Gets the constant value to emit for an expression.
38653866
*/
Collapse file

‎tests/baselines/reference/api/typescript.d.ts‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/api/typescript.d.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,7 @@ declare namespace ts {
38603860
function getSyntheticTrailingComments(node: Node): SynthesizedComment[] | undefined;
38613861
function setSyntheticTrailingComments<T extends Node>(node: T, comments: SynthesizedComment[]): T;
38623862
function addSyntheticTrailingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean): T;
3863+
function moveSyntheticComments<T extends Node>(node: T, original: Node): T;
38633864
/**
38643865
* Gets the constant value to emit for an expression.
38653866
*/
Collapse file
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var foo = function () {
2+
//// comment!
3+
return void 0;
4+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.