-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
method_definition_arguments_multiline.rb
76 lines (67 loc) · 1.75 KB
/
method_definition_arguments_multiline.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
module RuboCop
module Cop
module Sevencop
# Inserts new lines between method definition arguments.
#
# @example
# # bad
# def foo(a, b)
# end
#
# # good
# def foo(
# a,
# b
# )
# end
#
# # good
# def foo(a)
# end
class MethodDefinitionArgumentsMultiline < Base
extend AutoCorrector
MSG = 'Insert new lines between method definition arguments.'
# @param node [RuboCop::AST::ArgsNode]
def on_args(node)
return unless method_args?(node)
return if multilined?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
private
# @param corrector [RuboCop::AST::Corrector]
# @param node [RuboCop::AST::ArgsNode]
# @return [String]
def autocorrect(
corrector,
node
)
indent = ' ' * node.parent.source_range.column
corrector.replace(
node.source_range.with(
begin_pos: node.parent.location.name.end_pos
),
[
"(\n#{indent} ",
node.children.map(&:source).join(",\n#{indent} "),
"\n#{indent})"
].join
)
end
# @return [Boolean]
def method_args?(node)
!node.parent.nil? && node.parent.def_type?
end
# @param node [RuboCop::AST::ArgsNode]
# @return [Boolean]
def multilined?(node)
node.children.map do |child|
child.source_range.line
end.uniq.length == node.children.length
end
end
end
end
end