Skip to content

Commit 73dbffa

Browse files
committed
Support aliases for any type
1 parent 80e1988 commit 73dbffa

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Quoter.Tests/Quoter.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="xunit" Version="2.4.2" />
10-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
9+
<PackageReference Include="xunit" Version="2.9.2" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1111
<PrivateAssets>all</PrivateAssets>
1212
<IncludeAssets>runtime;build;native;contentFiles;analyzers</IncludeAssets>
1313
</PackageReference>

src/Quoter.Tests/Tests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,13 @@ public void TestRecordStruct()
867867
nodeKind: NodeKind.MemberDeclaration);
868868
}
869869

870+
[Fact]
871+
public void TestIssue85()
872+
{
873+
Test("using Foo = object;");
874+
Test("using Foo = (int foo, int bar);");
875+
}
876+
870877
private void Test(
871878
string sourceText,
872879
string expected,

src/Quoter/Quoter.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,25 @@ private MethodInfo PickFactoryMethodToCreateNode(SyntaxNode node)
10441044
return candidates.First();
10451045
}
10461046

1047-
var usingDirectiveSyntax = node as UsingDirectiveSyntax;
1048-
if (usingDirectiveSyntax != null)
1047+
if (node is UsingDirectiveSyntax usingDirectiveSyntax)
10491048
{
10501049
if (usingDirectiveSyntax.Alias == null)
10511050
{
1052-
candidates = candidates.Where(m => m.ToString() != "Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax UsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.NameEqualsSyntax, Microsoft.CodeAnalysis.CSharp.Syntax.NameSyntax)");
1051+
const string signatureWithNameSyntax = "Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax UsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.NameEqualsSyntax, Microsoft.CodeAnalysis.CSharp.Syntax.NameSyntax)";
1052+
const string signatureWithTypeSyntax = "Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax UsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.NameEqualsSyntax, Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax)";
1053+
1054+
candidates = candidates.Where(m => m.ToString() is not (signatureWithNameSyntax or signatureWithTypeSyntax));
1055+
}
1056+
else
1057+
{
1058+
var preferredSignature = usingDirectiveSyntax.NamespaceOrType is NameSyntax
1059+
? "Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax UsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.NameSyntax)"
1060+
: "Microsoft.CodeAnalysis.CSharp.Syntax.UsingDirectiveSyntax UsingDirective(Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax)";
1061+
1062+
if (candidates.FirstOrDefault(m => m.ToString() == preferredSignature) is { } preferredMethod)
1063+
{
1064+
return preferredMethod;
1065+
}
10531066
}
10541067
}
10551068

src/Quoter/Quoter.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.11.0" />
29+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.12.0" />
3030
</ItemGroup>
3131

3232
</Project>

0 commit comments

Comments
 (0)