Skip to content

Commit f51d6f3

Browse files
authored
Replace referencesInExpr with function in SDK (#5)
1 parent 8dbed4f commit f51d6f3

File tree

5 files changed

+34
-151
lines changed

5 files changed

+34
-151
lines changed

go.mod

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ go 1.19
44

55
require (
66
github.com/Masterminds/semver/v3 v3.1.1
7+
github.com/google/go-cmp v0.5.8
78
github.com/hashicorp/go-getter v1.6.2
89
github.com/hashicorp/go-version v1.6.0
910
github.com/hashicorp/hcl/v2 v2.13.0
1011
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
11-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220813132108-e52a563fa4ce
12+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929
1213
github.com/zclconf/go-cty v1.10.0
1314
)
1415

@@ -23,7 +24,6 @@ require (
2324
github.com/fatih/color v1.13.0 // indirect
2425
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
2526
github.com/golang/protobuf v1.5.2 // indirect
26-
github.com/google/go-cmp v0.5.8 // indirect
2727
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
2828
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
2929
github.com/hashicorp/go-hclog v1.2.2 // indirect
@@ -45,13 +45,12 @@ require (
4545
github.com/vmihailenco/tagparser v0.1.1 // indirect
4646
go.opencensus.io v0.22.3 // indirect
4747
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
48-
golang.org/x/mod v0.2.0 // indirect
49-
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
48+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
49+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
5050
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
5151
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
52-
golang.org/x/text v0.3.6 // indirect
53-
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4 // indirect
54-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
52+
golang.org/x/text v0.3.7 // indirect
53+
golang.org/x/tools v0.1.11 // indirect
5554
google.golang.org/api v0.29.0 // indirect
5655
google.golang.org/appengine v1.6.5 // indirect
5756
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect

go.sum

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
195195
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
196196
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
197197
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
198-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220813132108-e52a563fa4ce h1:ZbHTdkpzQwTHL/UFt04Dehqc0yWlRTvp5LCCrvva3M8=
199-
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220813132108-e52a563fa4ce/go.mod h1:ajEKpdl5npedBy1EeOADfruuh8KqfF1V3lFnsJAVcXs=
198+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929 h1:YcqKJVo57QsUG2XV+8MYGTUZgLe/AeazMPj7FcRHNJY=
199+
github.com/terraform-linters/tflint-plugin-sdk v0.11.1-0.20220820132643-9bf8cad3b929/go.mod h1:E+gbw/AE/SDuvqh2RgVykKuQLleDd/iYP55DE1lLKac=
200200
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
201201
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
202202
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
@@ -248,8 +248,9 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG
248248
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
249249
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
250250
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
251-
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
252251
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
252+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
253+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
253254
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
254255
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
255256
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -272,8 +273,9 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL
272273
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
273274
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
274275
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
275-
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 h1:003p0dJM77cxMSyCPFphvZf/Y5/NXf5fzg6ufd1/Oew=
276276
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
277+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
278+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
277279
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
278280
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
279281
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -286,8 +288,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
286288
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
287289
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
288290
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
289-
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
290291
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
292+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
291293
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
292294
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
293295
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -323,8 +325,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
323325
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
324326
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
325327
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
326-
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
327-
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
328+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
329+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
328330
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
329331
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
330332
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -357,12 +359,12 @@ golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapK
357359
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
358360
golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
359361
golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
360-
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4 h1:kDtqNkeBrZb8B+atrj50B5XLHpzXXqcCdZPP/ApQ5NY=
361362
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
363+
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
364+
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
362365
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
363366
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
364367
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
365-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
366368
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
367369
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
368370
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=

rules/terraform_unused_declaration.go

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

66
"github.com/hashicorp/hcl/v2"
77
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
8+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
9+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang"
810
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
911
"github.com/terraform-linters/tflint-ruleset-terraform/project"
1012
)
@@ -141,14 +143,16 @@ func (r *TerraformUnusedDeclarationsRule) declarations(runner tflint.Runner) (*d
141143
}
142144

143145
func (r *TerraformUnusedDeclarationsRule) checkForRefsInExpr(expr hcl.Expression, decl *declarations) error {
144-
for _, ref := range referencesInExpr(expr) {
145-
switch sub := ref.subject.(type) {
146-
case inputVariableReference:
147-
delete(decl.Variables, sub.name)
148-
case localValueReference:
149-
delete(decl.Locals, sub.name)
150-
case dataResourceReference:
146+
for _, ref := range lang.ReferencesInExpr(expr) {
147+
switch sub := ref.Subject.(type) {
148+
case addrs.InputVariable:
149+
delete(decl.Variables, sub.Name)
150+
case addrs.LocalValue:
151+
delete(decl.Locals, sub.Name)
152+
case addrs.Resource:
151153
delete(decl.DataResources, sub.String())
154+
case addrs.ResourceInstance:
155+
delete(decl.DataResources, sub.Resource.String())
152156
}
153157
}
154158

rules/terraform_workspace_remote.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/hashicorp/hcl/v2/hclsyntax"
66
"github.com/hashicorp/hcl/v2/json"
77
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
8+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
9+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang"
810
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
911
"github.com/terraform-linters/tflint-ruleset-terraform/project"
1012
)
@@ -94,10 +96,10 @@ func (r *TerraformWorkspaceRemoteRule) checkForTerraformWorkspaceInExpr(runner t
9496
return nil
9597
}
9698

97-
for _, ref := range referencesInExpr(expr) {
98-
switch sub := ref.subject.(type) {
99-
case terraformReference:
100-
if sub.name == "workspace" {
99+
for _, ref := range lang.ReferencesInExpr(expr) {
100+
switch sub := ref.Subject.(type) {
101+
case addrs.TerraformAttr:
102+
if sub.Name == "workspace" {
101103
return runner.EmitIssue(
102104
r,
103105
"terraform.workspace should not be used with a 'remote' backend",

rules/utils.go

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rules
22

33
import (
4-
"fmt"
54
"strings"
65

76
"github.com/hashicorp/go-version"
@@ -296,129 +295,6 @@ func decodeProviderRef(expr hcl.Expression, defRange hcl.Range) (*providerRef, h
296295
}, nil
297296
}
298297

299-
type reference struct {
300-
subject referencable
301-
}
302-
303-
type referencable interface {
304-
referenceableSigil()
305-
}
306-
307-
type inputVariableReference struct {
308-
referencable
309-
name string
310-
}
311-
312-
type localValueReference struct {
313-
referencable
314-
name string
315-
}
316-
317-
type terraformReference struct {
318-
referencable
319-
name string
320-
}
321-
322-
type dataResourceReference struct {
323-
referencable
324-
typeName string
325-
name string
326-
}
327-
328-
func (d *dataResourceReference) String() string {
329-
return fmt.Sprintf("data.%s.%s", d.typeName, d.name)
330-
}
331-
332-
// referencesInExpr extracts and returns references from hcl.Expression.
333-
// This is roughly equivalent to ReferencesInExpr in Terraform interlan API,
334-
// but simply ignores invalid references without returing diagnostics.
335-
// This is because it is not the responsibility of this method to detect invalid references.
336-
// Invalid references should be caught by terraform validate.
337-
//
338-
// @see https://github.com/hashicorp/terraform/blob/v1.2.5/internal/lang/references.go#L75
339-
func referencesInExpr(expr hcl.Expression) []*reference {
340-
references := []*reference{}
341-
342-
for _, traversal := range expr.Variables() {
343-
name := traversal.RootName()
344-
345-
switch name {
346-
case "var":
347-
name, diags := parseSingleAttrRef(traversal)
348-
if diags.HasErrors() {
349-
continue
350-
}
351-
references = append(references, &reference{subject: inputVariableReference{name: name}})
352-
case "local":
353-
name, diags := parseSingleAttrRef(traversal)
354-
if diags.HasErrors() {
355-
continue
356-
}
357-
references = append(references, &reference{subject: localValueReference{name: name}})
358-
case "terraform":
359-
name, diags := parseSingleAttrRef(traversal)
360-
if diags.HasErrors() {
361-
continue
362-
}
363-
references = append(references, &reference{subject: terraformReference{name: name}})
364-
case "data":
365-
if len(traversal) < 3 {
366-
// The "data" object must be followed by two attribute names: the data source type and the resource name.
367-
continue
368-
}
369-
370-
var typeName string
371-
switch tt := traversal[1].(type) {
372-
case hcl.TraverseRoot:
373-
typeName = tt.Name
374-
case hcl.TraverseAttr:
375-
typeName = tt.Name
376-
default:
377-
// The "data" object does not support this operation.
378-
continue
379-
}
380-
381-
attrTrav, ok := traversal[2].(hcl.TraverseAttr)
382-
if !ok {
383-
// A reference to a data source must be followed by at least one attribute access, specifying the resource name.
384-
continue
385-
}
386-
387-
references = append(references, &reference{subject: dataResourceReference{typeName: typeName, name: attrTrav.Name}})
388-
}
389-
}
390-
391-
return references
392-
}
393-
394-
// @see https://github.com/hashicorp/terraform/blob/v1.2.5/internal/addrs/parse_ref.go#L392
395-
func parseSingleAttrRef(traversal hcl.Traversal) (string, hcl.Diagnostics) {
396-
var diags hcl.Diagnostics
397-
398-
root := traversal.RootName()
399-
rootRange := traversal[0].SourceRange()
400-
401-
if len(traversal) < 2 {
402-
diags = diags.Append(&hcl.Diagnostic{
403-
Severity: hcl.DiagError,
404-
Summary: "Invalid reference",
405-
Detail: fmt.Sprintf("The %q object cannot be accessed directly. Instead, access one of its attributes.", root),
406-
Subject: &rootRange,
407-
})
408-
return "", diags
409-
}
410-
if attrTrav, ok := traversal[1].(hcl.TraverseAttr); ok {
411-
return attrTrav.Name, diags
412-
}
413-
diags = diags.Append(&hcl.Diagnostic{
414-
Severity: hcl.DiagError,
415-
Summary: "Invalid reference",
416-
Detail: fmt.Sprintf("The %q object does not support this operation.", root),
417-
Subject: traversal[1].SourceRange().Ptr(),
418-
})
419-
return "", diags
420-
}
421-
422298
// @see https://github.com/hashicorp/terraform/blob/v1.2.5/internal/configs/compat_shim.go#L34
423299
func shimTraversalInString(expr hcl.Expression) (hcl.Expression, hcl.Diagnostics) {
424300
// ObjectConsKeyExpr is a special wrapper type used for keys on object

0 commit comments

Comments
 (0)