Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Lint/MissingSuper [See #8376] #8480

Merged
merged 1 commit into from Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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