diff --git a/EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs b/EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs index 025e921..190889c 100644 --- a/EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs +++ b/EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs @@ -1,4 +1,8 @@ -namespace EasySourceGenerators.GeneratorTests; +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; + +namespace EasySourceGenerators.GeneratorTests; public class SimpleMethodWithParameterTests { @@ -22,6 +26,16 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter) } """; - //TODO: This should not compile and throw error MSGH007 + ImmutableArray 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."); } -} \ No newline at end of file +} diff --git a/EasySourceGenerators.Generators/GeneratesMethodGenerationPipeline.cs b/EasySourceGenerators.Generators/GeneratesMethodGenerationPipeline.cs index a52ab9d..22c8858 100644 --- a/EasySourceGenerators.Generators/GeneratesMethodGenerationPipeline.cs +++ b/EasySourceGenerators.Generators/GeneratesMethodGenerationPipeline.cs @@ -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; @@ -71,6 +72,21 @@ private static string GenerateSourceForGroup( compilation); } + List 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); } @@ -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); + } }