-
Notifications
You must be signed in to change notification settings - Fork 231
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
Changes from 1 commit
Commits
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
30 changes: 0 additions & 30 deletions
30
...tnet/its/expected/Ember-MM/Ember.Plugins-{9496C697-5AFD-4813-AEDC-AF33FACEADF0}-S101.json
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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, | ||
allowInitialI: typeDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword)), | ||
areUnderscoresAllowed: context.IsTest(), | ||
suggestion: out suggestion); | ||
|
||
if (!isNameValid) | ||
{ | ||
|
@@ -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]); | ||
|
@@ -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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
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
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.
There was a problem hiding this comment.
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 :)