Skip to content

Commit f6b3be2

Browse files
committed
fixed local var scope
1 parent 23924e8 commit f6b3be2

File tree

14 files changed

+150
-55
lines changed

14 files changed

+150
-55
lines changed

.haxerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "bd1a05d",
2+
"version": "a8f2911",
33
"resolveLibs": "scoped"
44
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
## 2.2.1 (2022-05-20)
6+
7+
- fixed local var scope
8+
59
## 2.2.0 (2022-05-03)
610

711
- added canRename API call

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"refactor"
99
],
1010
"description": "A code renaming tool for Haxe",
11-
"version": "2.2.0",
12-
"releasenote": "added canRename API call - see CHANGELOG",
11+
"version": "2.2.1",
12+
"releasenote": "fixed local var scope - see CHANGELOG",
1313
"contributors": [
1414
"AlexHaxe"
1515
],

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@haxecheckstyle/haxe-rename",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Renaming tool for Haxe",
55
"repository": {
66
"type": "git",

src/refactor/PrintHelper.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class PrintHelper {
1818
'EnumField(${params.map((p) -> '"${p.name}"')})';
1919
case CaseLabel(switchIdentifier):
2020
'CaseLabel(${switchIdentifier.name})';
21-
case ScopedLocal(scopeEnd, scopeType):
22-
'ScopedLocal($scopeEnd, ${scopeType.scopeTypeToString()})';
21+
case ScopedLocal(scopeStart, scopeEnd, scopeType):
22+
'ScopedLocal($scopeStart - $scopeEnd, ${scopeType.scopeTypeToString()})';
2323
default:
2424
'$identType';
2525
}
@@ -29,8 +29,8 @@ class PrintHelper {
2929
return switch (scopeType) {
3030
case Parameter(params):
3131
'Parameter(${params.map((i) -> '"${i.name}"')})';
32-
case ForLoop(scopeStart, loopIdentifiers):
33-
'ForLoop(${loopIdentifiers.map((i) -> '"${i.name}"')}) - scopeStart: $scopeStart';
32+
case ForLoop(loopIdentifiers):
33+
'ForLoop(${loopIdentifiers.map((i) -> '"${i.name}"')})';
3434
default:
3535
'$scopeType';
3636
}

src/refactor/Refactor.hx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class Refactor {
119119
rename(context);
120120
case CaseLabel(_):
121121
Promise.reject(RefactorResult.Unsupported(identifier.toString()).printRefactorResult());
122-
case ScopedLocal(scopeEnd, type):
123-
context.verboseLog('rename scoped local "${identifier.name}" (${type.scopeTypeToString()}) to "${context.what.toName}"');
124-
RenameScopedLocal.refactorScopedLocal(context, file, identifier, scopeEnd);
122+
case ScopedLocal(scopeStart, scopeEnd, type):
123+
context.verboseLog('rename scoped local "${identifier.name} [$scopeStart - $scopeEnd]" (${type.scopeTypeToString()}) to "${context.what.toName}"');
124+
RenameScopedLocal.refactorScopedLocal(context, file, identifier, scopeStart, scopeEnd);
125125
}
126126
}
127127

@@ -153,12 +153,12 @@ class Refactor {
153153
if (candidate == null) {
154154
candidate = use;
155155
}
156-
case ScopedLocal(scopeEnd, ForLoop(scopeStart, _)) if (!onlyFields):
156+
case ScopedLocal(scopeStart, scopeEnd, ForLoop(_)) if (!onlyFields):
157157
if ((scopeStart < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
158158
candidate = use;
159159
}
160-
case ScopedLocal(scopeEnd, _) if (!onlyFields):
161-
if ((use.pos.start < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
160+
case ScopedLocal(scopeStart, scopeEnd, _) if (!onlyFields):
161+
if ((scopeStart < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
162162
candidate = use;
163163
}
164164
default:

src/refactor/discover/IdentifierType.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ enum IdentifierType {
3636
ArrayAccess(posClosing:Int);
3737
Access;
3838
ForIterator;
39-
ScopedLocal(scopeEnd:Int, scopeType:ScopedLocalType);
39+
ScopedLocal(scopeStart:Int, scopeEnd:Int, scopeType:ScopedLocalType);
4040
StringConst;
4141
}
4242

4343
enum ScopedLocalType {
4444
Parameter(params:Array<Identifier>);
4545
Var;
4646
CaseCapture;
47-
ForLoop(scopeStart:Int, loopIdentifiers:Array<Identifier>);
47+
ForLoop(loopIdentifiers:Array<Identifier>);
4848
}
4949

5050
enum TypedefFieldType {

src/refactor/discover/UsageCollector.hx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,14 @@ class UsageCollector {
560560
for (child in token.children) {
561561
switch (child.tok) {
562562
case Kwd(KwdVar):
563-
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
563+
child = child.getFirstChild();
564+
makeIdentifier(context, child, ScopedLocal(child.getPos().max, scopeEnd, Var), identifier);
564565
case Kwd(KwdFinal):
565-
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
566+
child = child.getFirstChild();
567+
makeIdentifier(context, child, ScopedLocal(child.getPos().max, scopeEnd, Var), identifier);
566568
case Kwd(KwdFunction):
567-
var method:Identifier = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
569+
child = child.getFirstChild();
570+
var method:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Var), identifier);
568571
readMethod(context, method, child.getFirstChild());
569572
case Dot:
570573
case Semicolon:
@@ -599,17 +602,19 @@ class UsageCollector {
599602
case Kwd(KwdVar):
600603
var fullPos:Position = token.parent.getPos();
601604
var scopeEnd:Int = fullPos.max;
602-
var variable:Identifier = makeIdentifier(context, token.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
603-
readVarInit(context, variable, token.getFirstChild());
605+
var token:TokenTree = token.getFirstChild();
606+
var variable:Identifier = makeIdentifier(context, token, ScopedLocal(token.getPos().max, scopeEnd, Var), identifier);
607+
readVarInit(context, variable, token);
604608
return;
605609
case Kwd(KwdFunction):
606610
var fullPos:Position = token.parent.getPos();
607611
var scopeEnd:Int = fullPos.max;
608-
var method:Null<Identifier> = makeIdentifier(context, token.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
612+
var child:TokenTree = token.getFirstChild();
613+
var method:Null<Identifier> = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Var), identifier);
609614
if (method == null) {
610615
readMethod(context, identifier, token);
611616
} else {
612-
readMethod(context, method, token.getFirstChild());
617+
readMethod(context, method, child);
613618
}
614619
return;
615620
case Kwd(KwdThis):
@@ -706,15 +711,15 @@ class UsageCollector {
706711
if (pClose != null) {
707712
scopeStart = pClose.pos.max;
708713
}
709-
var ident:Identifier = makeIdentifier(context, token, ScopedLocal(scopeEnd, ForLoop(scopeStart, loopIdentifiers)), identifier);
714+
var ident:Identifier = makeIdentifier(context, token, ScopedLocal(scopeStart, scopeEnd, ForLoop(loopIdentifiers)), identifier);
710715
loopIdentifiers.push(ident);
711716
if (!token.hasChildren()) {
712717
return;
713718
}
714719
for (child in token.children) {
715720
switch (child.tok) {
716721
case Binop(OpArrow):
717-
ident = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, ForLoop(scopeStart, loopIdentifiers)), identifier);
722+
ident = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeStart, scopeEnd, ForLoop(loopIdentifiers)), identifier);
718723
loopIdentifiers.push(ident);
719724
default:
720725
readExpression(context, ident, child);
@@ -744,7 +749,8 @@ class UsageCollector {
744749
case Const(CIdent(_)):
745750
readCaseConst(context, identifier, child, scopeEnd);
746751
case Kwd(KwdVar):
747-
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, CaseCapture), identifier);
752+
child = child.getFirstChild();
753+
makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, CaseCapture), identifier);
748754
case BkOpen:
749755
readCaseArray(context, identifier, child, scopeEnd);
750756
case BrOpen:
@@ -780,7 +786,7 @@ class UsageCollector {
780786
return;
781787
}
782788
for (child in token.children) {
783-
makeIdentifier(context, child, ScopedLocal(scopeEnd, CaseCapture), identifier);
789+
makeIdentifier(context, child, ScopedLocal(child.pos.max, scopeEnd, CaseCapture), identifier);
784790
}
785791
}
786792

@@ -809,7 +815,7 @@ class UsageCollector {
809815
}
810816
if (field.uses != null) {
811817
for (use in field.uses) {
812-
use.type = ScopedLocal(scopeEnd, CaseCapture);
818+
use.type = ScopedLocal(use.pos.start, scopeEnd, CaseCapture);
813819
}
814820
}
815821
default:
@@ -823,10 +829,11 @@ class UsageCollector {
823829
for (child in token.children) {
824830
switch (child.tok) {
825831
case Question:
826-
var paramIdent:Identifier = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Parameter(params)), identifier);
832+
child = child.getFirstChild();
833+
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Parameter(params)), identifier);
827834
params.push(paramIdent);
828-
case Const(CIdent(s)):
829-
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(scopeEnd, Parameter(params)), identifier);
835+
case Const(CIdent(_)):
836+
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Parameter(params)), identifier);
830837
params.push(paramIdent);
831838
default:
832839
}
@@ -912,7 +919,7 @@ class UsageCollector {
912919
case Binop(OpIn):
913920
case Binop(OpArrow):
914921
switch (type) {
915-
case ScopedLocal(_, ForLoop(_)):
922+
case ScopedLocal(_, _, ForLoop(_)):
916923
default:
917924
pOpenToken = child;
918925
}

src/refactor/rename/RenameField.hx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ class RenameField {
6565

6666
static function replaceInType(changelist:Changelist, type:Type, prefix:String, from:String, to:String) {
6767
var allUses:Array<Identifier> = type.getIdentifiers(prefix + from);
68-
var scopeEnd:Int = 0;
68+
var innerScopeStart:Int = 0;
69+
var innerScopeEnd:Int = -1;
6970
for (use in allUses) {
70-
if (use.pos.start <= scopeEnd) {
71+
if ((innerScopeStart < use.pos.start) && (use.pos.start < innerScopeEnd)) {
7172
continue;
7273
}
74+
7375
switch (use.type) {
74-
case ScopedLocal(end, _):
75-
scopeEnd = end;
76+
case ScopedLocal(start, end, _):
77+
innerScopeStart = start;
78+
innerScopeEnd = end;
7679
continue;
7780
case StructureField(_):
7881
continue;
@@ -92,7 +95,7 @@ class RenameField {
9295
break;
9396
}
9497
switch (use.type) {
95-
case ScopedLocal(end, _):
98+
case ScopedLocal(_, end, _):
9699
if (end > access.pos.start) {
97100
shadowed = true;
98101
break;

0 commit comments

Comments
 (0)