Sha256: 83fc0a6b802aa216ab5b7cfdc95710007d173db8d8a6d89ccaedc9a8af22d277

Contents?: true

Size: 888 Bytes

Versions: 6

Compression:

Stored size: 888 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for colon (:) not followed by some kind of space.
      class SpaceAfterColon < Cop
        include IfNode

        MSG = 'Space missing after colon.'.freeze

        def on_pair(node)
          oper = node.loc.operator
          return unless oper.is?(':') && followed_by_space?(oper)

          add_offense(oper, oper)
        end

        def on_if(node)
          return unless ternary_op?(node)

          colon = node.loc.colon
          return unless followed_by_space?(colon)

          add_offense(colon, colon)
        end

        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

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.39.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.38.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.37.2 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.37.1 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.37.0 lib/rubocop/cop/style/space_after_colon.rb
rubocop-0.36.0 lib/rubocop/cop/style/space_after_colon.rb