Skip to content

Commit 1761bb7

Browse files
authored
Merge pull request #135 from pierre3/master
fix IndexOutOfRangeException when ConsoleAppFilterAttribute is defined above CommandAttribute
2 parents eb00116 + 1f5b765 commit 1761bb7

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/ConsoleAppFramework/Parser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
144144
var commandAttribute = x.GetAttributes().FirstOrDefault(x => x.AttributeClass?.Name == "CommandAttribute");
145145
if (commandAttribute != null)
146146
{
147-
commandName = (x.GetAttributes()[0].ConstructorArguments[0].Value as string)!;
147+
commandName = (commandAttribute.ConstructorArguments[0].Value as string)!;
148148
}
149149
else
150150
{

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,46 @@ public void Do()
241241

242242
verifier.Execute(code, "nomunomu", "yeah");
243243
}
244-
}
245244

245+
[Fact]
246+
public void CommandAttrWithFilter()
247+
{
248+
var code = """
249+
var builder = ConsoleApp.Create();
250+
builder.Add<MyClass>();
251+
builder.Run(args);
252+
253+
public class MyClass()
254+
{
255+
[ConsoleAppFilter<NopFilter1>]
256+
[Command("nomunomu")]
257+
[ConsoleAppFilter<NopFilter2>]
258+
public void Do()
259+
{
260+
Console.Write("command");
261+
}
262+
}
246263
264+
internal class NopFilter1(ConsoleAppFilter next)
265+
: ConsoleAppFilter(next)
266+
{
267+
public override Task InvokeAsync(ConsoleAppContext context,CancellationToken cancellationToken)
268+
{
269+
Console.Write("filter1-");
270+
return Next.InvokeAsync(context, cancellationToken);
271+
}
272+
}
273+
internal class NopFilter2(ConsoleAppFilter next)
274+
: ConsoleAppFilter(next)
275+
{
276+
public override Task InvokeAsync(ConsoleAppContext context,CancellationToken cancellationToken)
277+
{
278+
Console.Write("filter2-");
279+
return Next.InvokeAsync(context, cancellationToken);
280+
}
281+
}
282+
""";
247283

284+
verifier.Execute(code, "nomunomu", "filter1-filter2-command");
285+
}
286+
}

0 commit comments

Comments
 (0)