-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Analyzer: accteststep: returns resource.TestStep literals for lat…
…er passes
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package accteststep | ||
|
||
import ( | ||
"go/ast" | ||
"reflect" | ||
|
||
"github.com/bflad/tfproviderlint/helper/terraformtype" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: "accteststep", | ||
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/resource.TestStep literals for later passes", | ||
Requires: []*analysis.Analyzer{ | ||
inspect.Analyzer, | ||
}, | ||
Run: run, | ||
ResultType: reflect.TypeOf([]*terraformtype.HelperResourceTestStepInfo{}), | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
nodeFilter := []ast.Node{ | ||
(*ast.CompositeLit)(nil), | ||
} | ||
var result []*terraformtype.HelperResourceTestStepInfo | ||
|
||
inspect.Preorder(nodeFilter, func(n ast.Node) { | ||
x := n.(*ast.CompositeLit) | ||
|
||
if !isResourceTestStep(pass, x) { | ||
return | ||
} | ||
|
||
result = append(result, terraformtype.NewHelperResourceTestStepInfo(x, pass.TypesInfo)) | ||
}) | ||
|
||
return result, nil | ||
} | ||
|
||
func isResourceTestStep(pass *analysis.Pass, cl *ast.CompositeLit) bool { | ||
switch v := cl.Type.(type) { | ||
default: | ||
return false | ||
case *ast.SelectorExpr: | ||
return terraformtype.IsHelperResourceTypeTestStep(pass.TypesInfo.TypeOf(v)) | ||
} | ||
} |