Class: RuboCop::Cop::Style::SoleNestedConditional
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::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/sole_nested_conditional.rb |
Overview
If the branch of a conditional consists solely of a conditional node, its conditions can be combined with the conditions of the outer branch. This helps to keep the nesting level from getting too deep.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 53'Consider merging nested conditions into outer `%<conditional_type>s` conditions.'
::RuboCop::Cop::Base
- Inherited
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::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 |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
- #allow_modifier? ⇒ Boolean readonly private
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #on_if(node)
- #add_parentheses?(node) ⇒ Boolean private
- #add_parentheses_if_needed(condition) private
- #assigned_variables(condition) private
- #autocorrect(corrector, node, if_branch) private
- #autocorrect_outer_condition_basic(corrector, node, if_branch) private
- #autocorrect_outer_condition_modify_form(corrector, node, if_branch) private
- #chainable_condition(node) private
- #correct_for_basic_condition_style(corrector, node, if_branch) private
- #correct_for_comment(corrector, node, if_branch) private
- #correct_for_guard_condition_style(corrector, node, if_branch) private
- #correct_node(corrector, node) private
- #offending_branch?(node, branch) ⇒ Boolean private
- #parenthesize_method?(node) ⇒ Boolean private
- #parenthesized_method_arguments(node) private
- #use_variable_assignment_in_condition?(condition, if_branch) ⇒ 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, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #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, | |
#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. |
#parser_engine, | |
#ready | Called between investigations. |
#relevant_file?, | |
#target_gem_version | Returns a gems locked versions (i.e. |
#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, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #heredoc_range, #max_line_length, #multiline_ranges, #multiline_string?, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation?, #surrounding_heredoc?, #surrounding_percent_array? |
::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/sole_nested_conditional.rb', line 55
def self.autocorrect_incompatible_with [Style::NegatedIf, Style::NegatedUnless] end
Instance Attribute Details
#allow_modifier? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 199
def allow_modifier? cop_config['AllowModifier'] end
Instance Method Details
#add_parentheses?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 187
def add_parentheses?(node) node.assignment? || (node.operator_keyword? && !node.and_type?) || (node.call_type? && node.arguments.any? && !node.parenthesized?) end
#add_parentheses_if_needed(condition) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 168
def add_parentheses_if_needed(condition) # Handle `send` and `block` nodes that need to be wrapped in parens # FIXME: autocorrection prevents syntax errors by wrapping the entire node in parens, # but wrapping the argument list would be a more ergonomic correction. node_to_check = condition&.any_block_type? ? condition.send_node : condition return condition.source unless add_parentheses?(node_to_check) if parenthesize_method?(condition) parenthesized_method_arguments(condition) else "(#{condition.source})" end end
#assigned_variables(condition) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 81
def assigned_variables(condition) assigned_variables = condition.assignment? ? [condition.children.first.to_s] : [] assigned_variables + condition.descendants.select(&:assignment?).map do |node| node.children.first.to_s end end
#autocorrect(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 98
def autocorrect(corrector, node, if_branch) if node.modifier_form? autocorrect_outer_condition_modify_form(corrector, node, if_branch) else autocorrect_outer_condition_basic(corrector, node, if_branch) end end
#autocorrect_outer_condition_basic(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 106
def autocorrect_outer_condition_basic(corrector, node, if_branch) correct_node(corrector, node) if if_branch.modifier_form? correct_for_guard_condition_style(corrector, node, if_branch) else correct_for_basic_condition_style(corrector, node, if_branch) correct_for_comment(corrector, node, if_branch) end end
#autocorrect_outer_condition_modify_form(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 142
def autocorrect_outer_condition_modify_form(corrector, node, if_branch) correct_node(corrector, if_branch) corrector.insert_before(if_branch.condition, "#{chainable_condition(node)} && ") range = range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos) corrector.remove(range_with_surrounding_space(range, newlines: false)) end
#chainable_condition(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 160
def chainable_condition(node) wrapped_condition = add_parentheses_if_needed(node.condition) return wrapped_condition if node.if? node.condition.and_type? ? "!(#{wrapped_condition})" : "!#{wrapped_condition}" end
#correct_for_basic_condition_style(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 131
def correct_for_basic_condition_style(corrector, node, if_branch) range = range_between( node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos ) corrector.replace(range, ' && ') corrector.replace(if_branch.condition, chainable_condition(if_branch)) corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true)) end
#correct_for_comment(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 151
def correct_for_comment(corrector, node, if_branch) comments = processed_source.ast_with_comments[if_branch].select do |comment| comment.loc.line < if_branch.condition.first_line end comment_text = comments.map(&:text).join("\n") << "\n" corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty? end
#correct_for_guard_condition_style(corrector, node, if_branch) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 122
def correct_for_guard_condition_style(corrector, node, if_branch) corrector.insert_after(node.condition, " && #{chainable_condition(if_branch)}") range = range_between( if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos ) corrector.remove(range_with_surrounding_space(range, newlines: false)) end
#correct_node(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 117
def correct_node(corrector, node) corrector.replace(node.loc.keyword, 'if') if node.unless? corrector.replace(node.condition, chainable_condition(node)) end
#offending_branch?(node, branch) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 89
def offending_branch?(node, branch) return false unless branch branch.if_type? && !branch.else? && !branch.ternary? && !((node.modifier_form? || branch.modifier_form?) && allow_modifier?) end
#on_if(node)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 59
def on_if(node) return if node.ternary? || node.else? || node.elsif? if_branch = node.if_branch return if use_variable_assignment_in_condition?(node.condition, if_branch) return unless offending_branch?(node, if_branch) = format(MSG, conditional_type: node.keyword) add_offense(if_branch.loc.keyword, message: ) do |corrector| autocorrect(corrector, node, if_branch) end end
#parenthesize_method?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 182
def parenthesize_method?(node) node.call_type? && node.arguments.any? && !node.parenthesized? && !node.comparison_method? && !node.operator_method? end
#parenthesized_method_arguments(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 192
def parenthesized_method_arguments(node) method_call = node.source_range.begin.join(node.loc.selector.end).source arguments = node.first_argument.source_range.begin.join(node.source_range.end).source "#{method_call}(#{arguments})" end
#use_variable_assignment_in_condition?(condition, if_branch) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/style/sole_nested_conditional.rb', line 74
def use_variable_assignment_in_condition?(condition, if_branch) assigned_variables = assigned_variables(condition) assigned_variables && if_branch&.if_type? && assigned_variables.include?(if_branch.condition.source) end