Skip to content

Commit e3970b8

Browse files
committed
Merge pull request #1616 from vweijsters/fix-1604
2 parents cce12c9 + 8955bcb commit e3970b8

3 files changed

Lines changed: 88 additions & 29 deletions

File tree

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,19 @@ public void MethodName()
148148
}
149149
}";
150150

151+
var fixedTestCode = @"public class TypeName
152+
{
153+
public void MethodName()
154+
{
155+
string bar = ""baz"";
156+
}
157+
}";
158+
151159
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("_bar").WithLocation(5, 16);
152160

153161
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
154-
155-
// Verify the code fix doesn't do anything in this case
156-
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
162+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
163+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
157164
}
158165

159166
[Fact]
@@ -267,6 +274,28 @@ public void MethodName(int parameter)
267274
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
268275
}
269276

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+
270299
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
271300
{
272301
yield return new SA1312VariableNamesMustBeginWithLowerCaseLetter();

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,6 @@ public void MethodName(string bar, string car, string par)
114114
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
115115
}
116116

117-
[Fact]
118-
public async Task TestParameterStartingWithAnUnderscoreAsync()
119-
{
120-
// Makes sure SA1313 is reported for parameters starting with an underscore
121-
var testCode = @"public class TypeName
122-
{
123-
public void MethodName(string _bar)
124-
{
125-
}
126-
}";
127-
128-
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("_bar").WithLocation(3, 35);
129-
130-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
131-
132-
// Verify the code fix doesn't do anything in this case
133-
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
134-
}
135-
136117
[Fact]
137118
public async Task TestParameterStartingWithLetterAsync()
138119
{
@@ -453,6 +434,7 @@ public void MethodName()
453434
this.CSharpDiagnostic().WithArguments("__").WithLocation(7, 51)
454435
};
455436
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
437+
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
456438
}
457439

458440
/// <summary>
@@ -505,6 +487,48 @@ public interface IDerivedTest : ITest, IEmptyInterface
505487
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
506488
}
507489

490+
/// <summary>
491+
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1604:
492+
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1604
493+
/// </summary>
494+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
495+
[Fact]
496+
public async Task TestCodeFixProperlyRemovesUnderscoreAsync()
497+
{
498+
var testCode = @"
499+
public class TestClass
500+
{
501+
public TestClass(string _text)
502+
: this(_text, false)
503+
{
504+
}
505+
506+
public TestClass(string text, bool flag)
507+
{
508+
}
509+
}
510+
";
511+
512+
var fixedCode = @"
513+
public class TestClass
514+
{
515+
public TestClass(string text)
516+
: this(text, false)
517+
{
518+
}
519+
520+
public TestClass(string text, bool flag)
521+
{
522+
}
523+
}
524+
";
525+
526+
var expected = this.CSharpDiagnostic().WithLocation(4, 29).WithArguments("_text");
527+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
528+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
529+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
530+
}
531+
508532
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
509533
{
510534
yield return new SA1313ParameterNamesMustBeginWithLowerCaseLetter();

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4646
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
4747
if (!string.IsNullOrEmpty(token.ValueText))
4848
{
49-
var newName = char.ToLower(token.ValueText[0]) + token.ValueText.Substring(1);
50-
context.RegisterCodeFix(
51-
CodeAction.Create(
52-
string.Format(NamingResources.RenameToCodeFix, newName),
53-
cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
54-
nameof(RenameToLowerCaseCodeFixProvider)),
55-
diagnostic);
49+
var newName = token.ValueText.TrimStart('_');
50+
51+
// only offer a codefix if the name does not consist of only underscores.
52+
if (!string.IsNullOrEmpty(newName))
53+
{
54+
newName = char.ToLower(newName[0]) + newName.Substring(1);
55+
context.RegisterCodeFix(
56+
CodeAction.Create(
57+
string.Format(NamingResources.RenameToCodeFix, newName),
58+
cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
59+
nameof(RenameToLowerCaseCodeFixProvider)),
60+
diagnostic);
61+
}
5662
}
5763
}
5864
}

0 commit comments

Comments
 (0)