11import {
2+ softline ,
3+ hardline ,
24 group ,
35 indent ,
6+ ifBreak ,
47 indentIfBreak ,
58 line ,
69 lineSuffixBoundary ,
10+ conditionalGroup ,
711} from "../../document/builders.js" ;
812import { canBreak , cleanDoc , willBreak } from "../../document/utils.js" ;
913import getStringWidth from "../../utils/get-string-width.js" ;
@@ -20,7 +24,7 @@ import {
2024 isNumericLiteral ,
2125 isObjectProperty ,
2226 isStringLiteral ,
23- isUnionType ,
27+ isUnionType
2428} from "../utils/index.js" ;
2529import { shouldInlineLogicalExpression } from "./binaryish.js" ;
2630import { printCallExpression } from "./call-expression.js" ;
@@ -42,11 +46,23 @@ function printAssignment(
4246 const rightDoc = rightPropertyName
4347 ? print ( rightPropertyName , { assignmentLayout : layout } )
4448 : "" ;
49+ const rightNode = path . node [ rightPropertyName ] ;
4550
4651 switch ( layout ) {
4752 // First break after operator, then the sides are broken independently on their own lines
48- case "break-after-operator" :
49- return group ( [ group ( leftDoc ) , operator , group ( indent ( [ line , rightDoc ] ) ) ] ) ;
53+ case "break-after-operator" : {
54+ // // MOD: Wrap multi-line binaryish assignments in parenthesis.
55+ const needsParens = isBinaryish ( rightNode ) ;
56+ const rightGroup = needsParens
57+ ? group ( [
58+ ifBreak ( " (" , " " ) ,
59+ indent ( [ softline , rightDoc ] ) ,
60+ softline ,
61+ ifBreak ( ")" ) ,
62+ ] )
63+ : group ( indent ( [ line , rightDoc ] ) ) ;
64+ return group ( [ group ( leftDoc ) , operator , rightGroup ] ) ;
65+ }
5066
5167 // First break right-hand side, then left-hand side
5268 case "never-break-after-operator" :
@@ -55,13 +71,25 @@ function printAssignment(
5571 // First break right-hand side, then after operator
5672 case "fluid" : {
5773 const groupId = Symbol ( "assignment" ) ;
58- return group ( [
74+ const grouped = group ( [
5975 group ( leftDoc ) ,
6076 operator ,
6177 group ( indent ( line ) , { id : groupId } ) ,
6278 lineSuffixBoundary ,
6379 indentIfBreak ( rightDoc , { groupId } ) ,
6480 ] ) ;
81+ // // MOD: Wrap multi-line binaryish assignments in parenthesis.
82+ return isBinaryish ( rightNode ) &&
83+ ! shouldInlineLogicalExpression ( path . node , path . parent , options )
84+ ? conditionalGroup ( [
85+ grouped ,
86+ group ( [
87+ group ( leftDoc ) ,
88+ operator ,
89+ group ( [ " (" , indent ( [ hardline , rightDoc ] ) , hardline , ")" ] ) ,
90+ ] ) ,
91+ ] )
92+ : grouped
6593 }
6694
6795 case "break-lhs" :
@@ -193,7 +221,10 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) {
193221function shouldBreakAfterOperator ( path , options , print , hasShortKey ) {
194222 const rightNode = path . node ;
195223
196- if ( isBinaryish ( rightNode ) && ! shouldInlineLogicalExpression ( rightNode ) ) {
224+ if (
225+ isBinaryish ( rightNode ) &&
226+ ! shouldInlineLogicalExpression ( rightNode , path . parent , options )
227+ ) {
197228 return true ;
198229 }
199230
@@ -213,7 +244,7 @@ function shouldBreakAfterOperator(path, options, print, hasShortKey) {
213244 case "ConditionalExpression" : {
214245 if ( ! options . experimentalTernaries ) {
215246 const { test } = rightNode ;
216- return isBinaryish ( test ) && ! shouldInlineLogicalExpression ( test ) ;
247+ return isBinaryish ( test ) && ! shouldInlineLogicalExpression ( test , rightNode , options ) ;
217248 }
218249 const { consequent, alternate } = rightNode ;
219250 return (
0 commit comments