Skip to content

Commit

Permalink
fix: Miss parentheses after line break (#16122)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Nov 28, 2023
1 parent 7505475 commit ce60d99
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,14 @@ class Printer {

let val;
if (isBlockComment) {
const { _parenPushNewlineState } = this;
if (
_parenPushNewlineState?.printed === false &&
HAS_NEWLINE.test(comment.value)
) {
this.token("(");
_parenPushNewlineState.printed = true;
}
val = `/*${comment.value}*/`;
if (this.format.indent.adjustMultilineComment) {
const offset = comment.loc?.start.column;
Expand Down
26 changes: 26 additions & 0 deletions packages/babel-generator/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,32 @@ describe("programmatic generation", function () {
);"
`);
});

it("multi-line leading comment after return compact", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
compact: true,
}).code,
).toMatchInlineSnapshot(`
"return(/*new
line*/val);"
`);
});

it("multi-line leading comment after return concise", () => {
const val = t.identifier("val");
val.leadingComments = [{ type: "CommentBlock", value: "new\nline" }];
expect(
generate(t.returnStatement(val), {
concise: true,
}).code,
).toMatchInlineSnapshot(`
"return (/*new
line*/ val );"
`);
});
});
});

Expand Down

0 comments on commit ce60d99

Please sign in to comment.