Skip to content

Commit 704507c

Browse files
committed
Fix bugs in body removal rewriter in accesors, operators and records
1 parent d4ef227 commit 704507c

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff/SyntaxRewriter/RemoveBodySyntaxRewriter.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
1515
public static readonly RemoveBodyCSharpSyntaxRewriter Singleton = new();
1616

1717
private readonly SyntaxToken _semiColonToken = SyntaxFactory.Token(SyntaxKind.SemicolonToken);
18+
private readonly SyntaxToken _noneToken = SyntaxFactory.Token(SyntaxKind.None);
19+
private readonly SyntaxTriviaList _endLineTrivia = SyntaxFactory.TriviaList((Environment.NewLine == "\r\n") ? SyntaxFactory.CarriageReturnLineFeed : SyntaxFactory.LineFeed);
1820

1921
public override SyntaxNode? VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
2022
{
@@ -55,7 +57,9 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
5557

5658
public override SyntaxNode? VisitEventDeclaration(EventDeclarationSyntax node)
5759
{
58-
var result = node.WithAccessorList(GetEmptiedAccessors(node.AccessorList));
60+
var result = node
61+
.WithIdentifier(node.Identifier.WithoutTrivia())
62+
.WithAccessorList(GetEmptiedAccessors(node.AccessorList));
5963
return base.VisitEventDeclaration(result);
6064
}
6165

@@ -73,6 +77,11 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
7377

7478
public override SyntaxNode? VisitOperatorDeclaration(OperatorDeclarationSyntax node)
7579
{
80+
if (node.OperatorToken.IsKind(SyntaxKind.GreaterThanToken))
81+
{
82+
// Takes care of the missing space before the greater than
83+
node = node.WithOperatorToken(SyntaxFactory.Token(SyntaxFactory.TriviaList(SyntaxFactory.Space), node.OperatorToken.Kind(), SyntaxTriviaList.Empty));
84+
}
7685
var result = node
7786
.WithBody(null) // remove the default empty body wrapped by brackets
7887
.WithoutLeadingTrivia()
@@ -89,6 +98,36 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
8998
return base.VisitPropertyDeclaration(result);
9099
}
91100

101+
public override SyntaxNode? VisitRecordDeclaration(RecordDeclarationSyntax node)
102+
{
103+
if (node.Members.Any())
104+
{
105+
// Fix the spaces and lack of newline
106+
var replacedOpenBrace = SyntaxFactory.Token(_endLineTrivia.AddRange(node.GetLeadingTrivia()), SyntaxKind.OpenBraceToken, node.OpenBraceToken.TrailingTrivia);
107+
node = node.WithOpenBraceToken(replacedOpenBrace);
108+
}
109+
else
110+
{
111+
// Replace braces with semicolon
112+
node = node.WithOpenBraceToken(_noneToken)
113+
.WithCloseBraceToken(_noneToken)
114+
.WithSemicolonToken(_semiColonToken);
115+
}
116+
117+
if (node.ParameterList != null && node.ParameterList.Parameters.Any())
118+
{
119+
// Fix the extra space
120+
node = node.WithParameterList(node.ParameterList.WithoutTrailingTrivia());
121+
}
122+
else
123+
{
124+
// Remove the parentheses if there are no arguments
125+
node = node.WithParameterList(null);
126+
}
127+
128+
return base.VisitRecordDeclaration(node);
129+
}
130+
92131
private AccessorListSyntax? GetEmptiedAccessors(AccessorListSyntax? accessorList)
93132
{
94133
if (accessorList == null)
@@ -116,7 +155,7 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
116155
newAccessors.Add(accessorDeclaration);
117156
}
118157

119-
return SyntaxFactory.AccessorList(SyntaxFactory.List(newAccessors));
158+
return SyntaxFactory.AccessorList(SyntaxFactory.List(newAccessors)).WithLeadingTrivia(SyntaxFactory.Space);
120159
}
121160

122161
}

0 commit comments

Comments
 (0)