-
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.
Move TypeMap Elem check to S006, add S001 check for TypeList or TypeSet
- Loading branch information
Showing
15 changed files
with
236 additions
and
20 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
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
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,91 @@ | ||
// Package S006 defines an Analyzer that checks for | ||
// Schema of TypeMap missing Elem | ||
package S006 | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/schemaschema" | ||
) | ||
|
||
const Doc = `check for Schema of TypeMap missing Elem | ||
The S006 analyzer reports cases of TypeMap schemas missing Elem, | ||
which currently passes Terraform schema validation, but breaks downstream tools | ||
and may be required in the future.` | ||
|
||
const analyzerName = "S006" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
schemaschema.Analyzer, | ||
commentignore.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
schemas := pass.ResultOf[schemaschema.Analyzer].([]*ast.CompositeLit) | ||
for _, schema := range schemas { | ||
if ignorer.ShouldIgnore(analyzerName, schema) { | ||
continue | ||
} | ||
|
||
var elemFound bool | ||
var typeMap bool | ||
|
||
for _, elt := range schema.Elts { | ||
switch v := elt.(type) { | ||
default: | ||
continue | ||
case *ast.KeyValueExpr: | ||
name := v.Key.(*ast.Ident).Name | ||
|
||
if name == "Elem" { | ||
elemFound = true | ||
continue | ||
} | ||
|
||
if name != "Type" { | ||
continue | ||
} | ||
|
||
switch v := v.Value.(type) { | ||
default: | ||
continue | ||
case *ast.SelectorExpr: | ||
// Use AST over TypesInfo here as schema uses ValueType | ||
if v.Sel.Name != "TypeMap" { | ||
continue | ||
} | ||
|
||
switch t := pass.TypesInfo.TypeOf(v).(type) { | ||
default: | ||
continue | ||
case *types.Named: | ||
// HasSuffix here due to vendoring | ||
if !strings.HasSuffix(t.Obj().Pkg().Path(), "github.com/hashicorp/terraform/helper/schema") { | ||
continue | ||
} | ||
|
||
typeMap = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
if typeMap && !elemFound { | ||
pass.Reportf(schema.Type.(*ast.SelectorExpr).Sel.Pos(), "%s: schema of TypeMap should include Elem", analyzerName) | ||
} | ||
} | ||
|
||
return nil, nil | ||
} |
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,14 @@ | ||
package S006_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
|
||
"github.com/bflad/tfproviderlint/passes/S006" | ||
) | ||
|
||
func TestS006(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, S006.Analyzer, "a") | ||
} |
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,11 @@ | ||
package a | ||
|
||
import ( | ||
s "github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func falias() { | ||
_ = s.Schema{ // want "schema of TypeMap should include Elem" | ||
Type: s.TypeMap, | ||
} | ||
} |
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,14 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func fcommentignore() { | ||
_ = schema.Schema{Type: schema.TypeMap} //lintignore:S006 | ||
|
||
//lintignore:S006 | ||
_ = schema.Schema{ | ||
Type: schema.TypeMap, | ||
} | ||
} |
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,16 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func f() { | ||
_ = schema.Schema{ // want "schema of TypeMap should include Elem" | ||
Type: schema.TypeMap, | ||
} | ||
|
||
_ = schema.Schema{ | ||
Type: schema.TypeMap, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
} | ||
} |
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,11 @@ | ||
package a | ||
|
||
import ( | ||
"a/schema" | ||
) | ||
|
||
func foutside() { | ||
_ = schema.Schema{ | ||
Type: schema.TypeMap, | ||
} | ||
} |
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,33 @@ | ||
package schema | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
type Schema struct { | ||
Type ValueType | ||
} | ||
|
||
type ValueType int | ||
|
||
const ( | ||
TypeInvalid ValueType = iota | ||
TypeBool | ||
TypeInt | ||
TypeFloat | ||
TypeString | ||
TypeList | ||
TypeMap | ||
TypeSet | ||
) | ||
|
||
const _ValueType_name = "TypeInvalidTypeBoolTypeIntTypeFloatTypeStringTypeListTypeMapTypeSettypeObject" | ||
|
||
var _ValueType_index = [...]uint8{0, 11, 19, 26, 35, 45, 53, 60, 67, 77} | ||
|
||
func (i ValueType) String() string { | ||
if i < 0 || i >= ValueType(len(_ValueType_index)-1) { | ||
return "ValueType(" + strconv.FormatInt(int64(i), 10) + ")" | ||
} | ||
return _ValueType_name[_ValueType_index[i]:_ValueType_index[i+1]] | ||
} |
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 @@ | ||
../../../../vendor |