Fix silent statement/data loss in root render/parse path#38
Conversation
- #15: format_root only emitted a statement when its toplevel_stmt had a `stmt` child, so `BEGIN` (parsed as TransactionStmtLegacy directly under toplevel_stmt) was silently dropped. Fall back to the toplevel_stmt node when there is no `stmt` child so the inner statement still routes through the statement formatter. Adds river fixtures begin and begin_insert_commit. - #16: has_structural_error returned false as soon as any toplevel_stmt existed, so a broken sibling statement never triggered FormatError::Syntax and was silently discarded. Now, when at least one statement parsed, scan for MISSING nodes or ERROR nodes >= 5 bytes (preserving the documented tolerance for small benign ERROR leaves). Adds a smoke test asserting Err(FormatError::Syntax(..)) for `SELECT 1; THIS IS NOT SQL @@@;`. - #17: partially addressed. Standalone comments between/around top-level statements (direct children of source_file) are now preserved verbatim in source order instead of being dropped. Comments embedded within a statement's clauses are still dropped (out of scope for a surgical change). Adds river fixture comment_between_statements. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 6 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR fixes two formatter bugs: ChangesPreserve BEGIN statements and top-level comments
Detect significant parse errors instead of silently dropping statements
Estimated code review effort: 2 (Simple) | ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/formatter/mod.rs (1)
190-194: 📐 Maintainability & Code Quality | 🔵 TrivialMatching on
Styleenum variant violates path coding guideline.
if self.style == Style::PgDump { ... } else { ... }matches directly on theStyleenum. As per coding guidelines,src/formatter/**/*.rsshould "Use StyleConfig flags (boolean/string) in formatter logic instead of matching on Style enum variants." Consider adding apg_dump-style boolean toStyleConfigand branching on that instead, consistent with how the rest of the formatter dispatches onself.config.*flags (e.g.self.config.river,self.config.leading_commasseen elsewhere in the file).♻️ Suggested direction
-if self.style == Style::PgDump { +if self.config.pg_dump { results.push(self.format_pgdump_stmt(stmt)?); } else { results.push(self.format_stmt(stmt)?); }🤖 Prompt for 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. In `@src/formatter/mod.rs` around lines 190 - 194, The formatter dispatch in `format_stmt` is branching directly on `Style::PgDump`, which violates the formatter coding guideline. Update this logic to use a boolean flag on `StyleConfig` (for example a `pg_dump`-style flag) and branch on `self.config.*` instead of matching the `Style` enum, following the same pattern used for other formatter options like `self.config.river` and `self.config.leading_commas`.Source: Coding guidelines
🤖 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.
Nitpick comments:
In `@src/formatter/mod.rs`:
- Around line 190-194: The formatter dispatch in `format_stmt` is branching
directly on `Style::PgDump`, which violates the formatter coding guideline.
Update this logic to use a boolean flag on `StyleConfig` (for example a
`pg_dump`-style flag) and branch on `self.config.*` instead of matching the
`Style` enum, following the same pattern used for other formatter options like
`self.config.river` and `self.config.leading_commas`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: de63b472-7e4e-44e3-8792-ddf7f7c97b24
📒 Files selected for processing (9)
src/formatter/mod.rssrc/lib.rstests/fixtures/river/begin.expectedtests/fixtures/river/begin.sqltests/fixtures/river/begin_insert_commit.expectedtests/fixtures/river/begin_insert_commit.sqltests/fixtures/river/comment_between_statements.expectedtests/fixtures/river/comment_between_statements.sqltests/smoke_test.rs
Address CodeRabbit review feedback: the root statement loop branched directly on `self.style == Style::PgDump`, which violates the formatter coding guideline of reading StyleConfig flags rather than matching on Style variants. Add a `pg_dump` boolean to StyleConfig and branch on `self.config.pg_dump` instead, consistent with the rest of the module. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Addressed in 22486df: added a |
Summary
Three bugs in the root render/parse path (
src/formatter/mod.rs,src/lib.rs) caused statements, whole broken statements, and comments to be silently dropped rather than formatted or reported as errors.Problem
format_rootonly emitted a statement when itstoplevel_stmthad astmtchild. tree-sitter-postgres parsesBEGINas(toplevel_stmt (TransactionStmtLegacy ...))— a direct child, not wrapped instmt— soBEGIN;returnedOk("")and multi-statement input likeBEGIN; INSERT ...; COMMIT;silently lost theBEGIN.has_structural_errorreturnedfalseas soon as anytoplevel_stmtexisted, so a genuinely broken sibling statement never triggeredFormatError::Syntax.SELECT 1; THIS IS NOT SQL @@@;returnedOk("SELECT 1;"), silently discarding the broken input instead of erroring.Solution
toplevel_stmthas nostmtchild, fall back to thetoplevel_stmtnode so the inner statement (e.g.TransactionStmtLegacy) still routes through the statement formatter. New river fixturesbeginandbegin_insert_commit.Err(FormatError::Syntax(..)). Small (< 5 byte) benign ERROR leaves remain tolerated per the documented grammar-limitation behavior; all 70 existing fixtures still pass. New smoke testbroken_statement_errors_instead_of_dropping.source_fileare now preserved verbatim in source order. Note: comments embedded within a statement's clauses are still dropped — faithful intra-statement comment preservation is a larger feature requiring comment handling threaded through every clause formatter, out of scope for a surgical fix. New river fixturecomment_between_statements. This partially addresses SQL comments are silently discarded from formatted output #17.Fixes #15
Fixes #16
🤖 Generated with Claude Code
Summary by CodeRabbit
BEGIN/COMMITduring formatting.