Skip to content

How would you match jsx returning expressions only? #654

Closed Answered by HerringtonDarkholme
skoch13 asked this question in Q&A
Discussion options

You must be logged in to vote

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.html

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  # () => <></>

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by skoch13
Comment options

You must be logged in to vote
1 reply
@HerringtonDarkholme
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants