123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::SpaceAfterPunctuation

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/space_after_punctuation.rb

Overview

Common functionality for cops checking for missing space after punctuation.

Constant Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#space_forbidden_before_rcurly?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 42

def space_forbidden_before_rcurly?
  style = space_style_before_rcurly
  style == 'no_space'
end

Instance Method Details

#allowed_type?(token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 38

def allowed_type?(token)
  %i[tRPAREN tRBRACK tPIPE tSTRING_DEND].include?(token.type)
end

#each_missing_space(tokens) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 20

def each_missing_space(tokens)
  tokens.each_cons(2) do |token1, token2|
    next unless kind(token1)
    next unless space_missing?(token1, token2)
    next unless space_required_before?(token2)

    yield token1
  end
end

#offset (private)

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

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 49

def offset
  1
end

#on_new_investigation

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 10

def on_new_investigation
  each_missing_space(processed_source.tokens) do |token|
    add_offense(token.pos, message: format(MSG, token: kind(token))) do |corrector|
      PunctuationCorrector.add_space(corrector, token)
    end
  end
end

#space_missing?(token1, token2) ⇒ Boolean (private)

[ GitHub ]

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

def space_missing?(token1, token2)
  same_line?(token1, token2) && token2.column == token1.column + offset
end

#space_required_before?(token) ⇒ Boolean (private)

[ GitHub ]

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

def space_required_before?(token)
  !(allowed_type?(token) || (token.right_curly_brace? && space_forbidden_before_rcurly?))
end