Skip to content

Commit a46b781

Browse files
committed
Add verify parameter name mismatch
1 parent 76609ad commit a46b781

File tree

5 files changed

+40
-43
lines changed

5 files changed

+40
-43
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class MyCommands
3636
/// <summary>
3737
///
3838
/// </summary>
39-
/// <param name="msg2">foobarbaz!</param>
39+
/// <param name="msg">foobarbaz!</param>
4040
[Command("Error1")]
4141
public void Error1(string msg = @"\")
4242
{

src/ConsoleAppFramework/DiagnosticDescriptors.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,8 @@ public static DiagnosticDescriptor Create(int id, string title, string messageFo
114114
public static DiagnosticDescriptor DefinedInOtherProject { get; } = Create(
115115
14,
116116
"ConsoleAppFramework cannot register type/method in another project outside the SourceGenerator referenced project.");
117+
118+
public static DiagnosticDescriptor DocCommentParameterNameNotMatched { get; } = Create(
119+
15,
120+
"Document Comment parameter name '{0}' does not match method parameter name.");
117121
}

src/ConsoleAppFramework/FunctionSyntax.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/ConsoleAppFramework/Parser.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,19 @@ internal class Parser(DiagnosticReporter context, SyntaxNode node, SemanticModel
481481
return null;
482482
}
483483

484+
// validate parametersymbols
485+
if (parameterDescriptions != null)
486+
{
487+
foreach (var item in parameterDescriptions)
488+
{
489+
if (!methodSymbol.Parameters.Any(x => x.Name == item.Key))
490+
{
491+
context.ReportDiagnostic(DiagnosticDescriptors.DocCommentParameterNameNotMatched, methodSymbol.Locations[0], item.Key);
492+
return null;
493+
}
494+
}
495+
}
496+
484497
var parsableIndex = 0;
485498
var parameters = methodSymbol.Parameters
486499
.Select(x =>

tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,5 +434,27 @@ public interface IFoo
434434
}
435435
""", "app.Add<IFoo>()");
436436
}
437+
438+
[Fact]
439+
public void DocCommentName()
440+
{
441+
verifier.Verify(15, """
442+
var app = ConsoleApp.Create();
443+
app.Add<Foo>();
444+
app.Run(args);
445+
446+
public class Foo
447+
{
448+
/// <param name="nomsg">foobarbaz!</param>
449+
[Command("Error1")]
450+
public void Bar(string msg)
451+
{
452+
Console.WriteLine(msg);
453+
}
454+
}
455+
456+
""", "Bar");
457+
458+
}
437459
}
438460

0 commit comments

Comments
 (0)