Sha256: 6fc08c02cd069ce973673e7feda96b6c049c11e1f64d8cfafe404cb93c8e02af

Contents?: true

Size: 1.24 KB

Versions: 54

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # 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 Layout/SpaceAroundOperators.
      #
      # @example
      #   # bad
      #   def f(a:, b:2); {a:3}; end
      #
      #   # good
      #   def f(a:, b: 2); {a: 3}; end
      class SpaceAfterColon < Cop
        MSG = 'Space missing after colon.'

        def on_pair(node)
          return unless node.colon?

          colon = node.loc.operator

          add_offense(colon, location: 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, location: colon) unless followed_by_space?(colon)
        end

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

        private

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

Version data entries

54 entries across 35 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_after_colon.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/layout/space_after_colon.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.85.1 lib/rubocop/cop/layout/space_after_colon.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.85.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.84.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.83.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.82.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.81.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.80.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.80.0 lib/rubocop/cop/layout/space_after_colon.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_after_colon.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.79.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.78.0 lib/rubocop/cop/layout/space_after_colon.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/layout/space_after_colon.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_after_colon.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/layout/space_after_colon.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/layout/space_after_colon.rb