inspect.go raw
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package inspect defines an Analyzer that provides an AST inspector
6 // (golang.org/x/tools/go/ast/inspector.Inspector) for the syntax trees
7 // of a package. It is only a building block for other analyzers.
8 //
9 // Example of use in another analysis:
10 //
11 // import (
12 // "golang.org/x/tools/go/analysis"
13 // "golang.org/x/tools/go/analysis/passes/inspect"
14 // "golang.org/x/tools/go/ast/inspector"
15 // )
16 //
17 // var Analyzer = &analysis.Analyzer{
18 // ...
19 // Requires: []*analysis.Analyzer{inspect.Analyzer},
20 // }
21 //
22 // func run(pass *analysis.Pass) (interface{}, error) {
23 // inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
24 // inspect.Preorder(nil, func(n ast.Node) {
25 // ...
26 // })
27 // return nil, nil
28 // }
29 package inspect
30
31 import (
32 "reflect"
33
34 "golang.org/x/tools/go/analysis"
35 "golang.org/x/tools/go/ast/inspector"
36 )
37
38 var Analyzer = &analysis.Analyzer{
39 Name: "inspect",
40 Doc: "optimize AST traversal for later passes",
41 URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/inspect",
42 Run: run,
43 RunDespiteErrors: true,
44 ResultType: reflect.TypeFor[*inspector.Inspector](),
45 }
46
47 func run(pass *analysis.Pass) (any, error) {
48 return inspector.New(pass.Files), nil
49 }
50