Skip to content

Commit

Permalink
dsl: add experimental Do() action
Browse files Browse the repository at this point in the history
Unlike `Report()` and `Suggest()`, new `Do()` method allows
a more granular match action handling.

Now it's possible to emit a conditional report/suggest message
based on the matching context.

The API is experimental and is likely to change.
  • Loading branch information
quasilyte committed Feb 13, 2022
1 parent 2e73e37 commit a307c89
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dsl/do.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dsl

import (
"github.com/quasilyte/go-ruleguard/dsl/types"
)

type DoContext struct{}

func (*DoContext) Var(varname string) *DoVar { return nil }

func (*DoContext) SetReport(report string) {}

func (*DoContext) SetSuggest(suggest string) {}

type DoVar struct{}

func (*DoVar) Text() string { return "" }

func (*DoVar) Type() types.Type { return nil }
4 changes: 4 additions & 0 deletions dsl/dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (m Matcher) Suggest(suggestion string) Matcher {
return m
}

func (m Matcher) Do(fn func(*DoContext)) Matcher {
return m
}

// At binds the reported node to a named submatch.
// If no explicit location is given, the outermost node ($$) is used.
func (m Matcher) At(v Var) Matcher {
Expand Down

0 comments on commit a307c89

Please sign in to comment.