Skip to content

Commit 61d697b

Browse files
authored
Merge pull request #5090 from rmosolgo/minus-sign-fix
Handle minus followed by identifier
2 parents dda906b + 45f6221 commit 61d697b

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lib/graphql/language/lexer.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ def advance
5757
@scanner.skip(IDENTIFIER_REGEXP)
5858
:IDENTIFIER
5959
when ByteFor::NUMBER
60-
@scanner.skip(NUMERIC_REGEXP)
60+
if len = @scanner.skip(NUMERIC_REGEXP)
6161

62-
if GraphQL.reject_numbers_followed_by_names
63-
new_pos = @scanner.pos
64-
peek_byte = @string.getbyte(new_pos)
65-
next_first_byte = FIRST_BYTES[peek_byte]
66-
if next_first_byte == ByteFor::NAME || next_first_byte == ByteFor::IDENTIFIER
67-
number_part = token_value
68-
name_part = @scanner.scan(IDENTIFIER_REGEXP)
69-
raise_parse_error("Name after number is not allowed (in `#{number_part}#{name_part}`)")
62+
if GraphQL.reject_numbers_followed_by_names
63+
new_pos = @scanner.pos
64+
peek_byte = @string.getbyte(new_pos)
65+
next_first_byte = FIRST_BYTES[peek_byte]
66+
if next_first_byte == ByteFor::NAME || next_first_byte == ByteFor::IDENTIFIER
67+
number_part = token_value
68+
name_part = @scanner.scan(IDENTIFIER_REGEXP)
69+
raise_parse_error("Name after number is not allowed (in `#{number_part}#{name_part}`)")
70+
end
7071
end
72+
# Check for a matched decimal:
73+
@scanner[1] ? :FLOAT : :INT
74+
else
75+
raise_parse_error("Expected a number, but it was malformed (#{@string[@pos].inspect})")
7176
end
72-
# Check for a matched decimal:
73-
@scanner[1] ? :FLOAT : :INT
7477
when ByteFor::ELLIPSIS
7578
if @string.getbyte(@pos + 1) != 46 || @string.getbyte(@pos + 2) != 46
7679
raise_parse_error("Expected `...`, actual: #{@string[@pos..@pos + 2].inspect}")

spec/graphql/language/parser_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@
109109
assert_instance_of GraphQL::Language::Nodes::Enum, doc.definitions.first.selections.first.arguments.first.value
110110
end
111111

112+
it "handles invalid minus signs" do
113+
err = assert_raises GraphQL::ParseError do
114+
GraphQL.parse("{ a(b: -c) }")
115+
end
116+
expected_message = if USING_C_PARSER
117+
"syntax error, unexpected invalid token (\"-\") at [1, 8]"
118+
else
119+
"Expected a number, but it was malformed (\"-\")"
120+
end
121+
assert_equal expected_message, err.message
122+
end
123+
112124
it "allows operation names to match operation types" do
113125
doc = GraphQL.parse("query subscription { foo }")
114126
assert_equal "subscription", doc.definitions.first.name
@@ -180,7 +192,7 @@
180192
expected_msg = if USING_C_PARSER
181193
"syntax error, unexpected invalid token (\"-\") at [1, 19]"
182194
else
183-
"Expected NAME, actual: INT (\"-\") at [1, 19]"
195+
"Expected a number, but it was malformed (\"-\")"
184196
end
185197

186198
assert_equal expected_msg, err.message

0 commit comments

Comments
 (0)