Skip to content

Commit 2ebbac6

Browse files
committed
Address review feedback: nil guards, undef on_rbrace, comment accuracy, bare-pattern tests
- Add `undef on_rbrace` to the undef block, consistent with every other explicit event override in the file - Use `(@Map[:lbracket] ||= []).pop` and `(@Map[:aref] ||= []).shift` in on_aryptn/on_fndptn, matching on_hshptn's defensive nil-safe style - Correct the comment on on_aryptn/on_fndptn: YARD does traverse these nodes via root.traverse; what it lacks is a registered handler, so they produce no documentation output - Add explanatory comment on on_rbrace clarifying why it maintains @Map[:rbrace] when on_rbracket does not - Add tests for bare array pattern (`in [a, b]`) and bare hash pattern (`in name: String`) to cover the unbraced branches of on_aryptn and on_hshptn
1 parent 0ebc6a0 commit 2ebbac6

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

lib/yard/parser/ruby/ruby_parser.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def add_token(token, data)
337337
undef on_aref_field
338338
undef on_lbracket
339339
undef on_rbracket
340+
undef on_rbrace
340341
undef on_string_literal
341342
undef on_lambda
342343
undef on_unary
@@ -433,6 +434,8 @@ def on_rbracket(tok)
433434
visit_ns_token(:rbracket, tok, false)
434435
end
435436

437+
# Maintained explicitly (unlike on_rbracket) so on_hshptn can distinguish
438+
# braced from bare hash patterns in Ruby 3.0+ pattern matching.
436439
def on_rbrace(tok)
437440
(@map[:rbrace] ||= []) << [lineno, charno]
438441
visit_ns_token(:rbrace, tok, false)
@@ -444,16 +447,18 @@ def on_rbrace(tok)
444447
# (not on_aref), so we must clean up the bracket maps to prevent stale entries from
445448
# corrupting source ranges of later array indexing expressions.
446449
def on_aryptn(*args)
447-
@map[:lbracket].pop
448-
@map[:aref].shift
449-
# Source range is intentionally not set; YARD does not traverse pattern-match arms.
450+
(@map[:lbracket] ||= []).pop
451+
(@map[:aref] ||= []).shift
452+
# Source range is intentionally not set; no handler is registered for
453+
# pattern-match nodes, so they produce no documentation output.
450454
AstNode.new(:aryptn, args)
451455
end
452456

453457
def on_fndptn(*args)
454-
@map[:lbracket].pop
455-
@map[:aref].shift
456-
# Source range is intentionally not set; YARD does not traverse pattern-match arms.
458+
(@map[:lbracket] ||= []).pop
459+
(@map[:aref] ||= []).shift
460+
# Source range is intentionally not set; no handler is registered for
461+
# pattern-match nodes, so they produce no documentation output.
457462
AstNode.new(:fndptn, args)
458463
end
459464

spec/parser/ruby/ruby_parser_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,68 @@ def check(obj)
751751
end
752752
end
753753
754+
def build
755+
{ name: "Alice" }
756+
end
757+
end
758+
RUBY
759+
760+
parser = YARD::Parser::Ruby::RubyParser.new(code, nil)
761+
ast = parser.parse.root
762+
763+
hash_node = nil
764+
ast.traverse do |node|
765+
if node.type == :hash
766+
hash_node = node
767+
break
768+
end
769+
end
770+
771+
expect(hash_node).not_to be_nil
772+
expect(code[hash_node.source_range]).to eq('{ name: "Alice" }')
773+
end if RUBY_VERSION >= '3.'
774+
775+
it "provides correct source range for aref after bare array pattern deconstruction" do
776+
code = <<-RUBY
777+
class Foo
778+
def check(obj)
779+
case obj
780+
in [a, b]
781+
a
782+
end
783+
end
784+
785+
def fetch(h)
786+
h["result"]
787+
end
788+
end
789+
RUBY
790+
791+
parser = YARD::Parser::Ruby::RubyParser.new(code, nil)
792+
ast = parser.parse.root
793+
794+
aref_node = nil
795+
ast.traverse do |node|
796+
if node.type == :aref
797+
aref_node = node
798+
break
799+
end
800+
end
801+
802+
expect(aref_node).not_to be_nil
803+
expect(code[aref_node.source_range]).to eq('h["result"]')
804+
end if RUBY_VERSION >= '3.'
805+
806+
it "provides correct source range for hash literal after bare hash pattern deconstruction" do
807+
code = <<-RUBY
808+
class Foo
809+
def check(obj)
810+
case obj
811+
in name: String
812+
"matched"
813+
end
814+
end
815+
754816
def build
755817
{ name: "Alice" }
756818
end

0 commit comments

Comments
 (0)