mirror of
https://github.com/golang/go
synced 2024-11-24 19:10:15 -07:00
go/ast: fix ast.MergePackageFiles to collect infos about imports
R=golang-dev, gri CC=golang-dev https://golang.org/cl/4710047
This commit is contained in:
parent
7bbe2c8998
commit
c7a742c510
@ -344,6 +344,8 @@ const (
|
|||||||
// If set, comments that are not associated with a specific
|
// If set, comments that are not associated with a specific
|
||||||
// AST node (as Doc or Comment) are excluded.
|
// AST node (as Doc or Comment) are excluded.
|
||||||
FilterUnassociatedComments
|
FilterUnassociatedComments
|
||||||
|
// If set, duplicate import declarations are excluded.
|
||||||
|
FilterImportDuplicates
|
||||||
)
|
)
|
||||||
|
|
||||||
// separator is an empty //-style comment that is interspersed between
|
// separator is an empty //-style comment that is interspersed between
|
||||||
@ -459,6 +461,32 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Collect import specs from all package files.
|
||||||
|
var imports []*ImportSpec
|
||||||
|
if mode&FilterImportDuplicates != 0 {
|
||||||
|
seen := make(map[string]bool)
|
||||||
|
for _, f := range pkg.Files {
|
||||||
|
for _, imp := range f.Imports {
|
||||||
|
path := imp.Path.Value
|
||||||
|
if !seen[path] {
|
||||||
|
//TODO: consider handling cases where:
|
||||||
|
// - 2 imports exist with the same import path but
|
||||||
|
// have different local names (one should probably
|
||||||
|
// keep both of them)
|
||||||
|
// - 2 imports exist but only one has a comment
|
||||||
|
// - 2 imports exist and they both have (possibly
|
||||||
|
// different) comments
|
||||||
|
seen[path] = true
|
||||||
|
imports = append(imports, imp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, f := range pkg.Files {
|
||||||
|
imports = append(imports, f.Imports...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Collect comments from all package files.
|
// Collect comments from all package files.
|
||||||
var comments []*CommentGroup
|
var comments []*CommentGroup
|
||||||
if mode&FilterUnassociatedComments == 0 {
|
if mode&FilterUnassociatedComments == 0 {
|
||||||
@ -469,7 +497,6 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(gri) need to compute pkgScope and unresolved identifiers!
|
// TODO(gri) need to compute unresolved identifiers!
|
||||||
// TODO(gri) need to compute imports!
|
return &File{doc, pos, NewIdent(pkg.Name), decls, pkg.Scope, imports, nil, comments}
|
||||||
return &File{doc, pos, NewIdent(pkg.Name), decls, nil, nil, nil, comments}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user