class RuboCop::Cop::Layout::MultilineAssignmentLayout

This cop checks whether the multiline assignments have a newline after the assignment operator.

@example EnforcedStyle: new_line (default)

# bad
foo = if expression
  'bar'
end

# good
foo =
  if expression
    'bar'
  end

# good
foo =
  begin
    compute
  rescue => e
    nil
  end

@example EnforcedStyle: same_line

# good
foo = if expression
  'bar'
end

Constants

NEW_LINE_OFFENSE
SAME_LINE_OFFENSE

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 75
def autocorrect(node)
  case style
  when :new_line
    ->(corrector) { corrector.insert_after(node.loc.operator, "\n") }
  when :same_line
    range = range_between(node.loc.operator.end_pos,
                          extract_rhs(node).source_range.begin_pos)

    ->(corrector) { corrector.replace(range, ' ') }
  end
end
check_assignment(node, rhs) click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 45
def check_assignment(node, rhs)
  return if node.send_type?
  return unless rhs
  return unless supported_types.include?(rhs.type)
  return if rhs.first_line == rhs.last_line

  check_by_enforced_style(node, rhs)
end
check_by_enforced_style(node, rhs) click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 54
def check_by_enforced_style(node, rhs)
  case style
  when :new_line
    check_new_line_offense(node, rhs)
  when :same_line
    check_same_line_offense(node, rhs)
  end
end
check_new_line_offense(node, rhs) click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 63
def check_new_line_offense(node, rhs)
  return unless node.loc.operator.line == rhs.first_line

  add_offense(node, message: NEW_LINE_OFFENSE)
end
check_same_line_offense(node, rhs) click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 69
def check_same_line_offense(node, rhs)
  return unless node.loc.operator.line != rhs.first_line

  add_offense(node, message: SAME_LINE_OFFENSE)
end

Private Instance Methods

supported_types() click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 89
def supported_types
  @supported_types ||= cop_config['SupportedTypes'].map(&:to_sym)
end