Skip to content

Commit 895a346

Browse files
author
Colin Wilmans
committed
*feature* If there is a base with a constructor with parameters, we are gonna take that one and copy and fill those parameters.
1 parent 44f13e2 commit 895a346

File tree

3 files changed

+171
-46
lines changed

3 files changed

+171
-46
lines changed

src/SourceDepend.Tests/SourceDependTests.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,113 @@ public ExampleService(IAnotherService anotherService)
356356
//Assert
357357
Assert.Equal(expec, actual.Item2);
358358
}
359+
360+
[Fact]
361+
public void SourceDepend_WithBaseWithConstructorWithParameters_ShouldRepeatAndCallThisBaseConstructor()
362+
{
363+
//Arrange
364+
var source = """
365+
public partial class ExampleService : BaseService
366+
{
367+
368+
[Dependency]
369+
private readonly IAnotherService anotherService;
370+
}
371+
372+
public partial class BaseService
373+
{
374+
private readonly string _someString;
375+
public BaseService(string someString)
376+
{
377+
_someString = someString;
378+
}
379+
}
380+
381+
public interface IAnotherService {}
382+
""";
383+
384+
var expec = """
385+
// <auto-generated/>
386+
#pragma warning disable
387+
#nullable enable
388+
/// <inheritdoc/>
389+
public partial class ExampleService
390+
{
391+
public ExampleService(IAnotherService anotherService, string someString) : base(someString)
392+
{
393+
394+
#if NET6_0_OR_GREATER
395+
ArgumentNullException.ThrowIfNull(anotherService);
396+
#endif
397+
PreConstruct();
398+
399+
this.anotherService = anotherService;
400+
401+
PostConstruct();
402+
}
403+
404+
partial void PreConstruct();
405+
partial void PostConstruct();
406+
}
407+
408+
""";
409+
//Act
410+
var actual = SourceCompiler.GetGeneratedOutput(source, _output);
411+
412+
//Assert
413+
Assert.Equal(expec, actual.Item2);
414+
}
415+
416+
[Fact]
417+
public void SourceDepend_WithBaseWithDependencyAttributes_ShouldRepeatTheseAndCallThisBaseConstructor()
418+
{
419+
//Arrange
420+
var source = """
421+
public partial class ExampleService : BaseService
422+
{
423+
424+
[Dependency]
425+
private readonly IAnotherService anotherService;
426+
}
427+
428+
public partial class BaseService
429+
{
430+
[Dependency]
431+
private readonly string _someString;
432+
}
433+
434+
public interface IAnotherService {}
435+
""";
436+
437+
var expec = """
438+
// <auto-generated/>
439+
#pragma warning disable
440+
#nullable enable
441+
/// <inheritdoc/>
442+
public partial class ExampleService
443+
{
444+
public ExampleService(IAnotherService anotherService, string someString) : base(someString)
445+
{
446+
447+
#if NET6_0_OR_GREATER
448+
ArgumentNullException.ThrowIfNull(anotherService);
449+
#endif
450+
PreConstruct();
451+
452+
this.anotherService = anotherService;
453+
454+
PostConstruct();
455+
}
456+
457+
partial void PreConstruct();
458+
partial void PostConstruct();
459+
}
460+
461+
""";
462+
//Act
463+
var actual = SourceCompiler.GetGeneratedOutput(source, _output);
464+
465+
//Assert
466+
Assert.Equal(expec, actual.Item1);
467+
}
359468
}

src/SourceDepend/CodeGenerators/DependencyClassCodeGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ private static string[] GetParams(List<ISymbol> symbols)
140140
case IPropertySymbol property:
141141
variables[i] = $@"{property.Type} {property.Name.ToCamelCase()}";
142142
break;
143+
case IParameterSymbol parameter:
144+
variables[i] = $@"{parameter.Type} {parameter.Name.ToCamelCase()}";
145+
break;
143146
}
144147
}
145148

@@ -160,6 +163,9 @@ private static string[] GetBaseParams(List<ISymbol> symbols)
160163
case IPropertySymbol property:
161164
variables[i] = property.Name.ToCamelCase();
162165
break;
166+
case IParameterSymbol parameter:
167+
variables[i] = parameter.Name.ToCamelCase();
168+
break;
163169
}
164170
}
165171

src/SourceDepend/Extensions/DependentClassExtensions.cs

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,91 @@
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
44
using SourceDepend.Model;
55
using System.Collections.Generic;
6+
using System.Linq;
67

7-
namespace SourceDepend.Extensions
8+
namespace SourceDepend.Extensions;
9+
10+
internal static class DependentClassExtensions
811
{
9-
internal static class DependentClassExtensions
10-
{
11-
private const string attributeDisplayName = "DependencyAttribute";
12+
private const string attributeDisplayName = "DependencyAttribute";
1213

13-
internal static DependencyClassData? ToDependencyClass(this GeneratorSyntaxContext ctx)
14-
{
15-
//if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Launch();
14+
internal static DependencyClassData? ToDependencyClass(this GeneratorSyntaxContext ctx)
15+
{
16+
//if (!System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Launch();
1617

17-
var ClassDeclarationSyntax = (ClassDeclarationSyntax)ctx.Node;
18-
List<ISymbol>? symbols = null;
18+
var ClassDeclarationSyntax = (ClassDeclarationSyntax)ctx.Node;
19+
List<ISymbol>? symbols = null;
1920

20-
foreach (var member in ClassDeclarationSyntax.Members)
21+
foreach (var member in ClassDeclarationSyntax.Members)
22+
{
23+
if (member.AttributeLists.Any() == false)
2124
{
22-
if (member.AttributeLists.Any() == false)
23-
{
24-
continue;
25-
}
25+
continue;
26+
}
2627

27-
if (member is FieldDeclarationSyntax fieldSyntax)
28+
if (member is FieldDeclarationSyntax fieldSyntax)
29+
{
30+
foreach (var variable in fieldSyntax.Declaration.Variables)
2831
{
29-
foreach (var variable in fieldSyntax.Declaration.Variables)
32+
if (ctx.SemanticModel.GetDeclaredSymbol(variable) is ISymbol symbol)
3033
{
31-
if (ctx.SemanticModel.GetDeclaredSymbol(variable) is ISymbol symbol)
32-
{
33-
AddSymbolIfTagged(ref symbols, symbol);
34-
}
34+
AddSymbolIfTagged(ref symbols, symbol);
3535
}
3636
}
37-
else if (member.IsKind(SyntaxKind.PropertyDeclaration))
37+
}
38+
else if (member.IsKind(SyntaxKind.PropertyDeclaration))
39+
{
40+
if (ctx.SemanticModel.GetDeclaredSymbol(member) is ISymbol symbol)
3841
{
39-
if (ctx.SemanticModel.GetDeclaredSymbol(member) is ISymbol symbol)
40-
{
41-
AddSymbolIfTagged(ref symbols, symbol);
42-
}
42+
AddSymbolIfTagged(ref symbols, symbol);
4343
}
4444
}
45+
}
4546

46-
if (symbols != null)
47+
if (symbols != null)
48+
{
49+
var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(ClassDeclarationSyntax);
50+
List<ISymbol>? baseSymbols = null;
51+
if (classSymbol?.BaseType != null)
4752
{
48-
var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(ClassDeclarationSyntax);
49-
List<ISymbol>? baseSymbols = null;
50-
if (classSymbol?.BaseType != null)
53+
var members = classSymbol.BaseType.GetMembers();
5154
{
52-
var members = classSymbol.BaseType.GetMembers();
55+
foreach (var member in members)
5356
{
54-
foreach (var member in members)
57+
if (member is IFieldSymbol or
58+
IPropertySymbol)
5559
{
56-
if (member is IFieldSymbol or
57-
IPropertySymbol)
58-
{
59-
AddSymbolIfTagged(ref baseSymbols, member);
60-
}
60+
AddSymbolIfTagged(ref baseSymbols, member);
6161
}
6262
}
6363
}
6464

65-
return classSymbol == null ? null : new DependencyClassData(classSymbol, symbols, baseSymbols);
65+
//Check the first constructor
66+
if (baseSymbols == null && classSymbol.BaseType.Constructors.Length > 0)
67+
{
68+
var constructor = classSymbol.BaseType.Constructors[0];
69+
if (constructor.Parameters.Length > 0)
70+
{
71+
baseSymbols = constructor.Parameters.Cast<ISymbol>().ToList();
72+
}
73+
}
6674
}
6775

68-
return null;
76+
return classSymbol == null ? null : new DependencyClassData(classSymbol, symbols, baseSymbols);
6977
}
7078

71-
private static void AddSymbolIfTagged(ref List<ISymbol>? symbols, ISymbol fieldSymbol)
79+
return null;
80+
}
81+
82+
private static void AddSymbolIfTagged(ref List<ISymbol>? symbols, ISymbol fieldSymbol)
83+
{
84+
foreach (var att in fieldSymbol.GetAttributes())
7285
{
73-
foreach (var att in fieldSymbol.GetAttributes())
86+
if (att.AttributeClass?.ToDisplayString() == attributeDisplayName)
7487
{
75-
if (att.AttributeClass?.ToDisplayString() == attributeDisplayName)
76-
{
77-
symbols ??= [];
78-
symbols.Add(fieldSymbol);
79-
return;
80-
}
88+
symbols ??= [];
89+
symbols.Add(fieldSymbol);
90+
return;
8191
}
8292
}
8393
}

0 commit comments

Comments
 (0)