Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Missing parentheses after line break #16122

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,15 @@ class Printer {

let val;
if (isBlockComment) {
const { _parenPushNewlineState } = this;
if (
_parenPushNewlineState?.printed === false &&
lastCharCode != charCodes.lineFeed &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case in which _parenPushNewlineState?.printed is false, lastCharCode is charCodes.lineFeed and we do not need to print the parenthesis?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't understand what you meant, if lastCharCode is charCodes.lineFeed then it will not continue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant: if we deleted the lastCharCode != charCodes.lineFeed && check, there would be any difference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thank you! There is no difference between them. When lastCharCode is charCodes.lineFeed, _parenPushNewlineState?.printed must be true

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