Skip to content

Commit 7e8655a

Browse files
committed
Fix escaped attribute type names in generated factories (#7668)
1 parent 6bc28cd commit 7e8655a

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎src/TUnit.Core.SourceGenerator/CodeGenerators/Writers/AttributeWriter.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public class AttributeWriter(Compilation compilation)
1010
{
1111
private const string TUnitRootNamespace = "TUnit";
1212

13+
// Attribute constructors are C# expressions; reflection names must retain raw identifiers.
14+
private static readonly SymbolDisplayFormat AttributeTypeFormat =
15+
DisplayFormats.FullyQualifiedGenericWithGlobalPrefix.WithMiscellaneousOptions(
16+
DisplayFormats.FullyQualifiedGenericWithGlobalPrefix.MiscellaneousOptions |
17+
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers);
18+
1319
private readonly Dictionary<AttributeData, string> _attributeObjectInitializerCache = new();
1420
private readonly Dictionary<INamedTypeSymbol, string> _argumentFreeAttributeInitializerCache = new(SymbolEqualityComparer.Default);
1521
private readonly Dictionary<INamedTypeSymbol, bool> _tunitRelatedCache = new(SymbolEqualityComparer.Default);
@@ -134,7 +140,7 @@ private string GetAttributeObjectInitializerInner(AttributeData attributeData, A
134140

135141
var constructorArgs = arguments.Where(x => x.NameEquals == null);
136142

137-
var attributeName = attributeData.AttributeClass!.GloballyQualified();
143+
var attributeName = attributeData.AttributeClass!.ToDisplayString(AttributeTypeFormat);
138144

139145
var formattedConstructorArgs = string.Join(", ", constructorArgs.Select(x => FormatConstructorArgument(x)));
140146

@@ -178,7 +184,7 @@ private string FormatProperty(AttributeArgumentSyntax attributeArgumentSyntax)
178184

179185
public static void WriteAttributeWithoutSyntax(ICodeWriter sourceCodeWriter, AttributeData attributeData)
180186
{
181-
var attributeName = attributeData.AttributeClass!.GloballyQualified();
187+
var attributeName = attributeData.AttributeClass!.ToDisplayString(AttributeTypeFormat);
182188

183189
// Skip if any constructor arguments contain compiler-generated types
184190
if (attributeData.ConstructorArguments.Any(arg =>

‎tests/TUnit.Core.SourceGenerator.Tests/AttributeInitializerCacheTests.cs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,46 @@ [Marker<string>] public void Second() { }
5656
await Assert.That(second).Contains("MarkerAttribute<string>");
5757
}
5858

59+
[Test]
60+
[Arguments(false)]
61+
[Arguments(true)]
62+
public async Task KeywordNamespacesCompileInAttributeInitializers(bool withoutSyntax)
63+
{
64+
var compilation = CreateCompilation("""
65+
namespace @event.@class
66+
{
67+
public class MarkerAttribute<T> : System.Attribute { }
68+
public class @struct { }
69+
public class Tests
70+
{
71+
[Marker<@struct>] public void Run() { }
72+
}
73+
}
74+
""");
75+
var attribute = compilation.GetTypeByMetadataName("event.class.Tests")!
76+
.GetMembers("Run").Single().GetAttributes().Single();
77+
string initializer;
78+
if (withoutSyntax)
79+
{
80+
var writer = new CodeWriter("", includeHeader: false);
81+
AttributeWriter.WriteAttributeWithoutSyntax(writer, attribute);
82+
initializer = writer.ToString();
83+
}
84+
else
85+
{
86+
initializer = new AttributeWriter(compilation).GetAttributeObjectInitializer(attribute);
87+
}
88+
89+
await Assert.That(initializer).IsEqualTo(
90+
"new global::@event.@class.MarkerAttribute<global::@event.@class.@struct>()");
91+
var generated = CSharpSyntaxTree.ParseText(
92+
$"public class Factory {{ public object Create() => {initializer}; }}",
93+
new CSharpParseOptions(LanguageVersion.Preview));
94+
var errors = compilation.AddSyntaxTrees(generated).GetDiagnostics()
95+
.Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();
96+
await Assert.That(errors).IsEmpty();
97+
}
98+
5999
private static CSharpCompilation CreateCompilation(string source) => CSharpCompilation.Create(
60100
"AttributeInitializers",
61101
[CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview))],

0 commit comments

Comments
 (0)