Class: RuboCop::Cop::Style::DataInheritance
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::TargetRubyVersion,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| 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
-
ASSIGNMENT_TYPES =
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 45%i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
-
BODY_TYPES =
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 46%i[module class sclass any_def any_block when in_pattern resbody for].freeze
-
LEFT_OPERAND_TYPES =
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 44%i[and or irange erange match_pattern match_pattern_p].freeze
-
MSG =
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 42"Don't extend an instance initialized by `Data.define`. " \ 'Use a block to customize the class.'
::RuboCop::Cop::Base - Inherited
DEPARTMENT_SEVERITIES, EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp - Included
Class Attribute Summary
::RuboCop::Cop::AutoCorrector - Extended
::RuboCop::Cop::Base - Inherited
| .gem_requirements, .lint?, | |
| .support_autocorrect? | Returns if class supports autocorrect. |
| .support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::TargetRubyVersion - Extended
| maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version? |
::RuboCop::Cop::Base - Inherited
| .autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
| .badge | Naming. |
| .callbacks_needed, .cop_name, .department, | |
| .documentation_url | Returns a url to view this cops documentation online. |
| .exclude_from_registry | Call for abstract Cop classes. |
| .inherited, | |
| .joining_forces | Override and return the Force class(es) you need to join. |
| .match? | Returns true if the cop name or the cop namespace matches any of the given names. |
| .new, | |
| .requires_gem | Register a version requirement for the given gem name. |
| .restrict_on_send | Reserved for Commissioner. |
::RuboCop::ExcludeLimit - Extended
| exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
| transform | |
Instance Attribute Summary
::RuboCop::Cop::Base - Inherited
| #active_support_extensions_enabled?, #always_autocorrect?, #config, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #disabled_autocorrect?, | |
| #preview? | Whether the user opted in to unstable behavior, with |
| #processed_source, #project_index, #string_literals_frozen_by_default?, #target_satisfies_all_gem_version_requirements? | |
::RuboCop::Cop::AutocorrectLogic - Included
| #autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?, | |
| #skipped_unsafe_correction_with_disable_uncorrectable? | Whether a correction exists but is skipped because it is unsafe in a safe autocorrect run, while |
| #contextual_autocorrect_held_back? | When LSP is enabled or the |
Instance Method Summary
- #data_define?(node)
- #on_class(node)
- #conditional_statement?(node) ⇒ Boolean private
- #correct_parent(parent, corrector) private
-
#defines_constants?(class_body) ⇒ Boolean
private
Constants, including nested classes and modules, are scoped to the class when they are defined in a class body, but leak into the enclosing namespace once moved into a
Data.defineblock, so such a class cannot be rewritten as an assignment. - #end_line_kept?(class_node) ⇒ Boolean private
- #left_operand?(class_node) ⇒ Boolean private
- #parenthesize_arguments(corrector, data_define) private
- #parenthesize_left_operand(corrector, class_node, keyword_range) private
- #range_for_empty_class_body(data_define) private
- #statement?(class_node) ⇒ Boolean private
- #statement_child(node) private
- #unparenthesized_data_define?(data_define) ⇒ Boolean private
::RuboCop::Cop::RangeHelp - Included
| #add_range, | |
| #arguments_range | A range containing the first to the last argument of a method call or method definition. |
| #column_offset_between, | |
| #contents_range | A range containing only the contents of a literal with delimiters (e.g. |
| #directions, | |
| #effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
| #final_pos | rubocop:disable-next Metrics/ParameterLists. |
| #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, | |
| #range_for_statement_removal | Returns the range to remove a statement: its whole lines, including a trailing comment and the final newline, when no other code shares them, and otherwise the range itself with the spaces after it. |
| #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range | |
::RuboCop::Cop::Base - Inherited
| #add_global_offense | Adds an offense that has no particular location. |
| #add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
| #begin_investigation | Called before any investigation. |
| #callbacks_needed | — grouped under the Commissioner heading above. |
| #cop_config | Configuration Helpers. |
| #cop_name, #excluded_file?, | |
| #external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
| #inspect, | |
| #message | Gets called if no message is specified when calling |
| #name | Alias for Base#cop_name. |
| #offenses, | |
| #on_investigation_end | Called after all on_… |
| #on_new_investigation | Called before all on_… |
| #on_other_file | Called instead of all on_… |
| #parse | There should be very limited reasons for a Cop to do it’s own parsing The source is parsed the way the file under investigation was. |
| #parser_engine, | |
| #ready | Called between investigations. |
| #relevant_file?, | |
| #target_gem_version | Returns the version of a gem in the target, by default its locked version (i.e. from Gemfile.lock or gems.locked). |
| #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
| #callback_argument | Reserved for Cop::Cop. |
| #complete_investigation | Called to complete an investigation. |
| #correct, #covering_disabled_range, #current_corrector, | |
| #current_offense_locations | Reserved for Commissioner: |
| #current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, | |
| #enabled_lines? | A multi-line offense is suppressed by a directive on any line of its range, not only its first line, matching the intuition that the directive is attached to the offending code. |
| #file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #new_offense, #range_for_original, #range_from_node_or_range, | |
| #reset_investigation | Actually private methods. |
| #suppression_reason | The |
| #use_corrector | |
::RuboCop::Cop::AutocorrectLogic - Included
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Class Method Details
.autocorrect_incompatible_with
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 50
def self.autocorrect_incompatible_with [Style::MethodCallWithArgsParentheses] end
Instance Method Details
#conditional_statement?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 138
def conditional_statement?(node) return true if node.type?(:while, :until) node.if_type? && !node.ternary? end
#correct_parent(parent, corrector) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 88
def correct_parent(parent, corrector) if parent.block_type? # Convert a brace block to `do`, so the class's own `end` closes it # once the closing delimiter is removed; otherwise a dangling `{` is # left behind, producing invalid Ruby. corrector.replace(parent.loc.begin, 'do') if parent.braces? corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false)) elsif (range = range_for_empty_class_body(parent)) corrector.remove(range) parenthesize_arguments(corrector, parent) unless statement?(parent.parent) else parenthesize_arguments(corrector, parent) corrector.insert_after(parent, ' do') end end
#data_define?(node)
[ GitHub ]# 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
#defines_constants?(class_body) ⇒ Boolean (private)
Constants, including nested classes and modules, are scoped to the class when they
are defined in a class body, but leak into the enclosing namespace once moved into a
Data.define block, so such a class cannot be rewritten as an assignment.
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 82
def defines_constants?(class_body) return false unless class_body class_body.each_node(:casgn, :class, :module).any? end
#end_line_kept?(class_node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 115
def end_line_kept?(class_node) return true if left_operand?(class_node) class_node.loc.end.source_line.strip != 'end' end
#left_operand?(class_node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 121
def left_operand?(class_node) parent = class_node.parent return false if parent.nil? return parent.receiver.equal?(class_node) if parent.call_type? return parent.condition.equal?(class_node) if parent.if_type? && parent.ternary? LEFT_OPERAND_TYPES.include?(parent.type) && parent.children.first.equal?(class_node) end
#on_class(node)
[ GitHub ]# 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
#parenthesize_arguments(corrector, data_define) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 152
def parenthesize_arguments(corrector, data_define) return unless unparenthesized_data_define?(data_define) arguments_begin = data_define.first_argument.source_range.begin corrector.replace(data_define.loc.selector.end.join(arguments_begin), '(') corrector.insert_after(data_define.last_argument, ')') end
#parenthesize_left_operand(corrector, class_node, keyword_range) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 164
def parenthesize_left_operand(corrector, class_node, keyword_range) return unless left_operand?(class_node) corrector.insert_before(keyword_range, '(') corrector.insert_after(class_node.source_range.end, ')') end
#range_for_empty_class_body(data_define) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 104
def range_for_empty_class_body(data_define) class_node = data_define.parent return if class_node.body range = range_between(data_define.source_range.end_pos, class_node.source_range.end_pos) return range if /\A[\s;]*end\z/.match?(range.source) return if end_line_kept?(class_node) range_by_whole_lines(class_node.loc.end, include_final_newline: true) end
#statement?(class_node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 130
def statement?(class_node) parent = class_node.parent return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure) return !parent.condition.equal?(class_node) if conditional_statement?(parent) statement_child(parent).equal?(class_node) end
#statement_child(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/data_inheritance.rb', line 144
def statement_child(node) return node.expression if node.type?(*ASSIGNMENT_TYPES) return node.else_branch if node.type?(:case, :case_match) return if node.any_def_type? && node.endless? node.body if node.type?(*BODY_TYPES) end
#unparenthesized_data_define?(data_define) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/data_inheritance.rb', line 160
def unparenthesized_data_define?(data_define) data_define.arguments.any? && !data_define.parenthesized? end