Class: Tailor::Rulers::SpacesBeforeCommaRuler

Inherits:
Tailor::Ruler show all
Defined in:
lib/tailor/rulers/spaces_before_comma_ruler.rb

Overview

Checks for spaces before a ‘,’ as given by @config.

Instance Attribute Summary

Attributes inherited from Tailor::Ruler

#lexer_observers

Instance Method Summary collapse

Methods inherited from Tailor::Ruler

#add_child_ruler, #problem_type, #problems

Methods included from Logger::Mixin

included

Constructor Details

#initialize(config, options) ⇒ SpacesBeforeCommaRuler

Returns a new instance of SpacesBeforeCommaRuler.



8
9
10
11
12
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 8

def initialize(config, options)
  super(config, options)
  add_lexer_observers :comma, :comment, :ignored_nl, :nl
  @comma_columns = []
end

Instance Method Details

#check_spaces_before_comma(lexed_line, lineno) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 41

def check_spaces_before_comma(lexed_line, lineno)
  @comma_columns.each do |c|
    event_index = lexed_line.event_index(c)
    if event_index.nil?
      log 'Event index is nil.  Weird...'
      next
    end

    previous_event = lexed_line.at(event_index - 1)
    actual_spaces = if previous_event[1] != :on_sp
      0
    else
      previous_event.last.size
    end

    measure(actual_spaces, lineno, c)
  end

  @comma_columns.clear
end

#comma_update(_, _, column) ⇒ Object



14
15
16
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 14

def comma_update(_, _, column)
  @comma_columns << column
end

#comment_update(token, lexed_line, _, lineno, column) ⇒ Object



18
19
20
21
22
23
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 18

def comment_update(token, lexed_line, _, lineno, column)
  if token =~ /\n$/
    log 'Found comment with trailing newline.'
    ignored_nl_update(lexed_line, lineno, column)
  end
end

#ignored_nl_update(lexed_line, lineno, _) ⇒ Object



62
63
64
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 62

def ignored_nl_update(lexed_line, lineno, _)
  check_spaces_before_comma(lexed_line, lineno)
end

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the number of spaces before a comma equals the value at @config.

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces before the comma.

  • lineno (Fixnum)

    Line the potential problem is on.

  • column (Fixnum)

    Column the potential problem is on.



31
32
33
34
35
36
37
38
39
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 31

def measure(actual_spaces, lineno, column)
  if actual_spaces != @config
    msg = "Line has #{actual_spaces} space(s) before a comma, "
    msg << "but should have #{@config}."

    @problems << Problem.new(problem_type, lineno, column - 1, msg,
      @options[:level])
  end
end

#nl_update(lexed_line, lineno, column) ⇒ Object



66
67
68
# File 'lib/tailor/rulers/spaces_before_comma_ruler.rb', line 66

def nl_update(lexed_line, lineno, column)
  ignored_nl_update(lexed_line, lineno, column)
end