Repository navigation
Preserve trailing line comment on defer statements in baml fmt (B-629) - #3919
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
⏭️ Performance benchmarks were skippedPerf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to To run them on this PR, do any of the following, then push a commit (or re-run CI):
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors return and unknown expression formatting to use token-anchored verbatim spans, centralizes wrapped arm printing, and adds regression tests for comment preservation around ChangesVerbatim expression formatting and arm wrapping
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@baml_language/crates/baml_fmt/src/ast/expressions.rs`:
- Around line 387-388: The token range handling for Expression::Return still
relies on elem.text_range(), so trailing comments on a braceless return arm can
be skipped. Update the leftmost_token()/rightmost_token() logic in
expressions.rs to use the underlying token span from the Return variant’s range,
similar to how Unknown uses unknown.last_token, so the anchor is based on the
actual tokens rather than the full text range.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f02a1ef6-a40c-4de3-9cb5-2017c1216bbe
📒 Files selected for processing (2)
baml_language/crates/baml_fmt/src/ast/expressions.rsbaml_language/crates/baml_fmt/src/lib.rs
Binary size checks passed✅ 7 passed
Generated by |
A defer statement is an unmodeled node held as Expression::Unknown and printed verbatim. It reported the whole node span as both its leftmost and rightmost token, but the trivia classifier keys leading/trailing comments to individual token ranges. The whole-node span never matched the closing-brace token key, so a trailing line comment on a defer statement was silently dropped, while an identical comment on a normal statement survived. Carry the node's true first/last non-trivia token ranges on Expression::Unknown so leading/trailing comments attach and emit correctly. Print only the content range (first token start to last token end), which also excludes any leading comment the CST folds into the node span (previously re-printed verbatim at the wrong indent). Fixes B-629
…629)
CodeRabbit follow-up: Expression::Return had the same comment-loss bug as
Expression::Unknown — it reported the whole node span as its leftmost/rightmost
token. A braceless return arm with no trailing comma delegates the arm's
rightmost token to the return body, so a trailing comment (anchored to the
return value's last token) never matched and was silently dropped.
- Back both Return and Unknown with a shared VerbatimSpan carrying the node's
true first/last non-trivia token ranges (renamed from UnknownExpr).
- Factor the three arm block-wrap sites into print_wrapped_arm_body. For a
braceless return it emits 'return ...;' but leaves the trailing trivia to the
arm level, so a same-line comment is preserved once (after the wrapped '},'),
not split from its ';' nor duplicated when the arm has no comma. The existing
{ return ...; } wrapping and the catch_arm_return snapshot are unchanged.
- Add print_standalone_leading_and_body printer helper.
- Drop the rustdoc intra-doc link to the private content_range method, which
failed 'cargo doc' under -D warnings.
Regression tests: trailing comment on a braceless return arm survives (comma
and no-comma), plus a catch-arm round-trip guard.
Fixes B-629
028cc9c to
5242ac0
Compare
What
baml fmtsilently deleted the trailing line-comment on adefer { … }statement, while an identical trailing comment on a normal statement survived. This is source-data loss, distinct from the known fmt over-expansion cluster (B-575/600/602/606).Repro (before):
After
baml fmt, the// THIS comment on a defer linewas gone; the normal-line comment stayed.Why
A
deferstatement (DEFER_STMT) is not modeled by the formatter's strong AST, so it is held asExpression::Unknownand printed verbatim.Unknownreported the whole node span as both itsleftmost_tokenandrightmost_token. But the trivia classifier keys leading/trailing comments to individual token ranges (the comment afterdefer { … }is anchored to the closing}token). A whole-node span never matches a per-token key, soget_trailing_for_elementreturned nothing and the comment was dropped.The whole-node span also begins inside leading trivia the CST folds into the node, which is why a leading comment on the line before a
deferwas re-printed verbatim at its original (wrong) indent.How
Expression::Unknownnow carries the node's true first and last non-trivia token ranges instead of the whole span:leftmost_token/rightmost_tokenreturn those exact token ranges, so leading/trailing comments attach and emit like any normal statement.content_range(first-token start … last-token end), which excludes any leading/trailing trivia the CST folded into the node — so the leading comment is emitted once (via the trivia path) at the correct indent, not duplicated.This is a general fix for all unmodeled nodes (
defer,throw,await,spawn,x.as<T>), and it keeps them printed verbatim, so it does not touch the over-expansion behavior.Tests
Added
defer_comment_testsinbaml_fmt: trailing-comment preservation (ticket repro), trailing comment on a last-statementdefer, and leading-comment re-indentation — each asserts idempotency. Fullbaml_testssuite passes with zero formatter-snapshot changes.Fixes B-629
Summary by CodeRabbit
Bug Fixes
deferstatements, preserving both leading and trailing//comments and aligning them to canonical indentation/spacing.returnbodies in match/catch arms so semicolons and inline comments remain correctly attached.Tests
defercomment formatting and idempotency.