From eb5bb5ea3682ebc8b9af56f9e4ea065fdfc9a1db Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Sat, 18 Jul 2026 03:33:54 -0400 Subject: [PATCH] fix: remove extra space after BETWEEN _ AND _ expression The whitespace between the upper bound of a BETWEEN predicate and whatever follows it was doubled, because the sub-expression already emits a trailing space and formatBetweenPredicate added another one on top. It shows up with default options, e.g. SELECT CASE WHEN foo BETWEEN 1 AND 2 THEN 3 END produced "WHEN foo BETWEEN 1 AND 2 THEN 3". Normalize the trailing whitespace the same way the lower bound already does, with NO_SPACE followed by SPACE. --- src/formatter/ExpressionFormatter.ts | 2 +- test/features/between.ts | 11 ++++++++++- test/features/case.ts | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/formatter/ExpressionFormatter.ts b/src/formatter/ExpressionFormatter.ts index 4a917db031..98d00d0b0e 100644 --- a/src/formatter/ExpressionFormatter.ts +++ b/src/formatter/ExpressionFormatter.ts @@ -222,7 +222,7 @@ export default class ExpressionFormatter { this.layout = this.formatSubExpression(node.expr1); this.layout.add(WS.NO_SPACE, WS.SPACE, this.showNonTabularKw(node.andKw), WS.SPACE); this.layout = this.formatSubExpression(node.expr2); - this.layout.add(WS.SPACE); + this.layout.add(WS.NO_SPACE, WS.SPACE); } private formatCaseExpression(node: CaseExpressionNode) { diff --git a/test/features/between.ts b/test/features/between.ts index 88075c9e21..c8a68e3440 100644 --- a/test/features/between.ts +++ b/test/features/between.ts @@ -19,7 +19,7 @@ export default function supportsBetween(format: FormatFn) { it('supports complex expressions inside BETWEEN', () => { // Not ideal, but better than crashing - expect(format('foo BETWEEN 1+2 AND 3+4')).toBe('foo BETWEEN 1 + 2 AND 3 + 4'); + expect(format('foo BETWEEN 1+2 AND 3+4')).toBe('foo BETWEEN 1 + 2 AND 3 + 4'); }); it('supports CASE inside BETWEEN', () => { @@ -30,6 +30,15 @@ export default function supportsBetween(format: FormatFn) { `); }); + it('does not add extra space after the BETWEEN expression', () => { + expect(format('SELECT CASE WHEN foo BETWEEN 1 AND 2 THEN 3 END')).toBe(dedent` + SELECT + CASE + WHEN foo BETWEEN 1 AND 2 THEN 3 + END + `); + }); + // Regression test for #534 it('supports AND after BETWEEN', () => { expect(format('SELECT foo BETWEEN 1 AND 2 AND x > 10')).toBe(dedent` diff --git a/test/features/case.ts b/test/features/case.ts index c710cdb3b6..03cea8c088 100644 --- a/test/features/case.ts +++ b/test/features/case.ts @@ -209,7 +209,7 @@ export default function supportsCase(format: FormatFn) { expect(result).toBe(dedent` SELECT CASE - WHEN x1 BETWEEN 1 AND 12 THEN '' + WHEN x1 BETWEEN 1 AND 12 THEN '' END c1; `); });