ast-grep / ast-grep

⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
https://ast-grep.github.io/
MIT License
7.47k stars 170 forks source link

[feature] Support filter `sg run` results by metavar regular expression #379

Open HerringtonDarkholme opened 1 year ago

HerringtonDarkholme commented 1 year ago

Related: https://github.com/ast-grep/ast-grep/discussions/378

We can only use one single pattern to search code for command line sg run. This is convenient but less powerful to sg scan. For example, if users want to find userCol.update or itemCol.update, a single pattern $COL.update cannot be done on command line. The work around is to write a temporary rule in YAML and use sg scan -r temp.yml to scan code base.

We can build a better CLI option to accept regular expression for meta-variables on command line.

HerringtonDarkholme commented 1 year ago

I have an idea like this: sg run will have a new option -f --filter to filter match results based on the metavariable.

The filter will have three forms:

This is a mini-syntax to keep in sync with constraints in the YAML format. Maybe it is too hard to learn and even harder to search on web.

Please leave comments here if you have any feedback! It can be a simple "love it"/"hate it". Or a lengthy paragraph explaining your preference. Of course I welcome your proposal!

HerringtonDarkholme commented 1 year ago

Alternative design:

which one is better?

adamazing commented 8 months ago

I would definitely prefer the second design. As a sometime / irregular user of ast-grep, I'm far less likely to recall the former syntax (e.g. -f META_VAR:kind_id etc.).

The second/alternative design would probably lend itself more to ergonomic/easily-grokked shell completions?