Class: Tailor::Rulers::MaxCodeLinesInMethodRuler

Inherits:
Tailor::Ruler show all
Includes:
LexerConstants
Defined in:
lib/tailor/rulers/max_code_lines_in_method_ruler.rb

Constant Summary

Constants included from LexerConstants

LexerConstants::CONTINUATION_KEYWORDS, LexerConstants::KEYWORDS_AND_MODIFIERS, LexerConstants::KEYWORDS_TO_INDENT, LexerConstants::LOOP_KEYWORDS, LexerConstants::MODIFIERS, LexerConstants::MULTILINE_OPERATORS

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

Returns a new instance of MaxCodeLinesInMethodRuler.



9
10
11
12
13
14
15
# File 'lib/tailor/rulers/max_code_lines_in_method_ruler.rb', line 9

def initialize(config, options)
  super(config, options)
  add_lexer_observers(:ignored_nl, :kw, :nl)
  @method_start_lines = []
  @kw_start_lines = []
  @end_last_method = false
end

Instance Method Details

#ignored_nl_update(lexed_line, _, _) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tailor/rulers/max_code_lines_in_method_ruler.rb', line 17

def ignored_nl_update(lexed_line, _, _)
  return if @method_start_lines.empty?
  return if lexed_line.only_spaces?
  return if lexed_line.comment_line?

  @method_start_lines.each do |line|
    line[:count] += 1
    log "Method from line #{line[:lineno]} now at #{line[:count]} lines."
  end

  if @end_last_method
    measure(@method_start_lines.last[:count],
      @method_start_lines.last[:lineno],
      @method_start_lines.last[:column])
    @method_start_lines.pop
    @end_last_method = false
  end
end

#kw_update(token, _, lineno, column) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tailor/rulers/max_code_lines_in_method_ruler.rb', line 36

def kw_update(token, _, lineno, column)
  if token == 'def'
    @method_start_lines << { lineno: lineno, column: column, count: 0 }
    log "Method start lines: #{@method_start_lines}"
  end

  unless token.modifier_keyword? ||
    !token.keyword_to_indent? ||
    token.do_is_for_a_loop? ||
    token.continuation_keyword?
    @kw_start_lines << lineno
    log "Keyword start lines: #{@kw_start_lines}"
  end

  if token == 'end'
    log "Got 'end' of method."

    unless @method_start_lines.empty?
      if @method_start_lines.last[:lineno] == @kw_start_lines.last
        #msg = "Method from line #{@method_start_lines.last[:lineno]}"
        #msg << " was #{@method_start_lines.last[:count]} lines long."
        #log msg
        @end_last_method = true
      end
    end

    @kw_start_lines.pop
    log "End of keyword statement.  Keywords: #{@kw_start_lines}"
  end
end

#measure(actual_count, lineno, column) ⇒ Object

Checks to see if the actual count of code lines in the method is greater than the value in @config.

Parameters:

  • actual_count (Fixnum)

    The number of code lines found.

  • lineno (Fixnum)

    The line the potential problem is on.

  • column (Fixnum)

    The column the potential problem is on.



77
78
79
80
81
82
83
84
85
# File 'lib/tailor/rulers/max_code_lines_in_method_ruler.rb', line 77

def measure(actual_count, lineno, column)
  if actual_count > @config
    msg = "Method has #{actual_count} code lines, but "
    msg << "should have no more than #{@config}."

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

#nl_update(lexed_line, lineno, column) ⇒ Object



67
68
69
# File 'lib/tailor/rulers/max_code_lines_in_method_ruler.rb', line 67

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