Class: RuboCop::Cop::Style::DataInheritance
- Extended by:
- AutoCorrector, TargetRubyVersion
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/style/data_inheritance.rb
Overview
Checks for inheritance from Data.define to avoid creating the anonymous parent class.
Inheriting from Data.define adds a superfluous level in inheritance tree.
Constant Summary collapse
- MSG =
"Don't extend an instance initialized by `Data.define`. " \ 'Use a block to customize the class.'
- 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
Methods included from TargetRubyVersion
maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?
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
50 51 52 |
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 50 def self.autocorrect_incompatible_with [Style::MethodCallWithArgsParentheses] end |
Instance Method Details
#data_define?(node) ⇒ Object
72 73 74 75 |
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 72 def_node_matcher :data_define?, <<~PATTERN {(send (const {nil? cbase} :Data) :define ...) (block (send (const {nil? cbase} :Data) :define ...) ...)} PATTERN |
#on_class(node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 54 def on_class(node) return unless data_define?(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 |