class RuboCop::Cop::Layout::SpaceAfterColon

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

Constants

MSG

Public Instance Methods

on_kwoptarg(node) click to toggle source
# File lib/rubocop/cop/layout/space_after_colon.rb, line 29
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
on_pair(node) click to toggle source
# File lib/rubocop/cop/layout/space_after_colon.rb, line 21
def on_pair(node)
  return if !node.colon? || node.value_omission?

  colon = node.loc.operator

  register_offense(colon) unless followed_by_space?(colon)
end

Private Instance Methods

followed_by_space?(colon) click to toggle source
# File lib/rubocop/cop/layout/space_after_colon.rb, line 43
def followed_by_space?(colon)
  /\s/.match?(colon.source_buffer.source[colon.end_pos])
end
register_offense(colon) click to toggle source
# File lib/rubocop/cop/layout/space_after_colon.rb, line 39
def register_offense(colon)
  add_offense(colon) { |corrector| corrector.insert_after(colon, ' ') }
end