Class: RuboCop::Cop::Style::EmptyClassDefinition

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Alignment, ConfigurableEnforcedStyle, RangeHelp
Defined in:
lib/rubocop/cop/style/empty_class_definition.rb

Overview

Enforces consistent style for empty class definitions.

This cop can enforce either a two-line class definition or Class.new for classes with no body.

The supported styles are:

  • class_definition (default) - prefer two-line class definition over Class.new

  • class_new - prefer Class.new over class definition

Examples:

EnforcedStyle: class_definition (default)

# bad
FooError = Class.new(StandardError)

# okish
class FooError < StandardError; end

# good
class FooError < StandardError
end

EnforcedStyle: class_new

# bad
class FooError < StandardError
end

# bad
class FooError < StandardError; end

# good
FooError = Class.new(StandardError)

Constant Summary collapse

MSG_CLASS_DEFINITION =
'Prefer a two-line class definition over `Class.new` for classes with no body.'
MSG_CLASS_NEW =
'Prefer `Class.new` over class definition for classes with no body.'

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants included from Alignment

Alignment::SPACE

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#class_new_assignment?(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/style/empty_class_definition.rb', line 49

def_node_matcher :class_new_assignment?, "(casgn _ _ (send (const _ :Class) :new ...))\n"

#on_casgn(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/style/empty_class_definition.rb', line 53

def on_casgn(node)
  return unless style == :class_definition
  return unless node.expression

  class_new_node = find_class_new_node(node.expression)
  return if chained_with_any_method?(node.expression, class_new_node)
  return if variable_parent_class?(class_new_node)

  add_offense(node, message: MSG_CLASS_DEFINITION) do |corrector|
    autocorrect_class_new(corrector, node)
  end
end

#on_class(node) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/style/empty_class_definition.rb', line 66

def on_class(node)
  return unless style == :class_new
  return unless empty_class?(node)

  add_offense(node, message: MSG_CLASS_NEW) do |corrector|
    autocorrect_class_definition(corrector, node)
  end
end