Skip to content

Commit 231568f

Browse files
authored
Add support for scoped data sources (#135)
1 parent 006e6de commit 231568f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

rules/terraform_unused_declarations.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,38 @@ func (r *TerraformUnusedDeclarationsRule) declarations(runner *terraform.Runner)
138138
LabelNames: []string{"type", "name"},
139139
Body: &hclext.BodySchema{},
140140
},
141+
{
142+
Type: "check",
143+
LabelNames: []string{"name"},
144+
Body: &hclext.BodySchema{
145+
Blocks: []hclext.BlockSchema{
146+
{
147+
Type: "data",
148+
LabelNames: []string{"type", "name"},
149+
Body: &hclext.BodySchema{},
150+
},
151+
},
152+
},
153+
},
141154
},
142155
}, &tflint.GetModuleContentOption{ExpandMode: tflint.ExpandModeNone})
143156
if err != nil {
144157
return decl, err
145158
}
146159

147160
for _, block := range body.Blocks {
148-
if block.Type == "variable" {
161+
switch block.Type {
162+
case "variable":
149163
decl.Variables[block.Labels[0]] = block
150-
} else {
164+
case "data":
151165
decl.DataResources[fmt.Sprintf("data.%s.%s", block.Labels[0], block.Labels[1])] = block
166+
case "check":
167+
for _, data := range block.Body.Blocks {
168+
// Scoped data source addresses are unique in the module
169+
decl.DataResources[fmt.Sprintf("data.%s.%s", data.Labels[0], data.Labels[1])] = data
170+
}
171+
default:
172+
panic("unreachable")
152173
}
153174
}
154175

rules/terraform_unused_declarations_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ variable "unused" {
201201
},
202202
},
203203
Fixed: `
204+
`,
205+
},
206+
{
207+
Name: "unused scoped data source",
208+
Content: `
209+
check "unused" {
210+
data "null_data_source" "unused" {}
211+
}
212+
`,
213+
Expected: helper.Issues{
214+
{
215+
Rule: NewTerraformUnusedDeclarationsRule(),
216+
Message: `data "null_data_source" "unused" is declared but not used`,
217+
Range: hcl.Range{
218+
Filename: "config.tf",
219+
Start: hcl.Pos{Line: 3, Column: 3},
220+
End: hcl.Pos{Line: 3, Column: 35},
221+
},
222+
},
223+
},
224+
Fixed: `
225+
check "unused" {
226+
}
204227
`,
205228
},
206229
{

0 commit comments

Comments
 (0)