Skip to content

Commit 0b204e8

Browse files
committed
SA1313 codefix will remove underscore if necessary
1 parent bbd6bba commit 0b204e8

3 files changed

Lines changed: 54 additions & 28 deletions

File tree

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

Lines changed: 10 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]

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

Lines changed: 42 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
{
@@ -505,6 +486,48 @@ public interface IDerivedTest : ITest, IEmptyInterface
505486
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
506487
}
507488

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

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4545

4646
foreach (var diagnostic in context.Diagnostics)
4747
{
48-
if (!this.FixableDiagnosticIds.Contains(diagnostic.Id))
49-
{
50-
continue;
51-
}
52-
5348
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
5449
if (!string.IsNullOrEmpty(token.ValueText))
5550
{
56-
var newName = char.ToLower(token.ValueText[0]) + token.ValueText.Substring(1);
51+
var newName = token.ValueText.TrimStart('_');
52+
newName = char.ToLower(newName[0]) + newName.Substring(1);
5753
context.RegisterCodeFix(CodeAction.Create(string.Format(NamingResources.RenameToCodeFix, newName), cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken), equivalenceKey: nameof(RenameToLowerCaseCodeFixProvider)), diagnostic);
5854
}
5955
}

0 commit comments

Comments
 (0)