class RuboCop::Cop::Layout::MultilineAssignmentLayout
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
@example SupportedTypes: ['block', 'case', 'class', 'if', 'kwbegin', 'module'] (default)
# good foo = if expression 'bar' end # good foo = [1].map do |i| i + 1 end
@example SupportedTypes: ['block']
# good foo = if expression 'bar' end # good foo = [1].map do |i| 'bar' * i end
Constants
- NEW_LINE_OFFENSE
- SAME_LINE_OFFENSE
Public Instance Methods
check_assignment(node, rhs)
click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 72 def check_assignment(node, rhs) return if node.send_type? && node.loc.operator&.source != '=' return unless rhs return unless supported_types.include?(rhs.type) return if rhs.single_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 81 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 90 def check_new_line_offense(node, rhs) return unless same_line?(node.loc.operator, rhs) add_offense(node, message: NEW_LINE_OFFENSE) do |corrector| corrector.insert_after(node.loc.operator, "\n") end end
check_same_line_offense(node, rhs)
click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 98 def check_same_line_offense(node, rhs) return unless node.loc.operator.line != rhs.first_line add_offense(node, message: SAME_LINE_OFFENSE) do |corrector| range = range_between( node.loc.operator.end_pos, extract_rhs(node).source_range.begin_pos ) corrector.replace(range, ' ') end end
Private Instance Methods
supported_types()
click to toggle source
# File lib/rubocop/cop/layout/multiline_assignment_layout.rb, line 111 def supported_types @supported_types ||= cop_config['SupportedTypes'].map(&:to_sym) end