go/ast/astutil: add merging pass to AddImport and AddNamedImport.
After inserting an import, merge all import declarations. Fixes golang/go#14075. Change-Id: I17fceb60f490deced2ee8eadf78091720580ffa1 Reviewed-on: https://go-review.googlesource.com/19828 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
361bcb2be3
commit
c6be41b91f
|
@ -143,6 +143,30 @@ func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added
|
|||
}
|
||||
|
||||
f.Imports = append(f.Imports, newImport)
|
||||
|
||||
if len(f.Decls) <= 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
// Merge all the import declarations into the first one.
|
||||
var first *ast.GenDecl
|
||||
for i, decl := range f.Decls {
|
||||
gen, ok := decl.(*ast.GenDecl)
|
||||
if !ok || gen.Tok != token.IMPORT || declImports(gen, "C") {
|
||||
continue
|
||||
}
|
||||
if first == nil {
|
||||
first = gen
|
||||
continue // Don't touch the first one.
|
||||
}
|
||||
// Move the imports of the other import declaration to the first one.
|
||||
for _, spec := range gen.Specs {
|
||||
spec.(*ast.ImportSpec).Path.ValuePos = first.Pos()
|
||||
first.Specs = append(first.Specs, spec)
|
||||
}
|
||||
f.Decls = append(f.Decls[:i], f.Decls[i+1:]...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -396,6 +396,119 @@ import (
|
|||
"fmt" // b
|
||||
"log"
|
||||
)
|
||||
`,
|
||||
},
|
||||
// Issue 14075: Merge import declarations
|
||||
{
|
||||
name: "issue 14075",
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
import "bytes"
|
||||
import "fmt"
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "issue 14075 update position",
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
import "bytes"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: `issue 14075 ignore import "C"`,
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
|
||||
import "bytes"
|
||||
import "fmt"
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: `issue 14075 ignore adjacent import "C"`,
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
import "fmt"
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: `issue 14075 ignore adjacent import "C" (without factored import)`,
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
import "fmt"
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
)
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: `issue 14075 ignore single import "C"`,
|
||||
pkg: "bufio",
|
||||
in: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
`,
|
||||
out: `package main
|
||||
|
||||
// Comment
|
||||
import "C"
|
||||
import "bufio"
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue