Class: Rubocop::Cop::SpaceAroundOperators

Inherits:
Cop
  • Object
show all
Includes:
SurroundingSpace
Defined in:
lib/rubocop/cop/surrounding_space.rb

Constant Summary collapse

ERROR_MESSAGE =
'Surrounding space missing for operator '

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #offences

Instance Method Summary collapse

Methods included from SurroundingSpace

#inspect

Methods inherited from Cop

#add_offence, #has_report?, inherited, #initialize

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check_missing_space(tokens, ix, grammar_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/surrounding_space.rb', line 40

def check_missing_space(tokens, ix, grammar_path)
  t = tokens[ix]
  if t.type == :on_op
    unless surrounded_by_whitespace?(tokens[ix - 1, 3])
      unless ok_without_spaces?(grammar_path)
        add_offence(:convention, t.pos.lineno,
                    ERROR_MESSAGE + "'#{t.text}'.")
      end
    end
  end
end

#check_unwanted_space(tokens, ix) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/surrounding_space.rb', line 52

def check_unwanted_space(tokens, ix)
  prev, t, nxt = tokens.values_at(ix - 1, ix, ix + 1)
  if t.type == :on_op && t.text == '**' &&
      (whitespace?(prev) || whitespace?(nxt))
    add_offence(:convention, t.pos.lineno,
                "Space around operator #{t.text} detected.")
  end
end