forked from cue-lang/cue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplify.go
113 lines (98 loc) · 2.68 KB
/
simplify.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
// Copyright 2019 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package format
import (
"strconv"
"github.com/wylswz/cue-se/cue/ast"
"github.com/wylswz/cue-se/cue/ast/astutil"
"github.com/wylswz/cue-se/internal"
)
// labelSimplifier rewrites string labels to identifiers if
// no identifiers will subsequently bind to the exposed label.
// In other words, string labels are only replaced if this does
// not change the semantics of the CUE code.
type labelSimplifier struct {
parent *labelSimplifier
scope map[string]bool
}
func (s *labelSimplifier) processDecls(decls []ast.Decl) {
sc := labelSimplifier{parent: s, scope: map[string]bool{}}
for _, d := range decls {
switch x := d.(type) {
case *ast.Field:
ast.Walk(x.Label, sc.markStrings, nil)
}
}
for _, d := range decls {
switch x := d.(type) {
case *ast.Field:
ast.Walk(x.Value, sc.markReferences, nil)
default:
ast.Walk(x, sc.markReferences, nil)
}
}
for _, d := range decls {
switch x := d.(type) {
case *ast.Field:
x.Label = astutil.Apply(x.Label, sc.replace, nil).(ast.Label)
}
}
}
func (s *labelSimplifier) markReferences(n ast.Node) bool {
// Record strings at this level.
switch x := n.(type) {
case *ast.File:
s.processDecls(x.Decls)
return false
case *ast.StructLit:
s.processDecls(x.Elts)
return false
case *ast.SelectorExpr:
ast.Walk(x.X, s.markReferences, nil)
return false
case *ast.Ident:
for c := s; c != nil; c = c.parent {
if _, ok := c.scope[x.Name]; ok {
c.scope[x.Name] = false
break
}
}
}
return true
}
func (s *labelSimplifier) markStrings(n ast.Node) bool {
switch x := n.(type) {
case *ast.BasicLit:
str, err := strconv.Unquote(x.Value)
if err != nil || !ast.IsValidIdent(str) || internal.IsDefOrHidden(str) {
return false
}
s.scope[str] = true
case *ast.Ident:
s.scope[x.Name] = true
case *ast.ListLit, *ast.Interpolation:
return false
}
return true
}
func (s *labelSimplifier) replace(c astutil.Cursor) bool {
switch x := c.Node().(type) {
case *ast.BasicLit:
str, err := strconv.Unquote(x.Value)
if err == nil && s.scope[str] && !internal.IsDefOrHidden(str) {
c.Replace(ast.NewIdent(str))
}
}
return true
}