Skip to content

Commit 0b3de13

Browse files
authored
terraform_deprecated_index: handle Terraform directives (HCL template) (#43)
1 parent f88d3ec commit 0b3de13

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

rules/terraform_deprecated_index.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
6666

6767
tokens, diags := hclsyntax.LexExpression(bytes, filename, variable.SourceRange().Start)
6868
if diags.HasErrors() {
69-
return diags
69+
// HACK: If the expression cannot be lexed, try to lex it as a template.
70+
// If it still cannot be lexed, return the original error.
71+
tTokens, tDiags := hclsyntax.LexTemplate(bytes, filename, variable.SourceRange().Start)
72+
if tDiags.HasErrors() {
73+
return diags
74+
}
75+
76+
tokens = tTokens
7077
}
7178

7279
tokens = tokens[1:]

rules/terraform_deprecated_index_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,48 @@ locals {
8484
`,
8585
Expected: helper.Issues{},
8686
},
87+
{
88+
Name: "directive: valid",
89+
Content: `
90+
locals {
91+
servers = <<EOF
92+
%{ for ip in aws_instance.example[*].private_ip }
93+
server ${ip}
94+
%{ endfor }
95+
EOF
96+
}
97+
`,
98+
Expected: helper.Issues{},
99+
},
100+
{
101+
Name: "directive: invalid",
102+
Content: `
103+
locals {
104+
servers = <<EOF
105+
%{ for ip in aws_instance.example.*.private_ip }
106+
server ${ip}
107+
%{ endfor }
108+
EOF
109+
}
110+
`,
111+
Expected: helper.Issues{
112+
{
113+
Rule: NewTerraformDeprecatedIndexRule(),
114+
Message: "List items should be accessed using square brackets",
115+
Range: hcl.Range{
116+
Filename: "config.tf",
117+
Start: hcl.Pos{
118+
Line: 4,
119+
Column: 16,
120+
},
121+
End: hcl.Pos{
122+
Line: 4,
123+
Column: 49,
124+
},
125+
},
126+
},
127+
},
128+
},
87129
}
88130

89131
rule := NewTerraformDeprecatedIndexRule()

0 commit comments

Comments
 (0)