From 55b45d40d9021667cee43f09a168d21a7ad91b7f Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 11 Nov 2025 20:27:22 -0700 Subject: [PATCH] Fixed warnings caused by generators. Pushing to v2_develop as a test. Refactored `GenerateMethods` to replace LINQ with explicit loops, improving readability and maintainability. Added support for `notnull` and `new()` constraints and ensured constraint clauses are only added when necessary. Streamlined `switch` statements for `RefKind` modifiers and default parameter values to use more compact syntax. Updated exception message formatting for clarity. Commented out unused "Throws" method generation. Removed an extraneous closing brace for cleanup. --- .../TheGenerator.cs | 114 +++++++++++------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/Tests/TerminalGuiFluentTestingXunit.Generator/TheGenerator.cs b/Tests/TerminalGuiFluentTestingXunit.Generator/TheGenerator.cs index 0d319b618..eafae63c1 100644 --- a/Tests/TerminalGuiFluentTestingXunit.Generator/TheGenerator.cs +++ b/Tests/TerminalGuiFluentTestingXunit.Generator/TheGenerator.cs @@ -28,7 +28,7 @@ public class TheGenerator : IIncrementalGenerator private void Execute (SourceProductionContext context, (Compilation Left, ImmutableArray Right) arg2) { INamedTypeSymbol assertType = arg2.Left.GetTypeByMetadataName ("Xunit.Assert") - ?? throw new NotSupportedException("Referencing codebase does not include Xunit, could not find Xunit.Assert"); + ?? throw new NotSupportedException ("Referencing codebase does not include Xunit, could not find Xunit.Assert"); GenerateMethods (assertType, context, "Equal", false); @@ -70,7 +70,7 @@ public class TheGenerator : IIncrementalGenerator GenerateMethods (assertType, context, "Subset", true); GenerateMethods (assertType, context, "Superset", true); -// GenerateMethods (assertType, context, "Throws", true); + // GenerateMethods (assertType, context, "Throws", true); // GenerateMethods (assertType, context, "ThrowsAny", true); GenerateMethods (assertType, context, "True", false); } @@ -208,25 +208,49 @@ public class TheGenerator : IIncrementalGenerator dec = dec.WithTypeParameterList (SyntaxFactory.TypeParameterList (SyntaxFactory.SeparatedList (typeParameters))); // Handle type parameter constraints - List constraintClauses = methodSymbol.TypeParameters - .Where (tp => tp.ConstraintTypes.Length > 0) - .Select ( - tp => - SyntaxFactory.TypeParameterConstraintClause (tp.Name) - .WithConstraints ( - SyntaxFactory - .SeparatedList ( - tp.ConstraintTypes.Select ( - constraintType => - SyntaxFactory.TypeConstraint ( - SyntaxFactory.ParseTypeName ( - constraintType - .ToDisplayString ())) - ) - ) - ) - ) - .ToList (); + List constraintClauses = new (); + + foreach (ITypeParameterSymbol tp in methodSymbol.TypeParameters) + { + List constraints = new (); + + // Add class/struct constraints + if (tp.HasReferenceTypeConstraint) + { + constraints.Add (SyntaxFactory.ClassOrStructConstraint (SyntaxKind.ClassConstraint)); + } + else if (tp.HasValueTypeConstraint) + { + constraints.Add (SyntaxFactory.ClassOrStructConstraint (SyntaxKind.StructConstraint)); + } + else if (tp.HasNotNullConstraint) + { + // Add notnull constraint + constraints.Add (SyntaxFactory.TypeConstraint (SyntaxFactory.IdentifierName ("notnull"))); + } + + // Add type constraints + foreach (ITypeSymbol constraintType in tp.ConstraintTypes) + { + constraints.Add ( + SyntaxFactory.TypeConstraint ( + SyntaxFactory.ParseTypeName (constraintType.ToDisplayString ()))); + } + + // Add new() constraint + if (tp.HasConstructorConstraint) + { + constraints.Add (SyntaxFactory.ConstructorConstraint ()); + } + + // Only add constraint clause if there are constraints + if (constraints.Any ()) + { + constraintClauses.Add ( + SyntaxFactory.TypeParameterConstraintClause (tp.Name) + .WithConstraints (SyntaxFactory.SeparatedList (constraints))); + } + } if (constraintClauses.Any ()) { @@ -281,12 +305,12 @@ public class TheGenerator : IIncrementalGenerator if (p.RefKind != RefKind.None) { SyntaxKind modifierKind = p.RefKind switch - { - RefKind.Ref => SyntaxKind.RefKeyword, - RefKind.Out => SyntaxKind.OutKeyword, - RefKind.In => SyntaxKind.InKeyword, - _ => throw new NotSupportedException ($"Unsupported RefKind: {p.RefKind}") - }; + { + RefKind.Ref => SyntaxKind.RefKeyword, + RefKind.Out => SyntaxKind.OutKeyword, + RefKind.In => SyntaxKind.InKeyword, + _ => throw new NotSupportedException ($"Unsupported RefKind: {p.RefKind}") + }; modifiers.Add (SyntaxFactory.Token (modifierKind)); @@ -302,23 +326,23 @@ public class TheGenerator : IIncrementalGenerator if (p.HasExplicitDefaultValue) { ExpressionSyntax defaultValueExpression = p.ExplicitDefaultValue switch - { - null => SyntaxFactory.LiteralExpression (SyntaxKind.NullLiteralExpression), - bool b => SyntaxFactory.LiteralExpression ( - b - ? SyntaxKind.TrueLiteralExpression - : SyntaxKind.FalseLiteralExpression), - int i => SyntaxFactory.LiteralExpression ( - SyntaxKind.NumericLiteralExpression, - SyntaxFactory.Literal (i)), - double d => SyntaxFactory.LiteralExpression ( - SyntaxKind.NumericLiteralExpression, - SyntaxFactory.Literal (d)), - string s => SyntaxFactory.LiteralExpression ( - SyntaxKind.StringLiteralExpression, - SyntaxFactory.Literal (s)), - _ => SyntaxFactory.ParseExpression (p.ExplicitDefaultValue.ToString ()) // Fallback - }; + { + null => SyntaxFactory.LiteralExpression (SyntaxKind.NullLiteralExpression), + bool b => SyntaxFactory.LiteralExpression ( + b + ? SyntaxKind.TrueLiteralExpression + : SyntaxKind.FalseLiteralExpression), + int i => SyntaxFactory.LiteralExpression ( + SyntaxKind.NumericLiteralExpression, + SyntaxFactory.Literal (i)), + double d => SyntaxFactory.LiteralExpression ( + SyntaxKind.NumericLiteralExpression, + SyntaxFactory.Literal (d)), + string s => SyntaxFactory.LiteralExpression ( + SyntaxKind.StringLiteralExpression, + SyntaxFactory.Literal (s)), + _ => SyntaxFactory.ParseExpression (p.ExplicitDefaultValue.ToString ()) // Fallback + }; parameterSyntax = parameterSyntax.WithDefault ( SyntaxFactory.EqualsValueClause (defaultValueExpression) @@ -330,4 +354,4 @@ public class TheGenerator : IIncrementalGenerator // Helper method to check if a parameter name is a reserved keyword private bool IsReservedKeyword (string name) { return string.Equals (name, "object"); } -} +} \ No newline at end of file