rubocop 0.77.0 → 0.79.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +39 -26
- data/lib/rubocop.rb +11 -2
- data/lib/rubocop/ast/builder.rb +43 -42
- data/lib/rubocop/ast/node/def_node.rb +11 -0
- data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
- data/lib/rubocop/ast/traversal.rb +11 -3
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +7 -7
- data/lib/rubocop/cli/command/show_cops.rb +11 -4
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader.rb +19 -19
- data/lib/rubocop/config_obsoletion.rb +3 -3
- data/lib/rubocop/config_validator.rb +55 -95
- data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
- data/lib/rubocop/cop/cop.rb +3 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -4
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -4
- data/lib/rubocop/cop/{metrics → layout}/line_length.rb +5 -78
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
- data/lib/rubocop/cop/layout/space_around_operators.rb +31 -6
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +4 -4
- data/lib/rubocop/cop/migration/department_name.rb +16 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
- data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
- data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +6 -3
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/offense.rb +11 -0
- data/lib/rubocop/cop/registry.rb +7 -2
- data/lib/rubocop/cop/style/attr.rb +8 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/guard_clause.rb +3 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -207
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/json_formatter.rb +6 -5
- data/lib/rubocop/node_pattern.rb +1 -1
- data/lib/rubocop/options.rb +8 -8
- data/lib/rubocop/result_cache.rb +2 -0
- data/lib/rubocop/rspec/shared_contexts.rb +5 -0
- data/lib/rubocop/runner.rb +5 -1
- data/lib/rubocop/target_ruby.rb +151 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +12 -5
@@ -10,13 +10,24 @@ module RuboCop
|
|
10
10
|
|
11
11
|
MSG = 'Department name is missing.'
|
12
12
|
|
13
|
+
DISABLE_COMMENT_FORMAT =
|
14
|
+
/\A(# *rubocop *: *((dis|en)able|todo) +)(.*)/.freeze
|
15
|
+
|
16
|
+
# The token that makes up a disable comment.
|
17
|
+
# The token used after `# rubocop: disable` are `A-z`, `/`, and `,`.
|
18
|
+
# Also `A-z` includes `all`.
|
19
|
+
DISABLING_COPS_CONTENT_TOKEN = %r{[A-z/,]+}.freeze
|
20
|
+
|
13
21
|
def investigate(processed_source)
|
14
22
|
processed_source.each_comment do |comment|
|
15
|
-
next if comment.text !~
|
23
|
+
next if comment.text !~ DISABLE_COMMENT_FORMAT
|
16
24
|
|
17
25
|
offset = Regexp.last_match(1).length
|
18
26
|
Regexp.last_match(4).scan(%r{[\w/]+|\W+}) do |name|
|
27
|
+
break unless valid_content_token?(name.strip)
|
28
|
+
|
19
29
|
check_cop_name(name, comment, offset)
|
30
|
+
|
20
31
|
offset += name.length
|
21
32
|
end
|
22
33
|
end
|
@@ -38,6 +49,10 @@ module RuboCop
|
|
38
49
|
range = range_between(start, start + name.length)
|
39
50
|
add_offense(range, location: range)
|
40
51
|
end
|
52
|
+
|
53
|
+
def valid_content_token?(content_token)
|
54
|
+
!DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil?
|
55
|
+
end
|
41
56
|
end
|
42
57
|
end
|
43
58
|
end
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
# If this offense is within a line range that is already being
|
36
36
|
# realigned by autocorrect, we report the offense without
|
37
37
|
# autocorrecting it. Two rewrites in the same area by the same
|
38
|
-
# cop
|
38
|
+
# cop cannot be handled. The next iteration will find the
|
39
39
|
# offense again and correct it.
|
40
40
|
add_offense(nil, location: expr)
|
41
41
|
else
|
@@ -40,13 +40,7 @@ module RuboCop
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def leading_comment_lines
|
43
|
-
|
44
|
-
|
45
|
-
comments.each_with_object([]) do |comment, leading_comments|
|
46
|
-
next if comment.loc.line > 3
|
47
|
-
|
48
|
-
leading_comments << comment.text
|
49
|
-
end
|
43
|
+
processed_source.comments.first(3).map(&:text)
|
50
44
|
end
|
51
45
|
end
|
52
46
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Help methods for determining if a line is too long.
|
6
|
+
module LineLengthHelp
|
7
|
+
private
|
8
|
+
|
9
|
+
def ignore_cop_directives?
|
10
|
+
config.for_cop('Layout/LineLength')['IgnoreCopDirectives']
|
11
|
+
end
|
12
|
+
|
13
|
+
def directive_on_source_line?(line_index)
|
14
|
+
source_line_number = line_index + processed_source.buffer.first_line
|
15
|
+
comment =
|
16
|
+
processed_source.comments
|
17
|
+
.detect { |e| e.location.line == source_line_number }
|
18
|
+
|
19
|
+
return false unless comment
|
20
|
+
|
21
|
+
comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
|
22
|
+
end
|
23
|
+
|
24
|
+
def allow_uri?
|
25
|
+
config.for_cop('Layout/LineLength')['AllowURI']
|
26
|
+
end
|
27
|
+
|
28
|
+
def allowed_uri_position?(line, uri_range)
|
29
|
+
uri_range.begin < max_line_length &&
|
30
|
+
(uri_range.end == line_length(line) ||
|
31
|
+
uri_range.end == line_length(line) - 1)
|
32
|
+
end
|
33
|
+
|
34
|
+
def line_length(line)
|
35
|
+
line.length + indentation_difference(line)
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_excessive_uri_range(line)
|
39
|
+
last_uri_match = match_uris(line).last
|
40
|
+
return nil unless last_uri_match
|
41
|
+
|
42
|
+
begin_position, end_position = last_uri_match.offset(0).map do |pos|
|
43
|
+
pos + indentation_difference(line)
|
44
|
+
end
|
45
|
+
return nil if begin_position < max_line_length &&
|
46
|
+
end_position < max_line_length
|
47
|
+
|
48
|
+
begin_position...end_position
|
49
|
+
end
|
50
|
+
|
51
|
+
def match_uris(string)
|
52
|
+
matches = []
|
53
|
+
string.scan(uri_regexp) do
|
54
|
+
matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
|
55
|
+
end
|
56
|
+
matches
|
57
|
+
end
|
58
|
+
|
59
|
+
def indentation_difference(line)
|
60
|
+
return 0 unless tab_indentation_width
|
61
|
+
|
62
|
+
line.match(/^\t*/)[0].size * (tab_indentation_width - 1)
|
63
|
+
end
|
64
|
+
|
65
|
+
def tab_indentation_width
|
66
|
+
config.for_cop('Layout/Tab')['IndentationWidth']
|
67
|
+
end
|
68
|
+
|
69
|
+
def uri_regexp
|
70
|
+
@uri_regexp ||=
|
71
|
+
URI::DEFAULT_PARSER
|
72
|
+
.make_regexp(config.for_cop('Layout/LineLength')['URISchemes'])
|
73
|
+
end
|
74
|
+
|
75
|
+
def valid_uri?(uri_ish_string)
|
76
|
+
URI.parse(uri_ish_string)
|
77
|
+
true
|
78
|
+
rescue URI::InvalidURIError, NoMethodError
|
79
|
+
false
|
80
|
+
end
|
81
|
+
|
82
|
+
def line_length_without_directive(line)
|
83
|
+
before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
|
84
|
+
before_comment.rstrip.length
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common functionality for handling Rational literals.
|
6
|
+
module RationalLiteral
|
7
|
+
extend NodePattern::Macros
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def_node_matcher :rational_literal?, <<~PATTERN
|
12
|
+
(send
|
13
|
+
(int _) :/
|
14
|
+
(rational _))
|
15
|
+
PATTERN
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -52,9 +52,9 @@ module RuboCop
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def max_line_length
|
55
|
-
return unless config.for_cop('
|
55
|
+
return unless config.for_cop('Layout/LineLength')['Enabled']
|
56
56
|
|
57
|
-
config.for_cop('
|
57
|
+
config.for_cop('Layout/LineLength')['Max']
|
58
58
|
end
|
59
59
|
|
60
60
|
def indentation_multiplier
|
@@ -93,9 +93,12 @@ module RuboCop
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def method_name_and_arguments_on_same_line?(node)
|
96
|
-
|
97
|
-
|
98
|
-
|
96
|
+
return false unless node.call_type?
|
97
|
+
|
98
|
+
line = node.loc.selector.nil? ? node.loc.line : node.loc.selector.line
|
99
|
+
|
100
|
+
line == node.last_argument.last_line &&
|
101
|
+
node.last_line == node.last_argument.last_line
|
99
102
|
end
|
100
103
|
|
101
104
|
# A single argument with the closing bracket on the same line as the end
|
data/lib/rubocop/cop/offense.rb
CHANGED
@@ -65,6 +65,17 @@ module RuboCop
|
|
65
65
|
freeze
|
66
66
|
end
|
67
67
|
|
68
|
+
# @api public
|
69
|
+
#
|
70
|
+
# @!attribute [r] correctable?
|
71
|
+
#
|
72
|
+
# @return [Boolean]
|
73
|
+
# whether this offense can be automatically corrected via
|
74
|
+
# autocorrect or a todo.
|
75
|
+
def correctable?
|
76
|
+
@status != :unsupported
|
77
|
+
end
|
78
|
+
|
68
79
|
# @api public
|
69
80
|
#
|
70
81
|
# @!attribute [r] corrected?
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -146,10 +146,15 @@ module RuboCop
|
|
146
146
|
|
147
147
|
def enabled?(cop, config, only_safe)
|
148
148
|
cfg = config.for_cop(cop)
|
149
|
+
|
150
|
+
# cfg['Enabled'] might be a string `pending`, which is considered
|
151
|
+
# disabled
|
152
|
+
cop_enabled = cfg.fetch('Enabled') == true
|
153
|
+
|
149
154
|
if only_safe
|
150
|
-
|
155
|
+
cop_enabled && cfg.fetch('Safe', true)
|
151
156
|
else
|
152
|
-
|
157
|
+
cop_enabled
|
153
158
|
end
|
154
159
|
end
|
155
160
|
|
@@ -21,6 +21,10 @@ module RuboCop
|
|
21
21
|
|
22
22
|
def on_send(node)
|
23
23
|
return unless node.command?(:attr) && node.arguments?
|
24
|
+
# check only for method definitions in class/module body
|
25
|
+
return if node.parent &&
|
26
|
+
!node.parent.class_type? &&
|
27
|
+
!class_eval?(node.parent)
|
24
28
|
|
25
29
|
add_offense(node, location: :selector)
|
26
30
|
end
|
@@ -56,6 +60,10 @@ module RuboCop
|
|
56
60
|
'attr_reader'
|
57
61
|
end
|
58
62
|
end
|
63
|
+
|
64
|
+
def_node_matcher :class_eval?, <<~PATTERN
|
65
|
+
(block (send _ {:class_eval :module_eval}) ...)
|
66
|
+
PATTERN
|
59
67
|
end
|
60
68
|
end
|
61
69
|
end
|
@@ -212,7 +212,7 @@ module RuboCop
|
|
212
212
|
%i[casgn cvasgn gvasgn ivasgn lvasgn].freeze
|
213
213
|
ASSIGNMENT_TYPES = VARIABLE_ASSIGNMENT_TYPES +
|
214
214
|
%i[and_asgn or_asgn op_asgn masgn].freeze
|
215
|
-
LINE_LENGTH = '
|
215
|
+
LINE_LENGTH = 'Layout/LineLength'
|
216
216
|
INDENTATION_WIDTH = 'Layout/IndentationWidth'
|
217
217
|
ENABLED = 'Enabled'
|
218
218
|
MAX = 'Max'
|
@@ -376,7 +376,7 @@ module RuboCop
|
|
376
376
|
assignment_types_match?(*statements)
|
377
377
|
end
|
378
378
|
|
379
|
-
# If `
|
379
|
+
# If `Layout/LineLength` is enabled, we do not want to introduce an
|
380
380
|
# offense by auto-correcting this cop. Find the max configured line
|
381
381
|
# length. Find the longest line of condition. Remove the assignment
|
382
382
|
# from lines that contain the offending assignment because after
|
@@ -49,8 +49,9 @@ module RuboCop
|
|
49
49
|
|
50
50
|
if body.if_type?
|
51
51
|
check_ending_if(body)
|
52
|
-
elsif body.begin_type?
|
53
|
-
|
52
|
+
elsif body.begin_type?
|
53
|
+
final_expression = body.children.last
|
54
|
+
check_ending_if(final_expression) if final_expression&.if_type?
|
54
55
|
end
|
55
56
|
end
|
56
57
|
alias on_defs on_def
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
# written as modifier `if`/`unless`. The cop also checks for modifier
|
8
8
|
# `if`/`unless` lines that exceed the maximum line length.
|
9
9
|
#
|
10
|
-
# The maximum line length is configured in the `
|
10
|
+
# The maximum line length is configured in the `Layout/LineLength`
|
11
11
|
# cop. The tab size is configured in the `IndentationWidth` of the
|
12
12
|
# `Layout/Tab` cop.
|
13
13
|
#
|
@@ -32,6 +32,8 @@ module RuboCop
|
|
32
32
|
# end
|
33
33
|
class IfUnlessModifier < Cop
|
34
34
|
include StatementModifier
|
35
|
+
include LineLengthHelp
|
36
|
+
include IgnoredPattern
|
35
37
|
|
36
38
|
MSG_USE_MODIFIER = 'Favor modifier `%<keyword>s` usage when having a ' \
|
37
39
|
'single-line body. Another good alternative is ' \
|
@@ -66,6 +68,10 @@ module RuboCop
|
|
66
68
|
|
67
69
|
private
|
68
70
|
|
71
|
+
def ignored_patterns
|
72
|
+
config.for_cop('Layout/LineLength')['IgnoredPatterns'] || []
|
73
|
+
end
|
74
|
+
|
69
75
|
def too_long_single_line?(node)
|
70
76
|
return false unless max_line_length
|
71
77
|
|
@@ -73,12 +79,41 @@ module RuboCop
|
|
73
79
|
return false unless range.first_line == range.last_line
|
74
80
|
return false unless line_length_enabled_at_line?(range.first_line)
|
75
81
|
|
76
|
-
range.
|
82
|
+
line = range.source_line
|
83
|
+
return false if line_length(line) <= max_line_length
|
84
|
+
|
85
|
+
too_long_line_based_on_config?(range, line)
|
86
|
+
end
|
87
|
+
|
88
|
+
def too_long_line_based_on_config?(range, line)
|
89
|
+
return false if matches_ignored_pattern?(line)
|
90
|
+
|
91
|
+
too_long = too_long_line_based_on_ignore_cop_directives?(range, line)
|
92
|
+
return too_long unless too_long == :undetermined
|
93
|
+
|
94
|
+
too_long_line_based_on_allow_uri?(line)
|
95
|
+
end
|
96
|
+
|
97
|
+
def too_long_line_based_on_ignore_cop_directives?(range, line)
|
98
|
+
if ignore_cop_directives? && directive_on_source_line?(range.line - 1)
|
99
|
+
return line_length_without_directive(line) > max_line_length
|
100
|
+
end
|
101
|
+
|
102
|
+
:undetermined
|
103
|
+
end
|
104
|
+
|
105
|
+
def too_long_line_based_on_allow_uri?(line)
|
106
|
+
if allow_uri?
|
107
|
+
uri_range = find_excessive_uri_range(line)
|
108
|
+
return false if uri_range && allowed_uri_position?(line, uri_range)
|
109
|
+
end
|
110
|
+
|
111
|
+
true
|
77
112
|
end
|
78
113
|
|
79
114
|
def line_length_enabled_at_line?(line)
|
80
115
|
processed_source.comment_config
|
81
|
-
.cop_enabled_at_line?('
|
116
|
+
.cop_enabled_at_line?('Layout/LineLength', line)
|
82
117
|
end
|
83
118
|
|
84
119
|
def named_capture_in_condition?(node)
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
# `loop do` without further modification. The reason is that a
|
59
59
|
# variable that's introduced inside a while/until loop is in scope
|
60
60
|
# outside of that loop too, but a variable that's assigned for the
|
61
|
-
# first time inside a block
|
61
|
+
# first time inside a block cannot be accessed after the block. In
|
62
62
|
# those more complicated cases we don't report an offense.
|
63
63
|
return if @variables.any? do |var|
|
64
64
|
assigned_inside_loop?(var, range) &&
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# rubocop:disable Metrics/ClassLength
|
4
3
|
module RuboCop
|
5
4
|
module Cop
|
6
5
|
module Style
|
@@ -43,7 +42,6 @@ module RuboCop
|
|
43
42
|
#
|
44
43
|
# @example EnforcedStyle: require_parentheses (default)
|
45
44
|
#
|
46
|
-
#
|
47
45
|
# # bad
|
48
46
|
# array.delete e
|
49
47
|
#
|
@@ -150,94 +148,18 @@ module RuboCop
|
|
150
148
|
include IgnoredMethods
|
151
149
|
include IgnoredPattern
|
152
150
|
|
153
|
-
|
154
|
-
|
155
|
-
def on_send(node)
|
156
|
-
case style
|
157
|
-
when :require_parentheses
|
158
|
-
add_offense_for_require_parentheses(node)
|
159
|
-
when :omit_parentheses
|
160
|
-
add_offense_for_omit_parentheses(node)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
alias on_csend on_send
|
164
|
-
alias on_super on_send
|
165
|
-
alias on_yield on_send
|
166
|
-
|
167
|
-
def autocorrect(node)
|
168
|
-
case style
|
169
|
-
when :require_parentheses
|
170
|
-
autocorrect_for_require_parentheses(node)
|
171
|
-
when :omit_parentheses
|
172
|
-
autocorrect_for_omit_parentheses(node)
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def message(_node = nil)
|
151
|
+
def initialize(*)
|
152
|
+
super
|
177
153
|
case style
|
178
154
|
when :require_parentheses
|
179
|
-
|
155
|
+
extend RequireParentheses
|
180
156
|
when :omit_parentheses
|
181
|
-
|
157
|
+
extend OmitParentheses
|
182
158
|
end
|
183
159
|
end
|
184
160
|
|
185
161
|
private
|
186
162
|
|
187
|
-
def add_offense_for_require_parentheses(node)
|
188
|
-
return if ignored_method?(node.method_name)
|
189
|
-
return if matches_ignored_pattern?(node.method_name)
|
190
|
-
return if eligible_for_parentheses_omission?(node)
|
191
|
-
return unless node.arguments? && !node.parenthesized?
|
192
|
-
|
193
|
-
add_offense(node)
|
194
|
-
end
|
195
|
-
|
196
|
-
def add_offense_for_omit_parentheses(node)
|
197
|
-
return unless node.parenthesized?
|
198
|
-
return if node.implicit_call?
|
199
|
-
return if super_call_without_arguments?(node)
|
200
|
-
return if allowed_camel_case_method_call?(node)
|
201
|
-
return if legitimate_call_with_parentheses?(node)
|
202
|
-
|
203
|
-
add_offense(node, location: node.loc.begin.join(node.loc.end))
|
204
|
-
end
|
205
|
-
|
206
|
-
def autocorrect_for_require_parentheses(node)
|
207
|
-
lambda do |corrector|
|
208
|
-
corrector.replace(args_begin(node), '(')
|
209
|
-
|
210
|
-
unless args_parenthesized?(node)
|
211
|
-
corrector.insert_after(args_end(node), ')')
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
def autocorrect_for_omit_parentheses(node)
|
217
|
-
lambda do |corrector|
|
218
|
-
if parentheses_at_the_end_of_multiline_call?(node)
|
219
|
-
corrector.replace(args_begin(node), ' \\')
|
220
|
-
else
|
221
|
-
corrector.replace(args_begin(node), ' ')
|
222
|
-
end
|
223
|
-
corrector.remove(node.loc.end)
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
def eligible_for_parentheses_omission?(node)
|
228
|
-
node.operator_method? || node.setter_method? || ignored_macro?(node)
|
229
|
-
end
|
230
|
-
|
231
|
-
def included_macros_list
|
232
|
-
cop_config.fetch('IncludedMacros', []).map(&:to_sym)
|
233
|
-
end
|
234
|
-
|
235
|
-
def ignored_macro?(node)
|
236
|
-
cop_config['IgnoreMacros'] &&
|
237
|
-
node.macro? &&
|
238
|
-
!included_macros_list.include?(node.method_name)
|
239
|
-
end
|
240
|
-
|
241
163
|
def args_begin(node)
|
242
164
|
loc = node.loc
|
243
165
|
selector =
|
@@ -257,132 +179,7 @@ module RuboCop
|
|
257
179
|
first_node = node.arguments.first
|
258
180
|
first_node.begin_type? && first_node.parenthesized_call?
|
259
181
|
end
|
260
|
-
|
261
|
-
def parentheses_at_the_end_of_multiline_call?(node)
|
262
|
-
node.multiline? &&
|
263
|
-
node.loc.begin.source_line
|
264
|
-
.gsub(TRAILING_WHITESPACE_REGEX, '')
|
265
|
-
.end_with?('(')
|
266
|
-
end
|
267
|
-
|
268
|
-
def super_call_without_arguments?(node)
|
269
|
-
node.super_type? && node.arguments.none?
|
270
|
-
end
|
271
|
-
|
272
|
-
def allowed_camel_case_method_call?(node)
|
273
|
-
node.camel_case_method? &&
|
274
|
-
(node.arguments.none? ||
|
275
|
-
cop_config['AllowParenthesesInCamelCaseMethod'])
|
276
|
-
end
|
277
|
-
|
278
|
-
def legitimate_call_with_parentheses?(node)
|
279
|
-
call_in_literals?(node) ||
|
280
|
-
call_with_ambiguous_arguments?(node) ||
|
281
|
-
call_in_logical_operators?(node) ||
|
282
|
-
call_in_optional_arguments?(node) ||
|
283
|
-
allowed_multiline_call_with_parentheses?(node) ||
|
284
|
-
allowed_chained_call_with_parentheses?(node)
|
285
|
-
end
|
286
|
-
|
287
|
-
def call_in_literals?(node)
|
288
|
-
node.parent &&
|
289
|
-
(node.parent.pair_type? ||
|
290
|
-
node.parent.array_type? ||
|
291
|
-
node.parent.range_type? ||
|
292
|
-
splat?(node.parent) ||
|
293
|
-
ternary_if?(node.parent))
|
294
|
-
end
|
295
|
-
|
296
|
-
def call_in_logical_operators?(node)
|
297
|
-
node.parent &&
|
298
|
-
(logical_operator?(node.parent) ||
|
299
|
-
node.parent.send_type? &&
|
300
|
-
node.parent.arguments.any?(&method(:logical_operator?)))
|
301
|
-
end
|
302
|
-
|
303
|
-
def call_in_optional_arguments?(node)
|
304
|
-
node.parent &&
|
305
|
-
(node.parent.optarg_type? || node.parent.kwoptarg_type?)
|
306
|
-
end
|
307
|
-
|
308
|
-
def call_with_ambiguous_arguments?(node)
|
309
|
-
call_with_braced_block?(node) ||
|
310
|
-
call_as_argument_or_chain?(node) ||
|
311
|
-
hash_literal_in_arguments?(node) ||
|
312
|
-
node.descendants.any? do |n|
|
313
|
-
ambigious_literal?(n) || logical_operator?(n) ||
|
314
|
-
call_with_braced_block?(n)
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
def call_with_braced_block?(node)
|
319
|
-
(node.send_type? || node.super_type?) &&
|
320
|
-
node.block_node && node.block_node.braces?
|
321
|
-
end
|
322
|
-
|
323
|
-
def call_as_argument_or_chain?(node)
|
324
|
-
node.parent &&
|
325
|
-
(node.parent.send_type? && !assigned_before?(node.parent, node) ||
|
326
|
-
node.parent.csend_type? || node.parent.super_type?)
|
327
|
-
end
|
328
|
-
|
329
|
-
def hash_literal_in_arguments?(node)
|
330
|
-
node.arguments.any? do |n|
|
331
|
-
hash_literal?(n) ||
|
332
|
-
n.send_type? && node.descendants.any?(&method(:hash_literal?))
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
def allowed_multiline_call_with_parentheses?(node)
|
337
|
-
cop_config['AllowParenthesesInMultilineCall'] && node.multiline?
|
338
|
-
end
|
339
|
-
|
340
|
-
def allowed_chained_call_with_parentheses?(node)
|
341
|
-
return false unless cop_config['AllowParenthesesInChaining']
|
342
|
-
|
343
|
-
previous = node.descendants.first
|
344
|
-
return false unless previous&.send_type?
|
345
|
-
|
346
|
-
previous.parenthesized? ||
|
347
|
-
allowed_chained_call_with_parentheses?(previous)
|
348
|
-
end
|
349
|
-
|
350
|
-
def ambigious_literal?(node)
|
351
|
-
splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) ||
|
352
|
-
unary_literal?(node)
|
353
|
-
end
|
354
|
-
|
355
|
-
def splat?(node)
|
356
|
-
node.splat_type? || node.kwsplat_type? || node.block_pass_type?
|
357
|
-
end
|
358
|
-
|
359
|
-
def ternary_if?(node)
|
360
|
-
node.if_type? && node.ternary?
|
361
|
-
end
|
362
|
-
|
363
|
-
def logical_operator?(node)
|
364
|
-
(node.and_type? || node.or_type?) && node.logical_operator?
|
365
|
-
end
|
366
|
-
|
367
|
-
def hash_literal?(node)
|
368
|
-
node.hash_type? && node.braces?
|
369
|
-
end
|
370
|
-
|
371
|
-
def regexp_slash_literal?(node)
|
372
|
-
node.regexp_type? && node.loc.begin.source == '/'
|
373
|
-
end
|
374
|
-
|
375
|
-
def unary_literal?(node)
|
376
|
-
node.numeric_type? && node.sign? ||
|
377
|
-
node.parent&.send_type? && node.parent&.unary_operation?
|
378
|
-
end
|
379
|
-
|
380
|
-
def assigned_before?(node, target)
|
381
|
-
node.assignment? &&
|
382
|
-
node.loc.operator.begin < target.loc.begin
|
383
|
-
end
|
384
182
|
end
|
385
183
|
end
|
386
184
|
end
|
387
185
|
end
|
388
|
-
# rubocop:enable Metrics/ClassLength
|