Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace System.Windows.Forms.CSharp.Analyzers.MissingPropertySerializationConf
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MissingPropertySerializationConfigurationAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> [CSharpDiagnosticDescriptors.s_missingPropertySerializationConfiguration];
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
[CSharpDiagnosticDescriptors.s_missingPropertySerializationConfiguration];

public override void Initialize(AnalysisContext context)
{
Expand All @@ -24,26 +24,52 @@ public override void Initialize(AnalysisContext context)

private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
// We analyze only properties.
var propertySymbol = (IPropertySymbol)context.Symbol;
// We only care about properties, and don't care about static properties.
if (context.Symbol is not IPropertySymbol propertySymbol
|| propertySymbol.IsStatic)
{
return;
}

// A property of System.ComponentModel.ISite we never flag.
if (propertySymbol.Type.Name == nameof(ISite)
&& propertySymbol.Type.ContainingNamespace.ToString() == "System.ComponentModel")
{
return;
}

// Does the property belong to a class which derives from Component?
// If the property is part of any interface named IComponent, we're out.
if (propertySymbol.ContainingType.Name == nameof(IComponent))
{
return;
}

// Does the property belong to a class which implements the System.ComponentModel.IComponent interface?
if (propertySymbol.ContainingType is null
|| !propertySymbol
.ContainingType
.AllInterfaces
.Any(i => i.Name == nameof(IComponent)))
.Any(i => i.Name == nameof(IComponent) &&
i.ContainingNamespace is not null &&
i.ContainingNamespace.ToString() == "System.ComponentModel"))
{
return;
}

// Is the property read/write and at least internal?
if (propertySymbol.SetMethod is null
// Is the property read/write and at least internal and doesn't have a private setter?
if (propertySymbol.SetMethod is not IMethodSymbol propertySetter
|| propertySetter.DeclaredAccessibility == Accessibility.Private
|| propertySymbol.DeclaredAccessibility < Accessibility.Internal)
{
return;
}

// Skip overridden properties since the base property should already have the appropriate serialization configuration
if (propertySymbol.IsOverride)
{
return;
}

// Is the property attributed with DesignerSerializationVisibility or DefaultValue?
if (propertySymbol.GetAttributes()
.Any(a => a?.AttributeClass?.Name is (nameof(DesignerSerializationVisibilityAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,40 @@ Namespace Global.System.Windows.Forms.VisualBasic.Analyzers.MissingPropertySeria
Return
End If

' Does the property belong to a class which derives from Component?
' A property of System.ComponentModel.ISite we never flag.
If propertySymbol.Type.Name = NameOf(ISite) AndAlso
propertySymbol.Type.ContainingNamespace.ToString() = "System.ComponentModel" Then
Return
End If

' If the property is part of any interface named IComponent, we're out.
If propertySymbol.ContainingType.Name = NameOf(IComponent) Then
Return
End If

' Does the property belong to a class which implements the System.ComponentModel.IComponent interface?
If propertySymbol.ContainingType Is Nothing OrElse
Not propertySymbol.ContainingType.AllInterfaces.Any(
Function(i) i.Name = NameOf(IComponent)) Then
Function(i) i.Name = NameOf(IComponent) AndAlso
i.ContainingNamespace IsNot Nothing AndAlso
i.ContainingNamespace.ToString() = "System.ComponentModel") Then
Return
End If

' Skip static properties since they are not serialized by the designer
If propertySymbol.IsStatic Then
Return
End If

' Is the property read/write and at least internal?
' Is the property read/write, at least internal, and doesn't have a private setter?
If propertySymbol.SetMethod Is Nothing OrElse
propertySymbol.SetMethod.DeclaredAccessibility = Accessibility.Private OrElse
propertySymbol.DeclaredAccessibility < Accessibility.Internal Then
Return
End If

' Skip overridden properties since the base property should already have the appropriate serialization configuration
If propertySymbol.IsOverride Then
Return
End If

Expand Down
Loading