rubocop 1.41.1 → 1.45.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +97 -31
- data/lib/rubocop/cli.rb +55 -9
- data/lib/rubocop/config.rb +7 -7
- data/lib/rubocop/config_loader.rb +12 -15
- data/lib/rubocop/config_loader_resolver.rb +8 -5
- data/lib/rubocop/cop/base.rb +89 -70
- data/lib/rubocop/cop/commissioner.rb +8 -2
- data/lib/rubocop/cop/cop.rb +51 -31
- data/lib/rubocop/cop/corrector.rb +30 -10
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
- data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
- data/lib/rubocop/cop/layout/class_structure.rb +31 -23
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
- data/lib/rubocop/cop/layout/indentation_style.rb +4 -1
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -2
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -27
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
- data/lib/rubocop/cop/lint/else_layout.rb +2 -6
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
- data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
- data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
- data/lib/rubocop/cop/lint/void.rb +19 -10
- data/lib/rubocop/cop/metrics/block_length.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +57 -23
- data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/registry.rb +34 -29
- data/lib/rubocop/cop/security/compound_hash.rb +2 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
- data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +22 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +6 -0
- data/lib/rubocop/cop/style/guard_clause.rb +11 -7
- data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
- data/lib/rubocop/cop/style/map_to_set.rb +61 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
- data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
- data/lib/rubocop/cop/style/missing_else.rb +13 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
- data/lib/rubocop/cop/style/operator_method_call.rb +16 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
- data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +4 -2
- data/lib/rubocop/cop/style/require_order.rb +6 -11
- data/lib/rubocop/cop/style/select_by_regexp.rb +6 -2
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +24 -2
- data/lib/rubocop/cop/style/signal_exception.rb +8 -6
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
- data/lib/rubocop/cop/style/word_array.rb +42 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
- data/lib/rubocop/cop/style/yoda_expression.rb +90 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
- data/lib/rubocop/cop/team.rb +48 -43
- data/lib/rubocop/cop/variable_force/scope.rb +3 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
- data/lib/rubocop/cop/variable_force.rb +1 -4
- data/lib/rubocop/formatter.rb +0 -1
- data/lib/rubocop/options.rb +22 -1
- data/lib/rubocop/path_util.rb +17 -7
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +6 -4
- data/lib/rubocop/runner.rb +50 -7
- data/lib/rubocop/server/cache.rb +10 -3
- data/lib/rubocop/server/cli.rb +37 -18
- data/lib/rubocop/server/client_command/exec.rb +1 -1
- data/lib/rubocop/server/client_command/start.rb +6 -1
- data/lib/rubocop/server/core.rb +23 -8
- data/lib/rubocop/target_ruby.rb +0 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +8 -0
- metadata +21 -33
@@ -13,8 +13,7 @@ module RuboCop
|
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
# rubocop:disable Metrics/PerceivedComplexity
|
17
|
-
def omit_parentheses(node)
|
16
|
+
def omit_parentheses(node) # rubocop:disable Metrics/PerceivedComplexity
|
18
17
|
return unless node.parenthesized?
|
19
18
|
return if inside_endless_method_def?(node)
|
20
19
|
return if require_parentheses_for_hash_value_omission?(node)
|
@@ -28,7 +27,6 @@ module RuboCop
|
|
28
27
|
autocorrect(corrector, node)
|
29
28
|
end
|
30
29
|
end
|
31
|
-
# rubocop:enable Metrics/PerceivedComplexity
|
32
30
|
|
33
31
|
def autocorrect(corrector, node)
|
34
32
|
if parentheses_at_the_end_of_multiline_call?(node)
|
@@ -90,7 +88,7 @@ module RuboCop
|
|
90
88
|
.end_with?('(')
|
91
89
|
end
|
92
90
|
|
93
|
-
def legitimate_call_with_parentheses?(node)
|
91
|
+
def legitimate_call_with_parentheses?(node) # rubocop:disable Metrics/PerceivedComplexity
|
94
92
|
call_in_literals?(node) ||
|
95
93
|
call_with_ambiguous_arguments?(node) ||
|
96
94
|
call_in_logical_operators?(node) ||
|
@@ -98,24 +96,28 @@ module RuboCop
|
|
98
96
|
call_in_single_line_inheritance?(node) ||
|
99
97
|
allowed_multiline_call_with_parentheses?(node) ||
|
100
98
|
allowed_chained_call_with_parentheses?(node) ||
|
101
|
-
assignment_in_condition?(node)
|
99
|
+
assignment_in_condition?(node) ||
|
100
|
+
forwards_anonymous_rest_arguments?(node)
|
102
101
|
end
|
103
102
|
|
104
103
|
def call_in_literals?(node)
|
105
|
-
node.parent
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
104
|
+
parent = node.parent&.block_type? ? node.parent.parent : node.parent
|
105
|
+
return unless parent
|
106
|
+
|
107
|
+
parent.pair_type? ||
|
108
|
+
parent.array_type? ||
|
109
|
+
parent.range_type? ||
|
110
|
+
splat?(parent) ||
|
111
|
+
ternary_if?(parent)
|
111
112
|
end
|
112
113
|
|
113
114
|
def call_in_logical_operators?(node)
|
114
115
|
parent = node.parent&.block_type? ? node.parent.parent : node.parent
|
115
|
-
parent
|
116
|
-
|
116
|
+
return unless parent
|
117
|
+
|
118
|
+
logical_operator?(parent) ||
|
117
119
|
(parent.send_type? &&
|
118
|
-
parent.arguments.any? { |argument| logical_operator?(argument) })
|
120
|
+
parent.arguments.any? { |argument| logical_operator?(argument) })
|
119
121
|
end
|
120
122
|
|
121
123
|
def call_in_optional_arguments?(node)
|
@@ -213,6 +215,13 @@ module RuboCop
|
|
213
215
|
|
214
216
|
parent.assignment? && (grandparent.conditional? || grandparent.when_type?)
|
215
217
|
end
|
218
|
+
|
219
|
+
def forwards_anonymous_rest_arguments?(node)
|
220
|
+
return false unless (last_argument = node.last_argument)
|
221
|
+
return true if last_argument.forwarded_restarg_type?
|
222
|
+
|
223
|
+
last_argument.hash_type? && last_argument.children.first&.forwarded_kwrestarg_type?
|
224
|
+
end
|
216
225
|
end
|
217
226
|
# rubocop:enable Metrics/ModuleLength, Metrics/CyclomaticComplexity
|
218
227
|
end
|
@@ -10,7 +10,9 @@ module RuboCop
|
|
10
10
|
#
|
11
11
|
# 1. Endless methods
|
12
12
|
# 2. Argument lists containing a `forward-arg` (`...`)
|
13
|
-
# 3. Argument lists containing an anonymous
|
13
|
+
# 3. Argument lists containing an anonymous rest arguments forwarding (`*`)
|
14
|
+
# 4. Argument lists containing an anonymous keyword rest arguments forwarding (`**`)
|
15
|
+
# 5. Argument lists containing an anonymous block forwarding (`&`)
|
14
16
|
#
|
15
17
|
# Removing the parens would be a syntax error here.
|
16
18
|
#
|
@@ -130,9 +132,11 @@ module RuboCop
|
|
130
132
|
# Regardless of style, parentheses are necessary for:
|
131
133
|
# 1. Endless methods
|
132
134
|
# 2. Argument lists containing a `forward-arg` (`...`)
|
133
|
-
# 3. Argument lists containing an anonymous
|
135
|
+
# 3. Argument lists containing an anonymous rest arguments forwarding (`*`)
|
136
|
+
# 4. Argument lists containing an anonymous keyword rest arguments forwarding (`**`)
|
137
|
+
# 5. Argument lists containing an anonymous block forwarding (`&`)
|
134
138
|
# Removing the parens would be a syntax error here.
|
135
|
-
node.endless? ||
|
139
|
+
node.endless? || anonymous_arguments?(node)
|
136
140
|
end
|
137
141
|
|
138
142
|
def require_parentheses?(args)
|
@@ -162,7 +166,10 @@ module RuboCop
|
|
162
166
|
end
|
163
167
|
end
|
164
168
|
|
165
|
-
def
|
169
|
+
def anonymous_arguments?(node)
|
170
|
+
return true if node.arguments.any? do |arg|
|
171
|
+
arg.forward_arg_type? || arg.restarg_type? || arg.kwrestarg_type?
|
172
|
+
end
|
166
173
|
return false unless (last_argument = node.arguments.last)
|
167
174
|
|
168
175
|
last_argument.blockarg_type? && last_argument.name.nil?
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Enforces the use of `max` or `min` instead of comparison for greater or less.
|
7
|
+
#
|
8
|
+
# NOTE: It can be used if you want to present limit or threshold in Ruby 2.7+.
|
9
|
+
# That it is slow though. So autocorrection will apply generic `max` or `min`:
|
10
|
+
#
|
11
|
+
# [source,ruby]
|
12
|
+
# ----
|
13
|
+
# a.clamp(b..) # Same as `[a, b].max`
|
14
|
+
# a.clamp(..b) # Same as `[a, b].min`
|
15
|
+
# ----
|
16
|
+
#
|
17
|
+
# @safety
|
18
|
+
# This cop is unsafe because even if a value has `<` or `>` method,
|
19
|
+
# it is not necessarily `Comparable`.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
# a > b ? a : b
|
25
|
+
# a >= b ? a : b
|
26
|
+
#
|
27
|
+
# # good
|
28
|
+
# [a, b].max
|
29
|
+
#
|
30
|
+
# # bad
|
31
|
+
# a < b ? a : b
|
32
|
+
# a <= b ? a : b
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# [a, b].min
|
36
|
+
#
|
37
|
+
class MinMaxComparison < Base
|
38
|
+
extend AutoCorrector
|
39
|
+
include RangeHelp
|
40
|
+
|
41
|
+
MSG = 'Use `%<prefer>s` instead.'
|
42
|
+
GRATER_OPERATORS = %i[> >=].freeze
|
43
|
+
LESS_OPERATORS = %i[< <=].freeze
|
44
|
+
COMPARISON_OPERATORS = GRATER_OPERATORS + LESS_OPERATORS
|
45
|
+
|
46
|
+
def on_if(node)
|
47
|
+
lhs, operator, rhs = *node.condition
|
48
|
+
return unless COMPARISON_OPERATORS.include?(operator)
|
49
|
+
|
50
|
+
if_branch = node.if_branch
|
51
|
+
else_branch = node.else_branch
|
52
|
+
preferred_method = preferred_method(operator, lhs, rhs, if_branch, else_branch)
|
53
|
+
return unless preferred_method
|
54
|
+
|
55
|
+
replacement = "[#{lhs.source}, #{rhs.source}].#{preferred_method}"
|
56
|
+
|
57
|
+
add_offense(node, message: format(MSG, prefer: replacement)) do |corrector|
|
58
|
+
autocorrect(corrector, node, replacement)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def preferred_method(operator, lhs, rhs, if_branch, else_branch)
|
65
|
+
if lhs == if_branch && rhs == else_branch
|
66
|
+
GRATER_OPERATORS.include?(operator) ? 'max' : 'min'
|
67
|
+
elsif lhs == else_branch && rhs == if_branch
|
68
|
+
LESS_OPERATORS.include?(operator) ? 'max' : 'min'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def autocorrect(corrector, node, replacement)
|
73
|
+
if node.elsif?
|
74
|
+
corrector.remove(range_between(node.parent.loc.else.begin_pos, node.loc.else.begin_pos))
|
75
|
+
corrector.replace(node.else_branch, replacement)
|
76
|
+
else
|
77
|
+
corrector.replace(node, replacement)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -99,6 +99,7 @@ module RuboCop
|
|
99
99
|
class MissingElse < Base
|
100
100
|
include OnNormalIfUnless
|
101
101
|
include ConfigurableEnforcedStyle
|
102
|
+
extend AutoCorrector
|
102
103
|
|
103
104
|
MSG = '`%<type>s` condition requires an `else`-clause.'
|
104
105
|
MSG_NIL = '`%<type>s` condition requires an `else`-clause with `nil` in it.'
|
@@ -126,7 +127,9 @@ module RuboCop
|
|
126
127
|
def check(node)
|
127
128
|
return if node.else?
|
128
129
|
|
129
|
-
add_offense(node, message: format(message_template, type: node.type))
|
130
|
+
add_offense(node, message: format(message_template, type: node.type)) do |corrector|
|
131
|
+
autocorrect(corrector, node)
|
132
|
+
end
|
130
133
|
end
|
131
134
|
|
132
135
|
def message_template
|
@@ -140,6 +143,15 @@ module RuboCop
|
|
140
143
|
end
|
141
144
|
end
|
142
145
|
|
146
|
+
def autocorrect(corrector, node)
|
147
|
+
case empty_else_style
|
148
|
+
when :empty
|
149
|
+
corrector.insert_before(node.loc.end, 'else; nil; ')
|
150
|
+
when :nil
|
151
|
+
corrector.insert_before(node.loc.end, 'else; ')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
143
155
|
def if_style?
|
144
156
|
style == :if
|
145
157
|
end
|
@@ -40,10 +40,6 @@ module RuboCop
|
|
40
40
|
[condition, indented_body, indented_end].join("\n")
|
41
41
|
end
|
42
42
|
|
43
|
-
def configured_indentation_width
|
44
|
-
super || 2
|
45
|
-
end
|
46
|
-
|
47
43
|
def indented_body(body, node)
|
48
44
|
body_source = "#{offset(node)}#{body.source}"
|
49
45
|
body_source.each_line.map do |line|
|
@@ -31,6 +31,7 @@ module RuboCop
|
|
31
31
|
# baz
|
32
32
|
# )
|
33
33
|
class MultilineMemoization < Base
|
34
|
+
include Alignment
|
34
35
|
include ConfigurableEnforcedStyle
|
35
36
|
extend AutoCorrector
|
36
37
|
|
@@ -75,11 +76,10 @@ module RuboCop
|
|
75
76
|
end
|
76
77
|
|
77
78
|
def keyword_begin_str(node, node_buf)
|
78
|
-
indent = config.for_cop('Layout/IndentationWidth')['Width'] || 2
|
79
79
|
if node_buf.source[node.loc.begin.end_pos] == "\n"
|
80
80
|
'begin'
|
81
81
|
else
|
82
|
-
"begin\n#{' ' * (node.loc.column +
|
82
|
+
"begin\n#{' ' * (node.loc.column + configured_indentation_width)}"
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -34,6 +34,7 @@ module RuboCop
|
|
34
34
|
# return cond ? b : c
|
35
35
|
#
|
36
36
|
class MultilineTernaryOperator < Base
|
37
|
+
include CommentsHelp
|
37
38
|
extend AutoCorrector
|
38
39
|
|
39
40
|
MSG_IF = 'Avoid multi-line ternary operators, use `if` or `unless` instead.'
|
@@ -46,9 +47,7 @@ module RuboCop
|
|
46
47
|
message = enforce_single_line_ternary_operator?(node) ? MSG_SINGLE_LINE : MSG_IF
|
47
48
|
|
48
49
|
add_offense(node, message: message) do |corrector|
|
49
|
-
|
50
|
-
|
51
|
-
corrector.replace(node, replacement(node))
|
50
|
+
autocorrect(corrector, node)
|
52
51
|
end
|
53
52
|
end
|
54
53
|
|
@@ -58,6 +57,16 @@ module RuboCop
|
|
58
57
|
node.ternary? && node.multiline?
|
59
58
|
end
|
60
59
|
|
60
|
+
def autocorrect(corrector, node)
|
61
|
+
return unless offense?(node)
|
62
|
+
|
63
|
+
corrector.replace(node, replacement(node))
|
64
|
+
return unless (parent = node.parent)
|
65
|
+
return unless (comments_in_condition = comments_in_condition(node))
|
66
|
+
|
67
|
+
corrector.insert_before(parent, comments_in_condition)
|
68
|
+
end
|
69
|
+
|
61
70
|
def replacement(node)
|
62
71
|
if enforce_single_line_ternary_operator?(node)
|
63
72
|
"#{node.condition.source} ? #{node.if_branch.source} : #{node.else_branch.source}"
|
@@ -72,6 +81,12 @@ module RuboCop
|
|
72
81
|
end
|
73
82
|
end
|
74
83
|
|
84
|
+
def comments_in_condition(node)
|
85
|
+
comments_in_range(node).map do |comment|
|
86
|
+
"#{comment.loc.expression.source}\n"
|
87
|
+
end.join
|
88
|
+
end
|
89
|
+
|
75
90
|
def enforce_single_line_ternary_operator?(node)
|
76
91
|
SINGLE_LINE_TYPES.include?(node.parent&.type) && !use_assignment_method?(node.parent)
|
77
92
|
end
|
@@ -103,11 +103,7 @@ module RuboCop
|
|
103
103
|
if node.if_branch.nil?
|
104
104
|
corrector.remove(range_by_whole_lines(node.loc.else, include_final_newline: true))
|
105
105
|
else
|
106
|
-
if_range
|
107
|
-
else_range = else_range(node)
|
108
|
-
|
109
|
-
corrector.replace(if_range, else_range.source)
|
110
|
-
corrector.replace(else_range, if_range.source)
|
106
|
+
corrector.swap(if_range(node), else_range(node))
|
111
107
|
end
|
112
108
|
end
|
113
109
|
|
@@ -12,10 +12,11 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# @example Max: 1 (default)
|
14
14
|
# # bad
|
15
|
-
#
|
15
|
+
# use_multiple_numbered_parameters { _1.call(_2, _3, _4) }
|
16
16
|
#
|
17
17
|
# # good
|
18
|
-
#
|
18
|
+
# array.each { use_array_element_as_numbered_parameter(_1) }
|
19
|
+
# hash.each { use_only_hash_value_as_numbered_parameter(_2) }
|
19
20
|
class NumberedParametersLimit < Base
|
20
21
|
extend TargetRubyVersion
|
21
22
|
extend ExcludeLimit
|
@@ -26,9 +27,10 @@ module RuboCop
|
|
26
27
|
exclude_limit 'Max'
|
27
28
|
|
28
29
|
MSG = 'Avoid using more than %<max>i numbered %<parameter>s; %<count>i detected.'
|
30
|
+
NUMBERED_PARAMETER_PATTERN = /\A_[1-9]\z/.freeze
|
29
31
|
|
30
32
|
def on_numblock(node)
|
31
|
-
|
33
|
+
param_count = numbered_parameter_nodes(node).uniq.count
|
32
34
|
return if param_count <= max_count
|
33
35
|
|
34
36
|
parameter = max_count > 1 ? 'parameters' : 'parameter'
|
@@ -38,6 +40,12 @@ module RuboCop
|
|
38
40
|
|
39
41
|
private
|
40
42
|
|
43
|
+
def numbered_parameter_nodes(node)
|
44
|
+
node.each_descendant(:lvar).select do |lvar_node|
|
45
|
+
lvar_node.source.match?(NUMBERED_PARAMETER_PATTERN)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
41
49
|
def max_count
|
42
50
|
max = cop_config.fetch('Max', DEFAULT_MAX_VALUE)
|
43
51
|
|
@@ -31,6 +31,7 @@ module RuboCop
|
|
31
31
|
# baz
|
32
32
|
# end
|
33
33
|
class OneLineConditional < Base
|
34
|
+
include Alignment
|
34
35
|
include ConfigurableEnforcedStyle
|
35
36
|
include OnNormalIfUnless
|
36
37
|
extend AutoCorrector
|
@@ -57,7 +58,7 @@ module RuboCop
|
|
57
58
|
|
58
59
|
def autocorrect(corrector, node)
|
59
60
|
if always_multiline? || cannot_replace_to_ternary?(node)
|
60
|
-
IfThenCorrector.new(node, indentation:
|
61
|
+
IfThenCorrector.new(node, indentation: configured_indentation_width).call(corrector)
|
61
62
|
else
|
62
63
|
corrector.replace(node, ternary_correction(node))
|
63
64
|
end
|
@@ -67,7 +68,7 @@ module RuboCop
|
|
67
68
|
replaced_node = ternary_replacement(node)
|
68
69
|
|
69
70
|
return replaced_node unless node.parent
|
70
|
-
return "(#{replaced_node})" if
|
71
|
+
return "(#{replaced_node})" if node.parent.operator_keyword?
|
71
72
|
return "(#{replaced_node})" if node.parent.send_type? && node.parent.operator_method?
|
72
73
|
|
73
74
|
replaced_node
|
@@ -116,10 +117,6 @@ module RuboCop
|
|
116
117
|
|
117
118
|
node.respond_to?(:arguments?) && node.arguments? && !node.parenthesized_call?
|
118
119
|
end
|
119
|
-
|
120
|
-
def indentation_width
|
121
|
-
@config.for_cop('Layout/IndentationWidth')['Width']
|
122
|
-
end
|
123
120
|
end
|
124
121
|
end
|
125
122
|
end
|
@@ -25,10 +25,10 @@ module RuboCop
|
|
25
25
|
|
26
26
|
def on_send(node)
|
27
27
|
return unless (dot = node.loc.dot)
|
28
|
-
return if node.receiver.const_type?
|
28
|
+
return if node.receiver.const_type? || !node.arguments.one?
|
29
29
|
|
30
30
|
_lhs, _op, rhs = *node
|
31
|
-
return if rhs
|
31
|
+
return if !rhs || method_call_with_parenthesized_arg?(rhs) || anonymous_forwarding?(rhs)
|
32
32
|
|
33
33
|
add_offense(dot) do |corrector|
|
34
34
|
wrap_in_parentheses_if_chained(corrector, node)
|
@@ -38,6 +38,20 @@ module RuboCop
|
|
38
38
|
|
39
39
|
private
|
40
40
|
|
41
|
+
# Checks for an acceptable case of `foo.+(bar).baz`.
|
42
|
+
def method_call_with_parenthesized_arg?(argument)
|
43
|
+
return false unless argument.parent.parent&.send_type?
|
44
|
+
|
45
|
+
argument.children.first && argument.parent.parenthesized?
|
46
|
+
end
|
47
|
+
|
48
|
+
def anonymous_forwarding?(argument)
|
49
|
+
return true if argument.forwarded_args_type? || argument.forwarded_restarg_type?
|
50
|
+
return true if argument.hash_type? && argument.children.first&.forwarded_kwrestarg_type?
|
51
|
+
|
52
|
+
argument.block_pass_type? && argument.source == '&'
|
53
|
+
end
|
54
|
+
|
41
55
|
def wrap_in_parentheses_if_chained(corrector, node)
|
42
56
|
return unless node.parent&.call_type?
|
43
57
|
|
@@ -172,7 +172,9 @@ module RuboCop
|
|
172
172
|
end
|
173
173
|
|
174
174
|
def modifier_statement?(node)
|
175
|
-
|
175
|
+
return false unless node
|
176
|
+
|
177
|
+
node.basic_conditional? && node.modifier_form?
|
176
178
|
end
|
177
179
|
|
178
180
|
# An internal class for correcting parallel assignment
|
@@ -36,6 +36,9 @@ module RuboCop
|
|
36
36
|
|
37
37
|
MSG = 'Use double pipes `||` instead.'
|
38
38
|
REDUNDANT_CONDITION = 'This condition is not needed.'
|
39
|
+
ARGUMENT_WITH_OPERATOR_TYPES = %i[
|
40
|
+
splat block_pass forwarded_restarg forwarded_kwrestarg forwarded_args
|
41
|
+
].freeze
|
39
42
|
|
40
43
|
def on_if(node)
|
41
44
|
return if node.elsif_conditional?
|
@@ -150,13 +153,25 @@ module RuboCop
|
|
150
153
|
end
|
151
154
|
|
152
155
|
def single_argument_method?(node)
|
153
|
-
node.send_type?
|
156
|
+
return false if !node.send_type? || node.method?(:[]) || !node.arguments.one?
|
157
|
+
|
158
|
+
!argument_with_operator?(node.first_argument)
|
154
159
|
end
|
155
160
|
|
156
161
|
def same_method?(if_branch, else_branch)
|
157
162
|
if_branch.method?(else_branch.method_name) && if_branch.receiver == else_branch.receiver
|
158
163
|
end
|
159
164
|
|
165
|
+
# If the argument is using an operator, it is an invalid syntax.
|
166
|
+
# e.g. `foo || *bar`, `foo || **bar`, and `foo || &bar`.
|
167
|
+
def argument_with_operator?(argument)
|
168
|
+
return true if ARGUMENT_WITH_OPERATOR_TYPES.include?(argument.type)
|
169
|
+
return false unless argument.hash_type?
|
170
|
+
return false unless (node = argument.children.first)
|
171
|
+
|
172
|
+
node.kwsplat_type? || node.forwarded_kwrestarg_type?
|
173
|
+
end
|
174
|
+
|
160
175
|
def if_source(if_branch, arithmetic_operation)
|
161
176
|
if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
|
162
177
|
if_branch.source.delete_suffix(')')
|
@@ -18,21 +18,27 @@ module RuboCop
|
|
18
18
|
|
19
19
|
MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
|
20
20
|
|
21
|
-
# @!method double_splat_hash_braces?(node)
|
22
|
-
def_node_matcher :double_splat_hash_braces?, <<~PATTERN
|
23
|
-
(hash (kwsplat (hash ...)))
|
24
|
-
PATTERN
|
25
|
-
|
26
21
|
def on_hash(node)
|
27
22
|
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
|
23
|
+
return unless (parent = node.parent)
|
24
|
+
return unless parent.kwsplat_type?
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
corrector.replace(grandparent, node.pairs.map(&:source).join(', '))
|
26
|
+
add_offense(parent) do |corrector|
|
27
|
+
corrector.remove(parent.loc.operator)
|
28
|
+
corrector.remove(opening_brace(node))
|
29
|
+
corrector.remove(closing_brace(node))
|
34
30
|
end
|
35
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def opening_brace(node)
|
36
|
+
node.loc.begin.join(node.children.first.loc.expression.begin)
|
37
|
+
end
|
38
|
+
|
39
|
+
def closing_brace(node)
|
40
|
+
node.children.last.loc.expression.end.join(node.loc.end)
|
41
|
+
end
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for redundant heredoc delimiter quotes.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# do_something(<<~'EOS')
|
12
|
+
# no string interpolation style text
|
13
|
+
# EOS
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# do_something(<<~EOS)
|
17
|
+
# no string interpolation style text
|
18
|
+
# EOS
|
19
|
+
#
|
20
|
+
# do_something(<<~'EOS')
|
21
|
+
# #{string_interpolation_style_text_not_evaluated}
|
22
|
+
# EOS
|
23
|
+
#
|
24
|
+
# do_something(<<~'EOS')
|
25
|
+
# Preserve \
|
26
|
+
# newlines
|
27
|
+
# EOS
|
28
|
+
#
|
29
|
+
class RedundantHeredocDelimiterQuotes < Base
|
30
|
+
include Heredoc
|
31
|
+
extend AutoCorrector
|
32
|
+
|
33
|
+
MSG = 'Remove the redundant heredoc delimiter quotes, use `%<replacement>s` instead.'
|
34
|
+
STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN = /#(\{|@|\$)|\\/.freeze
|
35
|
+
|
36
|
+
def on_heredoc(node)
|
37
|
+
return if need_heredoc_delimiter_quotes?(node)
|
38
|
+
|
39
|
+
replacement = "#{heredoc_type(node)}#{delimiter_string(node)}"
|
40
|
+
|
41
|
+
add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
|
42
|
+
corrector.replace(node, replacement)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def need_heredoc_delimiter_quotes?(node)
|
49
|
+
heredoc_delimiter = node.source.delete(heredoc_type(node))
|
50
|
+
return true unless heredoc_delimiter.start_with?("'", '"')
|
51
|
+
|
52
|
+
node.loc.heredoc_end.source.strip.match?(/\W/) ||
|
53
|
+
node.loc.heredoc_body.source.match?(STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -77,7 +77,8 @@ module RuboCop
|
|
77
77
|
# but it's not necessry to escape hyphen if it's the first or last character
|
78
78
|
# within the character class. This method checks if that's the case.
|
79
79
|
# e.g. "[0-9\\-]" or "[\\-0-9]" would return true
|
80
|
-
node.source[index] == '[' ||
|
80
|
+
contents_range(node).source[index - 1] == '[' ||
|
81
|
+
contents_range(node).source[index + 2] == ']'
|
81
82
|
end
|
82
83
|
|
83
84
|
def delimiter?(node, char)
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
private
|
59
59
|
|
60
60
|
def message(range)
|
61
|
-
format(MSG, char: range.source
|
61
|
+
format(MSG, char: range.source[-1])
|
62
62
|
end
|
63
63
|
|
64
64
|
def str_contents_range(node)
|
@@ -76,6 +76,7 @@ module RuboCop
|
|
76
76
|
node.loc.to_hash.key?(:begin) && !node.loc.begin.nil?
|
77
77
|
end
|
78
78
|
|
79
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
79
80
|
def allowed_escape?(node, range)
|
80
81
|
escaped = range.source[(1..-1)]
|
81
82
|
|
@@ -88,13 +89,14 @@ module RuboCop
|
|
88
89
|
# with different versions of Ruby so that e.g. /\d/ != /d/
|
89
90
|
return true if /[\n\\[[:alnum:]]]/.match?(escaped[0])
|
90
91
|
|
91
|
-
return true if escaped[0] == ' ' && percent_array_literal?(node)
|
92
|
+
return true if escaped[0] == ' ' && (percent_array_literal?(node) || node.heredoc?)
|
92
93
|
|
93
94
|
return true if disabling_interpolation?(range)
|
94
95
|
return true if delimiter?(node, escaped[0])
|
95
96
|
|
96
97
|
false
|
97
98
|
end
|
99
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
98
100
|
|
99
101
|
def interpolation_not_enabled?(node)
|
100
102
|
single_quoted?(node) ||
|