Improve comment formatting for logical expression in unary expression (#16593)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-08-24 08:22:43 +09:00
1 parent 870f8f926c
commit 92253a3f40
4 files changed
+458

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
#### Improve comment formatting for logical expression in unary expression (#16593 by @sosukesuzuki)
<!-- prettier-ignore -->
```jsx
// Input
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
// Prettier stable
!(
(
cond1 || // foo
cond2 || // bar
cond3
) // baz
);
// Prettier main
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
```
@@ -90,6 +90,7 @@ function handleEndOfLineComment(context) {
handleBreakAndContinueStatementComments,
handleSwitchDefaultCaseComments,
handleLastUnionElementInExpression,
handleLastBinaryOperatorOperand,
].some((fn) => fn(context));
}
@@ -1021,6 +1022,44 @@ function handleCommentsInDestructuringPattern({
}
}
function handleLastBinaryOperatorOperand({
comment,
precedingNode,
enclosingNode,
followingNode,
}) {
// "baz" should be a trailing comment of `cond3`:
//
// !(
// cond1 || // foo
// cond2 || // bar
// cond3 // baz
// );
if (
!followingNode &&
enclosingNode?.type === "UnaryExpression" &&
(precedingNode?.type === "LogicalExpression" ||
precedingNode?.type === "BinaryExpression")
) {
// !(
// (cond1 || cond2) // foo
// );
const isMultilineExpression =
enclosingNode.argument.loc?.start.line !==
precedingNode.right.loc.start.line;
const isSingleLineComment =
isLineComment(comment) || comment.loc.start.line === comment.loc.end.line;
const isSameLineComment =
comment.loc.start.line === precedingNode.right.loc.start.line;
if (isMultilineExpression && isSingleLineComment && isSameLineComment) {
addTrailingComment(precedingNode.right, comment);
return true;
}
}
return false;
}
/**
* @param {Node} node
* @returns {boolean}
+71
View File
@@ -0,0 +1,71 @@
!(
x || // foo
y || // bar
z /*
* comment
*/
)
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
!(
a && // alpha
b || // bravo
c // charlie
);
!(
x || // foo
(y && z) // bar
);
!(
a || // first condition
(b || c) // second condition
|| d // third condition
);
void(
p && // first
q || // second
r && // third
s // fourth
);
void(
cond1 || // foo
cond2 && // bar
cond3 || // baz
cond4 // qux
);
!(
(cond1 && cond2) || // multi-cond1
(cond3 || cond4) // multi-cond2
);
!(
(cond1 || cond2) && // complex-cond1
(cond3 || cond4) || // complex-cond2
cond5 // complex-cond3
);
void(
(condA || condB) && // test A
(condC || condD) || // test B
condE // test C
);
void(
(x || y) && ( // nested
z || w) // comment for w
);
!(
a && ( // begin nested
b || c) // end nested
);
@@ -1,5 +1,326 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`15661.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
!(
x || // foo
y || // bar
z /*
* comment
*/
)
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
!(
a && // alpha
b || // bravo
c // charlie
);
!(
x || // foo
(y && z) // bar
);
!(
a || // first condition
(b || c) // second condition
|| d // third condition
);
void(
p && // first
q || // second
r && // third
s // fourth
);
void(
cond1 || // foo
cond2 && // bar
cond3 || // baz
cond4 // qux
);
!(
(cond1 && cond2) || // multi-cond1
(cond3 || cond4) // multi-cond2
);
!(
(cond1 || cond2) && // complex-cond1
(cond3 || cond4) || // complex-cond2
cond5 // complex-cond3
);
void(
(condA || condB) && // test A
(condC || condD) || // test B
condE // test C
);
void(
(x || y) && ( // nested
z || w) // comment for w
);
!(
a && ( // begin nested
b || c) // end nested
);
=====================================output=====================================
!(
(
x || // foo
y || // bar
z
) /*
* comment
*/
)
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
)
!(
(a && // alpha
b) || // bravo
c // charlie
)
!(
x || // foo
(y && z) // bar
)
!(
a || // first condition
b ||
c || // second condition
d // third condition
)
void (
(
(p && // first
q) || // second
(r && // third
s)
) // fourth
)
void (
cond1 || // foo
(cond2 && // bar
cond3) || // baz
cond4 // qux
)
!(
(cond1 && cond2) || // multi-cond1
cond3 ||
cond4 // multi-cond2
)
!(
((cond1 || cond2) && // complex-cond1
(cond3 || cond4)) || // complex-cond2
cond5 // complex-cond3
)
void (
((condA || condB) && // test A
(condC || condD)) || // test B
condE // test C
)
void (
(x || y) && // nested
(z || w) // comment for w
)
!(
a && // begin nested
(b || c) // end nested
)
================================================================================
`;
exports[`15661.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
!(
x || // foo
y || // bar
z /*
* comment
*/
)
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
!(
a && // alpha
b || // bravo
c // charlie
);
!(
x || // foo
(y && z) // bar
);
!(
a || // first condition
(b || c) // second condition
|| d // third condition
);
void(
p && // first
q || // second
r && // third
s // fourth
);
void(
cond1 || // foo
cond2 && // bar
cond3 || // baz
cond4 // qux
);
!(
(cond1 && cond2) || // multi-cond1
(cond3 || cond4) // multi-cond2
);
!(
(cond1 || cond2) && // complex-cond1
(cond3 || cond4) || // complex-cond2
cond5 // complex-cond3
);
void(
(condA || condB) && // test A
(condC || condD) || // test B
condE // test C
);
void(
(x || y) && ( // nested
z || w) // comment for w
);
!(
a && ( // begin nested
b || c) // end nested
);
=====================================output=====================================
!(
(
x || // foo
y || // bar
z
) /*
* comment
*/
);
!(
cond1 || // foo
cond2 || // bar
cond3 // baz
);
!(
(a && // alpha
b) || // bravo
c // charlie
);
!(
x || // foo
(y && z) // bar
);
!(
a || // first condition
b ||
c || // second condition
d // third condition
);
void (
(
(p && // first
q) || // second
(r && // third
s)
) // fourth
);
void (
cond1 || // foo
(cond2 && // bar
cond3) || // baz
cond4 // qux
);
!(
(cond1 && cond2) || // multi-cond1
cond3 ||
cond4 // multi-cond2
);
!(
((cond1 || cond2) && // complex-cond1
(cond3 || cond4)) || // complex-cond2
cond5 // complex-cond3
);
void (
((condA || condB) && // test A
(condC || condD)) || // test B
condE // test C
);
void (
(x || y) && // nested
(z || w) // comment for w
);
!(
a && // begin nested
(b || c) // end nested
);
================================================================================
`;
exports[`arrow.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]