From e3e8321075e22180dc057a98741c6661abb40d52 Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Sun, 3 May 2020 13:46:27 -0700 Subject: [PATCH] Fix handling of interpolated if/unless blocks Resolves "beginless range" error. Fixes #1308, #1324, #1326, #1327 --- lib/yard/parser/ruby/ruby_parser.rb | 5 +++-- spec/parser/ruby/ruby_parser_spec.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/yard/parser/ruby/ruby_parser.rb b/lib/yard/parser/ruby/ruby_parser.rb index 35b90eddc..c7e25ee8e 100644 --- a/lib/yard/parser/ruby/ruby_parser.rb +++ b/lib/yard/parser/ruby/ruby_parser.rb @@ -202,8 +202,7 @@ def on_#{event}(tok) begin; undef on_#{event}; rescue NameError; end def on_#{event}(tok) unless @last_ns_token == [:kw, "def"] || - (@tokens.last && @tokens.last[0] == :symbeg) || - (!@newline && %w(if while until unless).include?(tok)) + (@tokens.last && @tokens.last[0] == :symbeg) (@map[tok] ||= []) << [lineno, charno] end visit_ns_token(:#{event}, tok, true) @@ -442,6 +441,8 @@ def on_const_path_ref(*args) module_eval(<<-eof, __FILE__, __LINE__ + 1) begin; undef on_#{kw}; rescue NameError; end def on_#{kw}(*args) + mapping = @map[#{kw.to_s.sub(/_mod$/, '').inspect}] + mapping.pop if mapping sr = args.last.source_range.first..args.first.source_range.last lr = args.last.line_range.first..args.first.line_range.last #{node_class}.new(:#{kw}, args, :line => lr, :char => sr) diff --git a/spec/parser/ruby/ruby_parser_spec.rb b/spec/parser/ruby/ruby_parser_spec.rb index ae0cbe98f..3ac348d21 100644 --- a/spec/parser/ruby/ruby_parser_spec.rb +++ b/spec/parser/ruby/ruby_parser_spec.rb @@ -529,5 +529,15 @@ class B eof expect(ast.jump(:class).docstring).to eq "comment 1\ncomment 2" end + + %w(if unless).each do |type| + let(:condition_type) { type } + let(:ast) { stmt '"#{' + type + ' condition?; 42; end}" ' + type + ' verbose?' } + let(:subject) { ast.jump(:string_embexpr)[0][0].source } + + it "returns correct source for interpolated non-ternary '#{type}' conditionals" do + is_expected.to eq "#{condition_type} condition?; 42; end" + end + end end end if HAVE_RIPPER