Class: Tailor::Rulers::SpacesBeforeLbraceRuler

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

Overview

Detects spaces before a { as given by @config. It skips checking when:

  • it’s the first char in the line.

  • the char before it is a ‘#{’.

  • the char before it is a ‘(’.

  • the char before it is a ‘[’.

  • it’s only preceded by spaces.

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



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

def initialize(config, options)
  super(config, options)
  add_lexer_observers :lbrace
end

Instance Method Details

#count_spaces(lexed_line, column) ⇒ Fixnum

Counts the spaces before the ‘{’.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tailor/rulers/spaces_before_lbrace_ruler.rb', line 24

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?
    log 'lbrace must be at the beginning of the line.'
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_embexpr_beg
    log "lbrace comes after a '\#{'."
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_lparen
    log "lbrace comes after a '('."
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_lbracket
    log "lbrace comes after a '['."
    @do_measurement = false
    return 0
  end

  return 0 if previous_event[1] != :on_sp

  if current_index - 2 < 0
    log 'lbrace comes at the beginning of an indented line.'
    @do_measurement = false
    return previous_event.last.size
  end

  previous_event.last.size
end

#lbrace_update(lexed_line, lineno, column) ⇒ Object

Called by Lexer when :on_lbrace is encountered.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tailor/rulers/spaces_before_lbrace_ruler.rb', line 70

def lbrace_update(lexed_line, lineno, column)
  count = count_spaces(lexed_line, column)
  log "Found #{count} space(s) before lbrace."

  if !@do_measurement
    log 'Skipping measurement.'
  else
    measure(count, lineno, column)
  end

  @do_measurement = true
end

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the counted spaces before an lbrace equals the value at @config.



89
90
91
92
93
94
95
96
97
# File 'lib/tailor/rulers/spaces_before_lbrace_ruler.rb', line 89

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

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