Module: RuboCop::Cop::SpaceAfterPunctuation

Included in:
RuboCop::Cop::Style::SpaceAfterComma, RuboCop::Cop::Style::SpaceAfterSemicolon
Defined in:
lib/rubocop/cop/mixin/space_after_punctuation.rb

Overview

Common functionality for cops checking for missing space after punctuation.

Constant Summary collapse

MSG =
'Space missing after %s.'.freeze

Instance Method Summary collapse

Instance Method Details

#autocorrect(token) ⇒ Object



34
35
36
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 34

def autocorrect(token)
  ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') }
end

#investigate(processed_source) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 11

def investigate(processed_source)
  processed_source.tokens.each_cons(2) do |t1, t2|
    next unless kind(t1) && t1.pos.line == t2.pos.line &&
                t2.pos.column == t1.pos.column + offset &&
                ![:tRPAREN, :tRBRACK, :tPIPE].include?(t2.type) &&
                !(t2.type == :tRCURLY && space_forbidden_before_rcurly?)

    add_offense(t1, t1.pos, format(MSG, kind(t1)))
  end
end

#offsetObject

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



30
31
32
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 30

def offset
  1
end

#space_forbidden_before_rcurly?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 22

def space_forbidden_before_rcurly?
  cfg = config.for_cop('Style/SpaceInsideBlockBraces')
  style = cfg['EnforcedStyle'] || 'space'
  style == 'no_space'
end