Skip to content

Commit 5a60f26

Browse files
author
Mirroring
committed
Merge commit 'fe592d7b1d4ad5f786c96b3ed785cacb77dfad94'
2 parents ee12dd5 + fe592d7 commit 5a60f26

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

src/System.Windows.Forms.Analyzers.CSharp/src/System/Windows/Forms/CSharp/Analyzers/MissingPropertySerializationConfiguration/MissingPropertySerializationConfigurationAnalyzer.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ namespace System.Windows.Forms.CSharp.Analyzers.MissingPropertySerializationConf
1212
[DiagnosticAnalyzer(LanguageNames.CSharp)]
1313
public class MissingPropertySerializationConfigurationAnalyzer : DiagnosticAnalyzer
1414
{
15-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
16-
=> [CSharpDiagnosticDescriptors.s_missingPropertySerializationConfiguration];
15+
private const string SystemComponentModelName = "System.ComponentModel";
16+
17+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
18+
[CSharpDiagnosticDescriptors.s_missingPropertySerializationConfiguration];
1719

1820
public override void Initialize(AnalysisContext context)
1921
{
@@ -24,26 +26,53 @@ public override void Initialize(AnalysisContext context)
2426

2527
private static void AnalyzeSymbol(SymbolAnalysisContext context)
2628
{
27-
// We analyze only properties.
28-
var propertySymbol = (IPropertySymbol)context.Symbol;
29+
// We never flag a property named Site of type of ISite
30+
if (context.Symbol is not IPropertySymbol propertySymbol
31+
|| propertySymbol.IsStatic)
32+
{
33+
return;
34+
}
2935

30-
// Does the property belong to a class which derives from Component?
31-
if (propertySymbol.ContainingType is null
32-
|| !propertySymbol
33-
.ContainingType
34-
.AllInterfaces
35-
.Any(i => i.Name == nameof(IComponent)))
36+
// A property of System.ComponentModel.ISite we never flag.
37+
if (propertySymbol.Type.Name == nameof(ISite)
38+
&& propertySymbol.Type.ContainingNamespace.ToString() == SystemComponentModelName)
3639
{
3740
return;
3841
}
3942

40-
// Is the property read/write and at least internal?
41-
if (propertySymbol.SetMethod is null
43+
// Is the property read/write and at least internal and doesn't have a private setter?
44+
if (propertySymbol.SetMethod is not IMethodSymbol propertySetter
45+
|| propertySetter.DeclaredAccessibility == Accessibility.Private
4246
|| propertySymbol.DeclaredAccessibility < Accessibility.Internal)
4347
{
4448
return;
4549
}
4650

51+
// Skip overridden properties since the base property should already
52+
// have the appropriate serialization configuration
53+
if (propertySymbol.IsOverride)
54+
{
55+
return;
56+
}
57+
58+
// If the property is part of any interface named IComponent, we're out.
59+
if (propertySymbol.ContainingType.Name == nameof(IComponent))
60+
{
61+
return;
62+
}
63+
64+
// Does the property belong to a class which implements the System.ComponentModel.IComponent interface?
65+
if (propertySymbol.ContainingType is null
66+
|| !propertySymbol
67+
.ContainingType
68+
.AllInterfaces
69+
.Any(i => i.Name == nameof(IComponent) &&
70+
i.ContainingNamespace is not null &&
71+
i.ContainingNamespace.ToString() == SystemComponentModelName))
72+
{
73+
return;
74+
}
75+
4776
// Is the property attributed with DesignerSerializationVisibility or DefaultValue?
4877
if (propertySymbol.GetAttributes()
4978
.Any(a => a?.AttributeClass?.Name is (nameof(DesignerSerializationVisibilityAttribute))

src/System.Windows.Forms.Analyzers.VisualBasic/src/Analyzers/MissingPropertySerializationConfiguration/MissingPropertySerializationConfigurationDiagnosticAnalyzer.vb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Namespace Global.System.Windows.Forms.VisualBasic.Analyzers.MissingPropertySeria
1212
Public Class MissingPropertySerializationConfigurationAnalyzer
1313
Inherits DiagnosticAnalyzer
1414

15+
Private Const SystemComponentModelName As String = "System.ComponentModel"
16+
1517
Public Overrides ReadOnly Property SupportedDiagnostics As ImmutableArray(Of DiagnosticDescriptor)
1618
Get
1719
Return ImmutableArray.Create(s_missingPropertySerializationConfiguration)
@@ -28,22 +30,46 @@ Namespace Global.System.Windows.Forms.VisualBasic.Analyzers.MissingPropertySeria
2830

2931
' We analyze only properties.
3032
Dim propertySymbol As IPropertySymbol = TryCast(context.Symbol, IPropertySymbol)
33+
3134
If propertySymbol Is Nothing Then
3235
Return
3336
End If
3437

35-
' Does the property belong to a class which derives from Component?
36-
If propertySymbol.ContainingType Is Nothing OrElse
37-
Not propertySymbol.ContainingType.AllInterfaces.Any(
38-
Function(i) i.Name = NameOf(IComponent)) Then
38+
' A property of System.ComponentModel.ISite we never flag.
39+
If propertySymbol.Type.Name = NameOf(ISite) AndAlso
40+
propertySymbol.Type.ContainingNamespace.ToString() = SystemComponentModelName Then
41+
Return
42+
End If
43+
44+
' If the property is part of any interface named IComponent, we're out.
45+
If propertySymbol.ContainingType.Name = NameOf(IComponent) Then
46+
Return
47+
End If
3948

49+
' Skip static properties since they are not serialized by the designer
50+
If propertySymbol.IsStatic Then
4051
Return
4152
End If
4253

43-
' Is the property read/write and at least internal?
54+
' Is the property read/write, at least internal, and doesn't have a private setter?
4455
If propertySymbol.SetMethod Is Nothing OrElse
56+
propertySymbol.SetMethod.DeclaredAccessibility = Accessibility.Private OrElse
4557
propertySymbol.DeclaredAccessibility < Accessibility.Internal Then
58+
Return
59+
End If
60+
61+
' Skip overridden properties since the base property should already
62+
' have the appropriate serialization configuration
63+
If propertySymbol.IsOverride Then
64+
Return
65+
End If
4666

67+
' Does the property belong to a class which implements the System.ComponentModel.IComponent interface?
68+
If propertySymbol.ContainingType Is Nothing OrElse
69+
Not propertySymbol.ContainingType.AllInterfaces.Any(
70+
Function(i) i.Name = NameOf(IComponent) AndAlso
71+
i.ContainingNamespace IsNot Nothing AndAlso
72+
i.ContainingNamespace.ToString() = SystemComponentModelName) Then
4773
Return
4874
End If
4975

0 commit comments

Comments
 (0)