Skip to content

Commit

Permalink
Simplify DiagnosticConverter (#292)
Browse files Browse the repository at this point in the history
Only a few fields on Diagnostic are its own, and the rest can be delegated to the existing DiagnosticDescriptor renderer.

This both simplifies the implementation, and makes Diagnostic's own output have clearer separation between own and "inherited" information.
  • Loading branch information
RReverser authored Sep 18, 2024
1 parent 1cfd06a commit 5ee50d8
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 96 deletions.
18 changes: 11 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,23 @@ An info file containing all metadata about the current state. eg any Diagnostics
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
```
<sup><a href='/src/Tests/SampleTest.Driver.verified.txt#L1-L14' title='Snippet source file'>snippet source</a> | <a href='#snippet-SampleTest.Driver.verified.txt' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/SampleTest.Driver.verified.txt#L1-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-SampleTest.Driver.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
16 changes: 10 additions & 6 deletions src/Tests/IgnoreTest.IgnoreFile.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/IgnoreTest.SettingsIgnoreFile.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleTest.Driver.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleTest.RunResult.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public static class HelloWorld
],
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleTest.RunResults.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleVbTest.Driver.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.vb: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
14 changes: 9 additions & 5 deletions src/Tests/SampleVbTest.DriverWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Message: the message from hello world generator,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleVbTest.RunResult.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ End Module
],
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.vb: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
14 changes: 9 additions & 5 deletions src/Tests/SampleVbTest.RunResultWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ End Module
],
Diagnostics: [
{
Id: theId,
Title: the title,
Message: the message from hello world generator,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/SampleVbTest.RunResults.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.vb: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
14 changes: 9 additions & 5 deletions src/Tests/SampleVbTest.RunResultsWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Message: the message from hello world generator,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
16 changes: 10 additions & 6 deletions src/Tests/ScrubTest.ScrubLines.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
Location: dir\theFile.cs: (1,2)-(3,4),
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
Severity: Info,
WarningLevel: 1,
Descriptor: {
Id: theId,
Title: the title,
MessageFormat: the message from {0},
Category: the category,
DefaultSeverity: Info,
IsEnabledByDefault: true
}
}
]
}
26 changes: 6 additions & 20 deletions src/Verify.SourceGenerators/Converters/DiagnosticConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,17 @@
public override void Write(VerifyJsonWriter writer, Diagnostic value)
{
writer.WriteStartObject();
writer.WriteMember(value, value.Id, "Id");
var descriptor = value.Descriptor;
writer.WriteMember(value, descriptor.Title, "Title");
writer.WriteMember(value, value.Severity, "Severity");
writer.WriteMember(value, value.WarningLevel, "WarningLevel");
if (value.Location != Location.None)
{
writer.WriteMember(value, value.Location, "Location");
}
var description = descriptor.Description.ToString(CultureInfo.InvariantCulture);
if (!string.IsNullOrWhiteSpace(description))
{
writer.WriteMember(value, description, "Description");
}

var help = descriptor.HelpLinkUri;
if (!string.IsNullOrWhiteSpace(help))
writer.WriteMember(value, value.GetMessage(CultureInfo.InvariantCulture), "Message");
writer.WriteMember(value, value.Severity, "Severity");
if (value.WarningLevel != 0)
{
writer.WriteMember(value, help, "HelpLink");
writer.WriteMember(value, value.WarningLevel, "WarningLevel");
}

writer.WriteMember(value, descriptor.MessageFormat, "MessageFormat");
writer.WriteMember(value, value.GetMessage(CultureInfo.InvariantCulture), "Message");
writer.WriteMember(value, descriptor.Category, "Category");
writer.WriteMember(value, descriptor.CustomTags, "CustomTags");
writer.WriteMember(value, value.Descriptor, "Descriptor");
writer.WriteEndObject();
}
}
}

0 comments on commit 5ee50d8

Please sign in to comment.