Sha256: c2b48afaa16ce592b400ad35e6463bd28d95245dc399efeef938f469ca43d0b9

Contents?: true

Size: 950 Bytes

Versions: 9

Compression:

Stored size: 950 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)
          ->(corrector) { corrector.insert_before(node.loc.keyword, ' ') }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.35.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.34.2 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.34.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.34.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.33.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.32.1 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.32.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb
rubocop-0.31.0 lib/rubocop/cop/style/space_before_modifier_keyword.rb