Skip to content

Matching React props: strings and expressions #1103

Discussion options

You must be logged in to vote

Hi, I recommend https://dev.to/herrington_darkholme/migrate-to-react-19-with-ast-grep-28op as an example to match JSX attribute.

In your case, the rewriter is also a helpful construct to change string literal "old".

rule:
  all:
  - pattern: 
      context: '<div variant=$A />'
      selector: jsx_attribute
  - pattern: $ATTR
rewriters:
- id: old-to-new
  rule:
    kind: string_fragment
    regex: '^old$'
  fix: new 
transform:
  NEW_ATTR: 
    rewrite:
      rewriters: [old-to-new]
      source: $ATTR
fix: $NEW_ATTR

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InRzeCIsInF1ZXJ5IjoiIiwicmV3cml0ZSI6IiIsImNvbmZpZyI6InJ1bGU6XG4gIGFsbDpcbiAgLSBwYXR0ZXJuOiBcbiAgICA…

Replies: 1 comment 1 reply

Comment options

@reidbarber
Comment options

Answer selected by reidbarber
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