fix: keep a block comment leading a later statement on its own line - #961
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe formatter now tracks whether each statement follows another statement, passes that state into ChangesStatement layout formatting
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
A block comment before a statement that follows another is formatted inline on the first pass but moved onto its own line on the second, so formatting isn't idempotent:
Same shape as #952, one level up. That fix made a block comment standalone when
layout.isAtStartOfLine(), butformatAstgives each statement its ownLayout, so for a leading comment the layout is still empty andisAtStartOfLine()falls through toreturn false. Statements are joined with newlines, so every statement after the first does start on a line — the second pass sees that newline as the comment'sprecedingWhitespaceand flips the decision.Layoutnow takes whether its statement is preceded by others, and an empty layout reportsisAtStartOfLine()accordingly. The first statement is unaffected, so a leading comment on the whole query still formats inline as before.Found by fuzzing
format(format(x)) === format(x)with comments injected at every token boundary: 14,427 variants over 14 dialects, 10 failures before, 0 after. It reproduces in 11 of 21 dialects and for any keyword-only statement —COMMIT,ROLLBACK,BEGIN,END,GO,USE,VACUUM,ANALYZE,SAVEPOINT. Statements with clauses are stable because the following keyword forces the newline anyway.5,861 tests pass; tsc, eslint and prettier clean.