Module: RuboCop::Cop::SpaceAfterPunctuation
- Included in:
- Layout::SpaceAfterComma, Layout::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
- #allowed_type?(token) ⇒ Boolean
- #autocorrect(token) ⇒ Object
- #each_missing_space(tokens) ⇒ Object
- #investigate(processed_source) ⇒ Object
-
#offset ⇒ Object
The normal offset, i.e., the distance from the punctuation token where a space should be, is 1.
- #space_forbidden_before_rcurly? ⇒ Boolean
- #space_missing?(t1, t2) ⇒ Boolean
- #space_required_before?(token) ⇒ Boolean
Instance Method Details
#allowed_type?(token) ⇒ Boolean
35 36 37 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 35 def allowed_type?(token) %i[tRPAREN tRBRACK tPIPE].include?(token.type) end |
#autocorrect(token) ⇒ Object
50 51 52 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 50 def autocorrect(token) ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') } end |
#each_missing_space(tokens) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 16 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) ⇒ Object
10 11 12 13 14 |
# 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, token.pos, format(MSG, kind(token))) end end |
#offset ⇒ Object
The normal offset, i.e., the distance from the punctuation token where a space should be, is 1.
46 47 48 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 46 def offset 1 end |
#space_forbidden_before_rcurly? ⇒ Boolean
39 40 41 42 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 39 def space_forbidden_before_rcurly? style = space_style_before_rcurly style == 'no_space' end |
#space_missing?(t1, t2) ⇒ Boolean
26 27 28 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 26 def space_missing?(t1, t2) t1.pos.line == t2.pos.line && t2.pos.column == t1.pos.column + offset end |
#space_required_before?(token) ⇒ Boolean
30 31 32 33 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 30 def space_required_before?(token) !(allowed_type?(token) || (token.type == :tRCURLY && space_forbidden_before_rcurly?)) end |