Class: Tailor::Rulers::SpacesInEmptyBracesRuler

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

Overview

Checks for spaces that exist between a { and } when there is only space in between them.

Instance Attribute Summary

Attributes inherited from Tailor::Ruler

#level, #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) ⇒ SpacesInEmptyBracesRuler



9
10
11
12
13
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 9

def initialize(config, options)
  super(config, options)
  add_lexer_observers :embexpr_beg, :lbrace, :rbrace
  @lbrace_nesting = []
end

Instance Method Details

#count_spaces(lexed_line, column) ⇒ Fixnum



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 18

def count_spaces(lexed_line, column)
  current_index = lexed_line.event_index(column)
  log "Current event index: #{current_index}"
  previous_event = lexed_line.at(current_index - 1)
  log "Previous event: #{previous_event}"

  if column.zero? || previous_event.nil?
    return
  end

  if previous_event[1] == :on_lbrace
    return 0
  end

  if previous_event[1] == :on_sp
    second_previous_event = lexed_line.at(current_index - 2)

    if second_previous_event[1] == :on_lbrace
      previous_event.last.size
    else
      nil
    end
  end
end

#embexpr_beg_update(lexed_line, lineno, column) ⇒ Object



43
44
45
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 43

def embexpr_beg_update(lexed_line, lineno, column)
  @lbrace_nesting << :embexpr_beg
end

#lbrace_update(lexed_line, lineno, column) ⇒ Object



47
48
49
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 47

def lbrace_update(lexed_line, lineno, column)
  @lbrace_nesting << :lbrace
end

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the counted spaces between an lbrace and an rbrace equals the value at @config.



61
62
63
64
65
66
67
68
69
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 61

def measure(actual_spaces, lineno, column)
  if actual_spaces != @config
    msg = "Line has #{actual_spaces} space(s) in between empty "
    msg << "braces, but should have #{@config}."

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

#nl_update(lexed_line, lineno, column) ⇒ Object



51
52
53
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 51

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

#rbrace_update(lexed_line, lineno, column) ⇒ Object

This has to keep track of {s and only follow through with the check if the { was an lbrace because Ripper doesn’t scan the } of an embedded expression (embexpr_end) as such.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tailor/rulers/spaces_in_empty_braces_ruler.rb', line 78

def rbrace_update(lexed_line, lineno, column)
  if @lbrace_nesting.last == :embexpr_beg
    @lbrace_nesting.pop
    return
  end

  @lbrace_nesting.pop
  count = count_spaces(lexed_line, column)

  if count.nil?
    log "Braces aren't empty.  Moving on."
    return
  else
    log "Found #{count} space(s) before rbrace."
  end

  measure(count, lineno, column)
end