Use CSharpier

This commit is contained in:
Tyrrrz
2023-08-22 21:20:04 +03:00
parent a4726fcefd
commit 21b601da66
99 changed files with 975 additions and 1163 deletions

View File

@@ -29,56 +29,72 @@ internal static class RoslynExtensions
}
}
public static ITypeSymbol? TryGetEnumerableUnderlyingType(this ITypeSymbol type) => type
.AllInterfaces
.FirstOrDefault(i => i.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T)?
.TypeArguments[0];
public static ITypeSymbol? TryGetEnumerableUnderlyingType(this ITypeSymbol type) =>
type.AllInterfaces
.FirstOrDefault(
i =>
i.ConstructedFrom.SpecialType
== SpecialType.System_Collections_Generic_IEnumerable_T
)
?.TypeArguments[0];
// Detect if the property is required through roundabout means so as to not have to take dependency
// on higher versions of the C# compiler.
public static bool IsRequired(this IPropertySymbol property) => property
public static bool IsRequired(this IPropertySymbol property) =>
property
// Can't rely on the RequiredMemberAttribute because it's generated by the compiler, not added by the user,
// so we have to check for the presence of the `required` modifier in the syntax tree instead.
.DeclaringSyntaxReferences
.Select(r => r.GetSyntax())
.OfType<PropertyDeclarationSyntax>()
.SelectMany(p => p.Modifiers)
.Any(m => m.IsKind((SyntaxKind)8447));
.Select(r => r.GetSyntax())
.OfType<PropertyDeclarationSyntax>()
.SelectMany(p => p.Modifiers)
.Any(m => m.IsKind((SyntaxKind)8447));
public static bool IsAssignable(this Compilation compilation, ITypeSymbol source, ITypeSymbol destination) =>
compilation.ClassifyConversion(source, destination).Exists;
public static bool IsAssignable(
this Compilation compilation,
ITypeSymbol source,
ITypeSymbol destination
) => compilation.ClassifyConversion(source, destination).Exists;
public static void HandleClassDeclaration(
this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze)
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze
)
{
analysisContext.RegisterSyntaxNodeAction(ctx =>
{
if (ctx.Node is not ClassDeclarationSyntax classDeclaration)
return;
analysisContext.RegisterSyntaxNodeAction(
ctx =>
{
if (ctx.Node is not ClassDeclarationSyntax classDeclaration)
return;
var type = ctx.SemanticModel.GetDeclaredSymbol(classDeclaration);
if (type is null)
return;
var type = ctx.SemanticModel.GetDeclaredSymbol(classDeclaration);
if (type is null)
return;
analyze(ctx, classDeclaration, type);
}, SyntaxKind.ClassDeclaration);
analyze(ctx, classDeclaration, type);
},
SyntaxKind.ClassDeclaration
);
}
public static void HandlePropertyDeclaration(
this AnalysisContext analysisContext,
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze)
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze
)
{
analysisContext.RegisterSyntaxNodeAction(ctx =>
{
if (ctx.Node is not PropertyDeclarationSyntax propertyDeclaration)
return;
analysisContext.RegisterSyntaxNodeAction(
ctx =>
{
if (ctx.Node is not PropertyDeclarationSyntax propertyDeclaration)
return;
var property = ctx.SemanticModel.GetDeclaredSymbol(propertyDeclaration);
if (property is null)
return;
var property = ctx.SemanticModel.GetDeclaredSymbol(propertyDeclaration);
if (property is null)
return;
analyze(ctx, propertyDeclaration, property);
}, SyntaxKind.PropertyDeclaration);
analyze(ctx, propertyDeclaration, property);
},
SyntaxKind.PropertyDeclaration
);
}
}
}