From 7df2d3d363751d75247e885eb6f4d3c65ea88864 Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Tue, 30 May 2023 16:43:45 -0700 Subject: [PATCH] `deprecated_index`: restore evaluation of JSON expressions --- rules/terraform_deprecated_index.go | 16 +++++++ rules/terraform_deprecated_index_test.go | 60 +++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/rules/terraform_deprecated_index.go b/rules/terraform_deprecated_index.go index 1700b8e..e302970 100644 --- a/rules/terraform_deprecated_index.go +++ b/rules/terraform_deprecated_index.go @@ -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" ) @@ -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) @@ -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 +} diff --git a/rules/terraform_deprecated_index_test.go b/rules/terraform_deprecated_index_test.go index 6a81936..c44bc5e 100644 --- a/rules/terraform_deprecated_index_test.go +++ b/rules/terraform_deprecated_index_test.go @@ -11,6 +11,7 @@ func Test_TerraformDeprecatedIndexRule(t *testing.T) { cases := []struct { Name string Content string + JSON bool Expected helper.Issues }{ { @@ -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)