Class: Rubocop::Cop::Lint::EndAlignment

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/lint/end_alignment.rb

Overview

This cop checks whether the end keywords are aligned properly.

For keywords (if, def, etc.) the end is aligned with the start of the keyword. For blocks - with the start of the expression where the block is defined.

Examples:


variable = if true
           end

variable = lambda do |i|
  i
end

Constant Summary collapse

MSG =
'end at %d, %d is not aligned with %s at %d, %d'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, lint?, #name, rails?, style?

Constructor Details

#initializeEndAlignment

Returns a new instance of EndAlignment.



24
25
26
27
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 24

def initialize
  super
  @inspected_blocks = []
end

Instance Method Details

#inspect(source_buffer, source, tokens, ast, comments) ⇒ Object



29
30
31
32
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 29

def inspect(source_buffer, source, tokens, ast, comments)
  @inspected_blocks = []
  super
end

#on_and(node) ⇒ Object Also known as: on_or



77
78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 77

def on_and(node)
  return if already_processed_node?(node)

  _left, right = *node
  if right.type == :block
    check_block_alignment(node.loc.expression, right.loc)
    @inspected_blocks << right
  end
  super
end

#on_block(node) ⇒ Object

Block related alignments



71
72
73
74
75
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 71

def on_block(node)
  return if already_processed_node?(node)
  check_block_alignment(node.loc.expression, node.loc)
  super
end

#on_casgn(node) ⇒ Object



102
103
104
105
106
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 102

def on_casgn(node)
  _, _, children = *node
  process_block_assignment(node, children)
  super
end

#on_class(node) ⇒ Object



44
45
46
47
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 44

def on_class(node)
  check(node)
  super
end

#on_def(node) ⇒ Object



34
35
36
37
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 34

def on_def(node)
  check(node)
  super
end

#on_defs(node) ⇒ Object



39
40
41
42
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 39

def on_defs(node)
  check(node)
  super
end

#on_if(node) ⇒ Object



54
55
56
57
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 54

def on_if(node)
  check(node) if node.loc.respond_to?(:end)
  super
end

#on_lvasgn(node) ⇒ Object Also known as: on_ivasgn, on_cvasgn, on_gvasgn, on_and_asgn, on_or_asgn



90
91
92
93
94
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 90

def on_lvasgn(node)
  _, children = *node
  process_block_assignment(node, children)
  super
end

#on_masgn(node) ⇒ Object



120
121
122
123
124
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 120

def on_masgn(node)
  variables, args = *node
  process_block_assignment(variables, args)
  super
end

#on_module(node) ⇒ Object



49
50
51
52
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 49

def on_module(node)
  check(node)
  super
end

#on_op_asgn(node) ⇒ Object



108
109
110
111
112
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 108

def on_op_asgn(node)
  variable, _op, args = *node
  process_block_assignment(variable, args)
  super
end

#on_send(node) ⇒ Object



114
115
116
117
118
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 114

def on_send(node)
  _receiver, _method, *args = *node
  process_block_assignment(node, args.last)
  super
end

#on_until(node) ⇒ Object



64
65
66
67
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 64

def on_until(node)
  check(node)
  super
end

#on_while(node) ⇒ Object



59
60
61
62
# File 'lib/rubocop/cop/lint/end_alignment.rb', line 59

def on_while(node)
  check(node)
  super
end