From d809578886ed04a0f7f154b0848944d1a4d92059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=C5=A1ek=20Henzl?= Date: Mon, 13 Sep 2021 13:09:42 +1200 Subject: [PATCH 1/2] Add support for empty descriptions --- src/Language/BlockString.php | 6 +++--- src/Utils/ASTDefinitionBuilder.php | 3 ++- src/Utils/SchemaPrinter.php | 2 +- tests/Language/BlockStringTest.php | 8 ++++++++ tests/Utils/SchemaPrinterTest.php | 21 +++++++++++++++++++++ 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Language/BlockString.php b/src/Language/BlockString.php index 1b60dc639..e27e718cf 100644 --- a/src/Language/BlockString.php +++ b/src/Language/BlockString.php @@ -118,9 +118,9 @@ public static function print( ): string { $valueLength = mb_strlen($value); $isSingleLine = strpos($value, "\n") === false; - $hasLeadingSpace = $value[0] === ' ' || $value[0] === '\t'; - $hasTrailingQuote = $value[$valueLength - 1] === '"'; - $hasTrailingSlash = $value[$valueLength - 1] === '\\'; + $hasLeadingSpace = $value !== '' && ($value[0] === ' ' || $value[0] === '\t'); + $hasTrailingQuote = $value !== '' && $value[$valueLength - 1] === '"'; + $hasTrailingSlash = $value !== '' && $value[$valueLength - 1] === '\\'; $printAsMultipleLines = ! $isSingleLine || $hasTrailingQuote diff --git a/src/Utils/ASTDefinitionBuilder.php b/src/Utils/ASTDefinitionBuilder.php index a4888ad60..970ef9ef6 100644 --- a/src/Utils/ASTDefinitionBuilder.php +++ b/src/Utils/ASTDefinitionBuilder.php @@ -37,6 +37,7 @@ use Throwable; use function array_reverse; +use function count; use function implode; use function is_array; use function is_string; @@ -137,7 +138,7 @@ private function getLeadingCommentBlock(Node $node): ?string $token = $token->prev; } - return implode("\n", array_reverse($comments)); + return count($comments) > 0 ? implode("\n", array_reverse($comments)) : null; } /** diff --git a/src/Utils/SchemaPrinter.php b/src/Utils/SchemaPrinter.php index ed85e1e97..771094193 100644 --- a/src/Utils/SchemaPrinter.php +++ b/src/Utils/SchemaPrinter.php @@ -164,7 +164,7 @@ protected static function printDirective(Directive $directive, array $options): */ protected static function printDescription(array $options, $def, string $indentation = '', bool $firstInBlock = true): string { - if ($def->description === null || $def->description === '') { + if ($def->description === null) { return ''; } diff --git a/tests/Language/BlockStringTest.php b/tests/Language/BlockStringTest.php index 1dcf81905..b2071df24 100644 --- a/tests/Language/BlockStringTest.php +++ b/tests/Language/BlockStringTest.php @@ -255,4 +255,12 @@ public function testCorrectlyPrintsStringWithAFirstLineIndentation(): void BlockString::print($str) ); } + + public function testCorrectlyPrintsEmptyString(): void + { + $str = ''; + + self::assertEquals('""""""', BlockString::print($str)); + self::assertEquals("\"\"\"\n\n\"\"\"", BlockString::print($str, '', true)); + } } diff --git a/tests/Utils/SchemaPrinterTest.php b/tests/Utils/SchemaPrinterTest.php index f0decb202..2042c49f0 100644 --- a/tests/Utils/SchemaPrinterTest.php +++ b/tests/Utils/SchemaPrinterTest.php @@ -858,6 +858,27 @@ public function testPrintsCustomDirectives(): void ); } + /** + * @see it('Prints an empty description') + */ + public function testPrintsAnEmptyDescription(): void + { + $output = $this->printSingleFieldSchema([ + 'type' => Type::string(), + 'description' => '', + ]); + + self::assertEquals( + ' +type Query { + """""" + singleField: String +} +', + $output + ); + } + /** * @see it('One-line prints a short description') */ From 840a9e6f62e1f7522fdcc71cbd26f49b51aca67d Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Fri, 1 Oct 2021 10:04:28 +0200 Subject: [PATCH 2/2] Multiline ternary --- src/Utils/ASTDefinitionBuilder.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Utils/ASTDefinitionBuilder.php b/src/Utils/ASTDefinitionBuilder.php index 970ef9ef6..80110e75a 100644 --- a/src/Utils/ASTDefinitionBuilder.php +++ b/src/Utils/ASTDefinitionBuilder.php @@ -138,7 +138,9 @@ private function getLeadingCommentBlock(Node $node): ?string $token = $token->prev; } - return count($comments) > 0 ? implode("\n", array_reverse($comments)) : null; + return count($comments) > 0 + ? implode("\n", array_reverse($comments)) + : null; } /**