Skip to content

Commit 7018480

Browse files
committed
Fix unstable trailing comment on a parenthesized arrow chain
`x = (a) => ((b) => {} /* comment */)` printed the comment before the semicolon on the first pass and after it on the second, so the file never settled. The parentheses are redundant, so they are dropped. That moves the comment next to the statement's semicolon, where the next format attaches it to the ExpressionStatement instead of to the arrow it was written against. The same thing happens with a nested assignment, which is already handled by attaching the comment to the statement up front; this extends that to a chain that passes through arrow bodies.
1 parent 0283c88 commit 7018480

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎src/language-js/comments/handle-comments.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,10 @@ function getEnclosingAssignmentChainExpressionStatement(node, ancestors) {
12041204
let child = node;
12051205

12061206
for (const ancestor of ancestors) {
1207-
if (ancestor.type === "AssignmentExpression" && ancestor.right === child) {
1207+
if (
1208+
(ancestor.type === "AssignmentExpression" && ancestor.right === child) ||
1209+
(ancestor.type === "ArrowFunctionExpression" && ancestor.body === child)
1210+
) {
12081211
child = ancestor;
12091212
continue;
12101213
}
@@ -1255,10 +1258,16 @@ function handleParenthesizedExpressionTrailingComment({
12551258
// `a = (b = c /* comment */);` drops the parentheses, so the comment ends
12561259
// up trailing the whole statement anyway. Attach it there right away
12571260
// instead of leaving it on `c` for the next format to move.
1261+
const isArrowBody =
1262+
enclosingNode.type === "ArrowFunctionExpression" &&
1263+
enclosingNode.body === precedingNode &&
1264+
precedingNode.type === "ArrowFunctionExpression";
1265+
12581266
if (
1259-
isAssignment &&
1260-
enclosingNode.type === "AssignmentExpression" &&
1261-
enclosingNode.right === precedingNode
1267+
(isAssignment &&
1268+
enclosingNode.type === "AssignmentExpression" &&
1269+
enclosingNode.right === precedingNode) ||
1270+
isArrowBody
12621271
) {
12631272
const expressionStatement =
12641273
getEnclosingAssignmentChainExpressionStatement(

‎tests/config/format-test/failed-format-tests.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const unstableTests = new Map(
2828
(options) => options.objectWrap !== "collapse",
2929
],
3030
"typescript/call/callee-comments.ts",
31-
"js/arrows/arrow-chain-with-trailing-comments.js",
3231
"typescript/as/comments/18160.ts",
3332
"typescript/union/consistent-with-flow/single-type.ts",
3433
].map((fixture) => {

‎tests/format/js/arrows/__snapshots__/format.test.js.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ x2 =
268268
askTrovenaBeenaDependsRowans3,
269269
) => {
270270
c();
271-
} /* ! */; // KABOOM
271+
}; /* ! */ // KABOOM
272272
273273
================================================================================
274274
`;
@@ -305,7 +305,7 @@ x2 =
305305
askTrovenaBeenaDependsRowans3,
306306
) => {
307307
c();
308-
} /* ! */; // KABOOM
308+
}; /* ! */ // KABOOM
309309
310310
================================================================================
311311
`;

0 commit comments

Comments
 (0)