Skip to content

Commit

Permalink
Avoid duplication for workaround to support C#9 init
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource committed Nov 4, 2020
1 parent f6c9420 commit 738c655
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}

namespace Tests.Diagnostics
namespace Tests.Diagnostics
{
abstract record AbstractRecordOne
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using System;
using System.Collections.Generic;

namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}

namespace Tests.Diagnostics
namespace Tests.Diagnostics
{
record Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,3 @@ public record WithAttribute2
private WithAttribute2() { }
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,3 @@ public abstract record View2 // Compliant, has abstract and non abstract members

public abstract record Record(string X);
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ public bool Equals(C other)
return false;
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,3 @@ static void TargetTypedConditional(bool isTrue, Apple[] apples, Fruit[] givenFru
Fruit[] fruits3 = givenFruits ?? apples;
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
namespace Tests.Diagnostics
namespace Tests.Diagnostics
{
public interface IMyInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,3 @@ static void Bar()
{ }
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@ public int this[int i]
}
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ void Method()
string unusedString = string.Empty; // Noncompliant
}
}

// See https://github.com/dotnet/roslyn/issues/45510
namespace System.Runtime.CompilerServices
{
public class IsExternalInit { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace SonarAnalyzer.UnitTest.TestFramework
internal struct SolutionBuilder
{
private const string GeneratedAssemblyName = "project";
private const string InitSnippet = @"namespace System.Runtime.CompilerServices { public class IsExternalInit { } } // See https://github.com/dotnet/roslyn/issues/45510";

private Solution Solution { get; }

Expand Down Expand Up @@ -89,8 +90,9 @@ public static SolutionBuilder Create() =>
FromSolution(new AdhocWorkspace().CurrentSolution);

public static SolutionBuilder CreateSolutionFromPaths(IEnumerable<string> paths,
OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary,
IEnumerable<MetadataReference> additionalReferences = null)
OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary,
IEnumerable<MetadataReference> additionalReferences = null,
bool isSupportForCSharp9InitNeeded = false)
{
if (paths == null ||
!paths.Any())
Expand All @@ -109,6 +111,11 @@ public static SolutionBuilder CreateSolutionFromPaths(IEnumerable<string> paths,
.AddDocuments(paths)
.AddReferences(additionalReferences);

if (isSupportForCSharp9InitNeeded)
{
project = project.AddSnippet(InitSnippet);
}

if (additionalReferences != null &&
additionalReferences.Any(r => r.Display.Contains("\\netstandard")))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void VerifyAnalyzer(IEnumerable<string> paths, SonarDiagnosticAna
OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary,
IEnumerable<MetadataReference> additionalReferences = null)
{
var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, outputKind, additionalReferences);
var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, outputKind, additionalReferences, IsSupportForCSharp9InitNeeded(options));

foreach (var compilation in solutionBuilder.Compile(options?.ToArray()))
{
Expand Down Expand Up @@ -206,5 +206,9 @@ public static void VerifyCodeFix(string path, string pathToExpected,
{
CodeFixVerifier.VerifyCodeFix(path, pathToExpected, pathToExpected, diagnosticAnalyzer, codeFixProvider, codeFixTitle, options, additionalReferences);
}

private static bool IsSupportForCSharp9InitNeeded(IEnumerable<ParseOptions> options) =>
options != null
&& options.OfType<CSharpParseOptions>().Select(option => option.LanguageVersion).Contains(LanguageVersion.CSharp9);
}
}

0 comments on commit 738c655

Please sign in to comment.