Skip to content

Commit

Permalink
take a predicate instead of a glob list
Browse files Browse the repository at this point in the history
  • Loading branch information
dawedawe committed Jan 5, 2024
1 parent 83931dd commit 92f49a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
11 changes: 8 additions & 3 deletions src/FSharp.Analyzers.Cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,19 @@ let main argv =
let includeAnalyzers = results.GetResult(<@ Include_Analyzers @>, [])

match excludeAnalyzers, includeAnalyzers with
| e, [] -> Exclude(Set.ofList e)
| [], i -> Include(Set.ofList i)
| e, [] ->
fun (s: string) -> e |> List.map Glob |> List.exists (fun g -> g.IsMatch s)
|> ExcludeFilter
| [], i ->
fun (s: string) -> i |> List.map Glob |> List.exists (fun g -> g.IsMatch s)
|> IncludeFilter
| _e, i ->
logger.LogWarning(
"--exclude-analyzers and --include-analyzers are mutually exclusive, ignoring --exclude-analyzers"
)

Include(Set.ofList i)
fun (s: string) -> i |> List.map Glob |> List.exists (fun g -> g.IsMatch s)
|> IncludeFilter

AssemblyLoadContext.Default.add_Resolving (fun _ctx assemblyName ->
if assemblyName.Name <> "FSharp.Core" then
Expand Down
22 changes: 6 additions & 16 deletions src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ open System.Collections.Concurrent
open System.Reflection
open System.Runtime.Loader
open System.Text.RegularExpressions
open GlobExpressions
open McMaster.NETCore.Plugins
open Microsoft.Extensions.Logging

Expand Down Expand Up @@ -129,8 +128,8 @@ module Client =
|> Seq.toList

type ExcludeInclude =
| Exclude of string Set
| Include of string Set
| ExcludeFilter of (string -> bool)
| IncludeFilter of (string -> bool)

type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TContext :> Context>(logger: ILogger) =
do TASTCollecting.logger <- logger
Expand All @@ -142,13 +141,6 @@ type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TC

member x.LoadAnalyzers(dir: string, ?excludeInclude: ExcludeInclude) : int * int =
if Directory.Exists dir then

let excludeInclude =
match excludeInclude with
| Some(Exclude excluded) -> Some(Choice1Of2(List.ofSeq excluded |> List.map Glob))
| Some(Include included) -> Some(Choice2Of2(List.ofSeq included |> List.map Glob))
| None -> None

let analyzerAssemblies =
let regex = Regex(@".*test.*\.dll$")

Expand Down Expand Up @@ -208,9 +200,8 @@ type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TC
|> Seq.collect (Client.analyzersFromType<'TAttribute, 'TContext> path)
|> Seq.filter (fun registeredAnalyzer ->
match excludeInclude with
| Some(Choice1Of2 excluded) ->
let shouldExclude =
excluded |> List.exists (fun g -> g.IsMatch registeredAnalyzer.Name)
| Some(ExcludeFilter excludeFilter) ->
let shouldExclude = excludeFilter registeredAnalyzer.Name

if shouldExclude then
logger.LogInformation(
Expand All @@ -220,9 +211,8 @@ type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TC
)

not shouldExclude
| Some(Choice2Of2 included) ->
let shouldInclude =
included |> List.exists (fun g -> g.IsMatch registeredAnalyzer.Name)
| Some(IncludeFilter includeFilter) ->
let shouldInclude = includeFilter registeredAnalyzer.Name

if shouldInclude then
logger.LogInformation(
Expand Down
8 changes: 4 additions & 4 deletions src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.Client.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ type AnalysisResult =
}

type ExcludeInclude =
/// Analyzers in this set should be ignored.
| Exclude of string Set
/// Analyzers in this set should be used exclusively, while all others are ignored.
| Include of string Set
/// A predicate function to exclude Analyzers.
| ExcludeFilter of (string -> bool)
/// A predicate function to include Analyzers exclusively, while all others are ignored.
| IncludeFilter of (string -> bool)

type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TContext :> Context> =
new: logger: ILogger -> Client<'TAttribute, 'TContext>
Expand Down
1 change: 0 additions & 1 deletion src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
<PackageReference Include="FSharp.Compiler.Service" />
<PackageReference Include="McMaster.NETCore.Plugins" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Glob" />
</ItemGroup>
</Project>

0 comments on commit 92f49a2

Please sign in to comment.