Skip to content

Commit f4458e8

Browse files
authored
deprecated_index: restore evaluation of JSON expressions (#101)
1 parent a3fbb47 commit f4458e8

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

rules/terraform_deprecated_index.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/hcl/v2"
77
"github.com/hashicorp/hcl/v2/hclsyntax"
8+
"github.com/hashicorp/hcl/v2/json"
89
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
910
"github.com/terraform-linters/tflint-ruleset-terraform/project"
1011
)
@@ -59,6 +60,11 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
5960
filename := e.Range().Filename
6061
file := files[filename]
6162

63+
if json.IsJSONExpression(e) {
64+
r.checkJSONExpression(runner, e, file.Bytes)
65+
return nil
66+
}
67+
6268
switch expr := e.(type) {
6369
case *hclsyntax.ScopeTraversalExpr:
6470
r.checkLegacyTraversalIndex(runner, expr.Traversal, file.Bytes)
@@ -112,3 +118,13 @@ func (r *TerraformDeprecatedIndexRule) checkLegacyTraversalIndex(runner tflint.R
112118
}
113119
return nil
114120
}
121+
122+
func (r *TerraformDeprecatedIndexRule) checkJSONExpression(runner tflint.Runner, e hcl.Expression, file []byte) hcl.Diagnostics {
123+
var diags hcl.Diagnostics
124+
125+
for _, v := range e.Variables() {
126+
diags = append(diags, r.checkLegacyTraversalIndex(runner, v, file)...)
127+
}
128+
129+
return diags
130+
}

rules/terraform_deprecated_index_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func Test_TerraformDeprecatedIndexRule(t *testing.T) {
1111
cases := []struct {
1212
Name string
1313
Content string
14+
JSON bool
1415
Expected helper.Issues
1516
}{
1617
{
@@ -193,13 +194,70 @@ locals {
193194
},
194195
},
195196
},
197+
{
198+
Name: "json invalid",
199+
JSON: true,
200+
Content: `
201+
{
202+
"locals": {
203+
"list": ["a"],
204+
"value": "${list.0}"
205+
}
206+
}`,
207+
Expected: helper.Issues{
208+
{
209+
Rule: NewTerraformDeprecatedIndexRule(),
210+
Message: "List items should be accessed using square brackets",
211+
Range: hcl.Range{
212+
Filename: "config.tf.json",
213+
Start: hcl.Pos{
214+
Line: 5,
215+
Column: 27,
216+
},
217+
End: hcl.Pos{
218+
Line: 5,
219+
Column: 29,
220+
},
221+
},
222+
},
223+
},
224+
},
225+
{
226+
Name: "json valid",
227+
JSON: true,
228+
Content: `
229+
{
230+
"locals": {
231+
"list": ["a"],
232+
"value": "${list[0]}"
233+
}
234+
}`,
235+
Expected: helper.Issues{},
236+
},
237+
{
238+
Name: "json strings",
239+
JSON: true,
240+
Content: `
241+
{
242+
"locals": {
243+
"string": "foo",
244+
"bool": "${local.string == \"foo\"}"
245+
}
246+
}`,
247+
Expected: helper.Issues{},
248+
},
196249
}
197250

198251
rule := NewTerraformDeprecatedIndexRule()
199252

200253
for _, tc := range cases {
201254
t.Run(tc.Name, func(t *testing.T) {
202-
runner := helper.TestRunner(t, map[string]string{"config.tf": tc.Content})
255+
filename := "config.tf"
256+
if tc.JSON {
257+
filename += ".json"
258+
}
259+
260+
runner := helper.TestRunner(t, map[string]string{filename: tc.Content})
203261

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

0 commit comments

Comments
 (0)