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 %<token>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
40 41 42 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 40 def allowed_type?(token) %i[tRPAREN tRBRACK tPIPE].include?(token.type) end |
#autocorrect(token) ⇒ Object
17 18 19 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 17 def autocorrect(token) ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') } end |
#each_missing_space(tokens) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 21 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 15 |
# 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, location: token.pos, message: format(MSG, token: kind(token))) end end |
#offset ⇒ Object
The normal offset, i.e., the distance from the punctuation token where a space should be, is 1.
51 52 53 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 51 def offset 1 end |
#space_forbidden_before_rcurly? ⇒ Boolean
44 45 46 47 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 44 def space_forbidden_before_rcurly? style = space_style_before_rcurly style == 'no_space' end |
#space_missing?(t1, t2) ⇒ Boolean
31 32 33 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 31 def space_missing?(t1, t2) t1.line == t2.line && t2.column == t1.column + offset end |
#space_required_before?(token) ⇒ Boolean
35 36 37 38 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 35 def space_required_before?(token) !(allowed_type?(token) || (token.right_curly_brace? && space_forbidden_before_rcurly?)) end |