diff --git a/src/Language/Printer.php b/src/Language/Printer.php index edf5510..0e5566d 100644 --- a/src/Language/Printer.php +++ b/src/Language/Printer.php @@ -307,12 +307,14 @@ class Printer */ public function block($array) { - return $array && $this->length($array) ? $this->indent("{\n" . $this->join($array, "\n")) . "\n}" : '{}'; + return ($array && $this->length($array)) + ? "{\n" . $this->indent($this->join($array, "\n")) . "\n}" + : '{}'; } public function indent($maybeString) { - return $maybeString ? str_replace("\n", "\n ", $maybeString) : ''; + return $maybeString ? ' ' . str_replace("\n", "\n ", $maybeString) : ''; } public function manyList($start, $list, $separator, $end) @@ -344,12 +346,9 @@ class Printer * a single-line, adding a leading blank line would strip that whitespace. */ private function printBlockString($value, $isDescription) { + $escaped = str_replace('"""', '\\"""', $value); return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false) - ? ('"""' . str_replace('"""', '\\"""', $value) . '"""') - : ( - $isDescription - ? ("\"\"\"\n" . str_replace('"""', '\\"""', $value) . "\n\"\"\"") - : ($this->indent("\"\"\"\n" . str_replace('"""', '\\"""', $value)) . "\n\"\"\"") - ); + ? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""') + : ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\""); } } diff --git a/tests/Language/PrinterTest.php b/tests/Language/PrinterTest.php index 301abff..42bc0bc 100644 --- a/tests/Language/PrinterTest.php +++ b/tests/Language/PrinterTest.php @@ -106,7 +106,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase '; $this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); } - + /** * @it correctly prints block strings with a first line indentation */ @@ -132,6 +132,25 @@ class PrinterTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); } + /** + * @it correctly prints single-line with leading space and quotation + */ + public function testCorrectlyPrintsSingleLineStringsWithLeadingSpaceAndQuotation() + { + $mutationAstWithArtifacts = Parser::parse( + '{ + field(arg: """ space-led value "quoted string" + """) +}' + ); + $expected = '{ + field(arg: """ space-led value "quoted string" + """) +} +'; + $this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); + } + /** * @it prints kitchen sink */