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

#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

Returns a new instance of SpacesBeforeRbraceRuler.



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

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

Instance Method Details

#count_spaces(lexed_line, column) ⇒ Fixnum

Returns the number of spaces before the rbrace.

Parameters:

Returns:

  • (Fixnum)

    the number of spaces before the rbrace.



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_update(_, _, _) ⇒ Object



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

def embexpr_beg_update(_, _, _)
  if RUBY_VERSION < '2.0.0'
    @lbrace_nesting << :embexpr_beg
  end
end

#lbrace_update(_, _, _) ⇒ Object



56
57
58
# File 'lib/tailor/rulers/spaces_before_rbrace_ruler.rb', line 56

def lbrace_update(_, _, _)
  @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.

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces after the rbrace.

  • lineno (Fixnum)

    Line the problem was found on.

  • column (Fixnum)

    Column the problem was found on.



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

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

For Ruby versions < 2.0.0-p0, 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.

Parameters:



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

def rbrace_update(lexed_line, lineno, column)
  if RUBY_VERSION < '2.0.0' && @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
    log 'Skipping measurement.'
  else
    measure(count, lineno, column)
  end

  @do_measurement = true
end