Skip to content

Commit

Permalink
fixed detecting local var shadows when renaming a field
Browse files Browse the repository at this point in the history
added more testcases
  • Loading branch information
AlexHaxe committed Apr 19, 2022
1 parent 09302bc commit 63dee75
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## dev branch / next version (2.x.x)

## 2.1.0 (2022-x-x)

- added support for handling shadowed identifiers during local var/param rename
- fixed detecting local var shadows when renaming a field

## 2.0.0 (2022-04-16)

Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"refactor"
],
"description": "A code renaming tool for Haxe",
"version": "2.0.0",
"releasenote": "added external typer api; refactored codebase for asynchronous operation",
"version": "2.1.0",
"releasenote": "added support for handling shadowed identifiers",
"contributors": [
"AlexHaxe"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haxecheckstyle/haxe-rename",
"version": "2.0.0",
"version": "2.1.0",
"description": "Renaming tool for Haxe",
"repository": {
"type": "git",
Expand Down
5 changes: 3 additions & 2 deletions src/refactor/PrintHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PrintHelper {
'UnknownType($name, ${params.map((i) -> i.name)})';
}
}

public static function printRefactorResult(result:RefactorResult):String {
return switch (result) {
case NoChange:
Expand All @@ -53,9 +54,9 @@ class PrintHelper {
case Unsupported(name):
"renaming not supported for " + name;
case DryRun:
"dry run - no chnages were made";
"dry run - no changes were made";
case Done:
"rename succesful";
"rename successful";
}
}
}
31 changes: 25 additions & 6 deletions src/refactor/rename/RenameField.hx
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,33 @@ class RenameField {
}

static function replaceInTypeWithFieldAccess(changelist:Changelist, type:Type, prefix:String, from:String, to:String) {
var allUses:Array<Identifier> = type.getStartsWith('$prefix$from.');
for (use in allUses) {
var allUses:Array<Identifier> = type.getIdentifiers(prefix + from);
var allAccess:Array<Identifier> = type.getStartsWith('$prefix$from.');
var shadowed:Bool = false;
for (access in allAccess) {
for (use in allUses) {
if (use.pos.start > access.pos.start) {
break;
}
switch (use.type) {
case ScopedLocal(end, _):
if (end > access.pos.start) {
shadowed = true;
break;
}
case StructureField(_):
default:
}
}
if (shadowed) {
continue;
}
var pos:IdentifierPos = {
fileName: use.pos.fileName,
start: use.pos.start + prefix.length,
end: use.pos.start + prefix.length + from.length
fileName: access.pos.fileName,
start: access.pos.start + prefix.length,
end: access.pos.start + prefix.length + from.length
};
changelist.addChange(use.pos.fileName, ReplaceText(to, pos), use);
changelist.addChange(access.pos.fileName, ReplaceText(to, pos), access);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

-D coverage-console-summary-reporter
-D coverage-console-package-summary-reporter
#-D coverage-console-file-summary-reporter
# -D coverage-console-file-summary-reporter
-D coverage-lcov-reporter
-D coverage-codecov-reporter

Expand Down
26 changes: 25 additions & 1 deletion test/refactor/ClassTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,26 @@ class ClassTest extends TestBase {
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 162, 166),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 174, 178),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 248, 252),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 336, 340),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 407, 411),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 425, 429),

];
refactorAndCheck({fileName: "testcases/classes/BaseClass.hx", toName: "listOfData", pos: 44}, edits, async);
}

public function testRenameBaseClassVarFromSomewhere(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 41, 45),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 89, 93),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 162, 166),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 174, 178),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 248, 252),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 407, 411),
makeReplaceTestEdit("testcases/classes/BaseClass.hx", "listOfData", 425, 429),
];
refactorAndCheck({fileName: "testcases/classes/BaseClass.hx", toName: "listOfData", pos: 163}, edits, async);
}

public function testRenameChildClass(async:Async) {
var edits:Array<TestEdit> = [
makeMoveTestEdit("testcases/classes/ChildClass.hx", "testcases/classes/ItemClass.hx"),
Expand Down Expand Up @@ -191,4 +203,16 @@ class ClassTest extends TestBase {
var edits:Array<TestEdit> = [];
failRefactor({fileName: "testcases/classes/BaseClass.hx", toName: "data", pos: 470}, 'local var "data" exists', async);
}

public function testRenameChildClassExtendsBaseClass(async:Async) {
var edits:Array<TestEdit> = [];
failRefactor({fileName: "testcases/classes/ChildClass.hx", toName: "parentBase", pos: 47},
"renaming not supported for BaseClass testcases/classes/ChildClass.hx@43-52 (Extends)", async);
}

public function testRenameImport(async:Async) {
var edits:Array<TestEdit> = [];
failRefactor({fileName: "testcases/classes/MyIdentifier.hx", toName: "refactor.Foo", pos: 44},
"renaming not supported for refactor.discover.File testcases/classes/MyIdentifier.hx@25-47 (ImportModul)", async);
}
}
6 changes: 6 additions & 0 deletions test/refactor/TypedefTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ class TypedefTest extends TestBase {
];
refactorAndCheck({fileName: "testcases/typedefs/Main.hx", toName: "lineNumber", pos: 552}, edits, async);
}

public function testRenameTypedefBase(async:Async) {
var edits:Array<TestEdit> = [];
failRefactor({fileName: "testcases/typedefs/Types.hx", toName: "Position", pos: 140},
"renaming not supported for IdentifierPos testcases/typedefs/Types.hx@134-147 (TypedefBase)", async);
}
}

0 comments on commit 63dee75

Please sign in to comment.