Skip to content

Commit 3ef265e

Browse files
katexochenvikstrous
authored andcommitted
Don't rename import alias equal to moved pkg
1 parent cf54644 commit 3ef265e

11 files changed

Lines changed: 68 additions & 3 deletions

File tree

internal/mvpkg/mvpkg.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (p *pkgMover) fixImportsInFile(fset *token.FileSet, src, dst, filename stri
198198

199199
ast.SortImports(fset, astFile)
200200

201-
newFile := astutil.Apply(astFile, func(c *astutil.Cursor) bool {
201+
applyFunc := func(c *astutil.Cursor) bool {
202202
selExpr, ok := c.Node().(*ast.SelectorExpr)
203203
if !ok {
204204
return true
@@ -221,7 +221,15 @@ func (p *pkgMover) fixImportsInFile(fset *token.FileSet, src, dst, filename stri
221221
}
222222

223223
return true
224-
}, nil)
224+
}
225+
226+
if isImportAliased(astFile, dstPkgPath) {
227+
// if the import of the package we are moving has an import alias,
228+
// we don't need to rename identifiers in the file.
229+
applyFunc = nil
230+
}
231+
232+
newFile := astutil.Apply(astFile, applyFunc, nil)
225233

226234
var buf bytes.Buffer
227235

@@ -243,6 +251,21 @@ func (p *pkgMover) fixImportsInFile(fset *token.FileSet, src, dst, filename stri
243251
return nil
244252
}
245253

254+
// isImportAliased returns true if the import with the given path has an import alias in the given file.
255+
func isImportAliased(file *ast.File, path string) bool {
256+
for _, imp := range file.Imports {
257+
if imp.Path == nil {
258+
continue
259+
}
260+
261+
if imp.Path.Value == fmt.Sprintf("%q", path) {
262+
return imp.Name != nil
263+
}
264+
}
265+
266+
return false
267+
}
268+
246269
func (p *pkgMover) getPkgPath(path string) string {
247270
newPath, ok := p.alreadyMovedPkgs[path]
248271
if ok {
@@ -252,7 +275,7 @@ func (p *pkgMover) getPkgPath(path string) string {
252275
return path
253276
}
254277

255-
// getFilePath is meant to be used on files (not directories) with file paths relvative to the root of the module.
278+
// getFilePath is meant to be used on files (not directories) with file paths relative to the root of the module.
256279
func (p *pkgMover) getFilePath(filePath string) string {
257280
newPath, ok := p.alreadyMovedFiles[filePath]
258281
if ok {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Package alias
2+
3+
This tests moves and renames a package that is imported under a package alias by another package.
4+
The test ensures we don't rename identifiers that equal the package we're moving, but are alias.
5+
6+
We move ./source/target to ./destination/targetnew.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package targetnew
2+
3+
func Foo() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com
2+
3+
go 1.13
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package depender
2+
3+
import (
4+
target "example.com/destination/targetnew"
5+
)
6+
7+
func Bar() {
8+
target.Foo()
9+
}

internal/mvpkg/tests/03_pkg_alias/expected/source/target/.gitkeep

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com
2+
3+
go 1.13
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package depender
2+
3+
import (
4+
target "example.com/source/target"
5+
)
6+
7+
func Bar() {
8+
target.Foo()
9+
}

internal/mvpkg/tests/03_pkg_alias/original/source/target/.gitkeep

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package target
2+
3+
func Foo() {}

0 commit comments

Comments
 (0)