Skip to content

Commit

Permalink
Fix handling of interpolated if/unless blocks
Browse files Browse the repository at this point in the history
Resolves "beginless range" error.

Fixes #1308, #1324, #1326, #1327
  • Loading branch information
lsegal committed May 3, 2020
1 parent 651917f commit e3e8321
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/yard/parser/ruby/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions spec/parser/ruby/ruby_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e3e8321

Please sign in to comment.