Class: Tailor::Rulers::SpacesBeforeRbraceRuler

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

Overview

Checks for spaces before a } as given by @config. It skips checking when:

  • it’s the first char in the line.

  • it’s the first char in the line, preceded by spaces.

  • it’s directly preceded by a ‘{’.

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



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

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



21
22
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
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 21

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 "rbrace is at the beginning of the line."
    @do_measurement = false
    return 0
  end

  if previous_event[1] == :on_lbrace
    log "rbrace comes after a '{'"
    @do_measurement = false
    return 0
  end

  return 0 if previous_event[1] != :on_sp

  if current_index - 2 < 0
    log "rbrace is at the beginning of an indented line.  Moving on."
    @do_measurement = false
    return previous_event.last.size
  end

  previous_event.last.size
end

#embexpr_beg_updateObject



50
51
52
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 50

def embexpr_beg_update
  @lbrace_nesting << :embexpr_beg
end

#lbrace_update(lexed_line, lineno, column) ⇒ Object



54
55
56
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 54

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

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the number of spaces before an rbrace equals the value at @config.



64
65
66
67
68
69
70
71
72
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 64

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

#rbrace_update(lexed_line, lineno, column) ⇒ Object

This has to keep track of ‘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.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 81

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)
  log "Found #{count} space(s) before rbrace."

  if @do_measurement == false
    log "Skipping measurement."
  else
    measure(count, lineno, column)
  end

  @do_measurement = true
end