提交 c17724b1 编写于 作者: R Ryuta Kamizono

Fix `deprecate_methods` to not expand positional argument hash

上级 ec2e7d57
......@@ -58,29 +58,21 @@ def deprecate_methods(target_module, *method_names)
method_names.each do |method_name|
if target_module.method_defined?(method_name) || target_module.private_method_defined?(method_name)
method = target_module.instance_method(method_name)
if RUBY_VERSION < "2.7"
target_module.redefine_method(method_name) do |*args, &block|
target_module.module_eval do
redefine_method(method_name) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name])
method.bind(self).call(*args, &block)
end
else
target_module.redefine_method(method_name) do |*args, **kwargs, &block|
deprecator.deprecation_warning(method_name, options[method_name])
method.bind(self).call(*args, **kwargs, &block)
end
ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
end
else
mod ||= Module.new
if RUBY_VERSION < "2.7"
mod.define_method(method_name) do |*args, &block|
mod.module_eval do
define_method(method_name) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name])
super(*args, &block)
end
else
mod.define_method(method_name) do |*args, **kwargs, &block|
deprecator.deprecation_warning(method_name, options[method_name])
super(*args, **kwargs, &block)
end
ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
end
end
end
......
......@@ -32,7 +32,7 @@ def f=(v); end
deprecate :f=
deprecate :g
def g; end
def g(h) h end
module B
C = 1
......@@ -85,7 +85,7 @@ def test_undeprecated
end
end
def test_deprecate_class_method
def test_deprecate_method_on_class
assert_deprecated(/none is deprecated/) do
assert_equal 1, @dtc.none
end
......@@ -99,6 +99,18 @@ def test_deprecate_class_method
end
end
def test_deprecate_method_doesnt_expand_positional_argument_hash
hash = { k: 1 }
assert_deprecated(/one is deprecated/) do
assert_same hash, @dtc.one(hash)
end
assert_deprecated(/g is deprecated/) do
assert_same hash, @dtc.g(hash)
end
end
def test_deprecate_object
deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:")
assert_deprecated(/:bomb:/) { deprecated_object.to_s }
......@@ -446,7 +458,7 @@ def test_custom_gem_name
end
def test_deprecate_work_before_define_method
assert_deprecated { @dtc.g }
assert_deprecated(/g is deprecated/) { @dtc.g(1) }
end
private
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册