Sha256: f5d1060f45119d74193bf8c3033e519e2bf4862900f34f60ec308a8ee9652c2a

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for colon (:) not followed by some kind of space.
      # N.B. this cop does not handle spaces after a ternary operator, which are
      # instead handled by Style/SpaceAroundOperators.
      class SpaceAfterColon < Cop
        include IfNode

        MSG = 'Space missing after colon.'.freeze

        def on_pair(node)
          colon = node.loc.operator

          return unless colon.is?(':')

          add_offense(colon, colon) unless followed_by_space?(colon)
        end

        def on_kwoptarg(node)
          # We have no direct reference to the colon source range following an
          # optional keyword argument's name, so must construct one.
          colon = node.loc.name.end.resize(1)

          add_offense(colon, colon) unless followed_by_space?(colon)
        end

        private

        def followed_by_space?(colon)
          colon.source_buffer.source[colon.end_pos] =~ /\s/
        end

        def autocorrect(range)
          ->(corrector) { corrector.insert_after(range, ' ') }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_after_colon.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.43.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.42.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.41.2 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.41.1 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.41.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.40.0 lib/rubocop/cop/style/space_after_colon.rb