Skip to content

Commit

Permalink
Fix a false positive in LoggingTemplateMissingValuesAnalyzer (#81)
Browse files Browse the repository at this point in the history
* - Don't enforce any regex inside of braces
- Don't care about unbalanced braces

* add changelog entry and release
  • Loading branch information
dawedawe authored Feb 16, 2024
1 parent ac234ce commit 3f866a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.9.3 - 2024-02-16

### Fixed
* Fixed a false positive of LoggingTemplateMissingValuesAnalyzer. [#78](https://github.com/G-Research/fsharp-analyzers/issues/78)

## 0.9.2 - 2024-02-16

### Fixed
Expand Down
13 changes: 9 additions & 4 deletions src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ let analyze (typedTree : FSharpImplementationFileContents) =
"Microsoft.Extensions.Logging.LoggerExtensions.LogWarning"
]

let pattern = @"(?<opening>{+)[a-zA-Z0-9_-]*(?<closing>}+)"
let pattern = @"(?<opening>{+)[^{}]*(?<closing>}+)"
let regex = Regex pattern

let walker =
Expand Down Expand Up @@ -86,10 +86,15 @@ let analyze (typedTree : FSharpImplementationFileContents) =

match logString with
| Some s ->
let matches = regex.Matches s
let matches =
regex.Matches s
|> Seq.filter (fun matchItem ->
matchItem.Groups["opening"].Value.Length = matchItem.Groups["closing"].Value.Length
)
|> Seq.toArray

let escapedMatches =
Seq.sumBy
Array.sumBy
(fun (matchItem : Match) ->
let opening = matchItem.Groups["opening"]
let closing = matchItem.Groups["closing"]
Expand All @@ -98,7 +103,7 @@ let analyze (typedTree : FSharpImplementationFileContents) =
)
matches

matches.Count - escapedMatches
matches.Length - escapedMatches
| None -> 0

if
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module M

open Microsoft.Extensions.Logging

let testlog () =
use factory = LoggerFactory.Create(fun b -> b.AddConsole() |> ignore)
let logger: ILogger = factory.CreateLogger("Program")

logger.LogDebug("Blah {xxx}} foo {yyy}", 23)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module M

open Microsoft.Extensions.Logging

let testlog () =
use factory = LoggerFactory.Create(fun b -> b.AddConsole() |> ignore)
let logger: ILogger = factory.CreateLogger("Program")
let someString = "someString"
let someOtherString = "someOtherString"

logger.LogDebug("Blah {s_thing:l} foo {s_other_thing:l}", someString, someOtherString)

0 comments on commit 3f866a4

Please sign in to comment.