class RuboCop::Cop::Layout::ElseAlignment
This cop checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def keyword, but there are special cases when they should follow the same rules as the alignment of end.
@example
# bad if something code else code end # bad if something code elsif something code end # good if something code else code end
Constants
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 62 def autocorrect(node) AlignmentCorrector.correct(processed_source, node, column_delta) end
on_case(node)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 56 def on_case(node) return unless node.else? check_alignment(node.when_branches.last.loc.keyword, node.loc.else) end
on_if(node, base = nil)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 39 def on_if(node, base = nil) return if ignored_node?(node) return unless node.else? && begins_its_line?(node.loc.else) check_alignment(base_range_of_if(node, base), node.loc.else) return unless node.elsif_conditional? check_nested(node.else_branch, base) end
on_rescue(node)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 50 def on_rescue(node) return unless node.loc.respond_to?(:else) && node.loc.else check_alignment(base_range_of_rescue(node), node.loc.else) end
Private Instance Methods
base_for_method_definition(node)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 93 def base_for_method_definition(node) parent = node.parent if parent&.send_type? parent.loc.selector # For example "private def ..." else node.loc.keyword end end
base_range_of_if(node, base)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 73 def base_range_of_if(node, base) if base base.source_range else lineage = [node, *node.each_ancestor(:if)] lineage.find { |parent| parent.if? || parent.unless? }.loc.keyword end end
base_range_of_rescue(node)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 82 def base_range_of_rescue(node) parent = node.parent parent = parent.parent if parent.ensure_type? case parent.type when :def, :defs then base_for_method_definition(parent) when :kwbegin then parent.loc.begin when :block then parent.send_node.source_range else node.loc.keyword end end
check_alignment(base_range, else_range)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 118 def check_alignment(base_range, else_range) return unless begins_its_line?(else_range) @column_delta = column_offset_between(base_range, else_range) return if @column_delta.zero? message = format( MSG, else_range: else_range.source, base_range: base_range.source[/^\S*/] ) add_offense(else_range, location: else_range, message: message) end
check_assignment(node, rhs)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 102 def check_assignment(node, rhs) # If there are method calls chained to the right hand side of the # assignment, we let rhs be the receiver of those method calls before # we check its indentation. rhs = first_part_of_call_chain(rhs) return unless rhs end_config = config.for_cop('Layout/EndAlignment') style = end_config['EnforcedStyleAlignWith'] || 'keyword' base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs return unless rhs.if_type? check_nested(rhs, base) end
check_nested(node, base)
click to toggle source
# File lib/rubocop/cop/layout/else_alignment.rb, line 68 def check_nested(node, base) on_if(node, base) ignore_node(node) end