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

#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

Returns a new instance of SpacesBeforeLbraceRuler.



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

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 ‘{’.

Parameters:

Returns:

  • (Fixnum)

    The number of spaces before the lbrace.



23
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
# File 'lib/tailor/rulers/spaces_before_lbrace_ruler.rb', line 23

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.

Parameters:

  • lexed_line (LexedLine)
  • lineno (Fixnum)
  • column (Fixnum)


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

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.

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces before the lbrace.

  • lineno (Fixnum)

    Line the potential problem is on.

  • column (Fixnum)

    Column the potential problem is on.



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

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