Sha256: 3a822d017d7dbaae44b84369e950915fc40931536e1d6d6630ab1a648098f2bb

Contents?: true

Size: 1003 Bytes

Versions: 5

Compression:

Stored size: 1003 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # Checks for various control keywords missing a space after them.
      class SpaceAfterControlKeyword < Cop
        MSG = 'Use space after control keywords.'
        # elsif and unless are handled by on_if.
        KEYWORDS = %w(if case when while until)

        def on_keyword(node)
          return if node.loc.is_a?(Parser::Source::Map::Ternary)

          exp = node.loc.expression
          kw = node.loc.keyword
          kw_offset = kw.begin_pos - exp.begin_pos
          if exp.source[kw_offset..-1].start_with?(kw.source + '(')
            add_offense(node, kw)
          end
        end

        KEYWORDS.each do |keyword|
          define_method(:"on_#{keyword}") do |node|
            on_keyword(node)
          end
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            corrector.insert_after(node.loc.keyword, ' ')
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.20.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.20.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.19.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.19.0 lib/rubocop/cop/style/space_after_control_keyword.rb