New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grasp -e returning no results #18

Closed
ibash opened this Issue Jan 25, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@ibash
Copy link

ibash commented Jan 25, 2014

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

This comment has been minimized.

Copy link
Owner

gkz commented Jan 25, 2014

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}}.

@gkz gkz closed this Jan 25, 2014

@ibash

This comment has been minimized.

Copy link

ibash commented Jan 25, 2014

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

This comment has been minimized.

Copy link
Owner

gkz commented Jan 25, 2014

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

This comment has been minimized.

Copy link

ibash commented Jan 25, 2014

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//issues/18#issuecomment-33302810
.

Islam Sharabash
217.377.9657

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment