Skip to content

Commit 81798ba

Browse files
committed
print_ast: remove indentation inside of block strings
Replicates graphql/graphql-js@45e33ce
1 parent 7e06bb6 commit 81798ba

File tree

6 files changed

+16
-21
lines changed

6 files changed

+16
-21
lines changed

src/graphql/language/block_string.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ def get_block_string_indentation(value: str) -> int:
6969
return common_indent or 0
7070

7171

72-
def print_block_string(
73-
value: str, indentation: str = "", prefer_multiple_lines: bool = False
74-
) -> str:
72+
def print_block_string(value: str, prefer_multiple_lines: bool = False) -> str:
7573
"""Print a block string in the indented block form.
7674
7775
Prints a block string in the indented block form by adding a leading and
@@ -92,11 +90,11 @@ def print_block_string(
9290
)
9391

9492
# Format a multi-line block quote to account for leading space.
95-
if print_as_multiple_lines and not (is_single_line and has_leading_space):
96-
result = "\n" + indentation
97-
else:
98-
result = ""
99-
result += value.replace("\n", "\n" + indentation) if indentation else value
93+
result = (
94+
"\n"
95+
if print_as_multiple_lines and not (is_single_line and has_leading_space)
96+
else ""
97+
) + value
10098
if print_as_multiple_lines:
10199
result += "\n"
102100

src/graphql/language/printer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def leave_float_value(node: PrintedNode, *_args: Any) -> str:
154154
return node.value
155155

156156
@staticmethod
157-
def leave_string_value(node: PrintedNode, key: str, *_args: Any) -> str:
157+
def leave_string_value(node: PrintedNode, *_args: Any) -> str:
158158
if node.block:
159-
return print_block_string(node.value, "" if key == "description" else " ")
159+
return print_block_string(node.value)
160160
return dumps(node.value)
161161

162162
@staticmethod

src/graphql/utilities/print_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def print_description(
295295
return ""
296296

297297
prefer_multiple_lines = len(description) > 70
298-
block_string = print_block_string(description, "", prefer_multiple_lines)
298+
block_string = print_block_string(description, prefer_multiple_lines)
299299

300300
prefix = "\n" + indentation if indentation and not first_in_block else indentation
301301

tests/language/test_block_string.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,27 @@ def do_not_escape_characters():
116116
def by_default_print_block_strings_as_single_line():
117117
s = "one liner"
118118
assert print_block_string(s) == '"""one liner"""'
119-
assert print_block_string(s, "", True) == '"""\none liner\n"""'
119+
assert print_block_string(s, True) == '"""\none liner\n"""'
120120

121121
def correctly_prints_single_line_with_leading_space():
122122
s = " space-led string"
123123
assert print_block_string(s) == '""" space-led string"""'
124-
assert print_block_string(s, "", True) == '""" space-led string\n"""'
124+
assert print_block_string(s, True) == '""" space-led string\n"""'
125125

126126
def correctly_prints_single_line_with_leading_space_and_quotation():
127127
s = ' space-led value "quoted string"'
128128

129129
assert print_block_string(s) == '""" space-led value "quoted string"\n"""'
130130

131131
assert (
132-
print_block_string(s, "", True)
133-
== '""" space-led value "quoted string"\n"""'
132+
print_block_string(s, True) == '""" space-led value "quoted string"\n"""'
134133
)
135134

136135
def correctly_prints_single_line_with_trailing_backslash():
137136
s = "backslash \\"
138137

139138
assert print_block_string(s) == '"""\nbackslash \\\n"""'
140-
assert print_block_string(s, "", True) == '"""\nbackslash \\\n"""'
139+
assert print_block_string(s, True) == '"""\nbackslash \\\n"""'
141140

142141
def correctly_prints_string_with_a_first_line_indentation():
143142
s = join_lines(" first ", " line ", "indentation", " string")

tests/language/test_block_string_fuzz.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ def correctly_print_random_strings():
4141
"""
4242
)
4343

44-
printed_multiline_string = lex_value(
45-
print_block_string(test_value, " ", True)
46-
)
44+
printed_multiline_string = lex_value(print_block_string(test_value, True))
4745

4846
assert test_value == printed_multiline_string, dedent(
4947
f"""
50-
Expected lex_value(print_block_string({test_value!r}, ' ', True)
48+
Expected lex_value(print_block_string({test_value!r}, True)
5149
to equal {test_value!r}
5250
but got {printed_multiline_string!r}
5351
"""

tests/language/test_printer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def prints_kitchen_sink(kitchen_sink_query): # noqa: F811
189189
size: $size
190190
bar: $b
191191
obj: {key: "value", block: """
192-
block string uses \"""
192+
block string uses \"""
193193
"""}
194194
)
195195
}

0 commit comments

Comments
 (0)