Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the AttributeValueOutOfSyncInspection
This inspection finds instances where a Rubberduck attribute annotation is used, the attribute exists, but the values do not agree. This commit also changes the default severity of the realated MissingAttributeInspection to Warning.
- Loading branch information
Showing
13 changed files
with
338 additions
and
22 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Rubberduck.CodeAnalysis/Inspections/Concrete/AttributeValueOutOfSyncInspection.cs
This file contains 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,69 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Rubberduck.Inspections.Abstract; | ||
using Rubberduck.Inspections.Results; | ||
using Rubberduck.Parsing; | ||
using Rubberduck.Parsing.Annotations; | ||
using Rubberduck.Parsing.Inspections; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Resources.Inspections; | ||
|
||
namespace Rubberduck.Inspections.Concrete | ||
{ | ||
[CannotAnnotate] | ||
public sealed class AttributeValueOutOfSyncInspection : InspectionBase | ||
{ | ||
public AttributeValueOutOfSyncInspection(RubberduckParserState state) | ||
:base(state) | ||
{ | ||
} | ||
|
||
protected override IEnumerable<IInspectionResult> DoGetInspectionResults() | ||
{ | ||
var declarationsWithAttributeAnnotations = State.DeclarationFinder.AllUserDeclarations | ||
.Where(declaration => declaration.Annotations.Any(annotation => annotation.AnnotationType.HasFlag(AnnotationType.Attribute))); | ||
var results = new List<DeclarationInspectionResult>(); | ||
foreach (var declaration in declarationsWithAttributeAnnotations) | ||
{ | ||
foreach (var annotation in declaration.Annotations.OfType<IAttributeAnnotation>()) | ||
{ | ||
if (HasDifferingAttributeValues(declaration, annotation, out var attributeValues)) | ||
{ | ||
var description = string.Format(InspectionResults.AttributeValueOutOfSyncInspection, declaration.IdentifierName, | ||
annotation.Attribute, string.Join(", ", annotation.AttributeValues), string.Join(", ", attributeValues)); | ||
|
||
var result = new DeclarationInspectionResult(this, description, declaration, | ||
new QualifiedContext(declaration.QualifiedModuleName, annotation.Context)); | ||
result.Properties.Annotation = annotation; | ||
result.Properties.AttributeValues = attributeValues; | ||
|
||
results.Add(result); | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
private static bool HasDifferingAttributeValues(Declaration declaration, IAttributeAnnotation annotation, out IReadOnlyList<string> attributeValues) | ||
{ | ||
var attributeNodes = declaration.DeclarationType.HasFlag(DeclarationType.Module) | ||
? declaration.Attributes.AttributeNodesFor(annotation).ToList() | ||
: declaration.Attributes.AttributeNodesFor(annotation, declaration.IdentifierName).ToList(); | ||
|
||
foreach (var attributeNode in attributeNodes) | ||
{ | ||
var values = attributeNode.Values; | ||
if (!annotation.AttributeValues.SequenceEqual(values)) | ||
{ | ||
attributeValues = values; | ||
return true; | ||
} | ||
} | ||
attributeValues = new List<string>(); | ||
return false; | ||
} | ||
} | ||
} |
This file contains 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 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
12 changes: 11 additions & 1 deletion
12
Rubberduck.Resources/Inspections/InspectionInfo.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
12 changes: 11 additions & 1 deletion
12
Rubberduck.Resources/Inspections/InspectionNames.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
12 changes: 11 additions & 1 deletion
12
Rubberduck.Resources/Inspections/InspectionResults.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
Oops, something went wrong.