Skip to content

Commit

Permalink
Unwrap Short Logical AND/OR Expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Dec 12, 2024
1 parent fb456b2 commit 4a40187
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ func TestStatement(t *testing.T) {
);
}`},

{
`if ((accountIds == null ||
accountIds.isEmpty()) &&
(contactIds == null ||
contactIds.isEmpty())) {
return null;
}`,
`if ((accountIds == null || accountIds.isEmpty()) &&
(contactIds == null || contactIds.isEmpty())) {
return null;
}`},

{
`return new List<CountryZip>{
new CountryZip( new Territory_Zip_Lookup__c( Id = zip.Id, Name = zip.Name, City__c = zip.City__c, State_2_Letter_Code__c = zip.State_2_Letter_Code__c, Country__c = zip.Country__c))
Expand Down
8 changes: 6 additions & 2 deletions formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ func (v *FormatVisitor) VisitCondExpression(ctx *parser.CondExpressionContext) i

func (v *FormatVisitor) VisitLogAndExpression(ctx *parser.LogAndExpressionContext) interface{} {
i := NewChainVisitor()
if i.visitRule(ctx.Expression(0)).(int)+i.visitRule(ctx.Expression(1)).(int) > 2 {
if len(ctx.GetText()) < 40 {
defer restoreWrap(unwrap(v))
} else if i.visitRule(ctx.Expression(0)).(int)+i.visitRule(ctx.Expression(1)).(int) > 2 {
defer restoreWrap(wrap(v))
}
if v.wrap {
Expand All @@ -456,7 +458,9 @@ func (v *FormatVisitor) VisitLogAndExpression(ctx *parser.LogAndExpressionContex

func (v *FormatVisitor) VisitLogOrExpression(ctx *parser.LogOrExpressionContext) interface{} {
i := NewChainVisitor()
if i.visitRule(ctx.Expression(0)).(int)+i.visitRule(ctx.Expression(1)).(int) > 2 {
if len(ctx.GetText()) < 40 {
defer restoreWrap(unwrap(v))
} else if i.visitRule(ctx.Expression(0)).(int)+i.visitRule(ctx.Expression(1)).(int) > 2 {
defer restoreWrap(wrap(v))
}
if v.wrap {
Expand Down

0 comments on commit 4a40187

Please sign in to comment.