mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using CliFx.Analyzers.ObjectModel;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
namespace CliFx.Analyzers
|
|
{
|
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
|
public class OptionMustHaveValidNameAnalyzer : AnalyzerBase
|
|
{
|
|
public OptionMustHaveValidNameAnalyzer()
|
|
: base(
|
|
"Options must have valid names",
|
|
"This option's name must be at least 2 characters long and must start with a letter.")
|
|
{
|
|
}
|
|
|
|
private void Analyze(SyntaxNodeAnalysisContext context)
|
|
{
|
|
if (context.Node is not PropertyDeclarationSyntax propertyDeclaration)
|
|
return;
|
|
|
|
var property = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration);
|
|
if (property is null)
|
|
return;
|
|
|
|
var option = CommandOptionSymbol.TryResolve(property);
|
|
if (option is null)
|
|
return;
|
|
|
|
if (string.IsNullOrWhiteSpace(option.Name))
|
|
return;
|
|
|
|
if (option.Name.Length < 2 || !char.IsLetter(option.Name[0]))
|
|
{
|
|
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.GetLocation()));
|
|
}
|
|
}
|
|
|
|
public override void Initialize(AnalysisContext context)
|
|
{
|
|
base.Initialize(context);
|
|
context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.PropertyDeclaration);
|
|
}
|
|
}
|
|
} |