Skip to content

Commit 94218fa

Browse files
Fix value converters that also derive from MarkupExtension
1 parent 9dfef8e commit 94218fa

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed
Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,59 @@
1-
namespace Catel.SourceGenerators
1+
namespace Catel.SourceGenerators;
2+
3+
using System.Linq;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp.Syntax;
6+
7+
internal static class INamedTypeSymbolExtensions
28
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Diagnostics;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading;
9-
using Microsoft.CodeAnalysis;
10-
using Microsoft.CodeAnalysis.CSharp.Syntax;
11-
12-
internal static class INamedTypeSymbolExtensions
9+
public static bool HasStaticConstructorWithContent(this INamedTypeSymbol namedTypeSymbol)
1310
{
14-
public static bool HasStaticConstructorWithContent(this INamedTypeSymbol namedTypeSymbol)
11+
var staticCtor = namedTypeSymbol.Constructors.FirstOrDefault(x => x.IsStatic);
12+
if (staticCtor is null)
13+
{
14+
return false;
15+
}
16+
17+
var syntaxReferences = staticCtor.DeclaringSyntaxReferences;
18+
19+
foreach (var syntaxReference in syntaxReferences)
1520
{
16-
var staticCtor = namedTypeSymbol.Constructors.FirstOrDefault(x => x.IsStatic);
17-
if (staticCtor is null)
21+
var constructorDeclarationSyntax = syntaxReference.GetSyntax() as ConstructorDeclarationSyntax;
22+
if (constructorDeclarationSyntax is null)
23+
{
24+
continue;
25+
}
26+
27+
var body = constructorDeclarationSyntax.Body;
28+
if (body is null)
1829
{
1930
return false;
2031
}
2132

22-
var syntaxReferences = staticCtor.DeclaringSyntaxReferences;
33+
foreach (var childNode in body.ChildNodes())
34+
{
35+
// We found at least 1 child, thus body
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
}
2342

24-
foreach (var syntaxReference in syntaxReferences)
43+
public static bool DerivesFromBaseClass(this INamedTypeSymbol classSymbol, string typeName)
44+
{
45+
var baseType = classSymbol.BaseType;
46+
while (baseType is not null)
47+
{
48+
var displayString = baseType.ToDisplayString();
49+
if (displayString == typeName)
2550
{
26-
var constructorDeclarationSyntax = syntaxReference.GetSyntax() as ConstructorDeclarationSyntax;
27-
if (constructorDeclarationSyntax is null)
28-
{
29-
continue;
30-
}
31-
32-
var body = constructorDeclarationSyntax.Body;
33-
if (body is null)
34-
{
35-
return false;
36-
}
37-
38-
foreach (var childNode in body.ChildNodes())
39-
{
40-
// We found at least 1 child, thus body
41-
return true;
42-
}
51+
return true;
4352
}
4453

45-
return false;
54+
baseType = baseType.BaseType;
4655
}
56+
57+
return false;
4758
}
4859
}

src/Catel.SourceGenerators/XamlConstructors/MarkupExtensionsConstructorsSourceGenerator.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node)
7373
return null;
7474
}
7575

76-
var baseType = classSymbol.BaseType;
77-
while (baseType is not null)
78-
{
79-
var displayString = baseType.ToDisplayString();
80-
if (displayString == "System.Windows.Markup.MarkupExtension")
81-
{
82-
break;
83-
}
84-
85-
baseType = baseType.BaseType;
86-
}
87-
88-
if (baseType is null)
76+
if (!classSymbol.DerivesFromBaseClass("System.Windows.Markup.MarkupExtension"))
8977
{
9078
return null;
9179
}

src/Catel.SourceGenerators/XamlConstructors/ValueConverterConstructorsSourceGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node)
7272
return null;
7373
}
7474

75+
// Already handled by markup extension constructor source generator
76+
if (classSymbol.DerivesFromBaseClass("System.Windows.Markup.MarkupExtension"))
77+
{
78+
return null;
79+
}
80+
7581
var emptyClassConstructor = classSymbol.Constructors.FirstOrDefault(x => !x.IsStatic && x.Parameters.Length == 0);
7682
if (emptyClassConstructor is not null)
7783
{

0 commit comments

Comments
 (0)