Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
314 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ class << self | |
attr_accessor :command_name | ||
|
||
def inherited(subclass) | ||
super | ||
@subclasses << subclass | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ def self.all | |
end | ||
|
||
def self.inherited(subclass) | ||
super | ||
all << subclass | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for the presence of constructors and lifecycle callbacks | ||
# without calls to `super`. | ||
# | ||
# @example | ||
# # bad | ||
# class Employee < Person | ||
# def initialize(name, salary) | ||
# @salary = salary | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class Employee < Person | ||
# def initialize(name, salary) | ||
# super(name) | ||
# @salary = salary | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# class Parent | ||
# def self.inherited(base) | ||
# do_something | ||
# end | ||
# end | ||
# | ||
# # good | ||
# class Parent | ||
# def self.inherited(base) | ||
# super | ||
# do_something | ||
# end | ||
# end | ||
# | ||
class MissingSuper < Base | ||
CONSTRUCTOR_MSG = 'Call `super` to initialize state of the parent class.' | ||
CALLBACK_MSG = 'Call `super` to invoke callback defined in the parent class.' | ||
|
||
STATELESS_CLASSES = %w[BasicObject Object].freeze | ||
|
||
OBJECT_LIFECYCLE_CALLBACKS = %i[method_missing respond_to_missing?].freeze | ||
CLASS_LIFECYCLE_CALLBACKS = %i[inherited].freeze | ||
MODULE_LIFECYCLE_CALLBACKS = %i[included extended prepended].freeze | ||
|
||
def on_def(node) | ||
return unless offender?(node) | ||
|
||
if node.method?(:initialize) | ||
add_offense(node, message: CONSTRUCTOR_MSG) if inside_class_with_stateful_parent?(node) | ||
elsif callback_method_def?(node) | ||
add_offense(node, message: CALLBACK_MSG) | ||
end | ||
end | ||
|
||
def on_defs(node) | ||
return if !callback_method_def?(node) || contains_super?(node) | ||
|
||
add_offense(node, message: CALLBACK_MSG) | ||
end | ||
|
||
private | ||
|
||
def offender?(node) | ||
(node.method?(:initialize) || callback_method_def?(node)) && !contains_super?(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) | ||
|
||
node.each_ancestor(:class, :sclass, :module).first | ||
elsif MODULE_LIFECYCLE_CALLBACKS.include?(method_name) | ||
node.each_ancestor(:module).first | ||
end | ||
end | ||
|
||
def contains_super?(node) | ||
node.each_descendant(:super, :zsuper).any? | ||
end | ||
|
||
def inside_class_with_stateful_parent?(node) | ||
class_node = node.each_ancestor(:class).first | ||
class_node&.parent_class && !stateless_class?(class_node.parent_class) | ||
end | ||
|
||
def stateless_class?(node) | ||
STATELESS_CLASSES.include?(node.const_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ def self.classes | |
end | ||
|
||
def self.inherited(subclass) | ||
super | ||
classes << subclass | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.