Skip to content

Commit 4f4a635

Browse files
committed
Add support for empty descriptions
1 parent eb0675e commit 4f4a635

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/Language/BlockString.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ public static function print(
118118
): string {
119119
$valueLength = mb_strlen($value);
120120
$isSingleLine = strpos($value, "\n") === false;
121-
$hasLeadingSpace = $value[0] === ' ' || $value[0] === '\t';
122-
$hasTrailingQuote = $value[$valueLength - 1] === '"';
123-
$hasTrailingSlash = $value[$valueLength - 1] === '\\';
121+
$hasLeadingSpace = $value !== '' && ($value[0] === ' ' || $value[0] === '\t');
122+
$hasTrailingQuote = $value !== '' && $value[$valueLength - 1] === '"';
123+
$hasTrailingSlash = $value !== '' && $value[$valueLength - 1] === '\\';
124124
$printAsMultipleLines =
125125
! $isSingleLine ||
126126
$hasTrailingQuote ||

src/Utils/ASTDefinitionBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Throwable;
3838

3939
use function array_reverse;
40+
use function count;
4041
use function implode;
4142
use function is_array;
4243
use function is_string;
@@ -137,7 +138,7 @@ private function getLeadingCommentBlock(Node $node): ?string
137138
$token = $token->prev;
138139
}
139140

140-
return implode("\n", array_reverse($comments));
141+
return count($comments) > 0 ? implode("\n", array_reverse($comments)) : null;
141142
}
142143

143144
/**

src/Utils/SchemaPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected static function printDirective(Directive $directive, array $options):
163163
*/
164164
protected static function printDescription(array $options, $def, string $indentation = '', bool $firstInBlock = true): string
165165
{
166-
if ($def->description === null || $def->description === '') {
166+
if ($def->description === null) {
167167
return '';
168168
}
169169

tests/Language/BlockStringTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,12 @@ public function testCorrectlyPrintsStringWithAFirstLineIndentation(): void
255255
BlockString::print($str)
256256
);
257257
}
258+
259+
public function testCorrectlyPrintsEmptyString(): void
260+
{
261+
$str = '';
262+
263+
self::assertEquals('""""""', BlockString::print($str));
264+
self::assertEquals("\"\"\"\n\n\"\"\"", BlockString::print($str, '', true));
265+
}
258266
}

tests/Utils/SchemaPrinterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,27 @@ public function testPrintsCustomDirectives(): void
824824
);
825825
}
826826

827+
/**
828+
* @see it('Prints an empty description')
829+
*/
830+
public function testPrintsAnEmptyDescription(): void
831+
{
832+
$output = $this->printSingleFieldSchema([
833+
'type' => Type::string(),
834+
'description' => '',
835+
]);
836+
837+
self::assertEquals(
838+
'
839+
type Query {
840+
""""""
841+
singleField: String
842+
}
843+
',
844+
$output
845+
);
846+
}
847+
827848
/**
828849
* @see it('One-line prints a short description')
829850
*/

0 commit comments

Comments
 (0)