提交 1fa2adb7 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #14611 from CyrusNajmabadi/portFix11

Fix issue with chained calls in Replace-Method-With-Property.
...@@ -598,6 +598,33 @@ void Test() ...@@ -598,6 +598,33 @@ void Test()
{ {
var y = GetFoo(out int i); var y = GetFoo(out int i);
} }
}");
}
[WorkItem(14327, "https://github.com/dotnet/roslyn/issues/14327")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
public async Task TestUpdateChainedGet1()
{
await TestAsync(
@"
public class Foo
{
public Foo()
{
Foo value = GetValue().GetValue();
}
public Foo [||]GetValue() { return this; }
}",
@"
public class Foo
{
public Foo()
{
Foo value = Value.Value;
}
public Foo Value { get { return this; } }
}"); }");
} }
} }
......
...@@ -270,5 +270,31 @@ NewLines("partial class C \n function [||]GetFoo() as integer \n End function \n ...@@ -270,5 +270,31 @@ NewLines("partial class C \n function [||]GetFoo() as integer \n End function \n
NewLines("partial class C \n Property Foo as integer \n Get \n End Get \n Set(i as integer) \n End Set \n End Property \n End class \n partial class C \n End class"), NewLines("partial class C \n Property Foo as integer \n Get \n End Get \n Set(i as integer) \n End Set \n End Property \n End class \n partial class C \n End class"),
index:=1) index:=1)
End Function End Function
<WorkItem(14327, "https://github.com/dotnet/roslyn/issues/14327")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)>
Public Async Function TestUpdateChainedGet1() As Task
Await TestAsync(
"
public class Foo
public sub Foo()
dim v = GetValue().GetValue()
end sub
Public Function [||]GetValue() As Foo
End Function
end class",
"
public class Foo
public sub Foo()
dim v = Value.Value
end sub
Public ReadOnly Property Value As Foo
Get
End Get
End Property
end class")
End Function
End Class End Class
End Namespace End Namespace
\ No newline at end of file
...@@ -207,8 +207,17 @@ public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node) ...@@ -207,8 +207,17 @@ public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
} }
} }
// We use the callback form if "ReplaceNode" here because we want to see the
// invocation expression after any rewrites we already did when rewriting previous
// 'get' references.
private static Action<SyntaxEditor, InvocationExpressionSyntax, SimpleNameSyntax, SimpleNameSyntax> s_replaceGetReferenceInvocation = private static Action<SyntaxEditor, InvocationExpressionSyntax, SimpleNameSyntax, SimpleNameSyntax> s_replaceGetReferenceInvocation =
(editor, invocation, nameNode, newName) => editor.ReplaceNode(invocation, invocation.Expression.ReplaceNode(nameNode, newName)); (editor, invocation, nameNode, newName) => editor.ReplaceNode(invocation, (i, g) =>
{
var currentInvocation = (InvocationExpressionSyntax)i;
var currentName = currentInvocation.Expression.GetRightmostName();
return currentInvocation.Expression.ReplaceNode(currentName, newName);
});
private static Action<SyntaxEditor, InvocationExpressionSyntax, SimpleNameSyntax, SimpleNameSyntax> s_replaceSetReferenceInvocation = private static Action<SyntaxEditor, InvocationExpressionSyntax, SimpleNameSyntax, SimpleNameSyntax> s_replaceSetReferenceInvocation =
(editor, invocation, nameNode, newName) => (editor, invocation, nameNode, newName) =>
...@@ -300,4 +309,4 @@ private static bool IsInvocationName(IdentifierNameSyntax nameNode, ExpressionSy ...@@ -300,4 +309,4 @@ private static bool IsInvocationName(IdentifierNameSyntax nameNode, ExpressionSy
return false; return false;
} }
} }
} }
\ No newline at end of file
...@@ -169,7 +169,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP ...@@ -169,7 +169,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.ReplaceMethodWithP
Dim parentExpression = If(nameNode.IsRightSideOfDot(), DirectCast(nameNode.Parent, ExpressionSyntax), nameNode) Dim parentExpression = If(nameNode.IsRightSideOfDot(), DirectCast(nameNode.Parent, ExpressionSyntax), nameNode)
Dim root = If(parentExpression.IsParentKind(SyntaxKind.InvocationExpression), parentExpression.Parent, parentExpression) Dim root = If(parentExpression.IsParentKind(SyntaxKind.InvocationExpression), parentExpression.Parent, parentExpression)
editor.ReplaceNode(root, parentExpression.ReplaceNode(nameNode, newName)) editor.ReplaceNode(
root,
Function(c As SyntaxNode, g As SyntaxGenerator)
Dim currentRoot = DirectCast(c, ExpressionSyntax)
Dim expression = If(currentRoot.IsKind(SyntaxKind.InvocationExpression),
DirectCast(currentRoot, InvocationExpressionSyntax).Expression,
currentRoot)
Dim rightName = expression.GetRightmostName()
Return expression.ReplaceNode(rightName, newName.WithTrailingTrivia(currentRoot.GetTrailingTrivia()))
End Function)
End Sub End Sub
Public Sub ReplaceSetReference(editor As SyntaxEditor, nameToken As SyntaxToken, propertyName As String, nameChanged As Boolean) Implements IReplaceMethodWithPropertyService.ReplaceSetReference Public Sub ReplaceSetReference(editor As SyntaxEditor, nameToken As SyntaxToken, propertyName As String, nameChanged As Boolean) Implements IReplaceMethodWithPropertyService.ReplaceSetReference
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册