From c9237fcf62c7a9e3ee5b7e0c42152a4b8a5d1165 Mon Sep 17 00:00:00 2001 From: sveinungf Date: Sun, 10 Nov 2024 08:53:38 +0100 Subject: [PATCH] Don't emit SPCH1002 if property has ColumnIgnore attribute --- .../Tests/ColumnIgnoreTests.cs | 28 +++++++++++++++++++ .../Tests/PropertyTypeTests.cs | 16 +++++++---- .../Extensions/SymbolExtensions.cs | 9 ++++-- .../WorksheetRowAnalyzer.cs | 5 ++-- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/ColumnIgnoreTests.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/ColumnIgnoreTests.cs index 93427cb2..57f4e5e8 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/ColumnIgnoreTests.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/ColumnIgnoreTests.cs @@ -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(); + } } diff --git a/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/PropertyTypeTests.cs b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/PropertyTypeTests.cs index ecde1ce3..b4590f44 100644 --- a/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/PropertyTypeTests.cs +++ b/SpreadCheetah.SourceGenerator.SnapshotTest/Tests/PropertyTypeTests.cs @@ -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; """; @@ -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; """; diff --git a/SpreadCheetah.SourceGenerator/Extensions/SymbolExtensions.cs b/SpreadCheetah.SourceGenerator/Extensions/SymbolExtensions.cs index 345e7080..77a2b2f5 100644 --- a/SpreadCheetah.SourceGenerator/Extensions/SymbolExtensions.cs +++ b/SpreadCheetah.SourceGenerator/Extensions/SymbolExtensions.cs @@ -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) diff --git a/SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs b/SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs index 85663f65..3aa14845 100644 --- a/SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs +++ b/SpreadCheetah.SourceGenerator/WorksheetRowAnalyzer.cs @@ -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;