Skip to content

Commit 0fb8a62

Browse files
committed
Minor code refactoring
1 parent ab6d300 commit 0fb8a62

File tree

3 files changed

+96
-64
lines changed

3 files changed

+96
-64
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected override bool ValidateTargetType(INamedTypeSymbol typeSymbol, Observab
8282
// In order to use [ObservableRecipient], the target type needs to inherit from ObservableObject,
8383
// or be annotated with [ObservableObject] or [INotifyPropertyChanged] (with additional helpers).
8484
if (!typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject") &&
85-
!typeSymbol.HasOrInheritsAttribute(static a => a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") == true) &&
85+
!typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") &&
8686
!typeSymbol.HasOrInheritsAttribute(static a =>
8787
a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
8888
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))

CommunityToolkit.Mvvm.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Linq;
75
using System.Text;
86
using Microsoft.CodeAnalysis;
97

@@ -41,65 +39,4 @@ static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
4139
// those characters at the moment, see: https://github.com/dotnet/roslyn/issues/58476.
4240
return BuildFrom(symbol, new StringBuilder(256)).ToString().Replace('`', '-').Replace('+', '.');
4341
}
44-
45-
/// <summary>
46-
/// Checks whether or not a given <see cref="INamedTypeSymbol"/> inherits from a specified type.
47-
/// </summary>
48-
/// <param name="typeSymbol">The target <see cref="INamedTypeSymbol"/> instance to check.</param>
49-
/// <param name="name">The full name of the type to check for inheritance.</param>
50-
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
51-
public static bool InheritsFromFullyQualifiedName(this INamedTypeSymbol typeSymbol, string name)
52-
{
53-
INamedTypeSymbol? baseType = typeSymbol.BaseType;
54-
55-
while (baseType != null)
56-
{
57-
if (baseType.HasFullyQualifiedName(name))
58-
{
59-
return true;
60-
}
61-
62-
baseType = baseType.BaseType;
63-
}
64-
65-
return false;
66-
}
67-
68-
/// <summary>
69-
/// Checks whether or not a given <see cref="INamedTypeSymbol"/> implements an interface with a specied name.
70-
/// </summary>
71-
/// <param name="typeSymbol">The target <see cref="INamedTypeSymbol"/> instance to check.</param>
72-
/// <param name="name">The full name of the type to check for interface implementation.</param>
73-
/// <returns>Whether or not <paramref name="typeSymbol"/> has an interface with the specified name.</returns>
74-
public static bool HasInterfaceWithFullyQualifiedName(this INamedTypeSymbol typeSymbol, string name)
75-
{
76-
foreach (INamedTypeSymbol interfaceType in typeSymbol.AllInterfaces)
77-
{
78-
if (interfaceType.HasFullyQualifiedName(name))
79-
{
80-
return true;
81-
}
82-
}
83-
84-
return false;
85-
}
86-
87-
/// <summary>
88-
/// Checks whether or not a given <see cref="INamedTypeSymbol"/> has or inherits a specified attribute.
89-
/// </summary>
90-
/// <param name="typeSymbol">The target <see cref="INamedTypeSymbol"/> instance to check.</param>
91-
/// <param name="predicate">The predicate used to match available attributes.</param>
92-
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute matching <paramref name="predicate"/>.</returns>
93-
public static bool HasOrInheritsAttribute(this INamedTypeSymbol typeSymbol, Func<AttributeData, bool> predicate)
94-
{
95-
for (INamedTypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
96-
{
97-
if (currentType.GetAttributes().Any(predicate))
98-
{
99-
return true;
100-
}
101-
}
102-
103-
return false;
104-
}
10542
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Linq;
7+
using Microsoft.CodeAnalysis;
8+
9+
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
10+
11+
/// <summary>
12+
/// Extension methods for the <see cref="ITypeSymbol"/> type.
13+
/// </summary>
14+
internal static class ITypeSymbolExtensions
15+
{
16+
/// <summary>
17+
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
18+
/// </summary>
19+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
20+
/// <param name="name">The full name of the type to check for inheritance.</param>
21+
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
22+
public static bool InheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
23+
{
24+
INamedTypeSymbol? baseType = typeSymbol.BaseType;
25+
26+
while (baseType != null)
27+
{
28+
if (baseType.HasFullyQualifiedName(name))
29+
{
30+
return true;
31+
}
32+
33+
baseType = baseType.BaseType;
34+
}
35+
36+
return false;
37+
}
38+
39+
/// <summary>
40+
/// Checks whether or not a given <see cref="ITypeSymbol"/> implements an interface with a specied name.
41+
/// </summary>
42+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
43+
/// <param name="name">The full name of the type to check for interface implementation.</param>
44+
/// <returns>Whether or not <paramref name="typeSymbol"/> has an interface with the specified name.</returns>
45+
public static bool HasInterfaceWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
46+
{
47+
foreach (INamedTypeSymbol interfaceType in typeSymbol.AllInterfaces)
48+
{
49+
if (interfaceType.HasFullyQualifiedName(name))
50+
{
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
}
57+
58+
/// <summary>
59+
/// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits a specified attribute.
60+
/// </summary>
61+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
62+
/// <param name="predicate">The predicate used to match available attributes.</param>
63+
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute matching <paramref name="predicate"/>.</returns>
64+
public static bool HasOrInheritsAttribute(this ITypeSymbol typeSymbol, Func<AttributeData, bool> predicate)
65+
{
66+
for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
67+
{
68+
if (currentType.GetAttributes().Any(predicate))
69+
{
70+
return true;
71+
}
72+
}
73+
74+
return false;
75+
}
76+
77+
/// <summary>
78+
/// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits a specified attribute.
79+
/// </summary>
80+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
81+
/// <param name="name">The name of the attribute to look for.</param>
82+
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute with the specified type name.</returns>
83+
public static bool HasOrInheritsAttributeWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
84+
{
85+
for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
86+
{
87+
if (currentType.HasAttributeWithFullyQualifiedName(name))
88+
{
89+
return true;
90+
}
91+
}
92+
93+
return false;
94+
}
95+
}

0 commit comments

Comments
 (0)