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

Contextual matching and transformations #11

Open
abhinav opened this issue Apr 21, 2021 · 0 comments
Open

Contextual matching and transformations #11

abhinav opened this issue Apr 21, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@abhinav
Copy link
Contributor

abhinav commented Apr 21, 2021

Patches currently apply at matching "levels" in the AST.

For example, consider the following patch.

@@
@@
-func foo(...) {
+func foo(...) error {
    ...
    if err != nil {
-       log.Fatal(err)
+       return err
    }
    ...
 }

In addition to the issue with replacing only the first instance of the matching statement (described in #10), this has the problem that the statement is only matched at the "top level" of the function. It will not affect the following versions, for example:

func foo() {
    for x := range y {
        err := bar(x)
        if err != nil {
            log.Fatal(err)
        }
    }
}

We need a means of specifying that given some previously matched context, we want to apply a "sub transformation" to the entire tree inside that context.

I've been referring to this concept as contextual matching but there may be a better term.

Borrowing from Coccinelle, one possible syntax here is <..., ...>. For example,

@@
@@
-func foo(...) {
+func foo(...) error {
    <...
      if err != nil {
-       log.Fatal(err)
+       return err
      }
    ...>
 }

Which I'm roughly translating to "given that matched context, apply this match/transformation to everything inside it." The "match all" nature of this syntax might also help address #10.

@abhinav abhinav added the enhancement New feature or request label Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant