mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
C#10ify
This commit is contained in:
@@ -3,80 +3,79 @@ using System.Linq;
|
||||
using CliFx.Analyzers.Utils.Extensions;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers.ObjectModel
|
||||
namespace CliFx.Analyzers.ObjectModel;
|
||||
|
||||
internal partial class CommandParameterSymbol
|
||||
{
|
||||
internal partial class CommandParameterSymbol
|
||||
public int Order { get; }
|
||||
|
||||
public string? Name { get; }
|
||||
|
||||
public ITypeSymbol? ConverterType { get; }
|
||||
|
||||
public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; }
|
||||
|
||||
public CommandParameterSymbol(
|
||||
int order,
|
||||
string? name,
|
||||
ITypeSymbol? converterType,
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes)
|
||||
{
|
||||
public int Order { get; }
|
||||
Order = order;
|
||||
Name = name;
|
||||
ConverterType = converterType;
|
||||
ValidatorTypes = validatorTypes;
|
||||
}
|
||||
}
|
||||
|
||||
public string? Name { get; }
|
||||
internal partial class CommandParameterSymbol
|
||||
{
|
||||
private static AttributeData? TryGetParameterAttribute(IPropertySymbol property) =>
|
||||
property
|
||||
.GetAttributes()
|
||||
.FirstOrDefault(a => a.AttributeClass.DisplayNameMatches(SymbolNames.CliFxCommandParameterAttribute));
|
||||
|
||||
public ITypeSymbol? ConverterType { get; }
|
||||
private static CommandParameterSymbol FromAttribute(AttributeData attribute)
|
||||
{
|
||||
var order = (int) attribute
|
||||
.ConstructorArguments
|
||||
.Select(a => a.Value)
|
||||
.First()!;
|
||||
|
||||
public IReadOnlyList<ITypeSymbol> ValidatorTypes { get; }
|
||||
var name = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Name")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
public CommandParameterSymbol(
|
||||
int order,
|
||||
string? name,
|
||||
ITypeSymbol? converterType,
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes)
|
||||
{
|
||||
Order = order;
|
||||
Name = name;
|
||||
ConverterType = converterType;
|
||||
ValidatorTypes = validatorTypes;
|
||||
}
|
||||
var converter = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Converter")
|
||||
.Select(a => a.Value.Value)
|
||||
.Cast<ITypeSymbol?>()
|
||||
.FirstOrDefault();
|
||||
|
||||
var validators = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Validators")
|
||||
.SelectMany(a => a.Value.Values)
|
||||
.Select(c => c.Value)
|
||||
.Cast<ITypeSymbol>()
|
||||
.ToArray();
|
||||
|
||||
return new CommandParameterSymbol(order, name, converter, validators);
|
||||
}
|
||||
|
||||
internal partial class CommandParameterSymbol
|
||||
public static CommandParameterSymbol? TryResolve(IPropertySymbol property)
|
||||
{
|
||||
private static AttributeData? TryGetParameterAttribute(IPropertySymbol property) =>
|
||||
property
|
||||
.GetAttributes()
|
||||
.FirstOrDefault(a => a.AttributeClass.DisplayNameMatches(SymbolNames.CliFxCommandParameterAttribute));
|
||||
var attribute = TryGetParameterAttribute(property);
|
||||
|
||||
private static CommandParameterSymbol FromAttribute(AttributeData attribute)
|
||||
{
|
||||
var order = (int) attribute
|
||||
.ConstructorArguments
|
||||
.Select(a => a.Value)
|
||||
.First()!;
|
||||
if (attribute is null)
|
||||
return null;
|
||||
|
||||
var name = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Name")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
var converter = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Converter")
|
||||
.Select(a => a.Value.Value)
|
||||
.Cast<ITypeSymbol?>()
|
||||
.FirstOrDefault();
|
||||
|
||||
var validators = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Validators")
|
||||
.SelectMany(a => a.Value.Values)
|
||||
.Select(c => c.Value)
|
||||
.Cast<ITypeSymbol>()
|
||||
.ToArray();
|
||||
|
||||
return new CommandParameterSymbol(order, name, converter, validators);
|
||||
}
|
||||
|
||||
public static CommandParameterSymbol? TryResolve(IPropertySymbol property)
|
||||
{
|
||||
var attribute = TryGetParameterAttribute(property);
|
||||
|
||||
if (attribute is null)
|
||||
return null;
|
||||
|
||||
return FromAttribute(attribute);
|
||||
}
|
||||
|
||||
public static bool IsParameterProperty(IPropertySymbol property) =>
|
||||
TryGetParameterAttribute(property) is not null;
|
||||
return FromAttribute(attribute);
|
||||
}
|
||||
|
||||
public static bool IsParameterProperty(IPropertySymbol property) =>
|
||||
TryGetParameterAttribute(property) is not null;
|
||||
}
|
||||
Reference in New Issue
Block a user