mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor (#94)
This commit is contained in:
53
CliFx.Analyzers/OptionMustBeInsideCommandAnalyzer.cs
Normal file
53
CliFx.Analyzers/OptionMustBeInsideCommandAnalyzer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Linq;
|
||||
using CliFx.Analyzers.ObjectModel;
|
||||
using CliFx.Analyzers.Utils.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class OptionMustBeInsideCommandAnalyzer : AnalyzerBase
|
||||
{
|
||||
public OptionMustBeInsideCommandAnalyzer()
|
||||
: base(
|
||||
"Options must be defined inside commands",
|
||||
$"This option must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`.")
|
||||
{
|
||||
}
|
||||
|
||||
private void Analyze(SyntaxNodeAnalysisContext context)
|
||||
{
|
||||
if (context.Node is not PropertyDeclarationSyntax propertyDeclaration)
|
||||
return;
|
||||
|
||||
var property = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration);
|
||||
if (property is null)
|
||||
return;
|
||||
|
||||
if (property.ContainingType.IsAbstract)
|
||||
return;
|
||||
|
||||
if (!CommandOptionSymbol.IsOptionProperty(property))
|
||||
return;
|
||||
|
||||
var isInsideCommand = property
|
||||
.ContainingType
|
||||
.AllInterfaces
|
||||
.Any(s => s.DisplayNameMatches(SymbolNames.CliFxCommandInterface));
|
||||
|
||||
if (!isInsideCommand)
|
||||
{
|
||||
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.GetLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
base.Initialize(context);
|
||||
context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.PropertyDeclaration);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user