Skip to content

Commit

Permalink
Handle minus followed by identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Sep 6, 2024
1 parent dda906b commit 45f6221
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
25 changes: 14 additions & 11 deletions lib/graphql/language/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ def advance
@scanner.skip(IDENTIFIER_REGEXP)
:IDENTIFIER
when ByteFor::NUMBER
@scanner.skip(NUMERIC_REGEXP)
if len = @scanner.skip(NUMERIC_REGEXP)

if GraphQL.reject_numbers_followed_by_names
new_pos = @scanner.pos
peek_byte = @string.getbyte(new_pos)
next_first_byte = FIRST_BYTES[peek_byte]
if next_first_byte == ByteFor::NAME || next_first_byte == ByteFor::IDENTIFIER
number_part = token_value
name_part = @scanner.scan(IDENTIFIER_REGEXP)
raise_parse_error("Name after number is not allowed (in `#{number_part}#{name_part}`)")
if GraphQL.reject_numbers_followed_by_names
new_pos = @scanner.pos
peek_byte = @string.getbyte(new_pos)
next_first_byte = FIRST_BYTES[peek_byte]
if next_first_byte == ByteFor::NAME || next_first_byte == ByteFor::IDENTIFIER
number_part = token_value
name_part = @scanner.scan(IDENTIFIER_REGEXP)
raise_parse_error("Name after number is not allowed (in `#{number_part}#{name_part}`)")
end
end
# Check for a matched decimal:
@scanner[1] ? :FLOAT : :INT
else
raise_parse_error("Expected a number, but it was malformed (#{@string[@pos].inspect})")
end
# Check for a matched decimal:
@scanner[1] ? :FLOAT : :INT
when ByteFor::ELLIPSIS
if @string.getbyte(@pos + 1) != 46 || @string.getbyte(@pos + 2) != 46
raise_parse_error("Expected `...`, actual: #{@string[@pos..@pos + 2].inspect}")
Expand Down
14 changes: 13 additions & 1 deletion spec/graphql/language/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
assert_instance_of GraphQL::Language::Nodes::Enum, doc.definitions.first.selections.first.arguments.first.value
end

it "handles invalid minus signs" do
err = assert_raises GraphQL::ParseError do
GraphQL.parse("{ a(b: -c) }")
end
expected_message = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 8]"
else
"Expected a number, but it was malformed (\"-\")"
end
assert_equal expected_message, err.message
end

it "allows operation names to match operation types" do
doc = GraphQL.parse("query subscription { foo }")
assert_equal "subscription", doc.definitions.first.name
Expand Down Expand Up @@ -180,7 +192,7 @@
expected_msg = if USING_C_PARSER
"syntax error, unexpected invalid token (\"-\") at [1, 19]"
else
"Expected NAME, actual: INT (\"-\") at [1, 19]"
"Expected a number, but it was malformed (\"-\")"
end

assert_equal expected_msg, err.message
Expand Down

0 comments on commit 45f6221

Please sign in to comment.