Skip to content

Commit 832bdce

Browse files
authored
Fixing edge case with null coalescing (#1681)
closes #1573
1 parent 87b1bc8 commit 832bdce

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,62 @@ private static List<Doc> PrintBinaryExpression(SyntaxNode node, PrintingContext
8484
var binaryOnTheRight = binaryExpressionSyntax.Kind() == SyntaxKind.CoalesceExpression;
8585
if (binaryOnTheRight)
8686
{
87-
docs.Add(
88-
Node.Print(binaryExpressionSyntax.Left, context),
89-
Doc.Line,
90-
Token.Print(binaryExpressionSyntax.OperatorToken, context),
91-
" "
92-
);
87+
var chain = 0;
88+
var possibleInvocation = binaryExpressionSyntax.Left;
89+
while (possibleInvocation is not null)
90+
{
91+
if (possibleInvocation is InvocationExpressionSyntax invocationExpression)
92+
{
93+
possibleInvocation = invocationExpression.Expression;
94+
chain++;
95+
}
96+
else if (
97+
possibleInvocation
98+
is MemberAccessExpressionSyntax memberAccessExpressionSyntax
99+
)
100+
{
101+
possibleInvocation = memberAccessExpressionSyntax.Expression;
102+
chain++;
103+
}
104+
else if (
105+
possibleInvocation
106+
is ConditionalAccessExpressionSyntax conditionalAccessExpressionSyntax
107+
)
108+
{
109+
possibleInvocation = conditionalAccessExpressionSyntax.Expression;
110+
chain++;
111+
}
112+
else if (
113+
possibleInvocation
114+
is ElementAccessExpressionSyntax elementAccessExpressionSyntax
115+
)
116+
{
117+
possibleInvocation = elementAccessExpressionSyntax.Expression;
118+
chain++;
119+
}
120+
else
121+
{
122+
possibleInvocation = null;
123+
}
124+
}
125+
var leftDoc = Node.Print(binaryExpressionSyntax.Left, context);
126+
if (chain > 3)
127+
{
128+
docs.Add(
129+
Doc.Group(leftDoc, Doc.Line),
130+
Token.Print(binaryExpressionSyntax.OperatorToken, context),
131+
" "
132+
);
133+
}
134+
else
135+
{
136+
docs.Add(
137+
leftDoc,
138+
Doc.Line,
139+
Token.Print(binaryExpressionSyntax.OperatorToken, context),
140+
" "
141+
);
142+
}
93143
}
94144

95145
var possibleBinary = binaryOnTheRight

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ class TestClass
200200
someLongValue__________________________________________
201201
) ?? someOtherValue;
202202

203+
var x =
204+
someValue
205+
.Property.CallLongMethod_____________________________________()
206+
.CallMethod__________()
207+
?? throw new Exception();
208+
209+
var x =
210+
someValue
211+
.Property.CallLongMethod_____________________________________()
212+
.CallLongMethod___________________________________________________()
213+
?? throw new Exception();
214+
215+
return SomeObject.CallSomeMethod___________()
216+
?? SomeObject.CallSomeMethod___________()
217+
?? new SomeObject();
218+
219+
var storeObject =
220+
entityType.GetSchemaQualifiedTableName()
221+
?? entityType.GetInsertStoredProcedure()?.GetSchemaQualifiedName()
222+
?? entityType.GetDeleteStoredProcedure()?.GetSchemaQualifiedName()
223+
?? entityType.GetUpdateStoredProcedure()?.GetSchemaQualifiedName();
224+
225+
return parameterDescriptor.BindingInfo.Binder
226+
?? Binders.GetBinder(parameterDescriptor.ParameterType);
227+
203228
var notIdealSee355 =
204229
variable
205230
.Replace(someParameter_______________________, '.')

0 commit comments

Comments
 (0)