From a288a26a1fe59895824fcb622a58ed71a3c1fc6a Mon Sep 17 00:00:00 2001 From: Thomas Rydeen Skyldahl Date: Thu, 13 Apr 2023 22:27:01 +0200 Subject: [PATCH 1/2] fixed issue with VoFilter not matching the generic ValueObjectAttribute --- src/Vogen/VoFilter.cs | 5 +++-- .../AnalyzerTests/DoNotUseNewAnalyzerTests.cs | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Vogen/VoFilter.cs b/src/Vogen/VoFilter.cs index bd7fd2fec7..8711dd4a53 100644 --- a/src/Vogen/VoFilter.cs +++ b/src/Vogen/VoFilter.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis; @@ -68,7 +69,7 @@ public static bool IsTarget(INamedTypeSymbol? voClass) } AttributeData? voAttribute = - attributes.SingleOrDefault(a => a.AttributeClass?.FullName() is "Vogen.ValueObjectAttribute"); + attributes.SingleOrDefault(a => a.AttributeClass?.FullName()?.StartsWith("Vogen.ValueObjectAttribute", StringComparison.Ordinal) == true); return voAttribute is not null; } diff --git a/tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs b/tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs index fce1f89280..a13c2b9366 100644 --- a/tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs +++ b/tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs @@ -33,7 +33,29 @@ public async Task NoDiagnosticsForEmptyCode() var test = @""; await VerifyCS.VerifyAnalyzerAsync(test); } +#if NET7_0_OR_GREATER + [Theory(DisplayName = "Bug https://github.com/SteveDunn/Vogen/issues/389")] + [ClassData(typeof(Types))] + public async Task Disallow_new_for_creating_value_objects_using_generic_attribute(string type) + { + var source = $@"using Vogen; +namespace Whatever; +[ValueObject()] +public {type} MyVo {{ }} + +public class Test {{ + public Test() {{ + var c = {{|#0:new MyVo()|}}; + MyVo c2 = {{|#1:new()|}}; + }} +}} +"; + await Run( + source, + WithDiagnostics("VOG010", DiagnosticSeverity.Error, "MyVo", 0, 1)); + } +#endif [Theory] [ClassData(typeof(Types))] public async Task Disallow_new_for_creating_value_objects(string type) From 0ef29dbc2767b7a122d7546bf2ee49eee95bf625 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Fri, 14 Apr 2023 06:24:32 +0100 Subject: [PATCH 2/2] IsTarget now looks at the complete chain of attributes --- src/Vogen/VoFilter.cs | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/Vogen/VoFilter.cs b/src/Vogen/VoFilter.cs index 8711dd4a53..5ed24f230c 100644 --- a/src/Vogen/VoFilter.cs +++ b/src/Vogen/VoFilter.cs @@ -54,23 +54,6 @@ public static IEnumerable TryGetValueObjectAttributes(INamedTypeS return null; } - public static bool IsTarget(INamedTypeSymbol? voClass) - { - if (voClass == null) - { - return false; - } - - ImmutableArray attributes = voClass.GetAttributes(); - - if (attributes.Length == 0) - { - return false; - } - - AttributeData? voAttribute = - attributes.SingleOrDefault(a => a.AttributeClass?.FullName()?.StartsWith("Vogen.ValueObjectAttribute", StringComparison.Ordinal) == true); - - return voAttribute is not null; - } + public static bool IsTarget(INamedTypeSymbol? voClass) => + voClass is not null && TryGetValueObjectAttributes(voClass).Any(); }