From 4a401877266445ae701296de3e30494b735e7d42 Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Thu, 12 Dec 2024 12:19:05 -0600 Subject: [PATCH] Unwrap Short Logical AND/OR Expressions --- formatter/format_test.go | 12 ++++++++++++ formatter/visitors.go | 8 ++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/formatter/format_test.go b/formatter/format_test.go index 20a9ca5..792f7c2 100644 --- a/formatter/format_test.go +++ b/formatter/format_test.go @@ -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{ 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)) diff --git a/formatter/visitors.go b/formatter/visitors.go index 2362bae..f3b7cd3 100644 --- a/formatter/visitors.go +++ b/formatter/visitors.go @@ -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 { @@ -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 {