Skip to content

Commit dd80e4e

Browse files
committed
Deduplicate "get class syntax and named type symbol" logic
1 parent d3ad4cb commit dd80e4e

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

AutomaticInterface/AutomaticInterface/Builder.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ private static string InheritDoc(ISymbol source) =>
4242

4343
public static string? GetInterfaceNameFor(ITypeSymbol typeSymbol)
4444
{
45-
if (
46-
typeSymbol.DeclaringSyntaxReferences.First().GetSyntax()
47-
is not ClassDeclarationSyntax classSyntax
48-
|| typeSymbol is not INamedTypeSymbol
49-
)
45+
var declarationAndNamedTypeSymbol = GetClassDeclarationMetadata(typeSymbol);
46+
if (declarationAndNamedTypeSymbol == null)
5047
{
5148
return null;
5249
}
50+
51+
var (classSyntax, _) = declarationAndNamedTypeSymbol.Value;
52+
5353
var symbolDetails = GetSymbolDetails(typeSymbol, classSyntax);
5454

5555
return $"global::{symbolDetails.NamespaceName}.{symbolDetails.InterfaceName}";
@@ -63,15 +63,14 @@ public static string BuildInterfaceFor(
6363
List<string> generatedInterfaceNames
6464
)
6565
{
66-
if (
67-
typeSymbol.DeclaringSyntaxReferences.First().GetSyntax()
68-
is not ClassDeclarationSyntax classSyntax
69-
|| typeSymbol is not INamedTypeSymbol namedTypeSymbol
70-
)
66+
var declarationAndNamedTypeSymbol = GetClassDeclarationMetadata(typeSymbol);
67+
if (declarationAndNamedTypeSymbol == null)
7168
{
7269
return string.Empty;
7370
}
7471

72+
var (classSyntax, namedTypeSymbol) = declarationAndNamedTypeSymbol.Value;
73+
7574
var symbolDetails = GetSymbolDetails(typeSymbol, classSyntax);
7675
var interfaceGenerator = new InterfaceBuilder(
7776
symbolDetails.NamespaceName,
@@ -100,6 +99,23 @@ is not ClassDeclarationSyntax classSyntax
10099
return generatedCode;
101100
}
102101

102+
private static (
103+
ClassDeclarationSyntax Syntax,
104+
INamedTypeSymbol NamedTypeSymbol
105+
)? GetClassDeclarationMetadata(ITypeSymbol typeSymbol)
106+
{
107+
if (
108+
typeSymbol.DeclaringSyntaxReferences.First().GetSyntax()
109+
is not ClassDeclarationSyntax classSyntax
110+
|| typeSymbol is not INamedTypeSymbol namedTypeSymbol
111+
)
112+
{
113+
return null;
114+
}
115+
116+
return (classSyntax, namedTypeSymbol);
117+
}
118+
103119
private static GeneratedSymbolDetails GetSymbolDetails(
104120
ITypeSymbol typeSymbol,
105121
ClassDeclarationSyntax classSyntax

0 commit comments

Comments
 (0)