Skip to content
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

Problem replacing nested match #59

Open
thereforeiam opened this issue Apr 16, 2015 · 0 comments
Open

Problem replacing nested match #59

thereforeiam opened this issue Apr 16, 2015 · 0 comments

Comments

@thereforeiam
Copy link

I'm trying to factor addEventLister code to a library function, essentially turning this:

foo.bar.addEventListener('mousemove', callback);

into this :

factorizedFunction(foo.bar, 'mousemove', callback);

Let's consider an nested example:

foo.bar.addEventListener("mousedown", function(evt)
{
     //do some stuff

    evt.preventDefault();
    evt.stopPropagation();

    document.body.addEventListener("mouseup", function leMe(evt)
    {
      //do some other stuff
      document.body.removeEventListener("mouseup", leMe);
    });
});

I'm using Equery which works great for this, running this on the above code:
$ grasp -e '$target.addEventListener($eventName, $callback, _$capture)' test.js

outputs this :

1-13:(multiline):
foo.bar.addEventListener("mousedown", function(evt)
{
    //do some stuff

    evt.preventDefault();
    evt.stopPropagation();

    document.body.addEventListener("mouseup", function leMe(evt)
    {
    //do some other stuff
    document.body.removeEventListener("mouseup", leMe);
    });
});
8-12:(multiline):
    document.body.addEventListener("mouseup", function leMe(evt)
    {
    //do some other stuff
    document.body.removeEventListener("mouseup", leMe);
    });

So far so good, just notice that nested match is printed after surrounding one.

Now, when running my replace :
$ grasp -e '$target.addEventListener($eventName, $callback, _$capture)' -iR 'factorizedFunction({{target}}, {{eventName}}, {{callback}}{{capture | before ", "}})' test.js

results in the following code :

factorizedFunction(foo.bar, "mousedown", function(evt)
{
    //do some stuff

    evt.preventDefault();
    evt.stopPropagation();

    document.body.addEventListener("mouseup", function leMe(evt)
    {
      //do some other stuff
      document.body.removeEventListener("mouseup", leMe);
    });
});

As you can see the nested match is not replaced, I guess because it is treated after.
As a workaround I run the command as long as I find matches in my code base.

PS : Great Job guys, grasp is a fantastic tool ! 👍

@thereforeiam thereforeiam changed the title Probleme replacing nested match Problem replacing nested match Apr 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant