Skip to content

Commit e657fd1

Browse files
committed
Updates based on code review
* Extract helper class SpecialTypeHelper * Add additional unit test * Qualify using directives for consistency
1 parent 280f5e2 commit e657fd1

7 files changed

Lines changed: 83 additions & 25 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.Helpers
5+
{
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.CSharp.Syntax;
11+
12+
internal static class SpecialTypeHelper
13+
{
14+
private static ImmutableDictionary<SpecialType, PredefinedTypeSyntax> PredefinedSpecialTypes { get; } =
15+
new Dictionary<SpecialType, PredefinedTypeSyntax>
16+
{
17+
[SpecialType.System_Boolean] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)),
18+
[SpecialType.System_Byte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ByteKeyword)),
19+
[SpecialType.System_Char] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.CharKeyword)),
20+
[SpecialType.System_Decimal] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DecimalKeyword)),
21+
[SpecialType.System_Double] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.DoubleKeyword)),
22+
[SpecialType.System_Int16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ShortKeyword)),
23+
[SpecialType.System_Int32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)),
24+
[SpecialType.System_Int64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)),
25+
[SpecialType.System_Object] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
26+
[SpecialType.System_SByte] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.SByteKeyword)),
27+
[SpecialType.System_Single] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.FloatKeyword)),
28+
[SpecialType.System_String] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.StringKeyword)),
29+
[SpecialType.System_UInt16] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UShortKeyword)),
30+
[SpecialType.System_UInt32] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.UIntKeyword)),
31+
[SpecialType.System_UInt64] = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ULongKeyword))
32+
}.ToImmutableDictionary();
33+
34+
public static bool IsPredefinedType(SpecialType specialType)
35+
{
36+
return PredefinedSpecialTypes.ContainsKey(specialType);
37+
}
38+
39+
public static bool TryGetPredefinedType(SpecialType specialType, out PredefinedTypeSyntax predefinedTypeSyntax)
40+
{
41+
return PredefinedSpecialTypes.TryGetValue(specialType, out predefinedTypeSyntax);
42+
}
43+
}
44+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace StyleCop.Analyzers.OrderingRules
1717
using Microsoft.CodeAnalysis.CodeFixes;
1818
using Microsoft.CodeAnalysis.CSharp;
1919
using Microsoft.CodeAnalysis.CSharp.Syntax;
20-
using ReadabilityRules;
2120
using Settings.ObjectModel;
2221

2322
/// <summary>
@@ -599,7 +598,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
599598
// TODO: Preserve inner trivia
600599
// TODO: simplify after qualification
601600
string fullName;
602-
if (SA1121CodeFixProvider.PredefinedSpecialTypes.ContainsKey(((INamedTypeSymbol)symbol).OriginalDefinition.SpecialType))
601+
if (SpecialTypeHelper.IsPredefinedType(((INamedTypeSymbol)symbol).OriginalDefinition.SpecialType))
603602
{
604603
fullName = "global::System." + symbol.Name;
605604
}

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@ namespace StyleCop.Analyzers.ReadabilityRules
2626
[Shared]
2727
internal class SA1121CodeFixProvider : CodeFixProvider
2828
{
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();
48-
4929
/// <inheritdoc/>
5030
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
5131
ImmutableArray.Create(SA1121UseBuiltInTypeAlias.DiagnosticId);
@@ -86,7 +66,7 @@ private static SyntaxNode ComputeReplacement(SemanticModel semanticModel, Syntax
8666
var type = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol as INamedTypeSymbol;
8767

8868
PredefinedTypeSyntax typeSyntax;
89-
if (!PredefinedSpecialTypes.TryGetValue(type.SpecialType, out typeSyntax))
69+
if (!SpecialTypeHelper.TryGetPredefinedType(type.SpecialType, out typeSyntax))
9070
{
9171
return node;
9272
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/StyleCop.Analyzers.CodeFixes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="Helpers\IndentationOptions.cs" />
6666
<Compile Include="Helpers\QueryIndentationHelpers.cs" />
6767
<Compile Include="Helpers\RenameHelper.cs" />
68+
<Compile Include="Helpers\SpecialTypeHelper.cs" />
6869
<Compile Include="LayoutRules\SA1500CodeFixProvider.cs" />
6970
<Compile Include="LayoutRules\SA1501CodeFixProvider.cs" />
7071
<Compile Include="LayoutRules\SA1502CodeFixProvider.cs" />

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,40 @@ namespace System
115115
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
116116
}
117117

118+
/// <summary>
119+
/// Verifies that simplified using statements in a namespace are expanded during the code fix operation.
120+
/// </summary>
121+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
122+
[Fact]
123+
public async Task TestInvalidSimplifiedUsingStatementsInExtensionNamespaceAsync()
124+
{
125+
var testCode = @"namespace System.MyExtension
126+
{
127+
using System.Threading;
128+
using Reflection;
129+
}
130+
";
131+
var fixedTestCode = @"using System.Reflection;
132+
using System.Threading;
133+
134+
namespace System.MyExtension
135+
{
136+
}
137+
";
138+
139+
DiagnosticResult[] expectedResults =
140+
{
141+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
142+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
143+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
144+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
145+
};
146+
147+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
148+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
149+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
150+
}
151+
118152
/// <summary>
119153
/// Verifies that having using statements in the compilation unit will not produce any diagnostics when there are type definition present.
120154
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderCombinedSystemDirectivesUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace StyleCop.Analyzers.Test.OrderingRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Analyzers.Settings.ObjectModel;
109
using Microsoft.CodeAnalysis.CodeFixes;
1110
using Microsoft.CodeAnalysis.Diagnostics;
1211
using StyleCop.Analyzers.OrderingRules;
12+
using StyleCop.Analyzers.Settings.ObjectModel;
1313
using TestHelper;
1414
using Xunit;
1515

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace StyleCop.Analyzers.Test.OrderingRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Analyzers.Settings.ObjectModel;
109
using Microsoft.CodeAnalysis.CodeFixes;
1110
using Microsoft.CodeAnalysis.Diagnostics;
1211
using StyleCop.Analyzers.OrderingRules;
12+
using StyleCop.Analyzers.Settings.ObjectModel;
1313
using TestHelper;
1414
using Xunit;
1515

0 commit comments

Comments
 (0)