@@ -20,29 +20,6 @@ public class StubGenerator : IIncrementalGenerator
2020 /// <inheritdoc/>
2121 public void Initialize ( IncrementalGeneratorInitializationContext context )
2222 {
23- context . RegisterPostInitializationOutput (
24- ctx => ctx . AddSource ( "AddGeneratedStub.g.cs" ,
25- @"namespace Bunit
26- {
27- public static class ComponentFactoriesExtensions
28- {
29- /// <summary>
30- /// Marks a component, so that a stub get is generated for it. The stub has the same name as the component, but with the suffix ""Stub"" added.
31- /// </summary>
32- /// <typeparam name=""TComponent"">The type of component to generate a stub for.</typeparam>
33- /// <remarks>
34- /// When <c>ComponentFactories.AddGeneratedStub<MyButton>()</c> is called, a stub component is generated for the component
35- /// with the name <c>MyButtonStub</c>. The stub component is added to the <see cref=""ComponentFactoryCollection""/> and can be used.
36- /// It can also be retrieved via <c>cut.FindComponent<MyButtonStub>()</c>.
37- /// This call does the same as <c>ComponentFactories.Add<MyButton, MyButtonStub>()</c>.
38- /// </remarks>
39- public static ComponentFactoryCollection AddGeneratedStub<TComponent>(this ComponentFactoryCollection factories)
40- where TComponent : Microsoft.AspNetCore.Components.IComponent
41- {
42- return factories.AddGeneratedStubInterceptor<TComponent>();
43- }
44- }
45- }" ) ) ;
4623 var classesToStub = context . SyntaxProvider
4724 . CreateSyntaxProvider (
4825 predicate : static ( s , _ ) => s is InvocationExpressionSyntax ,
@@ -71,7 +48,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorSyntaxContext context)
7148 var path = GetInterceptorFilePath ( context . Node . SyntaxTree , context . SemanticModel . Compilation ) ;
7249 var lineSpan = context . SemanticModel . SyntaxTree . GetLineSpan ( context . Node . Span ) ;
7350 var line = lineSpan . StartLinePosition . Line + 1 ;
74- var column = lineSpan . Span . Start . Character + context . Node . ToString ( ) . IndexOf ( "AddGeneratedStub " , StringComparison . Ordinal ) ;
51+ var column = lineSpan . Span . Start . Character + context . Node . ToString ( ) . IndexOf ( "AddStub " , StringComparison . Ordinal ) + 1 ;
7552
7653 return new StubClassInfo
7754 {
@@ -80,7 +57,7 @@ private static StubClassInfo GetStubClassInfo(GeneratorSyntaxContext context)
8057 TargetType = symbol ,
8158 Path = path ,
8259 Line = line ,
83- Column = column + 1 ,
60+ Column = column ,
8461 } ;
8562 }
8663 }
@@ -94,7 +71,7 @@ static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation,
9471 return false ;
9572 }
9673
97- if ( memberAccess . Name . Identifier . Text != "AddGeneratedStub " ||
74+ if ( memberAccess . Name . Identifier . Text != "AddStub " ||
9875 invocation . ArgumentList . Arguments . Count != 0 )
9976 {
10077 return false ;
@@ -132,7 +109,7 @@ private static bool GenerateStubComponent(StubClassInfo classInfo, SourceProduct
132109
133110 sourceBuilder . AppendLine ( $ "namespace { classInfo . TargetTypeNamespace } ;") ;
134111 sourceBuilder . AppendLine ( ) ;
135- sourceBuilder . AppendLine ( $ "internal partial class { classInfo . StubClassName } : Microsoft.AspNetCore.Components.ComponentBase") ;
112+ sourceBuilder . AppendLine ( $ "internal partial class { classInfo . StubClassName } : global:: Microsoft.AspNetCore.Components.ComponentBase") ;
136113 sourceBuilder . Append ( "{" ) ;
137114
138115 foreach ( var member in targetTypeSymbol
@@ -222,21 +199,21 @@ public InterceptsLocationAttribute(string filePath, int line, int column)
222199 interceptorSource . AppendLine ( ) ;
223200 interceptorSource . AppendLine ( "namespace Bunit" ) ;
224201 interceptorSource . AppendLine ( "{" ) ;
225- interceptorSource . AppendLine ( $ "\t static class Interceptor{ stubbedComponentGroup . StubClassName } ") ;
202+ interceptorSource . AppendLine ( $ "\t internal static class Interceptor{ stubbedComponentGroup . StubClassName } ") ;
226203 interceptorSource . AppendLine ( "\t {" ) ;
227204
228205 foreach ( var hit in stubClassGrouped )
229206 {
230207 interceptorSource . AppendLine (
231- $ "\t \t [System.Runtime.CompilerServices.InterceptsLocationAttribute(\" { hit . Path } \" , { hit . Line } , { hit . Column } )]") ;
208+ $ "\t \t [global:: System.Runtime.CompilerServices.InterceptsLocationAttribute(\" { hit . Path } \" , { hit . Line } , { hit . Column } )]") ;
232209 }
233210
234211 interceptorSource . AppendLine (
235- "\t \t public static global::Bunit.ComponentFactoryCollection AddGeneratedStubInterceptor <TComponent>(this global::Bunit.ComponentFactoryCollection factories)" ) ;
236- interceptorSource . AppendLine ( "\t \t \t where TComponent : Microsoft.AspNetCore.Components.IComponent" ) ;
212+ "\t \t public static global::Bunit.ComponentFactoryCollection AddStubInterceptor <TComponent>(this global::Bunit.ComponentFactoryCollection factories)" ) ;
213+ interceptorSource . AppendLine ( "\t \t \t where TComponent : global:: Microsoft.AspNetCore.Components.IComponent" ) ;
237214 interceptorSource . AppendLine ( "\t \t {" ) ;
238215 interceptorSource . AppendLine (
239- $ "\t \t \t return factories.Add<global::{ stubbedComponentGroup . TargetType . ToDisplayString ( ) } , { stubbedComponentGroup . TargetTypeNamespace } .{ stubbedComponentGroup . StubClassName } >();") ;
216+ $ "\t \t \t return factories.Add<global::{ stubbedComponentGroup . TargetType . ToDisplayString ( ) } , global:: { stubbedComponentGroup . TargetTypeNamespace } .{ stubbedComponentGroup . StubClassName } >();") ;
240217 interceptorSource . AppendLine ( "\t \t }" ) ;
241218 interceptorSource . AppendLine ( "\t }" ) ;
242219 interceptorSource . AppendLine ( "}" ) ;
0 commit comments