Sha256: 2ef3d95ff8f2643111896a6fc63e2f4b474560446bca005f4527df9dd0ee823f

Contents?: true

Size: 999 Bytes

Versions: 15

Compression:

Stored size: 999 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
          return unless exp.source[kw_offset..-1].start_with?(kw.source + '(')

          add_offense(node, kw)
        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

15 entries across 15 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/space_after_control_keyword.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.30.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.30.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.29.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.29.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.28.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.27.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.27.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.26.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.26.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.25.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.24.1 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.24.0 lib/rubocop/cop/style/space_after_control_keyword.rb
rubocop-0.23.0 lib/rubocop/cop/style/space_after_control_keyword.rb