Module: RuboCop::Cop::SpaceBeforePunctuation

Included in:
RuboCop::Cop::Style::SpaceBeforeComma, RuboCop::Cop::Style::SpaceBeforeSemicolon
Defined in:
lib/rubocop/cop/mixin/space_before_punctuation.rb

Overview

Common functionality for cops checking for space before punctuation.

Constant Summary collapse

MSG =
'Space found before %s.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(pos_before_punctuation) ⇒ Object


25
26
27
28
29
# File 'lib/rubocop/cop/mixin/space_before_punctuation.rb', line 25

def autocorrect(pos_before_punctuation)
  @corrections << lambda do |corrector|
    corrector.remove(pos_before_punctuation)
  end
end

#investigate(processed_source) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/mixin/space_before_punctuation.rb', line 10

def investigate(processed_source)
  processed_source.tokens.each_cons(2) do |t1, t2|
    next unless kind(t2) && t1.pos.line == t2.pos.line &&
                t2.pos.begin_pos > t1.pos.end_pos
    buffer = processed_source.buffer
    pos_before_punctuation = Parser::Source::Range.new(buffer,
                                                       t1.pos.end_pos,
                                                       t2.pos.begin_pos)

    add_offense(pos_before_punctuation,
                pos_before_punctuation,
                format(MSG, kind(t2)))
  end
end