mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using System.Linq;
|
|
using CliFx.Analyzers.ObjectModel;
|
|
using CliFx.Analyzers.Utils.Extensions;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
namespace CliFx.Analyzers;
|
|
|
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
|
public class CommandMustBeAnnotatedAnalyzer()
|
|
: AnalyzerBase(
|
|
$"Commands must be annotated with `{SymbolNames.CliFxCommandAttribute}`",
|
|
$"This type must be annotated with `{SymbolNames.CliFxCommandAttribute}` in order to be a valid command."
|
|
)
|
|
{
|
|
private void Analyze(
|
|
SyntaxNodeAnalysisContext context,
|
|
ClassDeclarationSyntax classDeclaration,
|
|
ITypeSymbol type
|
|
)
|
|
{
|
|
// Ignore abstract classes, because they may be used to define
|
|
// base implementations for commands, in which case the command
|
|
// attribute doesn't make sense.
|
|
if (type.IsAbstract)
|
|
return;
|
|
|
|
var implementsCommandInterface = type.AllInterfaces.Any(i =>
|
|
i.DisplayNameMatches(SymbolNames.CliFxCommandInterface)
|
|
);
|
|
|
|
var hasCommandAttribute = type.GetAttributes()
|
|
.Select(a => a.AttributeClass)
|
|
.Any(c => c.DisplayNameMatches(SymbolNames.CliFxCommandAttribute));
|
|
|
|
// If the interface is implemented, but the attribute is missing,
|
|
// then it's very likely a user error.
|
|
if (implementsCommandInterface && !hasCommandAttribute)
|
|
{
|
|
context.ReportDiagnostic(CreateDiagnostic(classDeclaration.Identifier.GetLocation()));
|
|
}
|
|
}
|
|
|
|
public override void Initialize(AnalysisContext context)
|
|
{
|
|
base.Initialize(context);
|
|
context.HandleClassDeclaration(Analyze);
|
|
}
|
|
}
|