Skip to content

Commit 9f460c3

Browse files
authored
Merge pull request #2393 from sharwell/csharp7-naming
Update naming rules for C# 7
2 parents 09cdb06 + 127cf0d commit 9f460c3

22 files changed

Lines changed: 749 additions & 8 deletions

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.Helpers
1212
using Microsoft.CodeAnalysis.CSharp;
1313
using Microsoft.CodeAnalysis.CSharp.Syntax;
1414
using Microsoft.CodeAnalysis.Rename;
15+
using StyleCop.Analyzers.Lightup;
1516

1617
internal static class RenameHelper
1718
{
@@ -158,6 +159,8 @@ public static SyntaxNode GetParentDeclaration(SyntaxToken token)
158159
case SyntaxKind.UsingDirective:
159160
case SyntaxKind.LabeledStatement:
160161
case SyntaxKind.AnonymousObjectMemberDeclarator:
162+
case SyntaxKindEx.LocalFunctionStatement:
163+
case SyntaxKindEx.SingleVariableDesignation:
161164
return parent;
162165

163166
default:
@@ -191,6 +194,25 @@ public bool Found
191194
private set;
192195
}
193196

197+
public override void Visit(SyntaxNode node)
198+
{
199+
switch (node.Kind())
200+
{
201+
case SyntaxKindEx.LocalFunctionStatement:
202+
this.Found |= ((LocalFunctionStatementSyntaxWrapper)node).Identifier.ValueText == this.name;
203+
break;
204+
205+
case SyntaxKindEx.SingleVariableDesignation:
206+
this.Found |= ((SingleVariableDesignationSyntaxWrapper)node).Identifier.ValueText == this.name;
207+
break;
208+
209+
default:
210+
break;
211+
}
212+
213+
base.Visit(node);
214+
}
215+
194216
public override void VisitVariableDeclarator(VariableDeclaratorSyntax node)
195217
{
196218
this.Found |= node.Identifier.ValueText == this.name;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.Test.NamingRules;
9+
using TestHelper;
10+
using Xunit;
11+
12+
public class SA1300CSharp7UnitTests : SA1300UnitTests
13+
{
14+
[Fact]
15+
public async Task TestUpperCaseLocalFunctionAsync()
16+
{
17+
var testCode = @"public class TestClass
18+
{
19+
public void Method()
20+
{
21+
void LocalFunction()
22+
{
23+
}
24+
}
25+
}";
26+
27+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
28+
}
29+
30+
[Fact]
31+
public async Task TestLowerCaseLocalFunctionAsync()
32+
{
33+
var testCode = @"public class TestClass
34+
{
35+
public void Method()
36+
{
37+
void localFunction()
38+
{
39+
}
40+
}
41+
}";
42+
var fixedCode = @"public class TestClass
43+
{
44+
public void Method()
45+
{
46+
void LocalFunction()
47+
{
48+
}
49+
}
50+
}";
51+
52+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("localFunction").WithLocation(5, 14);
53+
54+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
55+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
56+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
57+
}
58+
59+
[Fact]
60+
public async Task TestLowerCaseLocalFunctionWithConflictAsync()
61+
{
62+
// Conflict resolution does not attempt to examine overloaded methods.
63+
var testCode = @"public class TestClass
64+
{
65+
public void Method()
66+
{
67+
void localFunction()
68+
{
69+
}
70+
71+
int LocalFunction(int value) => value;
72+
}
73+
}";
74+
var fixedCode = @"public class TestClass
75+
{
76+
public void Method()
77+
{
78+
void LocalFunction1()
79+
{
80+
}
81+
82+
int LocalFunction(int value) => value;
83+
}
84+
}";
85+
86+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments("localFunction").WithLocation(5, 14);
87+
88+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
89+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
90+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
91+
}
92+
}
93+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1301CSharp7UnitTests : SA1301UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1302CSharp7UnitTests : SA1302UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1303CSharp7UnitTests : SA1303UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1304CSharp7UnitTests : SA1304UnitTests
9+
{
10+
}
11+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.Test.NamingRules;
9+
using TestHelper;
10+
using Xunit;
11+
12+
public class SA1305CSharp7UnitTests : SA1305UnitTests
13+
{
14+
[Fact]
15+
public async Task TestInvalidVariableDesignatorNamesAreReportedAsync()
16+
{
17+
var testCode = @" public class TestClass
18+
{
19+
public void TestMethod()
20+
{
21+
var (baR, caRe, daRE, fAre) = (1, 2, 3, 4);
22+
}
23+
}
24+
";
25+
26+
DiagnosticResult[] expected =
27+
{
28+
this.CSharpDiagnostic().WithLocation(5, 14).WithArguments("variable", "baR"),
29+
this.CSharpDiagnostic().WithLocation(5, 19).WithArguments("variable", "caRe"),
30+
this.CSharpDiagnostic().WithLocation(5, 25).WithArguments("variable", "daRE"),
31+
this.CSharpDiagnostic().WithLocation(5, 31).WithArguments("variable", "fAre"),
32+
};
33+
34+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
35+
}
36+
37+
[Fact]
38+
public async Task TestParameterInLocalFunctionAsync()
39+
{
40+
var testCode = @"
41+
public class TypeName
42+
{
43+
public void MethodName()
44+
{
45+
void LocalFunction(bool abX)
46+
{
47+
}
48+
}
49+
}";
50+
51+
DiagnosticResult[] expected =
52+
{
53+
this.CSharpDiagnostic().WithArguments("parameter", "abX").WithLocation(6, 33),
54+
};
55+
56+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
57+
}
58+
59+
[Fact]
60+
public async Task TestParameterInNativeClassLocalFunctionAsync()
61+
{
62+
var testCode = @"
63+
public class TypeNameNativeMethods
64+
{
65+
public void MethodName()
66+
{
67+
void LocalFunction(bool abX)
68+
{
69+
}
70+
}
71+
}";
72+
73+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
}
75+
}
76+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1306CSharp7UnitTests : SA1306UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1307CSharp7UnitTests : SA1307UnitTests
9+
{
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
5+
{
6+
using StyleCop.Analyzers.Test.NamingRules;
7+
8+
public class SA1308CSharp7UnitTests : SA1308UnitTests
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)