Skip to content

Commit 8955bcb

Browse files
committed
Added tests + bug fix
1 parent 0b204e8 commit 8955bcb

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1312UnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,28 @@ public void MethodName(int parameter)
274274
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
275275
}
276276

277+
[Fact]
278+
public async Task TestUnderscoreOnlyNamesDoNotTriggerCodeFixAsync()
279+
{
280+
var testCode = @"public class TypeName
281+
{
282+
public void MethodName(int parameter)
283+
{
284+
string _ = parameter.ToString();
285+
string __ = parameter.ToString();
286+
}
287+
}";
288+
289+
DiagnosticResult[] expected =
290+
{
291+
this.CSharpDiagnostic().WithArguments("_").WithLocation(5, 16),
292+
this.CSharpDiagnostic().WithArguments("__").WithLocation(6, 16)
293+
};
294+
295+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
296+
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
297+
}
298+
277299
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
278300
{
279301
yield return new SA1312VariableNamesMustBeginWithLowerCaseLetter();

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public void MethodName()
434434
this.CSharpDiagnostic().WithArguments("__").WithLocation(7, 51)
435435
};
436436
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
437+
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
437438
}
438439

439440
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/RenameToLowerCaseCodeFixProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4949
if (!string.IsNullOrEmpty(token.ValueText))
5050
{
5151
var newName = token.ValueText.TrimStart('_');
52-
newName = char.ToLower(newName[0]) + newName.Substring(1);
53-
context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken), equivalenceKey: nameof(RenameToLowerCaseCodeFixProvider)), diagnostic);
52+
53+
// only offer a codefix if the name does not consist of only underscores.
54+
if (!string.IsNullOrEmpty(newName))
55+
{
56+
newName = char.ToLower(newName[0]) + newName.Substring(1);
57+
context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken), equivalenceKey: nameof(RenameToLowerCaseCodeFixProvider)), diagnostic);
58+
}
5459
}
5560
}
5661
}

0 commit comments

Comments
 (0)