Skip to content

Contextual matching and transformations #11

Open
@abhinav

Description

@abhinav

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions