From b53523b027d648a30bce2cf6c44215f18137eba6 Mon Sep 17 00:00:00 2001 From: Joni Savolainen Date: Sun, 23 Oct 2022 10:51:28 +0300 Subject: [PATCH] fix: ignore abstract classes in code generation --- .../LayoutAugmentGeneratorSnapshotTests.cs | 20 +++++++----- ...ylesheetComponent.Stylesheet.g.verified.cs | 31 ------------------- ...lesheetComponent.Stylesheet.g.verified.cs} | 0 ...lesheetComponent.Stylesheet.g.verified.cs} | 0 ...StylesheetAugmentGeneratorSnapshotTests.cs | 7 +---- .../Generators/UIComponentAugmentGenerator.cs | 3 ++ 6 files changed, 17 insertions(+), 44 deletions(-) delete mode 100644 UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00BaseStylesheetComponent.Stylesheet.g.verified.cs rename UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/{StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ConcreteStylesheetComponent.Stylesheet.g.verified.cs => StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00ConcreteStylesheetComponent.Stylesheet.g.verified.cs} (100%) rename UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/{StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.02ThirdStylesheetComponent.Stylesheet.g.verified.cs => StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ThirdStylesheetComponent.Stylesheet.g.verified.cs} (100%) diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/LayoutAugmentGeneratorSnapshotTests.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/LayoutAugmentGeneratorSnapshotTests.cs index 546feeac..663b864f 100644 --- a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/LayoutAugmentGeneratorSnapshotTests.cs +++ b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/LayoutAugmentGeneratorSnapshotTests.cs @@ -11,7 +11,6 @@ public Task It_Generates_Layout_Call() { var source = @" using UIComponents; -using UIComponents.Experimental; [Layout(""Components/LayoutTestComponent"")] public partial class LayoutTestComponent : UIComponent {} @@ -23,7 +22,7 @@ public partial class LayoutTestComponent : UIComponent {} public Task It_Does_Not_Generate_A_Call_For_Non_UIComponent_Class() { var source = @" -using UIComponents.Experimental; +using UIComponents; [Layout(""Components/LayoutTestComponent"")] public partial class LayoutTestComponent {} @@ -31,12 +30,23 @@ public partial class LayoutTestComponent {} return GeneratorTester.Verify(source); } + [Fact] + public Task It_Does_Not_Generate_For_Abstract_Class() + { + var source = @" +using UIComponents; + +[Layout(""Components/LayoutTestComponent"")] +public partial abstract class AbstractLayoutComponent : UIComponent {} +"; + return GeneratorTester.Verify(source); + } + [Fact] public Task It_Takes_AssetPrefixAttribute_Into_Account() { var source = @" using UIComponents; -using UIComponents.Experimental; [AssetPrefix(""UI/"")] [Layout(""Components/LayoutTestComponent"")] @@ -50,7 +60,6 @@ public Task It_Uses_Base_Class_Attribute() { var source = @" using UIComponents; -using UIComponents.Experimental; [Layout(""Components/BaseLayoutComponent"")] public partial class BaseLayoutComponent : UIComponent {} @@ -67,7 +76,6 @@ public Task It_Allows_Overriding_Base_Class_Attribute() { var source = @" using UIComponents; -using UIComponents.Experimental; [Layout(""Components/BaseLayoutComponent"")] public partial class BaseLayoutComponent : UIComponent {} @@ -83,7 +91,6 @@ public Task It_Handles_Nested_Types() { var source = @" using UIComponents; -using UIComponents.Experimental; public partial class ParentClass { @@ -101,7 +108,6 @@ public Task Does_Not_Generate_If_Layout_Type_Is_Missing() { var source = @" using UIComponents; -using UIComponents.Experimental; [Layout(""Components/LayoutTestComponent"")] public partial class LayoutTestComponent : UIComponent {} diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00BaseStylesheetComponent.Stylesheet.g.verified.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00BaseStylesheetComponent.Stylesheet.g.verified.cs deleted file mode 100644 index 6c7ee3c3..00000000 --- a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00BaseStylesheetComponent.Stylesheet.g.verified.cs +++ /dev/null @@ -1,31 +0,0 @@ -//HintName: BaseStylesheetComponent.Stylesheet.g.cs -// -// This file has been generated automatically by UIComponents.Roslyn. -// Do not attempt to modify it. Any changes will be overridden during compilation. -// - -using System.Threading.Tasks; -using System.CodeDom.Compiler; -using UnityEngine.UIElements; - -public partial class BaseStylesheetComponent -{ - [GeneratedCode("UIComponents.Roslyn.Generation", "1.0.0-alpha.1")] - private async Task UIC_GetSingleStyleSheet(string assetPath) - { - var styleSheet = await AssetResolver.LoadAsset(assetPath); - return new StyleSheetLoadTuple(assetPath, styleSheet); - } - - [GeneratedCode("UIComponents.Roslyn.Generation", "1.0.0-alpha.1")] - protected override Task[] UIC_StartStyleSheetLoad() - { - var assetPaths = new string[] { "Components/BaseStylesheet" }; - var styleSheetLoadTasks = new Task[assetPaths.Length]; - - for (var i = 0; i < assetPaths.Length; i++) - styleSheetLoadTasks[i] = UIC_GetSingleStyleSheet(assetPaths[i]); - - return styleSheetLoadTasks; - } -} diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ConcreteStylesheetComponent.Stylesheet.g.verified.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00ConcreteStylesheetComponent.Stylesheet.g.verified.cs similarity index 100% rename from UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ConcreteStylesheetComponent.Stylesheet.g.verified.cs rename to UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.00ConcreteStylesheetComponent.Stylesheet.g.verified.cs diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.02ThirdStylesheetComponent.Stylesheet.g.verified.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ThirdStylesheetComponent.Stylesheet.g.verified.cs similarity index 100% rename from UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.02ThirdStylesheetComponent.Stylesheet.g.verified.cs rename to UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/Snapshots/StylesheetAugmentGeneratorSnapshotTests.It_Inherits_Base_Class_Stylesheets.01ThirdStylesheetComponent.Stylesheet.g.verified.cs diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/StylesheetAugmentGeneratorSnapshotTests.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/StylesheetAugmentGeneratorSnapshotTests.cs index 1769168e..3633a38b 100644 --- a/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/StylesheetAugmentGeneratorSnapshotTests.cs +++ b/UIComponents.Roslyn/UIComponents.Roslyn.Generation.Tests/StylesheetAugmentGeneratorSnapshotTests.cs @@ -11,7 +11,6 @@ public Task It_Generates_Stylesheet_Call() { var source = @" using UIComponents; -using UIComponents.Experimental; [Stylesheet(""Components/StylesheetTestComponentStyle"")] public partial class StylesheetTestComponent : UIComponent {} @@ -23,7 +22,7 @@ public partial class StylesheetTestComponent : UIComponent {} public Task It_Does_Not_Generate_A_Call_For_Non_UIComponent_Class() { var source = @" -using UIComponents.Experimental; +using UIComponents; [Stylesheet(""Components/StylesheetTestComponentStyle"")] public partial class StylesheetTestComponent {} @@ -36,7 +35,6 @@ public Task It_Takes_AssetPrefixAttribute_Into_Account() { var source = @" using UIComponents; -using UIComponents.Experimental; [AssetPrefix(""UI/"")] [Stylesheet(""Components/StylesheetTestComponentStyle"")] @@ -50,7 +48,6 @@ public Task It_Inherits_Base_Class_Stylesheets() { var source = @" using UIComponents; -using UIComponents.Experimental; [Stylesheet(""Components/BaseStylesheet"")] public abstract partial class BaseStylesheetComponent : UIComponent {} @@ -70,7 +67,6 @@ public Task It_Handles_Nested_Types() { var source = @" using UIComponents; -using UIComponents.Experimental; public partial class ParentClass { @@ -88,7 +84,6 @@ public Task Does_Not_Generate_If_Stylesheet_Type_Is_Missing() { var source = @" using UIComponents; -using UIComponents.Experimental; [Stylesheet(""Components/StylesheetTestComponentStyle"")] public partial class StylesheetTestComponent : UIComponent {} diff --git a/UIComponents.Roslyn/UIComponents.Roslyn.Generation/Generators/UIComponentAugmentGenerator.cs b/UIComponents.Roslyn/UIComponents.Roslyn.Generation/Generators/UIComponentAugmentGenerator.cs index 3a9d3664..d4ed0d03 100644 --- a/UIComponents.Roslyn/UIComponents.Roslyn.Generation/Generators/UIComponentAugmentGenerator.cs +++ b/UIComponents.Roslyn/UIComponents.Roslyn.Generation/Generators/UIComponentAugmentGenerator.cs @@ -54,6 +54,9 @@ protected override bool ShouldGenerateSource(AugmentGenerationContext context) if (UIComponentSymbol == null || AssetPrefixAttributeSymbol == null) return false; + if (context.CurrentTypeSymbol.IsAbstract) + return false; + CurrentAssetPrefix = GetPathAttributeValue(AssetPrefixAttributeSymbol, context); return RoslynUtilities.HasBaseType(context.CurrentTypeSymbol, UIComponentSymbol);