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: Pure comments missing parentheses #16104

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 29 additions & 15 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type { TraceMap } from "@jridgewell/trace-mapping";

const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/;
const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;

Expand Down Expand Up @@ -534,16 +533,7 @@ class Printer {

if (chaPost === charCodes.asterisk) {
// This is a block comment

if (PURE_ANNOTATION_RE.test(str.slice(i + 2, len - 2))) {
// We avoid printing newlines after #__PURE__ comments (we treat
// then as unary operators), but we must keep the old
// parenPushNewlineState because, if a newline was forbidden, it is
// still forbidden after the comment.
return;
}

// NOTE: code flow continues from here to after these if/elses
return;
} else if (chaPost !== charCodes.slash) {
// This is neither a block comment, nor a line comment.
// After a normal token, the parentheses aren't needed anymore
Expand Down Expand Up @@ -674,13 +664,37 @@ class Printer {
this._insideAux = node.loc == undefined;
this._maybeAddAuxComment(this._insideAux && !oldInAux);

const shouldPrintParens =
const parenthesized = node.extra?.parenthesized as boolean | undefined;
let shouldPrintParens =
forceParens ||
(format.retainFunctionParens &&
nodeType === "FunctionExpression" &&
node.extra?.parenthesized) ||
(parenthesized &&
format.retainFunctionParens &&
nodeType === "FunctionExpression") ||
needsParens(node, parent, this._printStack);

if (
!shouldPrintParens &&
parenthesized &&
node.leadingComments?.length &&
node.leadingComments[0].type === "CommentBlock"
) {
const parentType = parent?.type;
switch (parentType) {
case "ExpressionStatement":
case "VariableDeclarator":
case "AssignmentExpression":
case "ReturnStatement":
break;
case "CallExpression":
case "OptionalCallExpression":
case "NewExpression":
if (parent.callee !== node) break;
// falls through
default:
shouldPrintParens = true;
}
}

if (shouldPrintParens) {
this.token("(");
this._endsWithInnerRaw = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(/* @__PURE__ */ f()).g();
(/* @__PURE__ */ f())();
(/* @__PURE__ */ f())?.bar;
(/* @__PURE__ */ f())`bar`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
( /* @__PURE__ */f()).g();
( /* @__PURE__ */f())();
( /* @__PURE__ */f())?.bar;
( /* @__PURE__ */f())`bar`;
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (/** @type {!Ele ment} */(
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
));
return /** @type {!Ele ment} */(
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
);
}

const slot = /** @type {!HTMLSlotElement} */(e.target);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (/** @type {!Ele ment} */
return /** @type {!Ele ment} */(
assertType_(assertFn, shouldBeElement, isElement(shouldBeElement), 'Element expected', opt_message)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (/** @type {!Ele ment} */(
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
));
return /** @type {!Ele ment} */(
assertType_(
assertFn,
shouldBeElement,
isElement(shouldBeElement),
'Element expected',
opt_message
)
);
}

const slot = /** @type {!HTMLSlotElement} */(e.target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"sourcesContent": [
"function assertElement(assertFn, shouldBeElement, opt_message) {\n return /** @type {!Ele\tment} */ (\n\t assertType_(\n\t assertFn,\n\t\t shouldBeElement,\n\t\t isElement(shouldBeElement),\n\t\t 'Element expected',\n\t\t opt_message\n\t )\n\t);\n}\n\nconst slot = /** @type {!HTMLSlotElement} */ (e.target);\n\nassertElement(\n /** @type {Element} */ (el),\n);"
],
"mappings": "AAAA,SAASA,aAAaA,CAACC,QAAQ,EAAEC,eAAe,EAAEC,WAAW,EAAE;EAC7D,OAAO,yBAAyB;IAC/BC,WAAW;MACTH,QAAQ;MACTC,eAAe;MACfG,SAAS,CAACH,eAAe,CAAC;MAC1B,kBAAkB;MAClBC;IACD;IACF,CAAC;AACF;;AAEA,MAAMG,IAAI,GAAG,+BAAgC,CAACC,CAAC,CAACC,MAAM,CAAC;;AAEvDR,aAAa;EACX,sBAAuB,CAACS,EAAE;AAC5B,CAAC"
"mappings": "AAAA,SAASA,aAAaA,CAACC,QAAQ,EAAEC,eAAe,EAAEC,WAAW,EAAE;EAC7D,OAAO,wBAAyB;EAC/BC,WAAW;IACTH,QAAQ;IACTC,eAAe;IACfG,SAAS,CAACH,eAAe,CAAC;IAC1B,kBAAkB;IAClBC;EACD;EACF,CAAC;AACF;;AAEA,MAAMG,IAAI,GAAG,+BAAgC,CAACC,CAAC,CAACC,MAAM,CAAC;;AAEvDR,aAAa;EACX,sBAAuB,CAACS,EAAE;AAC5B,CAAC"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function assertElement(assertFn, shouldBeElement, opt_message) {
return (/** @type {!Ele ment} */
return /** @type {!Ele ment} */(
assertType_(
assertFn,
shouldBeElement,
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-generator/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,16 @@ describe("programmatic generation", function () {
`"(x, y): any /*foo*/ => z"`,
);
});

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

Expand Down