Skip to content

Commit c372d84

Browse files
committed
Fix diagnostic suppressor, add tests
1 parent c36129f commit c372d84

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Diagnostics/Suppressors/StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
4646
SyntaxNode? syntaxNode = diagnostic.Location.SourceTree?.GetRoot(context.CancellationToken).FindNode(diagnostic.Location.SourceSpan);
4747

4848
// Check that the target is effectively [static:] over a property declaration, which is the only case we are interested in
49-
if (syntaxNode is AttributeTargetSpecifierSyntax { Parent: PropertyDeclarationSyntax propertyDeclaration, Identifier: SyntaxToken(SyntaxKind.StaticKeyword) })
49+
if (syntaxNode is AttributeTargetSpecifierSyntax { Parent.Parent: PropertyDeclarationSyntax propertyDeclaration, Identifier: SyntaxToken(SyntaxKind.StaticKeyword) })
5050
{
5151
SemanticModel semanticModel = context.GetSemanticModel(syntaxNode.SyntaxTree);
5252

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Threading.Tasks;
6+
using CommunityToolkit.GeneratedDependencyProperty.Tests.Helpers;
7+
using Microsoft.CodeAnalysis.Testing;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace CommunityToolkit.GeneratedDependencyProperty.Tests;
11+
12+
[TestClass]
13+
public class Test_DiagnosticSuppressors
14+
{
15+
private static readonly DiagnosticResult CS0658 = DiagnosticResult.CompilerWarning("CS0658");
16+
17+
[TestMethod]
18+
[DataRow("get")]
19+
[DataRow("with")]
20+
[DataRow("readonly")]
21+
[DataRow("propdp")]
22+
public async Task StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor_OtherTarget_NotSuppressed(string target)
23+
{
24+
await new CSharpSuppressorTest<StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor>(
25+
$$"""
26+
using System;
27+
using CommunityToolkit.WinUI;
28+
using Windows.UI.Xaml;
29+
30+
public class MyObject : DependencyObject
31+
{
32+
[GeneratedDependencyProperty]
33+
[{{target}}: Test]
34+
public string? Name { get; set; }
35+
}
36+
37+
public class TestAttribute : Attribute;
38+
""")
39+
.WithSpecificDiagnostics(CS0658)
40+
.RunAsync();
41+
}
42+
43+
[TestMethod]
44+
public async Task StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor_NoTriggerAttribute_NotSuppressed()
45+
{
46+
await new CSharpSuppressorTest<StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor>(
47+
"""
48+
using System;
49+
using Windows.UI.Xaml;
50+
51+
public class MyObject : DependencyObject
52+
{
53+
[static: Test]
54+
public string? Name { get; set; }
55+
}
56+
57+
public class TestAttribute : Attribute;
58+
""")
59+
.WithSpecificDiagnostics(CS0658)
60+
.RunAsync();
61+
}
62+
63+
[TestMethod]
64+
public async Task StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor_ValidUse_Suppressed()
65+
{
66+
await new CSharpSuppressorTest<StaticAttributeListTargetOnGeneratedDependencyPropertyDeclarationSuppressor>(
67+
"""
68+
using System;
69+
using CommunityToolkit.WinUI;
70+
using Windows.UI.Xaml;
71+
72+
public class MyObject : DependencyObject
73+
{
74+
[GeneratedDependencyProperty]
75+
[static: Test]
76+
public string? Name { get; set; }
77+
}
78+
79+
public class TestAttribute : Attribute;
80+
""")
81+
.WithSpecificDiagnostics(CS0658)
82+
.RunAsync();
83+
}
84+
}

0 commit comments

Comments
 (0)