class Tailor::Rulers::SpacesAfterCommaRuler

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

Public Class Methods

new(config, options) click to toggle source
Calls superclass method Tailor::Ruler::new
# 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

Public Instance Methods

check_spaces_after_comma(lexed_line, lineno) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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, _) click to toggle source
# 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) click to toggle source

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

@param [Fixnum] actual_spaces The number of spaces after the comma. @param [Fixnum] lineno Line the problem is on. @param [Fixnum] column Column the potential problem is on.

# 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) click to toggle source
# 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