1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

parse.y: Allow "command" syntax in endless method definition

This change allows `def hello = puts "Hello"` without parentheses.

Note that `private def hello = puts "Hello"` does not parse for
technical reason.

[Feature #17398]
This commit is contained in:
Yusuke Endoh 2021-05-13 00:14:50 +09:00
parent 81513c9dab
commit 31794d2e73
2 changed files with 71 additions and 0 deletions

46
parse.y
View file

@ -1629,6 +1629,52 @@ command_asgn : lhs '=' lex_ctxt command_rhs
/*% %*/
/*% 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
{
/*%%%*/

View file

@ -1462,6 +1462,31 @@ eom
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')