Class: Rubocop::Cop::SingleLineBlocks

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

Constant Summary collapse

ERROR_MESSAGE =
'Prefer {...} over do...end for single-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



47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/blocks.rb', line 47

def check(tokens, ix)
  t = tokens[ix]
  if [t.type, t.text] == [:on_kw, 'do']
    end_offset = tokens[ix..-1].index { |t2| t2.text == 'end' } or return
    end_token_ix = ix + end_offset
    if tokens[end_token_ix].pos.lineno == t.pos.lineno
      add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
    end
  end
end