ast-grep icon indicating copy to clipboard operation
ast-grep copied to clipboard

Add % commands

Open azz opened this issue 8 years ago • 0 comments

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 show ClassMethods where are static.

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.

azz avatar Jan 14 '18 08:01 azz