diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb index 2ec3b1a79682e51d7ed984673bc47415a774d6ef..bc202ff2e4e032f8bd41256b2bb551a697c78000 100644 --- a/activesupport/lib/active_support/deprecation/method_wrappers.rb +++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb @@ -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 diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 9898a1d9a0fa1800ce5c3590c05681655ccc94b6..9c7f03f10d33b683639ff3910900328b31466e12 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -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