From d75513fde407b0a6d437ad41cde7722efec7dd2a Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Thu, 25 Apr 2024 20:06:15 -0500 Subject: [PATCH] Fixed stupid issue where types would be treated as matches for the readonly generator even if they didn't have the right attribute. --- .../readOnly/ReadOnlyReferenceTests.cs | 46 +++++++++++++++++++ Schema/src/readOnly/ReadOnlyTypeGenerator.cs | 1 + 2 files changed, 47 insertions(+) diff --git a/Schema Tests/readOnly/ReadOnlyReferenceTests.cs b/Schema Tests/readOnly/ReadOnlyReferenceTests.cs index 5f1b2dc..c545c20 100644 --- a/Schema Tests/readOnly/ReadOnlyReferenceTests.cs +++ b/Schema Tests/readOnly/ReadOnlyReferenceTests.cs @@ -343,5 +343,51 @@ public interface IReadOnlyWrapper { """); } + + [Test] + public void TestIgnoresFakeMatches() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + using foo.bar.correct; + using foo.bar.wrong; + + namespace foo.bar.correct { + public class Other; + } + + namespace foo.bar.correct { + [GenerateReadOnly] + public partial interface IOther; + } + + namespace foo.bar { + [GenerateReadOnly] + public partial interface IWrapper { + IReadOnlyOther Field { get; set; } + } + } + """, + """ + namespace foo.bar.correct { + public partial interface IOther : IReadOnlyOther; + + public interface IReadOnlyOther; + } + + """, + """ + namespace foo.bar { + public partial interface IWrapper : IReadOnlyWrapper { + correct.IReadOnlyOther IReadOnlyWrapper.Field => Field; + } + + public interface IReadOnlyWrapper { + public correct.IReadOnlyOther Field { get; } + } + } + + """); + } } } \ No newline at end of file diff --git a/Schema/src/readOnly/ReadOnlyTypeGenerator.cs b/Schema/src/readOnly/ReadOnlyTypeGenerator.cs index 9947b0d..a2c1f46 100644 --- a/Schema/src/readOnly/ReadOnlyTypeGenerator.cs +++ b/Schema/src/readOnly/ReadOnlyTypeGenerator.cs @@ -604,6 +604,7 @@ public static IEnumerable LookupTypesWithNameAndArity( int arity) => semanticModel .LookupNamespacesAndTypes(syntax.SpanStart, null, searchString) + .Where(symbol => symbol.HasAttribute()) .Where(symbol => symbol is INamedTypeSymbol) .Select(symbol => symbol as INamedTypeSymbol) .Where(symbol => symbol.Arity == arity);