Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / lint / lintdsl / lintdsl.go
1 // Package lintdsl provides helpers for implementing static analysis
2 // checks. Dot-importing this package is encouraged.
3 package lintdsl
4
5 import (
6         "bytes"
7         "fmt"
8         "go/ast"
9         "go/format"
10
11         "golang.org/x/tools/go/analysis"
12         "honnef.co/go/tools/pattern"
13 )
14
15 func Inspect(node ast.Node, fn func(node ast.Node) bool) {
16         if node == nil {
17                 return
18         }
19         ast.Inspect(node, fn)
20 }
21
22 func Match(pass *analysis.Pass, q pattern.Pattern, node ast.Node) (*pattern.Matcher, bool) {
23         // Note that we ignore q.Relevant – callers of Match usually use
24         // AST inspectors that already filter on nodes we're interested
25         // in.
26         m := &pattern.Matcher{TypesInfo: pass.TypesInfo}
27         ok := m.Match(q.Root, node)
28         return m, ok
29 }
30
31 func MatchAndEdit(pass *analysis.Pass, before, after pattern.Pattern, node ast.Node) (*pattern.Matcher, []analysis.TextEdit, bool) {
32         m, ok := Match(pass, before, node)
33         if !ok {
34                 return m, nil, false
35         }
36         r := pattern.NodeToAST(after.Root, m.State)
37         buf := &bytes.Buffer{}
38         format.Node(buf, pass.Fset, r)
39         edit := []analysis.TextEdit{{
40                 Pos:     node.Pos(),
41                 End:     node.End(),
42                 NewText: buf.Bytes(),
43         }}
44         return m, edit, true
45 }
46
47 func Selector(x, sel string) *ast.SelectorExpr {
48         return &ast.SelectorExpr{
49                 X:   &ast.Ident{Name: x},
50                 Sel: &ast.Ident{Name: sel},
51         }
52 }
53
54 // ExhaustiveTypeSwitch panics when called. It can be used to ensure
55 // that type switches are exhaustive.
56 func ExhaustiveTypeSwitch(v interface{}) {
57         panic(fmt.Sprintf("internal error: unhandled case %T", v))
58 }