Skip to content

Commit 1932c75

Browse files
committed
Validate that commands have unique names
1 parent 55a1655 commit 1932c75

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

CliFx.Generators/CommandDescriptorGenerator.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
105105
.Collect(),
106106
static (ctx, commands) =>
107107
{
108+
// Validate that all commands have unique names
109+
foreach (var (i, first) in commands.Index())
110+
{
111+
foreach (var second in commands.Skip(i + 1))
112+
{
113+
if (
114+
string.Equals(
115+
first.Name,
116+
second.Name,
117+
System.StringComparison.OrdinalIgnoreCase
118+
)
119+
)
120+
{
121+
ctx.ReportDiagnostic(
122+
Diagnostic.Create(
123+
DiagnosticDescriptors.CommandMustHaveUniqueName,
124+
second.Type.Locations.FirstOrDefault(),
125+
second.Type.Name,
126+
first.Type.Name,
127+
first.Name ?? "<default>"
128+
)
129+
);
130+
}
131+
}
132+
}
133+
108134
ctx.AddSource(
109135
"CommandRegistrations.g.cs",
110136
SourceText.From(CommandRegistration.Emit(commands), Encoding.UTF8)

CliFx.Generators/DiagnosticDescriptors.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static class DiagnosticDescriptors
2626
true
2727
);
2828

29+
public static DiagnosticDescriptor CommandMustHaveUniqueName { get; } =
30+
new(
31+
$"{nameof(CliFx)}_{nameof(CommandMustHaveUniqueName)}",
32+
"Command must have a unique name",
33+
"Command bound to type '{0}' has the same name as the command bound to type '{1}': '{2}'. All commands registered in an application must have unique names (comparison IS NOT case-sensitive).",
34+
"CliFx",
35+
DiagnosticSeverity.Error,
36+
true
37+
);
38+
2939
// Inputs
3040

3141
public static DiagnosticDescriptor CommandInputConverterNotInferrable { get; } =

0 commit comments

Comments
 (0)