Skip to content

Commit

Permalink
Merge pull request #9774 from Youssef1313/avoid-throwing-roslyn-gen
Browse files Browse the repository at this point in the history
fix: Avoid throwing in DependencyObjectGenerator when using Roslyn
  • Loading branch information
jeromelaban authored Sep 9, 2022
2 parents 43e2636 + c43a639 commit a58c25d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Uno.UI.SourceGenerators.DependencyObject;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using System.Collections.Immutable;

namespace Uno.UI.SourceGenerators.Tests.DependencyObjectGeneratorTests;

[TestClass]
public class Given_DependenyObjectGenerator
{
private async Task TestAndroid(string testCode, params DiagnosticResult[] expectedDiagnostics)
{
var test = new CSharpSourceGeneratorTest<DependencyObjectGenerator, MSTestVerifier>()
{
TestState =
{
Sources = { testCode },
},
ReferenceAssemblies = ReferenceAssemblies.Net.Net60Android.AddPackages(ImmutableArray.Create(new PackageIdentity("Uno.UI", "4.4.20"))),
};
test.ExpectedDiagnostics.AddRange(expectedDiagnostics);
await test.RunAsync();
}

[TestMethod]
public async Task TestAndroidViewImplementingDependencyObject()
{
await TestAndroid(@"
using Android.Content;
using Windows.UI.Core;
using Windows.UI.Xaml;
public class C : Android.Views.View, DependencyObject
{
public C(Context context) : base(context)
{
}
public CoreDispatcher Dispatcher { get; }
public object GetValue(DependencyProperty dp) => null;
public void SetValue(DependencyProperty dp, object value) { }
public void ClearValue(DependencyProperty dp) { }
public object ReadLocalValue(DependencyProperty dp) => null;
public object GetAnimationBaseValue(DependencyProperty dp) => null;
public long RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback) => 0;
public void UnregisterPropertyChangedCallback(DependencyProperty dp, long token) { }
}",
// /0/Test0.cs(6,14): error Uno0003: 'Android.Views.View' shouldn't implement 'DependencyObject'. Inherit 'FrameworkElement' instead.
DiagnosticResult.CompilerError("Uno0003").WithSpan(6, 14, 6, 15).WithArguments("Android.Views.View"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#nullable enable

using System;
using Microsoft.CodeAnalysis;

#if NETFRAMEWORK
using Uno.SourceGeneration;
#endif

namespace Uno.UI.SourceGenerators.DependencyObject;
public partial class DependencyObjectGenerator
{
private static readonly DiagnosticDescriptor _descriptor = new(
#pragma warning disable RS2008 // Enable analyzer release tracking
id: "Uno0003",
#pragma warning restore RS2008 // Enable analyzer release tracking
title: "Native view shouldn't implement 'DependencyObject'",
messageFormat: "'{0}' shouldn't implement 'DependencyObject'. Inherit 'FrameworkElement' instead.",
category: "Usage",
DiagnosticSeverity.Error,
isEnabledByDefault: true,
helpLinkUri: "https://github.com/unoplatform/uno/issues/6758#issuecomment-898544729",
customTags: WellKnownDiagnosticTags.NotConfigurable);

private static void ReportDiagnostic(GeneratorExecutionContext context, Diagnostic diagnostic)
{
#if NETFRAMEWORK
throw new InvalidOperationException(diagnostic.GetMessage());
#else
context.ReportDiagnostic(diagnostic);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace Uno.UI.SourceGenerators.DependencyObject
{
[Generator]
public class DependencyObjectGenerator : ISourceGenerator
public partial class DependencyObjectGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
Expand Down Expand Up @@ -124,15 +124,18 @@ private void ProcessType(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.Is(_iosViewSymbol))
{
throw new InvalidOperationException("A 'UIKit.UIView' shouldn't implement 'DependencyObject'. Inherit 'FrameworkElement' instead.");
ReportDiagnostic(_context, Diagnostic.Create(_descriptor, typeSymbol.Locations[0], "UIKit.UIView"));
return;
}
else if (typeSymbol.Is(_androidViewSymbol))
{
throw new InvalidOperationException("An 'Android.Views.View' shouldn't implement 'DependencyObject'. Inherit 'FrameworkElement' instead.");
ReportDiagnostic(_context, Diagnostic.Create(_descriptor, typeSymbol.Locations[0], "Android.Views.View"));
return;
}
else if (typeSymbol.Is(_macosViewSymbol))
{
throw new InvalidOperationException("An 'AppKit.NSView' shouldn't implement 'DependencyObject'. Inherit 'FrameworkElement' instead.");
ReportDiagnostic(_context, Diagnostic.Create(_descriptor, typeSymbol.Locations[0], "AppKit.NSView"));
return;
}
}

Expand Down

0 comments on commit a58c25d

Please sign in to comment.