Class: Skeptic::Rules::SpacesAroundOperators

Inherits:
Object
  • Object
show all
Includes:
SexpVisitor
Defined in:
lib/skeptic/rules/spaces_around_operators.rb

Constant Summary collapse

DESCRIPTION =
'Check for spaces around operators'
OPERATORS_WITHOUT_SPACES_AROUND_THEM =
['**', '::', '...', '..']
IGNORED_TOKEN_TYPES =
[:on_sp, :on_ignored_nl, :on_nl, :on_lparen, :on_symbeg]

Instance Method Summary collapse

Methods included from SexpVisitor

included

Constructor Details

#initialize(data) ⇒ SpacesAroundOperators

Returns a new instance of SpacesAroundOperators.



11
12
13
14
# File 'lib/skeptic/rules/spaces_around_operators.rb', line 11

def initialize(data)
  @violations = []
  @special_tokens_locations = []
end

Instance Method Details

#apply_to(code, tokens, sexp) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/skeptic/rules/spaces_around_operators.rb', line 16

def apply_to(code, tokens, sexp)
  visit sexp

  @violations = tokens.each_cons(3).select do |_, token, _|
    operator_expecting_spaces? token
  end.select do |left, operator, right|
    no_spaces_on_left_of?(operator, left) or
    no_spaces_on_right_of?(operator, right)
  end.map do |_, operator, _|
    [operator.last, operator.first[0]]
  end
  self
end

#nameObject



36
37
38
# File 'lib/skeptic/rules/spaces_around_operators.rb', line 36

def name
  'Spaces around operators'
end

#violationsObject



30
31
32
33
34
# File 'lib/skeptic/rules/spaces_around_operators.rb', line 30

def violations
  @violations.map do |value, line_number|
    "no spaces around #{value} on line #{line_number}"
  end
end