From 156b608d48900b9489bd31aff03189a6c18f9040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 7 Nov 2022 17:50:59 +0100 Subject: [PATCH] Falback to printing inner comments as trailing (#15144) Fixes https://github.com/babel/babel/issues/15138#issuecomment-1304570362 --- packages/babel-generator/src/printer.ts | 31 ++++++++++++----- .../types-with-comments-retainLines/output.js | 4 +-- packages/babel-generator/test/index.js | 34 +++++++++++++++++++ .../fixtures/enum/mix-references/output.js | 2 +- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 9a11289bad77..401e19294d4f 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -787,15 +787,28 @@ class Printer { } _printTrailingComments(node: t.Node, parent?: t.Node, lineOffset?: number) { - const comments = node.trailingComments; - if (!comments?.length) return; - this._printComments( - COMMENT_TYPE.TRAILING, - comments, - node, - parent, - lineOffset, - ); + const { innerComments, trailingComments } = node; + // We print inner comments here, so that if for some reason they couldn't + // be printed in earlier locations they are still printed *somehwere*, + // even if at the end of the node. + if (innerComments?.length) { + this._printComments( + COMMENT_TYPE.TRAILING, + innerComments, + node, + parent, + lineOffset, + ); + } + if (trailingComments?.length) { + this._printComments( + COMMENT_TYPE.TRAILING, + trailingComments, + node, + parent, + lineOffset, + ); + } } _printLeadingComments(node: t.Node, parent: t.Node) { diff --git a/packages/babel-generator/test/fixtures/typescript/types-with-comments-retainLines/output.js b/packages/babel-generator/test/fixtures/typescript/types-with-comments-retainLines/output.js index 1238e16f7699..7647b84b9eda 100644 --- a/packages/babel-generator/test/fixtures/typescript/types-with-comments-retainLines/output.js +++ b/packages/babel-generator/test/fixtures/typescript/types-with-comments-retainLines/output.js @@ -6,8 +6,8 @@ C /* 2 */[ type T2 = U. /* 1 */ -C /* 2 */[]; - +C /* 2 */[] +/* 3 */; type T3 = U. diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index 68e043eb7a70..913325f21df5 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -1133,6 +1133,40 @@ describe("programmatic generation", function () { expect(output).toBe("for ((let)[x];;);"); }); }); + + describe("should print inner comments even if there are no suitable inner locations", () => { + it("atomic node", () => { + const id = t.identifier("foo"); + id.innerComments = [{ type: "CommentBlock", value: "foo" }]; + expect(generate(id).code).toMatchInlineSnapshot(`"foo /*foo*/"`); + }); + + it("node without inner locations", () => { + const expr = t.binaryExpression( + "+", + t.numericLiteral(1), + t.numericLiteral(2), + ); + expr.innerComments = [{ type: "CommentBlock", value: "foo" }]; + expect(generate(expr).code).toMatchInlineSnapshot(`"1 + 2 /*foo*/"`); + }); + + it("comment skipped because of newlines", () => { + const arrow = t.arrowFunctionExpression( + [t.identifier("x"), t.identifier("y")], + t.identifier("z"), + ); + arrow.innerComments = [ + { type: "CommentBlock", value: "foo" }, + { type: "CommentBlock", value: "new\nline" }, + ]; + expect(generate(arrow).code).toMatchInlineSnapshot(` + "(x, y) /*foo*/ => z + /*new + line*/" + `); + }); + }); }); describe("CodeGenerator", function () { diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/mix-references/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/mix-references/output.js index 551584e44474..1f38322e0d4c 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/enum/mix-references/output.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/mix-references/output.js @@ -18,7 +18,7 @@ var Baz; Baz[Baz["b"] = 1] = "b"; Baz[Baz["x"] = Baz.a.toString()] = "x"; })(Baz || (Baz = {})); -var A; +var A; // a refers to A.a (function (A) { A[A["a"] = 0] = "a"; A[A["b"] = (() => {