gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

Question: Can I repeat unnamed matches when replacing? #36

Open rksm opened 10 years ago

rksm commented 10 years ago

My use case is the following:

Example:

alert(foo() + bar());
function foo() {/*...*/}
function bar() {/*...*/}

⬇︎

Global.foo = foo;
Global.bar = bar;
alert(foo() + bar());
function foo() {/*...*/}
function bar() {/*...*/}

Right now I chain two rules to achieve it:

grasp.replace('squery', ':root[body]', 'Global.funcdecl$.{{func-dec.id | join "\nGlobal.funcdecl$."}}\n{{}}');
grasp.replace('equery', 'Global.funcdecl$.$funcName', 'Global.{{funcName}} = {{funcName}};');

Applying the second rule is rather expensive. Is there a way to repeat the id in the join expression of the first replacement?