Skip to content

Commit

Permalink
fix: Pure comments missing parentheses (#16104)
Browse files Browse the repository at this point in the history
* fix

* test

* review
  • Loading branch information
liuxingbaoyu committed Nov 18, 2023
1 parent 2db2e39 commit 4e38aa5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 36 deletions.
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

0 comments on commit 4e38aa5

Please sign in to comment.