Skip to content

Commit e989fa6

Browse files
authored
Replace WalkExpressions with SDK's one (#6)
1 parent f51d6f3 commit e989fa6

File tree

8 files changed

+85
-83
lines changed

8 files changed

+85
-83
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/hashicorp/go-version v1.6.0
1010
github.com/hashicorp/hcl/v2 v2.13.0
1111
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
12-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929
12+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821
1313
github.com/zclconf/go-cty v1.10.0
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
195195
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
196196
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
197197
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
198-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929 h1:YcqKJVo57QsUG2XV+8MYGTUZgLe/AeazMPj7FcRHNJY=
199-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929/go.mod h1:E+gbw/AE/SDuvqh2RgVykKuQLleDd/iYP55DE1lLKac=
198+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821 h1:EcgqevoO56J8zEbiEKVesICh4CaPY6+/JWbtxLnuUFw=
199+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220821130728-3b6ea1aa3821/go.mod h1:E+gbw/AE/SDuvqh2RgVykKuQLleDd/iYP55DE1lLKac=
200200
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
201201
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
202202
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

rules/terraform_deprecated_index.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,20 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
4848
return nil
4949
}
5050

51-
return WalkExpressions(runner, func(expr hcl.Expression) error {
51+
diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
5252
for _, variable := range expr.Variables() {
5353
for _, traversal := range variable.SimpleSplit().Rel {
5454
if traversal, ok := traversal.(hcl.TraverseIndex); ok {
5555
filename := traversal.SrcRange.Filename
5656
file, err := runner.GetFile(filename)
5757
if err != nil {
58-
return err
58+
return hcl.Diagnostics{
59+
{
60+
Severity: hcl.DiagError,
61+
Summary: "failed to call GetFile()",
62+
Detail: err.Error(),
63+
},
64+
}
5965
}
6066
bytes := traversal.SrcRange.SliceBytes(file.Bytes)
6167

@@ -70,13 +76,24 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
7076
"List items should be accessed using square brackets",
7177
expr.Range(),
7278
); err != nil {
73-
return err
79+
return hcl.Diagnostics{
80+
{
81+
Severity: hcl.DiagError,
82+
Summary: "failed to call EmitIssue()",
83+
Detail: err.Error(),
84+
},
85+
}
7486
}
7587
}
7688
}
7789
}
7890
}
7991

8092
return nil
81-
})
93+
}))
94+
if diags.HasErrors() {
95+
return diags
96+
}
97+
98+
return nil
8299
}

rules/terraform_deprecated_interpolation.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,33 @@ func (r *TerraformDeprecatedInterpolationRule) Check(runner tflint.Runner) error
5050
return nil
5151
}
5252

53-
return WalkExpressions(runner, func(expr hcl.Expression) error {
53+
diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
5454
return r.checkForDeprecatedInterpolationsInExpr(runner, expr)
55-
})
55+
}))
56+
if diags.HasErrors() {
57+
return diags
58+
}
59+
return nil
5660
}
5761

58-
func (r *TerraformDeprecatedInterpolationRule) checkForDeprecatedInterpolationsInExpr(runner tflint.Runner, expr hcl.Expression) error {
62+
func (r *TerraformDeprecatedInterpolationRule) checkForDeprecatedInterpolationsInExpr(runner tflint.Runner, expr hcl.Expression) hcl.Diagnostics {
5963
if _, ok := expr.(*hclsyntax.TemplateWrapExpr); !ok {
6064
return nil
6165
}
6266

63-
return runner.EmitIssue(
67+
err := runner.EmitIssue(
6468
r,
6569
"Interpolation-only expressions are deprecated in Terraform v0.12.14",
6670
expr.Range(),
6771
)
72+
if err != nil {
73+
return hcl.Diagnostics{
74+
{
75+
Severity: hcl.DiagError,
76+
Summary: "failed to call EmitIssue()",
77+
Detail: err.Error(),
78+
},
79+
}
80+
}
81+
return nil
6882
}

rules/terraform_empty_list_equality.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,41 @@ func (r *TerraformEmptyListEqualityRule) Check(runner tflint.Runner) error {
4949
return nil
5050
}
5151

52-
if err := r.checkEmptyList(runner); err != nil {
53-
return err
52+
if diags := r.checkEmptyList(runner); diags.HasErrors() {
53+
return diags
5454
}
5555

5656
return nil
5757
}
5858

5959
// checkEmptyList visits all blocks that can contain expressions and checks for comparisons with static empty list
60-
func (r *TerraformEmptyListEqualityRule) checkEmptyList(runner tflint.Runner) error {
61-
return WalkExpressions(runner, func(expr hcl.Expression) error {
60+
func (r *TerraformEmptyListEqualityRule) checkEmptyList(runner tflint.Runner) hcl.Diagnostics {
61+
return runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
6262
if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok && binaryOpExpr.Op.Type == cty.Bool {
6363
if tupleConsExpr, ok := binaryOpExpr.LHS.(*hclsyntax.TupleConsExpr); ok && len(tupleConsExpr.Exprs) == 0 {
6464
if err := r.emitIssue(binaryOpExpr.Range(), runner); err != nil {
65-
return err
65+
return hcl.Diagnostics{
66+
{
67+
Severity: hcl.DiagError,
68+
Summary: "failed to call EmitIssue()",
69+
Detail: err.Error(),
70+
},
71+
}
6672
}
6773
} else if tupleConsExpr, ok := binaryOpExpr.RHS.(*hclsyntax.TupleConsExpr); ok && len(tupleConsExpr.Exprs) == 0 {
6874
if err := r.emitIssue(binaryOpExpr.Range(), runner); err != nil {
69-
return err
75+
return hcl.Diagnostics{
76+
{
77+
Severity: hcl.DiagError,
78+
Summary: "failed to call EmitIssue()",
79+
Detail: err.Error(),
80+
},
81+
}
7082
}
7183
}
7284
}
7385
return nil
74-
})
86+
}))
7587
}
7688

7789
// emitIssue emits issue for comparison with static empty list

rules/terraform_unused_declaration.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ func (r *TerraformUnusedDeclarationsRule) Check(runner tflint.Runner) error {
6262
if err != nil {
6363
return err
6464
}
65-
err = WalkExpressions(runner, func(expr hcl.Expression) error {
66-
return r.checkForRefsInExpr(expr, decl)
67-
})
68-
if err != nil {
69-
return err
65+
diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
66+
r.checkForRefsInExpr(expr, decl)
67+
return nil
68+
}))
69+
if diags.HasErrors() {
70+
return diags
7071
}
7172

7273
for _, variable := range decl.Variables {
@@ -142,7 +143,7 @@ func (r *TerraformUnusedDeclarationsRule) declarations(runner tflint.Runner) (*d
142143
return decl, nil
143144
}
144145

145-
func (r *TerraformUnusedDeclarationsRule) checkForRefsInExpr(expr hcl.Expression, decl *declarations) error {
146+
func (r *TerraformUnusedDeclarationsRule) checkForRefsInExpr(expr hcl.Expression, decl *declarations) {
146147
for _, ref := range lang.ReferencesInExpr(expr) {
147148
switch sub := ref.Subject.(type) {
148149
case addrs.InputVariable:
@@ -155,6 +156,4 @@ func (r *TerraformUnusedDeclarationsRule) checkForRefsInExpr(expr hcl.Expression
155156
delete(decl.DataResources, sub.Resource.String())
156157
}
157158
}
158-
159-
return nil
160159
}

rules/terraform_workspace_remote.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ func (r *TerraformWorkspaceRemoteRule) Check(runner tflint.Runner) error {
8585
return nil
8686
}
8787

88-
return WalkExpressions(runner, func(expr hcl.Expression) error {
88+
diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
8989
return r.checkForTerraformWorkspaceInExpr(runner, expr)
90-
})
90+
}))
91+
if diags.HasErrors() {
92+
return diags
93+
}
94+
return nil
9195
}
9296

93-
func (r *TerraformWorkspaceRemoteRule) checkForTerraformWorkspaceInExpr(runner tflint.Runner, expr hcl.Expression) error {
97+
func (r *TerraformWorkspaceRemoteRule) checkForTerraformWorkspaceInExpr(runner tflint.Runner, expr hcl.Expression) hcl.Diagnostics {
9498
_, isScopeTraversalExpr := expr.(*hclsyntax.ScopeTraversalExpr)
9599
if !isScopeTraversalExpr && !json.IsJSONExpression(expr) {
96100
return nil
@@ -100,11 +104,20 @@ func (r *TerraformWorkspaceRemoteRule) checkForTerraformWorkspaceInExpr(runner t
100104
switch sub := ref.Subject.(type) {
101105
case addrs.TerraformAttr:
102106
if sub.Name == "workspace" {
103-
return runner.EmitIssue(
107+
err := runner.EmitIssue(
104108
r,
105109
"terraform.workspace should not be used with a 'remote' backend",
106110
expr.Range(),
107111
)
112+
if err != nil {
113+
return hcl.Diagnostics{
114+
{
115+
Severity: hcl.DiagError,
116+
Summary: "failed to call EmitIssue()",
117+
Detail: err.Error(),
118+
},
119+
}
120+
}
108121
}
109122
}
110123
}

rules/utils.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,6 @@ import (
1111
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1212
)
1313

14-
// WalkExpressions visits all expressions, including those in the file before merging.
15-
// Note that it behaves differently in native HCL syntax and JSON syntax.
16-
// In the HCL syntax, expressions in expressions, such as list and object are passed to
17-
// the walker function. The walker should check the type of the expression.
18-
// In the JSON syntax, only an expression of an attribute seen from the top level of the file
19-
// is passed, not expressions in expressions to the walker. This is an API limitation of JSON syntax.
20-
//
21-
// XXX: Should be moved to SDK?
22-
func WalkExpressions(r tflint.Runner, walker func(hcl.Expression) error) error {
23-
visit := func(node hclsyntax.Node) hcl.Diagnostics {
24-
if expr, ok := node.(hcl.Expression); ok {
25-
if err := walker(expr); err != nil {
26-
// FIXME: walker should returns hcl.Diagnostics directly
27-
return hcl.Diagnostics{
28-
{
29-
Severity: hcl.DiagError,
30-
Summary: err.Error(),
31-
},
32-
}
33-
}
34-
}
35-
return hcl.Diagnostics{}
36-
}
37-
38-
files, err := r.GetFiles()
39-
if err != nil {
40-
return err
41-
}
42-
for _, file := range files {
43-
if body, ok := file.Body.(*hclsyntax.Body); ok {
44-
diags := hclsyntax.VisitAll(body, visit)
45-
if diags.HasErrors() {
46-
return diags
47-
}
48-
continue
49-
}
50-
51-
// In JSON syntax, everything can be walked as an attribute.
52-
attrs, diags := file.Body.JustAttributes()
53-
if diags.HasErrors() {
54-
return diags
55-
}
56-
57-
for _, attr := range attrs {
58-
if err := walker(attr.Expr); err != nil {
59-
return err
60-
}
61-
}
62-
}
63-
64-
return nil
65-
}
66-
6714
type moduleCall struct {
6815
name string
6916
defRange hcl.Range

0 commit comments

Comments
 (0)