cue/format: fix formatting of generated ImportsDecl
Change-Id: Ifae591e8b1b481f021ceb88d111f910ea83c4bfe
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5961
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cue/format/node.go b/cue/format/node.go
index 0bd8525..f626851 100644
--- a/cue/format/node.go
+++ b/cue/format/node.go
@@ -352,7 +352,7 @@
break
}
switch {
- case len(n.Specs) == 1:
+ case len(n.Specs) == 1 && len(n.Specs[0].Comments()) == 0:
if !n.Lparen.IsValid() {
f.print(blank)
f.walkSpecList(n.Specs)
diff --git a/cue/format/node_test.go b/cue/format/node_test.go
index a7ca3a4..0ee0911 100644
--- a/cue/format/node_test.go
+++ b/cue/format/node_test.go
@@ -20,6 +20,7 @@
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
+ "cuelang.org/go/internal"
)
// TestInvalidAST verifies behavior for various invalid AST inputs. In some
@@ -46,6 +47,17 @@
node: &ast.Field{Label: &ast.Ident{}, Value: ast.NewString("foo")},
// Force a new struct.
out: `"": "foo"`,
+ }, {
+ desc: "ImportDecl without parens, but imports with comments",
+ node: func() ast.Node {
+ n := ast.NewImport(nil, "time")
+ ast.AddComment(n, internal.NewComment(true, "hello"))
+ return &ast.ImportDecl{Specs: []*ast.ImportSpec{n}}
+ }(),
+ out: `import (
+ // hello
+ "time"
+)`,
}}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {