Skip to content

Commit 980470c

Browse files
Copilotdex3r
andauthored
Enforce MSGH007 for simple generators with parameters and finish SimpleMethodWithParameter test (#82)
* Initial plan * Add MSGH007 validation for simple generators with parameters Co-authored-by: dex3r <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: dex3r <[email protected]>
1 parent 4dc9cc3 commit 980470c

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace EasySourceGenerators.GeneratorTests;
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Text;
4+
5+
namespace EasySourceGenerators.GeneratorTests;
26

37
public class SimpleMethodWithParameterTests
48
{
@@ -22,6 +26,16 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter)
2226
}
2327
""";
2428

25-
//TODO: This should not compile and throw error MSGH007
29+
ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);
30+
31+
Diagnostic? msgh007 = diagnostics.FirstOrDefault(diagnostic => diagnostic.Id == "MSGH007");
32+
Assert.That(msgh007, Is.Not.Null, "Expected MSGH007 for simple generator method with runtime parameter.");
33+
Assert.That(msgh007!.Location.IsInSource, Is.True, "MSGH007 should point to generator source.");
34+
35+
TextSpan span = msgh007.Location.SourceSpan;
36+
string highlightedCode = source.Substring(span.Start, span.Length);
37+
Assert.That(highlightedCode, Does.Contain("SimpleMethodWithParameter_Generator(int someIntParameter)"));
38+
Assert.That(highlightedCode, Does.Not.Contain("return 5;"),
39+
"MSGH007 should highlight the generator method signature, not the method body.");
2640
}
27-
}
41+
}

EasySourceGenerators.Generators/GeneratesMethodGenerationPipeline.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using Microsoft.CodeAnalysis.Text;
34
using System.Collections.Immutable;
45
using static EasySourceGenerators.Generators.Consts;
56

@@ -71,6 +72,21 @@ private static string GenerateSourceForGroup(
7172
compilation);
7273
}
7374

75+
List<GeneratesMethodGenerationTarget> methodsWithParameters = methods
76+
.Where(method => method.Symbol.Parameters.Length > 0)
77+
.ToList();
78+
if (methodsWithParameters.Count > 0)
79+
{
80+
foreach (GeneratesMethodGenerationTarget methodWithParameters in methodsWithParameters)
81+
{
82+
context.ReportDiagnostic(Diagnostic.Create(
83+
GeneratesMethodGeneratorDiagnostics.CannotUseRuntimeParameterForCompileTimeGeneratorError,
84+
GetMethodSignatureLocation(methodWithParameters.Syntax)));
85+
}
86+
87+
return string.Empty;
88+
}
89+
7490
return GenerateFromSimplePattern(context, firstMethod, compilation);
7591
}
7692

@@ -105,4 +121,10 @@ private static bool HasAttribute(IMethodSymbol methodSymbol, string fullAttribut
105121
return methodSymbol.GetAttributes()
106122
.Any(attribute => attribute.AttributeClass?.ToDisplayString() == fullAttributeTypeName);
107123
}
124+
125+
private static Location GetMethodSignatureLocation(MethodDeclarationSyntax methodSyntax)
126+
{
127+
TextSpan signatureSpan = TextSpan.FromBounds(methodSyntax.SpanStart, methodSyntax.ParameterList.Span.End);
128+
return Location.Create(methodSyntax.SyntaxTree, signatureSpan);
129+
}
108130
}

0 commit comments

Comments
 (0)