Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af2d2d6

Browse files
xieyuschengopherbot
authored andcommitted
internal/analysisinternal: extra new line when add first std import
AddImport now insert a std import at the first place of import group and it's consecutive with the next import. When the next consecutive import is 3rd package import, after applying edits when running 'modernizer -fix ...', go/format.Source is used to format it, which sorts consecutive imports by string comparing. As a result, when slicescontains modernizer adds std 'slices' import, it is adjcent with 3rd package import and sorted at the last place. This CL changes analysisinternal.AddImport behavior by adding an extra newline character when inserting std package to a group without any std import to ensure std and 3rd imports won't be consecutive. Fixes: golang/go#75296 Change-Id: I22b143b87bf901502c53a42e0363ed624ae4542e Reviewed-on: https://go-review.googlesource.com/c/tools/+/702335 Reviewed-by: Michael Pratt <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 6375378 commit af2d2d6

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

internal/analysisinternal/addimport_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,31 @@ import "io"
291291
292292
import "vendor/golang.org/x/net/dns/dnsmessage"
293293
294+
func _(io.Reader) {
295+
fmt
296+
}`,
297+
},
298+
{
299+
descr: descr("add import to group without std import"),
300+
src: `package a
301+
302+
import (
303+
"golang.org/x/tools/go/packages"
304+
gossa "golang.org/x/tools/go/ssa"
305+
)
306+
307+
func _(io.Reader) {
308+
«fmt fmt»
309+
}`,
310+
want: `package a
311+
312+
import (
313+
"fmt"
314+
315+
"golang.org/x/tools/go/packages"
316+
gossa "golang.org/x/tools/go/ssa"
317+
)
318+
294319
func _(io.Reader) {
295320
fmt
296321
}`,

internal/analysisinternal/analysis.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,27 @@ func AddImport(info *types.Info, file *ast.File, preferredName, pkgpath, member
277277
before = decl0.Doc
278278
}
279279
}
280-
// If the first decl is an import group, add this new import at the end.
281280
if gd, ok := before.(*ast.GenDecl); ok && gd.Tok == token.IMPORT && gd.Rparen.IsValid() {
282-
pos = gd.Rparen
283-
// if it's a std lib, we should append it at the beginning of import group.
284-
// otherwise we may see the std package is put at the last behind a 3rd module which doesn't follow our convention.
285-
// besides, gofmt doesn't help in this case.
286-
if IsStdPackage(pkgpath) && len(gd.Specs) != 0 {
287-
pos = gd.Specs[0].Pos()
281+
// Have existing grouped import ( ... ) decl.
282+
if IsStdPackage(pkgpath) && len(gd.Specs) > 0 {
283+
// Add spec for a std package before
284+
// first existing spec, followed by
285+
// a blank line if the next one is non-std.
286+
first := gd.Specs[0].(*ast.ImportSpec)
287+
pos = first.Pos()
288+
if !IsStdPackage(first.Path.Value) {
289+
newText += "\n"
290+
}
288291
newText += "\n\t"
289292
} else {
293+
// Add spec at end of group.
294+
pos = gd.Rparen
290295
newText = "\t" + newText + "\n"
291296
}
292297
} else {
298+
// No import decl, or non-grouped import.
299+
// Add a new import decl before first decl.
300+
// (gofmt will merge multiple import decls.)
293301
pos = before.Pos()
294302
newText = "import " + newText + "\n\n"
295303
}

0 commit comments

Comments
 (0)