Sha256: 67159577f4387c3b4c8e68355dc1b5a9f846fe2adc1e6690f795d94bc240e48d

Contents?: true

Size: 996 Bytes

Versions: 15

Compression:

Stored size: 996 Bytes

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # Here we check if modifier keywords are preceded by a space.
      class SpaceBeforeModifierKeyword < Cop
        MSG = 'Put a space before the modifier keyword.'

        def on_if(node)
          return unless modifier?(node)

          kw = node.loc.keyword
          b = kw.begin_pos
          left_of_kw = Parser::Source::Range.new(kw.source_buffer, b - 1, b)
          add_offense(node, left_of_kw) unless left_of_kw.is?(' ')
        end
        alias_method :on_while, :on_if
        alias_method :on_until, :on_if

        private

        def modifier?(node)
          node.loc.respond_to?(:end) && node.loc.end.nil? && !elsif?(node)
        end

        def elsif?(node)
          node.loc.keyword.is?('elsif')
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            corrector.insert_before(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_before_modifier_keyword.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.30.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.30.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.29.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.29.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.28.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.27.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.27.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.26.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.26.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.25.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.24.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.24.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.23.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb