jscodeshift
jscodeshift copied to clipboard
Does the CLI allows for syntactically-aware greps?
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;
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.
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