diff --git a/.haxerc b/.haxerc index bf607ad..5ee0dc9 100644 --- a/.haxerc +++ b/.haxerc @@ -1,4 +1,4 @@ { - "version": "deb58a3", + "version": "bd1a05d", "resolveLibs": "scoped" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index c86c558..2c89df5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## dev branch / next version (2.x.x) +## 2.1.3 (2022-05-01) + +- fixed package renaming + ## 2.1.2 (2022-04-25) - fixed handling of loop iterator shadowing, fixes [vshaxe/vshaxe#136](https://github.com/vshaxe/vshaxe/issues/136) diff --git a/haxelib.json b/haxelib.json index e09e0b8..aa6715e 100644 --- a/haxelib.json +++ b/haxelib.json @@ -8,8 +8,8 @@ "refactor" ], "description": "A code renaming tool for Haxe", - "version": "2.1.2", - "releasenote": "fixed handling of loop iterator shadowing", + "version": "2.1.3", + "releasenote": "fixed package renaming", "contributors": [ "AlexHaxe" ], diff --git a/package-lock.json b/package-lock.json index 03e4db2..59b8b3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.2", + "version": "2.1.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.2", + "version": "2.1.3", "license": "MIT", "bin": { "haxe-rename": "bin/rename.js" diff --git a/package.json b/package.json index 06bcc5b..76ef898 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.2", + "version": "2.1.3", "description": "Renaming tool for Haxe", "repository": { "type": "git", diff --git a/src/refactor/discover/File.hx b/src/refactor/discover/File.hx index 36c09bf..b7b6d56 100644 --- a/src/refactor/discover/File.hx +++ b/src/refactor/discover/File.hx @@ -40,12 +40,9 @@ class File { if (packName.length <= 0) { return Global; } - if (packName == getPackage()) { - return SamePackage; - } var fullModule:String = '$packName.$moduleName'; var fullSubModule:Null = null; - if (moduleName == typeName) { + if (moduleName != typeName) { fullSubModule = '$fullModule.$typeName'; } for (importEntry in importList) { @@ -64,9 +61,21 @@ class File { } if (importHxFile == null) { + if (packName == getPackage()) { + return SamePackage; + } + if (packName == getPackage()) { + return SamePackage; + } return None; } - return importHxFile.importsModule(packName, moduleName, typeName); + var result:ImportStatus = importHxFile.importsModule(packName, moduleName, typeName); + if (result == None) { + if (packName == getPackage()) { + return SamePackage; + } + } + return result; } public function getMainModulName():String { diff --git a/src/refactor/rename/RenamePackage.hx b/src/refactor/rename/RenamePackage.hx index df3ffdd..b8a09ea 100644 --- a/src/refactor/rename/RenamePackage.hx +++ b/src/refactor/rename/RenamePackage.hx @@ -22,59 +22,58 @@ class RenamePackage { changelist.addChange(file.name, InsertText('package ${context.what.toName};\n', {fileName: file.name, start: 0, end: 0}), identifier); } + var newMainModulName:String = context.what.toName + "." + mainTypeName; + var mainModule:String = packageNamePrefix + mainTypeName; + var allUses:Array = context.nameMap.getIdentifiers(mainModule); + for (use in allUses) { + changelist.addChange(use.pos.fileName, ReplaceText(newMainModulName, use.pos), use); + } for (type in file.typeList) { - var typeName:String = if (mainTypeName == type.name.name) { - type.name.name; - } else { - mainTypeName + "." + type.name; + if (mainTypeName == type.name.name) { + continue; } + var typeName:String = type.name.name; + var fullModulName:String = packageNamePrefix + typeName; var newFullModulName:String = context.what.toName + "." + typeName; - var allUses:Array = context.nameMap.getIdentifiers(fullModulName); - if (allUses != null) { - for (use in allUses) { - changelist.addChange(use.pos.fileName, ReplaceText(newFullModulName, use.pos), use); - } + allUses = context.nameMap.getIdentifiers(fullModulName); + for (use in allUses) { + changelist.addChange(use.pos.fileName, ReplaceText(newFullModulName, use.pos), use); } - allUses = context.nameMap.getIdentifiers(type.name.name); - allUses = allUses.concat(context.nameMap.getStartsWith(type.name.name + ".")); - var uniqueFiles:Array = []; - if (allUses != null) { - for (use in allUses) { - if (use.pos.fileName == identifier.pos.fileName) { - // ignore self - continue; - } - if (uniqueFiles.contains(use.pos.fileName)) { - // only add once per file - continue; - } - var useFile:Null = context.fileList.getFile(use.pos.fileName); - if (useFile == null) { - continue; - } - switch (useFile.importsModule(type.file.getPackage(), type.file.getMainModulName(), type.name.name)) { - case None | SamePackage: - case Global | Imported | ImportedWithAlias(_): - // imported old location -> skip (we renamed import in previous loop) - continue; - } - switch (useFile.importsModule(context.what.toName, type.file.getMainModulName(), type.name.name)) { - case None: - case Global | SamePackage | Imported | ImportedWithAlias(_): - // already imports new location -> skip - continue; - } - var importPos:IdentifierPos = {fileName: use.pos.fileName, start: useFile.importInsertPos, end: useFile.importInsertPos} - changelist.addChange(use.pos.fileName, InsertText('import $newFullModulName;\n', importPos), use); + fullModulName = packageNamePrefix + mainTypeName + "." + typeName; + newFullModulName = context.what.toName + "." + mainTypeName + "." + typeName; + allUses = context.nameMap.getIdentifiers(fullModulName); + for (use in allUses) { + changelist.addChange(use.pos.fileName, ReplaceText(newFullModulName, use.pos), use); + } + } + var uniqueFiles:Array = []; + + allUses = context.nameMap.getIdentifiers(mainTypeName); + for (use in allUses) { + if (use.file.name == file.name) { + continue; + } + if (use.file.getPackage() != packageName) { + continue; + } + if (uniqueFiles.contains(use.pos.fileName)) { + // only add once per file + continue; + } + switch (use.file.importsModule(packageName, mainTypeName, mainTypeName)) { + case None: + case Global | SamePackage: + var importPos:IdentifierPos = {fileName: use.pos.fileName, start: use.file.importInsertPos, end: use.file.importInsertPos} + changelist.addChange(use.pos.fileName, InsertText('import $newMainModulName;\n', importPos), use); uniqueFiles.push(use.pos.fileName); - } + case Imported: + case ImportedWithAlias(_): } } // TODO remove redundant imports - moveFileToPackage(context, file, changelist, packageName); return Promise.resolve(changelist.execute()); } diff --git a/test/TestMain.hx b/test/TestMain.hx index 80b00dc..85b59be 100644 --- a/test/TestMain.hx +++ b/test/TestMain.hx @@ -3,6 +3,7 @@ import refactor.EnumTest; import refactor.ImportAliasTest; import refactor.InterfaceTest; import refactor.ModuleLevelStaticTest; +import refactor.PackageTest; import refactor.ScopedLocalTest; import refactor.TypedefTest; import utest.Runner; @@ -16,6 +17,7 @@ class TestMain { new ImportAliasTest(), new InterfaceTest(), new ModuleLevelStaticTest(), + new PackageTest(), new ScopedLocalTest(), new TypedefTest() ]; diff --git a/test/import.hx b/test/import.hx index e3c54a8..70a3a5a 100644 --- a/test/import.hx +++ b/test/import.hx @@ -1,3 +1,4 @@ +import refactor.TestEditableDocument; import utest.Assert; import utest.Async; import utest.ITest; diff --git a/test/refactor/ClassTest.hx b/test/refactor/ClassTest.hx index ffe6c96..1dfae6d 100644 --- a/test/refactor/ClassTest.hx +++ b/test/refactor/ClassTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class ClassTest extends TestBase { function setupClass() { setupTestSources(["testcases/classes"]); diff --git a/test/refactor/EnumTest.hx b/test/refactor/EnumTest.hx index 009df74..06c04ec 100644 --- a/test/refactor/EnumTest.hx +++ b/test/refactor/EnumTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class EnumTest extends TestBase { function setupClass() { setupTestSources(["testcases/enums"]); diff --git a/test/refactor/ImportAliasTest.hx b/test/refactor/ImportAliasTest.hx index c07121f..ec76745 100644 --- a/test/refactor/ImportAliasTest.hx +++ b/test/refactor/ImportAliasTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class ImportAliasTest extends TestBase { function setupClass() { setupTestSources(["testcases/importalias"]); diff --git a/test/refactor/InterfaceTest.hx b/test/refactor/InterfaceTest.hx index c82dc4c..3f178e5 100644 --- a/test/refactor/InterfaceTest.hx +++ b/test/refactor/InterfaceTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class InterfaceTest extends TestBase { function setupClass() { setupTestSources(["testcases/interfaces"]); diff --git a/test/refactor/ModuleLevelStaticTest.hx b/test/refactor/ModuleLevelStaticTest.hx index fab31da..efc1ef3 100644 --- a/test/refactor/ModuleLevelStaticTest.hx +++ b/test/refactor/ModuleLevelStaticTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class ModuleLevelStaticTest extends TestBase { function setupClass() { setupTestSources(["testcases/modulelevelstatics"]); diff --git a/test/refactor/PackageTest.hx b/test/refactor/PackageTest.hx new file mode 100644 index 0000000..31ead9c --- /dev/null +++ b/test/refactor/PackageTest.hx @@ -0,0 +1,48 @@ +package refactor; + +class PackageTest extends TestBase { + function setupClass() { + setupTestSources(["testcases/packages"]); + } + + public function testRenameTypesModul(async:Async) { + var edits:Array = [ + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types.Type3", 53, 73), + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types", 82, 96), + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Type1", 289, 303), + makeMoveTestEdit("testcases/packages/Types.hx", "testcases/packages/sub/Types.hx"), + makeReplaceTestEdit("testcases/packages/Types.hx", "packages.sub", 8, 16), + + ]; + refactorAndCheck({fileName: "testcases/packages/Types.hx", toName: "packages.sub", pos: 12}, edits, async); + } + + public function testRenameMoreTypesModul(async:Async) { + var edits:Array = [ + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.MoreTypes", 26, 44), + makeMoveTestEdit("testcases/packages/MoreTypes.hx", "testcases/packages/sub/MoreTypes.hx"), + makeReplaceTestEdit("testcases/packages/MoreTypes.hx", "packages.sub", 8, 16), + ]; + refactorAndCheck({fileName: "testcases/packages/MoreTypes.hx", toName: "packages.sub", pos: 12}, edits, async); + } + + public function testRenameOtherTypesModul(async:Async) { + var edits:Array = [ + makeInsertTestEdit("testcases/packages/Main.hx", "import packages.sub.OtherTypes;\n", 19), + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes", 202, 221), + makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes.OtherTypeA", 240, 270), + makeMoveTestEdit("testcases/packages/OtherTypes.hx", "testcases/packages/sub/OtherTypes.hx"), + makeReplaceTestEdit("testcases/packages/OtherTypes.hx", "packages.sub", 8, 16), + ]; + refactorAndCheck({fileName: "testcases/packages/OtherTypes.hx", toName: "packages.sub", pos: 12}, edits, async); + } + + public function testRenameHelperTypesModul(async:Async) { + var edits:Array = [ + makeMoveTestEdit("testcases/packages/HelperTypes.hx", "testcases/packages/sub/HelperTypes.hx"), + makeReplaceTestEdit("testcases/packages/HelperTypes.hx", "packages.sub", 8, 16), + makeReplaceTestEdit("testcases/packages/import.hx", "packages.sub.HelperTypes", 26, 46), + ]; + refactorAndCheck({fileName: "testcases/packages/HelperTypes.hx", toName: "packages.sub", pos: 12}, edits, async); + } +} diff --git a/test/refactor/ScopedLocalTest.hx b/test/refactor/ScopedLocalTest.hx index 33388fd..7f05c98 100644 --- a/test/refactor/ScopedLocalTest.hx +++ b/test/refactor/ScopedLocalTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class ScopedLocalTest extends TestBase { function setupClass() { setupTestSources(["testcases/scopedlocal"]); diff --git a/test/refactor/TypedefTest.hx b/test/refactor/TypedefTest.hx index f765940..b2da7cb 100644 --- a/test/refactor/TypedefTest.hx +++ b/test/refactor/TypedefTest.hx @@ -1,7 +1,5 @@ package refactor; -import refactor.TestEditableDocument.TestEdit; - class TypedefTest extends TestBase { function setupClass() { setupTestSources(["testcases/typedefs"]); diff --git a/testcases/packages/HelperTypes.hx b/testcases/packages/HelperTypes.hx new file mode 100644 index 0000000..4fee1b5 --- /dev/null +++ b/testcases/packages/HelperTypes.hx @@ -0,0 +1,6 @@ +package packages; + +typedef HelperTypeA = {} +typedef HelperTypeB = {} +typedef HelperTypeC = {} +typedef HelperTypes = {} diff --git a/testcases/packages/Main.hx b/testcases/packages/Main.hx new file mode 100644 index 0000000..25c64f6 --- /dev/null +++ b/testcases/packages/Main.hx @@ -0,0 +1,16 @@ +package packages; + +import packages.MoreTypes; +import packages.Types.Type3; +import packages.Types; + +class Main { + static function main() { + var test:Type1 = {}; + var test2:OtherTypes = {}; + var test3:packages.OtherTypes = {}; + var test4:packages.OtherTypes.OtherTypeA = {}; + var test5:packages.Type1 = {}; + var test6:HelperTypes; + } +} diff --git a/testcases/packages/MoreTypes.hx b/testcases/packages/MoreTypes.hx new file mode 100644 index 0000000..edac51d --- /dev/null +++ b/testcases/packages/MoreTypes.hx @@ -0,0 +1,6 @@ +package packages; + +typedef TypeA = {} +typedef TypeB = {} +typedef TypeC = {} +typedef MoreTypes = {} diff --git a/testcases/packages/OtherTypes.hx b/testcases/packages/OtherTypes.hx new file mode 100644 index 0000000..282bb61 --- /dev/null +++ b/testcases/packages/OtherTypes.hx @@ -0,0 +1,6 @@ +package packages; + +typedef OtherTypeA = {} +typedef OtherTypeB = {} +typedef OtherTypeC = {} +typedef OtherTypes = {} diff --git a/testcases/packages/Types.hx b/testcases/packages/Types.hx new file mode 100644 index 0000000..2cf4e3f --- /dev/null +++ b/testcases/packages/Types.hx @@ -0,0 +1,5 @@ +package packages; + +typedef Type1 = {} +typedef Type2 = {} +typedef Type3 = {} diff --git a/testcases/packages/import.hx b/testcases/packages/import.hx new file mode 100644 index 0000000..6169581 --- /dev/null +++ b/testcases/packages/import.hx @@ -0,0 +1,3 @@ +package packages; + +import packages.HelperTypes;