Skip to content

Commit

Permalink
fix: ignore using declarations with alias during codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Nov 26, 2022
1 parent 5a1522f commit dc3331c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,45 @@ public partial class MultipleQueryComponent : UIComponent
return GeneratorTester.Verify<QueryAugmentGenerator>(source);
}

[Fact]
public Task It_Handles_Using_Declaration_With_Alias()
{
var firstSource = @"
namespace UnityEngine.UIElements
{
public class Button : VisualElement {}
}
namespace MyLibrary
{
public class Button : UnityEngine.UIElements.VisualElement {}
}
";
var secondSource = @"
using System.Collections.Generic;
using UnityEngine.UIElements;
using Button = MyLibrary.Button;
using UIComponents;
public partial class UsingAliasComponent : UIComponent
{
[Query]
[Query(Class = ""test"")]
public Button firstButton;
[Query(""uxml-name"", Class = ""class"")]
[Query(Class = ""class-name"")]
public Button[] buttonArray;
[Query(Name = ""name"" Class = ""class"")]
[Query(Name = ""other-name"" Class = ""other-class"")]
[Query(Name = ""third-name"" Class = ""third-class"")]
public List<Button> buttonList;
}
";
return GeneratorTester.Verify<QueryAugmentGenerator>(firstSource, secondSource);
}

[Fact]
public Task It_Handles_Common_Namespaces()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//HintName: UsingAliasComponent.Query.g.cs
// <auto-generated>
// This file has been generated automatically by UIComponents.Roslyn.
// Do not attempt to modify it. Any changes will be overridden during compilation.
// </auto-generated>

using System.Collections.Generic;
using UnityEngine.UIElements;
using UIComponents;
using System.CodeDom.Compiler;

public partial class UsingAliasComponent
{
[GeneratedCode("UIComponents.Roslyn.Generation", "1.0.0-alpha.4")]
protected override void UIC_PopulateQueryFields()
{
// firstButton
var UIC_firstButtonList = new List<MyLibrary.Button>();
this.Query<MyLibrary.Button>(null, (string) null).ToList(UIC_firstButtonList);
this.Query<MyLibrary.Button>(null, "test").ToList(UIC_firstButtonList);
if (UIC_firstButtonList.Count == 0)
Logger.LogError("Query (firstButton): No instances of MyLibrary.Button found", this);
if (UIC_firstButtonList.Count > 0)
firstButton = UIC_firstButtonList[0];

// buttonArray
var UIC_buttonArrayList = new List<MyLibrary.Button>();
this.Query<MyLibrary.Button>("uxml-name", "class").ToList(UIC_buttonArrayList);
this.Query<MyLibrary.Button>(null, "class-name").ToList(UIC_buttonArrayList);
if (UIC_buttonArrayList.Count == 0)
Logger.LogError("Query (buttonArray): No instances of MyLibrary.Button found", this);
buttonArray = new MyLibrary.Button[UIC_buttonArrayList.Count];
for (var i = 0; i < UIC_buttonArrayList.Count; i++)
buttonArray[i] = UIC_buttonArrayList[i];

// buttonList
var UIC_buttonListList = new List<MyLibrary.Button>();
this.Query<MyLibrary.Button>("name", "class").ToList(UIC_buttonListList);
this.Query<MyLibrary.Button>("other-name", "other-class").ToList(UIC_buttonListList);
this.Query<MyLibrary.Button>("third-name", "third-class").ToList(UIC_buttonListList);
if (UIC_buttonListList.Count == 0)
Logger.LogError("Query (buttonList): No instances of MyLibrary.Button found", this);
buttonList = UIC_buttonListList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private void ExecuteForType(TypeDeclarationSyntax node, GeneratorExecutionContex
return;

var usingsList = compilationUnitSyntax.Usings
.Where((declaration) => declaration.Alias == null)
.Select((declaration) => declaration.Name.ToString())
.ToList();

Expand Down

0 comments on commit dc3331c

Please sign in to comment.