diff --git a/changelog/fix_error_for_rails_active_support_load_hook.md b/changelog/fix_error_for_rails_active_support_load_hook.md new file mode 100644 index 0000000000..2b13be5802 --- /dev/null +++ b/changelog/fix_error_for_rails_active_support_load_hook.md @@ -0,0 +1 @@ +* [#1256](https://github.com/rubocop/rubocop-rails/pull/1256): Fix an error for `Rails/ActiveSupportOnLoad` when calling without arguments. ([@earlopain][]) diff --git a/lib/rubocop/cop/rails/active_support_on_load.rb b/lib/rubocop/cop/rails/active_support_on_load.rb index 1bb9f50d2b..c44b9050cd 100644 --- a/lib/rubocop/cop/rails/active_support_on_load.rb +++ b/lib/rubocop/cop/rails/active_support_on_load.rb @@ -57,7 +57,7 @@ class ActiveSupportOnLoad < Base def on_send(node) receiver, method, arguments = *node # rubocop:disable InternalAffairs/NodeDestructuring - return unless receiver && (hook = LOAD_HOOKS[receiver.const_name]) + return unless arguments && (hook = LOAD_HOOKS[receiver&.const_name]) preferred = "ActiveSupport.on_load(:#{hook}) { #{method} #{arguments.source} }" add_offense(node, message: format(MSG, prefer: preferred, current: node.source)) do |corrector| diff --git a/spec/rubocop/cop/rails/active_support_on_load_spec.rb b/spec/rubocop/cop/rails/active_support_on_load_spec.rb index 0d4adeb01a..984818aeca 100644 --- a/spec/rubocop/cop/rails/active_support_on_load_spec.rb +++ b/spec/rubocop/cop/rails/active_support_on_load_spec.rb @@ -12,6 +12,12 @@ RUBY end + it 'does not add offense for include without arguments' do + expect_no_offenses(<<~RUBY) + ActiveRecord::Base.include + RUBY + end + it 'adds offense and corrects when trying to extend a framework class with prepend' do expect_offense(<<~RUBY) ActiveRecord::Base.prepend(MyClass)