Class: Rubocop::Cop::MultilineBlocks

Inherits:
Cop
  • Object
show all
Includes:
Blocks
Defined in:
lib/rubocop/cop/blocks.rb

Constant Summary collapse

ERROR_MESSAGE =
'Avoid using {...} for multi-line blocks.'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #offences

Instance Method Summary collapse

Methods included from Blocks

#inspect

Methods inherited from Cop

#add_offence, #has_report?, inherited, #initialize

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check(tokens, ix) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/blocks.rb', line 25

def check(tokens, ix)
  t = tokens[ix]
  if [t.type, t.text] == [:on_lbrace, '{']
    path = @correlations[ix] or return
    if path.last == :brace_block
      rbrace_ix = @reverse_correlations[path.object_id] - [ix]
      if rbrace_ix.empty?
        fail "\n#@file:#{t.pos.lineno}:#{t.pos.column}: " +
          'Matching brace not found'
      end
      if tokens[*rbrace_ix].pos.lineno > t.pos.lineno
        add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
      end
    end
  end
end