Skip to content

Commit dfc6ada

Browse files
committed
Revert "Merge pull request kubernetes#277 from jpbetz/format-only-imports"
This reverts commit fb7743f, reversing changes made to a7b603a. Signed-off-by: Brad Davidson <[email protected]>
1 parent 2b36238 commit dfc6ada

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

v2/generator/execute.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"strings"
2727

2828
"golang.org/x/tools/imports"
29-
3029
"k8s.io/gengo/v2/namer"
3130
"k8s.io/gengo/v2/types"
3231
"k8s.io/klog/v2"
@@ -115,13 +114,7 @@ func assembleGoFile(w io.Writer, f *File) {
115114
}
116115

117116
func importsWrapper(src []byte) ([]byte, error) {
118-
opt := imports.Options{
119-
Comments: true,
120-
TabIndent: true,
121-
TabWidth: 8,
122-
FormatOnly: true, // Disable the insertion and deletion of imports
123-
}
124-
return imports.Process("", src, &opt)
117+
return imports.Process("", src, nil)
125118
}
126119

127120
func NewGoFile() *DefaultFileType {

v2/generator/import_tracker.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package generator
1818

1919
import (
2020
"go/token"
21-
"path/filepath"
2221
"strings"
2322

2423
"k8s.io/klog/v2"
@@ -46,7 +45,7 @@ import (
4645
func NewImportTrackerForPackage(local string, typesToAdd ...*types.Type) *namer.DefaultImportTracker {
4746
tracker := namer.NewDefaultImportTracker(types.Name{Package: local})
4847
tracker.IsInvalidType = func(*types.Type) bool { return false }
49-
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, local, name) }
48+
tracker.LocalName = func(name types.Name) string { return goTrackerLocalName(&tracker, name) }
5049
tracker.PrintImport = func(path, name string) string { return name + " \"" + path + "\"" }
5150

5251
tracker.AddTypes(typesToAdd...)
@@ -57,15 +56,14 @@ func NewImportTracker(typesToAdd ...*types.Type) *namer.DefaultImportTracker {
5756
return NewImportTrackerForPackage("", typesToAdd...)
5857
}
5958

60-
func goTrackerLocalName(tracker namer.ImportTracker, localPkg string, t types.Name) string {
59+
func goTrackerLocalName(tracker namer.ImportTracker, t types.Name) string {
6160
path := t.Package
6261

6362
// Using backslashes in package names causes gengo to produce Go code which
6463
// will not compile with the gc compiler. See the comment on GoSeperator.
6564
if strings.ContainsRune(path, '\\') {
6665
klog.Warningf("Warning: backslash used in import path '%v', this is unsupported.\n", path)
6766
}
68-
localLeaf := filepath.Base(localPkg)
6967

7068
dirs := strings.Split(path, namer.GoSeperator)
7169
for n := len(dirs) - 1; n >= 0; n-- {
@@ -76,13 +74,8 @@ func goTrackerLocalName(tracker namer.ImportTracker, localPkg string, t types.Na
7674
// packages, but aren't legal go names. So we'll sanitize.
7775
name = strings.ReplaceAll(name, ".", "")
7876
name = strings.ReplaceAll(name, "-", "")
79-
if _, found := tracker.PathOf(name); found || name == localLeaf {
80-
// This name collides with some other package.
81-
// Or, this name is tne same name as the local package,
82-
// which we avoid because it can be confusing. For example,
83-
// if the local package is v1, we to avoid importing
84-
// another package using the v1 name, and instead import
85-
// it with a more qualified name, such as metav1.
77+
if _, found := tracker.PathOf(name); found {
78+
// This name collides with some other package
8679
continue
8780
}
8881

v2/generator/import_tracker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestNewImportTracker(t *testing.T) {
7272
{Name: types.Name{Package: "bar.com/external/pkg"}},
7373
},
7474
expectedImports: []string{
75-
`externalpkg "bar.com/external/pkg"`,
75+
`pkg "bar.com/external/pkg"`,
7676
},
7777
},
7878
}

0 commit comments

Comments
 (0)