spec/transformer_spec.rb in ripper-plus-1.1.0.pre2 vs spec/transformer_spec.rb in ripper-plus-1.2.0

- old
+ new

@@ -246,10 +246,70 @@ false]]], [[:void_stmt]]]]] input_tree.should transform_to(output_tree) end + it 'should transform respecting stabby-lambda arguments' do + input_tree = + [:program, + [[:assign, + [:var_field, [:@ident, "y", [1, 0]]], + [:lambda, + [:paren, + [:params, + [[:@ident, "a", [1, 7]]], + [[[:@ident, "b", [1, 10]], [:var_ref, [:@ident, "a", [1, 12]]]], + [[:@ident, "c", [1, 15]], [:var_ref, [:@ident, "d", [1, 17]]]]], + [:rest_param, [:@ident, "rest", [1, 21]]], + nil, + nil]], + [[:binary, + [:array, + [[:binary, + [:binary, + [:binary, + [:var_ref, [:@ident, "a", [1, 30]]], + :*, + [:var_ref, [:@ident, "b", [1, 34]]]], + :*, + [:var_ref, [:@ident, "c", [1, 38]]]], + :*, + [:var_ref, [:@ident, "d", [1, 42]]]]]], + :+, + [:var_ref, [:@ident, "rest", [1, 47]]]]]]], + [:var_ref, [:@ident, "a", [1, 55]]]]] + output_tree = + [:program, + [[:assign, + [:var_field, [:@ident, "y", [1, 0]]], + [:lambda, + [:paren, + [:params, + [[:@ident, "a", [1, 7]]], + [[[:@ident, "b", [1, 10]], [:var_ref, [:@ident, "a", [1, 12]]]], + [[:@ident, "c", [1, 15]], [:zcall, [:@ident, "d", [1, 17]]]]], + [:rest_param, [:@ident, "rest", [1, 21]]], + nil, + nil]], + [[:binary, + [:array, + [[:binary, + [:binary, + [:binary, + [:var_ref, [:@ident, "a", [1, 30]]], + :*, + [:var_ref, [:@ident, "b", [1, 34]]]], + :*, + [:var_ref, [:@ident, "c", [1, 38]]]], + :*, + [:zcall, [:@ident, "d", [1, 42]]]]]], + :+, + [:var_ref, [:@ident, "rest", [1, 47]]]]]]], + [:zcall, [:@ident, "a", [1, 55]]]]] + input_tree.should transform_to output_tree + end + it 'should transform respecting subassignments in block arguments' do input_tree = [:program, [[:assign, [:var_field, [:@ident, "x", [1, 0]]], [:@int, "10", [1, 4]]], [:method_add_block, @@ -561,10 +621,102 @@ [:command, [:@ident, "p", [1, 76]], [:args_add_block, [[:zcall, [:@ident, "err", [1, 78]]]], false]]]]]]]] input_tree.should transform_to output_tree end + + it 'finds local variables created by named captures' do + input_tree = + [:program, + [[:def, + [:@ident, "foo", [1, 4]], + [:paren, [:params, [[:@ident, "a", [1, 8]]], nil, nil, nil, nil]], + [:bodystmt, + [[:void_stmt], + [:binary, + [:regexp_literal, + [[:@tstring_content, "name: (?<name>w+)", [1, 13]]], + [:@regexp_end, "/", [1, 30]]], + :=~, + [:var_ref, [:@ident, "a", [1, 35]]]], + [:command, + [:@ident, "p", [1, 38]], + [:args_add_block, [[:var_ref, [:@ident, "name", [1, 40]]]], false]], + [:var_ref, [:@ident, "named", [1, 46]]]], + nil, + nil, + nil]]]] + output_tree = + [:program, + [[:def, + [:@ident, "foo", [1, 4]], + [:paren, [:params, [[:@ident, "a", [1, 8]]], nil, nil, nil, nil]], + [:bodystmt, + [[:void_stmt], + [:binary, + [:regexp_literal, + [[:@tstring_content, "name: (?<name>w+)", [1, 13]]], + [:@regexp_end, "/", [1, 30]]], + :=~, + [:var_ref, [:@ident, "a", [1, 35]]]], + [:command, + [:@ident, "p", [1, 38]], + [:args_add_block, [[:var_ref, [:@ident, "name", [1, 40]]]], false]], + [:zcall, [:@ident, "named", [1, 46]]]], + nil, + nil, + nil]]]] + input_tree.should transform_to output_tree + end + + it 'finds local variables created by named captures in a paren LHS' do + input_tree = + [:program, + [[:def, + [:@ident, "abc", [1, 4]], + [:paren, [:params, [[:@ident, "a", [1, 8]]], nil, nil, nil, nil]], + [:bodystmt, + [[:void_stmt], + [:binary, + [:paren, + [[:var_ref, [:@ident, "foo", [1, 13]]], + [:var_ref, [:@ident, "bar", [1, 18]]], + [:regexp_literal, + [[:@tstring_content, "name: (?<name>w+) (?<numba>d+)", [1, 24]]], + [:@regexp_end, "/", [1, 54]]]]], + :=~, + [:var_ref, [:@ident, "a", [1, 60]]]], + [:var_ref, [:@ident, "name", [1, 63]]], + [:var_ref, [:@ident, "numba", [1, 69]]], + [:var_ref, [:@ident, "number", [1, 76]]]], + nil, + nil, + nil]]]] + output_tree = + [:program, + [[:def, + [:@ident, "abc", [1, 4]], + [:paren, [:params, [[:@ident, "a", [1, 8]]], nil, nil, nil, nil]], + [:bodystmt, + [[:void_stmt], + [:binary, + [:paren, + [[:zcall, [:@ident, "foo", [1, 13]]], + [:zcall, [:@ident, "bar", [1, 18]]], + [:regexp_literal, + [[:@tstring_content, "name: (?<name>w+) (?<numba>d+)", [1, 24]]], + [:@regexp_end, "/", [1, 54]]]]], + :=~, + [:var_ref, [:@ident, "a", [1, 60]]]], + [:var_ref, [:@ident, "name", [1, 63]]], + [:var_ref, [:@ident, "numba", [1, 69]]], + [:zcall, [:@ident, "number", [1, 76]]]], + nil, + nil, + nil]]]] + input_tree.should transform_to output_tree + end end describe 'error transformation' do it 'should push up module name errors' do input_tree = [:program, @@ -819,7 +971,185 @@ error[1][0].should be fail_node end end end + it 'should wrap method definitions with duplicated arguments in error nodes' do + input_tree = + [:program, + [[:def, + [:@ident, "foo", [1, 4]], + [:paren, + [:params, + [[:@ident, "a", [1, 8]], [:@ident, "a", [1, 11]]], + nil, + nil, + nil, + nil]], + [:bodystmt, [[:void_stmt]], nil, nil, nil]]]] + output_tree = + [:program, + [[:error, + [:def, + [:@ident, "foo", [1, 4]], + [:paren, + [:params, + [[:@ident, "a", [1, 8]], [:@ident, "a", [1, 11]]], + nil, + nil, + nil, + nil]], + [:bodystmt, [[:void_stmt]], nil, nil, nil]]]]] + input_tree.should transform_to output_tree + end + + it 'should wrap singleton method definitions with duplicated arguments in error nodes' do + input_tree = + [:program, + [[:defs, + [:var_ref, [:@kw, "self", [1, 4]]], + [:@period, ".", [1, 8]], + [:@ident, "foo", [1, 4]], + [:paren, + [:params, + [[:@ident, "a", [1, 8]], [:@ident, "a", [1, 11]]], + nil, + nil, + nil, + nil]], + [:bodystmt, [[:void_stmt]], nil, nil, nil]]]] + output_tree = + [:program, + [[:error, + [:defs, + [:var_ref, [:@kw, "self", [1, 4]]], + [:@period, ".", [1, 8]], + [:@ident, "foo", [1, 4]], + [:paren, + [:params, + [[:@ident, "a", [1, 8]], [:@ident, "a", [1, 11]]], + nil, + nil, + nil, + nil]], + [:bodystmt, [[:void_stmt]], nil, nil, nil]]]]] + input_tree.should transform_to output_tree + end + + it 'should wrap stabby lambda definitions with duplicated arguments in error nodes' do + input_tree = + [:program, + [[:lambda, + [:paren, + [:params, + [[:@ident, "a", [1, 3]], [:@ident, "b", [1, 6]], [:@ident, "a", [1, 9]]], + nil, + nil, + nil, + nil]], + [[:void_stmt]]]]] + output_tree = + [:program, + [[:error, + [:lambda, + [:paren, + [:params, + [[:@ident, "a", [1, 3]], [:@ident, "b", [1, 6]], [:@ident, "a", [1, 9]]], + nil, + nil, + nil, + nil]], + [[:void_stmt]]]]]] + input_tree.should transform_to output_tree + end + + it 'should wrap block calls with duplicated block args in error nodes' do + input_tree = + [:program, + [[:method_add_block, + [:call, + [:var_ref, [:@kw, "self", [1, 0]]], + :".", + [:@ident, "foo!", [1, 5]]], + [:brace_block, + [:block_var, + [:params, + [[:@ident, "a", [1, 13]]], + [[[:@ident, "b", [1, 16]], [:@int, "3", [1, 18]]]], + nil, + [[:mlhs_paren, + [:mlhs_add_star, + [[:mlhs_paren, [:@ident, "c", [1, 22]]]], + [:@ident, "a", [1, 26]]]]], + nil], + nil], + [[:void_stmt]]]]]] + output_tree = + [:program, + [[:error, + [:method_add_block, + [:call, + [:var_ref, [:@kw, "self", [1, 0]]], + :".", + [:@ident, "foo!", [1, 5]]], + [:brace_block, + [:block_var, + [:params, + [[:@ident, "a", [1, 13]]], + [[[:@ident, "b", [1, 16]], [:@int, "3", [1, 18]]]], + nil, + [[:mlhs_paren, + [:mlhs_add_star, + [[:mlhs_paren, [:@ident, "c", [1, 22]]]], + [:@ident, "a", [1, 26]]]]], + nil], + nil], + [[:void_stmt]]]]]]] + input_tree.should transform_to output_tree + end + + it 'should wrap block calls with duplicated block-local args in error nodes' do + input_tree = + [:program, + [[:method_add_block, + [:call, + [:var_ref, [:@kw, "self", [1, 0]]], + :".", + [:@ident, "foo!", [1, 5]]], + [:brace_block, + [:block_var, + [:params, + [[:@ident, "a", [1, 13]]], + [[[:@ident, "b", [1, 16]], [:@int, "3", [1, 18]]]], + nil, + [[:mlhs_paren, + [:mlhs_add_star, + [[:mlhs_paren, [:@ident, "c", [1, 22]]]], + [:@ident, "d", [1, 26]]]]], + nil], + [[:@ident, "e", [1, 30]], [:@ident, "a", [1, 33]]]], + [[:void_stmt]]]]]] + output_tree = + [:program, + [[:error, + [:method_add_block, + [:call, + [:var_ref, [:@kw, "self", [1, 0]]], + :".", + [:@ident, "foo!", [1, 5]]], + [:brace_block, + [:block_var, + [:params, + [[:@ident, "a", [1, 13]]], + [[[:@ident, "b", [1, 16]], [:@int, "3", [1, 18]]]], + nil, + [[:mlhs_paren, + [:mlhs_add_star, + [[:mlhs_paren, [:@ident, "c", [1, 22]]]], + [:@ident, "d", [1, 26]]]]], + nil], + [[:@ident, "e", [1, 30]], [:@ident, "a", [1, 33]]]], + [[:void_stmt]]]]]]] + input_tree.should transform_to output_tree + end end end