diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c89df5..44552ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## dev branch / next version (2.x.x) +## 2.1.4 (2022-05-02) + +- fixed handling of star imports + ## 2.1.3 (2022-05-01) - fixed package renaming diff --git a/haxelib.json b/haxelib.json index aa6715e..c5bfd4e 100644 --- a/haxelib.json +++ b/haxelib.json @@ -8,8 +8,8 @@ "refactor" ], "description": "A code renaming tool for Haxe", - "version": "2.1.3", - "releasenote": "fixed package renaming", + "version": "2.1.4", + "releasenote": "fixed handling of star imports", "contributors": [ "AlexHaxe" ], diff --git a/package-lock.json b/package-lock.json index 59b8b3c..c83f32f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.3", + "version": "2.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.3", + "version": "2.1.4", "license": "MIT", "bin": { "haxe-rename": "bin/rename.js" diff --git a/package.json b/package.json index 76ef898..285530f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@haxecheckstyle/haxe-rename", - "version": "2.1.3", + "version": "2.1.4", "description": "Renaming tool for Haxe", "repository": { "type": "git", diff --git a/src/refactor/discover/File.hx b/src/refactor/discover/File.hx index b7b6d56..df874de 100644 --- a/src/refactor/discover/File.hx +++ b/src/refactor/discover/File.hx @@ -42,8 +42,10 @@ class File { } var fullModule:String = '$packName.$moduleName'; var fullSubModule:Null = null; + var isMainModule:Bool = true; if (moduleName != typeName) { fullSubModule = '$fullModule.$typeName'; + isMainModule = false; } for (importEntry in importList) { if (importEntry.moduleName.name == fullModule) { @@ -58,6 +60,11 @@ class File { } return Imported; } + if (isMainModule && importEntry.starImport) { + if (importEntry.moduleName.name == packName) { + return StarImported; + } + } } if (importHxFile == null) { @@ -152,4 +159,5 @@ enum ImportStatus { SamePackage; Imported; ImportedWithAlias(alias:String); + StarImported; } diff --git a/src/refactor/rename/RenameAnonStructField.hx b/src/refactor/rename/RenameAnonStructField.hx index 62c1626..ceafb6b 100644 --- a/src/refactor/rename/RenameAnonStructField.hx +++ b/src/refactor/rename/RenameAnonStructField.hx @@ -60,7 +60,7 @@ class RenameAnonStructField { switch (use.file.importsModule(packName, mainModuleName, type.name.name)) { case None: continue; - case Global | SamePackage | Imported | ImportedWithAlias(_): + case Global | SamePackage | Imported | ImportedWithAlias(_) | StarImported: } changelist.addChange(use.pos.fileName, ReplaceText(context.what.toName, use.pos), use); } @@ -140,7 +140,7 @@ class RenameAnonStructField { switch (baseTypeName.file.importsModule(use.file.getPackage(), use.file.getMainModulName(), baseTypeName.name)) { case None: continue; - case Global | SamePackage | Imported | ImportedWithAlias(_): + case Global | SamePackage | Imported | ImportedWithAlias(_) | StarImported: } return use.defineType; } diff --git a/src/refactor/rename/RenameEnumField.hx b/src/refactor/rename/RenameEnumField.hx index 48c875b..98c4b02 100644 --- a/src/refactor/rename/RenameEnumField.hx +++ b/src/refactor/rename/RenameEnumField.hx @@ -24,9 +24,7 @@ class RenameEnumField { if (alias != typeName) { continue; } - case Global: - case SamePackage: - case Imported: + case Global | SamePackage | Imported | StarImported: } RenameHelper.replaceTextWithPrefix(use, typeName, context.what.toName, changelist); } @@ -65,7 +63,7 @@ class RenameEnumField { if (alias != typeName) { continue; } - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: } default: continue; diff --git a/src/refactor/rename/RenameField.hx b/src/refactor/rename/RenameField.hx index 3b6c7f2..23fc9a7 100644 --- a/src/refactor/rename/RenameField.hx +++ b/src/refactor/rename/RenameField.hx @@ -122,7 +122,7 @@ class RenameField { continue; case ImportedWithAlias(_): continue; - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: } RenameHelper.replaceTextWithPrefix(use, '${type.name.name}.', context.what.toName, changelist); } diff --git a/src/refactor/rename/RenameHelper.hx b/src/refactor/rename/RenameHelper.hx index 79aeb5c..fa3e33a 100644 --- a/src/refactor/rename/RenameHelper.hx +++ b/src/refactor/rename/RenameHelper.hx @@ -53,7 +53,7 @@ class RenameHelper { var search:String = switch (use.file.importsModule(baseType.file.getPackage(), baseType.file.getMainModulName(), baseType.name.name)) { case None: continue; - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: baseType.name.name; case ImportedWithAlias(alias): alias; @@ -316,7 +316,7 @@ class RenameHelper { switch (type.file.importsModule(candidate.file.getPackage(), candidate.file.getMainModulName(), candidate.name.name)) { case None: case ImportedWithAlias(_): - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: return candidate; } } @@ -359,7 +359,7 @@ class RenameHelper { switch (hint.file.importsModule(type.file.getPackage(), type.file.getMainModulName(), type.name.name)) { case None: case ImportedWithAlias(_): - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: return Promise.resolve(KnownType(type, typeParams)); } } diff --git a/src/refactor/rename/RenamePackage.hx b/src/refactor/rename/RenamePackage.hx index b8a09ea..8d0c5c5 100644 --- a/src/refactor/rename/RenamePackage.hx +++ b/src/refactor/rename/RenamePackage.hx @@ -64,7 +64,7 @@ class RenamePackage { } switch (use.file.importsModule(packageName, mainTypeName, mainTypeName)) { case None: - case Global | SamePackage: + case Global | SamePackage | StarImported: 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); diff --git a/src/refactor/rename/RenameTypeName.hx b/src/refactor/rename/RenameTypeName.hx index 7c133b2..3811628 100644 --- a/src/refactor/rename/RenameTypeName.hx +++ b/src/refactor/rename/RenameTypeName.hx @@ -51,7 +51,7 @@ class RenameTypeName { if (alias != identifier.name) { continue; } - case Global | SamePackage | Imported: + case Global | SamePackage | Imported | StarImported: } changes.push(RenameHelper.findTypeOfIdentifier(context, { name: use.name, diff --git a/test/refactor/ClassTest.hx b/test/refactor/ClassTest.hx index 1dfae6d..77f427d 100644 --- a/test/refactor/ClassTest.hx +++ b/test/refactor/ClassTest.hx @@ -21,10 +21,10 @@ class ClassTest extends TestBase { makeReplaceTestEdit("testcases/classes/BaseClass.hx", "addData", 121, 132), makeReplaceTestEdit("testcases/classes/ChildClass.hx", "addData", 145, 156), makeReplaceTestEdit("testcases/classes/ChildClass.hx", "addData", 187, 198), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 239, 250), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 355, 366), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 464, 475), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 652, 663), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 228, 239), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 344, 355), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 453, 464), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "addData", 641, 652), ]; refactorAndCheck({fileName: "testcases/classes/BaseClass.hx", toName: "addData", pos: 128}, edits, async); } @@ -71,14 +71,13 @@ class ClassTest extends TestBase { makeReplaceTestEdit("testcases/classes/pack/SecondChildHelper.hx", "ItemClass", 38, 48), makeReplaceTestEdit("testcases/classes/pack/SecondChildHelper.hx", "ItemClass", 111, 121), makeReplaceTestEdit("testcases/classes/pack/SecondChildHelper.hx", "ItemClass", 163, 173), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 38, 48), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 67, 77), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 143, 153), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 210, 220), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 299, 309), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 423, 433), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 510, 520), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 623, 633), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 56, 66), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 132, 142), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 199, 209), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 288, 298), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 412, 422), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 499, 509), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ItemClass", 612, 622), ]; refactorAndCheck({fileName: "testcases/classes/ChildClass.hx", toName: "ItemClass", pos: 28}, edits, async); } @@ -91,8 +90,7 @@ class ClassTest extends TestBase { makeInsertTestEdit("testcases/classes/EnumType.hx", "import classes.pack.ChildClass;\n", 18), makeInsertTestEdit("testcases/classes/StaticUsing.hx", "import classes.pack.ChildClass;\n", 18), makeReplaceTestEdit("testcases/classes/pack/SecondChildHelper.hx", "classes.pack.ChildClass", 30, 48), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "classes.pack.ChildClass", 30, 48), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "classes.pack.ChildClass", 59, 77), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "classes.pack.ChildClass", 48, 66), ]; refactorAndCheck({fileName: "testcases/classes/ChildClass.hx", toName: "classes.pack", pos: 10}, edits, async); } @@ -100,7 +98,7 @@ class ClassTest extends TestBase { public function testRenameTypedef(async:Async) { var edits:Array = [ makeReplaceTestEdit("testcases/classes/ChildClass.hx", "ChildList", 868, 880), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ChildList", 107, 119), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "ChildList", 96, 108), ]; refactorAndCheck({fileName: "testcases/classes/ChildClass.hx", toName: "ChildList", pos: 872}, edits, async); } @@ -109,6 +107,7 @@ class ClassTest extends TestBase { var edits:Array = [ makeReplaceTestEdit("testcases/classes/ChildHelper.hx", "sumChilds", 62, 65), makeReplaceTestEdit("testcases/classes/StaticUsing.hx", "sumChilds", 182, 185), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "sumChilds", 706, 709), ]; refactorAndCheck({fileName: "testcases/classes/ChildHelper.hx", toName: "sumChilds", pos: 64}, edits, async); } @@ -135,11 +134,11 @@ class ClassTest extends TestBase { var edits:Array = [ makeReplaceTestEdit("testcases/classes/ChildClass.hx", "parentBase", 67, 73), makeReplaceTestEdit("testcases/classes/ChildHelper.hx", "parentBase", 310, 316), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 232, 238), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 348, 354), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 457, 463), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 566, 572), - makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 645, 651), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 221, 227), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 337, 343), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 446, 452), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 555, 561), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "parentBase", 634, 640), ]; refactorAndCheck({fileName: "testcases/classes/ChildClass.hx", toName: "parentBase", pos: 69}, edits, async); } @@ -176,6 +175,19 @@ class ClassTest extends TestBase { refactorAndCheck({fileName: "testcases/classes/JsonClass.hx", toName: "jsonWidth", pos: 74}, edits, async); } + public function testRenameJsonClass(async:Async) { + var edits:Array = [ + makeMoveTestEdit("testcases/classes/JsonClass.hx", "testcases/classes/JsonImporter.hx"), + makeReplaceTestEdit("testcases/classes/JsonClass.hx", "JsonImporter", 24, 33), + makeReplaceTestEdit("testcases/classes/JsonClass.hx", "JsonImporter", 276, 285), + makeReplaceTestEdit("testcases/classes/JsonClass.hx", "JsonImporter", 287, 296), + makeReplaceTestEdit("testcases/classes/JsonClass.hx", "JsonImporter", 319, 328), + makeReplaceTestEdit("testcases/classes/JsonClass.hx", "JsonImporter", 336, 345), + makeReplaceTestEdit("testcases/classes/pack/UseChild.hx", "JsonImporter", 744, 753), + ]; + refactorAndCheck({fileName: "testcases/classes/JsonClass.hx", toName: "JsonImporter", pos: 28}, edits, async); + } + public function testRenameBaseClassParamterWithShadow(async:Async) { var edits:Array = [ makeReplaceTestEdit("testcases/classes/BaseClass.hx", "data", 227, 228), @@ -198,7 +210,7 @@ class ClassTest extends TestBase { public function testRenameUseChildClassParentSubPart(async:Async) { var edits:Array = []; - failRefactor({fileName: "testcases/classes/UseChild.hx", toName: "data", pos: 233}, "could not find identifier to rename", async); + failRefactor({fileName: "testcases/classes/UseChild.hx", toName: "data", pos: 222}, "could not find identifier to rename", async); } public function testRenameBaseClassDataToData(async:Async) { diff --git a/testcases/classes/import.hx b/testcases/classes/import.hx index 0cdc46e..dde61ac 100644 --- a/testcases/classes/import.hx +++ b/testcases/classes/import.hx @@ -1,3 +1,4 @@ package classes; import refactor.discover.Identifier; +import classes.*; diff --git a/testcases/classes/pack/UseChild.hx b/testcases/classes/pack/UseChild.hx index 56d4c36..d173a22 100644 --- a/testcases/classes/pack/UseChild.hx +++ b/testcases/classes/pack/UseChild.hx @@ -1,6 +1,6 @@ package classes.pack; -import classes.ChildClass.*; +import classes.*; import classes.ChildClass; class UseChild { @@ -38,4 +38,10 @@ class UseChild { child.parent.doSomething([]); } #end + + function helperSum() { + ChildHelper.sum(list[0]); + } + + function json(json:JsonClass) {} }