@@ -15,6 +15,8 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
15
15
public static readonly RemoveBodyCSharpSyntaxRewriter Singleton = new ( ) ;
16
16
17
17
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 ) ;
18
20
19
21
public override SyntaxNode ? VisitConstructorDeclaration ( ConstructorDeclarationSyntax node )
20
22
{
@@ -55,7 +57,9 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
55
57
56
58
public override SyntaxNode ? VisitEventDeclaration ( EventDeclarationSyntax node )
57
59
{
58
- var result = node . WithAccessorList ( GetEmptiedAccessors ( node . AccessorList ) ) ;
60
+ var result = node
61
+ . WithIdentifier ( node . Identifier . WithoutTrivia ( ) )
62
+ . WithAccessorList ( GetEmptiedAccessors ( node . AccessorList ) ) ;
59
63
return base . VisitEventDeclaration ( result ) ;
60
64
}
61
65
@@ -73,6 +77,11 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
73
77
74
78
public override SyntaxNode ? VisitOperatorDeclaration ( OperatorDeclarationSyntax node )
75
79
{
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
+ }
76
85
var result = node
77
86
. WithBody ( null ) // remove the default empty body wrapped by brackets
78
87
. WithoutLeadingTrivia ( )
@@ -89,6 +98,36 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
89
98
return base . VisitPropertyDeclaration ( result ) ;
90
99
}
91
100
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
+
92
131
private AccessorListSyntax ? GetEmptiedAccessors ( AccessorListSyntax ? accessorList )
93
132
{
94
133
if ( accessorList == null )
@@ -116,7 +155,7 @@ internal class RemoveBodyCSharpSyntaxRewriter : CSharpSyntaxRewriter
116
155
newAccessors . Add ( accessorDeclaration ) ;
117
156
}
118
157
119
- return SyntaxFactory . AccessorList ( SyntaxFactory . List ( newAccessors ) ) ;
158
+ return SyntaxFactory . AccessorList ( SyntaxFactory . List ( newAccessors ) ) . WithLeadingTrivia ( SyntaxFactory . Space ) ;
120
159
}
121
160
122
161
}
0 commit comments