Skip to content

Commit

Permalink
fix: diagnostic converter when location is empty (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwoods-7 committed Sep 12, 2024
1 parent 3c75435 commit aac665d
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 19 deletions.
15 changes: 9 additions & 6 deletions src/SampleGenerator/HelloWorldVbGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ End Module
DiagnosticSeverity.Info,
isEnabledByDefault: true);

var location = Location.Create(
Path.Combine("dir", "theFile.vb"),
new(1, 2),
new(
new(1, 2),
new(3, 4)));
var location = GetLocation();
var diagnostic = Diagnostic.Create(descriptor, location, "hello world generator");
context.ReportDiagnostic(diagnostic);
}

public void Initialize(GeneratorInitializationContext context)
{
}

protected virtual Location? GetLocation() =>
Location.Create(
Path.Combine("dir", "theFile.vb"),
new(1, 2),
new(
new(1, 2),
new(3, 4)));
}
8 changes: 8 additions & 0 deletions src/SampleGenerator/HelloWorldVbGeneratorWithoutLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.CodeAnalysis;

[Generator]
public class HelloWorldVbGeneratorWithoutLocation :
HelloWorldVbGenerator
{
protected override Location? GetLocation() => null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: helloWorld.vb
Imports System

Public Module HelloWorld
Public Sub SayHello()
Console.WriteLine("Hello from generated code!")
End Sub
End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//HintName: helper.vb
Imports System
Public Module Helper
Public Sub Method()
End Sub
End Module
13 changes: 13 additions & 0 deletions src/Tests/SampleVbTest.DriverWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
}
]
}
36 changes: 36 additions & 0 deletions src/Tests/SampleVbTest.RunResultWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
Generator: {},
GeneratedSources: [
{
HintName: helper.vb,
Source:
Imports System
Public Module Helper
Public Sub Method()
End Sub
End Module
},
{
HintName: helloWorld.vb,
Source:
Imports System

Public Module HelloWorld
Public Sub SayHello()
Console.WriteLine("Hello from generated code!")
End Sub
End Module
}
],
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: helloWorld.vb
Imports System

Public Module HelloWorld
Public Sub SayHello()
Console.WriteLine("Hello from generated code!")
End Sub
End Module
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//HintName: helper.vb
Imports System
Public Module Helper
Public Sub Method()
End Sub
End Module
13 changes: 13 additions & 0 deletions src/Tests/SampleVbTest.RunResultsWithoutLocation.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
Diagnostics: [
{
Id: theId,
Title: the title,
Severity: Info,
WarningLevel: 1,
MessageFormat: the message from {0},
Message: the message from hello world generator,
Category: the category
}
]
}
37 changes: 32 additions & 5 deletions src/Tests/SampleVbTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ public class SampleVbTest
[Fact]
public Task Driver()
{
var driver = BuildDriver();
var driver = BuildDriver<HelloWorldVbGenerator>();

return Verify(driver);
}

[Fact]
public Task RunResults()
{
var driver = BuildDriver();
var driver = BuildDriver<HelloWorldVbGenerator>();

var results = driver.GetRunResult();
return Verify(results);
Expand All @@ -22,16 +22,43 @@ public Task RunResults()
[Fact]
public Task RunResult()
{
var driver = BuildDriver();
var driver = BuildDriver<HelloWorldVbGenerator>();

var result = driver.GetRunResult().Results.Single();
return Verify(result);
}

static GeneratorDriver BuildDriver()
[Fact]
public Task DriverWithoutLocation()
{
var driver = BuildDriver<HelloWorldVbGeneratorWithoutLocation>();

return Verify(driver);
}

[Fact]
public Task RunResultsWithoutLocation()
{
var driver = BuildDriver<HelloWorldVbGeneratorWithoutLocation>();

var results = driver.GetRunResult();
return Verify(results);
}

[Fact]
public Task RunResultWithoutLocation()
{
var driver = BuildDriver<HelloWorldVbGeneratorWithoutLocation>();

var result = driver.GetRunResult().Results.Single();
return Verify(result);
}

static GeneratorDriver BuildDriver<T>()
where T : ISourceGenerator, new()
{
var compilation = VisualBasicCompilation.Create("name");
var generator = new HelloWorldVbGenerator();
var generator = new T();

var vbGeneratorDriver = VisualBasicGeneratorDriver.Create([generator]);
return vbGeneratorDriver.RunGenerators(compilation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public override void Write(VerifyJsonWriter writer, Diagnostic value)
writer.WriteMember(value, descriptor.Title, "Title");
writer.WriteMember(value, value.Severity, "Severity");
writer.WriteMember(value, value.WarningLevel, "WarningLevel");
writer.WriteMember(value, value.Location, "Location");
if (value.Location != Location.None)
{
writer.WriteMember(value, value.Location, "Location");
}
var description = descriptor.Description.ToString(CultureInfo.InvariantCulture);
if (!string.IsNullOrWhiteSpace(description))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
class FileLinePositionSpanConverter :
WriteOnlyJsonConverter<FileLinePositionSpan>
{
public override void Write(VerifyJsonWriter writer, FileLinePositionSpan value)
{
if (value.IsValid)
{
writer.WriteValue($"{value.Path.Replace('/', '\\')}: {value.Span}");
}
}
public override void Write(VerifyJsonWriter writer, FileLinePositionSpan value) =>
writer.WriteValue(value.IsValid ? $"{value.Path.Replace('/', '\\')}: {value.Span}" : "");
}

0 comments on commit aac665d

Please sign in to comment.