comby-tools/comby

Matching a non-empty pattern

rix0rrr opened this issue · 2 comments

I feel rather silly posting this issue, because it seems so simple that I must be overlooking something. Nevertheless, looking at the documentation here it really seems impossible to do what I want, so here goes.

The situation

I'm trying to rewrite jQuery handlers from a legacy syntax to a recommended syntax. Effectively, this is the transformation I'm trying to do:

$('#bla').submit(function() { ... });

            →

$('#bla').on('submit', function() { ... });

So far, so straightforward. The problem is that jQuery methods are heavily overloaded, and so $('#bla').submit() also exists, but should not be rewritten.

The attempt

So I'm trying the following:

comby '$(:[a]).submit(:[b])' '$(:[a]).on(\'submit\', :[b])' *.ts

And that kinda works:

image

Except it also catches the case I don't want to catch:

image

Oops!

The problem

So, it seems that I want to specify a pattern :[b], such that b may not be empty (but I do want to preserve the lazy/automatic matching that the :[b] syntax would do.

Looking at the matching syntax table, none of the patterns achieve this:

  • :[[b]] - only matches indentifiers, so will definitely not match anything of the form function() { ... }
  • :[b:e] - sounds like it might do what I want it to do, but doesn't (apparently it doesn't like the function () { } syntax?)
  • :[b.] - doesn't match whitespace

Sooo... I guess I'm looking for a non-empty variant of :[x] ?

:[b:e] - sounds like it might do what I want it to do, but doesn't (apparently it doesn't like the function () { }

Yeah this gets close, but the limitation is it won't match across the whitespace between function() { }. If you are sure that the second argument is always of the form function() {...} this corresponds to two expressions like :[f:e] :[braces:e] where f matches function() and braces match the block in braces {...}.

Sooo... I guess I'm looking for a non-empty variant of :[x] ?

The alternative here is to use a rule to case out on non-empty matches of :[x]. Something like -rule 'where :[x] != ""' should work on the command line?

Thanks for responding! I appreciate the workaround. Feels to me like the -rule approach would probably be best for my case, I'd prefer not to have to make assumptions on the form of the particular expression.

Seems like this would be a common enough use case that it might warrant special syntax?