Skip to content

Commit b3ae50d

Browse files
dex3rCopilot
andauthored
Bugfix/simple method with parameter (#83)
* Added SimpleMethodWithParameterTest * WIP * 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 <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> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent d47c7c4 commit b3ae50d

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Text;
4+
5+
namespace EasySourceGenerators.GeneratorTests;
6+
7+
public class SimpleMethodWithParameterTests
8+
{
9+
[Test]
10+
public void SimpleMethodWithParameterTests_Test()
11+
{
12+
string source = """
13+
using EasySourceGenerators.Abstractions;
14+
15+
namespace TestNamespace;
16+
17+
public partial class SimpleMethodWithParameterClass
18+
{
19+
public partial int SimpleMethodWithParameter(int someIntParameter);
20+
21+
[GeneratesMethod(sameClassMethodName: nameof(SimpleMethodWithParameter))]
22+
private static int SimpleMethodWithParameter_Generator(int someIntParameter)
23+
{
24+
return 5;
25+
}
26+
}
27+
""";
28+
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.");
40+
}
41+
}

EasySourceGenerators.Generators/AnalyzerReleases.Unshipped.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ MSGH002 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
77
MSGH003 | GeneratesMethodGenerator | Disabled | GeneratesMethodGenerator
88
MSGH004 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
99
MSGH005 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
10-
MSGH006 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
10+
MSGH006 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
11+
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics

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
}

EasySourceGenerators.Generators/GeneratesMethodGenerator.Diagnostics.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ internal static class GeneratesMethodGeneratorDiagnostics
5353
category: Category,
5454
defaultSeverity: DiagnosticSeverity.Error,
5555
isEnabledByDefault: true);
56+
57+
internal static readonly DiagnosticDescriptor CannotUseRuntimeParameterForCompileTimeGeneratorError = new(
58+
id: "MSGH007",
59+
title: "Cannot use runtime parameter for compile-time generator",
60+
messageFormat: "Method generators cannot have any parameters, as they will be run at compile time to generate the method output value. Use MethodTemplate if you want to emit method body instead of single value.",
61+
category: Category,
62+
defaultSeverity: DiagnosticSeverity.Error,
63+
isEnabledByDefault: true);
5664
}

0 commit comments

Comments
 (0)