Class: Tailor::Rulers::SpacesAfterCommaRuler

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

Overview

Looks for spaces after a ‘,’ as given by @config. It skips checking when:

  • the char after it is a ‘n’.

  • it’s at the end of a line, followed by a trailing comment.

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) ⇒ SpacesAfterCommaRuler

Returns a new instance of SpacesAfterCommaRuler.



11
12
13
14
15
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 11

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

Instance Method Details

#check_spaces_after_comma(lexed_line, lineno) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 52

def check_spaces_after_comma(lexed_line, lineno)
  log "Commas found at: #{@comma_columns}" unless @comma_columns.empty?

  @comma_columns.each do |c|
    event_index = lexed_line.event_index(c)
    if event_index.nil?
      log 'Event index is nil.  Weird...'
      break
    end

    next_event = lexed_line.at(event_index + 1)
    if next_event.nil?
      log 'Looks like there is no next event (this is last in the line).'
      break
    end

    if next_event[1] == :on_nl || next_event[1] == :on_ignored_nl
      log 'Next event is a newline.'
      break
    end

    second_next_event = lexed_line.at(event_index + 2)
    if second_next_event.nil?
      log 'Second next event is nil.'
      next
    end

    if second_next_event[1] == :on_comment
      log 'Event + 2 is a comment.'
      next
    end

    actual_spaces = next_event[1] != :on_sp ? 0 : next_event.last.size
    measure(actual_spaces, lineno, c)
  end

  @comma_columns.clear
end

#comma_update(_, _, column) ⇒ Object



17
18
19
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 17

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

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



21
22
23
24
25
26
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 21

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



28
29
30
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 28

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

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the actual_spaces after a comma equals the value at @config.

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces after the comma.

  • lineno (Fixnum)

    Line the problem is on.

  • column (Fixnum)

    Column the potential problem is on.



42
43
44
45
46
47
48
49
50
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 42

def measure(actual_spaces, lineno, column)
  if actual_spaces != @config
    msg = "Line has #{actual_spaces} space(s) after 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



32
33
34
# File 'lib/tailor/rulers/spaces_after_comma_ruler.rb', line 32

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