Skip to content
This repository was archived by the owner on Jun 28, 2023. It is now read-only.

Commit fda0a3c

Browse files
authored
fix: Don't break when one form is invalid (#95)
1 parent c0226c2 commit fda0a3c

File tree

3 files changed

+43
-58
lines changed

3 files changed

+43
-58
lines changed

src/Avalonia.NameGenerator/Generator.cs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Execute(GeneratorExecutionContext context)
2525
}
2626
catch (Exception exception)
2727
{
28-
ReportUnhandledError(context, exception);
28+
context.ReportUnhandledError(exception);
2929
}
3030
}
3131

@@ -44,42 +44,10 @@ private static INameGenerator CreateNameGenerator(GeneratorExecutionContext cont
4444
options.AvaloniaNameGeneratorViewFileNamingStrategy,
4545
new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByPath),
4646
new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByNamespace),
47-
new XamlXViewResolver(types, compiler, true, type => ReportInvalidType(context, type)),
47+
new XamlXViewResolver(types, compiler, true,
48+
type => context.ReportInvalidType(type),
49+
error => context.ReportUnhandledError(error)),
4850
new XamlXNameResolver(options.AvaloniaNameGeneratorDefaultFieldModifier),
4951
generator);
5052
}
51-
52-
private static void ReportUnhandledError(GeneratorExecutionContext context, Exception error)
53-
{
54-
const string message =
55-
"Unhandled exception occured while generating typed Name references. " +
56-
"Please file an issue: https://github.com/avaloniaui/avalonia.namegenerator";
57-
context.ReportDiagnostic(
58-
Diagnostic.Create(
59-
new DiagnosticDescriptor(
60-
"AXN0002",
61-
message,
62-
error.ToString(),
63-
"Usage",
64-
DiagnosticSeverity.Error,
65-
true),
66-
Location.None));
67-
}
68-
69-
private static void ReportInvalidType(GeneratorExecutionContext context, string typeName)
70-
{
71-
var message =
72-
$"Avalonia x:Name generator was unable to generate names for type '{typeName}'. " +
73-
$"The type '{typeName}' does not exist in the assembly.";
74-
context.ReportDiagnostic(
75-
Diagnostic.Create(
76-
new DiagnosticDescriptor(
77-
"AXN0001",
78-
message,
79-
message,
80-
"Usage",
81-
DiagnosticSeverity.Error,
82-
true),
83-
Location.None));
84-
}
8553
}

src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class XamlXViewResolver : IViewResolver, IXamlAstVisitor
1515
private readonly MiniCompiler _compiler;
1616
private readonly bool _checkTypeValidity;
1717
private readonly Action<string> _onTypeInvalid;
18+
private readonly Action<Exception> _onUnhandledError;
1819

1920
private ResolvedView _resolvedClass;
2021
private XamlDocument _xaml;
@@ -23,26 +24,36 @@ public XamlXViewResolver(
2324
RoslynTypeSystem typeSystem,
2425
MiniCompiler compiler,
2526
bool checkTypeValidity = false,
26-
Action<string> onTypeInvalid = null)
27+
Action<string> onTypeInvalid = null,
28+
Action<Exception> onUnhandledError = null)
2729
{
2830
_checkTypeValidity = checkTypeValidity;
2931
_onTypeInvalid = onTypeInvalid;
32+
_onUnhandledError = onUnhandledError;
3033
_typeSystem = typeSystem;
3134
_compiler = compiler;
3235
}
3336

3437
public ResolvedView ResolveView(string xaml)
3538
{
36-
_resolvedClass = null;
37-
_xaml = XDocumentXamlParser.Parse(xaml, new Dictionary<string, string>
39+
try
3840
{
39-
{XamlNamespaces.Blend2008, XamlNamespaces.Blend2008}
40-
});
41+
_resolvedClass = null;
42+
_xaml = XDocumentXamlParser.Parse(xaml, new Dictionary<string, string>
43+
{
44+
{XamlNamespaces.Blend2008, XamlNamespaces.Blend2008}
45+
});
4146

42-
_compiler.Transform(_xaml);
43-
_xaml.Root.Visit(this);
44-
_xaml.Root.VisitChildren(this);
45-
return _resolvedClass;
47+
_compiler.Transform(_xaml);
48+
_xaml.Root.Visit(this);
49+
_xaml.Root.VisitChildren(this);
50+
return _resolvedClass;
51+
}
52+
catch (Exception exception)
53+
{
54+
_onUnhandledError?.Invoke(exception);
55+
return null;
56+
}
4657
}
4758

4859
IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node)
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Linq;
1+
using System;
22
using Microsoft.CodeAnalysis;
33

44
namespace Avalonia.NameGenerator;
55

66
internal static class GeneratorContextExtensions
77
{
8-
private const string SourceItemGroupMetadata = "build_metadata.AdditionalFiles.SourceItemGroup";
8+
private const string UnhandledErrorDescriptorId = "AXN0002";
9+
private const string InvalidTypeDescriptorId = "AXN0001";
910

1011
public static string GetMsBuildProperty(
1112
this GeneratorExecutionContext context,
@@ -16,15 +17,20 @@ public static string GetMsBuildProperty(
1617
return value ?? defaultValue;
1718
}
1819

19-
public static string[] GetMsBuildItems(this GeneratorExecutionContext context, string name)
20-
=> context
21-
.AdditionalFiles
22-
.Where(f =>
23-
context
24-
.AnalyzerConfigOptions
25-
.GetOptions(f)
26-
.TryGetValue(SourceItemGroupMetadata, out var sourceItemGroup)
27-
&& sourceItemGroup == name)
28-
.Select(f => f.Path)
29-
.ToArray();
20+
public static void ReportUnhandledError(this GeneratorExecutionContext context, Exception error) =>
21+
context.Report(UnhandledErrorDescriptorId,
22+
"Unhandled exception occured while generating typed Name references. " +
23+
"Please file an issue: https://github.com/avaloniaui/avalonia.namegenerator",
24+
error.ToString());
25+
26+
public static void ReportInvalidType(this GeneratorExecutionContext context, string typeName) =>
27+
context.Report(InvalidTypeDescriptorId,
28+
$"Avalonia x:Name generator was unable to generate names for type '{typeName}'. " +
29+
$"The type '{typeName}' does not exist in the assembly.");
30+
31+
private static void Report(this GeneratorExecutionContext context, string id, string title, string message = null) =>
32+
context.ReportDiagnostic(
33+
Diagnostic.Create(
34+
new DiagnosticDescriptor(id, title, message ?? title, "Usage", DiagnosticSeverity.Error, true),
35+
Location.None));
3036
}

0 commit comments

Comments
 (0)