gkz / grasp

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

grasp -e returning no results #18

Closed ibash closed 10 years ago

ibash commented 10 years ago

Hi, I read http://graspjs.com/blog/2014/01/07/refactoring-javascript-with-grasp/ -- so maybe I need to read more documentation but...

My end goal is to turn last() calls like this:

lib/html5/parser.js:2395:                       if (tree.open_elements.last() != node) {
lib/html5/parser.js:2407:                       if (tree.open_elements.last().tagName.toLowerCase() != name)
lib/html5/parser.js:2431:               if (tree.open_elements.last().tagName.toLowerCase() != name)
lib/html5/parser.js:2464:                       if (afeElement != tree.open_elements.last()) {
lib/html5/parser.js:2556:               tree.activeFormattingElements.push(tree.open_elements.last());
lib/html5/parser.js:2576:                       {name: tree.open_elements.last().tagName.toLowerCase()});
lib/html5/parser.js:2650:               var currentNode = this.tree.open_elements.last() || null;

Into something like this:

lib/html5/parser.js:2395:                       if (last(tree.open_elements) != node) {
lib/html5/parser.js:2407:                       if (last(tree.open_elements).tagName.toLowerCase() != name)
lib/html5/parser.js:2431:               if (last(tree.open_elements).tagName.toLowerCase() != name)
lib/html5/parser.js:2464:                       if (afeElement != last(tree.open_elements)) {
lib/html5/parser.js:2556:               tree.activeFormattingElements.push(last(tree.open_elements));
lib/html5/parser.js:2576:                       {name: last(tree.open_elements).tagName.toLowerCase()});
lib/html5/parser.js:2650:               var currentNode = last(this.tree.open_elements) || null;

I can detect the last identifier like so: grasp -e 'last' lib/html5/parser.js

However -- when I try to detect it with parens I get no results: grasp -e 'last()' lib/html5/parser.js

I also tried adding wildcards -- but just get an error:

islams-mbp:html5 islam$ grasp -e '__last()__' lib/html5/parser.js

/Users/islam/.nvm/v0.10.24/lib/node_modules/grasp/node_modules/grasp-equery/lib/parse.js:42
      throw new Error("Error processing selector '" + selector + "'.");
            ^
Error: Error processing selector '__last()__'.
    at Object.parse (/Users/islam/.nvm/v0.10.24/lib/node_modules/grasp/node_modules/grasp-equery/lib/parse.js:42:13)
    at run (/Users/islam/.nvm/v0.10.24/lib/node_modules/grasp/lib/index.js:174:34)
    at Object.<anonymous> (/Users/islam/.nvm/v0.10.24/lib/node_modules/grasp/bin/grasp:4:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

Am I misunderstanding how to use grasp? What am I doing wrong? Or is it something to be debugged?

gkz commented 10 years ago

What you want is:

 grasp -e '$a.last()' -R 'last({{a}})' t.js

Explanation: last() is different from tree.open_elements.last(), to see the difference, think of the later as (tree.open_elements.last)() - as you see, the first is a call expression with no arguments on last, and the second is a call expression with no arguments on tree.open_elements.last, and this is different.

To just match tree.open_elements.last() you can use __.last(), but you want to capture the value, so you can use a named wildcard $a, so you can access it in the replacement pattern with {{a}}.

ibash commented 10 years ago

Thank you for pointing me in the right direction and clarifying the difference. What made it clearer was doing acorn.parse('__last()') vs acorn.parse('__.last()') and exporting the AST makes total sense now!

Also, thanks for creating grasp, great tool.

gkz commented 10 years ago

I think this would be a good example to make a blog post out of, so other people who how to do this. Would it be OK if I wrote a post about this example?

ibash commented 10 years ago

Sure, I was actually thinking about doing the same for our blog (DataHero) :)

On Sat, Jan 25, 2014 at 2:36 PM, George Zahariev notifications@github.comwrote:

I think this would be a good example to make a blog post out of, so other people who how to do this. Would it be OK if I wrote a post about this example?

— Reply to this email directly or view it on GitHubhttps://github.com/gkz/grasp/issues/18#issuecomment-33302810 .

Islam Sharabash 217.377.9657