forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
align_parameters.rb
94 lines (82 loc) · 2.52 KB
/
align_parameters.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Here we check if the parameters on a multi-line method call or
# definition are aligned.
#
# @example EnforcedStyle: with_first_parameter (default)
# # good
#
# foo :bar,
# :baz
#
# # bad
#
# foo :bar,
# :baz
#
# @example EnforcedStyle: with_fixed_indentation
# # good
#
# foo :bar,
# :baz
#
# # bad
#
# foo :bar,
# :baz
class AlignParameters < Cop
include Alignment
ALIGN_PARAMS_MSG = 'Align the parameters of a method %<type>s if ' \
'they span more than one line.'.freeze
FIXED_INDENT_MSG = 'Use one level of indentation for parameters ' \
'following the first line of a multi-line method %<type>s.'.freeze
def on_send(node)
return if node.arguments.size < 2 ||
node.send_type? && node.method?(:[]=)
check_alignment(node.arguments, base_column(node, node.arguments))
end
alias on_csend on_send
alias on_def on_send
alias on_defs on_send
def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, column_delta)
end
private
def message(node)
type = if node && (node.parent.send_type? || node.parent.csend_type?)
'call'
else
'definition'
end
msg = fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
format(msg, type: type)
end
def fixed_indentation?
cop_config['EnforcedStyle'] == 'with_fixed_indentation'
end
def base_column(node, args)
if fixed_indentation?
lineno = target_method_lineno(node)
line = node.source_range.source_buffer.source_line(lineno)
indentation_of_line = /\S.*/.match(line).begin(0)
indentation_of_line + configured_indentation_width
else
display_column(args.first.source_range)
end
end
def target_method_lineno(node)
if node.def_type? || node.defs_type?
node.loc.keyword.line
elsif node.loc.selector
node.loc.selector.line
else
# l.(1) has no selector, so we use the opening parenthesis instead
node.loc.begin.line
end
end
end
end
end
end