|
| 1 | +package tfclientcollection |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "go/token" |
| 6 | + "go/types" |
| 7 | + |
| 8 | + "github.com/golangci/plugin-module-register/register" |
| 9 | + "golang.org/x/tools/go/analysis" |
| 10 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 11 | + "golang.org/x/tools/go/ast/inspector" |
| 12 | +) |
| 13 | + |
| 14 | +var Analyzer = &analysis.Analyzer{ |
| 15 | + Name: "tfclientcollection", |
| 16 | + Doc: "Prevents the keeping the core.ClientCollection struct from being kept in a resource. Only clients should be extracted from it and kept.", |
| 17 | + Run: run, |
| 18 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 19 | +} |
| 20 | + |
| 21 | +const ( |
| 22 | + targetPkg = "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 23 | + targetStruct = "ClientCollection" |
| 24 | +) |
| 25 | + |
| 26 | +func run(pass *analysis.Pass) (any, error) { |
| 27 | + inspector, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 28 | + if !ok || inspector == nil { |
| 29 | + return nil, nil |
| 30 | + } |
| 31 | + |
| 32 | + nodeFilter := []ast.Node{ |
| 33 | + (*ast.GenDecl)(nil), |
| 34 | + (*ast.StructType)(nil), |
| 35 | + (*ast.FuncType)(nil), |
| 36 | + } |
| 37 | + |
| 38 | + inspector.Preorder(nodeFilter, func(n ast.Node) { |
| 39 | + switch node := n.(type) { |
| 40 | + // Checks 'var' declarations |
| 41 | + case *ast.GenDecl: |
| 42 | + if node.Tok != token.VAR { |
| 43 | + return |
| 44 | + } |
| 45 | + for _, spec := range node.Specs { |
| 46 | + vspec, ok := spec.(*ast.ValueSpec) |
| 47 | + if !ok { |
| 48 | + continue |
| 49 | + } |
| 50 | + for _, name := range vspec.Names { |
| 51 | + obj := pass.TypesInfo.ObjectOf(name) |
| 52 | + if obj != nil && isForbiddenType(obj.Type()) { |
| 53 | + pass.Reportf( |
| 54 | + name.Pos(), |
| 55 | + "variable %q declared via 'var' uses forbidden type %s.%s", |
| 56 | + name.Name, |
| 57 | + targetPkg, |
| 58 | + targetStruct, |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Checks struct fields |
| 65 | + case *ast.StructType: |
| 66 | + if node.Fields == nil { |
| 67 | + return |
| 68 | + } |
| 69 | + for _, field := range node.Fields.List { |
| 70 | + fieldType := pass.TypesInfo.TypeOf(field.Type) |
| 71 | + if isForbiddenType(fieldType) { |
| 72 | + pass.Reportf( |
| 73 | + field.Pos(), |
| 74 | + "struct field uses forbidden type %s.%s", |
| 75 | + targetPkg, |
| 76 | + targetStruct, |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Checks function, method, and function literal parameters |
| 82 | + case *ast.FuncType: |
| 83 | + if node.Params == nil { |
| 84 | + return |
| 85 | + } |
| 86 | + for _, param := range node.Params.List { |
| 87 | + paramType := pass.TypesInfo.TypeOf(param.Type) |
| 88 | + if isForbiddenType(paramType) { |
| 89 | + pass.Reportf( |
| 90 | + param.Pos(), |
| 91 | + "function parameter uses forbidden type %s.%s", |
| 92 | + targetPkg, |
| 93 | + targetStruct, |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + return nil, nil |
| 101 | +} |
| 102 | + |
| 103 | +func isForbiddenType(t types.Type) bool { |
| 104 | + if t == nil { |
| 105 | + return false |
| 106 | + } |
| 107 | + // Unwrap variadic parameters (...pkg.ForbiddenStruct -> pkg.ForbiddenStruct) |
| 108 | + if slice, ok := t.(*types.Slice); ok { |
| 109 | + t = slice.Elem() |
| 110 | + } |
| 111 | + // Unwrap pointers (*pkg.Struct -> pkg.Struct) |
| 112 | + if ptr, ok := t.(*types.Pointer); ok { |
| 113 | + t = ptr.Elem() |
| 114 | + } |
| 115 | + named, ok := t.(*types.Named) |
| 116 | + if !ok { |
| 117 | + return false |
| 118 | + } |
| 119 | + obj := named.Obj() |
| 120 | + if obj == nil || obj.Pkg() == nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + return obj.Pkg().Path() == targetPkg && obj.Name() == targetStruct |
| 124 | +} |
| 125 | + |
| 126 | +func init() { |
| 127 | + register.Plugin("tfclientcollection", New) |
| 128 | +} |
| 129 | + |
| 130 | +func New(settings any) (register.LinterPlugin, error) { |
| 131 | + return &plugin{}, nil |
| 132 | +} |
| 133 | + |
| 134 | +type plugin struct{} |
| 135 | + |
| 136 | +func (p *plugin) BuildAnalyzers() ([]*analysis.Analyzer, error) { |
| 137 | + return []*analysis.Analyzer{Analyzer}, nil |
| 138 | +} |
| 139 | + |
| 140 | +func (p *plugin) GetLoadMode() string { |
| 141 | + // LoadModeSyntax is required because we need to inspect the AST (Syntax trees) |
| 142 | + return register.LoadModeSyntax |
| 143 | +} |
0 commit comments