forked from rubocop/rubocop
/
class_length.rb
56 lines (51 loc) · 1.42 KB
/
class_length.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module Metrics
# This cop checks if the length a class exceeds some maximum value.
# Comment lines can optionally be ignored.
# The maximum allowed length is configurable.
#
# You can set literals you want to fold with `CountAsOne`.
# Available are: 'array', 'hash', and 'heredoc'. Each literal
# will be counted as one line regardless of its actual size.
#
# @example CountAsOne: ['array', 'heredoc']
#
# class Foo
# ARRAY = [ # +1
# 1,
# 2
# ]
#
# HASH = { # +3
# key: 'value'
# }
#
# MSG = <<~HEREDOC # +1
# Heredoc
# content.
# HEREDOC
# end # 5 points
#
#
# NOTE: This cop also applies for `Struct` definitions.
class ClassLength < Base
include CodeLength
def on_class(node)
check_code_length(node)
end
def on_casgn(node)
_scope, _name, block_node = *node
check_code_length(node) if block_node.class_definition?
end
private
def message(length, max_length)
format('Class has too many lines. [%<length>d/%<max>d]',
length: length,
max: max_length)
end
end
end
end
end