From 0123f9dbbe449e081a7a07cabdd56c895f407eec Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Fri, 21 Apr 2023 14:06:09 +0300 Subject: [PATCH] Introduce SDLPrinterOptions.PrintDescriptions (#315) --- src/Directory.Build.props | 2 +- .../GraphQLParser.approved.txt | 1 + .../Visitors/SDLPrinterFromParsedTextTests.cs | 20 ++++++++++++++++--- src/GraphQLParser/Visitors/SDLPrinter.cs | 13 ++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 1b9391d7..e62c13ef 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,7 +1,7 @@ - 9.0.2-preview + 9.1.0-preview latest true $(NoWarn);CA1707 diff --git a/src/GraphQLParser.ApiTests/GraphQLParser.approved.txt b/src/GraphQLParser.ApiTests/GraphQLParser.approved.txt index b470b33c..24dd427f 100644 --- a/src/GraphQLParser.ApiTests/GraphQLParser.approved.txt +++ b/src/GraphQLParser.ApiTests/GraphQLParser.approved.txt @@ -909,6 +909,7 @@ namespace GraphQLParser.Visitors { public SDLPrinterOptions() { } public int IndentSize { get; set; } + public bool PrintDescriptions { get; set; } public bool EachDirectiveLocationOnNewLine { get; init; } public bool EachUnionMemberOnNewLine { get; init; } public bool PrintComments { get; init; } diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs index 354893f4..935d47b9 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs @@ -123,7 +123,7 @@ ... on CustomerType { @"directive @skip(if: Boolean!) on | FIELD | FRAGMENT_SPREAD - | INLINE_FRAGMENT", false, true)] + | INLINE_FRAGMENT", false, true, true)] [InlineData(8, @"directive @twoArgs (a: Int, b: @@ -488,7 +488,7 @@ union Unity | B extend union Unity = - | C", true, false, true)] + | C", true, true, false, true)] [InlineData(38, @"enum Color #comment @@ -574,7 +574,7 @@ directive @skip( mutation: M subscription: S } -""", true, false, false, 5)] +""", true, true, false, false, 5)] [InlineData(45, """ "A component contains the parametric details of a PCB part." @@ -834,11 +834,24 @@ implements Entity & name: String } """)] + [InlineData(58, +"""" +"description" +type Person { +"""description""" +name: String } +"""", +""" +type Person { + name: String +} +""", false, false)] public async Task SDLPrinter_Should_Print_Document( int number, string text, string expected, bool writeComments = true, + bool writeDescriptions = true, bool eachDirectiveLocationOnNewLine = false, bool eachUnionMemberOnNewLine = false, int indentSize = 2) @@ -846,6 +859,7 @@ public async Task SDLPrinter_Should_Print_Document( var printer = new SDLPrinter(new SDLPrinterOptions { PrintComments = writeComments, + PrintDescriptions = writeDescriptions, EachDirectiveLocationOnNewLine = eachDirectiveLocationOnNewLine, EachUnionMemberOnNewLine = eachUnionMemberOnNewLine, IndentSize = indentSize, diff --git a/src/GraphQLParser/Visitors/SDLPrinter.cs b/src/GraphQLParser/Visitors/SDLPrinter.cs index 62407306..56248ca8 100644 --- a/src/GraphQLParser/Visitors/SDLPrinter.cs +++ b/src/GraphQLParser/Visitors/SDLPrinter.cs @@ -132,6 +132,9 @@ async ValueTask WriteMultilineBlockString() ValueTask WriteString() => WriteEncodedStringAsync(context, description.Value); + if (!Options.PrintDescriptions) + return default; + // http://spec.graphql.org/October2021/#StringValue return ShouldBeMultilineBlockString() ? WriteMultilineBlockString() @@ -1111,21 +1114,31 @@ public class SDLPrinterOptions { /// /// Print comments into the output. + /// By default . /// public bool PrintComments { get; init; } + /// + /// Print descriptions into the output. + /// By default . + /// + public bool PrintDescriptions { get; set; } = true; + /// /// Whether to print each directive location on its own line. + /// By default . /// public bool EachDirectiveLocationOnNewLine { get; init; } /// /// Whether to print each union member on its own line. + /// By default . /// public bool EachUnionMemberOnNewLine { get; init; } /// /// The size of the horizontal indentation in spaces. + /// By default 2. /// public int IndentSize { get; set; } = 2; }