Skip to content

Commit

Permalink
cue/format: fix formatting of generated ImportsDecl
Browse files Browse the repository at this point in the history
Change-Id: Ifae591e8b1b481f021ceb88d111f910ea83c4bfe
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5961
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
  • Loading branch information
mpvl committed May 12, 2020
1 parent afe86c1 commit 8938f35
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (f *formatter) decl(decl ast.Decl) {
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)
Expand Down
12 changes: 12 additions & 0 deletions cue/format/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
)

// TestInvalidAST verifies behavior for various invalid AST inputs. In some
Expand All @@ -46,6 +47,17 @@ func TestInvalidAST(t *testing.T) {
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) {
Expand Down

0 comments on commit 8938f35

Please sign in to comment.