class RuboCop::Cop::Layout::MultilineAssignmentLayout
This cop checks whether the multiline assignments have a newline after the assignment operator.
@example
# bad (with EnforcedStyle set to new_line) foo = if expression 'bar' end # good (with EnforcedStyle set to same_line) foo = if expression 'bar' end # good (with EnforcedStyle set to new_line) foo = if expression 'bar' end # good (with EnforcedStyle set to new_line) foo = begin compute rescue => e nil 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 67 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 42 def check_assignment(node, rhs) return unless rhs return unless supported_types.include?(rhs.type) return if rhs.loc.first_line == rhs.loc.last_line 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 55 def check_new_line_offense(node, rhs) return unless node.loc.operator.line == rhs.loc.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 61 def check_same_line_offense(node, rhs) return unless node.loc.operator.line != rhs.loc.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 81 def supported_types @supported_types ||= cop_config['SupportedTypes'].map(&:to_sym) end