Skip to content

Commit 25e77f5

Browse files
committed
Sort tokens to workaround Ripper out-of-order tokenization
Fixes #1313
1 parent 8e82a8a commit 25e77f5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/yard/parser/ruby/ruby_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ def add_comment(line, node = nil, before_node = nil, into = false)
684684
end
685685

686686
def freeze_tree(node = nil)
687+
@tokens = @tokens.sort_by {|t| t.last }
687688
nodes = [node || root]
688689
until nodes.empty?
689690
p_node = nodes.shift

spec/parser/ruby/ruby_parser_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,19 @@ class X
317317
expect(symbols).to eq %w(:'' :bar :BAR :"B+z" :if)
318318
end
319319

320+
# @bug gh-1313
321+
it "tokenizes comments in-order" do
322+
src = <<-eof
323+
def method
324+
# Method comment not docstring
325+
end
326+
eof
327+
328+
tokens = tokenize(src.gsub(/^ +/, ''))
329+
expect(tokens).to eq(tokens.sort_by {|t| t.last })
330+
expect(tokens.map {|t| t.first }).to eq %i(kw sp ident nl comment kw nl)
331+
end
332+
320333
it "parses %w() array in constant declaration" do
321334
s = stmt(<<-eof)
322335
class Foo

0 commit comments

Comments
 (0)