Sha256: 32bbe783dbfae5484d0aa8de3ae2e6486e5320818ce4587370f5bbb3a589ba7e

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 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)
          /\s/.match?(colon.source_buffer.source[colon.end_pos])
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.89.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.88.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.87.1 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.87.0 lib/rubocop/cop/layout/space_after_colon.rb
rubocop-0.86.0 lib/rubocop/cop/layout/space_after_colon.rb