@@ -35,7 +35,7 @@ private static CompilationUnitSyntax CompilationUnitWidget(
35
35
36
36
if ( addGeneratedCodeLeadingTrivia )
37
37
{
38
- compilationUnit = GeneratedCodeLeadingTrivia ( compilationUnit ) ;
38
+ compilationUnit = AddGeneratedCodeLeadingTrivia ( compilationUnit ) ;
39
39
}
40
40
41
41
return normalizeWhitespace
@@ -71,6 +71,8 @@ private static ClassDeclarationSyntax ClassWidget(
71
71
IEnumerable < BaseTypeSyntax > ? baseTypes = null ,
72
72
MemberDeclarationSyntax ? member = null ,
73
73
IEnumerable < MemberDeclarationSyntax > ? members = null ,
74
+ AttributeSyntax ? attribute = null ,
75
+ IEnumerable < AttributeSyntax > ? attributes = null ,
74
76
bool addGeneratedCodeAttributes = false )
75
77
{
76
78
var classDeclaration = ClassDeclaration ( identifier ) ;
@@ -95,7 +97,13 @@ private static ClassDeclarationSyntax ClassWidget(
95
97
classDeclaration = classDeclaration . WithMembers ( List ( members ) ) ;
96
98
}
97
99
98
- return BaseWidgetDecoration ( classDeclaration , modifier , modifiers , addGeneratedCodeAttributes ) ;
100
+ return BaseWidgetDecoration (
101
+ widget : classDeclaration ,
102
+ modifier : modifier ,
103
+ modifiers : modifiers ,
104
+ attribute : attribute ,
105
+ attributes : attributes ,
106
+ addGeneratedCodeAttributes : addGeneratedCodeAttributes ) ;
99
107
}
100
108
101
109
private static ConstructorDeclarationSyntax ConstructorWidget (
@@ -106,6 +114,8 @@ private static ConstructorDeclarationSyntax ConstructorWidget(
106
114
IEnumerable < ParameterSyntax > ? parameters = null ,
107
115
StatementSyntax ? bodyStatement = null ,
108
116
IEnumerable < StatementSyntax > ? bodyStatements = null ,
117
+ AttributeSyntax ? attribute = null ,
118
+ IEnumerable < AttributeSyntax > ? attributes = null ,
109
119
bool addGeneratedCodeAttributes = false )
110
120
{
111
121
var constructorDeclaration = ConstructorDeclaration ( Identifier ( identifier ) ) ;
@@ -132,7 +142,13 @@ private static ConstructorDeclarationSyntax ConstructorWidget(
132
142
constructorDeclaration = constructorDeclaration . WithBody ( Block ( bodyStatements ) ) ;
133
143
}
134
144
135
- return BaseWidgetDecoration ( constructorDeclaration , modifier , modifiers , addGeneratedCodeAttributes ) ;
145
+ return BaseWidgetDecoration (
146
+ widget : constructorDeclaration ,
147
+ modifier : modifier ,
148
+ modifiers : modifiers ,
149
+ attribute : attribute ,
150
+ attributes : attributes ,
151
+ addGeneratedCodeAttributes : addGeneratedCodeAttributes ) ;
136
152
}
137
153
138
154
private static FieldDeclarationSyntax FieldWidget (
@@ -142,6 +158,8 @@ private static FieldDeclarationSyntax FieldWidget(
142
158
IEnumerable < SyntaxKind > ? modifiers = null ,
143
159
ExpressionSyntax ? initializer = null ,
144
160
IEnumerable < ExpressionSyntax > ? initializers = null ,
161
+ AttributeSyntax ? attribute = null ,
162
+ IEnumerable < AttributeSyntax > ? attributes = null ,
145
163
bool addGeneratedCodeAttributes = false )
146
164
{
147
165
var variableDeclaration = VariableDeclarator ( Identifier ( identifier ) ) ;
@@ -161,7 +179,13 @@ private static FieldDeclarationSyntax FieldWidget(
161
179
var fieldDeclaration = FieldDeclaration ( VariableDeclaration ( type )
162
180
. WithVariables ( SingletonSeparatedList ( variableDeclaration ) ) ) ;
163
181
164
- return BaseWidgetDecoration ( fieldDeclaration , modifier , modifiers , addGeneratedCodeAttributes ) ;
182
+ return BaseWidgetDecoration (
183
+ widget : fieldDeclaration ,
184
+ modifier : modifier ,
185
+ modifiers : modifiers ,
186
+ attribute : attribute ,
187
+ attributes : attributes ,
188
+ addGeneratedCodeAttributes : addGeneratedCodeAttributes ) ;
165
189
}
166
190
167
191
private static PropertyDeclarationSyntax PropertyWidget (
@@ -171,6 +195,9 @@ private static PropertyDeclarationSyntax PropertyWidget(
171
195
IEnumerable < SyntaxKind > ? modifiers = null ,
172
196
SyntaxKind ? accessor = null ,
173
197
IEnumerable < SyntaxKind > ? accessors = null ,
198
+ ExpressionSyntax ? initializer = null ,
199
+ AttributeSyntax ? attribute = null ,
200
+ IEnumerable < AttributeSyntax > ? attributes = null ,
174
201
bool addGeneratedCodeAttributes = false )
175
202
{
176
203
var propertyDeclaration = PropertyDeclaration ( type , Identifier ( identifier ) ) ;
@@ -191,7 +218,20 @@ private static PropertyDeclarationSyntax PropertyWidget(
191
218
. WithAccessors ( accessorList . Accessors . AddRange ( accessors . Select ( AccessorWidget ) ) ) ) ;
192
219
}
193
220
194
- return BaseWidgetDecoration ( propertyDeclaration , modifier , modifiers , addGeneratedCodeAttributes ) ;
221
+ if ( initializer is not null )
222
+ {
223
+ propertyDeclaration = propertyDeclaration
224
+ . WithInitializer ( EqualsValueClause ( initializer ) )
225
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
226
+ }
227
+
228
+ return BaseWidgetDecoration (
229
+ widget : propertyDeclaration ,
230
+ modifier : modifier ,
231
+ modifiers : modifiers ,
232
+ attribute : attribute ,
233
+ attributes : attributes ,
234
+ addGeneratedCodeAttributes : addGeneratedCodeAttributes ) ;
195
235
}
196
236
197
237
private static MethodDeclarationSyntax MethodWidget (
@@ -203,6 +243,8 @@ private static MethodDeclarationSyntax MethodWidget(
203
243
IEnumerable < ParameterSyntax > ? parameters = null ,
204
244
StatementSyntax ? bodyStatement = null ,
205
245
IEnumerable < StatementSyntax > ? bodyStatements = null ,
246
+ AttributeSyntax ? attribute = null ,
247
+ IEnumerable < AttributeSyntax > ? attributes = null ,
206
248
bool addGeneratedCodeAttributes = false )
207
249
{
208
250
var methodDeclaration = MethodDeclaration ( type , Identifier ( identifier ) ) ;
@@ -229,7 +271,13 @@ private static MethodDeclarationSyntax MethodWidget(
229
271
methodDeclaration = methodDeclaration . WithBody ( Block ( bodyStatements ) ) ;
230
272
}
231
273
232
- return BaseWidgetDecoration ( methodDeclaration , modifier , modifiers , addGeneratedCodeAttributes ) ;
274
+ return BaseWidgetDecoration (
275
+ widget : methodDeclaration ,
276
+ modifier : modifier ,
277
+ modifiers : modifiers ,
278
+ attribute : attribute ,
279
+ attributes : attributes ,
280
+ addGeneratedCodeAttributes : addGeneratedCodeAttributes ) ;
233
281
}
234
282
235
283
private static ParameterSyntax ParameterWidget (
@@ -343,26 +391,85 @@ private static AccessorDeclarationSyntax AccessorWidget(SyntaxKind kind)
343
391
return AccessorDeclaration ( kind ) . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
344
392
}
345
393
346
- private static TSyntax BaseWidgetDecoration < TSyntax > (
347
- TSyntax widget ,
348
- SyntaxKind ? modifier ,
349
- IEnumerable < SyntaxKind > ? modifiers ,
350
- bool addGeneratedCodeAttributes ) where TSyntax : MemberDeclarationSyntax
394
+ private static AttributeSyntax AttributeWidget (
395
+ string identifier ,
396
+ AttributeArgumentSyntax ? argument = null ,
397
+ IEnumerable < AttributeArgumentSyntax > ? arguments = null
398
+ )
399
+ {
400
+ var attributeSyntax = Attribute ( IdentifierName ( identifier ) ) ;
401
+
402
+ if ( argument is not null )
403
+ {
404
+ var argumentList = attributeSyntax . ArgumentList ?? AttributeArgumentList ( ) ;
405
+
406
+ attributeSyntax = attributeSyntax . WithArgumentList ( argumentList
407
+ . WithArguments ( argumentList . Arguments . Add ( argument ) ) ) ;
408
+ }
409
+
410
+ if ( arguments is not null )
411
+ {
412
+ var argumentList = attributeSyntax . ArgumentList ?? AttributeArgumentList ( ) ;
413
+
414
+ attributeSyntax = attributeSyntax . WithArgumentList ( argumentList
415
+ . WithArguments ( argumentList . Arguments . AddRange ( arguments ) ) ) ;
416
+ }
417
+
418
+ return attributeSyntax ;
419
+ }
420
+
421
+ private static TWidget BaseWidgetDecoration < TWidget > (
422
+ TWidget widget ,
423
+ SyntaxKind ? modifier = null ,
424
+ IEnumerable < SyntaxKind > ? modifiers = null ,
425
+ AttributeSyntax ? attribute = null ,
426
+ IEnumerable < AttributeSyntax > ? attributes = null ,
427
+ bool addGeneratedCodeAttributes = true ) where TWidget : MemberDeclarationSyntax
351
428
{
352
429
if ( modifier is not null )
353
430
{
354
- widget = ( TSyntax ) widget . WithModifiers ( widget . Modifiers . Add ( Token ( modifier . Value ) ) ) ;
431
+ widget = ( TWidget ) widget . WithModifiers ( widget . Modifiers . Add ( Token ( modifier . Value ) ) ) ;
355
432
}
356
433
357
434
if ( modifiers is not null )
358
435
{
359
- widget = ( TSyntax ) widget . WithModifiers ( TokenList ( modifiers . Select ( Token ) ) ) ;
436
+ widget = ( TWidget ) widget . WithModifiers ( TokenList ( modifiers . Select ( Token ) ) ) ;
360
437
}
361
438
362
- return addGeneratedCodeAttributes ? GeneratedCodeAttributesWidget ( widget ) : widget ;
439
+ if ( addGeneratedCodeAttributes )
440
+ {
441
+ var attributesList = GetGeneratedCodeAttributes ( widget ) ;
442
+
443
+ if ( attribute is not null )
444
+ {
445
+ attributesList . Add ( attribute ) ;
446
+ }
447
+
448
+ if ( attributes is not null )
449
+ {
450
+ attributesList . AddRange ( attributes ) ;
451
+ }
452
+
453
+ return ( TWidget ) widget . WithAttributeLists ( widget . AttributeLists . AddRange (
454
+ attributesList . Select ( attributeSyntax => AttributeList ( SingletonSeparatedList ( attributeSyntax ) ) ) ) ) ;
455
+ }
456
+
457
+ if ( attribute is not null )
458
+ {
459
+ widget = ( TWidget ) widget . WithAttributeLists ( widget . AttributeLists . Add (
460
+ AttributeList ( SingletonSeparatedList ( attribute ) ) ) ) ;
461
+ }
462
+
463
+ if ( attributes is not null )
464
+ {
465
+ widget = ( TWidget ) widget . WithAttributeLists ( widget . AttributeLists . AddRange (
466
+ attributes . Select ( attributeSyntax => AttributeList ( SingletonSeparatedList ( attributeSyntax ) ) ) ) ) ;
467
+ }
468
+
469
+ return widget ;
363
470
}
364
471
365
- private static TSyntax GeneratedCodeLeadingTrivia < TSyntax > ( TSyntax node ) where TSyntax : SyntaxNode
472
+ private static TSyntax AddGeneratedCodeLeadingTrivia < TSyntax > ( TSyntax node ) where TSyntax : SyntaxNode
366
473
{
367
474
// Prepare the leading trivia for the generated compilation unit.
368
475
// This will produce code as follows:
@@ -378,26 +485,28 @@ private static TSyntax GeneratedCodeLeadingTrivia<TSyntax>(TSyntax node) where T
378
485
return node . WithLeadingTrivia ( syntaxTriviaList ) ;
379
486
}
380
487
381
- private static TSyntax GeneratedCodeAttributesWidget < TSyntax > ( TSyntax member ) where TSyntax : MemberDeclarationSyntax
488
+ private static List < AttributeSyntax > GetGeneratedCodeAttributes < TMember > ( TMember member ) where TMember : MemberDeclarationSyntax
382
489
{
383
- // [GeneratedCode] is always present.
384
- member = ( TSyntax ) member
385
- . WithoutLeadingTrivia ( )
386
- . AddAttributeLists ( AttributeList ( SingletonSeparatedList (
387
- Attribute ( IdentifierName ( "global::System.CodeDom.Compiler.GeneratedCode" ) )
388
- . AddArgumentListArguments (
389
- AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( AssemblyName . Name ) ) ) ,
390
- AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( AssemblyName . Version . ToString ( ) ) ) ) ) ) ) )
391
- . WithLeadingTrivia ( member . GetLeadingTrivia ( ) ) ;
490
+ var attributes = new List < AttributeSyntax >
491
+ {
492
+ AttributeWidget (
493
+ identifier : "global::System.CodeDom.Compiler.GeneratedCode" ,
494
+ arguments : new [ ]
495
+ {
496
+ AttributeArgument (
497
+ LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( AssemblyName . Name ) ) ) ,
498
+ AttributeArgument (
499
+ LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( AssemblyName . Version . ToString ( ) ) ) )
500
+ } )
501
+ } ;
392
502
393
503
// [ExcludeFromCodeCoverage] is not supported on interfaces and fields.
394
504
if ( member . Kind ( ) is not SyntaxKind . InterfaceDeclaration and not SyntaxKind . FieldDeclaration )
395
505
{
396
- member = ( TSyntax ) member
397
- . AddAttributeLists ( AttributeList ( SingletonSeparatedList (
398
- Attribute ( IdentifierName ( "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" ) ) ) ) ) ;
506
+ attributes . Add ( AttributeWidget (
507
+ identifier : "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" ) ) ;
399
508
}
400
509
401
- return member ;
510
+ return attributes ;
402
511
}
403
512
}
0 commit comments