Skip to content

Commit 6cbdc46

Browse files
committed
Update UsingCodeFixProvider to support configuration
This change updates UsingCodeFixProvider to change behavior based on the usingDirectivesPlacement setting in stylecop.json rather than simply checking if SA1200 has been disabled.
1 parent c8e4b60 commit 6cbdc46

12 files changed

Lines changed: 202 additions & 84 deletions

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs

Lines changed: 138 additions & 33 deletions
Large diffs are not rendered by default.

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1121CodeFixProvider.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ namespace StyleCop.Analyzers.ReadabilityRules
2626
[Shared]
2727
internal class SA1121CodeFixProvider : CodeFixProvider
2828
{
29-
private static readonly Dictionary<SpecialType, PredefinedTypeSyntax> PredefinedSpecialTypes = new Dictionary<SpecialType, PredefinedTypeSyntax>
30-
{
31-
[SpecialType.System_Boolean] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)),
32-
[SpecialType.System_Byte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ByteKeyword)),
33-
[SpecialType.System_Char] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.CharKeyword)),
34-
[SpecialType.System_Decimal] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DecimalKeyword)),
35-
[SpecialType.System_Double] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword)),
36-
[SpecialType.System_Int16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ShortKeyword)),
37-
[SpecialType.System_Int32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
38-
[SpecialType.System_Int64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)),
39-
[SpecialType.System_Object] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
40-
[SpecialType.System_SByte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.SByteKeyword)),
41-
[SpecialType.System_Single] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.FloatKeyword)),
42-
[SpecialType.System_String] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword)),
43-
[SpecialType.System_UInt16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UShortKeyword)),
44-
[SpecialType.System_UInt32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UIntKeyword)),
45-
[SpecialType.System_UInt64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ULongKeyword))
46-
};
29+
public static ImmutableDictionary<SpecialType, PredefinedTypeSyntax> PredefinedSpecialTypes { get; } =
30+
new Dictionary<SpecialType, PredefinedTypeSyntax>
31+
{
32+
[SpecialType.System_Boolean] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)),
33+
[SpecialType.System_Byte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ByteKeyword)),
34+
[SpecialType.System_Char] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.CharKeyword)),
35+
[SpecialType.System_Decimal] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DecimalKeyword)),
36+
[SpecialType.System_Double] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword)),
37+
[SpecialType.System_Int16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ShortKeyword)),
38+
[SpecialType.System_Int32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
39+
[SpecialType.System_Int64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)),
40+
[SpecialType.System_Object] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
41+
[SpecialType.System_SByte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.SByteKeyword)),
42+
[SpecialType.System_Single] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.FloatKeyword)),
43+
[SpecialType.System_String] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword)),
44+
[SpecialType.System_UInt16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UShortKeyword)),
45+
[SpecialType.System_UInt32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UIntKeyword)),
46+
[SpecialType.System_UInt64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ULongKeyword))
47+
}.ToImmutableDictionary();
4748

4849
/// <inheritdoc/>
4950
public override ImmutableArray<string> FixableDiagnosticIds { get; } =

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200OutsideNamespaceUnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace TestNamespace
7777

7878
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
7979
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
80-
////await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
80+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
8181
}
8282

8383
/// <summary>
@@ -112,7 +112,7 @@ namespace System
112112

113113
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
114114
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
115-
////await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
115+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
116116
}
117117

118118
/// <summary>
@@ -172,7 +172,7 @@ namespace TestNamespace
172172

173173
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
174174
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
175-
////await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
175+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
176176
}
177177

178178
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1208UnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ public async Task TestPreprocessorDirectivesAsync()
310310
using Microsoft.CodeAnalysis;
311311
using Microsoft.VisualStudio;
312312
using MyList = System.Collections.Generic.List<int>;
313-
314313
#if true
315314
using System.Collections;
316315
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1209UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class A
5959
using System.Linq;
6060
using System.Net;
6161
using TasksNamespace = System.Threading.Tasks;
62-
6362
class A
6463
{
6564
}";
@@ -249,7 +248,6 @@ public async Task TestPreprocessorDirectivesAsync()
249248
var fixedTestCode = @"using System;
250249
using Microsoft.VisualStudio;
251250
using MyList = System.Collections.Generic.List<int>;
252-
253251
#if true
254252
using Microsoft.CodeAnalysis;
255253
using Threads = System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ public async Task TestPreprocessorDirectivesAsync()
187187
var fixedTestCode = @"using Microsoft.VisualStudio;
188188
using System;
189189
using MyList = System.Collections.Generic.List<int>;
190-
191190
#if true
192191
using Microsoft.CodeAnalysis;
193192
using Microsoft.CodeAnalysis.Diagnostics;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210UnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ public async Task TestPreprocessorDirectivesAsync()
291291
var fixedTestCode = @"using System;
292292
using Microsoft.VisualStudio;
293293
using MyList = System.Collections.Generic.List<int>;
294-
295294
#if true
296295
using Microsoft.CodeAnalysis;
297296
using Microsoft.CodeAnalysis.Diagnostics;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1211UnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ public async Task TestPreprocessorDirectivesAsync()
179179
var fixedTestCode = @"using System;
180180
using Microsoft.VisualStudio;
181181
using MyList = System.Collections.Generic.List<int>;
182-
183182
#if true
184183
using AThing = System.Threading;
185184
using BThing = System.Threading.Tasks;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1216UnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ public async Task TestPreprocessorDirectivesAsync()
154154
using Microsoft.VisualStudio;
155155
using static System.String;
156156
using MyList = System.Collections.Generic.List<int>;
157-
158157
#if true
159158
using System.Threading;
160159
using System.Threading.Tasks;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1217UnitTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ public async Task TestPreprocessorDirectivesAsync()
211211
using Microsoft.VisualStudio;
212212
using static System.Tuple;
213213
using MyList = System.Collections.Generic.List<int>;
214-
215214
#if true
216215
using static System.Math;
217216
using static System.String;

0 commit comments

Comments
 (0)