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
Break multiline parenthesized logical expressions #11103
base: main
Are you sure you want to change the base?
Conversation
I like this, I havn't seen any tests on |
d442f78
to
7931e42
Compare
Sure. I added some basic tests in |
d01934e
to
e27744e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
e27744e
to
077f63c
Compare
077f63c
to
9dd4548
Compare
Wow, this prints this real-world case exactly as I expect. https://github.com/prettier/prettier/blob/main/src/language-js/print/angular.js#L68-L77 Prettier pr-11103 --parser babel Input: {{
const shouldNotPrintColon =
isNgForOf(node, index, parentNode) ||
(((index === 1 &&
(node.key.name === "then" || node.key.name === "else")) ||
(index === 2 &&
node.key.name === "else" &&
parentNode.body[index - 1].type ===
"NGMicrosyntaxKeyedExpression" &&
parentNode.body[index - 1].key.name === "then")) &&
parentNode.body[0].type === "NGMicrosyntaxExpression");
}} Output: {
{
const shouldNotPrintColon =
isNgForOf(node, index, parentNode) ||
(
(
(
index === 1 &&
(node.key.name === "then" || node.key.name === "else")
) ||
(
index === 2 &&
node.key.name === "else" &&
parentNode.body[index - 1].type ===
"NGMicrosyntaxKeyedExpression" &&
parentNode.body[index - 1].key.name === "then"
)
) &&
parentNode.body[0].type === "NGMicrosyntaxExpression"
);
}
} |
@thorn0 Can you review? |
const extraRendererAttrs = (( | ||
attrs.rendererAttrs && | ||
this.utils.safeParseJsonString(attrs.rendererAttrs) | ||
) || | ||
Object.create(null)) as FieldService.RendererAttributes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is so ugly, but out scope this PR
const extraRendererAttrs = (
(
attrs.rendererAttrs &&
this.utils.safeParseJsonString(attrs.rendererAttrs)
) ||
Object.create(null)
) as FieldService.RendererAttributes;
should be expected.
@fisker This change leads to big diffs. Ask the rest of the team about it. |
Could be OK in a new major version, especially together with #3806. Something like: {
{
const shouldNotPrintColon =
isNgForOf(node, index, parentNode)
|| (
(
(
index === 1
&& (node.key.name === "then" || node.key.name === "else")
)
|| (
index === 2
&& node.key.name === "else"
&& parentNode.body[index - 1].type ===
"NGMicrosyntaxKeyedExpression"
&& parentNode.body[index - 1].key.name === "then"
)
)
&& parentNode.body[0].type === "NGMicrosyntaxExpression"
);
}
} If these changes are combined, there’ll be only one instance of a large diff. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big 👍 for this change.
This PR is against |
It's fine to merge into |
The current formatting is okay as long as there is no lines starting with two or more parens. But #11103 (comment) is really a good example of how unreadable it can be. I wonder if it's possible to change only cases like that one. In the diffs I see a lot of expressions for which the current formatting is totally okay and this change doesn't look like an improvement. E.g.: if (
hasDeselectedButton ||
- (this.state.elementType !== "selection" &&
- this.state.elementType !== "text")
+ (
+ this.state.elementType !== "selection" &&
+ this.state.elementType !== "text"
+ )
) {
return;
} It'd be great to find a way not to touch simple expressions like that and use the proposed expanded formatting only where it's really needed. |
I haven't looked into this PR in depth, but in case it's useful, I wrote a simple algorithm to gauge expression complexity in another branch: https://github.com/rattrayalex/prettier/blob/postfix-ternaries/src/language-js/utils/index.js#L622-L647 |
Description
This PR adds line breaks to multiline parenthesized logical expressions. I'm not 100% sure the changes in this PR are the best way to add line breaks to logical expressions as it imports
needsParams
intosrc/language-js/print/binaryish.js
so it can check if the logcial expression will have params added.Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨
Relates to #9574. Breaks and indents nested conditions if they are multiline.