gkz / grasp

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

question: Replace function name #7

Closed fanderlf closed 10 years ago

fanderlf commented 10 years ago

Hi everybody,

I just stumbled across grasp and I find the idea pretty amazing and powerfull. My first use in real life would be to do a function rename. So image I have a file that looks like this:

a('a', ['b','c'], function(b,c) {
  if(b && c) {
    connsole.log('hello world');
  }
});

This is a call to function a that has several arguments 'a', ['b','c']...

Now I want to rename the function call from a to d. So I tried:

grasp -s "call[callee=#a]" --replace "d({{.args}})" file.js

The output is: d('a');

What I wanted is:

d('a', ['b','c'], function(b,c) {
  if(b && c) {
    console.log('hello world');
  }
});

Of course the number of arguments can vary, so a solution with a fixed number of parameters is not viable.

Any chances that I can get it to work?

Any help appreciated :)

Best, fanderlf

gkz commented 10 years ago

Hi, Update to Grasp 0.2.0 http://graspjs.com/blog/2013/12/17/grasp-0.2.0-released/ which add a new filters feature - use the join filter to print out more than one result.

grasp -s "call[callee=#a]" --replace "d({{.args | join ', '}})" file.js
retorquere commented 7 years ago

I tried calling this as

./node_modules/.bin/grasp -s "call[callee=#setImmediate]" --replace "setTimeout({{.args | join ', '}})" build/content/BetterBibTeX.js

(where build/content/BetterBibTeX.js is just a file a want to process) and I'm getting

Cannot read property 'type' of null

(no line number or other details are outputted to the console).

build/content/BetterBibTeX.js is a 101KLOC webpacked file though... I am hoping that this error is sufficiently informative.