matcher

package
v0.0.0-...-df944e1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "match",
	Doc:  "todo",
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
	Run: func(p *analysis.Pass) (any, error) {
		inspect := p.ResultOf[inspect.Analyzer].(*inspector.Inspector)

		patch, err := gopatch.Parse(bytes.NewBuffer([]byte(`
-import "errors"

-errors.New(fmt.Sprintf(...))
+fmt.Errorf(...)
`)))
		if err != nil {
			return nil, err
		}

		before := patch.Patches[0].Diff.Before()
		after := patch.Patches[0].Diff.After()
		fmt.Printf("Before: %s\n", before.Root.String())
		fmt.Printf("After: %s\n", after.Root.String())

		inspect.Preorder([]ast.Node{
			(*ast.CallExpr)(nil),
		}, func(n ast.Node) {
			_, edits, ok := code.MatchAndEdit(p, before, after, n)
			if ok {
				fmt.Printf("Matched! %s\n", edits[0].NewText)
			}
		})

		return nil, nil
	},
}

Functions

This section is empty.

Types

type ArgumentPattern

type ArgumentPattern struct {
}

type CallPattern

type CallPattern struct {
	pattern.CallExpr
}

func (*CallPattern) Matches

func (p *CallPattern) Matches(ctx Context, n ast.Node) bool

func (*CallPattern) Nodes

func (p *CallPattern) Nodes() []ast.Node

type Context

type Context = pattern.Matcher

type Match

type Match interface {
	// Nodes returns root [ast.Node]s that were matched.
	Nodes() []ast.Node
}

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

func (*Matcher) Find

func (m *Matcher) Find() []Match

type NamedPattern

type NamedPattern struct {
	Package  string
	Function string
}

func (*NamedPattern) Nodes

func (p *NamedPattern) Nodes() []ast.Node

type Pattern

type Pattern interface {
	Matches(Context, ast.Node) bool
	Nodes() []ast.Node
}

type Wildcard

type Wildcard struct {
}

func (*Wildcard) Matches

func (*Wildcard) Matches(types.Object) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL