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

terraform_deprecated_index: Emit issues based on expression types #90

Merged
merged 1 commit into from
Apr 22, 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
93 changes: 47 additions & 46 deletions rules/terraform_deprecated_index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rules

import (
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
Expand Down Expand Up @@ -53,61 +55,60 @@ func (r *TerraformDeprecatedIndexRule) Check(runner tflint.Runner) error {
return err
}

diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(expr hcl.Expression) hcl.Diagnostics {
for _, variable := range expr.Variables() {
filename := expr.Range().Filename
file := files[filename]

bytes := expr.Range().SliceBytes(file.Bytes)

tokens, diags := hclsyntax.LexExpression(bytes, filename, variable.SourceRange().Start)
if diags.HasErrors() {
// HACK: If the expression cannot be lexed, try to lex it as a template.
// If it still cannot be lexed, return the original error.
tTokens, tDiags := hclsyntax.LexTemplate(bytes, filename, variable.SourceRange().Start)
if tDiags.HasErrors() {
return diags
}

tokens = tTokens
}

tokens = tokens[1:]

for i, token := range tokens {
if token.Type == hclsyntax.TokenDot {
if len(tokens) == i+1 {
return nil
}

next := tokens[i+1].Type
if next == hclsyntax.TokenNumberLit || next == hclsyntax.TokenStar {
if tokens[0].Type == hclsyntax.TokenDot {
if err := runner.EmitIssue(
r,
"List items should be accessed using square brackets",
expr.Range(),
); err != nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "failed to call EmitIssue()",
Detail: err.Error(),
},
}
}
}
diags := runner.WalkExpressions(tflint.ExprWalkFunc(func(e hcl.Expression) hcl.Diagnostics {
filename := e.Range().Filename
file := files[filename]

switch expr := e.(type) {
case *hclsyntax.ScopeTraversalExpr:
r.checkLegacyTraversalIndex(runner, expr.Traversal, file.Bytes)
case *hclsyntax.RelativeTraversalExpr:
r.checkLegacyTraversalIndex(runner, expr.Traversal, file.Bytes)
case *hclsyntax.SplatExpr:
if strings.HasPrefix(string(expr.MarkerRange.SliceBytes(file.Bytes)), ".") {
if err := runner.EmitIssue(
r,
"List items should be accessed using square brackets",
expr.MarkerRange,
); err != nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "failed to call EmitIssue()",
Detail: err.Error(),
},
}
}
}
}

return nil
}))

if diags.HasErrors() {
return diags
}

return nil
}

func (r *TerraformDeprecatedIndexRule) checkLegacyTraversalIndex(runner tflint.Runner, traversal hcl.Traversal, file []byte) hcl.Diagnostics {
for _, t := range traversal {
if _, ok := t.(hcl.TraverseIndex); ok {
if strings.HasPrefix(string(t.SourceRange().SliceBytes(file)), ".") {
if err := runner.EmitIssue(
r,
"List items should be accessed using square brackets",
t.SourceRange(),
); err != nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "failed to call EmitIssue()",
Detail: err.Error(),
},
}
}
}
}
}
return nil
}
77 changes: 72 additions & 5 deletions rules/terraform_deprecated_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ locals {
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 11,
Column: 15,
},
End: hcl.Pos{
Line: 4,
Expand All @@ -55,11 +55,11 @@ locals {
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 12,
Column: 19,
},
End: hcl.Pos{
Line: 4,
Column: 23,
Column: 21,
},
},
},
Expand Down Expand Up @@ -116,11 +116,78 @@ EOF
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 16,
Column: 36,
},
End: hcl.Pos{
Line: 4,
Column: 49,
Column: 38,
},
},
},
},
},
{
Name: "legacy splat and legacy index",
Content: `
locals {
nested_list = [["a"]]
value = nested_list.*.0
}
`,
Expected: helper.Issues{
{
Rule: NewTerraformDeprecatedIndexRule(),
Message: "List items should be accessed using square brackets",
Range: hcl.Range{
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 22,
},
End: hcl.Pos{
Line: 4,
Column: 24,
},
},
},
{
Rule: NewTerraformDeprecatedIndexRule(),
Message: "List items should be accessed using square brackets",
Range: hcl.Range{
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 24,
},
End: hcl.Pos{
Line: 4,
Column: 26,
},
},
},
},
},
{
Name: "complex expression",
Content: `
locals {
create_namespace = true
kubernetes_namespace = local.create_namespace ? join("", kubernetes_namespace.default.*.id) : var.kubernetes_namespace
}
`,
Expected: helper.Issues{
{
Rule: NewTerraformDeprecatedIndexRule(),
Message: "List items should be accessed using square brackets",
Range: hcl.Range{
Filename: "config.tf",
Start: hcl.Pos{
Line: 4,
Column: 88,
},
End: hcl.Pos{
Line: 4,
Column: 90,
},
},
},
Expand Down