Skip to content

Commit

Permalink
Additional tests and fixes for qualified using directives
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 15, 2019
1 parent e9c8dc5 commit 1b6e1c0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using Xunit;
Expand Down Expand Up @@ -320,6 +321,67 @@ namespace MyNamespace {
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
public async Task TestOmittedTypeInGenericAsync()
{
var testCode = @"
namespace TestNamespace
{
using Example = System.Collections.Generic.List<>;
}
";

var expected = new DiagnosticResult("CS7003", DiagnosticSeverity.Error).WithLocation(4, 48).WithMessage("Unexpected use of an unbound generic name");
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
public async Task TestNullableTypeInGenericAsync()
{
var testCode = @"
namespace TestNamespace
{
using Example = System.Collections.Generic.List<int?>;
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
public async Task TestGlobalQualifiedTypeInGenericAsync()
{
var testCode = @"
namespace TestNamespace
{
using Example = System.Collections.Generic.List<global::System.Int32>;
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
public async Task TestTypeInGlobalNamespaceAsync()
{
var testCode = @"
namespace TestNamespace
{
using Example = MyClass;
}
class MyClass
{
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2879, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2879")]
public async Task TestAliasTypeNestedInGenericAsync()
Expand Down
34 changes: 26 additions & 8 deletions StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SymbolNameHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Helpers
{
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Lightup;

Expand Down Expand Up @@ -36,17 +37,19 @@ public static string ToQualifiedString(this ISymbol symbol, NameSyntax name)

private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol, TypeSyntax type)
{
switch (symbol)
switch (symbol.Kind)
{
case IArrayTypeSymbol arraySymbol:
case SymbolKind.ArrayType:
var arraySymbol = (IArrayTypeSymbol)symbol;
AppendQualifiedSymbolName(builder, arraySymbol.ElementType, (type as ArrayTypeSyntax)?.ElementType);
builder
.Append("[")
.Append(',', arraySymbol.Rank - 1)
.Append("]");
return true;

case INamespaceSymbol namespaceSymbol:
case SymbolKind.Namespace:
var namespaceSymbol = (INamespaceSymbol)symbol;
if (namespaceSymbol.IsGlobalNamespace)
{
return false;
Expand All @@ -55,7 +58,8 @@ private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol sym
builder.Append(namespaceSymbol.ToDisplayString());
return true;

case INamedTypeSymbol namedTypeSymbol:
case SymbolKind.NamedType:
var namedTypeSymbol = (INamedTypeSymbol)symbol;
if (SpecialTypeHelper.TryGetPredefinedType(namedTypeSymbol.SpecialType, out var specialTypeSyntax))
{
builder.Append(specialTypeSyntax.ToFullString());
Expand Down Expand Up @@ -94,6 +98,12 @@ private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol sym
return AppendQualifiedSymbolName(builder, namedTypeSymbol.TupleUnderlyingType(), type);
}
}
else if (namedTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
AppendQualifiedSymbolName(builder, namedTypeSymbol.TypeArguments[0], (type as NullableTypeSyntax)?.ElementType);
builder.Append("?");
return true;
}
else
{
if (AppendQualifiedSymbolName(builder, symbol.ContainingSymbol, (type as QualifiedNameSyntax)?.Left))
Expand All @@ -113,14 +123,17 @@ private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol sym
for (int i = 0; i < arguments.Length; i++)
{
var argument = arguments[i];
var argumentType = argumentTypes.Arguments.Count > i ? argumentTypes.Arguments[i] : null;
var argumentType = argumentTypes != null && argumentTypes.Arguments.Count > i ? argumentTypes.Arguments[i] : null;

if (i > 0)
{
builder.Append(GenericSeparator);
}

AppendQualifiedSymbolName(builder, argument, argumentType);
if (!argumentType.IsKind(SyntaxKind.OmittedTypeArgument))
{
AppendQualifiedSymbolName(builder, argument, argumentType);
}
}

builder.Append(GenericTypeParametersClose);
Expand All @@ -130,8 +143,13 @@ private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol sym
}

default:
builder.Append(symbol.Name);
return true;
if (symbol != null)
{
builder.Append(symbol.Name);
return true;
}

return false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace StyleCop.Analyzers.ReadabilityRules
{
using System;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -215,19 +214,6 @@ private static bool AppendCanonicalString(StringBuilder builder, TypeSyntax type
builder.Append(")");
return true;
}
else if (RefTypeSyntaxWrapper.IsInstance(type))
{
var refType = (RefTypeSyntaxWrapper)type;
builder.Append(refType.RefKeyword.Text);
if (refType.ReadOnlyKeyword.IsKind(SyntaxKind.ReadOnlyKeyword))
{
builder.Append(" ").Append(refType.ReadOnlyKeyword.Text);
}

builder.Append(" ");
AppendCanonicalString(builder, refType.Type);
return true;
}
else
{
return false;
Expand Down

0 comments on commit 1b6e1c0

Please sign in to comment.