comby-tools / comby

A code rewrite tool for structural search and replace that supports ~every language.
https://comby.dev
Apache License 2.0
2.38k stars 60 forks source link

JSX support #203

Open sqs opened 4 years ago

sqs commented 4 years ago

(Comby is awesome! Thank you!) I would like to be able to use Comby to match in JSX files (that is, JavaScript or TypeScript files with React-style <Tag>s).

On the following sample .jsx or .tsx file (to demonstrate the syntax):

const a = () => <>
  <Route path="/a" component={A}/>
  <Route path="/b" render={() => 'b'}/>
</>

With Comby 0.17.0, the :[1] hole is greedier than I'd like; it matches from the 1st <Route to the 2nd Route component's self-closing tag />:

$ comby -color -match-only '<Route :[1] render={:[2]}/>' '' ~/comby-test.tsx 
/Users/sqs/Downloads/comby-test.tsx:2:<Route path="/a" component={A}/>\n  <Route path="/b" render={() => 'b'}/>

I would like JSX support that would result in only <Route path="/b" render={() => 'b'}/> being matched.

rvantonder commented 4 years ago

Nice, yeah, the use case is clear here. From a parsing perspective, there is a breakdown of two concerns. The first is:

The second is:

So that's the context. The solution is workable, just a matter of time/effort in the right places :-)