-
Notifications
You must be signed in to change notification settings - Fork 349
/
references.go
50 lines (44 loc) · 1.54 KB
/
references.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package lang
import (
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint/terraform/addrs"
)
// References finds all of the references in the given set of traversals,
// returning diagnostics if any of the traversals cannot be interpreted as a
// reference.
//
// This function does not do any de-duplication of references, since references
// have source location information embedded in them and so any invalid
// references that are duplicated should have errors reported for each
// occurence.
//
// If the returned diagnostics contains errors then the result may be
// incomplete or invalid. Otherwise, the returned slice has one reference per
// given traversal, though it is not guaranteed that the references will
// appear in the same order as the given traversals.
func References(traversals []hcl.Traversal) ([]*addrs.Reference, hcl.Diagnostics) {
if len(traversals) == 0 {
return nil, nil
}
var diags hcl.Diagnostics
refs := make([]*addrs.Reference, 0, len(traversals))
for _, traversal := range traversals {
ref, refDiags := addrs.ParseRef(traversal)
diags = diags.Extend(refDiags)
if ref == nil {
continue
}
refs = append(refs, ref)
}
return refs, diags
}
// ReferencesInExpr is a helper wrapper around References that first searches
// the given expression for traversals, before converting those traversals
// to references.
func ReferencesInExpr(expr hcl.Expression) ([]*addrs.Reference, hcl.Diagnostics) {
if expr == nil {
return nil, nil
}
traversals := expr.Variables()
return References(traversals)
}