Skip to content

Commit

Permalink
Add F# shim for IDE0047 analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
brianrourkeboll committed Sep 7, 2024
1 parent 139f845 commit c9868af
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(DiagnosticDescriptor descriptor, Document document, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -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<ImmutableArray<Diagnostic>> 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<DiagnosticDescriptor> SupportedDiagnostics { get; }

public override Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken)
=> Task.FromResult<ImmutableArray<Diagnostic>>([]);

public override Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
=> document.Project.Services.GetService<FSharpUnnecessaryParenthesesDiagnosticAnalyzerService>()?.AnalyzeSyntaxAsync(_descriptor, document, cancellationToken)
?? Task.FromResult<ImmutableArray<Diagnostic>>([]);

public bool IsHighPriority => false;

public DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<System.Collections.Immutable.ImmutableArray<Microsoft.CodeAnalysis.Diagnostic!>>!
Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedDeclarationsDiagnosticAnalyzer
Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedOpensDiagnosticAnalyzer
Microsoft.CodeAnalysis.ExternalAccess.FSharp.DocumentHighlighting.FSharpDocumentHighlights
Expand Down

0 comments on commit c9868af

Please sign in to comment.