Skip to content

Commit

Permalink
cue/ast/astutil: refactor to prepare for Sanitize func
Browse files Browse the repository at this point in the history
- Factor out code
- Resolve imports in Resolve func

The latter is now possible because the package name, and
thus identifier, is now uniquely determined from the import
path.

Change-Id: I648c02eb43d0b104bf4bc3e70a3405f0a605e34f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5962
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed May 12, 2020
1 parent 8938f35 commit 00a8bb9
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 131 deletions.
8 changes: 8 additions & 0 deletions cue/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@ func (v *astVisitor) walk(astNode ast.Node) (ret value) {
}

f := v.label(name, true)
if _, ok := n.Node.(*ast.ImportSpec); ok {
n2 := v.mapScope(n.Node)
ref := &nodeRef{baseValue: newExpr(n), node: n2, label: f}
ret = ref
break
}

// TODO: probably unused. Verify and remove.
if n.Scope == nil {
// Package or direct ancestor node.
n2 := v.mapScope(n.Node)
Expand Down
58 changes: 5 additions & 53 deletions cue/ast/astutil/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ import (
"encoding/hex"
"fmt"
"hash/fnv"
"path"
"reflect"
"strconv"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
)

// A Cursor describes a node encountered during Apply.
Expand Down Expand Up @@ -130,62 +126,18 @@ func (c *cursor) Import(importPath string) *ast.Ident {
return nil
}

name := path.Base(importPath)
if p := strings.LastIndexByte(name, ':'); p > 0 {
name = name[p+1:]
}
name := importPathName(importPath)

// TODO: come up with something much better.
// For instance, hoist the uniquer form cue/export.go to
// here and make export.go use this.
hash := fnv.New32()
name += hex.EncodeToString(hash.Sum([]byte(importPath)))[:6]

quoted := strconv.Quote(importPath)

var imports *ast.ImportDecl
var spec *ast.ImportSpec
decls := info.current.decls
i := 0
outer:
for ; i < len(decls); i++ {
d := decls[i]
switch t := d.(type) {
default:
break outer

case *ast.Package:
case *ast.CommentGroup:
case *ast.ImportDecl:
imports = t
for _, s := range t.Specs {
if s.Path.Value != quoted ||
s.Name == nil ||
s.Name.Name != name {
continue
}
spec = s
break
}
}
}

if spec == nil {
// Import not found, add one.
if imports == nil {
imports = &ast.ImportDecl{}
a := append(append(decls[:i], imports), decls[i:]...)
decls = a
info.current.decls = decls
}
path := ast.NewLit(token.STRING, quoted)
spec = &ast.ImportSpec{
Name: ast.NewIdent(name),
Path: path,
}
imports.Specs = append(imports.Specs, spec)
ast.SetRelPos(imports.Specs[0], token.NoRelPos)
}
spec := insertImport(&info.current.decls, &ast.ImportSpec{
Name: ast.NewIdent(name),
Path: ast.NewString(importPath),
})

ident := &ast.Ident{Node: spec} // Name is set later.
info.importPatch = append(info.importPatch, ident)
Expand Down

0 comments on commit 00a8bb9

Please sign in to comment.