Skip to content

fix(hook): keep case terminators intact when rewriting (#3197) - #4130

Merged
KuSh merged 2 commits into
developfrom
lex/02-case-terminators
Oct 9, 2026
Merged

KuSh merged 2 commits into
developfrom
lex/02-case-terminators

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

;;, ;& and ;;& each terminate a case arm, but the lexer emitted a
separate Operator(";") per character. rewrite_compound splits on every
operator and rebuilds the text around it, so the second half of a terminator
came back as its own empty segment and the arm was re-emitted with the two
halves apart:

case x in a) echo 1;; esac    ->  case x in a) echo 1; ; esac
case x in a) echo 1;& esac    ->  case x in a) echo 1;  & esac

The first is a bash syntax error, so any command mixing a case statement
with a rewritable command never ran at all. The second is worse than an
error: ; & is valid, and turns a fall-through into a background job.

The ; arm now folds ;;, ;& and ;;& into one operator token, matching
bash's own maximal munch — ;;; is ;; then ;, and ;&& is ;& then
&, which bash's syntax errors name explicitly. The emitter's existing
no-padding branch for ; covers every terminator starting with one, so the
terminator itself is reproduced verbatim instead of being padded to ;;.

Whitespace after a terminator is not preserved: that branch appends a space
when the operator is not the last token, so echo 1;;esac comes back as
echo 1;; esac. Both parse identically, and the behaviour predates this
change — it applied to bare ; already.

No existing test changes: the old two-token form produced an empty segment
between the halves, which the segmenters already dropped.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all — 3852 passing, clippy clean, at this commit rather than only at the tip of the stack
  • rtk rewrite 'ls /tmp; case x in a) echo 1;; *) echo 2;; esac' leaves ;; intact, and ;& is no longer split into ; &

Part of stack #4139 (#4129 → #4138). Based on #4129. Each commit gates green on its own, so the stack is bisectable.

🤖 Generated with Claude Code

@KuSh
KuSh added this pull request to stack #4139 September 19, 2026 13:31
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

This PR fixes the shell command lexer so that case statement terminators (;;, ;&, ;;&) are tokenized as single operators instead of being split into a ; plus a separate operator. Previously, splitting them caused the command rewriter to reconstruct invalid syntax (; ;) or unintended semantics (; &) when rewriting commands containing case blocks. New unit tests verify both the tokenizer output and end-to-end rewrite behavior for all three terminator forms.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh marked this pull request as ready for review September 19, 2026 16:22
@KuSh
KuSh force-pushed the lex/02-case-terminators branch from f0fe86b to 4173c33 Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/02-case-terminators branch from 4173c33 to 8cc2de9 Compare September 25, 2026 22:55
@KuSh
KuSh force-pushed the lex/02-case-terminators branch 2 times, most recently from ef75dea to 6a9fbaf Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/02-case-terminators branch from 6a9fbaf to ec2696c Compare September 26, 2026 17:55
@KuSh
KuSh force-pushed the lex/02-case-terminators branch 3 times, most recently from 150f73c to 935d467 Compare October 2, 2026 15:04
Base automatically changed from lex/01-gate-security to develop October 2, 2026 15:04
@KuSh
KuSh force-pushed the lex/02-case-terminators branch 2 times, most recently from 1dc3d6d to 57f52b8 Compare October 2, 2026 15:07
@KuSh
KuSh force-pushed the lex/02-case-terminators branch 2 times, most recently from f64f271 to d0ba176 Compare October 7, 2026 07:16

@aeppling aeppling left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Nit: discover/README.md:24 and the split_on_operators / split_for_permissions doc comments still list only &&, ||, ; as split points. Please add ;;, ;&, ;;&.

@KuSh
KuSh force-pushed the lex/02-case-terminators branch from d0ba176 to dde81ea Compare October 9, 2026 11:07
@CLAassistant

CLAassistant commented Oct 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

KuSh and others added 2 commits October 9, 2026 18:19
`;;`, `;&` and `;;&` each terminate a `case` arm, but the lexer emitted a
separate `Operator(";")` per character. `rewrite_compound` splits on every
operator and rebuilds the text around it, so the second half of a terminator
came back as its own empty segment and the arm was re-emitted with the two
halves apart:

    case x in a) echo 1;; esac    ->  case x in a) echo 1; ; esac
    case x in a) echo 1;& esac    ->  case x in a) echo 1;  & esac

The first is a bash syntax error, so any command mixing a `case` statement
with a rewritable command never ran at all. The second is worse than an
error: `; &` is valid, and turns a fall-through into a background job.

The `;` arm now folds `;;`, `;&` and `;;&` into one operator token, matching
bash's own maximal munch — `;;;` is `;;` then `;`, and `;&&` is `;&` then
`&`, which bash's syntax errors name explicitly. The emitter's existing
no-padding branch for `;` covers every terminator starting with one, so the
terminator itself is reproduced verbatim instead of being padded to ` ;; `.

Whitespace after a terminator is not preserved: that branch appends a space
when the operator is not the last token, so `echo 1;;esac` comes back as
`echo 1;; esac`. Both parse identically, and the behaviour predates this
change — it applied to bare `;` already.

No existing test changes: the old two-token form produced an empty segment
between the halves, which the segmenters already dropped.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Review follow-up: the segmenter doc comments and the discover README
still named only `&&`, `||` and `;` as split points; `;;`, `;&` and
`;;&` are now one operator token each and split the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the lex/02-case-terminators branch from b06d637 to b295eb7 Compare October 9, 2026 16:27
@KuSh
KuSh merged commit e12ca86 into develop Oct 9, 2026
18 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 9, 2026
@KuSh
KuSh deleted the lex/02-case-terminators branch October 9, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants