Fix print of block string with leading space and quotation

ref: graphql/graphql-js#1190
This commit is contained in:
Daniel Tschinder 2018-02-08 19:49:40 +01:00
parent 022c490011
commit 7705e50e44
2 changed files with 27 additions and 9 deletions

View File

@ -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\"\"\"");
}
}

View File

@ -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
*/