Skip to content

Commit 50d8ff5

Browse files
committed
Generate new diagnostics on target symbols
1 parent 3393d30 commit 50d8ff5

File tree

9 files changed

+64
-59
lines changed

9 files changed

+64
-59
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/UnsupportedRoslynVersionForPartialPropertyAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if !ROSLYN_4_11_0_OR_GREATER
66

77
using System.Collections.Immutable;
8+
using System.Linq;
89
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
910
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.Diagnostics;
@@ -44,11 +45,11 @@ public override void Initialize(AnalysisContext context)
4445
}
4546

4647
// If the property has [ObservableProperty], emit an error in all cases
47-
if (propertySymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
48+
if (propertySymbol.HasAttributeWithType(observablePropertySymbol))
4849
{
4950
context.ReportDiagnostic(Diagnostic.Create(
5051
UnsupportedRoslynVersionForObservablePartialPropertySupport,
51-
observablePropertyAttribute.GetLocation(),
52+
propertySymbol.Locations.FirstOrDefault(),
5253
propertySymbol.ContainingType,
5354
propertySymbol));
5455
}

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/UseObservablePropertyOnPartialPropertyAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if ROSLYN_4_11_0_OR_GREATER
66

77
using System.Collections.Immutable;
8+
using System.Linq;
89
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
910
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.Diagnostics;
@@ -57,7 +58,7 @@ public override void Initialize(AnalysisContext context)
5758
}
5859

5960
// Check that we are in fact using [ObservableProperty]
60-
if (!fieldSymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
61+
if (!fieldSymbol.HasAttributeWithType(observablePropertySymbol))
6162
{
6263
return;
6364
}
@@ -74,7 +75,7 @@ public override void Initialize(AnalysisContext context)
7475
// Emit the diagnostic for this field to suggest changing to a partial property instead
7576
context.ReportDiagnostic(Diagnostic.Create(
7677
UseObservablePropertyOnPartialProperty,
77-
observablePropertyAttribute.GetLocation(),
78+
fieldSymbol.Locations.FirstOrDefault(),
7879
ImmutableDictionary.Create<string, string?>()
7980
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey, fieldSymbol.Name)
8081
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey, ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol)),

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System.Collections.Generic;
88
using System.Collections.Immutable;
9+
using System.Linq;
910
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.Diagnostics;
@@ -55,7 +56,7 @@ public override void Initialize(AnalysisContext context)
5556
}
5657

5758
// We only care about it if it's using [GeneratedBindableCustomProperty]
58-
if (!typeSymbol.TryGetAttributeWithType(generatedBindableCustomPropertySymbol, out AttributeData? generatedBindableCustomPropertyAttribute))
59+
if (!typeSymbol.HasAttributeWithType(generatedBindableCustomPropertySymbol))
5960
{
6061
return;
6162
}
@@ -65,7 +66,7 @@ public override void Initialize(AnalysisContext context)
6566
{
6667
context.ReportDiagnostic(Diagnostic.Create(
6768
WinRTGeneratedBindableCustomPropertyWithBaseObservablePropertyOnField,
68-
generatedBindableCustomPropertyAttribute.GetLocation(),
69+
typeSymbol.Locations.FirstOrDefault(),
6970
typeSymbol,
7071
fieldSymbol.ContainingType,
7172
fieldSymbol.Name));
@@ -76,7 +77,7 @@ public override void Initialize(AnalysisContext context)
7677
{
7778
context.ReportDiagnostic(Diagnostic.Create(
7879
WinRTGeneratedBindableCustomPropertyWithBaseRelayCommand,
79-
generatedBindableCustomPropertyAttribute.GetLocation(),
80+
typeSymbol.Locations.FirstOrDefault(),
8081
typeSymbol,
8182
methodSymbol));
8283
}

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTObservablePropertyOnFieldsIsNotAotCompatibleAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#if ROSLYN_4_11_0_OR_GREATER
66

77
using System.Collections.Immutable;
8+
using System.Linq;
89
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
910
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.Diagnostics;
@@ -50,11 +51,11 @@ public override void Initialize(AnalysisContext context)
5051
}
5152

5253
// Emit a diagnostic if the field is using the [ObservableProperty] attribute
53-
if (fieldSymbol.TryGetAttributeWithType(observablePropertySymbol, out AttributeData? observablePropertyAttribute))
54+
if (fieldSymbol.HasAttributeWithType(observablePropertySymbol))
5455
{
5556
context.ReportDiagnostic(Diagnostic.Create(
5657
WinRTObservablePropertyOnFieldsIsNotAotCompatible,
57-
observablePropertyAttribute.GetLocation(),
58+
fieldSymbol.Locations.FirstOrDefault(),
5859
ImmutableDictionary.Create<string, string?>()
5960
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.FieldNameKey, fieldSymbol.Name)
6061
.Add(FieldReferenceForObservablePropertyFieldAnalyzer.PropertyNameKey, ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol)),

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/WinRTRelayCommandIsNotGeneratedBindableCustomPropertyCompatibleAnalyzer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Immutable;
6+
using System.Linq;
67
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.Diagnostics;
@@ -49,7 +50,7 @@ public override void Initialize(AnalysisContext context)
4950
}
5051

5152
// If the method is not using [RelayCommand], we can skip it
52-
if (!methodSymbol.TryGetAttributeWithType(relayCommandSymbol, out AttributeData? relayCommandAttribute))
53+
if (!methodSymbol.HasAttributeWithType(relayCommandSymbol))
5354
{
5455
return;
5556
}
@@ -59,7 +60,7 @@ public override void Initialize(AnalysisContext context)
5960
{
6061
context.ReportDiagnostic(Diagnostic.Create(
6162
WinRTRelayCommandIsNotGeneratedBindableCustomPropertyCompatible,
62-
relayCommandAttribute.GetLocation(),
63+
methodSymbol.Locations.FirstOrDefault(),
6364
methodSymbol));
6465
}
6566
}, SymbolKind.Method);

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests/Test_UnsupportedRoslynVersionForPartialPropertyAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace MyApp
2121
{
2222
public partial class SampleViewModel : ObservableObject
2323
{
24-
[{|MVVMTK0044:ObservableProperty|}]
25-
public string Bar { get; set; }
24+
[ObservableProperty]
25+
public string {|MVVMTK0044:Bar|} { get; set; }
2626
}
2727
}
2828
""";

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4110.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ namespace MyApp
7878
{
7979
public partial class SampleViewModel : ObservableObject
8080
{
81-
[{|MVVMTK0042:ObservableProperty|}]
82-
private string name;
81+
[ObservableProperty]
82+
private string {|MVVMTK0042:name|};
8383
}
8484
}
8585
""";
@@ -371,8 +371,8 @@ namespace MyApp
371371
{
372372
public partial class SampleViewModel : ObservableObject
373373
{
374-
[{|MVVMTK0045:ObservableProperty|}]
375-
private string name;
374+
[ObservableProperty]
375+
private string {|MVVMTK0045:name|};
376376
}
377377
}
378378
""";
@@ -415,8 +415,8 @@ namespace MyApp
415415
{
416416
public partial class SampleViewModel : ObservableObject
417417
{
418-
[{|MVVMTK0045:ObservableProperty|}]
419-
private string name;
418+
[ObservableProperty]
419+
private string {|MVVMTK0045:name|};
420420
}
421421
}
422422
""";
@@ -437,8 +437,8 @@ namespace MyApp
437437
{
438438
public partial class SampleViewModel : ObservableObject
439439
{
440-
[{|MVVMTK0045:ObservableProperty|}]
441-
private string name;
440+
[ObservableProperty]
441+
private string {|MVVMTK0045:name|};
442442
}
443443
}
444444
""";
@@ -459,8 +459,8 @@ namespace MyApp
459459
{
460460
public partial class SampleViewModel : ObservableObject
461461
{
462-
[{|MVVMTK0045:ObservableProperty|}]
463-
private string name;
462+
[ObservableProperty]
463+
private string {|MVVMTK0045:name|};
464464
}
465465
}
466466
@@ -486,8 +486,8 @@ namespace MyApp
486486
{
487487
public partial class SampleViewModel : ObservableObject
488488
{
489-
[{|MVVMTK0045:ObservableProperty|}]
490-
private string name;
489+
[ObservableProperty]
490+
private string {|MVVMTK0045:name|};
491491
}
492492
}
493493
@@ -585,8 +585,8 @@ public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_Ta
585585
586586
namespace MyApp
587587
{
588-
[{|MVVMTK0047:GeneratedBindableCustomProperty|}]
589-
public partial class SampleViewModel : BaseViewModel
588+
[GeneratedBindableCustomProperty]
589+
public partial class {|MVVMTK0047:SampleViewModel|} : BaseViewModel
590590
{
591591
}
592592
@@ -620,8 +620,8 @@ public async Task WinRTGeneratedBindableCustomPropertyWithBasesMemberAnalyzer_Ta
620620
621621
namespace MyApp
622622
{
623-
[{|MVVMTK0048:GeneratedBindableCustomProperty|}]
624-
public partial class SampleViewModel : BaseViewModel
623+
[GeneratedBindableCustomProperty]
624+
public partial class {|MVVMTK0048:SampleViewModel|} : BaseViewModel
625625
{
626626
}
627627

0 commit comments

Comments
 (0)