Skip to content

Commit

Permalink
Skip preferred term checks for override members.
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Jan 14, 2024
1 parent a1254de commit 26c900a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<!-- Make the assembly, file, and NuGet package versions the same. -->
<!-- NOTE: Change the version in Vsix\source.extension.vsixmanifest to match this! -->
<Version>3.0.11</Version>
<Version>3.0.12</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Analyzers.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="Menees.Analyzers.Vsix" Version="3.0.11" Language="en-US" Publisher="Bill Menees"/>
<Identity Id="Menees.Analyzers.Vsix" Version="3.0.12" Language="en-US" Publisher="Bill Menees"/>
<DisplayName>Menees.Analyzers.Vsix</DisplayName>
<Description xml:space="preserve">Provides analyzers for validating that tabs are used for indentation, that the lengths of lines, methods, properties, and files are acceptable, and that #regions are used within long files and files that contain multiple types.</Description>
<License>License.txt</License>
Expand Down
23 changes: 19 additions & 4 deletions src/Menees.Analyzers/Men015UsePreferredTerms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public sealed class Men015UsePreferredTerms : Analyzer
SyntaxKind.EnumMemberDeclaration,
};

private static readonly HashSet<SyntaxKind> OverrideIdentifierDeclarationKinds = new()
{
SyntaxKind.MethodDeclaration,
SyntaxKind.PropertyDeclaration,
};

#endregion

#region Public Properties
Expand Down Expand Up @@ -191,11 +197,20 @@ private bool IsIdentifierDeclaration(SyntaxNode declaration)
SyntaxKind kind = declaration.Kind();
bool result = SimpleIdentifierDeclarationKinds.Contains(kind)
|| (kind == SyntaxKind.IdentifierName
&& declaration?.Parent?.Kind() == SyntaxKind.QualifiedName
&& declaration?.Parent?.Parent?.Kind() == SyntaxKind.NamespaceDeclaration)
&& declaration.Parent?.Kind() == SyntaxKind.QualifiedName
&& declaration.Parent?.Parent?.Kind() == SyntaxKind.NamespaceDeclaration)
|| (kind == SyntaxKind.IdentifierName
&& declaration?.Parent?.Kind() == SyntaxKind.NameEquals
&& declaration?.Parent?.Parent?.Kind() == SyntaxKind.UsingDirective);
&& declaration.Parent?.Kind() == SyntaxKind.NameEquals
&& declaration.Parent?.Parent?.Kind() == SyntaxKind.UsingDirective);

// If the current identifier is an override, then ignore it because the problem is inherited.
if (result
&& OverrideIdentifierDeclarationKinds.Contains(kind)
&& declaration.ChildTokens().Any(token => token.Kind() == SyntaxKind.OverrideKeyword))
{
result = false;
}

return result;
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Menees.Analyzers.Test/Men015UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ public Canceled()
this.VerifyCSharpFix(test, fixtest);
}

[TestMethod]
public void InvalidCodeIgnoreOverrideTest()
{
const string test = @"
namespace Testing;
class Base
{
public virtual int Colour => 1;
public virtual int GetIndices() => 1;
}
class Derived : Base
{
public override int Colour => 2;
public override int GetIndices() => 2;
}
";

var analyzer = this.CSharpDiagnosticAnalyzer;
DiagnosticResult[] expected = new[]
{
new DiagnosticResult(analyzer)
{
Message = "Use Color instead of Colour.",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 21) },
Properties = new Dictionary<string, string>() { { Men015UsePreferredTerms.PreferredKey, "Color" } }
},
new DiagnosticResult(analyzer)
{
Message = "Use GetIndexes instead of GetIndices.",
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 21) },
Properties = new Dictionary<string, string>() { { Men015UsePreferredTerms.PreferredKey, "Indexes" } }
},
};

this.VerifyCSharpDiagnostic(test, expected);
}

#endregion

#region SplitIntoTerms Tests
Expand Down

0 comments on commit 26c900a

Please sign in to comment.