Skip to content

Commit

Permalink
Properly handle dot imports
Browse files Browse the repository at this point in the history
Typically no dot imports are desired but ginkgo/gomega is a common
exception.

Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr authored and julz committed Aug 21, 2023
1 parent 478befd commit 441cd3f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
14 changes: 11 additions & 3 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,19 @@ func findEdits(node ast.Node, uses map[*ast.Ident]types.Object, importPath, orig
// skip identifiers pointing to a different import statement.
continue
}
pos := use.Pos()
end := use.End()
replacement := packageReplacement

if packageReplacement == "." {
replacement = ""
end = end + 1
}

result = append(result, analysis.TextEdit{
Pos: use.Pos(),
End: use.End(),
NewText: []byte(packageReplacement),
Pos: pos,
End: end,
NewText: []byte(replacement),
})
}

Expand Down
10 changes: 10 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func makeAnalyzer() *analysis.Analyzer {
RequiredAlias: make(map[string]string),
}
return &analysis.Analyzer{
Name: "importas",
Doc: "something",
Flags: flags(&cnf),
Run: func(pass *analysis.Pass) (interface{}, error) {
return runWithConfig(&cnf, pass)
Expand Down Expand Up @@ -103,6 +105,14 @@ func TestAnalyzer(t *testing.T) {
"knative.dev/serving/pkg/(?:apis/)?(\\w+)(?:/v[\\w\\d]+)?": "k$1",
},
},
{
desc: "dot imports should be handled correctly",
pkg: "h",
aliases: stringMap{
"github.com/onsi/gomega": ".",
},
disallowUnaliased: true,
},
}

for _, test := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions testdata/src/h/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/julz/importas/testdata/src/h

go 1.20

require github.com/onsi/gomega v1.24.1

require (
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions testdata/src/h/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/onsi/ginkgo/v2 v2.5.0 h1:TRtrvv2vdQqzkwrQ1ke6vtXf7IK34RBUJafIy1wMwls=
github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 11 additions & 0 deletions testdata/src/h/h.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package h

import (
"github.com/onsi/gomega" // want `import "github.com/onsi/gomega" imported without alias but must be with alias "." according to config`
)

func foo() {
gomega.Expect(nil).To(gomega.BeNil())
gomega.
Expect(true).To(gomega.BeTrue())
}
11 changes: 11 additions & 0 deletions testdata/src/h/h.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package h

import (
. "github.com/onsi/gomega" // want `import "github.com/onsi/gomega" imported without alias but must be with alias "." according to config`
)

func foo() {
Expect(nil).To(BeNil())

Expect(true).To(BeTrue())
}

0 comments on commit 441cd3f

Please sign in to comment.