From 6e48be3cdf6eee80a48f98e9e2615ed2aecc46fe Mon Sep 17 00:00:00 2001 From: Jason Woods Date: Wed, 11 Sep 2024 15:41:41 -0500 Subject: [PATCH] fix: nullReferenceException thrown when FileLinePositionSpan does not contain a path (#289) --- ...ests.FileLinePositionSpanConverter_Empty.verified.txt | 1 + src/Tests/Tests.cs | 4 ++++ .../Converters/FileLinePositionSpanConverter.cs | 9 +++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/Tests/Tests.FileLinePositionSpanConverter_Empty.verified.txt diff --git a/src/Tests/Tests.FileLinePositionSpanConverter_Empty.verified.txt b/src/Tests/Tests.FileLinePositionSpanConverter_Empty.verified.txt new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/src/Tests/Tests.FileLinePositionSpanConverter_Empty.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Tests/Tests.cs b/src/Tests/Tests.cs index 92d7015..093f777 100644 --- a/src/Tests/Tests.cs +++ b/src/Tests/Tests.cs @@ -21,6 +21,10 @@ public Task LocalizableStringConverter() => public Task FileLinePositionSpanConverter() => Verify(new FileLinePositionSpan("the path", new(1,2), new(2,4))); + [Fact] + public Task FileLinePositionSpanConverter_Empty() => + Verify(new FileLinePositionSpan()); + class LocalizableStringImp : LocalizableString { diff --git a/src/Verify.SourceGenerators/Converters/FileLinePositionSpanConverter.cs b/src/Verify.SourceGenerators/Converters/FileLinePositionSpanConverter.cs index fb3bf25..0ef3567 100644 --- a/src/Verify.SourceGenerators/Converters/FileLinePositionSpanConverter.cs +++ b/src/Verify.SourceGenerators/Converters/FileLinePositionSpanConverter.cs @@ -1,6 +1,11 @@ class FileLinePositionSpanConverter : WriteOnlyJsonConverter { - public override void Write(VerifyJsonWriter writer, FileLinePositionSpan value) => - writer.WriteValue($"{value.Path.Replace('/', '\\')}: {value.Span}"); + public override void Write(VerifyJsonWriter writer, FileLinePositionSpan value) + { + if (value.IsValid) + { + writer.WriteValue($"{value.Path.Replace('/', '\\')}: {value.Span}"); + } + } } \ No newline at end of file