mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Produce analyzer errors for invalid generic arguments in converters and validators
Closes #103
This commit is contained in:
@@ -13,7 +13,7 @@ public class OptionMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveValidConverterAnalyzer()
|
||||
: base(
|
||||
$"Option converters must derive from `{SymbolNames.CliFxBindingConverterClass}`",
|
||||
$"Converter specified for this option must derive from `{SymbolNames.CliFxBindingConverterClass}`.")
|
||||
$"Converter specified for this option must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`.")
|
||||
{
|
||||
}
|
||||
|
||||
@@ -29,13 +29,15 @@ public class OptionMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
if (option.ConverterType is null)
|
||||
return;
|
||||
|
||||
// We check against an internal interface because checking against a generic class is a pain
|
||||
var converterImplementsInterface = option
|
||||
var converterValueType = option
|
||||
.ConverterType
|
||||
.AllInterfaces
|
||||
.Any(s => s.DisplayNameMatches(SymbolNames.CliFxBindingConverterInterface));
|
||||
.GetBaseTypes()
|
||||
.FirstOrDefault(t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingConverterClass))?
|
||||
.TypeArguments
|
||||
.FirstOrDefault();
|
||||
|
||||
if (!converterImplementsInterface)
|
||||
// Value returned by the converter must be assignable to the property type
|
||||
if (converterValueType is null || !property.Type.IsAssignableFrom(converterValueType))
|
||||
{
|
||||
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.GetLocation()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user