jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

Does the CLI allows for syntactically-aware greps?

Open ninofiliu opened this issue 2 years ago • 2 comments

Meta has archived jsgrep and it feels like jscodeshift can potentially do everything that the jsgrep toolkit could do, yet I don't find any examples nor in this repo's readme or wiki any example on how I would do that

For example, can I use the CLI to pass a JS expression or an AST and it would print the matching lines in specified files?

user@host$ cat main.js
const a = 10;
console.log({ a });
user@host$ jscodeshift --js "const a = ANY" main.js # doesn't work but I wish something similar would
main.js:1:1:const a = 10;

ninofiliu avatar May 08 '22 03:05 ninofiliu

I could definitely see this being useful. In addition to jsgrep, at Meta we used to have a tool called pfff which had an sgrep tool that did something pretty similar for a few languages (https://github.com/facebookarchive/pfff/wiki/Sgrep), and we've got a similar internal tool for Hack code.

I'm not sure if anyone has tried it with jscodeshift before though! I think the tricky thing would be returning the results, as jscodeshift isn't really designed to have output other than the modified files.

Daniel15 avatar May 09 '22 23:05 Daniel15

I find the TSQuery approach particularly interesting and I've flirted with the idea of adding something like this to jscodeshift (after a heck of a lot refactoring, that is).

https://github.com/phenomnomnominal/tsquery

ElonVolo avatar May 10 '22 08:05 ElonVolo