Find all extension methods of a type in csharp using ast-grep
- 1 minutes read - 109 wordsUsually one will use simple find or more advanced regular expression based search to locate codes. How about use other options usig ast to get more accurate results and have the potential to use for codemod? In the past 2 days, I finally figured how to use ast-grep to find code.
Here I will use it to find all extension methods of an class. The rule is configured below.
rule:
kind: identifier
precedes:
kind: parameter_list
inside:
kind: method_declaration
all:
- has:
kind: parameter_list
has:
nthChild: 1
kind: parameter
all:
- has:
kind: modifier
regex: this
- has:
kind: identifier
field: type
regex: IFhirEngineBuilder
An example is at the playground