Skip to content
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

Update S101: A special case should be made for two-letter acronyms in… #577

Merged
merged 3 commits into from
Jul 21, 2017
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
{
"id": "S101",
"message": "Rename interface 'AT' to match camel case naming rules, consider using 'IAt'.",
"message": "Rename interface 'AT' to match camel case naming rules, consider using 'IAT'.",
"location": {
"uri": "akka.net\src\core\Akka.Tests\Event\EventStreamSpec.cs",
"region": {
Expand All @@ -80,7 +80,7 @@
},
{
"id": "S101",
"message": "Rename interface 'ATT' to match camel case naming rules, consider using 'IAtt'.",
"message": "Rename interface 'ATT' to match camel case naming rules, consider using 'IATt'.",
"location": {
"uri": "akka.net\src\core\Akka.Tests\Event\EventStreamSpec.cs",
"region": {
Expand All @@ -93,7 +93,7 @@
},
{
"id": "S101",
"message": "Rename interface 'BT' to match camel case naming rules, consider using 'IBt'.",
"message": "Rename interface 'BT' to match camel case naming rules, consider using 'IBT'.",
"location": {
"uri": "akka.net\src\core\Akka.Tests\Event\EventStreamSpec.cs",
"region": {
Expand All @@ -106,7 +106,7 @@
},
{
"id": "S101",
"message": "Rename interface 'BTT' to match camel case naming rules, consider using 'IBtt'.",
"message": "Rename interface 'BTT' to match camel case naming rules, consider using 'IBTt'.",
"location": {
"uri": "akka.net\src\core\Akka.Tests\Event\EventStreamSpec.cs",
"region": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics

private static readonly Dictionary<SyntaxKind, string> TypeKindNameMapping = new Dictionary<SyntaxKind, string>
{
{SyntaxKind.StructDeclaration, "struct" },
{SyntaxKind.ClassDeclaration, "class" },
{SyntaxKind.InterfaceDeclaration, "interface" },
{SyntaxKind.MethodDeclaration, "method" },
{SyntaxKind.PropertyDeclaration, "property" }
{ SyntaxKind.StructDeclaration, "struct" },
{ SyntaxKind.ClassDeclaration, "class" },
{ SyntaxKind.InterfaceDeclaration, "interface" },
{ SyntaxKind.MethodDeclaration, "method" },
{ SyntaxKind.PropertyDeclaration, "property" }
};

protected sealed override void Initialize(SonarAnalysisContext context)
Expand Down Expand Up @@ -108,10 +108,10 @@ private static void CheckTypeName(SyntaxNodeAnalysisContext context)

string suggestion;
bool isNameValid = IsTypeNameValid(identifier.ValueText,
typeDeclaration is InterfaceDeclarationSyntax,
typeDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword)),
context.SemanticModel.Compilation.IsTest(),
out suggestion);
requireInitialI: typeDeclaration is InterfaceDeclarationSyntax,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much easier to read :)

allowInitialI: typeDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword)),
areUnderscoresAllowed: context.IsTest(),
suggestion: out suggestion);

if (!isNameValid)
{
Expand Down Expand Up @@ -170,9 +170,9 @@ private static bool IsMemberNameValid(string identifierName, out string suggesti

var suggestionBuilder = new StringBuilder(identifierName.Length);

foreach (var part in SplitToCamelCase(identifierName))
foreach (var part in SplitToParts(identifierName))
{
suggestionBuilder.Append(SuggestLessUppercase(part, 1));
suggestionBuilder.Append(SuggestFixedCaseName(part, 1));
}

suggestionBuilder[0] = char.ToUpperInvariant(suggestionBuilder[0]);
Expand All @@ -193,7 +193,7 @@ private static bool IsTypeNameValid(string identifierName, bool requireInitialI,
var idealNameVariant = new StringBuilder(identifierName.Length);
var acceptableNameVariant = new StringBuilder(identifierName.Length);

var parts = SplitToCamelCase(identifierName).ToList();
var parts = SplitToParts(identifierName).ToList();
for (int i = 0; i < parts.Count; i++)
{
string part = parts[i];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about currentWord?

Expand All @@ -202,11 +202,13 @@ private static bool IsTypeNameValid(string identifierName, bool requireInitialI,
continue;
}

var ideal = i == 0 ? HandleFirstTypePart(part, requireInitialI, allowInitialI, 1)
: SuggestLessUppercase(part, 1);
var ideal = i == 0
? HandleFirstPartOfTypeName(part, requireInitialI, allowInitialI, 1)
: SuggestFixedCaseName(part, 1);

var acceptable = i == 0 ? HandleFirstTypePart(part, requireInitialI, allowInitialI, 2)
: SuggestLessUppercase(part, 2);
var acceptable = i == 0
? HandleFirstPartOfTypeName(part, requireInitialI, allowInitialI, 2)
: SuggestFixedCaseName(part, 2);

idealNameVariant.Append(ideal);
acceptableNameVariant.Append(acceptable);
Expand All @@ -219,15 +221,15 @@ private static bool IsTypeNameValid(string identifierName, bool requireInitialI,
suggestion == identifierName;
}

private static string HandleFirstTypePart(string input,
private static string HandleFirstPartOfTypeName(string input,
bool requireInitialI, bool allowInitialI, int maxUppercase)
{
bool startsWithI = input[0] == 'I';

if (requireInitialI)
{
string prefix = startsWithI ? string.Empty : "I";
return prefix + SuggestLessUppercase(ToInitCap(input), maxUppercase);
return prefix + SuggestFixedCaseName(FirstCharToUpper(input), maxUppercase + 1);
}

string suggestionToProcess;
Expand All @@ -237,14 +239,14 @@ private static string HandleFirstTypePart(string input,
IsCharUpper(input, 0) &&
!IsCharUpper(input, 1))
{
suggestionToProcess = ToInitCap(input.Substring(1));
suggestionToProcess = FirstCharToUpper(input.Substring(1));
}
else
{
suggestionToProcess = ToInitCap(input);
suggestionToProcess = FirstCharToUpper(input);
}

return SuggestLessUppercase(suggestionToProcess, maxUppercase);
return SuggestFixedCaseName(suggestionToProcess, maxUppercase);
}

private static string SuggestCapitalLetterAfterNonLetter(StringBuilder suggestion)
Expand All @@ -260,15 +262,15 @@ private static string SuggestCapitalLetterAfterNonLetter(StringBuilder suggestio
return suggestion.ToString();
}

private static string SuggestLessUppercase(string input, int maxUppercaseCount)
private static string SuggestFixedCaseName(string input, int maxUppercaseCount)
{
var upper = input.Take(maxUppercaseCount);
var lower = input.Skip(maxUppercaseCount).Select(char.ToLowerInvariant);

return new string(upper.Concat(lower).ToArray());
}

private static IEnumerable<string> SplitToCamelCase(string name)
private static IEnumerable<string> SplitToParts(string name)
{
var currentWord = new StringBuilder();
foreach (var c in name)
Expand Down Expand Up @@ -322,7 +324,7 @@ private static bool IsTypeComRelated(INamedTypeSymbol symbol)
a.AttributeClass.Is(KnownType.System_Runtime_InteropServices_InterfaceTypeAttribute));
}

private static string ToInitCap(string input)
private static string FirstCharToUpper(string input)
{
return input.Length > 0
? char.ToUpperInvariant(input[0]) + input.Substring(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface II
{
}

interface IIIFoo // Noncompliant {{Rename interface 'IIIFoo' to match camel case naming rules, consider using 'IiiFoo'.}}
interface IIIFoo // Compliant
{
}

Expand Down Expand Up @@ -80,6 +80,12 @@ internal interface SVsLog // Compliant
{
}

class IILMarker { } // Noncompliant {{Rename class 'IILMarker' to match camel case naming rules, consider using 'IilMarker'.}}

interface IILMarker { } // Compliant

interface ITVImageScraper { }

class A4 { }
class AA4 { }

Expand Down