Skip to content

Commit

Permalink
Added analyzer for AV1711.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman authored and Bart Koelman committed Nov 1, 2016
1 parent d381142 commit 7838839
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The list below describes per rule what its analyzer reports on. Checked rules ar
- [x] This analyzer reports members whose name contains the name of their containing type.

### AV1711: Name members similarly to members of related .NET Framework classes (LOW)
- [ ] This analyzer reports members that are named "AddItem", "Delete" or "NumberOfItems". The other scenarios are already covered by [CA1726](https://msdn.microsoft.com/en-us/library/ms182258.aspx).
- [x] This analyzer reports members that are named "AddItem", "Delete" or "NumberOfItems". The other scenarios are already covered by [CA1726](https://msdn.microsoft.com/en-us/library/ms182258.aspx).

### AV1712: Avoid short names or names that can be mistaken for other names (HIGH)
- [x] This analyzer reports variables and parameters that are named "b001", "lo", "I1" or "lOl". Requires [IOperation support](https://github.com/bkoelman/CSharpGuidelinesAnalyzer/blob/master/docs/IOperation.md).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ public class UseFrameworkTerminologyInMemberNamesSpecs : CSharpGuidelinesAnalysi
{
protected override string DiagnosticId => UseFrameworkTerminologyInMemberNamesAnalyzer.DiagnosticId;

[Fact]
public void When_method_is_named_AddItem_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new MemberSourceCodeBuilder()
.InDefaultClass(@"
void [|AddItem|]()
{
}
")
.Build();

// Act and assert
VerifyGuidelineDiagnostic(source,
"Method 'AddItem' should be renamed to 'Add'.");
}

[Fact]
public void When_method_is_named_Delete_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new MemberSourceCodeBuilder()
.InDefaultClass(@"
void [|Delete|]()
{
}
")
.Build();

// Act and assert
VerifyGuidelineDiagnostic(source,
"Method 'Delete' should be renamed to 'Remove'.");
}

[Fact]
public void When_property_is_named_NumberOfItems_it_must_be_reported()
{
// Arrange
ParsedSourceCode source = new MemberSourceCodeBuilder()
.InDefaultClass(@"
public int [|NumberOfItems|] { get; set; }
")
.Build();

// Act and assert
VerifyGuidelineDiagnostic(source,
"Property 'NumberOfItems' should be renamed to 'Count'.");
}

protected override DiagnosticAnalyzer CreateAnalyzer()
{
return new UseFrameworkTerminologyInMemberNamesAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
Expand All @@ -11,7 +12,7 @@ public sealed class UseFrameworkTerminologyInMemberNamesAnalyzer : DiagnosticAna
public const string DiagnosticId = "AV1711";

private const string Title = "AV1711";
private const string MessageFormat = "AV1711";
private const string MessageFormat = "{0} '{1}' should be renamed to '{2}'.";
private const string Description = "Name members similarly to members of related .NET Framework classes.";
private const string Category = "Naming";

Expand All @@ -23,10 +24,38 @@ public sealed class UseFrameworkTerminologyInMemberNamesAnalyzer : DiagnosticAna
[ItemNotNull]
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

private static readonly ImmutableArray<SymbolKind> MemberSymbolKinds =
new[] { SymbolKind.Property, SymbolKind.Method, SymbolKind.Field, SymbolKind.Event }.ToImmutableArray();

[NotNull]
private static readonly ImmutableDictionary<string, string> WordsReplacementMap =
new Dictionary<string, string>
{
{ "AddItem", "Add" },
{ "Delete", "Remove" },
{ "NumberOfItems", "Count" }
}.ToImmutableDictionary();

public override void Initialize([NotNull] AnalysisContext context)
{
//context.EnableConcurrentExecution();
//context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

context.RegisterSymbolAction(AnalyzeMember, MemberSymbolKinds);
}

private void AnalyzeMember(SymbolAnalysisContext context)
{
if (AnalysisUtilities.IsPropertyOrEventAccessor(context.Symbol))
{
return;
}

if (WordsReplacementMap.ContainsKey(context.Symbol.Name))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Symbol.Locations[0], context.Symbol.Kind,
context.Symbol.Name, WordsReplacementMap[context.Symbol.Name]));
}
}
}
}

0 comments on commit 7838839

Please sign in to comment.