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

Cleanup UpgradeMSBuildWorkspaceAnalyzer #6324

Merged
merged 4 commits into from
Jan 3, 2023
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 @@ -10,26 +10,11 @@ namespace Microsoft.CodeAnalysis.CSharp.Analyzers
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CSharpUpgradeMSBuildWorkspaceAnalyzer : UpgradeMSBuildWorkspaceAnalyzer
{
private protected CSharpUpgradeMSBuildWorkspaceAnalyzer(bool performAssemblyChecks)
: base(performAssemblyChecks)
{
}

public CSharpUpgradeMSBuildWorkspaceAnalyzer()
: this(performAssemblyChecks: true)
{
}

protected override void RegisterIdentifierAnalysis(CompilationStartAnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeIdentifier, SyntaxKind.IdentifierName);
}

protected override void RegisterIdentifierAnalysis(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeIdentifier, SyntaxKind.IdentifierName);
}

private void AnalyzeIdentifier(SyntaxNodeAnalysisContext context)
{
if (context.Node is IdentifierNameSyntax identifierName &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,15 @@ public abstract class UpgradeMSBuildWorkspaceAnalyzer : DiagnosticAnalyzer

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(UpgradeMSBuildWorkspaceDiagnosticRule);

private readonly bool _performAssemblyChecks;

protected UpgradeMSBuildWorkspaceAnalyzer(bool performAssemblyChecks)
{
_performAssemblyChecks = performAssemblyChecks;
}

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);

if (_performAssemblyChecks)
{
context.RegisterCompilationStartAction(AnalyzeAssemblyReferences);
}
else
{
RegisterIdentifierAnalysis(context);
}
context.RegisterCompilationStartAction(AnalyzeAssemblyReferences);
}

protected abstract void RegisterIdentifierAnalysis(CompilationStartAnalysisContext context);
protected abstract void RegisterIdentifierAnalysis(AnalysisContext context);

private void AnalyzeAssemblyReferences(CompilationStartAnalysisContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Analyzers;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.VisualBasic.Analyzers;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.CodeAnalysis.Analyzers.UnitTests.UpgradeMSBuildWorkspaceAnalyzerTests.TestCSharpUpgradeMSBuildWorkspaceAnalyzer,
Microsoft.CodeAnalysis.CSharp.Analyzers.CSharpUpgradeMSBuildWorkspaceAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
Microsoft.CodeAnalysis.Analyzers.UnitTests.UpgradeMSBuildWorkspaceAnalyzerTests.TestVisualBasicUpgradeMSBuildWorkspaceAnalyzer,
Microsoft.CodeAnalysis.VisualBasic.Analyzers.VisualBasicUpgradeMSBuildWorkspaceAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;

namespace Microsoft.CodeAnalysis.Analyzers.UnitTests
{
public class UpgradeMSBuildWorkspaceAnalyzerTests
{
private static readonly ReferenceAssemblies s_withDesktopWorkspaces = ReferenceAssemblies.NetFramework.Net46.Default.AddPackages(ImmutableArray.Create(new PackageIdentity("Microsoft.CodeAnalysis.Workspaces.Common", "2.9.0")));

private static readonly ReferenceAssemblies s_withMSBuildWorkspaces = ReferenceAssemblies.NetFramework.Net461.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.CodeAnalysis.Workspaces.MSBuild", "2.9.0"),
new PackageIdentity("Microsoft.CodeAnalysis.CSharp.Workspaces", "2.9.0"),
new PackageIdentity("Microsoft.Build.Locator", "1.0.18")));

private static async Task VerifyCSharpAsync(string source, ReferenceAssemblies referenceAssemblies)
{
await new VerifyCS.Test()
{
TestCode = source,
FixedCode = source,
ReferenceAssemblies = referenceAssemblies,
}.RunAsync();
}

private static async Task VerifyVisualBasicAsync(string source, ReferenceAssemblies referenceAssemblies)
{
await new VerifyVB.Test()
{
TestCode = source,
FixedCode = source,
ReferenceAssemblies = referenceAssemblies,
}.RunAsync();
}

[Fact]
public async Task CSharp_VerifyWithMSBuildWorkspaceAsync()
{
const string source1 = @"
namespace Microsoft.CodeAnalysis.MSBuild
{
public class MSBuildWorkspace
{
public static MSBuildWorkspace Create() => null;
}
}";

const string source2 = @"
const string source = @"
using Microsoft.CodeAnalysis.MSBuild;

class Usage
Expand All @@ -40,54 +56,37 @@ void M()
}
}";

await new VerifyCS.Test { TestState = { Sources = { source1, source2 } } }.RunAsync();
await VerifyCSharpAsync(source, s_withMSBuildWorkspaces);
}

[Fact]
public async Task CSharp_VerifyWithoutMSBuildWorkspaceAsync()
{
const string source = @"
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.{|CS0234:MSBuild|};

class Usage
{
void M()
{
var workspace = MSBuildWorkspace.Create();
var workspace = [|{|CS0103:MSBuildWorkspace|}|].Create();
}
}";

await VerifyCS.VerifyAnalyzerAsync(
source,
// Test0.cs(2,30): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
DiagnosticResult.CompilerError("CS0234").WithSpan(2, 30, 2, 37).WithArguments("MSBuild", "Microsoft.CodeAnalysis"),
// Test0.cs(8,25): error CS0103: The name 'MSBuildWorkspace' does not exist in the current context
DiagnosticResult.CompilerError("CS0103").WithSpan(8, 25, 8, 41).WithArguments("MSBuildWorkspace"),
GetCSharpExpectedDiagnostic(8, 25));
await VerifyCSharpAsync(source, s_withDesktopWorkspaces);
}

[Fact]
public async Task VisualBasic_VerifyWithMSBuildWorkspaceAsync()
{
const string source1 = @"
Namespace Microsoft.CodeAnalysis.MSBuild
Public Class MSBuildWorkspace
Public Shared Function Create() As MSBuildWorkspace
Return Nothing
End Function
End Class
End Namespace";

const string source2 = @"
const string source = @"
Imports Microsoft.CodeAnalysis.MSBuild

Class Usage
Sub M()
Dim workspace = MSBuildWorkspace.Create()
End Sub
End Class";

await new VerifyVB.Test { TestState = { Sources = { source1, source2 } } }.RunAsync();
await VerifyVisualBasicAsync(source, s_withMSBuildWorkspaces);
}

[Fact]
Expand All @@ -98,43 +97,10 @@ Imports Microsoft.CodeAnalysis.MSBuild

Class Usage
Sub M()
Dim workspace = MSBuildWorkspace.Create()
Dim workspace = [|{|BC30451:MSBuildWorkspace|}|].Create()
End Sub
End Class";

await VerifyVB.VerifyAnalyzerAsync(
source,
// Test0.vb(6) : error BC30451: 'MSBuildWorkspace' is not declared. It may be inaccessible due to its protection level.
DiagnosticResult.CompilerError("BC30451").WithSpan(6, 25, 6, 41).WithArguments("MSBuildWorkspace"),
GetBasicExpectedDiagnostic(6, 25));
}

private static DiagnosticResult GetCSharpExpectedDiagnostic(int line, int column) =>
#pragma warning disable RS0030 // Do not use banned APIs
VerifyCS.Diagnostic().WithLocation(line, column);
#pragma warning restore RS0030 // Do not use banned APIs

private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column) =>
#pragma warning disable RS0030 // Do not use banned APIs
VerifyVB.Diagnostic().WithLocation(line, column);
#pragma warning restore RS0030 // Do not use banned APIs

[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Used via new() constraint: https://github.com/dotnet/roslyn-analyzers/issues/3199")]
internal sealed class TestCSharpUpgradeMSBuildWorkspaceAnalyzer : CSharpUpgradeMSBuildWorkspaceAnalyzer
{
public TestCSharpUpgradeMSBuildWorkspaceAnalyzer()
: base(performAssemblyChecks: false)
{
}
}

[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Used via new() constraint: https://github.com/dotnet/roslyn-analyzers/issues/3199")]
internal sealed class TestVisualBasicUpgradeMSBuildWorkspaceAnalyzer : VisualBasicUpgradeMSBuildWorkspaceAnalyzer
{
public TestVisualBasicUpgradeMSBuildWorkspaceAnalyzer()
: base(performAssemblyChecks: false)
{
}
await VerifyVisualBasicAsync(source, s_withDesktopWorkspaces);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Analyzers
Public Class VisualBasicUpgradeMSBuildWorkspaceAnalyzer
Inherits UpgradeMSBuildWorkspaceAnalyzer

Private Protected Sub New(performAssemblyChecks As Boolean)
MyBase.New(performAssemblyChecks)
End Sub

Public Sub New()
Me.New(performAssemblyChecks:=True)
End Sub

Protected Overrides Sub RegisterIdentifierAnalysis(context As CompilationStartAnalysisContext)
context.RegisterSyntaxNodeAction(AddressOf AnalyzeIdentifier, SyntaxKind.IdentifierName)
End Sub

Protected Overrides Sub RegisterIdentifierAnalysis(context As AnalysisContext)
context.RegisterSyntaxNodeAction(AddressOf AnalyzeIdentifier, SyntaxKind.IdentifierName)
End Sub

Private Sub AnalyzeIdentifier(context As SyntaxNodeAnalysisContext)
Dim identifierName = TryCast(context.Node, IdentifierNameSyntax)
If identifierName Is Nothing Then
Expand Down