Skip to content

Commit 2137024

Browse files
Copilotdex3r
andauthored
Mark generated source as auto-generated and suppress warnings in .g.cs output (#51)
* Initial plan * Mark generated files and suppress warnings Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent 152d43d commit 2137024

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

EasySourceGenerators.Generators/GeneratesMethodPatternSourceBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ private static string GenerateSwitchMethodSource(
261261

262262
private static void AppendNamespaceAndTypeHeader(StringBuilder builder, INamedTypeSymbol containingType, IMethodSymbol partialMethod)
263263
{
264+
builder.AppendLine("// <auto-generated/>");
265+
builder.AppendLine($"// Generated by {typeof(GeneratesMethodGenerator).FullName} for method '{partialMethod.Name}'.");
266+
builder.AppendLine("#pragma warning disable");
267+
builder.AppendLine();
268+
264269
string? namespaceName = containingType.ContainingNamespace?.IsGlobalNamespace == false
265270
? containingType.ContainingNamespace.ToDisplayString()
266271
: null;

EasySourceGenerators.Tests/BoolSwitchKeyTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public static partial string GetBoolLabel(bool flag)
3737

3838
Assert.That(generatedCode, Is.EqualTo(expectedCode));
3939
}
40+
41+
[Test]
42+
public void BoolSwitchKey_GeneratedCode_HasAutoGeneratedHeaderAndWarningSuppression()
43+
{
44+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCodeRaw("TestBoolSwitchClass_GetBoolLabel.g.cs");
45+
46+
Assert.That(generatedCode, Does.StartWith("// <auto-generated/>"));
47+
Assert.That(generatedCode, Does.Contain("Generated by EasySourceGenerators.Generators.GeneratesMethodGenerator for method 'GetBoolLabel'"));
48+
Assert.That(generatedCode, Does.Contain("#pragma warning disable"));
49+
}
4050
}
4151

4252
public static partial class TestBoolSwitchClass

EasySourceGenerators.Tests/GeneratedCodeTestHelper.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,40 @@ internal static class GeneratedCodeTestHelper
55
private const char UnicodeBom = '\uFEFF';
66

77
internal static string ReadGeneratedCode(string generatedFileName)
8+
{
9+
string rawCode = ReadGeneratedCodeRaw(generatedFileName);
10+
return StripGeneratedPreamble(rawCode);
11+
}
12+
13+
internal static string ReadGeneratedCodeRaw(string generatedFileName)
814
{
915
string generatedCodePath = GetGeneratedCodePath(generatedFileName);
1016
return File.ReadAllText(generatedCodePath).TrimStart(UnicodeBom).ReplaceLineEndings("\n").TrimEnd();
1117
}
1218

19+
private static string StripGeneratedPreamble(string generatedCode)
20+
{
21+
string[] lines = generatedCode.Split('\n');
22+
int index = 0;
23+
24+
while (index < lines.Length)
25+
{
26+
string currentLine = lines[index];
27+
bool isGeneratedPreambleLine = currentLine.StartsWith("//", StringComparison.Ordinal)
28+
|| currentLine.StartsWith("#pragma warning disable", StringComparison.Ordinal)
29+
|| string.IsNullOrWhiteSpace(currentLine);
30+
31+
if (!isGeneratedPreambleLine)
32+
{
33+
break;
34+
}
35+
36+
index++;
37+
}
38+
39+
return string.Join('\n', lines[index..]).TrimEnd();
40+
}
41+
1342
private static string GetGeneratedCodePath(string generatedFileName)
1443
{
1544
string projectDirectory = FindProjectDirectory();

0 commit comments

Comments
 (0)