Skip to content

Commit 9edfcc2

Browse files
committed
print_location: Use vertical bar as number column delimiter
Replicates graphql/graphql-js@e171bbc
1 parent fca6eba commit 9edfcc2

File tree

6 files changed

+46
-42
lines changed

6 files changed

+46
-42
lines changed

graphql/language/print_location.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
2-
from functools import reduce
3-
from typing import List, Optional, Tuple
2+
from typing import List, Optional, Tuple, cast
43

54
from .ast import Location
65
from .location import SourceLocation, get_location
@@ -40,20 +39,20 @@ def get_line(index: int) -> Optional[str]:
4039

4140
return f"{source.name}:{line_num}:{column_num}\n" + print_prefixed_lines(
4241
[
43-
(f"{line_num - 1}: ", get_line(line_index - 1)),
44-
(f"{line_num}: ", get_line(line_index)),
42+
(f"{line_num - 1}", get_line(line_index - 1)),
43+
(f"{line_num}", get_line(line_index)),
4544
("", " " * (column_num - 1) + "^"),
46-
(f"{line_num + 1}: ", get_line(line_index + 1)),
45+
(f"{line_num + 1}", get_line(line_index + 1)),
4746
]
4847
)
4948

5049

5150
def print_prefixed_lines(lines: List[Tuple[str, Optional[str]]]) -> str:
52-
"""Print lines specified like this: ["prefix", "string"]"""
53-
existing_lines = [line for line in lines if line[1] is not None]
54-
pad_len = reduce(lambda pad, line: max(pad, len(line[0])), existing_lines, 0)
51+
"""Print lines specified like this: ("prefix", "string")"""
52+
existing_lines = [
53+
cast(Tuple[str, str], line) for line in lines if line[1] is not None
54+
]
55+
pad_len = max(len(line[0]) for line in existing_lines)
5556
return "\n".join(
56-
map(
57-
lambda line: line[0].rjust(pad_len) + line[1], existing_lines # type:ignore
58-
)
57+
map(lambda line: line[0].rjust(pad_len) + " | " + line[1], existing_lines)
5958
)

tests/error/test_graphql_error.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@ def prints_an_error_with_nodes_from_different_sources():
197197
Example error with two nodes
198198
199199
SourceA:2:10
200-
1: type Foo {
201-
2: field: String
202-
^
203-
3: }
200+
1 | type Foo {
201+
2 | field: String
202+
| ^
203+
3 | }
204204
205205
SourceB:2:10
206-
1: type Foo {
207-
2: field: Int
208-
^
209-
3: }
206+
1 | type Foo {
207+
2 | field: Int
208+
| ^
209+
3 | }
210210
"""
211211
)
212212
assert str(error) == printed_error

tests/error/test_print_location.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def prints_single_digit_line_number_with_no_padding():
1111
assert result + "\n" == dedent(
1212
"""
1313
Test:9:1
14-
9: *
15-
^
14+
9 | *
15+
| ^
1616
"""
1717
)
1818

@@ -24,8 +24,8 @@ def prints_line_numbers_with_correct_padding():
2424
assert result + "\n" == dedent(
2525
"""
2626
Test:9:1
27-
9: *
28-
^
29-
10:\x20
27+
9 | *
28+
| ^
29+
10 |\x20
3030
"""
3131
)

tests/language/test_lexer.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def errors_respect_whitespace():
6666
Syntax Error: Cannot parse the unexpected character '?'.
6767
6868
GraphQL request:3:5
69-
2:\x20
70-
3: ?
71-
^
72-
4:\x20
69+
2 |\x20
70+
3 | ?
71+
| ^
72+
4 |\x20
7373
"""
7474
)
7575

@@ -83,10 +83,10 @@ def updates_line_numbers_in_error_for_file_context():
8383
Syntax Error: Cannot parse the unexpected character '?'.
8484
8585
foo.js:13:6
86-
12:\x20
87-
13: ?
88-
^
89-
14:\x20
86+
12 |\x20
87+
13 | ?
88+
| ^
89+
14 |\x20
9090
"""
9191
)
9292

@@ -99,8 +99,8 @@ def updates_column_numbers_in_error_for_file_context():
9999
Syntax Error: Cannot parse the unexpected character '?'.
100100
101101
foo.js:1:5
102-
1: ?
103-
^
102+
1 | ?
103+
| ^
104104
"""
105105
)
106106

tests/language/test_parser.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def parse_provides_useful_errors():
7272
Syntax Error: Expected Name, found <EOF>
7373
7474
GraphQL request:1:2
75-
1: {
76-
^
75+
1 | {
76+
| ^
7777
"""
7878
)
7979
assert_syntax_error(
@@ -91,9 +91,14 @@ def parse_provides_useful_error_when_using_source():
9191
with raises(GraphQLSyntaxError) as exc_info:
9292
parse(Source("query", "MyQuery.graphql"))
9393
error = exc_info.value
94-
assert str(error) + "\n" == (
95-
"Syntax Error: Expected {, found <EOF>\n\n"
96-
"MyQuery.graphql:1:6\n1: query\n ^\n"
94+
assert str(error) + "\n" == dedent(
95+
"""
96+
Syntax Error: Expected {, found <EOF>
97+
98+
MyQuery.graphql:1:6
99+
1 | query
100+
| ^
101+
"""
97102
)
98103

99104
def parses_variable_inline_values():

tests/utilities/test_strip_ignored_characters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def report_document_with_invalid_token():
131131
Syntax Error: Unterminated string.
132132
133133
GraphQL request:1:13
134-
1: { foo(arg: "
135-
^
136-
2: "
134+
1 | { foo(arg: "
135+
| ^
136+
2 | "
137137
"""
138138
)
139139

0 commit comments

Comments
 (0)