-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Break lines before binary operators #7111
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
Changes from all commits
971fedd
ef9dd44
673de03
77b63d9
c5546a8
a727d66
5b93320
33050fe
6b4bf3f
32de8ac
8c6ed25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #### Break lines before binary operators (#7111 by @btmills) | ||
|
|
||
| This is implemented behind the `--experimental-operator-position <start|end>` flag. | ||
|
|
||
| When binary expressions wrap lines, `start` prints the operators at the start of new lines. Placing binary operators at the beginning of wrapped lines can make the operators more prominent and easier to scan. | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```jsx | ||
| // Input | ||
| var a = Math.random() * (yRange * (1 - minVerticalFraction)) + minVerticalFraction * yRange - offset; | ||
|
|
||
| // Default behavior | ||
| var a = | ||
| Math.random() * (yRange * (1 - minVerticalFraction)) + | ||
| minVerticalFraction * yRange - | ||
| offset; | ||
|
|
||
| // `experimentalOperatorPosition: true` | ||
| var a = | ||
| Math.random() * (yRange * (1 - minVerticalFraction)) | ||
| + minVerticalFraction * yRange | ||
| - offset; | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,22 @@ const options = { | |
| oppositeDescription: | ||
| "Do not print semicolons, except at the beginning of lines which may need them.", | ||
| }, | ||
| experimentalOperatorPosition: { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The options defined in this file appeared to follow alphabetical order with a few deviations, so I put this next to and alphabetically before |
||
| category: CATEGORY_JAVASCRIPT, | ||
| type: "choice", | ||
| default: "end", | ||
| description: "Where to print operators when binary expressions wrap lines.", | ||
| choices: [ | ||
| { | ||
| value: "start", | ||
| description: "Print operators at the start of new lines.", | ||
| }, | ||
| { | ||
| value: "end", | ||
| description: "Print operators at the end of previous lines.", | ||
| }, | ||
| ], | ||
| }, | ||
| experimentalTernaries: { | ||
| category: CATEGORY_JAVASCRIPT, | ||
| type: "boolean", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,13 @@ import { | |
| line, | ||
| softline, | ||
| } from "../../document/builders.js"; | ||
| import { DOC_TYPE_FILL, DOC_TYPE_GROUP } from "../../document/constants.js"; | ||
| import { cleanDoc } from "../../document/utils.js"; | ||
| import { | ||
| DOC_TYPE_ARRAY, | ||
| DOC_TYPE_FILL, | ||
| DOC_TYPE_GROUP, | ||
| DOC_TYPE_LABEL, | ||
| } from "../../document/constants.js"; | ||
| import { cleanDoc, getDocType } from "../../document/utils.js"; | ||
| import { printComments } from "../../main/comments/print.js"; | ||
| import { | ||
| CommentCheckFlags, | ||
|
|
@@ -23,6 +28,7 @@ import { | |
| isObjectProperty, | ||
| shouldFlatten, | ||
| } from "../utils/index.js"; | ||
| import isTypeCastComment from "../utils/is-type-cast-comment.js"; | ||
|
|
||
| /** @import {Doc} from "../../document/builders.js" */ | ||
|
|
||
|
|
@@ -231,6 +237,14 @@ function printBinaryishExpressions( | |
| node.type === "NGPipeExpression" || | ||
| isVueFilterSequenceExpression(path, options)) && | ||
| !hasLeadingOwnLineComment(options.originalText, node.right); | ||
| const hasTypeCastComment = hasComment( | ||
| node.right, | ||
| CommentCheckFlags.Leading, | ||
| isTypeCastComment, | ||
| ); | ||
| const commentBeforeOperator = | ||
| !hasTypeCastComment && | ||
| hasLeadingOwnLineComment(options.originalText, node.right); | ||
|
|
||
| const operator = node.type === "NGPipeExpression" ? "|" : node.operator; | ||
| const rightSuffix = | ||
|
|
@@ -267,13 +281,28 @@ function printBinaryishExpressions( | |
| "right", | ||
| ) | ||
| : print("right"); | ||
| right = [ | ||
| lineBeforeOperator ? line : "", | ||
| operator, | ||
| lineBeforeOperator ? " " : line, | ||
| rightContent, | ||
| rightSuffix, | ||
| ]; | ||
| if (options.experimentalOperatorPosition === "start") { | ||
| let comment = ""; | ||
| if (commentBeforeOperator) { | ||
| switch (getDocType(rightContent)) { | ||
| case DOC_TYPE_ARRAY: | ||
| comment = rightContent.splice(0, 1)[0]; | ||
| break; | ||
| case DOC_TYPE_LABEL: | ||
| comment = rightContent.contents.splice(0, 1)[0]; | ||
| break; | ||
| } | ||
| } | ||
| right = [line, comment, operator, " ", rightContent, rightSuffix]; | ||
| } else { | ||
| right = [ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assignment to |
||
| lineBeforeOperator ? line : "", | ||
| operator, | ||
| lineBeforeOperator ? " " : line, | ||
| rightContent, | ||
| rightSuffix, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| // If there's only a single binary expression, we want to create a group | ||
|
|
@@ -289,11 +318,15 @@ function printBinaryishExpressions( | |
| parent.type !== node.type && | ||
| node.left.type !== node.type && | ||
| node.right.type !== node.type); | ||
| if (shouldGroup) { | ||
| right = group(right, { shouldBreak }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line came from the inline expression |
||
| } | ||
|
|
||
| parts.push( | ||
| lineBeforeOperator ? "" : " ", | ||
| shouldGroup ? group(right, { shouldBreak }) : right, | ||
| ); | ||
| if (options.experimentalOperatorPosition === "start") { | ||
| parts.push(shouldInline || commentBeforeOperator ? " " : "", right); | ||
| } else { | ||
| parts.push(lineBeforeOperator ? "" : " ", right); | ||
| } | ||
|
|
||
| // The root comments are already printed, but we need to manually print | ||
| // the other ones since we don't call the normal print on BinaryExpression, | ||
|
|
||
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.
Again, I welcome feedback on wording here. I used the Experimental Ternaries section above as a starting point, though I omitted the line about "before it becomes the default behavior" because I don't know whether that's the intent for this flag.