Skip to content

Commit 6142684

Browse files
linkdotnetegil
authored andcommitted
refactor: Nest less
1 parent 96e6074 commit 6142684

1 file changed

Lines changed: 27 additions & 29 deletions

File tree

src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2121
.CreateSyntaxProvider(
2222
predicate: static (s, _) => s is InvocationExpressionSyntax
2323
{
24-
Expression: MemberAccessExpressionSyntax
25-
{
26-
Name.Identifier.Text: "AddStub"
27-
}
24+
Expression: MemberAccessExpressionSyntax { Name.Identifier.Text: "AddStub" }
2825
},
2926
transform: static (ctx, _) => GetStubClassInfo(ctx))
3027
.Where(static m => m is not null)
@@ -43,29 +40,34 @@ private static AddStubClassInfo GetStubClassInfo(GeneratorSyntaxContext context)
4340
return null;
4441
}
4542

46-
if (invocation?.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax { TypeArgumentList.Arguments.Count: 1 } genericName })
43+
if (invocation?.Expression is not MemberAccessExpressionSyntax
44+
{
45+
Name: GenericNameSyntax { TypeArgumentList.Arguments.Count: 1 } genericName
46+
})
4747
{
48-
var typeArgument = genericName.TypeArgumentList.Arguments[0];
49-
if (context.SemanticModel.GetSymbolInfo(typeArgument).Symbol is ITypeSymbol symbol)
50-
{
51-
var path = GetInterceptorFilePath(context.Node.SyntaxTree, context.SemanticModel.Compilation);
52-
var lineSpan = context.SemanticModel.SyntaxTree.GetLineSpan(context.Node.Span);
53-
var line = lineSpan.StartLinePosition.Line + 1;
54-
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
48+
return null;
49+
}
5550

56-
return new AddStubClassInfo
57-
{
58-
StubClassName = $"{symbol.Name}Stub",
59-
TargetTypeNamespace = symbol.ContainingNamespace.ToDisplayString(),
60-
TargetType = symbol,
61-
Path = path,
62-
Line = line,
63-
Column = column,
64-
};
65-
}
51+
var typeArgument = genericName.TypeArgumentList.Arguments[0];
52+
if (context.SemanticModel.GetSymbolInfo(typeArgument).Symbol is not ITypeSymbol symbol)
53+
{
54+
return null;
6655
}
6756

68-
return null;
57+
var path = GetInterceptorFilePath(context.Node.SyntaxTree, context.SemanticModel.Compilation);
58+
var lineSpan = context.SemanticModel.SyntaxTree.GetLineSpan(context.Node.Span);
59+
var line = lineSpan.StartLinePosition.Line + 1;
60+
var column = lineSpan.Span.Start.Character + context.Node.ToString().IndexOf("AddStub", StringComparison.Ordinal) + 1;
61+
62+
return new AddStubClassInfo
63+
{
64+
StubClassName = $"{symbol.Name}Stub",
65+
TargetTypeNamespace = symbol.ContainingNamespace.ToDisplayString(),
66+
TargetType = symbol,
67+
Path = path,
68+
Line = line,
69+
Column = column,
70+
};
6971

7072
static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation, SemanticModel semanticModel)
7173
{
@@ -95,17 +97,13 @@ private static void Execute(ImmutableArray<AddStubClassInfo> classInfos, SourceP
9597
{
9698
var stubbedComponentGroup = stubClassGrouped.First();
9799
var didStubComponent = StubComponentBuilder.GenerateStubComponent(stubbedComponentGroup, context);
98-
if (didStubComponent is false)
100+
if (didStubComponent)
99101
{
100-
return;
102+
GenerateInterceptorCode(stubbedComponentGroup, stubClassGrouped, context);
101103
}
102-
103-
GenerateInterceptorCode(stubbedComponentGroup, stubClassGrouped, context);
104104
}
105105
}
106106

107-
108-
109107
private static void GenerateInterceptorCode(AddStubClassInfo stubbedComponentGroup, IEnumerable<AddStubClassInfo> stubClassGrouped, SourceProductionContext context)
110108
{
111109
// Generate the attribute

0 commit comments

Comments
 (0)