Skip to content

Commit

Permalink
Tweak callback list of Lint/MissingSuper [See rubocop#8376]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Aug 6, 2020
1 parent be47dc7 commit d3bad88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#8463](https://github.com/rubocop-hq/rubocop/pull/8463): Fix false positives for `Lint/OutOfRangeRegexpRef` when a regexp is defined and matched in separate steps. ([@eugeneius][])
* [#8466](https://github.com/rubocop-hq/rubocop/issues/8466): Fix a false positive for `Lint/UriRegexp` when using `regexp` method without receiver. ([@koic][])
* [#8478](https://github.com/rubocop-hq/rubocop/issues/8478): Relax `Lint/BinaryOperatorWithIdenticalOperands` for mathematical operations. ([@marcandre][])
* [#8480](https://github.com/rubocop-hq/rubocop/issues/8480): Tweak callback list of `Lint/MissingSuper`. ([@marcandre][])

## 0.89.0 (2020-08-05)

Expand Down
18 changes: 9 additions & 9 deletions lib/rubocop/cop/lint/missing_super.rb
Expand Up @@ -45,7 +45,13 @@ class MissingSuper < Base

OBJECT_LIFECYCLE_CALLBACKS = %i[method_missing respond_to_missing?].freeze
CLASS_LIFECYCLE_CALLBACKS = %i[inherited].freeze
MODULE_LIFECYCLE_CALLBACKS = %i[included extended prepended].freeze
METHOD_LIFECYCLE_CALLBACKS = %i[method_added method_removed method_undefined
singleton_method_added singleton_method_removed
singleton_method_undefined].freeze

CALLBACKS = (OBJECT_LIFECYCLE_CALLBACKS +
CLASS_LIFECYCLE_CALLBACKS +
METHOD_LIFECYCLE_CALLBACKS).to_set.freeze

def on_def(node)
return unless offender?(node)
Expand All @@ -70,15 +76,9 @@ def offender?(node)
end

def callback_method_def?(node)
method_name = node.method_name

if OBJECT_LIFECYCLE_CALLBACKS.include?(method_name) ||
CLASS_LIFECYCLE_CALLBACKS.include?(method_name)
return unless CALLBACKS.include?(node.method_name)

node.each_ancestor(:class, :sclass, :module).first
elsif MODULE_LIFECYCLE_CALLBACKS.include?(method_name)
node.each_ancestor(:module).first
end
node.each_ancestor(:class, :sclass, :module).first
end

def contains_super?(node)
Expand Down
2 changes: 0 additions & 2 deletions lib/rubocop/cop/mixin/enforce_superclass.rb
Expand Up @@ -5,8 +5,6 @@ module Cop
# Common functionality for enforcing a specific superclass
module EnforceSuperclass
def self.included(base)
super

base.def_node_matcher :class_definition, <<~PATTERN
(class (const _ !:#{base::SUPERCLASS}) #{base::BASE_PATTERN} ...)
PATTERN
Expand Down
24 changes: 12 additions & 12 deletions spec/rubocop/cop/lint/missing_super_spec.rb
Expand Up @@ -53,11 +53,10 @@ def initialize
end

context 'callbacks' do
it 'registers an offense when module callback without `super` call' do
expect_offense(<<~RUBY)
it 'registers no offense when module callback without `super` call' do
expect_no_offenses(<<~RUBY)
module M
def self.included(base)
^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
Expand Down Expand Up @@ -85,6 +84,16 @@ def inherited(base)
RUBY
end

it 'registers an offense when method callback is without `super` call' do
expect_offense(<<~RUBY)
class Foo
def method_added(*)
^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end

it 'registers an offense for instance level `method_missing?` with no `super` call' do
expect_offense(<<~RUBY)
class Foo
Expand Down Expand Up @@ -116,15 +125,6 @@ def method_missing(*args)
RUBY
end

it 'does not register an offense when class has instance method named as callback' do
expect_no_offenses(<<~RUBY)
class Foo
def included(base)
end
end
RUBY
end

it 'does not register an offense when callback has a `super` call' do
expect_no_offenses(<<~RUBY)
class Foo
Expand Down

0 comments on commit d3bad88

Please sign in to comment.