fix(format): align cmdsub reformatter with bash canonical form#49
Merged
Conversation
Fix the six canonical-reformatter drifts tracked in issue #40: 1. **varfd `{fd}>` dropped** — add a `varfd: Option<String>` field to `NodeKind::Redirect`, thread it through `build_redirect` via a new `parse_redirect_with_varfd` entry, and emit the `{name}` prefix in `format_redirect`. The sexp layer destructures `Redirect` with `..` so top-level output is unchanged (matches existing Parable corpus in `tests/parable/26_variable_fd.tests`). `build_redirect` hit the 60-line limit with the new field, so its heredoc branch is factored into `build_heredoc_redirect`. 2. **`select` rendered as raw S-expression** — add a `Select` arm to `format_node` and a shared `format_for_or_select` helper that both `format_for` and `format_select` delegate to. 3. **Function body not wrapped in implicit braces** — rewrite `format_function_body` to always emit `{ \n … \n }`, unwrapping an inner `BraceGroup` when present and wrapping a bare body (e.g. a `Subshell` from `function f ( cmd )`) otherwise. 4. **Statement separators inside function body** — add `format_list_block` for the canonical multi-line form. Only `;` and `&` wrap to a new indented line; `&&` and `||` stay inline because they form a single logical command. 5. **`<<\EOF` quoted-delimiter marker lost** — honor `HereDoc.quoted` in both `format_redirect` and `format_command_with_heredoc_pipe` via a shared `write_heredoc_delimiter` helper. Bash always emits `<<'EOF'` for any quoted form (`<<'EOF'`, `<<"EOF"`, `<<\EOF`) — verified against `bash-oracle --dump-ast`. Three tests in `tests/parable/12_command_substitution.tests` that expected the wrong strip-quoting form are updated to the correct form. As a side effect, `heredoc_in_cmdsub 1` (tracked under #39) also newly passes — removed from `KNOWN_ORACLE_FAILURES` too. New coverage: - parser unit tests pinning the `varfd` AST field (populated vs. `None`) - 3 corpus regressions in `tests/parable/12_command_substitution.tests`: tab-stripped quoted heredoc in cmdsub, function body with `&&` separator, varfd close-fd `{fd}>&-` Closes #40. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
mpecan
pushed a commit
that referenced
this pull request
Apr 19, 2026
🤖 I have created a release *beep* *boop* --- ## [0.2.0](rable-v0.1.15...rable-v0.2.0) (2026-04-18) ### ⚠ BREAKING CHANGES * tighten lexer API surface and relocate WordSpan to ast ([#70](#70)) ### Bug Fixes * **format:** align cmdsub reformatter with bash canonical form ([#49](#49)) ([c7a4411](c7a4411)) * **lexer:** accept sloppy heredoc terminator in cmdsub mode ([#50](#50)) ([40f394f](40f394f)) * **lexer:** backticks opaque when content is invalid ([#71](#71)) ([e72166f](e72166f)), closes [#38](#38) * **lexer:** disable reserved-word recognition after assignment words ([#44](#44)) ([42e1fc0](42e1fc0)) * **lexer:** stop treating ]] and unbalanced [...] as special outside conditionals ([#45](#45)) ([4bf5a5c](4bf5a5c)) * **parser:** fall back from (( … )) arith to nested subshells ([#48](#48)) ([1437f00](1437f00)) ### Code Refactoring * **format:** introduce Formatter struct ([#65](#65)) ([d965a8f](d965a8f)) * **lexer:** drop Result<Token> wrapper from operator readers ([#62](#62)) ([d52a841](d52a841)) * **lexer:** split read_word_token into classify + advance + dispatch helpers ([#63](#63)) ([3ba09f5](3ba09f5)) * **parser:** extract fill_heredoc_contents visitor helpers ([#68](#68)) ([40e6165](40e6165)) * **parser:** extract helpers from three oversize parsers ([#69](#69)) ([25d0762](25d0762)) * **sexp:** dispatch NodeKind Display to per-category helpers ([#66](#66)) ([44b0330](44b0330)) * **sexp:** table-drive ANSI-C escape dispatch ([#67](#67)) ([91a5267](91a5267)) * tighten lexer API surface and relocate WordSpan to ast ([#70](#70)) ([5171d01](5171d01)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: repository-butler[bot] <166800726+repository-butler[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the six canonical-reformatter drifts in
$(…)body formatting tracked by #40.{fd}>droppedRedirect.varfd: Option<String>AST field +parse_redirect_with_varfd+ formatter emits{name}prefixselectrendered as raw S-exprformat_selectarm, shares layout helper withformat_forformat_function_bodyrewritten to always emit{ … }format_list_blockhelper — wraps on;/&but keeps&&/`<<\EOFquoted-delimiter lostHereDoc.quotedin both regular and heredoc-pipe pathsbuild_redirecthit the 60-line limit with the new field, so its heredoc branch was factored intobuild_heredoc_redirect.format_forandformat_selectdelegate to a sharedformat_for_or_selecthelper to remove duplication.Parable corpus correction
Three tests in
tests/parable/12_command_substitution.testsexpected the reformatter to strip heredoc delimiter quoting (<<'EOF'→<<EOF). Parable was wrong about bash's canonical form — verified directly againstbash-oracle --dump-ast:Updated the 3 goldens to match the correct quoted form.
Bonus fix
The heredoc-quoting fix also newly passes
heredoc_in_cmdsub 1(tracked under #39), removed fromKNOWN_ORACLE_FAILURESwith an attribution comment.Test plan
cargo fmtcargo clippy --all-targets -- -D warnings— no warningscargo test— 240 passed, 0 failed (185 lib + 54 integration + 1 tree-sitter compare)cmdsub_reformat Ncases now pass inoracle_bash_valid_divergencesheredoc_in_cmdsub 1also newly passesRedirect.varfdpopulated vs.Nonetests/parable/12_command_substitution.tests: tab-stripped quoted heredoc,&&in function body, varfd close-fdCloses #40.