Skip to content

Commit

Permalink
fixed local var scope
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed May 19, 2022
1 parent 23924e8 commit f6b3be2
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "bd1a05d",
"version": "a8f2911",
"resolveLibs": "scoped"
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 2.2.1 (2022-05-20)

- fixed local var scope

## 2.2.0 (2022-05-03)

- added canRename API call
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.2.0",
"releasenote": "added canRename API call - see CHANGELOG",
"version": "2.2.1",
"releasenote": "fixed local var scope - see CHANGELOG",
"contributors": [
"AlexHaxe"
],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.2.0",
"version": "2.2.1",
"description": "Renaming tool for Haxe",
"repository": {
"type": "git",
Expand Down
8 changes: 4 additions & 4 deletions src/refactor/PrintHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PrintHelper {
'EnumField(${params.map((p) -> '"${p.name}"')})';
case CaseLabel(switchIdentifier):
'CaseLabel(${switchIdentifier.name})';
case ScopedLocal(scopeEnd, scopeType):
'ScopedLocal($scopeEnd, ${scopeType.scopeTypeToString()})';
case ScopedLocal(scopeStart, scopeEnd, scopeType):
'ScopedLocal($scopeStart - $scopeEnd, ${scopeType.scopeTypeToString()})';
default:
'$identType';
}
Expand All @@ -29,8 +29,8 @@ class PrintHelper {
return switch (scopeType) {
case Parameter(params):
'Parameter(${params.map((i) -> '"${i.name}"')})';
case ForLoop(scopeStart, loopIdentifiers):
'ForLoop(${loopIdentifiers.map((i) -> '"${i.name}"')}) - scopeStart: $scopeStart';
case ForLoop(loopIdentifiers):
'ForLoop(${loopIdentifiers.map((i) -> '"${i.name}"')})';
default:
'$scopeType';
}
Expand Down
12 changes: 6 additions & 6 deletions src/refactor/Refactor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ class Refactor {
rename(context);
case CaseLabel(_):
Promise.reject(RefactorResult.Unsupported(identifier.toString()).printRefactorResult());
case ScopedLocal(scopeEnd, type):
context.verboseLog('rename scoped local "${identifier.name}" (${type.scopeTypeToString()}) to "${context.what.toName}"');
RenameScopedLocal.refactorScopedLocal(context, file, identifier, scopeEnd);
case ScopedLocal(scopeStart, scopeEnd, type):
context.verboseLog('rename scoped local "${identifier.name} [$scopeStart - $scopeEnd]" (${type.scopeTypeToString()}) to "${context.what.toName}"');
RenameScopedLocal.refactorScopedLocal(context, file, identifier, scopeStart, scopeEnd);
}
}

Expand Down Expand Up @@ -153,12 +153,12 @@ class Refactor {
if (candidate == null) {
candidate = use;
}
case ScopedLocal(scopeEnd, ForLoop(scopeStart, _)) if (!onlyFields):
case ScopedLocal(scopeStart, scopeEnd, ForLoop(_)) if (!onlyFields):
if ((scopeStart < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
candidate = use;
}
case ScopedLocal(scopeEnd, _) if (!onlyFields):
if ((use.pos.start < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
case ScopedLocal(scopeStart, scopeEnd, _) if (!onlyFields):
if ((scopeStart < identifier.pos.start) && (identifier.pos.start < scopeEnd)) {
candidate = use;
}
default:
Expand Down
4 changes: 2 additions & 2 deletions src/refactor/discover/IdentifierType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ enum IdentifierType {
ArrayAccess(posClosing:Int);
Access;
ForIterator;
ScopedLocal(scopeEnd:Int, scopeType:ScopedLocalType);
ScopedLocal(scopeStart:Int, scopeEnd:Int, scopeType:ScopedLocalType);
StringConst;
}

enum ScopedLocalType {
Parameter(params:Array<Identifier>);
Var;
CaseCapture;
ForLoop(scopeStart:Int, loopIdentifiers:Array<Identifier>);
ForLoop(loopIdentifiers:Array<Identifier>);
}

enum TypedefFieldType {
Expand Down
39 changes: 23 additions & 16 deletions src/refactor/discover/UsageCollector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,14 @@ class UsageCollector {
for (child in token.children) {
switch (child.tok) {
case Kwd(KwdVar):
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
child = child.getFirstChild();
makeIdentifier(context, child, ScopedLocal(child.getPos().max, scopeEnd, Var), identifier);
case Kwd(KwdFinal):
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
child = child.getFirstChild();
makeIdentifier(context, child, ScopedLocal(child.getPos().max, scopeEnd, Var), identifier);
case Kwd(KwdFunction):
var method:Identifier = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
child = child.getFirstChild();
var method:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Var), identifier);
readMethod(context, method, child.getFirstChild());
case Dot:
case Semicolon:
Expand Down Expand Up @@ -599,17 +602,19 @@ class UsageCollector {
case Kwd(KwdVar):
var fullPos:Position = token.parent.getPos();
var scopeEnd:Int = fullPos.max;
var variable:Identifier = makeIdentifier(context, token.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
readVarInit(context, variable, token.getFirstChild());
var token:TokenTree = token.getFirstChild();
var variable:Identifier = makeIdentifier(context, token, ScopedLocal(token.getPos().max, scopeEnd, Var), identifier);
readVarInit(context, variable, token);
return;
case Kwd(KwdFunction):
var fullPos:Position = token.parent.getPos();
var scopeEnd:Int = fullPos.max;
var method:Null<Identifier> = makeIdentifier(context, token.getFirstChild(), ScopedLocal(scopeEnd, Var), identifier);
var child:TokenTree = token.getFirstChild();
var method:Null<Identifier> = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Var), identifier);
if (method == null) {
readMethod(context, identifier, token);
} else {
readMethod(context, method, token.getFirstChild());
readMethod(context, method, child);
}
return;
case Kwd(KwdThis):
Expand Down Expand Up @@ -706,15 +711,15 @@ class UsageCollector {
if (pClose != null) {
scopeStart = pClose.pos.max;
}
var ident:Identifier = makeIdentifier(context, token, ScopedLocal(scopeEnd, ForLoop(scopeStart, loopIdentifiers)), identifier);
var ident:Identifier = makeIdentifier(context, token, ScopedLocal(scopeStart, scopeEnd, ForLoop(loopIdentifiers)), identifier);
loopIdentifiers.push(ident);
if (!token.hasChildren()) {
return;
}
for (child in token.children) {
switch (child.tok) {
case Binop(OpArrow):
ident = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, ForLoop(scopeStart, loopIdentifiers)), identifier);
ident = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeStart, scopeEnd, ForLoop(loopIdentifiers)), identifier);
loopIdentifiers.push(ident);
default:
readExpression(context, ident, child);
Expand Down Expand Up @@ -744,7 +749,8 @@ class UsageCollector {
case Const(CIdent(_)):
readCaseConst(context, identifier, child, scopeEnd);
case Kwd(KwdVar):
makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, CaseCapture), identifier);
child = child.getFirstChild();
makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, CaseCapture), identifier);
case BkOpen:
readCaseArray(context, identifier, child, scopeEnd);
case BrOpen:
Expand Down Expand Up @@ -780,7 +786,7 @@ class UsageCollector {
return;
}
for (child in token.children) {
makeIdentifier(context, child, ScopedLocal(scopeEnd, CaseCapture), identifier);
makeIdentifier(context, child, ScopedLocal(child.pos.max, scopeEnd, CaseCapture), identifier);
}
}

Expand Down Expand Up @@ -809,7 +815,7 @@ class UsageCollector {
}
if (field.uses != null) {
for (use in field.uses) {
use.type = ScopedLocal(scopeEnd, CaseCapture);
use.type = ScopedLocal(use.pos.start, scopeEnd, CaseCapture);
}
}
default:
Expand All @@ -823,10 +829,11 @@ class UsageCollector {
for (child in token.children) {
switch (child.tok) {
case Question:
var paramIdent:Identifier = makeIdentifier(context, child.getFirstChild(), ScopedLocal(scopeEnd, Parameter(params)), identifier);
child = child.getFirstChild();
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Parameter(params)), identifier);
params.push(paramIdent);
case Const(CIdent(s)):
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(scopeEnd, Parameter(params)), identifier);
case Const(CIdent(_)):
var paramIdent:Identifier = makeIdentifier(context, child, ScopedLocal(child.pos.min, scopeEnd, Parameter(params)), identifier);
params.push(paramIdent);
default:
}
Expand Down Expand Up @@ -912,7 +919,7 @@ class UsageCollector {
case Binop(OpIn):
case Binop(OpArrow):
switch (type) {
case ScopedLocal(_, ForLoop(_)):
case ScopedLocal(_, _, ForLoop(_)):
default:
pOpenToken = child;
}
Expand Down
13 changes: 8 additions & 5 deletions src/refactor/rename/RenameField.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ class RenameField {

static function replaceInType(changelist:Changelist, type:Type, prefix:String, from:String, to:String) {
var allUses:Array<Identifier> = type.getIdentifiers(prefix + from);
var scopeEnd:Int = 0;
var innerScopeStart:Int = 0;
var innerScopeEnd:Int = -1;
for (use in allUses) {
if (use.pos.start <= scopeEnd) {
if ((innerScopeStart < use.pos.start) && (use.pos.start < innerScopeEnd)) {
continue;
}

switch (use.type) {
case ScopedLocal(end, _):
scopeEnd = end;
case ScopedLocal(start, end, _):
innerScopeStart = start;
innerScopeEnd = end;
continue;
case StructureField(_):
continue;
Expand All @@ -92,7 +95,7 @@ class RenameField {
break;
}
switch (use.type) {
case ScopedLocal(end, _):
case ScopedLocal(_, end, _):
if (end > access.pos.start) {
shadowed = true;
break;
Expand Down
15 changes: 9 additions & 6 deletions src/refactor/rename/RenameHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ class RenameHelper {
fieldCandidate = use;
case EnumField(_):
return Promise.resolve(KnownType(use.defineType, []));
case ScopedLocal(scopeEnd, _):
if ((pos >= use.pos.start) && (pos <= scopeEnd)) {
case ScopedLocal(scopeStart, scopeEnd, _):
if ((pos >= scopeStart) && (pos <= scopeEnd)) {
candidate = use;
}
if (pos == use.pos.start) {
candidate = use;
}
case CaseLabel(switchIdentifier):
Expand All @@ -190,12 +193,12 @@ class RenameHelper {

var typeHint:Null<Identifier> = candidate.getTypeHint();
switch (candidate.type) {
case ScopedLocal(_, ForLoop(_, loopIdent)):
case ScopedLocal(_, _, ForLoop(loopIdent)):
var index:Int = loopIdent.indexOf(candidate);
var changes:Array<Promise<TypeHintType>> = [];
for (child in loopIdent) {
switch (child.type) {
case ScopedLocal(_, ForLoop(_, _)):
case ScopedLocal(_, _, ForLoop(_)):
continue;
default:
changes.push(findTypeOfIdentifier(context, {
Expand Down Expand Up @@ -223,7 +226,7 @@ class RenameHelper {
}
return Promise.reject("type not found");
});
case ScopedLocal(_, Parameter(params)):
case ScopedLocal(_, _, Parameter(params)):
if (typeHint != null) {
return typeFromTypeHint(context, typeHint);
}
Expand Down Expand Up @@ -376,7 +379,7 @@ class RenameHelper {
var firstParam:Null<Identifier> = null;
for (use in identifier.uses) {
switch (use.type) {
case ScopedLocal(_, Parameter(_)):
case ScopedLocal(_, _, Parameter(_)):
firstParam = use;
break;
default:
Expand Down
22 changes: 12 additions & 10 deletions src/refactor/rename/RenameScopedLocal.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import refactor.discover.IdentifierPos;
import refactor.edits.Changelist;

class RenameScopedLocal {
public static function refactorScopedLocal(context:RefactorContext, file:File, identifier:Identifier, scopeEnd:Int):Promise<RefactorResult> {
public static function refactorScopedLocal(context:RefactorContext, file:File, identifier:Identifier, scopeStart:Int,
scopeEnd:Int):Promise<RefactorResult> {
var changelist:Changelist = new Changelist(context);
var identifierDot:String = identifier.name + ".";
var toNameDot:String = context.what.toName + ".";
var scopeStart:Int = identifier.pos.start;
changelist.addChange(identifier.pos.fileName, ReplaceText(context.what.toName, identifier.pos), identifier);

var allUses:Array<Identifier> = identifier.defineType.findAllIdentifiers(function(ident:Identifier) {
if (ident.pos.start < scopeStart) {
return false;
Expand Down Expand Up @@ -60,20 +62,20 @@ class RenameScopedLocal {
}

var skipForIterator:Bool = false;
var innerScopeStart:Int = 0;
var innerScopeEnd:Int = -1;
for (use in allUses) {
if ((innerScopeStart < use.pos.start) && (use.pos.start < innerScopeEnd)) {
continue;
}
switch (use.type) {
case ScopedLocal(scopeEnd, ForLoop(start, _)):
case ScopedLocal(start, scopeEnd, _):
if (use.pos.start == identifier.pos.start) {
scopeStart = start;
skipForIterator = true;
} else {
scopeStart = scopeEnd;
continue;
}
case ScopedLocal(scopeEnd, _):
if (use.pos.start != identifier.pos.start) {
// new parameter with identical name, so we skip its scope
scopeStart = scopeEnd;
innerScopeStart = start;
innerScopeEnd = scopeEnd;
continue;
}
case StructureField(_):
Expand Down
Loading

0 comments on commit f6b3be2

Please sign in to comment.