forked from rubocop/rubocop
/
multiline_method_signature.rb
61 lines (51 loc) · 1.35 KB
/
multiline_method_signature.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
57
58
59
60
61
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for method signatures that span multiple lines.
#
# @example
#
# # good
#
# def foo(bar, baz)
# end
#
# # bad
#
# def foo(bar,
# baz)
# end
#
class MultilineMethodSignature < Cop
MSG = 'Avoid multi-line method signatures.'
def on_def(node)
return unless node.arguments?
return if opening_line(node) == closing_line(node)
return if correction_exceeds_max_line_length?(node)
add_offense(node)
end
alias on_defs on_def
private
def opening_line(node)
node.first_line
end
def closing_line(node)
node.arguments.last_line
end
def correction_exceeds_max_line_length?(node)
indentation_width(node) + definition_width(node) > max_line_length
end
def indentation_width(node)
processed_source.line_indentation(node.loc.expression.line)
end
def definition_width(node)
node.source_range.begin.join(node.arguments.source_range.end).length
end
def max_line_length
config.for_cop('Layout/LineLength')['Max'] || 120
end
end
end
end
end