From c9868afa936637874f83efdb7bc387b9d1340bf4 Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Sat, 7 Sep 2024 14:02:12 -0400 Subject: [PATCH 1/2] Add F# shim for IDE0047 analyzer * For background, see: * https://github.com/dotnet/roslyn/pull/35591 * https://github.com/dotnet/fsharp/issues/16078, * https://github.com/dotnet/fsharp/pull/16079#issuecomment-1771467974 * https://github.com/dotnet/fsharp/pull/16079#pullrequestreview-1692584202 * https://github.com/dotnet/fsharp/pull/16211 --- ...nnecessaryParenthesesDiagnosticAnalyzer.cs | 14 +++++ ...nnecessaryParenthesesDiagnosticAnalyzer.cs | 54 +++++++++++++++++++ .../FSharp/InternalAPI.Unshipped.txt | 2 + 3 files changed, 70 insertions(+) create mode 100644 src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs create mode 100644 src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs diff --git a/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs new file mode 100644 index 0000000000000..27d05e4ac5f73 --- /dev/null +++ b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Immutable; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics; + +internal interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer +{ + Task> AnalyzeSyntaxAsync(DiagnosticDescriptor descriptor, Document document, CancellationToken cancellationToken); +} diff --git a/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs b/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs new file mode 100644 index 0000000000000..c2693e1242405 --- /dev/null +++ b/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Immutable; +using System.Composition; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics; +using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Host.Mef; + +namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Internal.Diagnostics; + +[Shared] +[ExportLanguageService(typeof(FSharpUnnecessaryParenthesesDiagnosticAnalyzerService), LanguageNames.FSharp)] +[method: ImportingConstructor] +[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] +internal sealed class FSharpUnnecessaryParenthesesDiagnosticAnalyzerService(IFSharpUnnecessaryParenthesesDiagnosticAnalyzer analyzer) : ILanguageService +{ + public Task> AnalyzeSyntaxAsync(DiagnosticDescriptor descriptor, Document document, CancellationToken cancellationToken) + => analyzer.AnalyzeSyntaxAsync(descriptor, document, cancellationToken); +} + +[DiagnosticAnalyzer(LanguageNames.FSharp)] +internal sealed class FSharpUnnecessaryParenthesesDiagnosticAnalyzer : DocumentDiagnosticAnalyzer, IBuiltInAnalyzer +{ + private readonly DiagnosticDescriptor _descriptor = + new( + IDEDiagnosticIds.RemoveUnnecessaryParenthesesDiagnosticId, + new LocalizableResourceString(nameof(AnalyzersResources.Remove_unnecessary_parentheses), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)), + new LocalizableResourceString(nameof(AnalyzersResources.Parentheses_can_be_removed), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)), + DiagnosticCategory.Style, + DiagnosticSeverity.Hidden, + isEnabledByDefault: true, + customTags: FSharpDiagnosticCustomTags.Unnecessary); + + public FSharpUnnecessaryParenthesesDiagnosticAnalyzer() => SupportedDiagnostics = [_descriptor]; + + public override ImmutableArray SupportedDiagnostics { get; } + + public override Task> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken) + => Task.FromResult>([]); + + public override Task> AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken) + => document.Project.Services.GetService()?.AnalyzeSyntaxAsync(_descriptor, document, cancellationToken) + ?? Task.FromResult>([]); + + public bool IsHighPriority => false; + + public DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis; +} diff --git a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt index f5bd0244c11a9..8d215106500e2 100644 --- a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt +++ b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt @@ -27,6 +27,8 @@ Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpIDEDiagnosticIds Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpDiagnosticAnalyzerService Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpDocumentDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpSimplifyNameDiagnosticAnalyzer +Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnnecessaryParenthesesDiagnosticAnalyzer +Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.AnalyzeSyntaxAsync(Microsoft.CodeAnalysis.DiagnosticDescriptor! descriptor, Microsoft.CodeAnalysis.Document! document, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedDeclarationsDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedOpensDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.DocumentHighlighting.FSharpDocumentHighlights From 2bc9e4bba48198b7bcb15d7002b025f30b0aa858 Mon Sep 17 00:00:00 2001 From: Brian Rourke Boll Date: Sat, 7 Sep 2024 14:08:38 -0400 Subject: [PATCH 2/2] Add diagnostic ID --- .../ExternalAccess/FSharp/Diagnostics/FSharpIDEDiagnosticIds.cs | 1 + src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/FSharpIDEDiagnosticIds.cs b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/FSharpIDEDiagnosticIds.cs index ce24ace6a3432..7c99cd38220b6 100644 --- a/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/FSharpIDEDiagnosticIds.cs +++ b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/FSharpIDEDiagnosticIds.cs @@ -12,5 +12,6 @@ internal static class FSharpIDEDiagnosticIds { public static string SimplifyNamesDiagnosticId => IDEDiagnosticIds.SimplifyNamesDiagnosticId; public static string RemoveUnnecessaryImportsDiagnosticId => IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId; + public static string RemoveUnnecessaryParenthesesDiagnosticId => IDEDiagnosticIds.RemoveUnnecessaryParenthesesDiagnosticId; } } diff --git a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt index 8d215106500e2..cab55680746c2 100644 --- a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt +++ b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt @@ -571,6 +571,7 @@ static Microsoft.CodeAnalysis.ExternalAccess.FSharp.TaskList.FSharpTaskListItem. ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpDiagnosticCustomTags.Microsoft.get -> string[] ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpDiagnosticCustomTags.Unnecessary.get -> string[] ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpIDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId.get -> string +~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpIDEDiagnosticIds.RemoveUnnecessaryParenthesesDiagnosticId.get -> string ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpIDEDiagnosticIds.SimplifyNamesDiagnosticId.get -> string ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Editor.Shared.Extensions.FSharpDependencyObjectExtensions.SetDefaultTextProperties(this System.Windows.DependencyObject dependencyObject, Microsoft.VisualStudio.Text.Classification.IClassificationFormatMap formatMap) -> void ~static Microsoft.CodeAnalysis.ExternalAccess.FSharp.Editor.Shared.Extensions.FSharpDependencyObjectExtensions.SetTextProperties(this System.Windows.DependencyObject dependencyObject, Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties textProperties) -> void