Class: Tailor::Rulers::SpacesAfterConditionalRuler

Inherits:
Tailor::Ruler
  • Object
show all
Defined in:
lib/tailor/rulers/spaces_after_conditional_ruler.rb

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

Returns a new instance of SpacesAfterConditionalRuler.



7
8
9
10
# File 'lib/tailor/rulers/spaces_after_conditional_ruler.rb', line 7

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

Instance Method Details

#measure(lexed_line, lineno) ⇒ Object

Checks to see if spacing is present after conditionals

Parameters:

  • lexed_line (Array)

    The lexed line with a conditional

  • lineno (Fixnum)

    Line the problem was found on.



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

def measure(lexed_line, lineno)

  idx = lexed_line.index do |_, token, name|
    token == :on_kw and %w{if unless case}.include?(name)
  end

  expected_spaces = @config
  spaces = expected_spaces

  if idx
    column = lexed_line[idx].first.last
    pos, token, _ = lexed_line[idx + 1]
    spaces = case token
    when :on_lparen then 0
    when :on_sp
      next_token = lexed_line[idx + 2]
      next_token.first.last - pos.last
    end
  end

  if expected_spaces != spaces
    @problems << Problem.new(problem_type, lineno, column,
      "#{spaces} spaces after conditional at column #{column}, " +
        "expected #{expected_spaces}.", @options[:level])
  end
end

#nl_update(current_lexed_line, lineno, _) ⇒ Object



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

def nl_update(current_lexed_line, lineno, _)
  measure(current_lexed_line, lineno)
end