cue/format: fix newline bug with package decl
When formatting using a combination of programmatic
AST and parsed CUE code, the first declaration
could be printed on the same line as the package
declaration.
Change-Id: Ibf952d674c9a6b192d3300e2b7568a984d41c9d3
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2044
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/format/format_test.go b/cue/format/format_test.go
index 5b4f729..801c975 100644
--- a/cue/format/format_test.go
+++ b/cue/format/format_test.go
@@ -237,6 +237,28 @@
t.Errorf("got %q, expected %q", buf.String(), res)
}
}
+func TestPackage(t *testing.T) {
+ f := &ast.File{
+ Name: ast.NewIdent("foo"),
+ Decls: []ast.Decl{
+ &ast.EmitDecl{
+ Expr: &ast.BasicLit{
+ ValuePos: token.Pos(token.NoSpace),
+ Value: "1",
+ },
+ },
+ },
+ }
+ var buf bytes.Buffer
+ err := Node(&buf, f)
+ if err != nil {
+ t.Fatal(err)
+ }
+ const want = "package foo\n\n1\n"
+ if got := buf.String(); got != want {
+ t.Errorf("got %q, expected %q", got, want)
+ }
+}
// idents is an iterator that returns all idents in f via the result channel.
func idents(f *ast.File) <-chan *ast.Ident {
diff --git a/cue/format/node.go b/cue/format/node.go
index 527c92e..d054270 100644
--- a/cue/format/node.go
+++ b/cue/format/node.go
@@ -95,7 +95,7 @@
f.before(file)
if file.Name != nil {
f.print(file.Package, "package")
- f.print(blank, file.Name, newline, newsection)
+ f.print(blank, file.Name, newsection, nooverride)
}
f.current.pos = 3
f.visitComments(3)
@@ -222,11 +222,11 @@
f.walkSpecList(n.Specs)
f.print(unindent, newline, n.Rparen, token.RPAREN, newline)
}
- f.print(newsection)
+ f.print(newsection, nooverride)
case *ast.EmitDecl:
f.expr(n.Expr)
- f.print(newline, newsection) // force newline
+ f.print(newline, newsection, nooverride) // force newline
case *ast.Alias:
f.expr(n.Ident)