Skip to content

Commit 18556f6

Browse files
committed
Fix print of block string with leading space and quotation
ref: graphql/graphql-js#1190
1 parent 88e2ed4 commit 18556f6

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/Language/Printer.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,14 @@ public function wrap($start, $maybeString, $end = '')
307307
*/
308308
public function block($array)
309309
{
310-
return $array && $this->length($array) ? $this->indent("{\n" . $this->join($array, "\n")) . "\n}" : '{}';
310+
return ($array && $this->length($array))
311+
? "{\n" . $this->indent($this->join($array, "\n")) . "\n}"
312+
: '{}';
311313
}
312314

313315
public function indent($maybeString)
314316
{
315-
return $maybeString ? str_replace("\n", "\n ", $maybeString) : '';
317+
return $maybeString ? ' ' . str_replace("\n", "\n ", $maybeString) : '';
316318
}
317319

318320
public function manyList($start, $list, $separator, $end)
@@ -344,12 +346,9 @@ function($x) { return !!$x;}
344346
* a single-line, adding a leading blank line would strip that whitespace.
345347
*/
346348
private function printBlockString($value, $isDescription) {
349+
$escaped = str_replace('"""', '\\"""', $value);
347350
return (($value[0] === ' ' || $value[0] === "\t") && strpos($value, "\n") === false)
348-
? ('"""' . str_replace('"""', '\\"""', $value) . '"""')
349-
: (
350-
$isDescription
351-
? ("\"\"\"\n" . str_replace('"""', '\\"""', $value) . "\n\"\"\"")
352-
: ($this->indent("\"\"\"\n" . str_replace('"""', '\\"""', $value)) . "\n\"\"\"")
353-
);
351+
? ('"""' . preg_replace('/"$/', "\"\n", $escaped) . '"""')
352+
: ("\"\"\"\n" . ($isDescription ? $escaped : $this->indent($escaped)) . "\n\"\"\"");
354353
}
355354
}

tests/Language/PrinterTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function testCorrectlyPrintsSingleLineBlockStringsWithLeadingSpace()
106106
';
107107
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
108108
}
109-
109+
110110
/**
111111
* @it correctly prints block strings with a first line indentation
112112
*/
@@ -132,6 +132,25 @@ public function testCorrectlyPrintsBlockStringsWithAFirstLineIndentation()
132132
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
133133
}
134134

135+
/**
136+
* @it correctly prints single-line with leading space and quotation
137+
*/
138+
public function testCorrectlyPrintsSingleLineStringsWithLeadingSpaceAndQuotation()
139+
{
140+
$mutationAstWithArtifacts = Parser::parse(
141+
'{
142+
field(arg: """ space-led value "quoted string"
143+
""")
144+
}'
145+
);
146+
$expected = '{
147+
field(arg: """ space-led value "quoted string"
148+
""")
149+
}
150+
';
151+
$this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts));
152+
}
153+
135154
/**
136155
* @it prints kitchen sink
137156
*/

0 commit comments

Comments
 (0)