Skip to content

fix: remove extra space after BETWEEN _ AND _ expression#959

Open
sarathfrancis90 wants to merge 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-between-extra-space
Open

fix: remove extra space after BETWEEN _ AND _ expression#959
sarathfrancis90 wants to merge 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-between-extra-space

Conversation

@sarathfrancis90

@sarathfrancis90 sarathfrancis90 commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

I noticed the formatter emits two spaces after a BETWEEN predicate whenever something follows it on the same line. With default options:

format('SELECT CASE WHEN foo BETWEEN 1 AND 2 THEN 3 END');

gives WHEN foo BETWEEN 1 AND 2 THEN 3. Same for logicalOperatorNewline: 'after' (b BETWEEN 1 AND 2 AND) and for b BETWEEN 1 AND 2 IS TRUE.

The cause is in formatBetweenPredicate: the sub-expression for the upper bound already ends with a trailing space, and the method then adds another WS.SPACE on top. It usually goes unnoticed because with the default logicalOperatorNewline: 'before' the following WS.NEWLINE trims the doubled space away. The lower bound already normalizes this with WS.NO_SPACE, WS.SPACE, so I did the same for the upper bound.

Two existing expectations had the extra space baked in (test/features/between.ts and test/features/case.ts); both are updated to the intended single space.

Tested with pnpm run check (typecheck, prettier, eslint, full jest suite) — all green. I also re-ran a format/re-format idempotency sweep over the repo's own test queries across every dialect and the option matrix; nothing changed apart from the removed space.

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.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The formatter changes whitespace token emission after BETWEEN. Existing expectations are updated for arithmetic and CASE expressions, and a regression test verifies formatting inside a CASE WHEN clause.

Changes

BETWEEN whitespace correction

Layer / File(s) Summary
Update BETWEEN spacing and regression coverage
src/formatter/ExpressionFormatter.ts, test/features/between.ts, test/features/case.ts
formatBetweenPredicate now emits WS.NO_SPACE followed by WS.SPACE; tests validate corrected spacing for arithmetic and CASE expressions.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: nene

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main fix: removing an extra space in BETWEEN predicate formatting.
Description check ✅ Passed The description directly explains the formatting bug, the code change, and the updated tests for the same issue.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant