Sha256: 894f1535bbee94f8e49b4f7fb85f9deca6fe32e7870c33123db5a1e67c18c75d

Contents?: true

Size: 1.29 KB

Versions: 207

Compression:

Stored size: 1.29 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 < Base
        extend AutoCorrector

        MSG = 'Space missing after colon.'

        def on_pair(node)
          return if !node.colon? || node.value_omission?

          colon = node.loc.operator

          register_offense(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)

          register_offense(colon) unless followed_by_space?(colon)
        end

        private

        def register_offense(colon)
          add_offense(colon) { |corrector| corrector.insert_after(colon, ' ') }
        end

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

Version data entries

207 entries across 198 versions & 20 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/layout/space_after_colon.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.69.2 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.69.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.69.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.68.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.67.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.66.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.66.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.65.1 lib/rubocop/cop/layout/space_after_colon.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.65.0 lib/rubocop/cop/layout/space_after_colon.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_after_colon.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_after_colon.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_after_colon.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.64.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.63.4 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.63.3 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.63.2 lib/rubocop/cop/layout/space_after_colon.rb