ast-grep
ast-grep copied to clipboard
Add % commands
Say we want to match:
foo({ bar, baz })
foo({})
and not
foo(bar);
foo();
foo("bar");
Currently this isn't possible without running ast-grep many times.
V8 exposes custom functions through a "natives syntax", we could re-use this syntax to provide pattern matching:
- [ ]
%type:*- match any statement/expression/annotation. - [ ]
%type:Identifier,%type:LogicalExpression, etc: Only match one AST type. - [ ]
%type:ClassMethod,static:true: Only showClassMethods where arestatic.
So to match the above, we would issue:
ast-grep 'foo(%type:ObjectExpression)'
This would possibly require that all patterns must have a space after % if it is used as an actual binary expression, but that will be uncommon.