-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9774 from Youssef1313/avoid-throwing-roslyn-gen
fix: Avoid throwing in DependencyObjectGenerator when using Roslyn
- Loading branch information
Showing
3 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...rceGenerators.net6.Tests/DependencyObjectGeneratorTests/Given_DependenyObjectGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...erators/Uno.UI.SourceGenerators/DependencyObject/DependencyObjectGenerator.Diagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters