How would you match jsx returning expressions only? #654
-
Thank you for the great tool! Imagine we have the following:
What would be the YAML configuration here to match only the components? |
Beta Was this translation helpful? Give feedback.
Answered by
HerringtonDarkholme
Sep 30, 2023
Replies: 2 comments 1 reply
-
Hi! Thanks for trying ast-grep. In ast-grep, you can use the advanced I have broken down the rule into self-explanatory sub util rules. utils:
isFunc:
any:
- kind: arrow_function
- kind: function
- kind: function_declaration
isJSX:
any:
- kind: jsx_element
- kind: jsx_fragment
- kind: parenthesized_expression # if jsx inside ()
has:
matches: isJSX
hasJSXReturn:
any:
- matches: isJSX # () => <></>
- kind: statement_block # () => { return <></> }
has:
kind: return_statement
has:
matches: hasJSXReturn
stopBy: end
rule:
matches: isFunc
has:
field: body
matches: hasJSXReturn Hope this help! |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
skoch13
-
Man, this is just crazy! Appreciate your work(and donated :D) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Thanks for trying ast-grep.
Matching JSX element in return position isn't trivial! Even eslint-plugin-react uses some heuristics to approximate the matching algorithm.
In ast-grep, you can use the advanced
utils
rule. https://ast-grep.github.io/guide/rule-config/utility-rule.htmlI have broken down the rule into self-explanatory sub util rules.