Class: Rubocop::Cop::SpaceAfterControlKeyword

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

Constant Summary collapse

ERROR_MESSAGE =
'Use space after control keywords.'
KEYWORDS =
%w(if elsif case when while until unless)

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

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

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/space_after_control_keyword.rb', line 10

def inspect(file, source, tokens, sexp)
  # we need to keep track of the previous token to
  # avoid confusing symbols like :if with real keywords
  prev = Token.new(0, :init, '')

  tokens.each_cons(2) do |t1, t2|
    if prev.type != :on_symbeg && t1.type == :on_kw &&
        KEYWORDS.include?(t1.text) && t2.type != :on_sp
      add_offence(:convention,
                  t1.pos.lineno,
                  ERROR_MESSAGE)
    end

    prev = t1
  end
end