Skip to content

Commit bc3ce23

Browse files
authored
Merge pull request rmosolgo#5267 from rmosolgo/dangling-brace-fix
Raise a ParseError on dangling close curly brace
2 parents 94440dc + 47e0584 commit bc3ce23

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/graphql/language/lexer.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ def initialize(graphql_str, filename: nil, max_tokens: nil)
1313
@pos = nil
1414
@max_tokens = max_tokens || Float::INFINITY
1515
@tokens_count = 0
16+
@finished = false
1617
end
1718

18-
def eos?
19-
@scanner.eos?
19+
def finished?
20+
@finished
2021
end
2122

2223
attr_reader :pos, :tokens_count
2324

2425
def advance
2526
@scanner.skip(IGNORE_REGEXP)
26-
return false if @scanner.eos?
27+
if @scanner.eos?
28+
@finished = true
29+
return false
30+
end
2731
@tokens_count += 1
2832
if @tokens_count > @max_tokens
2933
raise_parse_error("This query is too large to execute.")

lib/graphql/language/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def document
110110
# Only ignored characters is not a valid document
111111
raise GraphQL::ParseError.new("Unexpected end of document", nil, nil, @graphql_str)
112112
end
113-
while !@lexer.eos?
113+
while !@lexer.finished?
114114
defns << definition
115115
end
116116
Document.new(pos: 0, definitions: defns, filename: @filename, source: self)

spec/graphql/language/parser_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@
8383
assert GraphQL.parse("{ a(b: \"\\u0000\") }")
8484
end
8585

86+
it "raises a parse error when there's a dangling close curly brace" do
87+
assert_raises(GraphQL::ParseError) {
88+
GraphQL.parse('{ foo } }')
89+
}
90+
end
91+
92+
it "raises a parse error when there's a dangling identifier" do
93+
assert_raises(GraphQL::ParseError) {
94+
GraphQL.parse('{ foo } fooagain')
95+
}
96+
end
97+
8698
describe "when there are no selections" do
8799
it 'raises a ParseError' do
88100
assert_raises(GraphQL::ParseError) {

0 commit comments

Comments
 (0)