class RuboCop::Cop::Lint::MissingSuper
end
end
do_something
def initialize
class MyConcreteClass < MyAbstractClass
# good
@example AllowedParentClasses: [MyAbstractClass]
end
end
do_something
def initialize
class ClassWithNoParent
# good
end
end
do_something
super
def self.inherited(base)
class Parent
# good
end
end
do_something
def self.inherited(base)
class Parent
# bad
end
end
@salary = salary
super(name)
def initialize(name, salary)
Employee = Class.new(Person) do
# good
end
end
@salary = salary
def initialize(name, salary)
Employee = Class.new(Person) do
# bad
end
end
@salary = salary
super(name)
def initialize(name, salary)
class Employee < Person
# good
end
end
@salary = salary
def initialize(name, salary)
class Employee < Person
# bad
@example
*in addition to* ‘Object` and `BasicObject`.
`AllowedParentClasses` option to specify which classes should be allowed
not meant to be called with `super`. In those cases, you can use the
classes from this cop, for example in the case of an abstract class that is
stateless nature. However, sometimes you might want to allow other parent
`Object` and `BasicObject` are allowed by this cop because of their
determined automatically.
Autocorrection is not supported because the position of `super` cannot be
challenging or verbose for no actual gain.
missing method. In other cases, the theoretical ideal handling could be
because in some cases it makes sense to overtake what is considered a
This cop does not consider `method_missing` (and `respond_to_missing?`)
without calls to `super`.
Checks for the presence of constructors and lifecycle callbacks
def allowed_class?(node)
def allowed_class?(node) allowed_classes.include?(node.const_name) end
def allowed_classes
def allowed_classes @allowed_classes ||= STATELESS_CLASSES + cop_config.fetch('AllowedParentClasses', []) end
def callback_method_def?(node)
def callback_method_def?(node) return false unless CALLBACKS.include?(node.method_name) node.each_ancestor(:class, :sclass, :module).first end
def contains_super?(node)
def contains_super?(node) node.each_descendant(:super, :zsuper).any? end
def inside_class_with_stateful_parent?(node)
def inside_class_with_stateful_parent?(node) if (block_node = node.each_ancestor(:block, :numblock).first) return false unless (super_class = class_new_block(block_node)) !allowed_class?(super_class) elsif (class_node = node.each_ancestor(:class).first) class_node.parent_class && !allowed_class?(class_node.parent_class) else false end end
def offender?(node)
def offender?(node) (node.method?(:initialize) || callback_method_def?(node)) && !contains_super?(node) end
def on_def(node)
def on_def(node) return unless offender?(node) if node.method?(:initialize) && inside_class_with_stateful_parent?(node) add_offense(node, message: CONSTRUCTOR_MSG) elsif callback_method_def?(node) add_offense(node, message: CALLBACK_MSG) end end
def on_defs(node)
def on_defs(node) return if !callback_method_def?(node) || contains_super?(node) add_offense(node, message: CALLBACK_MSG) end