Class: Rubocop::Cop::SurroundingSpace
- Defined in:
- lib/rubocop/cop/surrounding_space.rb
Constant Summary collapse
- ERROR_MESSAGE =
'Surrounding space missing for '
Instance Attribute Summary
Attributes inherited from Cop
Instance Method Summary collapse
Methods inherited from Cop
#add_offence, enabled?, #has_report?, inherited, #initialize
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#inspect(file, source, tokens, sexp) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/surrounding_space.rb', line 10 def inspect(file, source, tokens, sexp) @correlations.sort.each do |ix, grammar_path| t = tokens[ix] case t.type when :on_op unless surrounded_by_whitespace?(tokens[ix - 1, 3]) unless ok_without_spaces?(grammar_path) add_offence(:convention, t.pos.lineno, ERROR_MESSAGE + "operator '#{t.text}'.") end end when :on_lbrace unless surrounded_by_whitespace?(tokens[ix - 1, 3]) add_offence(:convention, t.pos.lineno, ERROR_MESSAGE + "'{'.") end when :on_rbrace unless whitespace?(tokens[ix - 1]) add_offence(:convention, t.pos.lineno, "Space missing to the left of '}'.") end end end tokens.each_index do |ix| t = tokens[ix] prev, nxt = tokens.values_at(ix - 1, ix + 1) offence_detected = case t.type when :on_lbracket, :on_lparen nxt.type == :on_sp when :on_rbracket, :on_rparen if prev.type == :on_sp prev_ns = previous_non_space(tokens, ix) prev_ns && tokens_on_same_row?(prev_ns, tokens[ix]) && # Avoid double repoting of [ ] and ( ) prev_ns.type != :on_lbracket && prev_ns.type != :on_lparen end when :on_op t.text == '**' && (whitespace?(prev) || whitespace?(nxt)) end if offence_detected kind = case t.type when :on_lparen, :on_rparen 'inside parentheses' when :on_lbracket, :on_rbracket 'inside square brackets' when :on_op "around operator #{t.text}" end add_offence(:convention, t.pos.lineno, "Space #{kind} detected.") end end end |