From 262270f65f9defd10c4bd5d87c7d32fd415e4f91 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 7 Feb 2014 13:47:28 +0100 Subject: [PATCH] Handle splat argument followed by regular arguments --- .../sexp_handlers/helper_methods.rb | 3 + test/unit/parser_method_calls_test.rb | 119 ++++++++++++++++++ test/unit/parser_test.rb | 98 --------------- 3 files changed, 122 insertions(+), 98 deletions(-) diff --git a/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb b/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb index ed928627..a4e33c15 100644 --- a/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +++ b/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb @@ -57,6 +57,9 @@ def generic_add_star exp _, args, splatarg = exp.shift 3 items = handle_potentially_typeless_sexp args items << s(:splat, process(splatarg)) + until exp.empty? + items << process(exp.shift) + end items end diff --git a/test/unit/parser_method_calls_test.rb b/test/unit/parser_method_calls_test.rb index f1778993..cf6ef363 100644 --- a/test/unit/parser_method_calls_test.rb +++ b/test/unit/parser_method_calls_test.rb @@ -2,6 +2,125 @@ describe RipperRubyParser::Parser do describe "#parse" do + describe "for method calls" do + describe "without a receiver" do + it "works without brackets" do + "foo bar". + must_be_parsed_as s(:call, nil, :foo, + s(:call, nil, :bar)) + end + + it "works with brackets" do + "foo(bar)". + must_be_parsed_as s(:call, nil, :foo, + s(:call, nil, :bar)) + end + + it "works with an empty parameter list and no brackets" do + "foo". + must_be_parsed_as s(:call, nil, :foo) + end + + it "works with brackets around an empty parameter list" do + "foo()". + must_be_parsed_as s(:call, nil, :foo) + end + + it "works for methods ending in a question mark" do + "foo?". + must_be_parsed_as s(:call, nil, :foo?) + end + + it "works with nested calls without brackets" do + "foo bar baz". + must_be_parsed_as s(:call, nil, :foo, + s(:call, nil, :bar, + s(:call, nil, :baz))) + end + + it "works with a non-final splat argument" do + "foo(bar, *baz, qux)". + must_be_parsed_as s(:call, + nil, + :foo, + s(:call, nil, :bar), + s(:splat, s(:call, nil, :baz)), + s(:call, nil, :qux)) + end + + it "works with a splat argument followed by several regular arguments" do + "foo(bar, *baz, qux, quuz)". + must_be_parsed_as s(:call, + nil, + :foo, + s(:call, nil, :bar), + s(:splat, s(:call, nil, :baz)), + s(:call, nil, :qux), + s(:call, nil, :quuz)) + end + end + + describe "with a receiver" do + it "works without brackets" do + "foo.bar baz". + must_be_parsed_as s(:call, + s(:call, nil, :foo), + :bar, + s(:call, nil, :baz)) + end + + it "works with brackets" do + "foo.bar(baz)". + must_be_parsed_as s(:call, + s(:call, nil, :foo), + :bar, + s(:call, nil, :baz)) + end + + it "works with brackets around a call with no brackets" do + "foo.bar(baz qux)". + must_be_parsed_as s(:call, + s(:call, nil, :foo), + :bar, + s(:call, nil, :baz, + s(:call, nil, :qux))) + end + + it "works with nested calls without brackets" do + "foo.bar baz qux". + must_be_parsed_as s(:call, + s(:call, nil, :foo), + :bar, + s(:call, nil, :baz, + s(:call, nil, :qux))) + end + end + + describe "with blocks" do + it "works for a do block" do + "foo.bar do baz; end". + must_be_parsed_as s(:iter, + s(:call, + s(:call, nil, :foo), + :bar), + s(:args), + s(:call, nil, :baz)) + end + + it "works for a do block with several statements" do + "foo.bar do baz; qux; end". + must_be_parsed_as s(:iter, + s(:call, + s(:call, nil, :foo), + :bar), + s(:args), + s(:block, + s(:call, nil, :baz), + s(:call, nil, :qux))) + end + end + end + describe "for calls to super" do specify { "super".must_be_parsed_as s(:zsuper) } specify { "super foo".must_be_parsed_as s(:super, diff --git a/test/unit/parser_test.rb b/test/unit/parser_test.rb index 7f3e5415..26bb656f 100644 --- a/test/unit/parser_test.rb +++ b/test/unit/parser_test.rb @@ -419,104 +419,6 @@ end end - describe "for method calls" do - describe "without a receiver" do - it "works without brackets" do - "foo bar". - must_be_parsed_as s(:call, nil, :foo, - s(:call, nil, :bar)) - end - - it "works with brackets" do - "foo(bar)". - must_be_parsed_as s(:call, nil, :foo, - s(:call, nil, :bar)) - end - - it "works with an empty parameter list and no brackets" do - "foo". - must_be_parsed_as s(:call, nil, :foo) - end - - it "works with brackets around an empty parameter list" do - "foo()". - must_be_parsed_as s(:call, nil, :foo) - end - - it "works for methods ending in a question mark" do - "foo?". - must_be_parsed_as s(:call, nil, :foo?) - end - - it "works with nested calls without brackets" do - "foo bar baz". - must_be_parsed_as s(:call, nil, :foo, - s(:call, nil, :bar, - s(:call, nil, :baz))) - end - end - - describe "with a receiver" do - it "works without brackets" do - "foo.bar baz". - must_be_parsed_as s(:call, - s(:call, nil, :foo), - :bar, - s(:call, nil, :baz)) - end - - it "works with brackets" do - "foo.bar(baz)". - must_be_parsed_as s(:call, - s(:call, nil, :foo), - :bar, - s(:call, nil, :baz)) - end - - it "works with brackets around a call with no brackets" do - "foo.bar(baz qux)". - must_be_parsed_as s(:call, - s(:call, nil, :foo), - :bar, - s(:call, nil, :baz, - s(:call, nil, :qux))) - end - - it "works with nested calls without brackets" do - "foo.bar baz qux". - must_be_parsed_as s(:call, - s(:call, nil, :foo), - :bar, - s(:call, nil, :baz, - s(:call, nil, :qux))) - end - end - - describe "with blocks" do - it "works for a do block" do - "foo.bar do baz; end". - must_be_parsed_as s(:iter, - s(:call, - s(:call, nil, :foo), - :bar), - s(:args), - s(:call, nil, :baz)) - end - - it "works for a do block with several statements" do - "foo.bar do baz; qux; end". - must_be_parsed_as s(:iter, - s(:call, - s(:call, nil, :foo), - :bar), - s(:args), - s(:block, - s(:call, nil, :baz), - s(:call, nil, :qux))) - end - end - end - describe "for blocks" do it "works with no statements in the block body" do "foo do; end".