Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
same name as getter
Browse files Browse the repository at this point in the history
  • Loading branch information
a14n committed Oct 21, 2018
1 parent f5eff00 commit 68ec3db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/src/rules/avoid_shadowing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class _Visitor extends SimpleAstVisitor {
initializer.target is SuperExpression);
});

// exclude pattern : same name as current getter name
var currentGetter = _getCurrentGetter(node);
if (currentGetter != null) {
variables.removeWhere(
(variable) => currentGetter.name.name == variable.name.name);
}

bool skipInstanceMembers = false;
AstNode current = node;
while (current != null) {
Expand Down Expand Up @@ -181,4 +188,18 @@ class _Visitor extends SimpleAstVisitor {
}
}
}

MethodDeclaration _getCurrentGetter(VariableDeclarationStatement node) {
AstNode current = node.parent;
while (current != null) {
if (current is Block || current is FunctionBody) {
current = current.parent;
} else if (current is MethodDeclaration && current.isGetter) {
return current;
} else {
break;
}
}
return null;
}
}
14 changes: 13 additions & 1 deletion test/rules/avoid_shadowing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,27 @@ class E {
}
}

// exclude pattern
// exclude pattern : var x = this.x;
class F {
int x;
m() {
var x = this.x; // OK
}
}

// exclude pattern : var x = super.x;
class F2 extends F {
m() {
var x = super.x; // OK
}
}

// exclude pattern : same name as current getter name
class G {
var i;
get g {
var i = ''; // LINT
var g = ''; // OK
return g;
}
}

0 comments on commit 68ec3db

Please sign in to comment.