From c797946c1741b7ca53d4af2095eaefc818971e73 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Sat, 10 Dec 2022 14:36:38 +0200 Subject: [PATCH 1/3] Cleanup UpgradeMSBuildWorkspaceAnalyzer --- .../CSharpUpgradeMSBuildWorkspaceAnalyzer.cs | 15 -------- .../Core/UpgradeMSBuildWorkspaceAnalyzer.cs | 17 +-------- .../UpgradeMSBuildWorkspaceAnalyzerTests.cs | 38 ++----------------- ...ualBasicUpgradeMSBuildWorkspaceAnalyzer.vb | 12 ------ 4 files changed, 5 insertions(+), 77 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.Analyzers/CSharp/CSharpUpgradeMSBuildWorkspaceAnalyzer.cs b/src/Microsoft.CodeAnalysis.Analyzers/CSharp/CSharpUpgradeMSBuildWorkspaceAnalyzer.cs index 16184f58d9..4c07781b0e 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/CSharp/CSharpUpgradeMSBuildWorkspaceAnalyzer.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/CSharp/CSharpUpgradeMSBuildWorkspaceAnalyzer.cs @@ -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 && diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/UpgradeMSBuildWorkspaceAnalyzer.cs b/src/Microsoft.CodeAnalysis.Analyzers/Core/UpgradeMSBuildWorkspaceAnalyzer.cs index e39d57fd99..e223d4bcc4 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/UpgradeMSBuildWorkspaceAnalyzer.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/UpgradeMSBuildWorkspaceAnalyzer.cs @@ -32,30 +32,15 @@ public abstract class UpgradeMSBuildWorkspaceAnalyzer : DiagnosticAnalyzer public override ImmutableArray 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) { diff --git a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs index 75a9a1c41b..e7ea692f74 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs @@ -7,10 +7,10 @@ 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 @@ -62,8 +62,7 @@ await VerifyCS.VerifyAnalyzerAsync( // 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)); + DiagnosticResult.CompilerError("CS0103").WithSpan(8, 25, 8, 41).WithArguments("MSBuildWorkspace")); } [Fact] @@ -105,36 +104,7 @@ End Sub 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 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 class TestVisualBasicUpgradeMSBuildWorkspaceAnalyzer : VisualBasicUpgradeMSBuildWorkspaceAnalyzer - { - public TestVisualBasicUpgradeMSBuildWorkspaceAnalyzer() - : base(performAssemblyChecks: false) - { - } + DiagnosticResult.CompilerError("BC30451").WithSpan(6, 25, 6, 41).WithArguments("MSBuildWorkspace")); } } } diff --git a/src/Microsoft.CodeAnalysis.Analyzers/VisualBasic/VisualBasicUpgradeMSBuildWorkspaceAnalyzer.vb b/src/Microsoft.CodeAnalysis.Analyzers/VisualBasic/VisualBasicUpgradeMSBuildWorkspaceAnalyzer.vb index 3146ef4850..908312cb7b 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/VisualBasic/VisualBasicUpgradeMSBuildWorkspaceAnalyzer.vb +++ b/src/Microsoft.CodeAnalysis.Analyzers/VisualBasic/VisualBasicUpgradeMSBuildWorkspaceAnalyzer.vb @@ -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 From e2bf53374ab3323cc69aff13088ff8f2b4dad2a0 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Sat, 10 Dec 2022 15:56:55 +0200 Subject: [PATCH 2/3] Fix tests --- .../UpgradeMSBuildWorkspaceAnalyzerTests.cs | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs index e7ea692f74..e63fa7cd52 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. +using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CSharp.Analyzers; @@ -17,19 +18,37 @@ 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 @@ -40,44 +59,29 @@ 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")); + 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 @@ -85,8 +89,7 @@ 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] @@ -97,14 +100,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")); + await VerifyVisualBasicAsync(source, s_withDesktopWorkspaces); } } } From 47fa7f64bac17692a9649a707b324fe2341b9256 Mon Sep 17 00:00:00 2001 From: Youssef Victor Date: Tue, 3 Jan 2023 17:50:04 +0200 Subject: [PATCH 3/3] Update UpgradeMSBuildWorkspaceAnalyzerTests.cs --- .../UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs index e63fa7cd52..ece178d27a 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/UpgradeMSBuildWorkspaceAnalyzerTests.cs @@ -1,11 +1,8 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. using System.Collections.Immutable; -using System.Diagnostics.CodeAnalysis; 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.CSharp.Analyzers.CSharpUpgradeMSBuildWorkspaceAnalyzer,