Skip to content

Commit 5e10461

Browse files
committed
Fixed issue where using the same property name in multiple samples within the same assembly broken the generator
1 parent 7124bb4 commit 5e10461

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

common/CommunityToolkit.Labs.Core.SourceGenerators/ToolkitSampleOptionGenerator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace CommunityToolkit.Labs.Core.SourceGenerators;
1717
public class ToolkitSampleOptionGenerator : IIncrementalGenerator
1818
{
1919
private readonly HashSet<string> _handledPropertyNames = new();
20-
private readonly HashSet<ToolkitSampleOptionBaseAttribute> _handledAttributes = new();
2120
private readonly HashSet<ISymbol> _handledContainingClasses = new(SymbolEqualityComparer.Default);
2221

2322
public void Initialize(IncrementalGeneratorInitializationContext context)
@@ -27,7 +26,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2726
static (s, _) => s is ClassDeclarationSyntax c && c.AttributeLists.Count > 0,
2827
static (ctx, _) => ctx.SemanticModel.GetDeclaredSymbol(ctx.Node))
2928
.Where(static m => m is not null)
30-
.Select(static (x, _) => x!);
29+
.Select(static (x, _) => x!)
30+
.Where(static x => x.ContainingAssembly?.Name.Contains(".Sample") ?? false); // Restrict generating sample options to sample assemblies only
3131

3232
// Get all attributes + the original type symbol.
3333
var allAttributeData = classes.SelectMany((sym, _) => sym.GetAttributes().Select(x => (sym, x)));
@@ -60,13 +60,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6060
ctx.AddSource($"{data.ContainingClassSymbol}.GeneratedPropertyContainer.g", propertyContainerSource);
6161
}
6262

63-
if (!_handledAttributes.Add(data.Attribute))
64-
return;
63+
var name = $"{data.ContainingClassSymbol}.Property.{data.Attribute.Name}.g";
6564

66-
var dependencyPropertySource = BuildProperty(data.ContainingClassSymbol, data.Attribute.Name, data.Attribute.TypeName, data.Type);
67-
68-
if (_handledPropertyNames.Add(data.Attribute.Name))
69-
ctx.AddSource($"{data.ContainingClassSymbol}.Property.{data.Attribute.Name}.g", dependencyPropertySource);
65+
if (_handledPropertyNames.Add(name))
66+
{
67+
var dependencyPropertySource = BuildProperty(data.ContainingClassSymbol, data.Attribute.Name, data.Attribute.TypeName, data.Type);
68+
ctx.AddSource(name, dependencyPropertySource);
69+
}
7070
});
7171

7272
}
@@ -109,7 +109,6 @@ public partial class {containingClassSymbol.Name} : {typeof(IToolkitSampleGenera
109109
item.PropertyChanged -= OnPropertyChanged;
110110
}}
111111
112-
113112
if (!(value is null))
114113
{{
115114
foreach (var item in value)

0 commit comments

Comments
 (0)