this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

all: use cue/ast.File.Preamble in two more places

To deduplicate the type switch logic, which can be easy to mess up.
The function simply takes a slice of File.Decls, so it doesn't add cost.
Also clarify the godoc a bit while we are here.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: If9c00d80ee96f0745bfc4740d99dfb72913162b7
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1233928
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>

+5 -22
+2 -1
cue/ast/ast.go
··· 1104 1104 comments 1105 1105 } 1106 1106 1107 - // Preamble returns the declarations of the preamble. 1107 + // Preamble returns the declarations of the preamble at the top of the file, 1108 + // including any package clause or import declaration found in it. 1108 1109 func (f *File) Preamble() []Decl { 1109 1110 p := 0 1110 1111 outer:
+2 -9
cue/types.go
··· 949 949 } 950 950 } 951 951 952 - outer: 953 - for _, d := range f.Decls { 954 - switch d.(type) { 955 - case *ast.Package, *ast.ImportDecl: 956 - return f 957 - case *ast.CommentGroup, *ast.Attribute: 958 - default: 959 - break outer 960 - } 952 + if len(f.Preamble()) > 0 { 953 + return f 961 954 } 962 955 963 956 if len(f.Decls) == 1 {
+1 -12
internal/internal.go
··· 213 213 return x 214 214 215 215 case *ast.File: 216 - start := 0 217 - outer: 218 - for i, d := range x.Decls { 219 - switch d.(type) { 220 - case *ast.Package, *ast.ImportDecl: 221 - start = i + 1 222 - case *ast.CommentGroup, *ast.Attribute: 223 - default: 224 - break outer 225 - } 226 - } 227 - decls := x.Decls[start:] 216 + decls := x.Decls[len(x.Preamble()):] 228 217 if len(decls) == 1 { 229 218 if e, ok := decls[0].(*ast.EmbedDecl); ok { 230 219 return e.Expr