-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add EquatableAnalyzer diagnostic analyzer for collection/attribute validation #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace Equatable.SourceGenerator; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public class EquatableAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
| ImmutableArray.Create( | ||
| DiagnosticDescriptors.MissingDictionaryEqualityAttribute, | ||
| DiagnosticDescriptors.MissingSequenceEqualityAttribute, | ||
| DiagnosticDescriptors.InvalidStringEqualityAttributeUsage, | ||
| DiagnosticDescriptors.InvalidDictionaryEqualityAttributeUsage, | ||
| DiagnosticDescriptors.InvalidHashSetEqualityAttributeUsage, | ||
| DiagnosticDescriptors.InvalidSequenceEqualityAttributeUsage | ||
| ); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
|
||
| context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); | ||
| } | ||
|
|
||
| private static void AnalyzeNamedType(SymbolAnalysisContext context) | ||
| { | ||
| var typeSymbol = (INamedTypeSymbol)context.Symbol; | ||
|
|
||
| // Only analyze types with [Equatable] attribute | ||
| if (!HasEquatableAttribute(typeSymbol)) | ||
| return; | ||
|
|
||
| foreach (var property in GetAnalyzableProperties(typeSymbol)) | ||
| AnalyzeProperty(context, property); | ||
| } | ||
|
|
||
| private static IEnumerable<IPropertySymbol> GetAnalyzableProperties(INamedTypeSymbol typeSymbol) | ||
| { | ||
| // De-duplicate by name (same as the generator's GetProperties) so that | ||
| // a derived-class property shadows any same-named base-class property. | ||
| var seenPropertyNames = new HashSet<string>(StringComparer.Ordinal); | ||
|
|
||
| for (var currentSymbol = typeSymbol; currentSymbol != null; currentSymbol = currentSymbol.BaseType) | ||
| { | ||
| // Stop at system base types | ||
| if (IsSystemBaseType(currentSymbol)) | ||
| break; | ||
|
|
||
| // If a base type (not the target itself) has [Equatable], stop: it will be analyzed separately | ||
| if (!SymbolEqualityComparer.Default.Equals(currentSymbol, typeSymbol) && HasEquatableAttribute(currentSymbol)) | ||
| break; | ||
|
|
||
| foreach (var property in currentSymbol | ||
| .GetMembers() | ||
| .OfType<IPropertySymbol>() | ||
| .Where(p => !p.IsIndexer | ||
| && p.DeclaredAccessibility == Accessibility.Public | ||
| && !IsIgnored(p))) | ||
| { | ||
| if (seenPropertyNames.Add(property.Name)) | ||
| yield return property; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void AnalyzeProperty(SymbolAnalysisContext context, IPropertySymbol property) | ||
| { | ||
| var attributes = property.GetAttributes(); | ||
| var hasEqualityAttribute = false; | ||
|
|
||
| foreach (var attribute in attributes) | ||
| { | ||
| if (!IsKnownAttribute(attribute)) | ||
| continue; | ||
|
|
||
| hasEqualityAttribute = true; | ||
|
|
||
| var className = attribute.AttributeClass?.Name; | ||
| var attributeLocation = attribute.ApplicationSyntaxReference | ||
| ?.GetSyntax(context.CancellationToken).GetLocation() | ||
| ?? property.Locations.FirstOrDefault(); | ||
|
|
||
| if (className == "StringEqualityAttribute" && !IsString(property.Type)) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.InvalidStringEqualityAttributeUsage, | ||
| attributeLocation, | ||
| property.Name)); | ||
| } | ||
| else if (className == "DictionaryEqualityAttribute" | ||
| && !ImplementsDictionary(property.Type)) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.InvalidDictionaryEqualityAttributeUsage, | ||
| attributeLocation, | ||
| property.Name)); | ||
| } | ||
| else if (className == "HashSetEqualityAttribute" | ||
| && !ImplementsEnumerable(property.Type)) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.InvalidHashSetEqualityAttributeUsage, | ||
| attributeLocation, | ||
| property.Name)); | ||
| } | ||
| else if (className == "SequenceEqualityAttribute" | ||
| && !ImplementsEnumerable(property.Type)) | ||
| { | ||
pwelter34 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.InvalidSequenceEqualityAttributeUsage, | ||
| attributeLocation, | ||
| property.Name)); | ||
| } | ||
| } | ||
|
|
||
| // Warn when a collection/dictionary property has no equality attribute | ||
| if (!hasEqualityAttribute) | ||
| { | ||
| var propertyLocation = property.Locations.FirstOrDefault(); | ||
|
|
||
| if (ImplementsDictionary(property.Type)) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.MissingDictionaryEqualityAttribute, | ||
| propertyLocation, | ||
| property.Name)); | ||
| } | ||
| else if (!IsString(property.Type) && ImplementsEnumerable(property.Type)) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.MissingSequenceEqualityAttribute, | ||
| propertyLocation, | ||
| property.Name)); | ||
| } | ||
pwelter34 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private static bool HasEquatableAttribute(INamedTypeSymbol typeSymbol) | ||
| { | ||
| return typeSymbol.GetAttributes().Any( | ||
| a => IsKnownAttribute(a) && a.AttributeClass?.Name == "EquatableAttribute"); | ||
| } | ||
|
|
||
| private static bool IsIgnored(IPropertySymbol propertySymbol) | ||
| { | ||
| return propertySymbol.GetAttributes().Any( | ||
| a => IsKnownAttribute(a) && a.AttributeClass?.Name == "IgnoreEqualityAttribute"); | ||
| } | ||
|
|
||
| private static bool IsKnownAttribute(AttributeData? attribute) | ||
| { | ||
| if (attribute == null) | ||
| return false; | ||
|
|
||
| return attribute.AttributeClass is | ||
| { | ||
| ContainingNamespace: | ||
| { | ||
| Name: "Attributes", | ||
| ContainingNamespace.Name: "Equatable" | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static bool IsSystemBaseType(INamedTypeSymbol symbol) | ||
| { | ||
| return symbol is | ||
| { | ||
| Name: "Object" or "ValueType", | ||
| ContainingNamespace.Name: "System" | ||
| }; | ||
| } | ||
|
|
||
| private static bool IsString(ITypeSymbol targetSymbol) | ||
| { | ||
| return targetSymbol is | ||
| { | ||
| Name: "String", | ||
| ContainingNamespace.Name: "System" | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true when the type either IS <c>IDictionary<TKey, TValue></c> | ||
| /// or implements it, covering both interface-typed and concrete-typed properties. | ||
| /// </summary> | ||
| private static bool ImplementsDictionary(ITypeSymbol type) | ||
| { | ||
| return (type is INamedTypeSymbol named && IsDictionary(named)) | ||
| || type.AllInterfaces.Any(IsDictionary); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true when the type either IS <c>IEnumerable<T></c> | ||
| /// or implements it, covering both interface-typed and concrete-typed properties. | ||
| /// </summary> | ||
| private static bool ImplementsEnumerable(ITypeSymbol type) | ||
| { | ||
| return (type is INamedTypeSymbol named && IsEnumerable(named)) | ||
| || type.AllInterfaces.Any(IsEnumerable); | ||
| } | ||
|
|
||
| private static bool IsEnumerable(INamedTypeSymbol targetSymbol) | ||
| { | ||
| return targetSymbol is | ||
| { | ||
| Name: "IEnumerable", | ||
| IsGenericType: true, | ||
| TypeArguments.Length: 1, | ||
| TypeParameters.Length: 1, | ||
| ContainingNamespace: | ||
| { | ||
| Name: "Generic", | ||
| ContainingNamespace: | ||
| { | ||
| Name: "Collections", | ||
| ContainingNamespace: | ||
| { | ||
| Name: "System" | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static bool IsDictionary(INamedTypeSymbol targetSymbol) | ||
| { | ||
| return targetSymbol is | ||
| { | ||
| Name: "IDictionary", | ||
| IsGenericType: true, | ||
| TypeArguments.Length: 2, | ||
| TypeParameters.Length: 2, | ||
| ContainingNamespace: | ||
| { | ||
| Name: "Generic", | ||
| ContainingNamespace: | ||
| { | ||
| Name: "Collections", | ||
| ContainingNamespace: | ||
| { | ||
| Name: "System" | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.