imports, go/ast/astutil: do not make grouped imports non-grouped when removing
import (
"fmt"
"strings"
)
When deleting "fmt" import statement, code should be converted to the
following code.
import (
"strings"
)
Instead of
import "strings"
Diff becomes nicer by this change and it avoids that rewriting grouped
imports non-grouped may result in confusion comments.
Example:
// comment 1
import (
"fmt"
// comment 2
"strings"
)
should be
// comment 1
import (
// comment 2
"strings"
)
instead of
// comment 1
// comment 2
import "strings"
Fixes golang/go#18051
Change-Id: I3c07b70b657191eacf83c3197a965e587286c950
Reviewed-on: https://go-review.googlesource.com/36853
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
13507735c4
commit
8524ce5143
|
|
@ -234,16 +234,17 @@ func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (del
|
|||
}
|
||||
}
|
||||
|
||||
gen.Lparen = token.NoPos // drop parens
|
||||
spec := gen.Specs[0].(*ast.ImportSpec)
|
||||
if spec.Doc != nil {
|
||||
// Move the documentation above the import statement.
|
||||
gen.TokPos = spec.Doc.End() + 1
|
||||
}
|
||||
|
||||
// Move the documentation right after the import decl.
|
||||
if spec.Doc != nil {
|
||||
for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Doc.Pos()).Line {
|
||||
fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line)
|
||||
}
|
||||
}
|
||||
for _, cg := range f.Comments {
|
||||
if cg.End() < spec.Pos() && fset.Position(cg.End()).Line == fset.Position(spec.Pos()).Line {
|
||||
for fset.Position(gen.TokPos).Line != fset.Position(spec.Pos()).Line {
|
||||
for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Pos()).Line {
|
||||
fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line)
|
||||
}
|
||||
break
|
||||
|
|
|
|||
|
|
@ -944,7 +944,9 @@ import (
|
|||
`,
|
||||
out: `package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -961,7 +963,7 @@ import y "fmt"
|
|||
import y "fmt"
|
||||
`,
|
||||
},
|
||||
// Issue #15432
|
||||
// Issue #15432, #18051
|
||||
{
|
||||
name: "import.19",
|
||||
pkg: "fmt",
|
||||
|
|
@ -975,8 +977,10 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
// Some comment.
|
||||
import "io"
|
||||
import (
|
||||
// Some comment.
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -993,9 +997,11 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
// Some
|
||||
// comment.
|
||||
import "io"
|
||||
import (
|
||||
// Some
|
||||
// comment.
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1014,11 +1020,13 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
/*
|
||||
Some
|
||||
comment.
|
||||
*/
|
||||
import "io"
|
||||
import (
|
||||
/*
|
||||
Some
|
||||
comment.
|
||||
*/
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1034,9 +1042,11 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
/* Some */
|
||||
// comment.
|
||||
import "io"
|
||||
import (
|
||||
/* Some */
|
||||
// comment.
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1052,8 +1062,10 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
// comment 2
|
||||
import "io"
|
||||
import (
|
||||
// comment 2
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1067,7 +1079,9 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
import "io" // comment 2
|
||||
import (
|
||||
"io" // comment 2
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1081,7 +1095,9 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
import /* comment */ "io"
|
||||
import (
|
||||
/* comment */ "io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1095,7 +1111,9 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
import "io" /* comment */
|
||||
import (
|
||||
"io" /* comment */
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1109,7 +1127,9 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
import "io"
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1123,7 +1143,9 @@ import (
|
|||
)`,
|
||||
out: `package main
|
||||
|
||||
import "io"
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1139,7 +1161,9 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import "io" // comment 2
|
||||
import (
|
||||
"io" // comment 2
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1155,7 +1179,9 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import "io"
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1171,7 +1197,9 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import /* comment 2 */ "io"
|
||||
import (
|
||||
/* comment 2 */ "io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1188,7 +1216,9 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import /* comment 2 */ i "io"
|
||||
import (
|
||||
/* comment 2 */ i "io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1205,7 +1235,9 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import i "io"
|
||||
import (
|
||||
i "io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
|
@ -1222,7 +1254,49 @@ import (
|
|||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import i "io"
|
||||
import (
|
||||
i "io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "import.35",
|
||||
pkg: "fmt",
|
||||
in: `package main
|
||||
|
||||
// comment 1
|
||||
import (
|
||||
"fmt"
|
||||
// comment 2
|
||||
"io"
|
||||
)`,
|
||||
out: `package main
|
||||
|
||||
// comment 1
|
||||
import (
|
||||
// comment 2
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "import.36",
|
||||
pkg: "fmt",
|
||||
in: `package main
|
||||
|
||||
/* comment 1 */
|
||||
import (
|
||||
"fmt"
|
||||
/* comment 2 */
|
||||
"io"
|
||||
)`,
|
||||
out: `package main
|
||||
|
||||
/* comment 1 */
|
||||
import (
|
||||
/* comment 2 */
|
||||
"io"
|
||||
)
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,7 +188,9 @@ _, _ := bytes.Buffer, bytes.NewReader
|
|||
`,
|
||||
out: `package foo
|
||||
|
||||
import "bytes"
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func bar() {
|
||||
_, _ := bytes.Buffer, bytes.NewReader
|
||||
|
|
@ -747,7 +749,9 @@ func main() { fmt.Println() }
|
|||
`,
|
||||
out: `package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() { fmt.Println() }
|
||||
`,
|
||||
|
|
@ -775,6 +779,27 @@ import (
|
|||
)
|
||||
|
||||
func main() {}
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "do not make grouped imports non-grouped",
|
||||
in: `package p
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var _ = fmt.Sprintf
|
||||
`,
|
||||
out: `package p
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var _ = fmt.Sprintf
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue