github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/analysis/passes/inspect/inspect.go (about)

     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  // (github.com/powerman/golang-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  //		"github.com/powerman/golang-tools/go/analysis"
    13  //		"github.com/powerman/golang-tools/go/analysis/passes/inspect"
    14  //		"github.com/powerman/golang-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
    28  // 	}
    29  //
    30  package inspect
    31  
    32  import (
    33  	"reflect"
    34  
    35  	"github.com/powerman/golang-tools/go/analysis"
    36  	"github.com/powerman/golang-tools/go/ast/inspector"
    37  )
    38  
    39  var Analyzer = &analysis.Analyzer{
    40  	Name:             "inspect",
    41  	Doc:              "optimize AST traversal for later passes",
    42  	Run:              run,
    43  	RunDespiteErrors: true,
    44  	ResultType:       reflect.TypeOf(new(inspector.Inspector)),
    45  }
    46  
    47  func run(pass *analysis.Pass) (interface{}, error) {
    48  	return inspector.New(pass.Files), nil
    49  }