3
3
// The .NET Foundation licenses this file to you under the MIT license.
4
4
// See the LICENSE file in the project root for full license information.
5
5
6
- using System ;
7
6
using System . Collections . Generic ;
8
7
using System . Collections . Immutable ;
9
8
using System . Linq ;
12
11
using Microsoft . CodeAnalysis . CSharp ;
13
12
using Microsoft . CodeAnalysis . CSharp . Syntax ;
14
13
using ReactiveUI . SourceGenerators . Helpers ;
15
- using ReactiveUI . SourceGenerators . Models ;
16
14
17
15
namespace ReactiveUI . SourceGenerators . Extensions ;
18
16
@@ -21,62 +19,6 @@ namespace ReactiveUI.SourceGenerators.Extensions;
21
19
/// </summary>
22
20
internal static class AttributeDataExtensions
23
21
{
24
- /// <summary>
25
- /// Checks whether a given <see cref="AttributeData"/> instance contains a specified named argument.
26
- /// </summary>
27
- /// <typeparam name="T">The type of argument to check.</typeparam>
28
- /// <param name="attributeData">The target <see cref="AttributeData"/> instance to check.</param>
29
- /// <param name="name">The name of the argument to check.</param>
30
- /// <param name="value">The expected value for the target named argument.</param>
31
- /// <returns>Whether or not <paramref name="attributeData"/> contains an argument named <paramref name="name"/> with the expected value.</returns>
32
- public static bool HasNamedArgument < T > ( this AttributeData attributeData , string name , T ? value )
33
- {
34
- foreach ( var properties in attributeData . NamedArguments )
35
- {
36
- if ( properties . Key == name )
37
- {
38
- return
39
- properties . Value . Value is T argumentValue &&
40
- EqualityComparer < T ? > . Default . Equals ( argumentValue , value ) ;
41
- }
42
- }
43
-
44
- return false ;
45
- }
46
-
47
- /// <summary>
48
- /// Tries to get the location of the input <see cref="AttributeData"/> instance.
49
- /// </summary>
50
- /// <param name="attributeData">The input <see cref="AttributeData"/> instance to get the location for.</param>
51
- /// <returns>The resulting location for <paramref name="attributeData"/>, if a syntax reference is available.</returns>
52
- public static Location ? GetLocation ( this AttributeData attributeData )
53
- {
54
- if ( attributeData . ApplicationSyntaxReference is { } syntaxReference )
55
- {
56
- return syntaxReference . SyntaxTree . GetLocation ( syntaxReference . Span ) ;
57
- }
58
-
59
- return null ;
60
- }
61
-
62
- /// <summary>
63
- /// Gets a given named argument value from an <see cref="AttributeData"/> instance, or a fallback value.
64
- /// </summary>
65
- /// <typeparam name="T">The type of argument to check.</typeparam>
66
- /// <param name="attributeData">The target <see cref="AttributeData"/> instance to check.</param>
67
- /// <param name="name">The name of the argument to check.</param>
68
- /// <param name="fallback">The fallback value to use if the named argument is not present.</param>
69
- /// <returns>The argument named <paramref name="name"/>, or a fallback value.</returns>
70
- public static T ? GetNamedArgument < T > ( this AttributeData attributeData , string name , T ? fallback = default )
71
- {
72
- if ( attributeData . TryGetNamedArgument ( name , out T ? value ) )
73
- {
74
- return value ;
75
- }
76
-
77
- return fallback ;
78
- }
79
-
80
22
/// <summary>
81
23
/// Tries to get a given named argument value from an <see cref="AttributeData"/> instance, if present.
82
24
/// </summary>
@@ -138,63 +80,6 @@ public static bool TryGetNamedArgument<T>(this AttributeData attributeData, stri
138
80
return Enumerate ( attributeData . ConstructorArguments ) ;
139
81
}
140
82
141
- /// <summary>
142
- /// Gets the attribute syntax as a string for generating code.
143
- /// </summary>
144
- /// <param name="attribute">The attribute data from the original code.</param>
145
- /// <param name="token">The cancellation token for the operation.</param>
146
- /// <returns>A class array containing the syntax and other relevant metadata.</returns>
147
- public static PropertyAttributeData ? GetAttributeSyntax ( this AttributeData attribute , CancellationToken token )
148
- {
149
- // Retrieve the syntax from the attribute reference.
150
- if ( attribute . ApplicationSyntaxReference ? . GetSyntax ( token ) is not AttributeSyntax syntax )
151
- {
152
- // If the syntax is not available, return an empty string.
153
- return null ;
154
- }
155
-
156
- // Normalize the syntax for correct formatting and return it as a string.
157
- return new ( attribute . AttributeClass ? . ContainingNamespace ? . ToDisplayString ( SymbolHelpers . DefaultDisplay ) , syntax . NormalizeWhitespace ( ) . ToFullString ( ) ) ;
158
- }
159
-
160
- /// <summary>
161
- /// Generates a string containing the applicable attributes for a given target (e.g., field or property).
162
- /// </summary>
163
- /// <param name="attributes">The collection of attribute data to process.</param>
164
- /// <param name="allowedTarget">The attribute target (e.g., property, field).</param>
165
- /// <param name="token">The cancellation token.</param>
166
- /// <returns>A class array containing the syntax and other relevant metadata.</returns>
167
- public static PropertyAttributeData [ ] GenerateAttributes (
168
- this IEnumerable < AttributeData > attributes ,
169
- AttributeTargets allowedTarget ,
170
- CancellationToken token )
171
- {
172
- // Filter and convert each attribute to its syntax form, ensuring it can target the given element type.
173
- var applicableAttributes = attributes
174
- . Where ( attr => attr . AttributeClass . AttributeCanTarget ( allowedTarget ) )
175
- . Select ( attr => attr . GetAttributeSyntax ( token ) )
176
- . Where ( x => x is not null )
177
- . Select ( x => x ! )
178
- . ToImmutableArray ( ) ;
179
-
180
- return [ .. applicableAttributes ] ;
181
- }
182
-
183
- /// <summary>
184
- /// Generates the formatted attributes for fields and properties.
185
- /// </summary>
186
- /// <param name="attr">The attribute to format.</param>
187
- /// <returns>A formatted string of attributes.</returns>
188
- public static string FormatAttributes ( this PropertyAttributeData attr )
189
- {
190
- // If the attribute namespace is null, omit the dot (.) separator.
191
- var namespacePrefix = string . IsNullOrEmpty ( attr . AttributeNamespace )
192
- ? string . Empty
193
- : $ "{ attr . AttributeNamespace } .";
194
-
195
- return $ "[{ namespacePrefix } { attr . AttributeSyntax } ]";
196
- }
197
-
198
83
/// <summary>
199
84
/// Gathers the forwarded attributes from class.
200
85
/// </summary>
0 commit comments