-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix generation of nested types that are not classes #211
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.