Browse Source

AccessToClosureIssue: fixed some constructor issues

newNRvisualizers
Mansheng Yang 14 years ago
parent
commit
cf08a17e8a
  1. 5
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToClosureIssue.cs
  2. 5
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToModifiedClosureIssue.cs
  3. 8
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/LocalVariableNamePicker.cs
  4. 29
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AccessToModifiedClosureTests.cs

5
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToClosureIssue.cs

@ -98,7 +98,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
CheckVariable (((LocalResolveResult)ctx.Resolve (variableInitializer)).Variable, CheckVariable (((LocalResolveResult)ctx.Resolve (variableInitializer)).Variable,
variableDecl.Type, variableDecl.Type,
variableDecl.GetParent<Statement> ()); variableDecl.GetParent<Statement> ());
base.VisitVariableInitializer (variableInitializer);
} }
public override void VisitForeachStatement (ForeachStatement foreachStatement) public override void VisitForeachStatement (ForeachStatement foreachStatement)
@ -106,7 +105,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
CheckVariable (((LocalResolveResult)ctx.Resolve (foreachStatement.VariableNameToken)).Variable, CheckVariable (((LocalResolveResult)ctx.Resolve (foreachStatement.VariableNameToken)).Variable,
foreachStatement.VariableType, foreachStatement.VariableType,
foreachStatement); foreachStatement);
base.VisitForeachStatement (foreachStatement);
} }
public override void VisitParameterDeclaration (ParameterDeclaration parameterDeclaration) public override void VisitParameterDeclaration (ParameterDeclaration parameterDeclaration)
@ -119,11 +117,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
body = ((AnonymousMethodExpression)parent).Body; body = ((AnonymousMethodExpression)parent).Body;
} else if (parent is LambdaExpression) { } else if (parent is LambdaExpression) {
body = ((LambdaExpression)parent).Body as Statement; body = ((LambdaExpression)parent).Body as Statement;
} else if (parent is ConstructorDeclaration) {
body = ((ConstructorDeclaration)parent).Body;
} }
if (body != null) if (body != null)
CheckVariable (((LocalResolveResult)ctx.Resolve (parameterDeclaration)).Variable, CheckVariable (((LocalResolveResult)ctx.Resolve (parameterDeclaration)).Variable,
parameterDeclaration.Type, body); parameterDeclaration.Type, body);
base.VisitParameterDeclaration (parameterDeclaration);
} }
void FindLocalReferences (IVariable variable, FoundReferenceCallback callback) void FindLocalReferences (IVariable variable, FoundReferenceCallback callback)

5
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/AccessToModifiedClosureIssue.cs

@ -82,8 +82,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Action<Script> action = script => Action<Script> action = script =>
{ {
var newName = LocalVariableNamePicker.PickSafeName (containingStatement.GetParent<MethodDeclaration> (), AstNode parent = containingStatement.GetParent<MethodDeclaration> () ??
(AstNode)containingStatement.GetParent<ConstructorDeclaration> ();
var newName = LocalVariableNamePicker.PickSafeName (parent,
Enumerable.Range (1, 100).Select (i => variableName + i)); Enumerable.Range (1, 100).Select (i => variableName + i));
var variableDecl = new VariableDeclarationStatement (variableType.Clone (), newName, var variableDecl = new VariableDeclarationStatement (variableType.Clone (), newName,
new IdentifierExpression (variableName)); new IdentifierExpression (variableName));

8
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/AccessToClosureIssues/LocalVariableNamePicker.cs

@ -31,9 +31,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
public static class LocalVariableNamePicker public static class LocalVariableNamePicker
{ {
public static string PickSafeName (MethodDeclaration method, IEnumerable<string> candidates) public static string PickSafeName (AstNode node, IEnumerable<string> candidates)
{ {
var existingNames = new VariableNameCollector ().Collect (method); var existingNames = new VariableNameCollector ().Collect (node);
return candidates.FirstOrDefault (name => !existingNames.Contains (name)); return candidates.FirstOrDefault (name => !existingNames.Contains (name));
} }
@ -41,10 +41,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
private ISet<string> variableNames = new HashSet<string> (); private ISet<string> variableNames = new HashSet<string> ();
public ISet<string> Collect (MethodDeclaration methodDecl) public ISet<string> Collect (AstNode node)
{ {
variableNames.Clear (); variableNames.Clear ();
methodDecl.AcceptVisitor (this); node.AcceptVisitor (this);
return variableNames; return variableNames;
} }

29
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AccessToModifiedClosureTests.cs

@ -749,5 +749,34 @@ class TestClass
Test (input, 1, output); Test (input, 1, output);
} }
[Test]
public void TestConstructor ()
{
var input = @"
class TestClass
{
public TestClass (int[] a)
{
foreach (var i in a) {
int i1;
var f = new System.Func<int, int> (x => x + i);
}
}
}";
var output = @"
class TestClass
{
public TestClass (int[] a)
{
foreach (var i in a) {
int i1;
var i2 = i;
var f = new System.Func<int, int> (x => x + i2);
}
}
}";
Test (input, 1, output);
}
} }
} }

Loading…
Cancel
Save