Skip to content

Commit

Permalink
Add support for scoped data sources (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Oct 9, 2023
1 parent 006e6de commit 231568f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
25 changes: 23 additions & 2 deletions rules/terraform_unused_declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,38 @@ func (r *TerraformUnusedDeclarationsRule) declarations(runner *terraform.Runner)
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{},
},
{
Type: "check",
LabelNames: []string{"name"},
Body: &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "data",
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{},
},
},
},
},
},
}, &tflint.GetModuleContentOption{ExpandMode: tflint.ExpandModeNone})
if err != nil {
return decl, err
}

for _, block := range body.Blocks {
if block.Type == "variable" {
switch block.Type {
case "variable":
decl.Variables[block.Labels[0]] = block
} else {
case "data":
decl.DataResources[fmt.Sprintf("data.%s.%s", block.Labels[0], block.Labels[1])] = block
case "check":
for _, data := range block.Body.Blocks {
// Scoped data source addresses are unique in the module
decl.DataResources[fmt.Sprintf("data.%s.%s", data.Labels[0], data.Labels[1])] = data
}
default:
panic("unreachable")
}
}

Expand Down
23 changes: 23 additions & 0 deletions rules/terraform_unused_declarations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,29 @@ variable "unused" {
},
},
Fixed: `
`,
},
{
Name: "unused scoped data source",
Content: `
check "unused" {
data "null_data_source" "unused" {}
}
`,
Expected: helper.Issues{
{
Rule: NewTerraformUnusedDeclarationsRule(),
Message: `data "null_data_source" "unused" is declared but not used`,
Range: hcl.Range{
Filename: "config.tf",
Start: hcl.Pos{Line: 3, Column: 3},
End: hcl.Pos{Line: 3, Column: 35},
},
},
},
Fixed: `
check "unused" {
}
`,
},
{
Expand Down

0 comments on commit 231568f

Please sign in to comment.