Sha256: 6e4ffaa4b934b3fcd3ff17b45ec2427926f62b828a92ff59910a4fde5209b9e9

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

# 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

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.46.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.45.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.44.1 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.44.0 lib/rubocop/cop/style/space_after_colon.rb