From 7273c4dd60aa8e61e29053042d085cabc3246e23 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 5 Mar 2024 20:55:40 -0500 Subject: [PATCH] [ruby/prism] Fix up lambda ripper translation https://github.com/ruby/prism/commit/a3156e60cc --- lib/prism/prism.gemspec | 1 - lib/prism/translation/ripper.rb | 62 ++++++++++++++++----------------- test/prism/ripper_test.rb | 55 ++++++----------------------- 3 files changed, 42 insertions(+), 76 deletions(-) diff --git a/lib/prism/prism.gemspec b/lib/prism/prism.gemspec index ad0a2d5fdda4c8..78fae594e6fbbe 100644 --- a/lib/prism/prism.gemspec +++ b/lib/prism/prism.gemspec @@ -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", diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index a08f39afbae2c7..1d16ca1ca89d26 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -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) @@ -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) @@ -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) @@ -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:]]/ diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 1e90681a55c1c5..00a3056b94b6d6 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -319,9 +295,6 @@ 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 @@ -329,39 +302,22 @@ class RipperTest < TestCase 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 @@ -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