Skip to content

Commit

Permalink
Keep initializer when converting properties to field-backed properties (
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Feb 4, 2025
2 parents 4a8a5e3 + ea80290 commit 40ec700
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ protected override SyntaxNode ConvertPropertyToExpressionBodyIfDesired(

var preference = info.Options.PreferExpressionBodiedProperties.Value;
if (preference == ExpressionBodyPreference.Never)
{
return propertyDeclaration.WithSemicolonToken(default);
}
return propertyDeclaration;

// if there is a get accessors only, we can move the expression body to the property
if (propertyDeclaration.AccessorList?.Accessors.Count == 1 &&
Expand All @@ -146,7 +144,7 @@ protected override SyntaxNode ConvertPropertyToExpressionBodyIfDesired(
}
}

return propertyDeclaration.WithSemicolonToken(default);
return propertyDeclaration;
}

protected override SyntaxNode GetTypeBlock(SyntaxNode syntaxNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1444,4 +1444,34 @@ public int Goo
""";
await TestInRegularAndScriptAsync(text, expected, options: DoNotPreferExpressionBodiedAccessors, index: 1, parseOptions: CSharp14);
}

[Theory]
[InlineData("set"), InlineData("init")]
[WorkItem("https://github.com/dotnet/roslyn/issues/76992")]
public async Task ProduceFieldBackedProperty2(string setter)
{
var text = $$"""
class TestClass
{
public int G[||]oo { get; {{setter}}; } = 0;
}
""";
var expected = $$"""
class TestClass
{
public int Goo
{
get
{
return field;
}
{{setter}}
{
field = value;
}
} = 0;
}
""";
await TestInRegularAndScriptAsync(text, expected, options: DoNotPreferExpressionBodiedAccessors, index: 1, parseOptions: CSharp14);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ private async Task<Document> ExpandToFullPropertyAsync(
var fieldName = await GetFieldNameAsync(document, propertySymbol, cancellationToken).ConfigureAwait(false);
var (newGetAccessor, newSetAccessor) = GetNewAccessors(info, property, fieldName, cancellationToken);

editor.ReplaceNode(
property,
CreateFinalProperty(document, property, info, newGetAccessor, newSetAccessor));
var finalProperty = CreateFinalProperty(
document, GetPropertyWithoutInitializer(property), info, newGetAccessor, newSetAccessor);
editor.ReplaceNode(property, finalProperty);

// add backing field, plus initializer if it exists
var newField = CodeGenerationSymbolFactory.CreateFieldSymbol(
Expand Down Expand Up @@ -137,7 +137,7 @@ protected SyntaxNode CreateFinalProperty(

var fullProperty = generator
.WithAccessorDeclarations(
GetPropertyWithoutInitializer(property),
property,
newSetAccessor == null
? [newGetAccessor]
: [newGetAccessor, newSetAccessor])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,15 @@ private static SyntaxNode AccessorDeclaration(
public override SyntaxNode WithAccessorDeclarations(SyntaxNode declaration, IEnumerable<SyntaxNode> accessorDeclarations)
=> declaration switch
{
PropertyDeclarationSyntax property => property.WithAccessorList(CreateAccessorList(property.AccessorList, accessorDeclarations))
.WithExpressionBody(null)
.WithSemicolonToken(default),

IndexerDeclarationSyntax indexer => indexer.WithAccessorList(CreateAccessorList(indexer.AccessorList, accessorDeclarations))
.WithExpressionBody(null)
.WithSemicolonToken(default),
PropertyDeclarationSyntax property =>
property.WithAccessorList(CreateAccessorList(property.AccessorList, accessorDeclarations))
.WithExpressionBody(null)
.WithSemicolonToken(property.Initializer is null ? default : property.SemicolonToken),

IndexerDeclarationSyntax indexer =>
indexer.WithAccessorList(CreateAccessorList(indexer.AccessorList, accessorDeclarations))
.WithExpressionBody(null)
.WithSemicolonToken(default),

_ => declaration,
};
Expand Down

0 comments on commit 40ec700

Please sign in to comment.