Class: RuboCop::Cop::Style::StructInheritance

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

Overview

Checks for inheritance from Struct.new. Inheriting from Struct.new adds a superfluous level in inheritance tree.

Examples:

# bad
class Person < Struct.new(:first_name, :last_name)
  def age
    42
  end
end

Person.ancestors
# => [Person, #<Class:0x000000010b4e14a0>, Struct, (...)]

# good
Person = Struct.new(:first_name, :last_name) do
  def age
    42
  end
end

Person.ancestors
# => [Person, Struct, (...)]

Constant Summary collapse

MSG =
"Don't extend an instance initialized by `Struct.new`. " \
'Use a block to customize the struct.'
LEFT_OPERAND_TYPES =
i[and or irange erange match_pattern match_pattern_p].freeze
ASSIGNMENT_TYPES =
i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
BODY_TYPES =
i[module class sclass any_def any_block when in_pattern resbody for].freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants inherited from Base

Base::DEPARTMENT_SEVERITIES, Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, #disabled_autocorrect?, 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, #preview?, #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

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

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

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

emulate_write_read_cycle, replace_file_contents, silence_warnings

Constructor Details

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

Class Method Details

.autocorrect_incompatible_with ⇒ Object

When the closing parenthesis of Struct.new is on its own line, omitting the parentheses in the same pass leaves a line break before the appended do.



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

def self.autocorrect_incompatible_with
  [Style::MethodCallWithArgsParentheses]
end

Instance Method Details

#on_class(node) ⇒ Object



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

def on_class(node)
  return unless struct_constructor?(node.parent_class)
  return if defines_constants?(node.body)

  add_offense(node.parent_class) do |corrector|
    keyword_range =
      range_with_surrounding_space(node.loc.keyword, side: :right, newlines: false)
    corrector.remove(keyword_range)
    corrector.replace(node.loc.operator, '=')

    correct_parent(node.parent_class, corrector)
    parenthesize_left_operand(corrector, node, keyword_range)
  end
end

#struct_constructor?(node) ⇒ Object



69
70
71
72
# File 'lib/rubocop/cop/style/struct_inheritance.rb', line 69

def_node_matcher :struct_constructor?, "{(send (const {nil? cbase} :Struct) :new ...)\n (block (send (const {nil? cbase} :Struct) :new ...) ...)}\n"