Logo FSharp.Analyzers.SDK

Programmatically running an analyzer

Using the SDK, it is possible to invoke analyzers programmatically.
This can be done via the Client type.
The Client needs to know what type of analyzer you intend to load: console or editor.

open FSharp.Analyzers.SDK

let client = Client<CliAnalyzerAttribute, CliContext>()
let countLoaded = client.LoadAnalyzers @"C:\MyAnalyzers"
let ctx = Unchecked.defaultof<CliContext> // Construct your context...

RunAnalyzers does not throw. It returns a result per analyzer, with either the messages of that analyzer or the exception it threw. An analyzer that throws does not affect the messages of the other analyzers.

async {
    let! results = client.RunAnalyzers(ctx)

    for result in results do
        match result.Output with
        | Ok messages ->
            for message in messages do
                printfn "%s: %s" message.Message.Code message.Message.Message
        | Error error -> printfn "Analyzer %s failed: %s" result.AnalyzerName error.Message
}

Previous Next

Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Analyzers
namespace FSharp.Analyzers.SDK
val client: Client<CliAnalyzerAttribute,CliContext>
Multiple items
module Client from FSharp.Analyzers.SDK

--------------------
type Client<'TAttribute,'TContext (requires 'TAttribute :> AnalyzerAttribute and 'TContext :> Context)> = new: logger: ILogger -> Client<'TAttribute,'TContext> + 1 overload member LoadAnalyzers: dir: string * ?excludeInclude: ExcludeInclude -> AssemblyLoadStats member RunAnalyzers: ctx: 'TContext -> Async<AnalysisResult list> + 1 overload

--------------------
new: unit -> Client<'TAttribute,'TContext>
new: logger: Extensions.Logging.ILogger -> Client<'TAttribute,'TContext>
Multiple items
type CliAnalyzerAttribute = inherit AnalyzerAttribute new: [<Optional; DefaultParameterValue (("Analyzer" :> obj))>] name: string * [<Optional; DefaultParameterValue (("" :> obj))>] shortDescription: string * [<Optional; DefaultParameterValue (("" :> obj))>] helpUri: string -> CliAnalyzerAttribute member Name: string
<summary> Marks an analyzer for scanning during the console application run. </summary>

--------------------
new: [<System.Runtime.InteropServices.Optional; System.Runtime.InteropServices.DefaultParameterValue (("Analyzer" :> obj))>] name: string * [<System.Runtime.InteropServices.Optional; System.Runtime.InteropServices.DefaultParameterValue (("" :> obj))>] shortDescription: string * [<System.Runtime.InteropServices.Optional; System.Runtime.InteropServices.DefaultParameterValue (("" :> obj))>] helpUri: string -> CliAnalyzerAttribute
type CliContext = { FileName: string SourceText: ISourceText ParseFileResults: FSharpParseFileResults CheckFileResults: FSharpCheckFileResults TypedTree: FSharpImplementationFileContents option CheckProjectResults: FSharpCheckProjectResults ProjectOptions: AnalyzerProjectOptions AnalyzerIgnoreRanges: Map<string,AnalyzerIgnoreRange list> } interface Context member GetAllEntities: publicOnly: bool -> AssemblySymbol list member GetAllSymbolUsesOfFile: unit -> FSharpSymbolUse seq member GetAllSymbolUsesOfProject: unit -> FSharpSymbolUse array
<summary> All the relevant compiler information for a given file. Contains the source text, untyped and typed tree information. </summary>
val countLoaded: AssemblyLoadStats
member Client.LoadAnalyzers: dir: string * ?excludeInclude: ExcludeInclude -> AssemblyLoadStats
val ctx: CliContext
module Unchecked from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T
val async: AsyncBuilder
val results: AnalysisResult list
member Client.RunAnalyzers: ctx: 'TContext -> Async<AnalysisResult list>
member Client.RunAnalyzers: ctx: 'TContext * analyzerPredicate: (Client.RegisteredAnalyzer<'TContext> -> bool) -> Async<AnalysisResult list>
val result: AnalysisResult
AnalysisResult.Output: Result<AnalyzerMessage list,exn>
<summary> The messages of the analyzer, or the exception it threw. </summary>
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val messages: AnalyzerMessage list
val message: AnalyzerMessage
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
AnalyzerMessage.Message: Message
<summary> A message produced by the analyzer. </summary>
Message.Code: string
Message.Message: string
union case Result.Error: ErrorValue: 'TError -> Result<'T,'TError>
val error: exn
AnalysisResult.AnalyzerName: string
property System.Exception.Message: string with get

Type something to start searching.