Skip to content

Commit

Permalink
fix handling multiple dot imports in aliaser
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed May 9, 2021
1 parent a9f4aa6 commit 0720c3f
Show file tree
Hide file tree
Showing 7 changed files with 486 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/gimps/aliaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (a *Aliaser) RewriteFile(file *ast.File, filePath string, imports map[strin
values = map[string]string{}
for _, metadata := range imports {
effectiveName := metadata.Alias
if effectiveName == "." {
continue
}

if effectiveName == "" {
effectiveName, _ = a.deps.GetPackageName(filePath, buildTags, metadata.Package)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gimps/testdata/aliases-conflict-alias/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ aliasRules:
- name: b-to-a
expr: 'log'
alias: 'a'
expectedExecuteError: 'failed to rewrite import aliases: two or more packages (at least "a" and "b") would be aliased to "a"'
expectedExecuteError: 'failed to rewrite import aliases: two or more packages (at least "fmt" and "log") would be aliased to "a"'
4 changes: 4 additions & 0 deletions pkg/gimps/testdata/aliases-dot-imports/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aliasRules:
- name: test
expr: 'k8s.io/api/([a-z0-9-]+)/(v[a-z0-9-]+)'
alias: '$1$2'
10 changes: 10 additions & 0 deletions pkg/gimps/testdata/aliases-dot-imports/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module go.xrstf.de/gimps/test

go 1.16

require (
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
k8s.io/api v0.21.0
k8s.io/client-go v0.21.0
)
428 changes: 428 additions & 0 deletions pkg/gimps/testdata/aliases-dot-imports/go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pkg/gimps/testdata/aliases-dot-imports/main.go.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"log"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
)

func main() {
Describe("test", func() {
fmt.Println(corev1.ConfigMap{})
fmt.Println(networkingv1beta1.HTTPIngressPath{})
log.Println("foo")
Expect(true).To(BeTrue())
})
}
19 changes: 19 additions & 0 deletions pkg/gimps/testdata/aliases-dot-imports/main.go.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/api/core/v1"
"k8s.io/api/networking/v1beta1"
"log"
)

func main() {
Describe("test", func() {
fmt.Println(v1.ConfigMap{})
fmt.Println(v1beta1.HTTPIngressPath{})
log.Println("foo")
Expect(true).To(BeTrue())
})
}

0 comments on commit 0720c3f

Please sign in to comment.