Skip to content

Commit 9db0f33

Browse files
author
Colin Wilmans
committed
When there is a default constructor, then do not call a constructor
1 parent 895a346 commit 9db0f33

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

src/SourceDepend.Tests/SourceDependTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,67 @@ public ExampleService(IAnotherService anotherService, string someString) : base(
413413
Assert.Equal(expec, actual.Item2);
414414
}
415415

416+
[Fact]
417+
public void SourceDepend_WithBaseWithConstructorWithParametersAndParameterless_ShouldNotCallABaseConstructor()
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+
private readonly string _someString;
431+
public BaseService(string someString)
432+
{
433+
_someString = someString;
434+
}
435+
436+
public BaseService()
437+
{
438+
439+
}
440+
}
441+
442+
public interface IAnotherService {}
443+
""";
444+
445+
var expec = """
446+
// <auto-generated/>
447+
#pragma warning disable
448+
#nullable enable
449+
/// <inheritdoc/>
450+
public partial class ExampleService
451+
{
452+
public ExampleService(IAnotherService anotherService)
453+
{
454+
455+
#if NET6_0_OR_GREATER
456+
ArgumentNullException.ThrowIfNull(anotherService);
457+
#endif
458+
PreConstruct();
459+
460+
this.anotherService = anotherService;
461+
462+
PostConstruct();
463+
}
464+
465+
partial void PreConstruct();
466+
partial void PostConstruct();
467+
}
468+
469+
""";
470+
//Act
471+
var actual = SourceCompiler.GetGeneratedOutput(source, _output);
472+
473+
//Assert
474+
Assert.Equal(expec, actual.Item2);
475+
}
476+
416477
[Fact]
417478
public void SourceDepend_WithBaseWithDependencyAttributes_ShouldRepeatTheseAndCallThisBaseConstructor()
418479
{

src/SourceDepend/Extensions/DependentClassExtensions.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
44
using SourceDepend.Model;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.Linq;
78

89
namespace SourceDepend.Extensions;
@@ -62,14 +63,24 @@ internal static class DependentClassExtensions
6263
}
6364
}
6465

65-
//Check the first constructor
66-
if (baseSymbols == null && classSymbol.BaseType.Constructors.Length > 0)
66+
//OK, next best: Check whether there is a default constructor, or otherwise take the first constructor
67+
if (baseSymbols is null)
6768
{
68-
var constructor = classSymbol.BaseType.Constructors[0];
69-
if (constructor.Parameters.Length > 0)
69+
ImmutableArray<IParameterSymbol>? parameters = null;
70+
foreach (var constructor in classSymbol.BaseType.Constructors)
7071
{
71-
baseSymbols = constructor.Parameters.Cast<ISymbol>().ToList();
72+
if (constructor.Parameters.Length == 0)
73+
{
74+
goto has_default_constructor;
75+
}
76+
parameters ??= constructor.Parameters;
77+
}
78+
79+
if (parameters is not null)
80+
{
81+
baseSymbols = parameters.Cast<ISymbol>().ToList();
7282
}
83+
has_default_constructor:;
7384
}
7485
}
7586

src/SourceDepend/SourceDepend.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<PrivateAssets>all</PrivateAssets>
3737
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3838
</PackageReference>
39-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
39+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" />
4040
</ItemGroup>
4141
</Project>
4242

0 commit comments

Comments
 (0)