Browse Source

fix: Don't break when one form is invalid (#95)

pull/10407/head 1.4.2
Artyom V. Gorchakov 4 years ago
committed by GitHub
parent
commit
fda0a3cc38
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      src/Avalonia.NameGenerator/Generator.cs
  2. 29
      src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs
  3. 32
      src/Avalonia.NameGenerator/GeneratorContextExtensions.cs

40
src/Avalonia.NameGenerator/Generator.cs

@ -25,7 +25,7 @@ public class AvaloniaNameSourceGenerator : ISourceGenerator
}
catch (Exception exception)
{
ReportUnhandledError(context, exception);
context.ReportUnhandledError(exception);
}
}
@ -44,42 +44,10 @@ public class AvaloniaNameSourceGenerator : ISourceGenerator
options.AvaloniaNameGeneratorViewFileNamingStrategy,
new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByPath),
new GlobPatternGroup(options.AvaloniaNameGeneratorFilterByNamespace),
new XamlXViewResolver(types, compiler, true, type => ReportInvalidType(context, type)),
new XamlXViewResolver(types, compiler, true,
type => context.ReportInvalidType(type),
error => context.ReportUnhandledError(error)),
new XamlXNameResolver(options.AvaloniaNameGeneratorDefaultFieldModifier),
generator);
}
private static void ReportUnhandledError(GeneratorExecutionContext context, Exception error)
{
const string message =
"Unhandled exception occured while generating typed Name references. " +
"Please file an issue: https://github.com/avaloniaui/avalonia.namegenerator";
context.ReportDiagnostic(
Diagnostic.Create(
new DiagnosticDescriptor(
"AXN0002",
message,
error.ToString(),
"Usage",
DiagnosticSeverity.Error,
true),
Location.None));
}
private static void ReportInvalidType(GeneratorExecutionContext context, string typeName)
{
var message =
$"Avalonia x:Name generator was unable to generate names for type '{typeName}'. " +
$"The type '{typeName}' does not exist in the assembly.";
context.ReportDiagnostic(
Diagnostic.Create(
new DiagnosticDescriptor(
"AXN0001",
message,
message,
"Usage",
DiagnosticSeverity.Error,
true),
Location.None));
}
}

29
src/Avalonia.NameGenerator/Generator/XamlXViewResolver.cs

@ -15,6 +15,7 @@ internal class XamlXViewResolver : IViewResolver, IXamlAstVisitor
private readonly MiniCompiler _compiler;
private readonly bool _checkTypeValidity;
private readonly Action<string> _onTypeInvalid;
private readonly Action<Exception> _onUnhandledError;
private ResolvedView _resolvedClass;
private XamlDocument _xaml;
@ -23,26 +24,36 @@ internal class XamlXViewResolver : IViewResolver, IXamlAstVisitor
RoslynTypeSystem typeSystem,
MiniCompiler compiler,
bool checkTypeValidity = false,
Action<string> onTypeInvalid = null)
Action<string> onTypeInvalid = null,
Action<Exception> onUnhandledError = null)
{
_checkTypeValidity = checkTypeValidity;
_onTypeInvalid = onTypeInvalid;
_onUnhandledError = onUnhandledError;
_typeSystem = typeSystem;
_compiler = compiler;
}
public ResolvedView ResolveView(string xaml)
{
_resolvedClass = null;
_xaml = XDocumentXamlParser.Parse(xaml, new Dictionary<string, string>
try
{
{XamlNamespaces.Blend2008, XamlNamespaces.Blend2008}
});
_resolvedClass = null;
_xaml = XDocumentXamlParser.Parse(xaml, new Dictionary<string, string>
{
{XamlNamespaces.Blend2008, XamlNamespaces.Blend2008}
});
_compiler.Transform(_xaml);
_xaml.Root.Visit(this);
_xaml.Root.VisitChildren(this);
return _resolvedClass;
_compiler.Transform(_xaml);
_xaml.Root.Visit(this);
_xaml.Root.VisitChildren(this);
return _resolvedClass;
}
catch (Exception exception)
{
_onUnhandledError?.Invoke(exception);
return null;
}
}
IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node)

32
src/Avalonia.NameGenerator/GeneratorContextExtensions.cs

@ -1,11 +1,12 @@
using System.Linq;
using System;
using Microsoft.CodeAnalysis;
namespace Avalonia.NameGenerator;
internal static class GeneratorContextExtensions
{
private const string SourceItemGroupMetadata = "build_metadata.AdditionalFiles.SourceItemGroup";
private const string UnhandledErrorDescriptorId = "AXN0002";
private const string InvalidTypeDescriptorId = "AXN0001";
public static string GetMsBuildProperty(
this GeneratorExecutionContext context,
@ -16,15 +17,20 @@ internal static class GeneratorContextExtensions
return value ?? defaultValue;
}
public static string[] GetMsBuildItems(this GeneratorExecutionContext context, string name)
=> context
.AdditionalFiles
.Where(f =>
context
.AnalyzerConfigOptions
.GetOptions(f)
.TryGetValue(SourceItemGroupMetadata, out var sourceItemGroup)
&& sourceItemGroup == name)
.Select(f => f.Path)
.ToArray();
public static void ReportUnhandledError(this GeneratorExecutionContext context, Exception error) =>
context.Report(UnhandledErrorDescriptorId,
"Unhandled exception occured while generating typed Name references. " +
"Please file an issue: https://github.com/avaloniaui/avalonia.namegenerator",
error.ToString());
public static void ReportInvalidType(this GeneratorExecutionContext context, string typeName) =>
context.Report(InvalidTypeDescriptorId,
$"Avalonia x:Name generator was unable to generate names for type '{typeName}'. " +
$"The type '{typeName}' does not exist in the assembly.");
private static void Report(this GeneratorExecutionContext context, string id, string title, string message = null) =>
context.ReportDiagnostic(
Diagnostic.Create(
new DiagnosticDescriptor(id, title, message ?? title, "Usage", DiagnosticSeverity.Error, true),
Location.None));
}
Loading…
Cancel
Save