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
20 changes: 17 additions & 3 deletions EasySourceGenerators.GeneratorTests/SimpleMethodWithParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace EasySourceGenerators.GeneratorTests;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace EasySourceGenerators.GeneratorTests;

public class SimpleMethodWithParameterTests
{
Expand All @@ -22,6 +26,16 @@ private static int SimpleMethodWithParameter_Generator(int someIntParameter)
}
""";

//TODO: This should not compile and throw error MSGH007
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
@@ -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);
}
}
Loading