Class: Tailor::Rulers::SpacesAfterLbracketRuler

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

Overview

Detects spaces after a ‘[’ as given by @config. It skips checking when:

  • it’s the last char in line.

  • the char after it is a ‘]’.

  • the char after it is space, 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) ⇒ SpacesAfterLbracketRuler

Returns a new instance of SpacesAfterLbracketRuler.



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

def initialize(config, options)
  super(config, options)
  add_lexer_observers :comment, :ignored_nl, :lbracket, :nl
  @lbracket_columns = []
end

Instance Method Details

#check_spaces_after_lbracket(lexed_line, lineno) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 53

def check_spaces_after_lbracket(lexed_line, lineno)
  unless @lbracket_columns.empty?
    log "lbracket found at: #{@lbracket_columns}"
  end

  @lbracket_columns.each do |column|
    actual_spaces = count_spaces(lexed_line, column)
    next if actual_spaces.nil?

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

    @do_measurement = true
  end

  @lbracket_columns.clear
end

#comment_update(token, lexed_line, _, lineno, column) ⇒ Object



18
19
20
21
22
23
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 18

def comment_update(token, lexed_line, _, lineno, column)
  if token =~ /\n$/
    log 'Found comment with trailing newline.'
    ignored_nl_update(lexed_line, lineno, column)
  end
end

#count_spaces(lexed_line, column) ⇒ Fixnum

Counts the number of spaces after the lbracket.

Parameters:

  • lexed_line (LexedLine)

    The LexedLine that contains the context the lbracket was found in.

  • column (Fixnum)

    Column the lbracket was found at.

Returns:

  • (Fixnum)

    The number of spaces found after the lbracket.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 80

def count_spaces(lexed_line, column)
  event_index = lexed_line.event_index(column)

  if event_index.nil?
    log 'No lbracket in this line.  Moving on...'
    @do_measurement = false
    return
  end

  next_event = lexed_line.at(event_index + 1)
  log "Next event: #{next_event}"

  if next_event.nil?
    log 'lbracket must be at the end of the line.'
    @do_measurement = false
    return 0
  end

  [:on_nl, :on_ignored_nl].each do |event|
    if next_event[1] == event
      log "lbracket is followed by a '#{event}'.  Moving on."
      @do_measurement = false
      return 0
    end
  end

  if next_event[1] == :on_rbracket
    log 'lbracket is followed by a rbracket.  Moving on.'
    @do_measurement = false
    return 0
  end

  second_next_event = lexed_line.at(event_index + 2)
  log "Event + 2: #{second_next_event}"

  [:on_comment, :on_lbrace].each do |event|
    if second_next_event[1] == event
      log "Event + 2 is a #{event}.  Moving on."
      @do_measurement = false
      return next_event.last.size
    end
  end

  next_event[1] != :on_sp ? 0 : next_event.last.size
end

#ignored_nl_update(lexed_line, lineno, _) ⇒ Object



25
26
27
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 25

def ignored_nl_update(lexed_line, lineno, _)
  check_spaces_after_lbracket(lexed_line, lineno)
end

#lbracket_update(_, _, column) ⇒ Object



29
30
31
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 29

def lbracket_update(_, _, column)
  @lbracket_columns << column
end

#measure(actual_spaces, lineno, column) ⇒ Object

Checks to see if the actual_spaces after an lbracket equals the value at @config.

Parameters:

  • actual_spaces (Fixnum)

    The number of spaces after the lbracket.

  • lineno (Fixnum)

    Line the problem was found on.

  • column (Fixnum)

    Column the problem was found on.



43
44
45
46
47
48
49
50
51
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 43

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

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

#nl_update(lexed_line, lineno, column) ⇒ Object



33
34
35
# File 'lib/tailor/rulers/spaces_after_lbracket_ruler.rb', line 33

def nl_update(lexed_line, lineno, column)
  ignored_nl_update(lexed_line, lineno, column)
end