Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 3c38138

Browse files
authored
Fix component generator (#81)
1 parent 7f0def3 commit 3c38138

File tree

12 files changed

+108
-22
lines changed

12 files changed

+108
-22
lines changed

samples/BlazorSample.Components/BlazorSample.Components.csproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010

1111

1212
<ItemGroup>
13-
<PackageReference Include="MatBlazor" Version="2.8.0"/>
14-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.2"/>
15-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.2"/>
13+
<PackageReference Include="MatBlazor" Version="2.8.0" />
14+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.2" />
15+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.2" />
1616
</ItemGroup>
1717

1818

1919
<ItemGroup>
20-
<ProjectReference Include="..\BlazorSample.ViewModels\BlazorSample.ViewModels.csproj"/>
20+
<ProjectReference Include="..\BlazorSample.ViewModels\BlazorSample.ViewModels.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\..\src\MvvmBlazor.CodeGenerators\MvvmBlazor.CodeGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
2125
</ItemGroup>
2226

2327
</Project>

samples/BlazorSample.Components/Pages/Clock.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/clock"
2-
@inherits MvvmComponentBase<ClockViewModel>
2+
@inherits CustomBaseComponent<ClockViewModel>
33

44
<MatCard>
55
<MatCardContent>

samples/BlazorSample.Components/Pages/Counter.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/counter"
2-
@inherits MvvmComponentBase<CounterViewModel>
2+
@inherits CustomBaseComponent<CounterViewModel>
33
@inject ClockViewModel ClockViewModel
44

55
<MatCard>

samples/BlazorSample.Components/Pages/Parameters.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/parameters/{name}"
2-
@inherits MvvmComponentBase<ParametersViewModel>
2+
@inherits CustomBaseComponent<ParametersViewModel>
33

44
<MatCard>
55
<MatCardContent>

samples/BlazorSample.Components/Pages/TypedParameters.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/typed-parameters/{Id}"
2-
@inherits MvvmComponentBase<TypedParametersViewModel>
2+
@inherits CustomBaseComponent<TypedParametersViewModel>
33

44
<MatCard>
55
<MatCardContent>

samples/BlazorSample.Components/Pages/WeatherForecasts.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@page "/fetchdata"
22

3-
@inherits MvvmComponentBase<WeatherForecastsViewModel>
3+
@inherits CustomBaseComponent<WeatherForecastsViewModel>
44

55
<MatCard>
66
<MatCardContent>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Components;
2+
using MvvmBlazor;
3+
4+
namespace BlazorSample.Components.Shared;
5+
6+
[MvvmComponent]
7+
public abstract partial class CustomBaseComponent<T> : CustomBaseComponent
8+
{
9+
10+
}
11+
12+
[MvvmComponent]
13+
public abstract partial class CustomBaseComponent : ComponentBase
14+
{
15+
16+
}

samples/BlazorSample.Components/_Imports.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
@using BlazorSample.ViewModels
44
@using BlazorSample.ViewModels.Navbar
55
@using Microsoft.AspNetCore.Components.Routing
6-
@using MatBlazor
6+
@using MatBlazor
7+
@using BlazorSample.Components.Shared

src/MvvmBlazor.CodeGenerators/AnalyzerReleases.Shipped.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ Rule ID | Category | Severity | Notes
66
MVVMBLAZOR001 | MvvmBlazorGenerator | Error | MVVMBLAZOR001_AnalyzerName
77
MVVMBLAZOR002 | MvvmBlazorGenerator | Error | MVVMBLAZOR002_AnalyzerName
88
MVVMBLAZOR003 | MvvmBlazorGenerator | Error | MVVMBLAZOR003_AnalyzerName
9-
MVVMBLAZOR004 | MvvmBlazorGenerator | Error | MVVMBLAZOR004_AnalyzerName
9+
MVVMBLAZOR004 | MvvmBlazorGenerator | Error | MVVMBLAZOR004_AnalyzerName
10+
11+
## Release 6.0.2
12+
### New Rules
13+
14+
Rule ID | Category | Severity | Notes
15+
--------|----------|----------|-------
16+
MVVMBLAZOR005 | MvvmBlazorGenerator | Error | MVVMBLAZOR005_AnalyzerName

src/MvvmBlazor.CodeGenerators/Components/MvvmComponentGenerator.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public class MvvmComponentGenerator : ISourceGenerator
2121
true
2222
);
2323

24+
private static readonly DiagnosticDescriptor ComponentWrongTypeParameterError = new(
25+
"MVVMBLAZOR005",
26+
"Wrong type parameter",
27+
"Mvvm Component class '{0}' needs to have exactly one type parameter named '{1}'",
28+
"MvvmBlazorGenerator",
29+
DiagnosticSeverity.Error,
30+
true
31+
);
32+
33+
2434
public void Execute(GeneratorExecutionContext context)
2535
{
2636
if (context.SyntaxContextReceiver is not MvvmComponentSyntaxReceiver syntaxReceiver ||
@@ -100,14 +110,26 @@ private static void AddGenericComponent(
100110
MvvmComponentClassContext componentClassContext,
101111
TypeDeclarationSyntax componentClass)
102112
{
113+
const string typeParameterName = "T";
103114
var genericComponentSourceText = SourceText.From(
104-
GenerateGenericComponentCode(componentClassContext),
115+
GenerateGenericComponentCode(componentClassContext, typeParameterName),
105116
Encoding.UTF8
106117
);
107118

108-
if (componentClass.TypeParameterList is null || componentClass.TypeParameterList.Parameters.Count != 1)
119+
var typeParameterList = componentClass.TypeParameterList;
120+
if (typeParameterList is null || typeParameterList.Parameters.Count != 1 || typeParameterList.Parameters[0].Identifier.ValueText != typeParameterName)
109121
{
110-
throw new InvalidOperationException("Expected exactly one type parameter");
122+
context.ReportDiagnostic(
123+
Diagnostic.Create(
124+
ComponentWrongTypeParameterError,
125+
Location.Create(
126+
componentClass.SyntaxTree,
127+
TextSpan.FromBounds(componentClass.SpanStart, componentClass.SpanStart)
128+
),
129+
componentClass.Identifier,
130+
typeParameterName
131+
)
132+
);
111133
}
112134

113135
context.AddSource(componentClass.Identifier + "T.Generated.cs", genericComponentSourceText);
@@ -137,7 +159,7 @@ partial class {componentClassName} : IDisposable
137159
[Inject] protected IServiceProvider RootServiceProvider {{ get; set; }} = default!;
138160
protected bool IsDisposed {{ get; private set; }}
139161
140-
public IBinder Binder {{ get; private set; }} = null!;
162+
public MvvmBlazor.Internal.Bindings.IBinder Binder {{ get; private set; }} = null!;
141163
142164
#pragma warning disable CS0109
143165
protected new IServiceProvider ScopedServices
@@ -177,7 +199,7 @@ partial class {componentClassName} : IDisposable
177199
178200
private void InitializeDependencies()
179201
{{
180-
Binder = ScopedServices.GetRequiredService<IBinder>();
202+
Binder = ScopedServices.GetRequiredService<MvvmBlazor.Internal.Bindings.IBinder>();
181203
Binder.ValueChangedCallback = BindingOnBindingValueChanged;
182204
}}
183205
@@ -224,7 +246,7 @@ protected virtual void Dispose(bool disposing)
224246
";
225247
}
226248

227-
private static string GenerateGenericComponentCode(MvvmComponentClassContext componentClassContext)
249+
private static string GenerateGenericComponentCode(MvvmComponentClassContext componentClassContext, string typeParameterName)
228250
{
229251
var componentNamespace = componentClassContext.ComponentSymbol.ContainingNamespace;
230252
var componentClassName = componentClassContext.ComponentClass.Identifier;
@@ -241,10 +263,10 @@ private static string GenerateGenericComponentCode(MvvmComponentClassContext com
241263
242264
namespace {componentNamespace}
243265
{{
244-
partial class {componentClassName}<T>
266+
partial class {componentClassName}<{typeParameterName}>
245267
where T : ViewModelBase
246268
{{
247-
private IViewModelParameterSetter? _viewModelParameterSetter;
269+
private MvvmBlazor.Internal.Parameters.IViewModelParameterSetter? _viewModelParameterSetter;
248270
249271
#pragma warning disable CS8618
250272
protected internal {componentClassName}(IServiceProvider serviceProvider) : base(serviceProvider)
@@ -275,7 +297,7 @@ private void SetParameters()
275297
if (BindingContext is null)
276298
throw new InvalidOperationException($""{{nameof(BindingContext)}} is not set"");
277299
278-
_viewModelParameterSetter ??= ScopedServices.GetRequiredService<IViewModelParameterSetter>();
300+
_viewModelParameterSetter ??= ScopedServices.GetRequiredService<MvvmBlazor.Internal.Parameters.IViewModelParameterSetter>();
279301
_viewModelParameterSetter.ResolveAndSet(this, BindingContext);
280302
}}
281303

0 commit comments

Comments
 (0)