Documentation ¶
Index ¶
- func ImportRules(prefix string, bundle Bundle)
- type Bundle
- type ExprType
- type ExprValue
- type File
- type MatchedNode
- type MatchedText
- type Matcher
- func (m Matcher) At(v Var) Matcher
- func (m Matcher) File() File
- func (m Matcher) Import(pkgPath string)
- func (m Matcher) Match(pattern string, alternatives ...string) Matcher
- func (m Matcher) Report(message string) Matcher
- func (m Matcher) Suggest(suggestion string) Matcher
- func (m Matcher) Where(cond bool) Matcher
- type String
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ImportRules ¶
ImportRules imports all rules from the bundle and prefixes them with a specified string. Only packages that have an exported Bundle variable can be imported.
Types ¶
type Bundle ¶
type Bundle struct { // Version is a bundle version. // It's preferred to use a semver format. // Examples: "0.5.1", "1.0.0". Version string }
Bundle is a rules file export manifest.
type ExprType ¶
type ExprType struct { // Size represents expression type size in bytes. Size int }
ExprType describes a type of a matcher expr.
func (ExprType) AssignableTo ¶
AssignableTo reports whether a type is assign-compatible with a given type. See https://golang.org/pkg/go/types/#AssignableTo.
func (ExprType) ConvertibleTo ¶
ConvertibleTo reports whether a type is conversible to a given type. See https://golang.org/pkg/go/types/#ConvertibleTo.
func (ExprType) Implements ¶
Implements reports whether a type implements a given interface. See https://golang.org/pkg/go/types/#Implements.
func (ExprType) Underlying ¶
Underlying returns expression type underlying type. See https://golang.org/pkg/go/types/#Type Underlying() method documentation. Read https://golang.org/ref/spec#Types section to learn more about underlying types.
type ExprValue ¶
type ExprValue struct{}
ExprValue describes a compile-time computable value of a matched expr.
type File ¶
type File struct { // Name is a file base name. Name String // PkgPath is a file package path. // Examples: "io/ioutil", "strings", "github.com/quasilyte/go-ruleguard/dsl/fluent". PkgPath String }
File represents the current Go source file.
type MatchedNode ¶
type MatchedNode struct{}
MatchedNode represents an AST node associated with a named submatch.
func (MatchedNode) Is ¶
func (MatchedNode) Is(typ string) bool
Is reports whether a matched node AST type is compatible with the specified type. A valid argument is a ast.Node implementing type name from the "go/ast" package. Examples: "BasicLit", "Expr", "Stmt", "Ident", "ParenExpr". See https://golang.org/pkg/go/ast/.
type MatchedText ¶
type MatchedText string
MatchedText represents a source text associated with a matched node.
func (MatchedText) Matches ¶
func (MatchedText) Matches(pattern string) bool
Matches reports whether the text matches the given regexp pattern.
type Matcher ¶
Matcher is a main API group-level entry point. It's used to define and configure the group rules. It also represents a map of all rule-local variables.
func (Matcher) At ¶
At binds the reported node to a named submatch. If no explicit location is given, the outermost node ($$) is used.
func (Matcher) Import ¶
Import loads given package path into a rule group imports table.
That table is used during the rules compilation.
The table has the following effect on the rules:
- For type expressions, it's used to resolve the full package paths of qualified types, like `foo.Bar`. If Import(`a/b/foo`) is called, `foo.Bar` will match `a/b/foo.Bar` type during the pattern execution.
func (Matcher) Match ¶
Match specifies a set of patterns that match a rule being defined. Pattern matching succeeds if at least 1 pattern matches.
If none of the given patterns matched, rule execution stops.
func (Matcher) Report ¶
Report prints a message if associated rule match is successful.
A message is a string that can contain interpolated expressions. For every matched variable it's possible to interpolate their printed representation into the message text with $<name>. An entire match can be addressed with $$.
type Var ¶
type Var struct { // Pure reports whether expr matched by var is side-effect-free. Pure bool // Const reports whether expr matched by var is a constant value. Const bool // Value is a compile-time computable value of the expression. Value ExprValue // Addressable reports whether the corresponding expression is addressable. // See https://golang.org/ref/spec#Address_operators. Addressable bool // Type is a type of a matched expr. // // For function call expressions, a type is a function result type, // but for a function expression itself it's a *types.Signature. // // Suppose we have a `a.b()` expression: // `$x()` m["x"].Type is `a.b` function type // `$x` m["x"].Type is `a.b()` function call result type Type ExprType // Text is a captured node text as in the source code. Text MatchedText // Node is a captured AST node. Node MatchedNode }
Var is a pattern variable that describes a named submatch.