Skip to content

Commit 79f12b3

Browse files
committed
Don't identity-alias stdlib imports
Fixes stuff like `context "context"` and `http "net/http"`. Signed-off-by: Brad Davidson <[email protected]>
1 parent 2b36238 commit 79f12b3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

v2/generator/import_tracker.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ func NewImportTrackerForPackage(local string, typesToAdd ...*types.Type) *namer.
4747
tracker := namer.NewDefaultImportTracker(types.Name{Package: local})
4848
tracker.IsInvalidType = func(*types.Type) bool { return false }
4949
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, local, name) }
50-
tracker.PrintImport = func(path, name string) string { return name + " \"" + path + "\"" }
50+
tracker.PrintImport = func(path, name string) string {
51+
// Don't alias stdlib imports. Anything with less than 3 path components
52+
// or no dot in the first path component is assumed to be stdlib.
53+
dirs := strings.Split(path, namer.GoSeperator)
54+
if len(dirs) < 3 || !strings.ContainsRune(dirs[0], '.') {
55+
return "\"" + path + "\""
56+
}
57+
return name + " \"" + path + "\""
58+
}
5159

5260
tracker.AddTypes(typesToAdd...)
5361
return &tracker

0 commit comments

Comments
 (0)