Skip to content

Commit d9a8764

Browse files
committed
Extract get_block_string_indentation function
Replicates graphql/graphql-js@280edb6
1 parent 230ab92 commit d9a8764

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.2.1. All parts of the API are covered by an extensive test suite of currently 1748
16+
14.2.1. All parts of the API are covered by an extensive test suite of currently 1753
1717
unit tests.
1818

1919

graphql/language/block_string.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
__all__ = ["dedent_block_string_value", "print_block_string"]
1+
from typing import List
2+
3+
__all__ = [
4+
"dedent_block_string_value",
5+
"print_block_string",
6+
"get_block_string_indentation",
7+
]
28

39

410
def dedent_block_string_value(raw_string: str) -> str:
@@ -9,28 +15,42 @@ def dedent_block_string_value(raw_string: str) -> str:
915
1016
This implements the GraphQL spec's BlockStringValue() static algorithm.
1117
"""
18+
# Expand a block string's raw value into independent lines.
1219
lines = raw_string.splitlines()
1320

14-
common_indent = None
15-
for line in lines[1:]:
16-
indent = leading_whitespace(line)
17-
if indent < len(line) and (common_indent is None or indent < common_indent):
18-
common_indent = indent
19-
if common_indent == 0:
20-
break
21+
# Remove common indentation from all lines but first.
22+
common_indent = get_block_string_indentation(lines)
2123

2224
if common_indent:
2325
lines[1:] = [line[common_indent:] for line in lines[1:]]
2426

27+
# Remove leading and trailing blank lines.
2528
while lines and not lines[0].strip():
2629
lines = lines[1:]
2730

2831
while lines and not lines[-1].strip():
2932
lines = lines[:-1]
3033

34+
# Return a string of the lines joined with U+000A.
3135
return "\n".join(lines)
3236

3337

38+
def get_block_string_indentation(lines: List[str]) -> int:
39+
common_indent = None
40+
41+
for line in lines[1:]:
42+
indent = leading_whitespace(line)
43+
if indent == len(line):
44+
continue # skip empty lines
45+
46+
if common_indent is None or indent < common_indent:
47+
common_indent = indent
48+
if common_indent == 0:
49+
break
50+
51+
return 0 if common_indent is None else common_indent
52+
53+
3454
def leading_whitespace(s):
3555
i = 0
3656
n = len(s)

tests/language/test_block_string.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from graphql.language.block_string import dedent_block_string_value, print_block_string
1+
from graphql.language.block_string import (
2+
dedent_block_string_value,
3+
print_block_string,
4+
get_block_string_indentation,
5+
)
26

37

48
def join_lines(*args):
@@ -69,6 +73,32 @@ def does_not_alter_trailing_spaces():
6973
)
7074

7175

76+
def describe_get_block_string_indentation():
77+
def returns_zero_for_an_empty_list():
78+
assert get_block_string_indentation([]) == 0
79+
80+
def do_not_take_first_line_into_account():
81+
assert get_block_string_indentation([" a"]) == 0
82+
assert get_block_string_indentation([" a", " b"]) == 2
83+
84+
def returns_minimal_indentation_length():
85+
assert get_block_string_indentation(["", " a", " b"]) == 1
86+
assert get_block_string_indentation(["", " a", " b"]) == 1
87+
assert get_block_string_indentation(["", " a", " b", "c"]) == 0
88+
89+
def count_both_tab_and_space_as_single_character():
90+
assert get_block_string_indentation(["", "\ta", " b"]) == 1
91+
assert get_block_string_indentation(["", "\t a", " b"]) == 2
92+
assert get_block_string_indentation(["", " \t a", " b"]) == 3
93+
94+
def do_not_take_empty_lines_into_account():
95+
assert get_block_string_indentation((["a", "\t"])) == 0
96+
assert get_block_string_indentation((["a", " "])) == 0
97+
assert get_block_string_indentation((["a", " ", " b"])) == 2
98+
assert get_block_string_indentation((["a", " ", " b"])) == 2
99+
assert get_block_string_indentation((["a", "", " b"])) == 1
100+
101+
72102
def describe_print_block_string():
73103
def by_default_print_block_strings_as_single_line():
74104
s = "one liner"

0 commit comments

Comments
 (0)