This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
/
helpers.go
195 lines (183 loc) · 4.98 KB
/
helpers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package uast
import (
"sort"
"strings"
"github.com/bblfsh/sdk/v3/uast/nodes"
)
// AllImportPaths returns a list of all import paths in the UAST. Resulting import paths will be deduplicated and sorted.
//
// Path elements in QualifiedIdentifiers import will be joined by '/'. For example, Java import "com.example.pkg"
// will be listed as "com/example/pkg".
func AllImportPaths(root nodes.External) []string {
it := nodes.NewIterator(root, nodes.IterAny)
var (
paths []string
seen = make(map[string]struct{})
)
for it.Next() {
n := it.Node()
if n.Kind() != nodes.KindObject {
continue
}
path, ok := getImportPath(n)
if !ok || path == "" {
continue
} else if _, ok := seen[path]; ok {
continue
}
seen[path] = struct{}{}
paths = append(paths, path)
}
sort.Strings(paths)
return paths
}
// getImportPath returns a concatenated import path of any node derived from Import and returns false if conversion fails.
// See AllImportPaths for details.
//
// This function won't decode the Import and won't verify the schema, except for the Path field.
func getImportPath(imp nodes.External) (string, bool) {
typ := TypeOf(imp)
if !strings.HasPrefix(typ, NS+":") {
return "", false
}
switch typ {
default:
return "", false
case importType, runtimeImportType, runtimeReImportType, inlineImportType: // TODO: automatically build this list using reflection
}
var _ Import // helps to find Import usages in IDE
path, ok := getField(imp, "Path", nodes.KindObject)
if !ok || path == nil {
return "", false
}
return asImportPath(path)
}
// asImportPath returns a concatenated import path of any node suitable for Import.Path field.
func asImportPath(path nodes.External) (string, bool) {
switch TypeOf(path) {
case stringType:
return getStringValue(path)
case identifierType:
return getIdentifierName(path)
case qualifiedIdentifierType:
names, ok := getQualifiedIdentifierNames(path)
if !ok {
return "", false
}
return strings.Join(names, "/"), true
case aliasType:
n, ok := getAliasNode(path)
if !ok {
return "", false
}
return asImportPath(n)
default:
return "", false
}
}
// getField extracts a specified field from the node.
func getField(n nodes.External, key string, kind nodes.Kind) (nodes.External, bool) {
if n == nil || n.Kind() != nodes.KindObject {
return nil, false
}
switch obj := n.(type) {
case nodes.Object:
v, ok := obj[key]
if !ok || v == nil || !v.Kind().In(kind) {
return nil, false
}
return v, ok
case nodes.ExternalObject:
v, ok := obj.ValueAt(key)
if !ok || v == nil || !v.Kind().In(kind) {
return nil, false
}
return v, ok
default:
return nil, false
}
}
// getValueField extracts a specified value field from the node.
func getValueField(n nodes.External, key string, kind nodes.Kind) (nodes.Value, bool) {
v, ok := getField(n, key, kind)
if !ok {
return nil, false
}
if v, ok := v.(nodes.Value); ok {
return v, true
}
return v.Value(), true
}
// getStringField extracts a specified string field from the node.
func getStringField(n nodes.External, key string) (string, bool) {
v, ok := getValueField(n, key, nodes.KindString)
if !ok {
return "", false
}
s, ok := v.(nodes.String)
return string(s), ok
}
// getStringValue extracts the String.Value field from the node.
func getStringValue(n nodes.External) (string, bool) {
var _ String // helps to find String usages in IDE
if TypeOf(n) != stringType {
return "", false
}
return getStringField(n, "Value")
}
// getIdentifierName extracts the Identifier.Name field from the node.
func getIdentifierName(n nodes.External) (string, bool) {
var _ Identifier // helps to find Identifier usages in IDE
if TypeOf(n) != identifierType {
return "", false
}
return getStringField(n, "Name")
}
// getQualifiedIdentifierNames extracts the QualifiedIdentifier.Names field from the node.
func getQualifiedIdentifierNames(n nodes.External) ([]string, bool) {
var _ QualifiedIdentifier // may help to find usages
if TypeOf(n) != qualifiedIdentifierType {
return nil, false
}
arr, ok := getField(n, "Names", nodes.KindArray)
if !ok || arr == nil {
return nil, false
}
switch arr := arr.(type) {
case nodes.Array:
names := make([]string, 0, len(arr))
for _, v := range arr {
name, ok := getIdentifierName(v)
if !ok {
return nil, false
}
names = append(names, name)
}
return names, len(names) != 0
case nodes.ExternalArray:
sz := arr.Size()
names := make([]string, 0, sz)
for i := 0; i < sz; i++ {
v := arr.ValueAt(i)
if v == nil || TypeOf(v) != identifierType {
return nil, false
}
name, ok := getIdentifierName(v)
if !ok {
return nil, false
}
names = append(names, name)
}
return names, len(names) != 0
default:
return nil, false
}
}
// getAliasNode extracts the Alias.Node field from the node.
func getAliasNode(n nodes.External) (nodes.External, bool) {
var _ Alias // helps to find Alias usages in IDE
if TypeOf(n) != aliasType {
return nil, false
}
return getField(n, "Node", nodes.KindObject)
}