Skip to content

Commit

Permalink
Merge pull request dotnet#6213 from CyrusNajmabadi/replaceMethodArrow…
Browse files Browse the repository at this point in the history
…BodyUpdate1

Don't add accessors for a property with an arrow-body
  • Loading branch information
CyrusNajmabadi committed Oct 22, 2015
2 parents c6963a1 + 2be80f9 commit b533a40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public void TestMethodWithoutGetName()
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
[WorkItem(6034, "https://github.com/dotnet/roslyn/issues/6034")]
public void TestMethodWithArrowBody()
{
Test(
@"class C { int [||]GetFoo() => 0; }",
@"class C { int Foo { get; } => 0; }");
@"class C { int Foo => 0; }");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,10 @@ public SyntaxNode ConvertMethodsToProperty(
var getAccessor = CreateGetAccessor(getAndSetMethods);
var setAccessor = CreateSetAccessor(semanticModel, generator, getAndSetMethods);

var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(getAccessor));
if (setAccessor != null)
{
accessorList = accessorList.AddAccessors(new[] { setAccessor });
}

var property = SyntaxFactory.PropertyDeclaration(
getMethodDeclaration.AttributeLists, getMethodDeclaration.Modifiers,
getMethodDeclaration.ReturnType, getMethodDeclaration.ExplicitInterfaceSpecifier,
GetPropertyName(getMethodDeclaration.Identifier, propertyName, nameChanged), accessorList);
GetPropertyName(getMethodDeclaration.Identifier, propertyName, nameChanged), accessorList: null);

IEnumerable<SyntaxTrivia> trivia = getMethodDeclaration.GetLeadingTrivia();
var setMethodDeclaration = getAndSetMethods.SetMethodDeclaration;
Expand All @@ -98,6 +92,16 @@ public SyntaxNode ConvertMethodsToProperty(
property = property.WithExpressionBody(getMethodDeclaration.ExpressionBody);
property = property.WithSemicolonToken(getMethodDeclaration.SemicolonToken);
}
else
{
var accessorList = SyntaxFactory.AccessorList(SyntaxFactory.SingletonList(getAccessor));
if (setAccessor != null)
{
accessorList = accessorList.AddAccessors(new[] { setAccessor });
}

property = property.WithAccessorList(accessorList);
}

return property.WithAdditionalAnnotations(Formatter.Annotation);
}
Expand Down

0 comments on commit b533a40

Please sign in to comment.