Skip to content

Commit c9df291

Browse files
committed
internal: simplify GetPackageInfo into an *ast.Package getter
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 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200971 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 44b4ab5 commit c9df291

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

Diff for: cmd/cue/cmd/orphans.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
274274
} else {
275275
field := &ast.Field{Label: labels[0]}
276276
f.Decls = append(f.Decls, field)
277-
if p := internal.GetPackageInfo(file).Package; p != nil {
278-
astutil.CopyComments(field, p)
277+
if pkg := internal.Package(file); pkg != nil {
278+
astutil.CopyComments(field, pkg)
279279
}
280280
for _, e := range labels[1:] {
281281
newField := &ast.Field{Label: e}

Diff for: cue/marshal.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ func (r *Runtime) Marshal(values ...InstanceOrValue) (b []byte, err error) {
151151
})
152152

153153
if inst.PkgName != "" {
154-
pi := internal.GetPackageInfo(file)
155-
if pi.Package == nil {
154+
if pkg := internal.Package(file); pkg == nil {
156155
pkg := &ast.Package{Name: ast.NewIdent(inst.PkgName)}
157156
file.Decls = append([]ast.Decl{pkg}, file.Decls...)
158-
} else if pi.Name != inst.PkgName {
157+
} else if pkg.Name.Name != inst.PkgName {
159158
// pi is guaranteed to be generated by Def, so it is "safe" to modify.
160-
pi.Package.Name = ast.NewIdent(inst.PkgName)
159+
pkg.Name = ast.NewIdent(inst.PkgName)
161160
}
162161
}
163162

Diff for: internal/core/export/extract.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ func extractDeclAttrs(attrs []*ast.Attribute, n ast.Node) []*ast.Attribute {
179179
switch x := n.(type) {
180180
case nil:
181181
case *ast.File:
182-
info := internal.GetPackageInfo(x)
183-
attrs = appendDeclAttrs(attrs, x.Decls[info.Index:])
182+
attrs = appendDeclAttrs(attrs, x.Decls[len(x.Preamble()):])
184183
case *ast.StructLit:
185184
attrs = appendDeclAttrs(attrs, x.Elts)
186185
}

Diff for: internal/internal.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,31 @@ func ListEllipsis(n *ast.ListLit) (elts []ast.Expr, e *ast.Ellipsis) {
132132
return elts, e
133133
}
134134

135-
type PkgInfo struct {
136-
Package *ast.Package
137-
Index int // position in File.Decls
138-
Name string
139-
}
140-
141-
func GetPackageInfo(f *ast.File) PkgInfo {
142-
for i, d := range f.Decls {
143-
switch x := d.(type) {
135+
// Package finds the package declaration from the preamble of a file.
136+
func Package(f *ast.File) *ast.Package {
137+
for _, d := range f.Decls {
138+
switch d := d.(type) {
144139
case *ast.CommentGroup:
145140
case *ast.Attribute:
146141
case *ast.Package:
147-
if x.Name == nil {
148-
return PkgInfo{}
142+
if d.Name == nil { // malformed package declaration
143+
return nil
149144
}
150-
return PkgInfo{x, i, x.Name.Name}
145+
return d
151146
default:
152-
return PkgInfo{}
147+
return nil
153148
}
154149
}
155-
return PkgInfo{}
150+
return nil
156151
}
157152

158153
func SetPackage(f *ast.File, name string, overwrite bool) {
159-
if pi := GetPackageInfo(f); pi.Package != nil {
160-
if !overwrite || pi.Name == name {
154+
if pkg := Package(f); pkg != nil {
155+
if !overwrite || pkg.Name.Name == name {
161156
return
162157
}
163158
ident := ast.NewIdent(name)
164-
astutil.CopyMeta(ident, pi.Package.Name)
159+
astutil.CopyMeta(ident, pkg.Name)
165160
return
166161
}
167162

@@ -223,7 +218,7 @@ func NewComment(isDoc bool, s string) *ast.CommentGroup {
223218

224219
func FileComment(f *ast.File) *ast.CommentGroup {
225220
var cgs []*ast.CommentGroup
226-
if pkg := GetPackageInfo(f).Package; pkg != nil {
221+
if pkg := Package(f); pkg != nil {
227222
cgs = pkg.Comments()
228223
} else if cgs = f.Comments(); len(cgs) > 0 {
229224
// Use file comment.

0 commit comments

Comments
 (0)