Skip to content

Commit

Permalink
make separateLocal work with nested local prefixes like "github.com/o…
Browse files Browse the repository at this point in the history
…rg,github.com/org/self" (#1)
  • Loading branch information
youxkei authored Oct 22, 2022
1 parent 8239498 commit 265baa5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 15 deletions.
39 changes: 32 additions & 7 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,47 @@ import (
"github.com/zchee/goimportz/internal/gopathwalk"
)

type prefixWithIndex struct {
prefix string
index int
}

// importToGroup is a list of functions which map from an import path to
// a group number.
var importToGroup = []func(localPrefix, importPath string, separateLocal bool) (num int, ok bool){
func(localPrefix, importPath string, separateLocal bool) (num int, ok bool) {
if localPrefix == "" {
return
}
for i, p := range strings.Split(localPrefix, ",") {
if strings.HasPrefix(importPath, p) || strings.TrimSuffix(p, "/") == importPath {
if separateLocal {
return i + 2, true // +2 for stdlib and 3rd-party package groups
}
return 3, true

prefixes := strings.Split(localPrefix, ",")

prefixesWithIndices := make([]prefixWithIndex, 0, len(prefixes))
for i, prefix := range prefixes {
pi := prefixWithIndex{
prefix: prefix,
}

if separateLocal {
pi.index = i + 2 // +2 for stdlib and 3rd-party package groups
} else {
pi.index = 3
}

prefixesWithIndices = append(prefixesWithIndices, pi)
}

sort.Slice(prefixesWithIndices, func(i, j int) bool {
// sort in reverse of lexicographically order
return prefixesWithIndices[i].prefix > prefixesWithIndices[j].prefix
})

for _, pi := range prefixesWithIndices {
if strings.HasPrefix(importPath, pi.prefix) || strings.TrimSuffix(pi.prefix, "/") == importPath {
return pi.index, true
}
}

return
},
func(_, importPath string, _ bool) (num int, ok bool) {
Expand Down Expand Up @@ -1086,7 +1112,6 @@ func addExternalCandidates(pass *pass, refs references, filename string) error {
defer wg.Done()

found, err := findImport(ctx, pass, found[pkgName], pkgName, symbols, filename)

if err != nil {
firstErrOnce.Do(func() {
firstErr = err
Expand Down
54 changes: 46 additions & 8 deletions internal/imports/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,6 @@ func TestSimpleCases(t *testing.T) {
}
t.assertProcessEquals("golang.org/fake", "x.go", nil, options, tt.out)
})

})
}
}
Expand Down Expand Up @@ -1302,7 +1301,6 @@ func bar() {
}.processTest(t, "golang.org/fake", "x.go", nil, options, tt.out)
})
}

}

// Test support for packages in GOPATH that are actually symlinks.
Expand Down Expand Up @@ -1783,7 +1781,7 @@ func TestLocalPrefix(t *testing.T) {
name string
modules []packagestest.Module
localPrefix string
separeteLocal bool
separateLocal bool
src string
want string
}{
Expand Down Expand Up @@ -1832,7 +1830,7 @@ const _ = runtime.GOOS
},
},
localPrefix: "example.org/pkg,foo.com/,code.org",
separeteLocal: true,
separateLocal: true,
src: "package main \n const W = pkg.A \n const X = bar.B \n const Y = expproj.C \n const Z = localprefix.D \n const _ = runtime.GOOS",
want: `package main
Expand Down Expand Up @@ -1914,6 +1912,49 @@ const X = pkg.A
const Y = bar.B
const Z = expproj.C
const _ = runtime.GOOS
`,
},
{
name: "nested_prefixes",
modules: []packagestest.Module{
{
Name: "github.com/org/self",
Files: fm{"pkg/pkg.go": "package pkg \n const A = 1"},
},
{
Name: "github.com/org/lib",
Files: fm{"lib.go": "package lib \n const B = 1"},
},
{
Name: "github.com/org/kit",
Files: fm{"kit.go": "package kit \n const C = 1"},
},
{
Name: "example.com/util",
Files: fm{"util.go": "package util \n const D = 1"},
},
},
localPrefix: "github.com/org,github.com/org/self",
separateLocal: true,
src: "package main \n const X = pkg.A \n const Y = lib.B \n const Z = kit.C \n const W = util.D \n const _ = runtime.GOOS",
want: `package main
import (
"runtime"
"example.com/util"
"github.com/org/kit"
"github.com/org/lib"
"github.com/org/self/pkg"
)
const X = pkg.A
const Y = lib.B
const Z = kit.C
const W = util.D
const _ = runtime.GOOS
`,
},
}
Expand All @@ -1929,7 +1970,7 @@ const _ = runtime.GOOS
}.test(t, func(t *goimportTest) {
options := &Options{
LocalPrefix: tt.localPrefix,
SepareteLocal: tt.separeteLocal,
SepareteLocal: tt.separateLocal,
TabWidth: 8,
TabIndent: true,
Comments: true,
Expand Down Expand Up @@ -2145,7 +2186,6 @@ var _, _ = fmt.Printf, bytes.Equal
// Tests that sibling files - other files in the same package - can provide an
// import that may not be the default one otherwise.
func TestSiblingImports(t *testing.T) {

// provide is the sibling file that provides the desired import.
const provide = `package siblingimporttest
Expand Down Expand Up @@ -2224,7 +2264,6 @@ var _ = fmt.Printf
},
},
}.processTest(t, "foo.com", "pkg/uses.go", nil, nil, want)

}

// Tests that an input file's own package is ignored.
Expand Down Expand Up @@ -2531,7 +2570,6 @@ var _ = &bytes.Buffer{}
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
}
})

}

// Ensures a token as large as 500000 bytes can be handled
Expand Down

0 comments on commit 265baa5

Please sign in to comment.