Skip to content

Commit

Permalink
[ruby/prism] Fix up lambda 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 db53705 commit 7273c4d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 76 deletions.
1 change: 0 additions & 1 deletion lib/prism/prism.gemspec
Expand Up @@ -96,7 +96,6 @@ Gem::Specification.new do |spec|
"lib/prism/translation/parser/lexer.rb",
"lib/prism/translation/parser/rubocop.rb",
"lib/prism/translation/ripper.rb",
"lib/prism/translation/ripper/ripper_compiler.rb",
"lib/prism/translation/ruby_parser.rb",
"lib/prism/visitor.rb",
"src/diagnostic.c",
Expand Down
62 changes: 31 additions & 31 deletions lib/prism/translation/ripper.rb
Expand Up @@ -537,28 +537,10 @@ def visit_call_node(node)
bounds(node.location)
on_unary(node.name, receiver)
when :!@
if node.message == "not"
receiver =
case node.receiver
when nil
nil
when ParenthesesNode
body = visit(node.receiver.body&.body&.first) || false

bounds(node.receiver.location)
on_paren(body)
else
visit(node.receiver)
end

bounds(node.location)
on_unary(:not, receiver)
else
receiver = visit(node.receiver)
receiver = visit(node.receiver)

bounds(node.location)
on_unary(:!@, receiver)
end
bounds(node.location)
on_unary(node.message == "not" ? :not : :!@, receiver)
when :!=, :!~, :=~, :==, :===, :<=>, :>, :>=, :<, :<=, :&, :|, :^, :>>, :<<, :-, :+, :%, :/, :*, :**
receiver = visit(node.receiver)
value = visit(node.arguments.arguments.first)
Expand Down Expand Up @@ -1675,20 +1657,36 @@ def visit_lambda_node(node)
else
# Ripper does not track block-locals within lambdas, so we skip
# directly to the parameters here.
visit(node.parameters.parameters)
end
params = visit(node.parameters.parameters)

if !node.opening_loc.nil?
bounds(node.opening_loc)
parameters = on_paren(parameters)
end
if node.parameters.opening_loc.nil?
params
else
bounds(node.parameters.opening_loc)
on_paren(params)
end
end

braces = node.opening == "{"
body =
if node.body.nil?
case node.body
when nil
bounds(node.location)
on_stmts_add(on_stmts_new, on_void_stmt)
stmts = on_stmts_add(on_stmts_new, on_void_stmt)

bounds(node.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";")
stmts = visit_statements_node_body(stmts)

bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_body_node(node.body, node.location)
else
visit(node.body)
raise
end

bounds(node.location)
Expand Down Expand Up @@ -1953,7 +1951,7 @@ def visit_parameters_node(node)
posts = visit_all(node.posts) if node.posts.any?
keywords = visit_all(node.keywords) if node.keywords.any?
keyword_rest = visit(node.keyword_rest)
block = node.keyword_rest.is_a?(ForwardingParameterNode) ? :& : visit(node.block)
block = visit(node.block)

bounds(node.location)
on_params(requireds, optionals, rest, posts, keywords, keyword_rest, block)
Expand Down Expand Up @@ -2483,6 +2481,8 @@ def visit_token(token)
on_period(token)
when *RUBY_KEYWORDS
on_kw(token)
when /^_/
on_ident(token)
when /^[[:upper:]]/
on_const(token)
when /^[[:punct:]]/
Expand Down
55 changes: 11 additions & 44 deletions test/prism/ripper_test.rb
Expand Up @@ -30,7 +30,6 @@ class RipperTest < TestCase
heredocs_nested.txt
heredocs_with_ignored_newlines.txt
if.txt
lambda.txt
method_calls.txt
methods.txt
modules.txt
Expand All @@ -45,12 +44,10 @@ class RipperTest < TestCase
return.txt
seattlerb/TestRubyParserShared.txt
seattlerb/array_lits_trailing_calls.txt
seattlerb/attr_asgn_colon_id.txt
seattlerb/begin_rescue_else_ensure_bodies.txt
seattlerb/begin_rescue_else_ensure_no_bodies.txt
seattlerb/block_break.txt
seattlerb/block_call_dot_op2_brace_block.txt
seattlerb/block_call_paren_call_block_call.txt
seattlerb/block_command_operation_colon.txt
seattlerb/block_command_operation_dot.txt
seattlerb/block_decomp_anon_splat_arg.txt
Expand All @@ -60,13 +57,9 @@ class RipperTest < TestCase
seattlerb/block_next.txt
seattlerb/block_paren_splat.txt
seattlerb/block_return.txt
seattlerb/bug169.txt
seattlerb/bug179.txt
seattlerb/bug190.txt
seattlerb/bug191.txt
seattlerb/bug_215.txt
seattlerb/bug_249.txt
seattlerb/bug_call_arglist_parens.txt
seattlerb/bug_comma.txt
seattlerb/bug_hash_args_trailing_comma.txt
seattlerb/bug_hash_interp_array.txt
Expand All @@ -77,11 +70,8 @@ class RipperTest < TestCase
seattlerb/call_assoc_new_if_multiline.txt
seattlerb/call_assoc_trailing_comma.txt
seattlerb/call_block_arg_named.txt
seattlerb/call_colon2.txt
seattlerb/call_colon_parens.txt
seattlerb/call_dot_parens.txt
seattlerb/call_stabby_do_end_with_block.txt
seattlerb/call_stabby_with_braces_block.txt
seattlerb/call_trailing_comma.txt
seattlerb/case_in.txt
seattlerb/case_in_else.txt
Expand All @@ -105,8 +95,6 @@ class RipperTest < TestCase
seattlerb/difficult3__7.txt
seattlerb/difficult3__8.txt
seattlerb/difficult3__9.txt
seattlerb/difficult6__7.txt
seattlerb/difficult6__8.txt
seattlerb/difficult7_.txt
seattlerb/do_lambda.txt
seattlerb/heredoc__backslash_dos_format.txt
Expand Down Expand Up @@ -144,7 +132,6 @@ class RipperTest < TestCase
seattlerb/masgn_splat_arg_arg.txt
seattlerb/masgn_star.txt
seattlerb/masgn_var_star_var.txt
seattlerb/messy_op_asgn_lineno.txt
seattlerb/method_call_assoc_trailing_comma.txt
seattlerb/method_call_trailing_comma.txt
seattlerb/mlhs_back_anonsplat.txt
Expand All @@ -156,8 +143,6 @@ class RipperTest < TestCase
seattlerb/module_comments.txt
seattlerb/non_interpolated_symbol_array_line_breaks.txt
seattlerb/non_interpolated_word_array_line_breaks.txt
seattlerb/op_asgn_primary_colon_identifier1.txt
seattlerb/op_asgn_primary_colon_identifier_command_call.txt
seattlerb/parse_if_not_canonical.txt
seattlerb/parse_if_not_noncanonical.txt
seattlerb/parse_line_defn_complex.txt
Expand All @@ -172,7 +157,6 @@ class RipperTest < TestCase
seattlerb/parse_opt_call_args_lit_comma.txt
seattlerb/parse_pattern_051.txt
seattlerb/parse_pattern_058.txt
seattlerb/parse_pattern_058_2.txt
seattlerb/parse_pattern_076.txt
seattlerb/pctW_lineno.txt
seattlerb/pct_nl.txt
Expand All @@ -194,11 +178,9 @@ class RipperTest < TestCase
seattlerb/rescue_do_end_no_raise.txt
seattlerb/rescue_do_end_raised.txt
seattlerb/rescue_do_end_rescued.txt
seattlerb/rescue_parens.txt
seattlerb/return_call_assocs.txt
seattlerb/safe_call_dot_parens.txt
seattlerb/slashy_newlines_within_string.txt
seattlerb/stabby_arg_no_paren.txt
seattlerb/stabby_block_iter_call.txt
seattlerb/stabby_block_iter_call_no_target_with_arg.txt
seattlerb/str_double_double_escaped_newline.txt
Expand Down Expand Up @@ -276,12 +258,8 @@ class RipperTest < TestCase
whitequark/array_words_interp.txt
whitequark/asgn_mrhs.txt
whitequark/break_block.txt
whitequark/bug_435.txt
whitequark/bug_452.txt
whitequark/bug_480.txt
whitequark/bug_ascii_8bit_in_literal.txt
whitequark/bug_cmdarg.txt
whitequark/bug_do_block_in_cmdarg.txt
whitequark/bug_do_block_in_hash_brace.txt
whitequark/bug_heredoc_do.txt
whitequark/bug_interp_single.txt
Expand All @@ -308,8 +286,6 @@ class RipperTest < TestCase
whitequark/interp_digit_var.txt
whitequark/kwbegin_compstmt.txt
whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt
whitequark/lbrace_arg_after_command_args.txt
whitequark/lparenarg_after_lvar__since_25.txt
whitequark/lvar_injecting_match.txt
whitequark/masgn.txt
whitequark/masgn_attr.txt
Expand All @@ -319,49 +295,29 @@ class RipperTest < TestCase
whitequark/next_block.txt
whitequark/numbered_args_after_27.txt
whitequark/numparam_outside_block.txt
whitequark/op_asgn.txt
whitequark/op_asgn_cmd.txt
whitequark/parser_bug_507.txt
whitequark/parser_bug_640.txt
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
whitequark/parser_slash_slash_n_escaping_in_literals.txt
whitequark/pattern_matching_blank_else.txt
whitequark/pattern_matching_else.txt
whitequark/rescue_else.txt
whitequark/rescue_else_ensure.txt
whitequark/rescue_in_lambda_block.txt
whitequark/rescue_without_begin_end.txt
whitequark/return.txt
whitequark/return_block.txt
whitequark/ruby_bug_10653.txt
whitequark/ruby_bug_11107.txt
whitequark/ruby_bug_11380.txt
whitequark/ruby_bug_11873.txt
whitequark/ruby_bug_11873_a.txt
whitequark/ruby_bug_11989.txt
whitequark/ruby_bug_11990.txt
whitequark/ruby_bug_12073.txt
whitequark/ruby_bug_12402.txt
whitequark/ruby_bug_12686.txt
whitequark/ruby_bug_14690.txt
whitequark/ruby_bug_15789.txt
whitequark/send_attr_asgn.txt
whitequark/send_block_chain_cmd.txt
whitequark/send_call.txt
whitequark/send_index_cmd.txt
whitequark/send_lambda.txt
whitequark/send_lambda_args_noparen.txt
whitequark/send_lambda_legacy.txt
whitequark/send_plain.txt
whitequark/send_plain_cmd.txt
whitequark/send_self.txt
whitequark/slash_newline_in_heredocs.txt
whitequark/space_args_arg.txt
whitequark/space_args_arg_block.txt
whitequark/space_args_arg_call.txt
whitequark/space_args_arg_newline.txt
whitequark/space_args_block.txt
whitequark/space_args_cmd.txt
whitequark/string_concat.txt
whitequark/ternary.txt
whitequark/ternary_ambiguous_symbol.txt
Expand All @@ -377,6 +333,17 @@ class RipperTest < TestCase

define_method "test_ripper_#{relative}" do
source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)

case relative
when /break|next|redo|if|unless|rescue|control|keywords|retry/
source = "-> do\nrescue\n#{source}\nend"
end

case source
when /^ *yield/
source = "def __invalid_yield__\n#{source}\nend"
end

assert_ripper(source, filepath, skips.include?(relative))
end
end
Expand Down

0 comments on commit 7273c4d

Please sign in to comment.