Skip to content

Commit

Permalink
Fix #40 Fully qualify type in a switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Jan 13, 2024
1 parent 5924db1 commit c573fde
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ bool ShouldRemoveAttribute(AttributeSyntax attributeSyntax)
return @base.WithType(ProcessType(node.Type)).WithTriviaFrom(@base);
}

public override SyntaxNode? VisitDeclarationPattern(DeclarationPatternSyntax node)
{
var @base = (DeclarationPatternSyntax)base.VisitDeclarationPattern(node)!;
return @base.WithType(ProcessType(node.Type)).WithTriviaFrom(@base);
}

public override SyntaxNode? VisitTypeOfExpression(TypeOfExpressionSyntax node)
{
var @base = (TypeOfExpressionSyntax)base.VisitTypeOfExpression(node)!;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//HintName: Test.Class.SwitchAsync.g.cs
public void Switch(global::System.IO.Stream stream)
{
var s = stream switch
{
global::System.IO.FileStream fs => fs,
_ => throw new global::System.InvalidOperationException("No"),
};
}
13 changes: 13 additions & 0 deletions tests/Generator.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ public Task HandleTypeOf()
public Task HandleNameOf()
=> "_ = nameof(Stream);"
.Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task SwitchType() => """
[CreateSyncVersion]
public async Task SwitchAsync(Stream stream)
{
var s = stream switch
{
FileStream fs => fs,
_ => throw new InvalidOperationException("No"),
};
}
""".Verify();
}

0 comments on commit c573fde

Please sign in to comment.