Skip to content

Commit

Permalink
Fix #75 Process type argument inside VisitIdentifierName and improve …
Browse files Browse the repository at this point in the history
…handling of QualifiedSyntaxName
  • Loading branch information
virzak committed May 14, 2024
1 parent 5689c41 commit da5ca52
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,20 @@ BinaryExpressionSyntax CheckNull(ExpressionSyntax expr) => BinaryExpression(
var typeString = fieldSymbol.ContainingType.ToDisplayString(GlobalDisplayFormatWithTypeParameters);
return @base.WithIdentifier(Identifier($"{typeString}.{fieldSymbol.Name}"));
}
else if (node.Parent is TypeArgumentListSyntax)
{
return ProcessType(node);
}

return @base;
}

public override SyntaxNode? VisitQualifiedName(QualifiedNameSyntax node)
{
var @base = (QualifiedNameSyntax)base.VisitQualifiedName(node)!;
return @base.Right is GenericNameSyntax ? @base.Right : (SyntaxNode)ProcessType(node);
}

public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
{
var @base = (LocalFunctionStatementSyntax)base.VisitLocalFunctionStatement(node)!;
Expand Down Expand Up @@ -804,8 +814,7 @@ bool RemoveType(TypeSyntax z, int index) =>
var newSep = RemoveSeparators(@base.Arguments.GetSeparators().ToList(), indicesToRemove);

var newArguments = SeparatedList(
RemoveAtRange(@base.Arguments, indicesToRemove)
.Select(z => z.SyntaxTree == node.SyntaxTree ? ProcessType(z) : z),
RemoveAtRange(@base.Arguments, indicesToRemove),
newSep);
return @base.WithArguments(newArguments);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/GenerationSandbox.Tests/GenerationSandbox.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<!--Suppress warninigs alphabetically and numerically-->
<NoWarn>$(NoWarn);CA1303;CA1515;CA1812;CA1822;CA1852;CA5394</NoWarn>
<NoWarn>$(NoWarn);CS0219;CS0162;CS1998;CS8603;CS8619</NoWarn>
<NoWarn>$(NoWarn);IDE0011;IDE0035;IDE0058;IDE0060</NoWarn>
<NoWarn>$(NoWarn);IDE0005;IDE0011;IDE0035;IDE0058;IDE0060;IDE0065</NoWarn>
<NoWarn>$(NoWarn);RS1035</NoWarn>
<NoWarn>$(NoWarn);SA1201;SA1400;SA1402;SA1404</NoWarn>
<NoWarn>$(NoWarn);SA1200;SA1201;SA1400;SA1402;SA1403;SA1404</NoWarn>
<ImplicitUsings>false</ImplicitUsings>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: Test.Class.MethodAsync.g.cs
var dict = new global::System.Collections.Generic.Dictionary<global::System.DateTime, global::System.Collections.Generic.List<int>>();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: Test.Class.MethodAsync.g.cs
global::System.Collections.Generic.HashSet<byte> z = null!;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//HintName: System.Class.MethodAsync.g.cs
// <auto-generated/>
#nullable enable
namespace System;
public partial class Class
{
public void Method()
{
global::System.Security.Cryptography.CryptographicException z = null!;
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//HintName: Test.Class.WithAction.g.cs
public static void WithAction(global::System.Action<Point, global::System.Collections.Generic.IEnumerable<global::System.Drawing.Point>>? action) { }
public static void WithAction(global::System.Action<global::System.Drawing.Point, global::System.Collections.Generic.IEnumerable<global::System.Drawing.Point>>? action) { }
28 changes: 28 additions & 0 deletions tests/Generator.Tests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public Task NullableForeach()
"""
.Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task NestedGenerics()
=> "var dict = new Dictionary<DateTime, List<int>>();"
.Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task TestTypes() => $$"""
String myStr;
Expand Down Expand Up @@ -134,6 +139,29 @@ public Task HandleNameOf()
=> "_ = nameof(Stream);"
.Verify(sourceType: SourceType.MethodBody);

[Fact]
public Task QualifiedGenericName()
=> "System.Collections.Generic.HashSet<byte> z = null!;"
.Verify(sourceType: SourceType.MethodBody);

[Theory]
[InlineData("")]
[InlineData("System.")]
[InlineData("global::System.")]
public Task QualifiedNonGenericName(string prefix)
=> $$"""
namespace System;
public partial class Class
{
[CreateSyncVersion]
public async Task MethodAsync()
{
{{prefix}}Security.Cryptography.CryptographicException z = null!;
}
}
""".Verify(disableUnique: true, sourceType: SourceType.Full);

[Fact]
public Task HandleAsCast()
=> "_ = new object() as Stream;"
Expand Down

0 comments on commit da5ca52

Please sign in to comment.