Sha256: de1ab0ca623c7db0c4b2a1f138cd26ba475ee2ba06bbb52e2f680799cb3b6f18

Contents?: true

Size: 839 Bytes

Versions: 10

Compression:

Stored size: 839 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class WhenThen < Cop
      ERROR_MESSAGE = 'Never use "when x;". Use "when x then" instead.'

      def inspect(file, source, tokens, sexp)
        each(:when, sexp) do |s|
          # The grammar is:
          # when <value> <divider> <body>
          # where divider is either semicolon, then, or line break.
          last_pos_in_value = all_positions(s[1])[-1]

          next unless last_pos_in_value # Give up if no positions found.

          start_index = tokens.index { |t| t.pos == last_pos_in_value }
          tokens[start_index..-1].each do |t|
            break if ['then', "\n"].include?(t.text)
            if t.type == :on_semicolon
              add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-0.7.2 lib/rubocop/cop/when_then.rb
rubocop-0.7.1 lib/rubocop/cop/when_then.rb
rubocop-0.7.0 lib/rubocop/cop/when_then.rb
rubocop-0.6.1 lib/rubocop/cop/when_then.rb
rubocop-0.6.0 lib/rubocop/cop/when_then.rb
rubocop-0.5.0 lib/rubocop/cop/when_then.rb
rubocop-0.4.6 lib/rubocop/cop/when_then.rb
rubocop-0.4.5 lib/rubocop/cop/when_then.rb
rubocop-0.4.4 lib/rubocop/cop/when_then.rb
rubocop-0.4.3 lib/rubocop/cop/when_then.rb