Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Handle expression spanning lines after the closing paren line
Browse files Browse the repository at this point in the history
  • Loading branch information
yujinakayama committed Oct 10, 2015
1 parent f65714c commit 232ba6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/rspec/core/formatters/snippet_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,25 @@ def line_range_of_location_nodes_in_expression
def expression_node
raise NoExpressionAtLineError if location_nodes_at_beginning_line.empty?

# Finding the lowest common ancestor node of nodes at the beginning line.
@expression_node ||= location_nodes_at_beginning_line.map do |node|
node.each_ancestor.to_a
end.reduce(:&).first
@expression_node ||= begin
common_ancestor_nodes = location_nodes_at_beginning_line.map do |node|
node.each_ancestor.to_a
end.reduce(:&)

# See `Ripper::PARSER_EVENTS` for the complete list of sexp types.
common_ancestor_nodes.find.each_with_index do |node, index|
next false if node.type.to_s.start_with?('@')
next_node = common_ancestor_nodes[index + 1]
next true unless next_node
next false if method_invocation_type?(node.type) && method_invocation_type?(next_node.type)
true
end
end
end

def method_invocation_type?(type)
type = type.to_s
type.end_with?('call') || type.start_with?('method_add_')
end

def location_nodes_at_beginning_line
Expand Down
22 changes: 22 additions & 0 deletions spec/rspec/core/formatters/snippet_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ def another_expression(*)
end
end

context 'when the expression spans lines after the closing paren line' do
let(:source) do
do_something_fail(
).
do_something_chain
end

# [:program,
# [[:call,
# [:method_add_arg, [:fcall, [:@ident, "do_something_fail", [1, 10]]], [:arg_paren, nil]],
# :".",
# [:@ident, "do_something_chain", [3, 10]]]]]

it 'returns all the lines' do
expect(expression_lines).to eq([
' do_something_fail(',
' ).',
' do_something_chain'
])
end
end

context "when the expression's final line includes the same type of opening paren of another multiline expression" do
let(:source) do
do_something_fail(
Expand Down

0 comments on commit 232ba6b

Please sign in to comment.