From a55762d386e52aceb8a66b26b4a5a8b820b68af3 Mon Sep 17 00:00:00 2001 From: Husqvik Date: Mon, 20 Nov 2023 00:15:31 +0100 Subject: [PATCH] generate Array.Empty() instead of new GraphQlFieldMetadata[0] --- .../GraphQlGenerator.cs | 22 ++++++++++--------- .../Unions | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/GraphQlClientGenerator/GraphQlGenerator.cs b/src/GraphQlClientGenerator/GraphQlGenerator.cs index be19786..22ed3c4 100644 --- a/src/GraphQlClientGenerator/GraphQlGenerator.cs +++ b/src/GraphQlClientGenerator/GraphQlGenerator.cs @@ -425,7 +425,7 @@ private void GenerateDataClassBody(GraphQlType type, ICollection f writer.Write(indentation); writer.Write(" private "); writer.Write(context.GetDataPropertyType(type, field).NetTypeName); - writer.Write(" "); + writer.Write(' '); writer.Write(backingFieldName); writer.WriteLine(";"); } @@ -596,13 +596,13 @@ private string GenerateFileMember(GenerationContext context, string typeName, Gr writer.Write(indentation); writer.Write(GetMemberAccessibility()); - writer.Write(" "); + writer.Write(' '); if (_configuration.GeneratePartialClasses) writer.Write("partial "); writer.Write(graphQlType.Kind is GraphQlTypeKind.Interface ? "interface" : "class"); - writer.Write(" "); + writer.Write(' '); writer.Write(typeName); if (!String.IsNullOrEmpty(baseTypeName)) @@ -718,7 +718,7 @@ private void GenerateDataProperty( writer.Write("public "); writer.Write(propertyTypeName); - writer.Write(" "); + writer.Write(' '); writer.Write(propertyContext.PropertyName); writeBody(propertyTypeDescription with { NetTypeName = propertyTypeName }, GetBackingFieldName(member.Name, propertyContext.RequiresRawName)); @@ -750,11 +750,12 @@ private void GenerateQueryBuilder(GenerationContext context, GraphQlType graphQl CSharpTypeName = className }); + var useCompatibleSyntax = _configuration.CSharpVersion == CSharpVersion.Compatible; var writer = context.Writer; var indentation = GetIndentation(context.Indentation); writer.Write(indentation); writer.Write(GetMemberAccessibility()); - writer.Write(" "); + writer.Write(' '); if (_configuration.GeneratePartialClasses) writer.Write("partial "); @@ -771,7 +772,9 @@ private void GenerateQueryBuilder(GenerationContext context, GraphQlType graphQl if (fields is null) { - writer.WriteLine(" new GraphQlFieldMetadata[0];"); // TODO: Array.Empty(); + writer.Write(' '); + writer.Write(useCompatibleSyntax ? "new GraphQlFieldMetadata[0]" : "Array.Empty()"); + writer.WriteLine(';'); writer.WriteLine(); } else @@ -780,7 +783,7 @@ private void GenerateQueryBuilder(GenerationContext context, GraphQlType graphQl var fieldMetadataIndentation = indentation; - if (_configuration.CSharpVersion == CSharpVersion.Compatible) + if (useCompatibleSyntax) { writer.Write(indentation); writer.WriteLine(" new []"); @@ -848,7 +851,6 @@ private void GenerateQueryBuilder(GenerationContext context, GraphQlType graphQl writer.WriteLine(); } - var useCompatibleSyntax = _configuration.CSharpVersion == CSharpVersion.Compatible; var directiveLocation = GetDirectiveLocation(context.Schema, graphQlType.Name); if (graphQlType.Kind is GraphQlTypeKind.Interface or GraphQlTypeKind.Union) @@ -1274,7 +1276,7 @@ private string WriteDirectiveParameterList( writer.Write(", "); writer.Write(AddQuestionMarkIfNullableReferencesEnabled(directiveClassName)); - writer.Write(" "); + writer.Write(' '); writer.Write(directiveParameterName); writer.Write(" = null"); } @@ -1312,7 +1314,7 @@ private void WriteOverrideProperty(string accessibility, string propertyType, st writer.Write(accessibility); writer.Write(" override "); writer.Write(propertyType); - writer.Write(" "); + writer.Write(' '); writer.Write(propertyName); writer.Write(" { get"); diff --git a/test/GraphQlClientGenerator.Test/ExpectedSingleFileGenerationContext/Unions b/test/GraphQlClientGenerator.Test/ExpectedSingleFileGenerationContext/Unions index 5b00283..0f0949e 100644 --- a/test/GraphQlClientGenerator.Test/ExpectedSingleFileGenerationContext/Unions +++ b/test/GraphQlClientGenerator.Test/ExpectedSingleFileGenerationContext/Unions @@ -112,7 +112,7 @@ public partial class SimpleObjectTypeQueryBuilder : GraphQlQueryBuilder { - private static readonly GraphQlFieldMetadata[] AllFieldMetadata = new GraphQlFieldMetadata[0]; + private static readonly GraphQlFieldMetadata[] AllFieldMetadata = Array.Empty(); public UnionTypeQueryBuilder() => WithTypeName();