feat(pattern): support multi-word alternation syntax in command position#94
feat(pattern): support multi-word alternation syntax in command position#94
Conversation
Single-word alternation (`ast-grep|sg scan *`) was supported but users needed separate rules for commands with different prefixes (e.g., `npx prettier *` and `prettier *`). Multi-word alternation allows combining these into a single rule using quoted alternatives: `"npx prettier"|"bunx prettier"|prettier *` Implementation approach: multi-word alternation patterns are expanded into multiple independent Pattern instances at parse time, so the matcher requires no changes. The lexer detects quoted-string `|` continuations and emits a new `MultiWordAlternation` token, which the parser's `parse_multi()` expands into separate patterns. The rule engine iterates over expanded patterns for each rule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello @fohte, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the flexibility of pattern matching by introducing support for multi-word alternations in the command position. This allows users to define a single, more powerful rule that can match several variations of a command, including those with multiple words or different prefixes, without altering the core matching logic. The change simplifies rule definition and makes the pattern language more robust for complex command structures. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #94 +/- ##
==========================================
+ Coverage 89.00% 89.07% +0.06%
==========================================
Files 29 29
Lines 5821 5966 +145
==========================================
+ Hits 5181 5314 +133
- Misses 640 652 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…re alternatives
`consume_word` did not treat `|` as a word boundary, so inside
`consume_alternation_continuation` a bare-word branch like
`"npx prettier"|foo|bar` would merge `foo|bar` into a single word
instead of yielding two separate alternatives.
Add an `extra_stop` parameter to `consume_word` and pass `Some('|')`
from the alternation continuation context.
Also extract shared tokenize/build logic between `parse` and
`parse_multi` into `tokenize_pattern` / `build_pattern_from_tokens`
helpers, and use iterator methods for alternation construction.
…e-word pipe The error message for an unclosed quote after a bare-word pipe reported the position of the bare word start instead of the opening quote character. Capture the quote position from `chars.peek()` and use it in the error message. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Verify that when a bare-word pipe is followed by an unclosed quote (e.g., `prettier|"npx prettier`), the error message reports the position of the opening quote character, not the bare word start. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Why
"npx prettier"|"bunx prettier"|prettier *should allow multi-word alternatives containing spacesnpx prettier,bunx prettier, andprettierunder a single rule currently requires three separate rulesWhat
Patterninstances at parse time, enabling matching without changes to the existing matcher