aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/code_statistics_calculator.rb
blob: 85f86bdbd0fb27ae75e550b38890627893b8ae0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

class CodeStatisticsCalculator #:nodoc:
  attr_reader :lines, :code_lines, :classes, :methods

  PATTERNS = {
    rb: {
      line_comment: /^\s*#/,
      begin_block_comment: /^=begin/,
      end_block_comment: /^=end/,
      class: /^\s*class\s+[_A-Z]/,
      method: /^\s*def\s+[_a-z]/,
    },
    js: {
      line_comment: %r{^\s*//},
      begin_block_comment: %r{^\s*/\*},
      end_block_comment: %r{\*/},
      method: /function(\s+[_a-zA-Z][\da-zA-Z]*)?\s*\(/,
    },
    coffee: {
      line_comment: /^\s*#/,
      begin_block_comment: /^\s*###/,
      end_block_comment: /^\s*###/,
      class: /^\s*class\s+[_A-Z]/,
      method: /[-=]>/,
    }
  }

  PATTERNS[:minitest] = PATTERNS[:rb].merge method: /^\s*(def|test)\s+['"_a-z]/
  PATTERNS[:rake] = PATTERNS[:rb]

  def initialize(lines = 0, code_lines = 0, classes = 0, methods = 0)
    @lines = lines
    @code_lines = code_lines
    @classes = classes
    @methods = methods
  end

  def add(code_statistics_calculator)
    @lines += code_statistics_calculator.lines
    @code_lines += code_statistics_calculator.code_lines
    @classes += code_statistics_calculator.classes
    @methods += code_statistics_calculator.methods
  end

  def add_by_file_path(file_path)
    File.open(file_path) do |f|
      add_by_io(f, file_type(file_path))
    end
  end

  def add_by_io(io, file_type)
    patterns = PATTERNS[file_type] || {}

    comment_started = false

    while line = io.gets
      @lines += 1

      if comment_started
        if patterns[:end_block_comment] && line =~ patterns[:end_block_comment]
          comment_started = false
        end
        next
      else
        if patterns[:begin_block_comment] && line =~ patterns[:begin_block_comment]
          comment_started = true
          next
        end
      end

      @classes   += 1 if patterns[:class] && line =~ patterns[:class]
      @methods   += 1 if patterns[:method] && line =~ patterns[:method]
      if line !~ /^\s*$/ && (patterns[:line_comment].nil? || line !~ patterns[:line_comment])
        @code_lines += 1
      end
    end
  end

  private
    def file_type(file_path)
      if file_path.end_with? "_test.rb"
        :minitest
      else
        File.extname(file_path).sub(/\A\./, "").downcase.to_sym
      end
    end
end