module RuboCop::Cop::SpaceAfterPunctuation

Common functionality for cops checking for missing space after punctuation.

Constants

MSG

Public Instance Methods

allowed_type?(token) click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 36
def allowed_type?(token)
  %i[tRPAREN tRBRACK tPIPE].include?(token.type)
end
autocorrect(token) click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 51
def autocorrect(token)
  ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') }
end
each_missing_space(tokens) { |t1| ... } click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 17
def each_missing_space(tokens)
  tokens.each_cons(2) do |t1, t2|
    next unless kind(t1)
    next unless space_missing?(t1, t2)
    next unless space_required_before?(t2)

    yield t1
  end
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 10
def investigate(processed_source)
  each_missing_space(processed_source.tokens) do |token|
    add_offense(token, location: token.pos,
                       message: format(MSG, kind(token)))
  end
end
offset() click to toggle source

The normal offset, i.e., the distance from the punctuation token where a space should be, is 1.

# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 47
def offset
  1
end
space_forbidden_before_rcurly?() click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 40
def space_forbidden_before_rcurly?
  style = space_style_before_rcurly
  style == 'no_space'
end
space_missing?(t1, t2) click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 27
def space_missing?(t1, t2)
  t1.pos.line == t2.pos.line && t2.pos.column == t1.pos.column + offset
end
space_required_before?(token) click to toggle source
# File lib/rubocop/cop/mixin/space_after_punctuation.rb, line 31
def space_required_before?(token)
  !(allowed_type?(token) ||
    (token.type == :tRCURLY && space_forbidden_before_rcurly?))
end