-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTypeDeclarationSyntaxExtensions.cs
46 lines (41 loc) · 1.67 KB
/
TypeDeclarationSyntaxExtensions.cs
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
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Architect.DomainModeling.Generator;
/// <summary>
/// Provides extensions on <see cref="TypeDeclarationSyntax"/>.
/// </summary>
internal static class TypeDeclarationSyntaxExtensions
{
/// <summary>
/// Returns whether the <see cref="TypeDeclarationSyntax"/> is a nested type.
/// </summary>
public static bool IsNested(this TypeDeclarationSyntax typeDeclarationSyntax)
{
var result = typeDeclarationSyntax.Parent is not BaseNamespaceDeclarationSyntax;
return result;
}
/// <summary>
/// Returns whether the <see cref="TypeDeclarationSyntax"/> has any attributes.
/// </summary>
public static bool HasAttributes(this TypeDeclarationSyntax typeDeclarationSyntax)
{
var result = typeDeclarationSyntax.AttributeLists.Count > 0;
return result;
}
/// <summary>
/// <para>
/// Returns whether the <see cref="TypeDeclarationSyntax"/> is directly annotated with an attribute whose name starts with the given prefix.
/// </para>
/// <para>
/// Prefixes are useful because a developer may type either "[Obsolete]" or "[ObsoleteAttribute]".
/// </para>
/// </summary>
public static bool HasAttributeWithPrefix(this TypeDeclarationSyntax typeDeclarationSyntax, string namePrefix)
{
foreach (var attributeList in typeDeclarationSyntax.AttributeLists)
foreach (var attribute in attributeList.Attributes)
if ((attribute.Name is IdentifierNameSyntax identifierName && identifierName.Identifier.ValueText.StartsWith(namePrefix)) ||
(attribute.Name is GenericNameSyntax genericName && genericName.Identifier.ValueText.StartsWith(namePrefix)))
return true;
return false;
}
}