diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2024-03-05 07:51:37 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:41 +0000 |
| commit | 5422b584571c7715b77cc3083d73e49536e06b1a (patch) | |
| tree | f6bf1e81b5b5ec47dbe9435e6fbed54423027a7d | |
| parent | d815d68cbb420243d0f2e3fc866350761154dd82 (diff) | |
[ruby/prism] Implement if/unless for ripper translation
https://github.com/ruby/prism/commit/57c1dd4859
| -rw-r--r-- | lib/prism/translation/ripper.rb | 57 | ||||
| -rw-r--r-- | test/prism/ripper_test.rb | 19 |
2 files changed, 52 insertions, 24 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index fe44fae836..692d3f3723 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -797,7 +797,16 @@ module Prism # if foo then bar else baz end # ^^^^^^^^^^^^ def visit_else_node(node) - raise NoMethodError, __method__ + statements = + if node.statements.nil? + [nil] + else + body = node.statements.body + body.unshift(nil) if source.byteslice(node.else_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";") + end + + bounds(node.location) + on_else(visit_statements_node_body(statements)) end # "foo #{bar}" @@ -1003,7 +1012,26 @@ module Prism # foo ? bar : baz # ^^^^^^^^^^^^^^^ def visit_if_node(node) - raise NoMethodError, __method__ + if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset) + predicate = visit(node.predicate) + statements = + if node.statements.nil? + bounds(node.location) + on_stmts_add(on_stmts_new, on_void_stmt) + else + visit(node.statements) + end + consequent = visit(node.consequent) + + bounds(node.location) + on_if(predicate, statements, consequent) + else + statements = visit(node.statements.body.first) + predicate = visit(node.predicate) + + bounds(node.location) + on_if_mod(predicate, statements) + end end # 1i @@ -1826,7 +1854,26 @@ module Prism # bar unless foo # ^^^^^^^^^^^^^^ def visit_unless_node(node) - raise NoMethodError, __method__ + if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset) + predicate = visit(node.predicate) + statements = + if node.statements.nil? + bounds(node.location) + on_stmts_add(on_stmts_new, on_void_stmt) + else + visit(node.statements) + end + consequent = visit(node.consequent) + + bounds(node.location) + on_unless(predicate, statements, consequent) + else + statements = visit(node.statements.body.first) + predicate = visit(node.predicate) + + bounds(node.location) + on_unless_mod(predicate, statements) + end end # until foo; bar end @@ -1835,7 +1882,7 @@ module Prism # bar until foo # ^^^^^^^^^^^^^ def visit_until_node(node) - if node.statements.nil? || (node.keyword_loc.start_offset < node.statements.location.start_offset) + if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset) predicate = visit(node.predicate) statements = if node.statements.nil? @@ -1880,7 +1927,7 @@ module Prism # bar while foo # ^^^^^^^^^^^^^ def visit_while_node(node) - if node.statements.nil? || (node.keyword_loc.start_offset < node.statements.location.start_offset) + if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset) predicate = visit(node.predicate) statements = if node.statements.nil? diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index a1744cd7cb..ecd709dbc3 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -290,7 +290,6 @@ module Prism embdoc_no_newline_at_end.txt emoji_method_calls.txt endless_methods.txt - endless_range_in_conditional.txt for.txt global_variables.txt hashes.txt @@ -400,7 +399,6 @@ module Prism seattlerb/case_in_multiple.txt seattlerb/case_in_or.txt seattlerb/class_comments.txt - seattlerb/cond_unary_minus.txt seattlerb/const_2_op_asgn_or2.txt seattlerb/const_3_op_asgn_or.txt seattlerb/const_op_asgn_and1.txt @@ -444,7 +442,6 @@ module Prism seattlerb/defs_oneliner_eq2.txt seattlerb/defs_oneliner_rescue.txt seattlerb/difficult1_line_numbers.txt - seattlerb/difficult1_line_numbers2.txt seattlerb/difficult2_.txt seattlerb/difficult3_.txt seattlerb/difficult3_4.txt @@ -463,8 +460,6 @@ module Prism seattlerb/do_lambda.txt seattlerb/f_kw.txt seattlerb/f_kw__required.txt - seattlerb/flip2_env_lvar.txt - seattlerb/float_with_if_modifier.txt seattlerb/heredoc__backslash_dos_format.txt seattlerb/heredoc_backslash_nl.txt seattlerb/heredoc_bad_hex_escape.txt @@ -483,10 +478,8 @@ module Prism seattlerb/heredoc_with_only_carriage_returns.txt seattlerb/heredoc_with_only_carriage_returns_windows.txt seattlerb/if_elsif.txt - seattlerb/if_symbol.txt seattlerb/index_0.txt seattlerb/index_0_opasgn.txt - seattlerb/integer_with_if_modifier.txt seattlerb/interpolated_symbol_array_line_breaks.txt seattlerb/interpolated_word_array_line_breaks.txt seattlerb/iter_args_2__19.txt @@ -634,7 +627,6 @@ module Prism unparser/corpus/literal/defs.txt unparser/corpus/literal/dstr.txt unparser/corpus/literal/empty.txt - unparser/corpus/literal/flipflop.txt unparser/corpus/literal/for.txt unparser/corpus/literal/if.txt unparser/corpus/literal/kwbegin.txt @@ -652,7 +644,6 @@ module Prism unparser/corpus/literal/super.txt unparser/corpus/literal/while.txt unparser/corpus/literal/yield.txt - unparser/corpus/semantic/and.txt unparser/corpus/semantic/block.txt unparser/corpus/semantic/def.txt unparser/corpus/semantic/dstr.txt @@ -716,10 +707,7 @@ module Prism whitequark/casgn_toplevel.txt whitequark/character.txt whitequark/class_definition_in_while_cond.txt - whitequark/cond_begin.txt whitequark/cond_begin_masgn.txt - whitequark/cond_eflipflop.txt - whitequark/cond_iflipflop.txt whitequark/cond_match_current_line.txt whitequark/const_op_asgn.txt whitequark/dedenting_heredoc.txt @@ -745,13 +733,9 @@ module Prism whitequark/forwarded_kwrestarg_with_additional_kwarg.txt whitequark/forwarded_restarg.txt whitequark/hash_label_end.txt - whitequark/if.txt whitequark/if_else.txt whitequark/if_elsif.txt whitequark/if_masgn__24.txt - whitequark/if_mod.txt - whitequark/if_nl_then.txt - whitequark/if_while_after_class__since_32.txt whitequark/interp_digit_var.txt whitequark/kwarg.txt whitequark/kwbegin_compstmt.txt @@ -810,7 +794,6 @@ module Prism whitequark/restarg_unnamed.txt whitequark/return.txt whitequark/return_block.txt - whitequark/ruby_bug_10279.txt whitequark/ruby_bug_10653.txt whitequark/ruby_bug_11107.txt whitequark/ruby_bug_11380.txt @@ -857,9 +840,7 @@ module Prism whitequark/ternary.txt whitequark/ternary_ambiguous_symbol.txt whitequark/trailing_forward_arg.txt - whitequark/unless.txt whitequark/unless_else.txt - whitequark/unless_mod.txt whitequark/var_op_asgn.txt whitequark/when_splat.txt whitequark/yield.txt |
