Skip to content

Commit

Permalink
Improve coverage for diagnostics reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Oct 5, 2024
1 parent 360e686 commit 573f723
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 75 deletions.
16 changes: 8 additions & 8 deletions SpreadCheetah.SourceGenerator/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public static Diagnostic InvalidColumnHeaderPropertyReference(Location? location
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static Diagnostic UnsupportedTypeForAttribute(Location? location, string attributeName, string typeFullName)
=> Diagnostic.Create(UnsupportedTypeForAttributeDescriptor, location, [attributeName, typeFullName]);
public static Diagnostic UnsupportedTypeForAttribute(Location? location, string? attributeName, string typeFullName)
=> Diagnostic.Create(UnsupportedTypeForAttributeDescriptor, location, [attributeName ?? "Attribute", typeFullName]);

private static readonly DiagnosticDescriptor UnsupportedTypeForAttributeDescriptor = new(
id: "SPCH1005",
Expand All @@ -75,8 +75,8 @@ public static Diagnostic UnsupportedTypeForAttribute(Location? location, string
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static Diagnostic InvalidAttributeArgument(Location? location, string attributeName)
=> Diagnostic.Create(InvalidAttributeArgumentDescriptor, location, [attributeName]);
public static Diagnostic InvalidAttributeArgument(Location? location, string? attributeName)
=> Diagnostic.Create(InvalidAttributeArgumentDescriptor, location, [attributeName ?? "attribute"]);

private static readonly DiagnosticDescriptor InvalidAttributeArgumentDescriptor = new(
id: "SPCH1006",
Expand All @@ -86,8 +86,8 @@ public static Diagnostic InvalidAttributeArgument(Location? location, string att
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static Diagnostic AttributeTypeArgumentMustInherit(Location? location, string typeName, string attributeName, string baseClassName)
=> Diagnostic.Create(AttributeTypeArgumentMustInheritDescriptor, location, [typeName, attributeName, baseClassName]);
public static Diagnostic AttributeTypeArgumentMustInherit(Location? location, string typeName, string? attributeName, string baseClassName)
=> Diagnostic.Create(AttributeTypeArgumentMustInheritDescriptor, location, [typeName, attributeName ?? "attribute", baseClassName]);

private static readonly DiagnosticDescriptor AttributeTypeArgumentMustInheritDescriptor = new(
id: "SPCH1007",
Expand All @@ -97,8 +97,8 @@ public static Diagnostic AttributeTypeArgumentMustInherit(Location? location, st
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static Diagnostic AttributeCombinationNotSupported(Location? location, string attribute1, string attribute2)
=> Diagnostic.Create(AttributeCombinationNotSupportedDescriptor, location, [attribute1, attribute2]);
public static Diagnostic AttributeCombinationNotSupported(Location? location, string? attribute1, string attribute2)
=> Diagnostic.Create(AttributeCombinationNotSupportedDescriptor, location, [attribute1 ?? "attribute", attribute2]);

private static readonly DiagnosticDescriptor AttributeCombinationNotSupportedDescriptor = new(
id: "SPCH1008",
Expand Down
93 changes: 29 additions & 64 deletions SpreadCheetah.SourceGenerator/Helpers/DiagnosticsReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,116 +8,81 @@ internal sealed class DiagnosticsReporter(SymbolAnalysisContext context) : IDiag
{
public void ReportAttributeCombinationNotSupported(AttributeData attribute, string otherAttribute, CancellationToken token)
{
if (attribute.ApplicationSyntaxReference?.GetSyntax(token) is not AttributeSyntax attributeSyntax)
return;
if (attribute.AttributeClass?.Name is not { } name)
return;

var diagnostic = Diagnostics.AttributeCombinationNotSupported(attributeSyntax.GetLocation(), name, otherAttribute);
var attributeSyntax = attribute.ApplicationSyntaxReference?.GetSyntax(token);
var name = attribute.AttributeClass?.Name;
var diagnostic = Diagnostics.AttributeCombinationNotSupported(attributeSyntax?.GetLocation(), name, otherAttribute);
context.ReportDiagnostic(diagnostic);
}

public void ReportDuplicateColumnOrdering(AttributeData attribute, CancellationToken token)
{
if (attribute.ApplicationSyntaxReference?.GetSyntax(token) is not AttributeSyntax attributeSyntax)
return;

var diagnostic = Diagnostics.DuplicateColumnOrder(attributeSyntax.GetLocation());
var attributeSyntax = attribute.ApplicationSyntaxReference?.GetSyntax(token);
var diagnostic = Diagnostics.DuplicateColumnOrder(attributeSyntax?.GetLocation());
context.ReportDiagnostic(diagnostic);
}

public void ReportInvalidArgument(AttributeData attribute, CancellationToken token)
{
if (!TryGetArgument(attribute, token, out var arg) || arg is null)
return;
if (attribute.AttributeClass?.Name is not { } name)
return;

var diagnostic = Diagnostics.InvalidAttributeArgument(arg.GetLocation(), name);
var location = GetArgument(attribute, token)?.GetLocation();
var name = attribute.AttributeClass?.Name;
var diagnostic = Diagnostics.InvalidAttributeArgument(location, name);
context.ReportDiagnostic(diagnostic);
}

public void ReportInvalidPropertyReference(AttributeData attribute, string propertyName, string typeFullName, CancellationToken token)
{
if (!TryGetArgumentList(attribute, token, out var argList) || argList is null)
return;

var diagnostic = Diagnostics.InvalidColumnHeaderPropertyReference(argList.GetLocation(), propertyName, typeFullName);
var argList = GetArgumentList(attribute, token);
var diagnostic = Diagnostics.InvalidColumnHeaderPropertyReference(argList?.GetLocation(), propertyName, typeFullName);
context.ReportDiagnostic(diagnostic);
}

public void ReportNoPropertiesFound(AttributeData attribute, INamedTypeSymbol rowType, CancellationToken token)
{
if (!TryGetArgument(attribute, token, out var arg) || arg is null)
return;

var diagnostic = Diagnostics.NoPropertiesFound(arg.GetLocation(), rowType.Name);
var location = GetArgument(attribute, token)?.GetLocation();
var diagnostic = Diagnostics.NoPropertiesFound(location, rowType.Name);
context.ReportDiagnostic(diagnostic);
}

public void ReportUnsupportedPropertyType(AttributeData attribute, INamedTypeSymbol rowType, IPropertySymbol property, CancellationToken token)
{
if (!TryGetArgument(attribute, token, out var arg) || arg is null)
return;

var diagnostic = Diagnostics.UnsupportedTypeForCellValue(arg.GetLocation(), rowType.Name, property.Name, property.Type.Name);
var location = GetArgument(attribute, token)?.GetLocation();
var diagnostic = Diagnostics.UnsupportedTypeForCellValue(location, rowType.Name, property.Name, property.Type.Name);
context.ReportDiagnostic(diagnostic);
}

public void ReportUnsupportedPropertyTypeForAttribute(AttributeData attribute, ITypeSymbol propertyType, CancellationToken token)
{
if (attribute.ApplicationSyntaxReference?.GetSyntax(token) is not AttributeSyntax attributeSyntax)
return;
if (attribute.AttributeClass?.Name is not { } name)
return;

var diagnostic = Diagnostics.UnsupportedTypeForAttribute(attributeSyntax.GetLocation(), name, propertyType.ToDisplayString());
var syntaxNode = attribute.ApplicationSyntaxReference?.GetSyntax(token);
var name = attribute.AttributeClass?.Name;
var diagnostic = Diagnostics.UnsupportedTypeForAttribute(syntaxNode?.GetLocation(), name, propertyType.ToDisplayString());
context.ReportDiagnostic(diagnostic);
}

public void ReportTypeMustHaveDefaultConstructor(AttributeData attribute, string typeName, CancellationToken token)
{
if (!TryGetArgument(attribute, token, out var arg) || arg is null)
return;

var diagnostic = Diagnostics.AttributeTypeArgumentMustHaveDefaultConstructor(arg.GetLocation(), typeName);
var location = GetArgument(attribute, token)?.GetLocation();
var diagnostic = Diagnostics.AttributeTypeArgumentMustHaveDefaultConstructor(location, typeName);
context.ReportDiagnostic(diagnostic);
}

public void ReportTypeMustInherit(AttributeData attribute, string typeName, string baseClassName, CancellationToken token)
{
if (!TryGetArgument(attribute, token, out var arg) || arg is null)
return;
if (attribute.AttributeClass?.Name is not { } name)
return;

var diagnostic = Diagnostics.AttributeTypeArgumentMustInherit(arg.GetLocation(), typeName, name, baseClassName);
var location = GetArgument(attribute, token)?.GetLocation();
var name = attribute.AttributeClass?.Name;
var diagnostic = Diagnostics.AttributeTypeArgumentMustInherit(location, typeName, name, baseClassName);
context.ReportDiagnostic(diagnostic);
}

private static bool TryGetArgumentList(AttributeData attribute, CancellationToken token, out AttributeArgumentListSyntax? argumentList)
private static AttributeArgumentListSyntax? GetArgumentList(AttributeData attribute, CancellationToken token)
{
argumentList = null;

if (attribute.ApplicationSyntaxReference?.GetSyntax(token) is not AttributeSyntax attributeSyntax)
return false;
if (attributeSyntax.ArgumentList is not { } argList)
return false;

argumentList = argList;
return true;
var syntaxNode = attribute.ApplicationSyntaxReference?.GetSyntax(token);
var attributeSyntax = syntaxNode as AttributeSyntax;
return attributeSyntax?.ArgumentList;
}

private static bool TryGetArgument(AttributeData attribute, CancellationToken token, out AttributeArgumentSyntax? argument)
private static AttributeArgumentSyntax? GetArgument(AttributeData attribute, CancellationToken token)
{
argument = null;

if (!TryGetArgumentList(attribute, token, out var argList))
return false;
if (argList?.Arguments.FirstOrDefault() is not { } arg)
return false;

argument = arg;
return true;
var argList = GetArgumentList(attribute, token);
return argList?.Arguments.FirstOrDefault();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;

namespace SpreadCheetah.SourceGenerator.Helpers;

[ExcludeFromCodeCoverage]
internal sealed class NullDiagnosticsReporter : IDiagnosticsReporter
{
public static readonly NullDiagnosticsReporter Instance = new();
Expand Down
5 changes: 2 additions & 3 deletions SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ private static void AnalyzeRowType(SymbolAnalysisContext context)
{
if (context.Symbol is not INamedTypeSymbol type)
return;
if (type.GetMembers() is { Length: 0 } members)
return;

var typeHasColumnOrderAttribute = members
var typeHasColumnOrderAttribute = type
.GetMembers()
.OfType<IPropertySymbol>()
.Any(x => x.GetColumnOrderAttribute() is not null);

Expand Down

0 comments on commit 573f723

Please sign in to comment.