Skip to content

Commit

Permalink
Simplify syntactic manipulatinos
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Feb 1, 2025
1 parent ae6e9cd commit ea80290
Show file tree
Hide file tree
Showing 2 changed files with 13 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 All @@ -168,10 +166,8 @@ protected override async Task<Document> ExpandToFieldPropertyAsync(
// Update the getter/setter to reference the 'field' expression instead.
var (newGetAccessor, newSetAccessor) = GetNewAccessors(info, property, FieldExpression(), cancellationToken);

// The normal helper will strip off the semicolon (as we're normally moving the initializer to a field).
// Don't do that here as we will keep the current initializer on the property if it is there.
var finalProperty = (PropertyDeclarationSyntax)CreateFinalProperty(document, property, info, newGetAccessor, newSetAccessor);
var finalRoot = root.ReplaceNode(property, finalProperty.WithSemicolonToken(property.SemicolonToken));
var finalProperty = CreateFinalProperty(document, property, info, newGetAccessor, newSetAccessor);
var finalRoot = root.ReplaceNode(property, finalProperty);

return document.WithSyntaxRoot(finalRoot);
}
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 ea80290

Please sign in to comment.