123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::StatementModifier

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Defined in: lib/rubocop/cop/mixin/statement_modifier.rb

Overview

Common functionality for modifier cops.

Constant Summary

Alignment - Included

SPACE

RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Instance Attribute Summary

Instance Method Summary

RangeHelp - Included

#add_range, #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

LineLengthHelp - Included

Alignment - Included

Instance Method Details

#code_after(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 79

def code_after(node)
  end_element = node.loc.end
  code = end_element.source_line[end_element.last_column..]
  code unless code.empty?
end

#comment_disables_cop?(comment) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 103

def comment_disables_cop?(comment)
  regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})"
  Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment)
end

#first_line_comment(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 71

def first_line_comment(node)
  comment = processed_source.comments.find { |c| same_line?(c, node) }
  return unless comment

  comment_source = comment.source
  comment_source unless comment_disables_cop?(comment_source)
end

#if_body_source(if_body) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 58

def if_body_source(if_body)
  if if_body.call_type? &&
     if_body.last_argument&.hash_type? && if_body.last_argument.pairs.last&.value_omission?
    "#{method_source(if_body)}(#{if_body.arguments.map(&:source).join(', ')})"
  else
    if_body.source
  end
end

#length_in_modifier_form(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 44

def length_in_modifier_form(node)
  keyword_element = node.loc.keyword
  code_before = keyword_element.source_line[0...keyword_element.column]
  expression = to_modifier_form(node)
  line_length("#{code_before}#{expression}#{code_after(node)}")
end

#max_line_length (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 97

def max_line_length
  return unless config.for_cop('Layout/LineLength')['Enabled']

  config.for_cop('Layout/LineLength')['Max']
end

#method_source(if_body) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 67

def method_source(if_body)
  range_between(if_body.source_range.begin_pos, if_body.loc.selector.end_pos).source
end

#modifier_fits_on_single_line?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 38

def modifier_fits_on_single_line?(node)
  return true unless max_line_length

  length_in_modifier_form(node) <= max_line_length
end

#non_eligible_body?(body) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 27

def non_eligible_body?(body)
  body.nil? ||
    body.empty_source? ||
    body.begin_type? ||
    processed_source.contains_comment?(body.source_range)
end

#non_eligible_condition?(condition) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 34

def non_eligible_condition?(condition)
  condition.each_node.any?(&:lvasgn_type?)
end

#non_eligible_node?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 20

def non_eligible_node?(node)
  node.modifier_form? ||
    node.nonempty_line_count > 3 ||
    processed_source.line_with_comment?(node.loc.last_line) ||
    (first_line_comment(node) && code_after(node))
end

#parenthesize?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 85

def parenthesize?(node)
  # Parenthesize corrected expression if changing to modifier-if form
  # would change the meaning of the parent expression
  # (due to the low operator precedence of modifier-if)
  parent = node.parent
  return false if parent.nil?
  return true if parent.assignment? || parent.operator_keyword?
  return true if %i[array pair].include?(parent.type)

  node.parent.send_type?
end

#single_line_as_modifier?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 12

def single_line_as_modifier?(node)
  return false if non_eligible_node?(node) ||
                  non_eligible_body?(node.body) ||
                  non_eligible_condition?(node.condition)

  modifier_fits_on_single_line?(node)
end

#to_modifier_form(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/statement_modifier.rb', line 51

def to_modifier_form(node)
  body = if_body_source(node.body)
  expression = [body, node.keyword, node.condition.source].compact.join(' ')
  parenthesized = parenthesize?(node) ? "(#{expression})" : expression
  [parenthesized, first_line_comment(node)].compact.join(' ')
end