Skip to content

Commit

Permalink
[ruby/prism] Stop looking at generated tree in ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Mar 6, 2024
1 parent 507ffc9 commit 2574e78
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
89 changes: 52 additions & 37 deletions lib/prism/translation/ripper.rb
Expand Up @@ -681,7 +681,7 @@ def visit_call_node(node)
if node.opening_loc.nil?
bounds(node.location)

if !arguments || arguments.empty?
if node.arguments.nil? && !node.block.is_a?(BlockArgumentNode)
on_call(receiver, call_operator, message)
else
on_command_call(receiver, call_operator, message, arguments)
Expand Down Expand Up @@ -1708,7 +1708,8 @@ def visit_interpolated_regular_expression_node(node)
# ^^^^^^^^^^^^
def visit_interpolated_string_node(node)
if node.opening&.start_with?("<<~")
heredoc = visit_string_heredoc_node(node.parts)
bounds(node.parts.first.location)
heredoc = visit_heredoc_node(node.parts, on_string_content) { |parts, part| on_string_add(parts, part) }

bounds(node.location)
on_string_literal(heredoc)
Expand Down Expand Up @@ -1749,7 +1750,8 @@ def visit_interpolated_symbol_node(node)
# ^^^^^^^^^^^^
def visit_interpolated_x_string_node(node)
if node.opening.start_with?("<<~")
heredoc = visit_x_string_heredoc_node(node.parts)
bounds(node.parts.first.location)
heredoc = visit_heredoc_node(node.parts, on_xstring_new) { |parts, part| on_xstring_add(parts, part) }

bounds(node.location)
on_xstring_literal(heredoc)
Expand Down Expand Up @@ -2468,7 +2470,8 @@ def visit_string_node(node)
bounds(node.location)
on_CHAR("?#{node.content}")
elsif opening.start_with?("<<~")
heredoc = visit_string_heredoc_node([node])
bounds(node.location)
heredoc = visit_heredoc_node([node], on_string_content) { |parts, part| on_string_add(parts, part) }

bounds(node.location)
on_string_literal(heredoc)
Expand Down Expand Up @@ -2530,44 +2533,55 @@ def visit_string_node(node)
end

# Visit a string that is expressed using a <<~ heredoc.
private def visit_string_heredoc_node(parts)
private def visit_heredoc_node(parts, base)
common_whitespace = heredoc_common_whitespace(parts)

bounds(parts.first.location)
parts.inject(on_string_content) do |string_content, part|
on_string_add(
string_content,
if part.is_a?(StringNode)
content = part.content
trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace)

bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace))
on_tstring_content(part.content[trimmed_whitespace..])
else
visit(part)
end
)
end
end
if common_whitespace == 0
bounds(parts.first.location)

# Visit an xstring that is expressed using a <<~ heredoc.
private def visit_x_string_heredoc_node(parts)
common_whitespace = heredoc_common_whitespace(parts)
previous_string = []
previous_result = base

bounds(parts.first.location)
parts.inject(on_xstring_new) do |xstring, part|
on_xstring_add(
xstring,
parts.each do |part|
if part.is_a?(StringNode)
content = part.content
trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace)

bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace))
on_tstring_content(part.content[trimmed_whitespace..])
if previous_string.empty?
previous_string = [part]
else
previous_string << part
end
else
visit(part)
unless previous_string.empty?
bounds(previous_string[0].location)
previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join))
previous_string = []
end

previous_result = yield(previous_result, visit(part))
end
)
end

unless previous_string.empty?
bounds(previous_string[0].location)
previous_result = yield(previous_result, on_tstring_content(previous_string.map(&:content).join))
end

previous_result
else
bounds(parts.first.location)
parts.inject(base) do |string_content, part|
yield(
string_content,
if part.is_a?(StringNode)
content = part.content
trimmed_whitespace = heredoc_trimmed_whitespace(content, common_whitespace)

bounds(part.content_loc.copy(start_offset: part.content_loc.start_offset + trimmed_whitespace))
on_tstring_content(part.content[trimmed_whitespace..])
else
visit(part)
end
)
end
end
end

Expand Down Expand Up @@ -2739,7 +2753,8 @@ def visit_x_string_node(node)
bounds(node.location)
on_xstring_literal(on_xstring_new)
elsif node.opening.start_with?("<<~")
heredoc = visit_x_string_heredoc_node([node])
bounds(node.location)
heredoc = visit_heredoc_node([node], on_xstring_new) { |parts, part| on_xstring_add(parts, part) }

bounds(node.location)
on_xstring_literal(heredoc)
Expand Down Expand Up @@ -2798,7 +2813,7 @@ def trailing_comma?(left, right)

# Returns true if there is a semicolon between the two locations.
def void_stmt?(left, right, allow_newline)
pattern = allow_newline ? /[;#\n]/ : /[;#]/
pattern = allow_newline ? /[;\n]/ : /;/
source.byteslice(left.end_offset...right.start_offset).match?(pattern)
end

Expand Down
4 changes: 1 addition & 3 deletions test/prism/ripper_test.rb
Expand Up @@ -31,8 +31,6 @@ class RipperTest < TestCase

heredocs = %w[
dos_endings.txt
heredocs_leading_whitespace.txt
heredocs_nested.txt
heredocs_with_ignored_newlines.txt
seattlerb/heredoc__backslash_dos_format.txt
seattlerb/heredoc_backslash_nl.txt
Expand All @@ -41,6 +39,7 @@ class RipperTest < TestCase
seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt
seattlerb/heredoc_squiggly_blank_lines.txt
seattlerb/heredoc_squiggly_interp.txt
seattlerb/heredoc_squiggly_no_indent.txt
seattlerb/heredoc_squiggly_tabs.txt
seattlerb/heredoc_squiggly_tabs_extra.txt
seattlerb/heredoc_squiggly_visually_blank_lines.txt
Expand All @@ -57,7 +56,6 @@ class RipperTest < TestCase
]

skips = incorrect | heredocs | %w[
seattlerb/TestRubyParserShared.txt
seattlerb/block_call_dot_op2_brace_block.txt
seattlerb/block_command_operation_colon.txt
seattlerb/block_command_operation_dot.txt
Expand Down

0 comments on commit 2574e78

Please sign in to comment.