Skip to content

Commit 005dfee

Browse files
authored
Clean up and remove unsused reference code (#151)
Clean up and remove reference code
1 parent 9c23d0f commit 005dfee

32 files changed

+94
-1245
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using ReactiveUI;
7+
using ReactiveUI.SourceGenerators;
8+
9+
namespace SGReactiveUI.SourceGenerators.Test;
10+
11+
/// <summary>
12+
/// TestViewModel3.
13+
/// </summary>
14+
public partial class TestViewModel3 : ReactiveObject
15+
{
16+
[Reactive]
17+
private float _testVM3Property;
18+
19+
/////// <summary>
20+
/////// TestInnerClass.
21+
/////// </summary>
22+
////public partial class TestInnerClass1 : ReactiveObject
23+
////{
24+
//// [Reactive]
25+
//// private int _testInner1;
26+
////}
27+
28+
/////// <summary>
29+
/////// TestInnerClass.
30+
/////// </summary>
31+
////public partial class TestInnerClass2 : ReactiveObject
32+
////{
33+
//// [Reactive]
34+
//// private int _testInner2;
35+
36+
//// /// <summary>
37+
//// /// TestInnerClass4.
38+
//// /// </summary>
39+
//// /// <seealso cref="ReactiveUI.ReactiveObject" />
40+
//// public partial class TestInnerClass3 : ReactiveObject
41+
//// {
42+
//// [Reactive]
43+
//// private int _testInner3;
44+
//// }
45+
////}
46+
}

src/ReactiveUI.SourceGenerators/Core/Extensions/AttributeDataExtensions.cs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// The .NET Foundation licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6-
using System;
76
using System.Collections.Generic;
87
using System.Collections.Immutable;
98
using System.Linq;
@@ -12,7 +11,6 @@
1211
using Microsoft.CodeAnalysis.CSharp;
1312
using Microsoft.CodeAnalysis.CSharp.Syntax;
1413
using ReactiveUI.SourceGenerators.Helpers;
15-
using ReactiveUI.SourceGenerators.Models;
1614

1715
namespace ReactiveUI.SourceGenerators.Extensions;
1816

@@ -21,62 +19,6 @@ namespace ReactiveUI.SourceGenerators.Extensions;
2119
/// </summary>
2220
internal static class AttributeDataExtensions
2321
{
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-
8022
/// <summary>
8123
/// Tries to get a given named argument value from an <see cref="AttributeData"/> instance, if present.
8224
/// </summary>
@@ -138,63 +80,6 @@ public static bool TryGetNamedArgument<T>(this AttributeData attributeData, stri
13880
return Enumerate(attributeData.ConstructorArguments);
13981
}
14082

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-
19883
/// <summary>
19984
/// Gathers the forwarded attributes from class.
20085
/// </summary>

src/ReactiveUI.SourceGenerators/Core/Extensions/CompilationExtensions.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
// The .NET Foundation licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6-
using System;
7-
using System.Collections.Generic;
8-
using System.Collections.Immutable;
9-
using System.Diagnostics.CodeAnalysis;
106
using Microsoft.CodeAnalysis;
11-
using Microsoft.CodeAnalysis.CSharp;
127

138
namespace ReactiveUI.SourceGenerators.Extensions;
149

@@ -17,24 +12,6 @@ namespace ReactiveUI.SourceGenerators.Extensions;
1712
/// </summary>
1813
internal static class CompilationExtensions
1914
{
20-
/// <summary>
21-
/// Checks whether a given compilation (assumed to be for C#) is using at least a given language version.
22-
/// </summary>
23-
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
24-
/// <param name="languageVersion">The minimum language version to check.</param>
25-
/// <returns>Whether <paramref name="compilation"/> is using at least the specified language version.</returns>
26-
public static bool HasLanguageVersionAtLeastEqualTo(this Compilation compilation, LanguageVersion languageVersion) =>
27-
((CSharpCompilation)compilation).LanguageVersion >= languageVersion;
28-
29-
/// <summary>
30-
/// Checks whether a given compilation (assumed to be for C#) is using at least a given language version.
31-
/// </summary>
32-
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
33-
/// <param name="languageVersion">The minimum language version to check.</param>
34-
/// <returns>Whether <paramref name="compilation"/> is using at least the specified language version.</returns>
35-
public static bool HasLanguageVersionAtLeastEqualTo(this Compilation compilation, int languageVersion) =>
36-
((int)((CSharpCompilation)compilation).LanguageVersion) >= languageVersion;
37-
3815
/// <summary>
3916
/// <para>
4017
/// Checks whether or not a type with a specified metadata name is accessible from a given <see cref="Compilation"/> instance.
@@ -99,37 +76,4 @@ public static bool HasAccessibleTypeWithMetadataName(this Compilation compilatio
9976

10077
return false;
10178
}
102-
103-
/// <summary>
104-
/// Tries to build a map of <see cref="INamedTypeSymbol"/> instances form the input mapping of names.
105-
/// </summary>
106-
/// <typeparam name="T">The type of keys for each symbol.</typeparam>
107-
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
108-
/// <param name="typeNames">The input mapping of <typeparamref name="T"/> keys to fully qualified type names.</param>
109-
/// <param name="typeSymbols">The resulting mapping of <typeparamref name="T"/> keys to resolved <see cref="INamedTypeSymbol"/> instances.</param>
110-
/// <returns>Whether all requested <see cref="INamedTypeSymbol"/> instances could be resolved.</returns>
111-
public static bool TryBuildNamedTypeSymbolMap<T>(
112-
this Compilation compilation,
113-
IEnumerable<KeyValuePair<T, string>> typeNames,
114-
[NotNullWhen(true)] out ImmutableDictionary<T, INamedTypeSymbol>? typeSymbols)
115-
where T : IEquatable<T>
116-
{
117-
var builder = ImmutableDictionary.CreateBuilder<T, INamedTypeSymbol>();
118-
119-
foreach (var pair in typeNames)
120-
{
121-
if (compilation.GetTypeByMetadataName(pair.Value) is not INamedTypeSymbol attributeSymbol)
122-
{
123-
typeSymbols = null;
124-
125-
return false;
126-
}
127-
128-
builder.Add(pair.Key, attributeSymbol);
129-
}
130-
131-
typeSymbols = builder.ToImmutable();
132-
133-
return true;
134-
}
13579
}

src/ReactiveUI.SourceGenerators/Core/Extensions/DiagnosticsExtensions.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,4 @@ public static void Add(
3939
DiagnosticDescriptor descriptor,
4040
SyntaxNode node,
4141
params object[] args) => diagnostics.Add(DiagnosticInfo.Create(descriptor, node, args));
42-
43-
/// <summary>
44-
/// Registers an output node into an <see cref="IncrementalGeneratorInitializationContext"/> to output diagnostics.
45-
/// </summary>
46-
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> instance.</param>
47-
/// <param name="diagnostics">The input <see cref="IncrementalValuesProvider{TValues}"/> sequence of diagnostics.</param>
48-
public static void ReportDiagnostics(this in IncrementalGeneratorInitializationContext context, in IncrementalValuesProvider<DiagnosticInfo> diagnostics) =>
49-
context.RegisterSourceOutput(diagnostics, static (context, diagnostic) =>
50-
context.ReportDiagnostic(diagnostic.ToDiagnostic()));
51-
52-
/// <summary>
53-
/// Registers an output node into an <see cref="IncrementalGeneratorInitializationContext"/> to output diagnostics.
54-
/// </summary>
55-
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> instance.</param>
56-
/// <param name="diagnostics">The input <see cref="IncrementalValuesProvider{TValues}"/> sequence of diagnostics.</param>
57-
public static void ReportDiagnostics(this in IncrementalGeneratorInitializationContext context, in IncrementalValuesProvider<EquatableArray<DiagnosticInfo>> diagnostics) =>
58-
context.RegisterSourceOutput(diagnostics, static (context, diagnostics) =>
59-
{
60-
foreach (var diagnostic in diagnostics)
61-
{
62-
context.ReportDiagnostic(diagnostic.ToDiagnostic());
63-
}
64-
});
6542
}

src/ReactiveUI.SourceGenerators/Core/Extensions/INamedTypeSymbolExtensions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System;
77
using System.Collections.Generic;
8-
using System.Linq;
98
using Microsoft.CodeAnalysis;
109

1110
namespace ReactiveUI.SourceGenerators.Extensions;
@@ -72,32 +71,4 @@ public static string GetTypeString(this INamedTypeSymbol namedTypeSymbol)
7271

7372
throw new InvalidOperationException("Unknown type kind.");
7473
}
75-
76-
/// <summary>
77-
/// Checks if a given attribute can be applied to a specific target element type.
78-
/// </summary>
79-
/// <param name="attributeClass">The attribute class symbol.</param>
80-
/// <param name="target">The target element type (e.g., field, property).</param>
81-
/// <returns><c>true</c> if the attribute can be applied to the target; otherwise, <c>false</c>.</returns>
82-
public static bool AttributeCanTarget(this INamedTypeSymbol? attributeClass, AttributeTargets target)
83-
{
84-
if (attributeClass == null)
85-
{
86-
return false;
87-
}
88-
89-
// Look for an AttributeUsage attribute to determine the valid targets.
90-
var usageAttribute = attributeClass.GetAttributes()
91-
.FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString() == "System.AttributeUsageAttribute");
92-
93-
if (usageAttribute == null)
94-
{
95-
// If no AttributeUsage attribute is found, assume the attribute can be applied anywhere.
96-
return true;
97-
}
98-
99-
// Retrieve the valid targets from the AttributeUsage constructor arguments.
100-
var validTargets = (AttributeTargets)usageAttribute.ConstructorArguments[0].Value!;
101-
return validTargets.HasFlag(target);
102-
}
10374
}

src/ReactiveUI.SourceGenerators/Core/Extensions/ISymbolExtensions.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public static string GetFullyQualifiedName(this ISymbol symbol) =>
3030
public static string GetFullyQualifiedNameWithNullabilityAnnotations(this ISymbol symbol) =>
3131
symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier));
3232

33-
/// <summary>
34-
/// Checks whether or not a given type symbol has a specified full name.
35-
/// </summary>
36-
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
37-
/// <param name="name">The full name to check.</param>
38-
/// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns>
39-
public static bool HasFullyQualifiedName(this ISymbol symbol, string name) => symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == name;
40-
4133
/// <summary>
4234
/// Checks whether or not a given symbol has an attribute with the specified fully qualified metadata name.
4335
/// </summary>
@@ -63,7 +55,8 @@ public static bool HasAttributeWithFullyQualifiedMetadataName(this ISymbol symbo
6355
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
6456
/// <param name="typeSymbol">The <see cref="ITypeSymbol"/> instance for the attribute type to look for.</param>
6557
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified type.</returns>
66-
public static bool HasAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol) => TryGetAttributeWithType(symbol, typeSymbol, out _);
58+
public static bool HasAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol) =>
59+
TryGetAttributeWithType(symbol, typeSymbol, out _);
6760

6861
/// <summary>
6962
/// Tries to get an attribute with the specified type.
@@ -89,7 +82,6 @@ public static bool TryGetAttributeWithType(this ISymbol symbol, ITypeSymbol type
8982
return false;
9083
}
9184

92-
#if !ROSLYN_4_3_1_OR_GREATER
9385
/// <summary>
9486
/// Tries to get an attribute with the specified fully qualified metadata name.
9587
/// </summary>
@@ -113,7 +105,6 @@ public static bool TryGetAttributeWithFullyQualifiedMetadataName(this ISymbol sy
113105

114106
return false;
115107
}
116-
#endif
117108

118109
/// <summary>
119110
/// Calculates the effective accessibility for a given symbol.

0 commit comments

Comments
 (0)