From 441cd3fd868da475456ab59cd7239396623d6137 Mon Sep 17 00:00:00 2001 From: Roman Mohr Date: Thu, 3 Aug 2023 13:50:11 +0200 Subject: [PATCH] Properly handle dot imports Typically no dot imports are desired but ginkgo/gomega is a common exception. Signed-off-by: Roman Mohr --- analyzer.go | 14 +++++++++++--- analyzer_test.go | 10 ++++++++++ testdata/src/h/go.mod | 12 ++++++++++++ testdata/src/h/go.sum | 15 +++++++++++++++ testdata/src/h/h.go | 11 +++++++++++ testdata/src/h/h.go.golden | 11 +++++++++++ 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 testdata/src/h/go.mod create mode 100644 testdata/src/h/go.sum create mode 100644 testdata/src/h/h.go create mode 100644 testdata/src/h/h.go.golden diff --git a/analyzer.go b/analyzer.go index f196534..a1272b9 100644 --- a/analyzer.go +++ b/analyzer.go @@ -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), }) } diff --git a/analyzer_test.go b/analyzer_test.go index 0329ad9..6a339aa 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -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) @@ -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 { diff --git a/testdata/src/h/go.mod b/testdata/src/h/go.mod new file mode 100644 index 0000000..1da964e --- /dev/null +++ b/testdata/src/h/go.mod @@ -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 +) diff --git a/testdata/src/h/go.sum b/testdata/src/h/go.sum new file mode 100644 index 0000000..a3d1dba --- /dev/null +++ b/testdata/src/h/go.sum @@ -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= diff --git a/testdata/src/h/h.go b/testdata/src/h/h.go new file mode 100644 index 0000000..739af35 --- /dev/null +++ b/testdata/src/h/h.go @@ -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()) +} diff --git a/testdata/src/h/h.go.golden b/testdata/src/h/h.go.golden new file mode 100644 index 0000000..c44a00d --- /dev/null +++ b/testdata/src/h/h.go.golden @@ -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()) +}