Skip to content

Commit

Permalink
deprecated_index: restore evaluation of JSON expressions (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker authored May 31, 2023
1 parent a3fbb47 commit f4458e8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
16 changes: 16 additions & 0 deletions rules/terraform_deprecated_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-terraform/project"
)
Expand Down Expand Up @@ -59,6 +60,11 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
filename := e.Range().Filename
file := files[filename]

if json.IsJSONExpression(e) {
r.checkJSONExpression(runner, e, file.Bytes)
return nil
}

switch expr := e.(type) {
case *hclsyntax.ScopeTraversalExpr:
r.checkLegacyTraversalIndex(runner, expr.Traversal, file.Bytes)
Expand Down Expand Up @@ -112,3 +118,13 @@ func (r *TerraformDeprecatedIndexRule) checkLegacyTraversalIndex(runner tflint.R
}
return nil
}

func (r *TerraformDeprecatedIndexRule) checkJSONExpression(runner tflint.Runner, e hcl.Expression, file []byte) hcl.Diagnostics {
var diags hcl.Diagnostics

for _, v := range e.Variables() {
diags = append(diags, r.checkLegacyTraversalIndex(runner, v, file)...)
}

return diags
}
60 changes: 59 additions & 1 deletion rules/terraform_deprecated_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func Test_TerraformDeprecatedIndexRule(t *testing.T) {
cases := []struct {
Name string
Content string
JSON bool
Expected helper.Issues
}{
{
Expand Down Expand Up @@ -193,13 +194,70 @@ locals {
},
},
},
{
Name: "json invalid",
JSON: true,
Content: `
{
"locals": {
"list": ["a"],
"value": "${list.0}"
}
}`,
Expected: helper.Issues{
{
Rule: NewTerraformDeprecatedIndexRule(),
Message: "List items should be accessed using square brackets",
Range: hcl.Range{
Filename: "config.tf.json",
Start: hcl.Pos{
Line: 5,
Column: 27,
},
End: hcl.Pos{
Line: 5,
Column: 29,
},
},
},
},
},
{
Name: "json valid",
JSON: true,
Content: `
{
"locals": {
"list": ["a"],
"value": "${list[0]}"
}
}`,
Expected: helper.Issues{},
},
{
Name: "json strings",
JSON: true,
Content: `
{
"locals": {
"string": "foo",
"bool": "${local.string == \"foo\"}"
}
}`,
Expected: helper.Issues{},
},
}

rule := NewTerraformDeprecatedIndexRule()

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
runner := helper.TestRunner(t, map[string]string{"config.tf": tc.Content})
filename := "config.tf"
if tc.JSON {
filename += ".json"
}

runner := helper.TestRunner(t, map[string]string{filename: tc.Content})

if err := rule.Check(runner); err != nil {
t.Fatalf("Unexpected error occurred: %s", err)
Expand Down

0 comments on commit f4458e8

Please sign in to comment.