Sha256: 51bc6e31fa4982d88d237f44738356d4ed67ad0e87e18a5846efb77eeb1c34e1

Contents?: true

Size: 1.3 KB

Versions: 35

Compression:

Stored size: 1.3 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 unless node.colon?

          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) do |corrector|
            corrector.insert_after(colon, ' ')
          end
        end

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

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.12.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.12.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.11.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.10.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.9.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.9.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.8.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.8.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.7.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.6.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.6.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.5.2 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.5.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-1.5.0 lib/rubocop/cop/layout/space_after_colon.rb