Skip to content

Commit

Permalink
internal: simplify GetPackageInfo into an *ast.Package getter
Browse files Browse the repository at this point in the history
The Index field was only used by the export package when obtaining
the attributes attached to the declarations to export.
We can use the length of ast.Package.Preamble instead.

The Name field reflected ast.Package.Name.Name when the package
was non-nil; so we can do that directly in the callers.
The resulting code is no more complex than before.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I9e5c291bc01687fd2f9472638748f8abef417588
  • Loading branch information
mvdan committed Sep 10, 2024
1 parent 002c62f commit 9ab3787
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/orphans.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
} else {
field := &ast.Field{Label: labels[0]}
f.Decls = append(f.Decls, field)
if p := internal.GetPackageInfo(file).Package; p != nil {
astutil.CopyComments(field, p)
if pkg := internal.Package(file); pkg != nil {
astutil.CopyComments(field, pkg)
}
for _, e := range labels[1:] {
newField := &ast.Field{Label: e}
Expand Down
7 changes: 3 additions & 4 deletions cue/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ func (r *Runtime) Marshal(values ...InstanceOrValue) (b []byte, err error) {
})

if inst.PkgName != "" {
pi := internal.GetPackageInfo(file)
if pi.Package == nil {
if pkg := internal.Package(file); pkg == nil {
pkg := &ast.Package{Name: ast.NewIdent(inst.PkgName)}
file.Decls = append([]ast.Decl{pkg}, file.Decls...)
} else if pi.Name != inst.PkgName {
} else if pkg.Name.Name != inst.PkgName {
// pi is guaranteed to be generated by Def, so it is "safe" to modify.
pi.Package.Name = ast.NewIdent(inst.PkgName)
pkg.Name = ast.NewIdent(inst.PkgName)
}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/core/export/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ func extractDeclAttrs(attrs []*ast.Attribute, n ast.Node) []*ast.Attribute {
switch x := n.(type) {
case nil:
case *ast.File:
info := internal.GetPackageInfo(x)
attrs = appendDeclAttrs(attrs, x.Decls[info.Index:])
attrs = appendDeclAttrs(attrs, x.Decls[len(x.Preamble()):])
case *ast.StructLit:
attrs = appendDeclAttrs(attrs, x.Elts)
}
Expand Down
30 changes: 12 additions & 18 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,30 @@ func ListEllipsis(n *ast.ListLit) (elts []ast.Expr, e *ast.Ellipsis) {
return elts, e
}

type PkgInfo struct {
Package *ast.Package
Index int // position in File.Decls
Name string
}

func GetPackageInfo(f *ast.File) PkgInfo {
for i, d := range f.Decls {
switch x := d.(type) {
func Package(f *ast.File) *ast.Package {
for _, d := range f.Decls {
switch d := d.(type) {
case *ast.CommentGroup:
case *ast.Attribute:
case *ast.Package:
if x.Name == nil {
return PkgInfo{}
if d.Name == nil {
return nil
}
return PkgInfo{x, i, x.Name.Name}
return d
default:
return PkgInfo{}
return nil
}
}
return PkgInfo{}
return nil
}

func SetPackage(f *ast.File, name string, overwrite bool) {
if pi := GetPackageInfo(f); pi.Package != nil {
if !overwrite || pi.Name == name {
if pkg := Package(f); pkg != nil {
if !overwrite || pkg.Name.Name == name {
return
}
ident := ast.NewIdent(name)
astutil.CopyMeta(ident, pi.Package.Name)
astutil.CopyMeta(ident, pkg.Name)
return
}

Expand Down Expand Up @@ -223,7 +217,7 @@ func NewComment(isDoc bool, s string) *ast.CommentGroup {

func FileComment(f *ast.File) *ast.CommentGroup {
var cgs []*ast.CommentGroup
if pkg := GetPackageInfo(f).Package; pkg != nil {
if pkg := Package(f); pkg != nil {
cgs = pkg.Comments()
} else if cgs = f.Comments(); len(cgs) > 0 {
// Use file comment.
Expand Down

0 comments on commit 9ab3787

Please sign in to comment.