From 65bc4063c2cb3b5080f1f908c4170b29b0a79a68 Mon Sep 17 00:00:00 2001 From: dawe Date: Wed, 10 Jan 2024 12:16:13 +0100 Subject: [PATCH] - show discrete name of used ILogger method - add tests for explicit value array --- .../LoggingTemplateMissingValuesAnalyzer.fs | 9 ++++----- .../Warnings for missing in explicit array.fs | 9 +++++++++ .../Warnings for missing in explicit array.fs.expected | 1 + .../Warnings for one missing.fs.expected | 2 +- .../negative/No warnings for explicit array.fs | 9 +++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs create mode 100644 tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs.expected create mode 100644 tests/data/loggingtemplatemissingvalues/negative/No warnings for explicit array.fs diff --git a/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs b/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs index 6922f26..266e0ec 100644 --- a/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs +++ b/src/FSharp.Analyzers/LoggingTemplateMissingValuesAnalyzer.fs @@ -20,7 +20,7 @@ let (|StringConst|_|) (e : FSharpExpr) = let analyze (typedTree : FSharpImplementationFileContents) = - let state = ResizeArray () + let state = ResizeArray () let namesToWarnAbout = set @@ -82,17 +82,16 @@ let analyze (typedTree : FSharpImplementationFileContents) = && Set.contains name namesToWarnAbout && provided <> expected then - state.Add range + state.Add (range, m.DisplayName) } walkTast walker typedTree [ - for range in state do + for range, name in state do { Type = "LoggingTemplateMissingValuesAnalyzer" - Message = - "The given values in your call to ILogger.Log{Warning, Error, ...} don't match the expected templated args." + Message = $"The given values in your call to ILogger.%s{name} don't match the expected templated args." Code = Code Severity = Warning Range = range diff --git a/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs b/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs new file mode 100644 index 0000000..bb2e8fc --- /dev/null +++ b/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs @@ -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.LogInformation("Foo {one} {two}", [| box 1 |]) diff --git a/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs.expected b/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs.expected new file mode 100644 index 0000000..1e652fe --- /dev/null +++ b/tests/data/loggingtemplatemissingvalues/Warnings for missing in explicit array.fs.expected @@ -0,0 +1 @@ +GRA-LOGTEMPLMISSVALS-001 | Warning | (9,8 - 9,61) | The given values in your call to ILogger.LogInformation don't match the expected templated args. | [] diff --git a/tests/data/loggingtemplatemissingvalues/Warnings for one missing.fs.expected b/tests/data/loggingtemplatemissingvalues/Warnings for one missing.fs.expected index 7910ecc..2ce7c2a 100644 --- a/tests/data/loggingtemplatemissingvalues/Warnings for one missing.fs.expected +++ b/tests/data/loggingtemplatemissingvalues/Warnings for one missing.fs.expected @@ -1 +1 @@ -GRA-LOGTEMPLMISSVALS-001 | Warning | (9,8 - 9,67) | The given values in your call to ILogger.Log{Warning, Error, ...} don't match the expected templated args. | [] +GRA-LOGTEMPLMISSVALS-001 | Warning | (9,8 - 9,67) | The given values in your call to ILogger.Log don't match the expected templated args. | [] diff --git a/tests/data/loggingtemplatemissingvalues/negative/No warnings for explicit array.fs b/tests/data/loggingtemplatemissingvalues/negative/No warnings for explicit array.fs new file mode 100644 index 0000000..6bfb880 --- /dev/null +++ b/tests/data/loggingtemplatemissingvalues/negative/No warnings for explicit array.fs @@ -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.LogInformation("Foo {one} {two}", [| box 1; box 2 |])