Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace EasySourceGenerators.GeneratorTests;

public class SimpleMethodWithParameterTests
{
[Test]
public void SimpleMethodWithParameterTests_Test()
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test naming is inconsistent with the rest of the test suite (which uses descriptive names like GeneratesMethod_WithNonStaticMethod_EmitsMSGH002). Consider renaming SimpleMethodWithParameterTests_Test to describe the behavior under test (e.g., that a parameterized simple generator emits MSGH007 and highlights only the signature).

Suggested change
public void SimpleMethodWithParameterTests_Test()
public void GeneratesMethod_WithParameter_EmitsMSGH007AndHighlightsOnlySignature()

Copilot uses AI. Check for mistakes.
{
string source = """
using EasySourceGenerators.Abstractions;

namespace TestNamespace;

public partial class SimpleMethodWithParameterClass
{
public partial int SimpleMethodWithParameter(int someIntParameter);

[GeneratesMethod(sameClassMethodName: nameof(SimpleMethodWithParameter))]
private static int SimpleMethodWithParameter_Generator(int someIntParameter)
{
return 5;
}
}
""";

ImmutableArray<Diagnostic> diagnostics = GeneratorTestHelper.GetGeneratorOnlyDiagnostics(source);

Diagnostic? msgh007 = diagnostics.FirstOrDefault(diagnostic => diagnostic.Id == "MSGH007");
Assert.That(msgh007, Is.Not.Null, "Expected MSGH007 for simple generator method with runtime parameter.");
Assert.That(msgh007!.Location.IsInSource, Is.True, "MSGH007 should point to generator source.");

TextSpan span = msgh007.Location.SourceSpan;
string highlightedCode = source.Substring(span.Start, span.Length);
Assert.That(highlightedCode, Does.Contain("SimpleMethodWithParameter_Generator(int someIntParameter)"));
Assert.That(highlightedCode, Does.Not.Contain("return 5;"),
"MSGH007 should highlight the generator method signature, not the method body.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ MSGH002 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
MSGH003 | GeneratesMethodGenerator | Disabled | GeneratesMethodGenerator
MSGH004 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
MSGH005 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
MSGH006 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
MSGH006 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnalyzerReleases.Unshipped.md “Notes” column is inconsistent for MSGH007: other MSGH00x entries use GeneratesMethodGenerator, but MSGH007 uses GeneratesMethodGeneratorDiagnostics. If the convention is to list the analyzer/generator name, align MSGH007 with the existing entries for consistency.

Suggested change
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGeneratorDiagnostics
MSGH007 | GeneratesMethodGenerator | Error | GeneratesMethodGenerator

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Immutable;
using static EasySourceGenerators.Generators.Consts;

Expand Down Expand Up @@ -71,6 +72,21 @@ private static string GenerateSourceForGroup(
compilation);
}

List<GeneratesMethodGenerationTarget> methodsWithParameters = methods
.Where(method => method.Symbol.Parameters.Length > 0)
.ToList();
if (methodsWithParameters.Count > 0)
{
foreach (GeneratesMethodGenerationTarget methodWithParameters in methodsWithParameters)
{
context.ReportDiagnostic(Diagnostic.Create(
GeneratesMethodGeneratorDiagnostics.CannotUseRuntimeParameterForCompileTimeGeneratorError,
GetMethodSignatureLocation(methodWithParameters.Syntax)));
}

return string.Empty;
}

return GenerateFromSimplePattern(context, firstMethod, compilation);
}

Expand Down Expand Up @@ -105,4 +121,10 @@ private static bool HasAttribute(IMethodSymbol methodSymbol, string fullAttribut
return methodSymbol.GetAttributes()
.Any(attribute => attribute.AttributeClass?.ToDisplayString() == fullAttributeTypeName);
}

private static Location GetMethodSignatureLocation(MethodDeclarationSyntax methodSyntax)
{
TextSpan signatureSpan = TextSpan.FromBounds(methodSyntax.SpanStart, methodSyntax.ParameterList.Span.End);
return Location.Create(methodSyntax.SyntaxTree, signatureSpan);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ internal static class GeneratesMethodGeneratorDiagnostics
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

internal static readonly DiagnosticDescriptor CannotUseRuntimeParameterForCompileTimeGeneratorError = new(
id: "MSGH007",
title: "Cannot use runtime parameter for compile-time generator",
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.",
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diagnostic MSGH007 message suggests using MethodTemplate, but there’s no MethodTemplate type/API in this repo (search only finds this string). This will mislead users; update the message to reference an existing alternative (e.g., the fluent pattern returning IMethodImplementationGenerator / Generate.Method() / MethodBuilder).

Suggested change
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.",
messageFormat: "Method generators cannot have any parameters, as they will be run at compile time to generate the method output value. Use the fluent method-generation API (e.g., Generate.Method() / MethodBuilder) if you want to emit a method body instead of a single value.",

Copilot uses AI. Check for mistakes.
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
Loading