module RuboCop::Cop::SpaceBeforePunctuation

Common functionality for cops checking for space before punctuation.

Constants

MSG

Public Instance Methods

autocorrect(pos_before_punctuation) click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 43
def autocorrect(pos_before_punctuation)
  ->(corrector) { corrector.remove(pos_before_punctuation) }
end
each_missing_space(tokens) { |t2, pos_before_punctuation| ... } click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 16
def each_missing_space(tokens)
  tokens.each_cons(2) do |t1, t2|
    next unless kind(t2)
    next unless space_missing?(t1, t2)
    next if space_required_after?(t1)

    pos_before_punctuation = range_between(t1.pos.end_pos,
                                           t2.pos.begin_pos)

    yield t2, pos_before_punctuation
  end
end
investigate(processed_source) click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 9
def investigate(processed_source)
  each_missing_space(processed_source.tokens) do |token, pos_before|
    add_offense(pos_before, location: pos_before,
                            message: format(MSG, kind(token)))
  end
end
space_missing?(t1, t2) click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 29
def space_missing?(t1, t2)
  t1.pos.line == t2.pos.line && t2.pos.begin_pos > t1.pos.end_pos
end
space_required_after?(token) click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 33
def space_required_after?(token)
  token.type == :tLCURLY && space_required_after_lcurly?
end
space_required_after_lcurly?() click to toggle source
# File lib/rubocop/cop/mixin/space_before_punctuation.rb, line 37
def space_required_after_lcurly?
  cfg = config.for_cop('Layout/SpaceInsideBlockBraces')
  style = cfg['EnforcedStyle'] || 'space'
  style == 'space'
end