-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathContextExtensions.cs
More file actions
114 lines (98 loc) · 5.91 KB
/
ContextExtensions.cs
File metadata and controls
114 lines (98 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) 2025 ReactiveUI and contributors. All rights reserved.
// Licensed to the ReactiveUI and contributors under one or more agreements.
// The ReactiveUI and contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ReactiveUI.SourceGenerators.Helpers;
using ReactiveUI.SourceGenerators.Models;
using static ReactiveUI.SourceGenerators.Diagnostics.DiagnosticDescriptors;
namespace ReactiveUI.SourceGenerators.Extensions;
internal static class ContextExtensions
{
internal static void GetForwardedAttributes(
this in GeneratorAttributeSyntaxContext context,
ImmutableArrayBuilder<DiagnosticInfo> builder,
ISymbol symbol,
in SyntaxList<AttributeListSyntax> attributeListSyntaxes,
CancellationToken token,
out ImmutableArray<string> forwardedAttributes)
{
using var forwardedAttributeBuilder = ImmutableArrayBuilder<AttributeInfo>.Rent();
var symbolAttributes = symbol.GetAttributes();
// if attributes contains the [Reactive] attribute, we should not forward any attributes without field targets
var isReactiveFromPartialProperty = symbol is IPropertySymbol && symbolAttributes.Any(a => a.AttributeClass?.HasFullyQualifiedMetadataName(AttributeDefinitions.ReactiveAttributeType) == true);
if (!isReactiveFromPartialProperty && symbolAttributes.Length > 1)
{
// Gather attributes info
foreach (var attribute in symbolAttributes)
{
token.ThrowIfCancellationRequested();
// Track the current attribute for forwarding if it is a validation attribute
if (attribute.AttributeClass?.InheritsFromFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.ValidationAttribute") == true)
{
forwardedAttributeBuilder.Add(AttributeInfo.Create(attribute));
}
// Track the current attribute for forwarding if it is a Json Serialization attribute
if (attribute.AttributeClass?.InheritsFromFullyQualifiedMetadataName("System.Text.Json.Serialization.JsonAttribute") == true)
{
forwardedAttributeBuilder.Add(AttributeInfo.Create(attribute));
}
// Also track the current attribute for forwarding if it is of any of the following types:
if (attribute.AttributeClass?.HasOrInheritsFromFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.UIHintAttribute") == true ||
attribute.AttributeClass?.HasOrInheritsFromFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.ScaffoldColumnAttribute") == true ||
attribute.AttributeClass?.HasFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.DisplayAttribute") == true ||
attribute.AttributeClass?.HasFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.EditableAttribute") == true ||
attribute.AttributeClass?.HasFullyQualifiedMetadataName("System.ComponentModel.DataAnnotations.KeyAttribute") == true ||
attribute.AttributeClass?.HasFullyQualifiedMetadataName("System.Runtime.Serialization.DataMemberAttribute") == true ||
attribute.AttributeClass?.HasFullyQualifiedMetadataName("System.Runtime.Serialization.IgnoreDataMemberAttribute") == true)
{
forwardedAttributeBuilder.Add(AttributeInfo.Create(attribute));
}
}
}
token.ThrowIfCancellationRequested();
// Gather explicit forwarded attributes info
foreach (var attributeList in attributeListSyntaxes)
{
// Only look for attribute lists explicitly targeting the (generated) property. Roslyn will normally emit a
// CS0657 warning (invalid target), but that is automatically suppressed by a dedicated diagnostic suppressor
// that recognizes uses of this target specifically to support [ObservableAsProperty].
if (attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.PropertyKeyword) && attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.FieldKeyword))
{
continue;
}
token.ThrowIfCancellationRequested();
foreach (var attribute in attributeList.Attributes)
{
if (!context.SemanticModel.GetSymbolInfo(attribute, token).TryGetAttributeTypeSymbol(out var attributeTypeSymbol))
{
builder.Add(
InvalidPropertyTargetedAttributeOnObservableAsPropertyField,
attribute,
symbol,
attribute.Name);
continue;
}
var attributeArguments = attribute.ArgumentList?.Arguments ?? Enumerable.Empty<AttributeArgumentSyntax>();
// Try to extract the forwarded attribute
if (!AttributeInfo.TryCreate(attributeTypeSymbol, context.SemanticModel, attributeArguments, token, out var attributeInfo))
{
builder.Add(
InvalidPropertyTargetedAttributeExpressionOnObservableAsPropertyField,
attribute,
symbol,
attribute.Name);
continue;
}
forwardedAttributeBuilder.Add(attributeInfo);
}
}
var attributes = forwardedAttributeBuilder.ToImmutable();
forwardedAttributes = attributes.Select(static a => a.ToString()).ToImmutableArray();
}
}