-
Notifications
You must be signed in to change notification settings - Fork 305
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 #211 from CommunityToolkit/dev/fix-non-classes-nes…
…ted-types Fix generation of nested types that are not classes
- Loading branch information
Showing
4 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
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
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
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,46 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace CommunityToolkit.Mvvm.SourceGenerators.Models; | ||
|
||
/// <summary> | ||
/// A model describing a type info in a type hierarchy. | ||
/// </summary> | ||
/// <param name="QualifiedName">The qualified name for the type.</param> | ||
/// <param name="Kind">The type of the type in the hierarchy.</param> | ||
/// <param name="IsRecord">Whether the type is a record type.</param> | ||
internal sealed record TypeInfo(string QualifiedName, TypeKind Kind, bool IsRecord) | ||
{ | ||
/// <summary> | ||
/// Creates a <see cref="TypeDeclarationSyntax"/> instance for the current info. | ||
/// </summary> | ||
/// <returns>A <see cref="TypeDeclarationSyntax"/> instance for the current info.</returns> | ||
public TypeDeclarationSyntax GetSyntax() | ||
{ | ||
// Create the partial type declaration with the kind. | ||
// This code produces a class declaration as follows: | ||
// | ||
// <TYPE_KIND> <TYPE_NAME> | ||
// { | ||
// } | ||
// | ||
// Note that specifically for record declarations, we also need to explicitly add the open | ||
// and close brace tokens, otherwise member declarations will not be formatted correctly. | ||
return Kind switch | ||
{ | ||
TypeKind.Struct => StructDeclaration(QualifiedName), | ||
TypeKind.Interface => InterfaceDeclaration(QualifiedName), | ||
TypeKind.Class when IsRecord => | ||
RecordDeclaration(Token(SyntaxKind.RecordKeyword), QualifiedName) | ||
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken)) | ||
.WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken)), | ||
_ => ClassDeclaration(QualifiedName) | ||
}; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/CommunityToolkit.Mvvm.UnitTests/Test_SourceGenerators.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,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace CommunityToolkit.Mvvm.UnitTests; | ||
|
||
/// <summary> | ||
/// This class contains general unit tests for source generators, without a specific dependency on one. | ||
/// For instance, this can be used for tests that validate common generation helpers used by all generators. | ||
/// </summary> | ||
[TestClass] | ||
public partial class Test_SourceGenerators | ||
{ | ||
[TestMethod] | ||
public void Test_SourceGenerators_NestedTypesThatAreNotJustClasses() | ||
{ | ||
// This test just needs to compile, mostly | ||
NestedStructType.NestedInterfaceType.NestedRecord.MyViewModel model = new(); | ||
|
||
Assert.IsNull(model.Name); | ||
Assert.IsTrue(model.TestCommand is IRelayCommand); | ||
} | ||
|
||
public partial struct NestedStructType | ||
{ | ||
public partial interface NestedInterfaceType | ||
{ | ||
public partial record NestedRecord | ||
{ | ||
[ObservableRecipient] | ||
public partial class MyViewModel : ObservableValidator | ||
{ | ||
[ObservableProperty] | ||
[Required] | ||
private string? name; | ||
|
||
[ICommand] | ||
private void Test() | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void Test_SourceGenerators_NestedTypesThatAreNotJustClassesAndWithGenerics() | ||
{ | ||
// This test just needs to compile, mostly | ||
NestedStructTypeWithGenerics<int, float>.NestedInterfaceType<string>.NestedRecord<string>.MyViewModel model = new(); | ||
|
||
Assert.IsNull(model.Name); | ||
Assert.IsTrue(model.TestCommand is IRelayCommand); | ||
} | ||
|
||
public partial struct NestedStructTypeWithGenerics<T1, T2> | ||
where T2 : struct | ||
{ | ||
public partial interface NestedInterfaceType<TFoo> | ||
{ | ||
public partial record NestedRecord<TBar> | ||
{ | ||
[INotifyPropertyChanged] | ||
public partial class MyViewModel | ||
{ | ||
[ObservableProperty] | ||
private string? name; | ||
|
||
[ICommand] | ||
private void Test() | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |