Module: Rubocop::Cop::IfThenElse

Included in:
IfWithSemicolon, MultilineIfThen, OneLineConditional
Defined in:
lib/rubocop/cop/if_then_else.rb

Instance Method Summary collapse

Instance Method Details

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



6
7
8
9
10
11
12
13
14
# File 'lib/rubocop/cop/if_then_else.rb', line 6

def inspect(file, source, tokens, sexp)
  tokens.each_with_index do |t, ix|
    if t.type == :on_kw && %w(if unless).include?(t.text)
      if kind_of_if(tokens, ix + 1) == self.class
        add_offence(:convention, t.pos.lineno, error_message)
      end
    end
  end
end

#kind_of_if(tokens, ix) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/if_then_else.rb', line 16

def kind_of_if(tokens, ix)
  then_found = false
  tokens[ix..-1].each do |t|
    case t.type
    when :on_kw
      case t.text
      when 'then' then then_found = true
      when 'end'  then return OneLineConditional
      end
    when :on_ignored_nl, :on_nl
      break
    when :on_semicolon
      return IfWithSemicolon
    when :on_comment
      break if t.text =~ /\n/
    when :on_sp
      nil
    else
      then_found = false
    end
  end
  then_found ? MultilineIfThen : nil
end