Skip to content

Commit

Permalink
Merge pull request #82 from sveinungf/dev/srcgen-columnignore-diagnostic
Browse files Browse the repository at this point in the history
Don't emit SPCH1002 if property has ColumnIgnore attribute
  • Loading branch information
sveinungf authored Nov 10, 2024
2 parents 34d5af7 + c9237fc commit bb12192
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,32 @@ public partial class MyGenRowContext : WorksheetRowContext;
// Act & Assert
return context.RunAsync();
}

[Fact]
public Task ColumnIgnore_IgnorePropertyOfUnsupportedType()
{
// Arrange
var context = AnalyzerTest.CreateContext();
context.TestCode = """
using SpreadCheetah.SourceGeneration;
using System;
using System.Diagnostics;
namespace MyNamespace;
public class ClassWithUnsupportedProperty
{
[ColumnIgnore] // SPCH1002 without this attribute
public Stopwatch? Stopwatch { get; set; }
public int Value { get; set; }
}
[WorksheetRow(typeof(ClassWithUnsupportedProperty))]
public partial class MyGenRowContext : WorksheetRowContext;
""";

// Act & Assert
return context.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public Task PropertyType_ClassWithUnsupportedProperty()
namespace MyNamespace;
public record RecordWithUnsupportedProperty(Uri HomepageUri);
public class ClassWithUnsupportedProperty
{
public System.Diagnostics.Stopwatch? Stopwatch { get; set; }
}
[WorksheetRow({|SPCH1002:typeof(RecordWithUnsupportedProperty)|})]
[WorksheetRow({|SPCH1002:typeof(ClassWithUnsupportedProperty)|})]
public partial class MyGenRowContext : WorksheetRowContext;
""";

Expand All @@ -36,10 +39,13 @@ public Task PropertyType_ClassWithUnsupportedPropertyAndWarningsSuppressed()
using System;
namespace MyNamespace;
public record RecordWithUnsupportedProperty(Uri HomepageUri);
[WorksheetRow(typeof(RecordWithUnsupportedProperty))]
public class ClassWithUnsupportedProperty
{
public System.Diagnostics.Stopwatch? Stopwatch { get; set; }
}
[WorksheetRow(typeof(ClassWithUnsupportedProperty))]
[WorksheetRowGenerationOptions(SuppressWarnings = true)]
public partial class MyGenRowContext : WorksheetRowContext;
""";
Expand Down
9 changes: 7 additions & 2 deletions SpreadCheetah.SourceGenerator/Extensions/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ private static bool TryGetInheritedColumnOrderingAttribute(this AttributeData at
return true;
}

public static AttributeData? GetCellValueConverterAttribute(this IPropertySymbol property)
public static bool HasCellValueConverterAttribute(this IPropertySymbol property)
{
return property.GetAttribute(Attributes.CellValueConverter);
return property.GetAttribute(Attributes.CellValueConverter) is not null;
}

public static bool HasColumnIgnoreAttribute(this IPropertySymbol property)
{
return property.GetAttribute(Attributes.ColumnIgnore) is not null;
}

public static AttributeData? GetColumnOrderAttribute(this IPropertySymbol property)
Expand Down
5 changes: 3 additions & 2 deletions SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ private static void AnalyzeContextType(SymbolAnalysisContext context)
{
hasProperties = true;

var cellValueConverter = property.GetCellValueConverterAttribute();
if (cellValueConverter is not null)
if (property.HasColumnIgnoreAttribute())
continue;
if (property.HasCellValueConverterAttribute())
continue;
if (property.Type.IsSupportedType())
continue;
Expand Down

0 comments on commit bb12192

Please sign in to comment.