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

Matching a non-empty pattern #357

Open
rix0rrr opened this issue Feb 13, 2023 · 2 comments
Open

Matching a non-empty pattern #357

rix0rrr opened this issue Feb 13, 2023 · 2 comments

Comments

@rix0rrr
Copy link

rix0rrr commented Feb 13, 2023

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] ?

@rvantonder
Copy link
Member

:[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?

@rix0rrr
Copy link
Author

rix0rrr commented Mar 24, 2023

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?

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

2 participants