Class: Tailor::Rulers::SpacesBeforeRbracketRuler

Inherits:
Tailor::Ruler show all
Defined in:
lib/tailor/rulers/spaces_before_rbracket_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 directly preceded by a [.

  • it’s directly preceded by spaces, then 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) ⇒ SpacesBeforeRbracketRuler

Returns a new instance of SpacesBeforeRbracketRuler.



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

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

Instance Method Details

#count_spaces(lexed_line, column) ⇒ Fixnum

Returns The number of spaces before the rbracket.

Parameters:

Returns:

  • (Fixnum)

    The number of spaces before the rbracket.



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

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? ||
    previous_event[1] == :on_lbracket
    return nil
  end

  return 0 if previous_event[1] != :on_sp
  return nil if current_index - 2 < 0

  second_previous_event = lexed_line.at(current_index - 2)
  return nil if second_previous_event[1] == :on_lbracket

  previous_event.last.size
end

#measure(actual_spaces, lineno, column) ⇒ Object

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

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces before the rbracket.

  • lineno (Fixnum)

    Line the potential problem is on.

  • column (Fixnum)

    Column the potential problem is on.



46
47
48
49
50
51
52
53
54
# File 'lib/tailor/rulers/spaces_before_rbracket_ruler.rb', line 46

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

#rbracket_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.

Parameters:



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

def rbracket_update(lexed_line, lineno, column)
  count = count_spaces(lexed_line, column)

  if count.nil?
    log 'rbracket must be at the beginning of the line.'
    return
  else
    log "Found #{count} space(s) before rbracket."
  end

  measure(count, lineno, column)
end