From 9d5cbe6e51f498f49152132518944ecb952d6f67 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 6 Aug 2020 16:42:04 -0400 Subject: [PATCH] Tweak callback list of Lint/MissingSuper [See #8376] --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/missing_super.rb | 18 ++++++++-------- lib/rubocop/cop/mixin/enforce_superclass.rb | 2 -- spec/rubocop/cop/lint/missing_super_spec.rb | 24 ++++++++++----------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e05d140f7f..a047b943c2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/missing_super.rb b/lib/rubocop/cop/lint/missing_super.rb index bdb0882dbed..ccf9d7d1b25 100644 --- a/lib/rubocop/cop/lint/missing_super.rb +++ b/lib/rubocop/cop/lint/missing_super.rb @@ -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) @@ -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) diff --git a/lib/rubocop/cop/mixin/enforce_superclass.rb b/lib/rubocop/cop/mixin/enforce_superclass.rb index 6a1fbc31757..e490149d8a2 100644 --- a/lib/rubocop/cop/mixin/enforce_superclass.rb +++ b/lib/rubocop/cop/mixin/enforce_superclass.rb @@ -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 diff --git a/spec/rubocop/cop/lint/missing_super_spec.rb b/spec/rubocop/cop/lint/missing_super_spec.rb index 464f3f46860..a85f6621b25 100644 --- a/spec/rubocop/cop/lint/missing_super_spec.rb +++ b/spec/rubocop/cop/lint/missing_super_spec.rb @@ -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 @@ -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 @@ -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