Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated_index: restore evaluation of JSON expressions #101

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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