Skip to content

Commit a3c8c27

Browse files
committed
Implement def for ripper translation
1 parent 57c1dd4 commit a3c8c27

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

lib/prism/translation/ripper.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,39 @@ def visit_constant_path_target_node(node)
781781
# def self.foo; end
782782
# ^^^^^^^^^^^^^^^^^
783783
def visit_def_node(node)
784-
raise NoMethodError, __method__
784+
bounds(node.name_loc)
785+
name = visit_token(node.name_loc.slice)
786+
787+
parameters =
788+
if node.parameters.nil?
789+
bounds(node.location)
790+
on_params(nil, nil, nil, nil, nil, nil, nil)
791+
else
792+
visit(node.parameters)
793+
end
794+
795+
if !node.lparen_loc.nil?
796+
bounds(node.lparen_loc)
797+
parameters = on_paren(parameters)
798+
end
799+
800+
bodystmt =
801+
case node.body
802+
when nil
803+
bounds(node.location)
804+
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
805+
when StatementsNode
806+
body = visit(node.body)
807+
808+
bounds(node.body.location)
809+
on_bodystmt(body, nil, nil, nil)
810+
when BeginNode
811+
visit_begin_node_clauses(node.body)
812+
else
813+
raise
814+
end
815+
816+
on_def(name, parameters, bodystmt)
785817
end
786818

787819
# defined? a

test/prism/ripper_test.rb

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ class RipperFixturesTest < RipperTestCase
299299
heredocs_nested.txt
300300
heredocs_with_ignored_newlines.txt
301301
if.txt
302-
indented_file_end.txt
303302
keyword_method_names.txt
304303
lambda.txt
305304
method_calls.txt
@@ -317,15 +316,12 @@ class RipperFixturesTest < RipperTestCase
317316
return.txt
318317
seattlerb/TestRubyParserShared.txt
319318
seattlerb/and_multi.txt
320-
seattlerb/args_kw_block.txt
321319
seattlerb/array_lits_trailing_calls.txt
322320
seattlerb/attr_asgn_colon_id.txt
323321
seattlerb/attrasgn_primary_dot_constant.txt
324322
seattlerb/begin_rescue_else_ensure_bodies.txt
325323
seattlerb/begin_rescue_else_ensure_no_bodies.txt
326-
seattlerb/block_arg__bare.txt
327324
seattlerb/block_break.txt
328-
seattlerb/block_call_defn_call_block_call.txt
329325
seattlerb/block_call_dot_op2_brace_block.txt
330326
seattlerb/block_call_dot_op2_cmd_args_do_block.txt
331327
seattlerb/block_call_operation_colon.txt
@@ -345,7 +341,6 @@ class RipperFixturesTest < RipperTestCase
345341
seattlerb/bug190.txt
346342
seattlerb/bug191.txt
347343
seattlerb/bug236.txt
348-
seattlerb/bug_187.txt
349344
seattlerb/bug_215.txt
350345
seattlerb/bug_249.txt
351346
seattlerb/bug_args__19.txt
@@ -404,33 +399,18 @@ class RipperFixturesTest < RipperTestCase
404399
seattlerb/const_op_asgn_and1.txt
405400
seattlerb/const_op_asgn_and2.txt
406401
seattlerb/const_op_asgn_or.txt
407-
seattlerb/defn_arg_asplat_arg.txt
408402
seattlerb/defn_arg_forward_args.txt
409403
seattlerb/defn_args_forward_args.txt
410-
seattlerb/defn_comments.txt
411404
seattlerb/defn_endless_command.txt
412405
seattlerb/defn_endless_command_rescue.txt
413406
seattlerb/defn_forward_args.txt
414407
seattlerb/defn_forward_args__no_parens.txt
415-
seattlerb/defn_kwarg_env.txt
416-
seattlerb/defn_kwarg_kwarg.txt
417-
seattlerb/defn_kwarg_kwsplat.txt
418-
seattlerb/defn_kwarg_kwsplat_anon.txt
419408
seattlerb/defn_kwarg_lvar.txt
420-
seattlerb/defn_kwarg_no_parens.txt
421-
seattlerb/defn_kwarg_val.txt
422-
seattlerb/defn_no_kwargs.txt
423409
seattlerb/defn_oneliner.txt
424410
seattlerb/defn_oneliner_eq2.txt
425411
seattlerb/defn_oneliner_noargs.txt
426412
seattlerb/defn_oneliner_noargs_parentheses.txt
427413
seattlerb/defn_oneliner_rescue.txt
428-
seattlerb/defn_opt_last_arg.txt
429-
seattlerb/defn_opt_reg.txt
430-
seattlerb/defn_opt_splat_arg.txt
431-
seattlerb/defn_powarg.txt
432-
seattlerb/defn_reg_opt_reg.txt
433-
seattlerb/defn_splat_arg.txt
434414
seattlerb/defn_unary_not.txt
435415
seattlerb/defns_reserved.txt
436416
seattlerb/defs_as_arg_with_do_block_inside.txt
@@ -458,8 +438,6 @@ class RipperFixturesTest < RipperTestCase
458438
seattlerb/difficult6__8.txt
459439
seattlerb/difficult7_.txt
460440
seattlerb/do_lambda.txt
461-
seattlerb/f_kw.txt
462-
seattlerb/f_kw__required.txt
463441
seattlerb/heredoc__backslash_dos_format.txt
464442
seattlerb/heredoc_backslash_nl.txt
465443
seattlerb/heredoc_bad_hex_escape.txt
@@ -523,12 +501,9 @@ class RipperFixturesTest < RipperTestCase
523501
seattlerb/op_asgn_primary_colon_identifier1.txt
524502
seattlerb/op_asgn_primary_colon_identifier_command_call.txt
525503
seattlerb/op_asgn_val_dot_ident_command_call.txt
526-
seattlerb/parse_def_special_name.txt
527504
seattlerb/parse_if_not_canonical.txt
528505
seattlerb/parse_if_not_noncanonical.txt
529506
seattlerb/parse_line_defn_complex.txt
530-
seattlerb/parse_line_defn_no_parens.txt
531-
seattlerb/parse_line_defn_no_parens_args.txt
532507
seattlerb/parse_line_dstr_escaped_newline.txt
533508
seattlerb/parse_line_dstr_soft_newline.txt
534509
seattlerb/parse_line_evstr_after_break.txt
@@ -567,7 +542,6 @@ class RipperFixturesTest < RipperTestCase
567542
seattlerb/read_escape_unicode_h4.txt
568543
seattlerb/regexp_esc_C_slash.txt
569544
seattlerb/regexp_escape_extended.txt
570-
seattlerb/required_kwarg_no_value.txt
571545
seattlerb/rescue_do_end_ensure_result.txt
572546
seattlerb/rescue_do_end_no_raise.txt
573547
seattlerb/rescue_do_end_raised.txt
@@ -645,7 +619,6 @@ class RipperFixturesTest < RipperTestCase
645619
unparser/corpus/literal/while.txt
646620
unparser/corpus/literal/yield.txt
647621
unparser/corpus/semantic/block.txt
648-
unparser/corpus/semantic/def.txt
649622
unparser/corpus/semantic/dstr.txt
650623
unparser/corpus/semantic/kwbegin.txt
651624
unparser/corpus/semantic/literal.txt
@@ -658,9 +631,6 @@ class RipperFixturesTest < RipperTestCase
658631
whitequark/and_asgn.txt
659632
whitequark/and_or_masgn.txt
660633
whitequark/anonymous_blockarg.txt
661-
whitequark/arg.txt
662-
whitequark/arg_duplicate_ignored.txt
663-
whitequark/arg_label.txt
664634
whitequark/arg_scope.txt
665635
whitequark/args.txt
666636
whitequark/args_args_assocs.txt
@@ -681,20 +651,16 @@ class RipperFixturesTest < RipperTestCase
681651
whitequark/array_words_empty.txt
682652
whitequark/array_words_interp.txt
683653
whitequark/asgn_mrhs.txt
684-
whitequark/blockarg.txt
685654
whitequark/blockargs.txt
686655
whitequark/break_block.txt
687656
whitequark/bug_435.txt
688657
whitequark/bug_447.txt
689658
whitequark/bug_452.txt
690659
whitequark/bug_466.txt
691660
whitequark/bug_480.txt
692-
whitequark/bug_481.txt
693661
whitequark/bug_ascii_8bit_in_literal.txt
694662
whitequark/bug_cmd_string_lookahead.txt
695663
whitequark/bug_cmdarg.txt
696-
whitequark/bug_def_no_paren_eql_begin.txt
697-
whitequark/bug_do_block_in_call_args.txt
698664
whitequark/bug_do_block_in_cmdarg.txt
699665
whitequark/bug_do_block_in_hash_brace.txt
700666
whitequark/bug_heredoc_do.txt
@@ -737,13 +703,9 @@ class RipperFixturesTest < RipperTestCase
737703
whitequark/if_elsif.txt
738704
whitequark/if_masgn__24.txt
739705
whitequark/interp_digit_var.txt
740-
whitequark/kwarg.txt
741706
whitequark/kwbegin_compstmt.txt
742707
whitequark/kwnilarg.txt
743-
whitequark/kwoptarg.txt
744708
whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt
745-
whitequark/kwrestarg_named.txt
746-
whitequark/kwrestarg_unnamed.txt
747709
whitequark/lbrace_arg_after_command_args.txt
748710
whitequark/lparenarg_after_lvar__since_25.txt
749711
whitequark/lvar_injecting_match.txt
@@ -767,10 +729,8 @@ class RipperFixturesTest < RipperTestCase
767729
whitequark/op_asgn_cmd.txt
768730
whitequark/op_asgn_index.txt
769731
whitequark/op_asgn_index_cmd.txt
770-
whitequark/optarg.txt
771732
whitequark/or_asgn.txt
772733
whitequark/parser_bug_272.txt
773-
whitequark/parser_bug_490.txt
774734
whitequark/parser_bug_507.txt
775735
whitequark/parser_bug_525.txt
776736
whitequark/parser_bug_604.txt
@@ -790,8 +750,6 @@ class RipperFixturesTest < RipperTestCase
790750
whitequark/rescue_in_lambda_block.txt
791751
whitequark/rescue_mod_masgn.txt
792752
whitequark/rescue_without_begin_end.txt
793-
whitequark/restarg_named.txt
794-
whitequark/restarg_unnamed.txt
795753
whitequark/return.txt
796754
whitequark/return_block.txt
797755
whitequark/ruby_bug_10653.txt
@@ -808,7 +766,6 @@ class RipperFixturesTest < RipperTestCase
808766
whitequark/ruby_bug_13547.txt
809767
whitequark/ruby_bug_14690.txt
810768
whitequark/ruby_bug_15789.txt
811-
whitequark/ruby_bug_9669.txt
812769
whitequark/send_attr_asgn.txt
813770
whitequark/send_attr_asgn_conditional.txt
814771
whitequark/send_block_chain_cmd.txt
@@ -841,11 +798,10 @@ class RipperFixturesTest < RipperTestCase
841798
whitequark/ternary_ambiguous_symbol.txt
842799
whitequark/trailing_forward_arg.txt
843800
whitequark/unless_else.txt
844-
whitequark/var_op_asgn.txt
845801
whitequark/when_splat.txt
846802
whitequark/yield.txt
847803
xstring.txt
848-
yield.txt
804+
yield.txt
849805
]
850806

851807
relatives.each do |relative|

0 commit comments

Comments
 (0)