Sha256: 2187d5525fcca53a3dab429a9b2627cbd74052d68fce0e2183b72fb65e31a2f6

Contents?: true

Size: 764 Bytes

Versions: 6

Compression:

Stored size: 764 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]
          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

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.4.2 lib/rubocop/cop/when_then.rb
rubocop-0.4.1 lib/rubocop/cop/when_then.rb
rubocop-0.4.0 lib/rubocop/cop/when_then.rb
rubocop-0.3.2 lib/rubocop/cop/when_then.rb
rubocop-0.3.1 lib/rubocop/cop/when_then.rb
rubocop-0.3.0 lib/rubocop/cop/when_then.rb