-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.go
132 lines (125 loc) · 2.84 KB
/
fix.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
package kubevelafix
import (
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
)
// Fix fixes the CUE code represented by x so that expressions
// of the form:
//
// { for k in parameter.foo { ... }}
//
// are rewritten to:
//
// { for k in *parameter.foo | {} { ... }}
//
// The node should have been parsed with the [parser.ParseComments]
// option.
func Fix(x ast.Node) ast.Node {
return astutil.Apply(x, visit, nil)
}
func visit(c astutil.Cursor) bool {
switch n := c.Node().(type) {
case *ast.StructLit:
visitClauses(n.Elts)
return false
case *ast.File:
visitClauses(n.Decls)
return false
default:
return true
}
}
func visitClauses[T ast.Node](elts []T) {
for i, e := range elts {
c, ok := any(e).(*ast.Comprehension)
if !ok {
elts[i] = astutil.Apply(e, visit, nil).(T)
continue
}
var guarded map[string]bool
for i, clause := range c.Clauses {
var field string
if isGuard(clause, &field) {
if guarded == nil {
guarded = make(map[string]bool)
}
guarded[field] = true
}
forClause, ok := clause.(*ast.ForClause)
if !ok || !isParameterDot(forClause.Source, &field) || guarded[field] {
c.Clauses[i] = astutil.Apply(clause, visit, nil).(ast.Clause)
continue
}
forClause.Source = &ast.BinaryExpr{
X: &ast.UnaryExpr{
Op: token.MUL,
X: forClause.Source,
},
Op: token.OR,
Y: &ast.StructLit{},
}
}
}
}
// isGuard reports whether x is a clause of the form
// if parameter.foo != _|_
// If field is non-nil and it returns true, it fills in *field
// with the name of the parameter field (foo above).
func isGuard(x0 ast.Clause, field *string) bool {
x, ok := x0.(*ast.IfClause)
if !ok {
return false
}
e, ok := x.Condition.(*ast.BinaryExpr)
if !ok || e.Op != token.NEQ || !is[*ast.BottomLit](e.Y) || !isParameterDot(e.X, field) {
return false
}
return true
}
func is[T any](x any) bool {
_, ok := x.(T)
return ok
}
// isParameterDot reports whether x represents an expression
// such as:
// parameter.foo
// parameter.foo.bar
// parameter["foo"]
//
// If field is non-nil and it returns true, it fills in *field
// with the name of the parameter field (foo above).
func isParameterDot(x ast.Expr, field *string) bool {
var lhs ast.Expr
var rhs ast.Node
switch x := x.(type) {
case *ast.SelectorExpr:
lhs = x.X
rhs = x.Sel
case *ast.IndexExpr:
lhs = x.X
rhs = x.Index
default:
return false
}
switch lhs := lhs.(type) {
case *ast.Ident:
if lhs.Name == "parameter" {
if field != nil {
switch rhs := rhs.(type) {
case *ast.Ident:
*field = rhs.Name
case *ast.BasicLit:
if rhs.Kind == token.STRING {
*field, _ = literal.Unquote(rhs.Value)
}
}
}
return true
}
case *ast.SelectorExpr, *ast.IndexExpr:
return isParameterDot(lhs, field)
}
return false
}