forked from google/go-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_variables.go
76 lines (68 loc) · 1.84 KB
/
find_variables.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
package linter
import (
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/parser"
)
type vScope map[ast.Identifier]*variable
func addVar(name ast.Identifier, node ast.Node, info *LintingInfo, scope vScope, param bool) {
v := variable{
name: name,
declNode: node,
uses: nil,
param: param,
}
scope[name] = &v
info.variables = append(info.variables, v)
}
func cloneScope(oldScope vScope) vScope {
new := make(vScope)
for k, v := range oldScope {
new[k] = v
}
return new
}
func findVariablesInFunc(node *ast.Function, info *LintingInfo, scope vScope) {
for _, param := range node.Parameters.Required {
addVar(param, node, info, scope, true)
}
for _, param := range node.Parameters.Optional {
addVar(param.Name, node, info, scope, true)
}
for _, param := range node.Parameters.Optional {
findVariables(param.DefaultArg, info, scope)
}
findVariables(node.Body, info, scope)
}
func findVariablesInLocal(node *ast.Local, info *LintingInfo, scope vScope) {
for _, bind := range node.Binds {
addVar(bind.Variable, node, info, scope, false)
}
for _, bind := range node.Binds {
if bind.Fun != nil {
newScope := cloneScope(scope)
findVariablesInFunc(bind.Fun, info, newScope)
} else {
findVariables(bind.Body, info, scope)
}
}
}
func findVariables(node ast.Node, info *LintingInfo, scope vScope) {
switch node := node.(type) {
case *ast.Function:
newScope := cloneScope(scope)
findVariablesInFunc(node, info, newScope)
case *ast.Local:
newScope := cloneScope(scope)
findVariablesInLocal(node, info, newScope)
case *ast.Var:
if v, ok := scope[node.Id]; ok {
v.uses = append(v.uses, node)
} else {
panic("Undeclared variable " + string(node.Id) + " - it should be caught earlier")
}
default:
for _, child := range parser.Children(node) {
findVariables(child, info, scope)
}
}
}