Project

General

Profile

Feature #17398 ยป allow-command-style-endless-method-def.patch

mame (Yusuke Endoh), 04/19/2021 05:05 AM

View differences:

parse.y
/*% %*/
/*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
}
| defn_head f_opt_paren_args '=' command
{
endless_method_name(p, $<node>1, &@1);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$$ = set_defun_body(p, $1, $2, $4, &@$);
/*% %*/
/*% ripper: def!(get_value($1), $2, $4) %*/
local_pop(p);
}
| defn_head f_opt_paren_args '=' command modifier_rescue arg
{
endless_method_name(p, $<node>1, &@1);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
$$ = set_defun_body(p, $1, $2, $4, &@$);
/*% %*/
/*% ripper: def!(get_value($1), $2, rescue_mod!($4, $6)) %*/
local_pop(p);
}
| defs_head f_opt_paren_args '=' command
{
endless_method_name(p, $<node>1, &@1);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$$ = set_defun_body(p, $1, $2, $4, &@$);
/*%
$1 = get_value($1);
%*/
/*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
local_pop(p);
}
| defs_head f_opt_paren_args '=' command modifier_rescue arg
{
endless_method_name(p, $<node>1, &@1);
restore_defun(p, $<node>1->nd_defn);
/*%%%*/
$4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
$$ = set_defun_body(p, $1, $2, $4, &@$);
/*%
$1 = get_value($1);
%*/
/*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, rescue_mod!($4, $6)) %*/
local_pop(p);
}
| backref tOP_ASGN lex_ctxt command_rhs
{
/*%%%*/
test/ruby/test_syntax.rb
assert_syntax_error('def obj.foo=() = 42 rescue nil', error)
end
def test_methoddef_endless_command
assert_valid_syntax('def foo = puts "Hello"')
assert_valid_syntax('def foo() = puts "Hello"')
assert_valid_syntax('def foo(x) = puts x')
assert_valid_syntax('def obj.foo = puts "Hello"')
assert_valid_syntax('def obj.foo() = puts "Hello"')
assert_valid_syntax('def obj.foo(x) = puts x')
k = Class.new do
class_eval('def rescued(x) = raise "to be caught" rescue "instance #{x}"')
class_eval('def self.rescued(x) = raise "to be caught" rescue "class #{x}"')
end
assert_equal("class ok", k.rescued("ok"))
assert_equal("instance ok", k.new.rescued("ok"))
# Current technical limitation: cannot prepend "private" or something for command endless def
error = /syntax error, unexpected string literal/
error2 = /syntax error, unexpected local variable or method/
assert_syntax_error('private def foo = puts "Hello"', error)
assert_syntax_error('private def foo() = puts "Hello"', error)
assert_syntax_error('private def foo(x) = puts x', error2)
assert_syntax_error('private def obj.foo = puts "Hello"', error)
assert_syntax_error('private def obj.foo() = puts "Hello"', error)
assert_syntax_error('private def obj.foo(x) = puts x', error2)
end
def test_methoddef_in_cond
assert_valid_syntax('while def foo; tap do end; end; break; end')
assert_valid_syntax('while def foo a = tap do end; end; break; end')
    (1-1/1)