Module: RuboCop::Cop::SpaceAfterPunctuation
- 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
36 37 38 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 36 def allowed_type?(token) [:tRPAREN, :tRBRACK, :tPIPE].include?(token.type) end |
#autocorrect(token) ⇒ Object
51 52 53 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 51 def autocorrect(token) ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') } end |
#each_missing_space(tokens) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 17 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
11 12 13 14 15 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 11 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.
47 48 49 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 47 def offset 1 end |
#space_forbidden_before_rcurly? ⇒ Boolean
40 41 42 43 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 40 def space_forbidden_before_rcurly? style = space_style_before_rcurly style == 'no_space' end |
#space_missing?(t1, t2) ⇒ Boolean
27 28 29 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 27 def space_missing?(t1, t2) t1.pos.line == t2.pos.line && t2.pos.column == t1.pos.column + offset end |
#space_required_before?(token) ⇒ Boolean
31 32 33 34 |
# File 'lib/rubocop/cop/mixin/space_after_punctuation.rb', line 31 def space_required_before?(token) !(allowed_type?(token) || (token.type == :tRCURLY && space_forbidden_before_rcurly?)) end |