rubocop 1.92.1 → 1.92.3
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/lib/rubocop/#directive_comment.rb# +224 -0
- data/lib/rubocop/cache_config.rb +3 -0
- data/lib/rubocop/cli.rb +5 -4
- data/lib/rubocop/config.rb +16 -4
- data/lib/rubocop/config_obsoletion.rb +9 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
- data/lib/rubocop/cop/layout/class_structure.rb +91 -13
- data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
- data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +15 -7
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
- data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +23 -5
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +6 -6
- data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
- data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +3 -0
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
- data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +11 -4
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +26 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -1
- data/lib/rubocop/cop/lint/void.rb +20 -9
- data/lib/rubocop/cop/message_annotator.rb +6 -8
- data/lib/rubocop/cop/mixin/comments_help.rb +2 -2
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -18
- data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +17 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
- data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
- data/lib/rubocop/cop/mixin/range_help.rb +13 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
- data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/and_or.rb +41 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
- data/lib/rubocop/cop/style/case_equality.rb +14 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
- data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
- data/lib/rubocop/cop/style/data_inheritance.rb +4 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +16 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +64 -16
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/float_division.rb +16 -1
- data/lib/rubocop/cop/style/for.rb +59 -4
- data/lib/rubocop/cop/style/guard_clause.rb +56 -11
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
- data/lib/rubocop/cop/style/hash_lookup_method.rb +21 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
- data/lib/rubocop/cop/style/lambda.rb +13 -5
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +12 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -10
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
- data/lib/rubocop/cop/style/quoted_symbols.rb +5 -7
- data/lib/rubocop/cop/style/raise_args.rb +66 -7
- data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
- data/lib/rubocop/cop/style/redundant_each.rb +8 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
- data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
- data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
- data/lib/rubocop/cop/style/redundant_self.rb +9 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
- data/lib/rubocop/cop/style/single_line_methods.rb +26 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +28 -8
- data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- data/lib/rubocop/cop/style/word_array.rb +1 -3
- data/lib/rubocop/cop/util.rb +2 -0
- data/lib/rubocop/cop/variable_force.rb +14 -1
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +13 -6
- data/lib/rubocop/plugin/configuration_integrator.rb +24 -3
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/runner.rb +6 -1
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +1 -1
- metadata +4 -3
|
@@ -44,6 +44,10 @@ module RuboCop
|
|
|
44
44
|
# # good
|
|
45
45
|
# hash.fetch(key)
|
|
46
46
|
#
|
|
47
|
+
# # good - `Array[]` and `Hash[]` are constructors
|
|
48
|
+
# Array[element]
|
|
49
|
+
# Hash[pairs]
|
|
50
|
+
#
|
|
47
51
|
# @example AllowedReceivers: ['Rails.cache']
|
|
48
52
|
# # good
|
|
49
53
|
# Rails.cache.fetch(name, options) { block }
|
|
@@ -61,6 +65,9 @@ module RuboCop
|
|
|
61
65
|
# @!method env_const?(node)
|
|
62
66
|
def_node_matcher :env_const?, '(const {nil? cbase} :ENV)'
|
|
63
67
|
|
|
68
|
+
# @!method array_or_hash_const?(node)
|
|
69
|
+
def_node_matcher :array_or_hash_const?, '(const {nil? cbase} {:Array :Hash})'
|
|
70
|
+
|
|
64
71
|
def on_send(node)
|
|
65
72
|
return if (receiver = node.receiver) && allowed_receiver?(receiver)
|
|
66
73
|
return if part_of_ignored_node?(node)
|
|
@@ -90,9 +97,19 @@ module RuboCop
|
|
|
90
97
|
end
|
|
91
98
|
|
|
92
99
|
def exempt_from_brackets?(node)
|
|
100
|
+
return true if forwarded_or_splatted?(node.first_argument)
|
|
101
|
+
|
|
93
102
|
comment_before_dot?(node) || env_fetch_preferred?(node.receiver)
|
|
94
103
|
end
|
|
95
104
|
|
|
105
|
+
def forwarded_or_splatted?(argument)
|
|
106
|
+
if argument.hash_type? && !argument.braces?
|
|
107
|
+
argument.children.any? { |child| child.type?(:kwsplat, :forwarded_kwrestarg) }
|
|
108
|
+
else
|
|
109
|
+
argument.type?(:forwarded_args, :forwarded_restarg, :splat, :block_pass)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
96
113
|
def comment_before_dot?(node)
|
|
97
114
|
processed_source.contains_comment?(node.receiver.source_range.end.join(node.loc.dot))
|
|
98
115
|
end
|
|
@@ -102,8 +119,10 @@ module RuboCop
|
|
|
102
119
|
end
|
|
103
120
|
|
|
104
121
|
def offense_for_fetch?(node)
|
|
105
|
-
style
|
|
106
|
-
|
|
122
|
+
return false if style != :fetch || !node.method?(:[]) || !node.arguments.one?
|
|
123
|
+
return false if array_or_hash_const?(node.receiver)
|
|
124
|
+
|
|
125
|
+
!compound_assignment_target?(node)
|
|
107
126
|
end
|
|
108
127
|
|
|
109
128
|
def compound_assignment_target?(node)
|
|
@@ -144,9 +144,10 @@ module RuboCop
|
|
|
144
144
|
|
|
145
145
|
# Omitting the last hash value of a call without parentheses adds the parentheses,
|
|
146
146
|
# which `Style/MethodCallWithArgsParentheses` and `Style/SuperWithArgsParentheses`
|
|
147
|
-
# would add again in the same pass.
|
|
147
|
+
# would add again in the same pass. Likewise, `Style/RedundantReturn` would add again
|
|
148
|
+
# the braces wrapped around a braceless hash returned with `return`.
|
|
148
149
|
def self.autocorrect_incompatible_with
|
|
149
|
-
[Style::MethodCallWithArgsParentheses, Style::SuperWithArgsParentheses]
|
|
150
|
+
[Style::MethodCallWithArgsParentheses, Style::RedundantReturn, Style::SuperWithArgsParentheses]
|
|
150
151
|
end
|
|
151
152
|
|
|
152
153
|
def on_hash(node)
|
|
@@ -109,10 +109,14 @@ module RuboCop
|
|
|
109
109
|
# end
|
|
110
110
|
class IdenticalConditionalBranches < Base
|
|
111
111
|
include RangeHelp
|
|
112
|
+
include RescueNode
|
|
112
113
|
extend AutoCorrector
|
|
113
114
|
|
|
114
115
|
MSG = 'Move `%<source>s` out of the conditional.'
|
|
115
116
|
|
|
117
|
+
STATEMENT_BODY_TYPES = %i[kwbegin block numblock itblock ensure].freeze
|
|
118
|
+
VOID_VALUE_TYPES = %i[return break next redo retry].freeze
|
|
119
|
+
|
|
116
120
|
def on_if(node)
|
|
117
121
|
return if node.elsif?
|
|
118
122
|
|
|
@@ -193,10 +197,12 @@ module RuboCop
|
|
|
193
197
|
return if expressions.any?(&:nil?)
|
|
194
198
|
|
|
195
199
|
inserted_expression = false
|
|
200
|
+
hoistable = hoistable?(node)
|
|
196
201
|
|
|
197
202
|
expressions.each do |expression|
|
|
198
203
|
add_offense(expression) do |corrector|
|
|
199
|
-
next
|
|
204
|
+
next unless hoistable
|
|
205
|
+
next if void_value_position?(node, expression, insert_position)
|
|
200
206
|
|
|
201
207
|
corrector.remove(removal_range(expression))
|
|
202
208
|
next if inserted_expression
|
|
@@ -212,6 +218,77 @@ module RuboCop
|
|
|
212
218
|
end
|
|
213
219
|
end
|
|
214
220
|
|
|
221
|
+
def hoistable?(node)
|
|
222
|
+
return false if node.if_type? && (node.ternary? || node.then?)
|
|
223
|
+
|
|
224
|
+
statement?(node.parent&.assignment? ? node.parent : node)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def statement?(node)
|
|
228
|
+
parent = node.parent
|
|
229
|
+
return true if parent.nil? || parent.type?(*STATEMENT_BODY_TYPES)
|
|
230
|
+
return false if inline_form?(parent)
|
|
231
|
+
|
|
232
|
+
statement_branch?(node, parent)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def inline_form?(node)
|
|
236
|
+
case node.type
|
|
237
|
+
when :def, :defs
|
|
238
|
+
node.endless?
|
|
239
|
+
when :if, :while, :until
|
|
240
|
+
node.modifier_form?
|
|
241
|
+
when :rescue
|
|
242
|
+
rescue_modifier?(node.resbody_branches.first)
|
|
243
|
+
when :resbody
|
|
244
|
+
rescue_modifier?(node)
|
|
245
|
+
else
|
|
246
|
+
false
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def statement_branch?(node, parent)
|
|
251
|
+
case parent.type
|
|
252
|
+
when :begin, :def, :defs, :rescue, :resbody
|
|
253
|
+
true
|
|
254
|
+
when :if
|
|
255
|
+
!parent.ternary? && !node.equal?(parent.condition)
|
|
256
|
+
when :class, :module, :sclass, :when, :in_pattern, :for, :while, :until
|
|
257
|
+
node.equal?(parent.body)
|
|
258
|
+
when :case, :case_match
|
|
259
|
+
node.equal?(parent.else_branch)
|
|
260
|
+
else
|
|
261
|
+
false
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def void_value_position?(node, expression, insert_position)
|
|
266
|
+
return false if insert_position != :after_condition
|
|
267
|
+
return false unless value_position?(node)
|
|
268
|
+
|
|
269
|
+
jump?(expression)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def value_position?(node)
|
|
273
|
+
parent = node.parent
|
|
274
|
+
return false unless parent
|
|
275
|
+
return true if parent.assignment?
|
|
276
|
+
return false unless explicit_begin?(parent)
|
|
277
|
+
|
|
278
|
+
node.value_used?
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def explicit_begin?(node)
|
|
282
|
+
node.kwbegin_type? || (node.begin_type? && !node.loc.begin.nil?)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def jump?(node)
|
|
286
|
+
return true if node.type?(*VOID_VALUE_TYPES)
|
|
287
|
+
return false if node.type?(:any_block, :any_def)
|
|
288
|
+
|
|
289
|
+
node.each_child_node.any? { |child| jump?(child) }
|
|
290
|
+
end
|
|
291
|
+
|
|
215
292
|
def removal_range(expression)
|
|
216
293
|
range = expression.source_range
|
|
217
294
|
whole_lines = range_by_whole_lines(range, include_final_newline: true)
|
|
@@ -88,7 +88,7 @@ module RuboCop
|
|
|
88
88
|
MSG_USE_NORMAL = 'Modifier form of `%<keyword>s` makes the line too long.'
|
|
89
89
|
|
|
90
90
|
def self.autocorrect_incompatible_with
|
|
91
|
-
[Style::Next, Style::SoleNestedConditional]
|
|
91
|
+
[Style::MethodCallWithArgsParentheses, Style::Next, Style::SoleNestedConditional]
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
@@ -29,7 +29,7 @@ module RuboCop
|
|
|
29
29
|
MSG = 'Avoid modifier `%<keyword>s` after another conditional.'
|
|
30
30
|
|
|
31
31
|
def self.autocorrect_incompatible_with
|
|
32
|
-
[Style::SoleNestedConditional]
|
|
32
|
+
[Style::MethodCallWithArgsParentheses, Style::SoleNestedConditional]
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
# rubocop:disable-next Metrics/AbcSize
|
|
@@ -126,8 +126,9 @@ module RuboCop
|
|
|
126
126
|
def lower_precedence_than_ternary?(node)
|
|
127
127
|
return true if node.type?(*LOWER_PRECEDENCE_THAN_TERNARY_TYPES)
|
|
128
128
|
return true if node.send_type? && (node.prefix_not? || prefix_bang_on_command?(node))
|
|
129
|
+
return true if command_assignment?(node)
|
|
129
130
|
|
|
130
|
-
|
|
131
|
+
keyword_command?(node)
|
|
131
132
|
end
|
|
132
133
|
|
|
133
134
|
def operand_of_parent?(node)
|
|
@@ -174,8 +175,37 @@ module RuboCop
|
|
|
174
175
|
require_argument_parentheses?(receiver) || lower_precedence_than_ternary?(receiver)
|
|
175
176
|
end
|
|
176
177
|
|
|
178
|
+
def command_assignment?(node)
|
|
179
|
+
return false unless node.assignment?
|
|
180
|
+
|
|
181
|
+
value = node.call_type? ? node.last_argument : node.expression
|
|
182
|
+
return true if value.array_type? && !value.bracketed?
|
|
183
|
+
|
|
184
|
+
value = value.body if value.rescue_type?
|
|
185
|
+
|
|
186
|
+
command_value?(value)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def command_value?(node)
|
|
190
|
+
node = node.send_node if node.any_block_type?
|
|
191
|
+
return true if command_assignment?(node)
|
|
192
|
+
return false if node.defined_type?
|
|
193
|
+
return true if keyword_command?(node)
|
|
194
|
+
return false if binary_operator_call?(node)
|
|
195
|
+
|
|
196
|
+
require_argument_parentheses?(node)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def keyword_command?(node)
|
|
200
|
+
node.type?(:yield, :super, :defined?) && node.arguments.any? && !node.parenthesized?
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def binary_operator_call?(node)
|
|
204
|
+
node.call_type? && node.binary_operation? && !node.loc.dot
|
|
205
|
+
end
|
|
206
|
+
|
|
177
207
|
def require_argument_parentheses?(node)
|
|
178
|
-
return false if !node.call_type? || node.arithmetic_operation?
|
|
208
|
+
return false if !node.call_type? || node.arithmetic_operation? || node.setter_method?
|
|
179
209
|
|
|
180
210
|
!node.parenthesized? && node.arguments.any? && !node.method?(:[]) && !node.method?(:[]=)
|
|
181
211
|
end
|
|
@@ -86,7 +86,7 @@ module RuboCop
|
|
|
86
86
|
find_block_variables(node, block_argument_name).each do |variable|
|
|
87
87
|
add_offense(variable, message: MSG_USE_IT_PARAMETER) do |corrector|
|
|
88
88
|
corrector.remove(node.arguments)
|
|
89
|
-
corrector
|
|
89
|
+
replace_with_it(corrector, variable)
|
|
90
90
|
end
|
|
91
91
|
end
|
|
92
92
|
end
|
|
@@ -131,6 +131,16 @@ module RuboCop
|
|
|
131
131
|
end
|
|
132
132
|
end
|
|
133
133
|
|
|
134
|
+
def replace_with_it(corrector, variable)
|
|
135
|
+
parent = variable.parent
|
|
136
|
+
|
|
137
|
+
if parent.pair_type? && parent.value_omission?
|
|
138
|
+
corrector.replace(parent.loc.operator, ': it')
|
|
139
|
+
else
|
|
140
|
+
corrector.replace(variable, 'it')
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
134
144
|
def it_variable_in_scope?(node)
|
|
135
145
|
scope = nearest_scope(node)
|
|
136
146
|
return true if it_argument_in_scope?(node, scope)
|
|
@@ -76,11 +76,9 @@ module RuboCop
|
|
|
76
76
|
return if uncorrectable_lambda_literal?(node)
|
|
77
77
|
|
|
78
78
|
add_offense(node.send_node, message: message(node, selector)) do |corrector|
|
|
79
|
-
if node.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
autocorrect_method_to_literal(corrector, node)
|
|
83
|
-
end
|
|
79
|
+
next if node.each_ancestor.any? { |ancestor| ignored_node?(ancestor) }
|
|
80
|
+
|
|
81
|
+
autocorrect(corrector, node)
|
|
84
82
|
end
|
|
85
83
|
end
|
|
86
84
|
alias on_numblock on_block
|
|
@@ -124,6 +122,16 @@ module RuboCop
|
|
|
124
122
|
end
|
|
125
123
|
end
|
|
126
124
|
|
|
125
|
+
def autocorrect(corrector, node)
|
|
126
|
+
if node.send_node.lambda_literal?
|
|
127
|
+
LambdaLiteralToMethodCorrector.new(node).call(corrector)
|
|
128
|
+
else
|
|
129
|
+
autocorrect_method_to_literal(corrector, node)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
ignore_node(node.arguments) if node.arguments?
|
|
133
|
+
end
|
|
134
|
+
|
|
127
135
|
def autocorrect_method_to_literal(corrector, node)
|
|
128
136
|
corrector.replace(node.send_node, '->')
|
|
129
137
|
|
|
@@ -63,6 +63,10 @@ module RuboCop
|
|
|
63
63
|
|
|
64
64
|
MSG = 'Use `%<new_method_name>s` instead of `each` to map elements into an array.'
|
|
65
65
|
|
|
66
|
+
def self.autocorrect_incompatible_with
|
|
67
|
+
[Style::TrailingCommaInArguments]
|
|
68
|
+
end
|
|
69
|
+
|
|
66
70
|
# @!method suitable_argument_node?(node)
|
|
67
71
|
def_node_matcher :suitable_argument_node?, <<-PATTERN
|
|
68
72
|
!{splat forwarded-restarg forwarded-args (hash (forwarded-kwrestarg)) block-pass}
|
|
@@ -298,9 +298,8 @@ module RuboCop
|
|
|
298
298
|
parent.assignment? && (grandparent.conditional? || grandparent.when_type?)
|
|
299
299
|
end
|
|
300
300
|
|
|
301
|
-
# rubocop:disable-next Metrics/PerceivedComplexity
|
|
302
301
|
def command_assignment_in_expression?(node)
|
|
303
|
-
value = node
|
|
302
|
+
value = assigned_value(node)
|
|
304
303
|
assignment = outermost_assignment(value)
|
|
305
304
|
return false if assignment.equal?(value)
|
|
306
305
|
|
|
@@ -308,9 +307,18 @@ module RuboCop
|
|
|
308
307
|
call_in_logical_operators?(assignment) ||
|
|
309
308
|
call_in_optional_arguments?(assignment) ||
|
|
310
309
|
call_as_argument_or_chain?(assignment) ||
|
|
310
|
+
call_in_match_pattern?(assignment) ||
|
|
311
311
|
assignment.parent&.type?(:masgn, :return, :next, :break, :defined?)
|
|
312
312
|
end
|
|
313
313
|
|
|
314
|
+
def assigned_value(node)
|
|
315
|
+
parent = node.parent
|
|
316
|
+
return parent if parent&.rescue_type?
|
|
317
|
+
return parent if parent&.any_block_type? && parent.send_node.equal?(node)
|
|
318
|
+
|
|
319
|
+
node
|
|
320
|
+
end
|
|
321
|
+
|
|
314
322
|
def outermost_assignment(value)
|
|
315
323
|
parent = value.parent
|
|
316
324
|
return value if !parent&.assignment? || parent.masgn_type?
|
|
@@ -29,9 +29,47 @@ module RuboCop
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def eligible_for_parentheses_omission?(node)
|
|
32
|
+
return true if do_end_block_in_command_arguments?(node)
|
|
33
|
+
|
|
32
34
|
node.operator_method? || node.setter_method? || ignored_macro?(node)
|
|
33
35
|
end
|
|
34
36
|
|
|
37
|
+
def do_end_block_in_command_arguments?(node)
|
|
38
|
+
return false unless node.block_node&.keywords?
|
|
39
|
+
|
|
40
|
+
child = node.block_node
|
|
41
|
+
child.each_ancestor do |ancestor|
|
|
42
|
+
return !ancestor.parenthesized? if command_argument?(ancestor, child)
|
|
43
|
+
return false if command_arguments_boundary?(ancestor, child)
|
|
44
|
+
|
|
45
|
+
child = ancestor
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
false
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def command_argument?(node, child)
|
|
52
|
+
return false unless node.type?(:call, :super, :yield)
|
|
53
|
+
return false if node.operator_method? || node.setter_method?
|
|
54
|
+
|
|
55
|
+
node.arguments.any? { |argument| argument.equal?(child) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def command_arguments_boundary?(node, child)
|
|
59
|
+
return true if node.type?(:any_block, :any_def, :class, :module, :sclass, :kwbegin)
|
|
60
|
+
return true if node.type?(:break, :next, :return) || index_argument?(node, child)
|
|
61
|
+
|
|
62
|
+
node.type?(:begin, :array, :hash) && node.loc?(:begin)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def index_argument?(node, child)
|
|
66
|
+
return false unless node.call_type?
|
|
67
|
+
return node.arguments.any? { |argument| argument.equal?(child) } if node.method?(:[])
|
|
68
|
+
return false unless node.method?(:[]=)
|
|
69
|
+
|
|
70
|
+
node.arguments[0...-1].any? { |argument| argument.equal?(child) }
|
|
71
|
+
end
|
|
72
|
+
|
|
35
73
|
def included_macros_list
|
|
36
74
|
@included_macros_list ||= cop_config.fetch('IncludedMacros', []).map(&:to_sym).freeze
|
|
37
75
|
end
|
|
@@ -243,7 +243,9 @@ module RuboCop
|
|
|
243
243
|
|
|
244
244
|
def self.autocorrect_incompatible_with
|
|
245
245
|
[
|
|
246
|
+
Layout::HeredocArgumentClosingParenthesis,
|
|
246
247
|
Style::NestedParenthesizedCalls,
|
|
248
|
+
Style::ParenthesesAroundCondition,
|
|
247
249
|
Style::RedundantParentheses,
|
|
248
250
|
Style::RescueModifier,
|
|
249
251
|
Style::TernaryParentheses,
|
|
@@ -277,7 +279,8 @@ module RuboCop
|
|
|
277
279
|
first_node = node.first_argument
|
|
278
280
|
return false unless first_node.begin_type? && first_node.parenthesized_call?
|
|
279
281
|
|
|
280
|
-
first_node.children.one? && mergeable_parentheses?(first_node.children.first)
|
|
282
|
+
first_node.children.one? && mergeable_parentheses?(first_node.children.first) &&
|
|
283
|
+
!semicolon_around_grouped_expression?(first_node)
|
|
281
284
|
end
|
|
282
285
|
|
|
283
286
|
def mergeable_parentheses?(node)
|
|
@@ -294,6 +297,14 @@ module RuboCop
|
|
|
294
297
|
true
|
|
295
298
|
end
|
|
296
299
|
end
|
|
300
|
+
|
|
301
|
+
def semicolon_around_grouped_expression?(node)
|
|
302
|
+
expression = node.children.first.source_range
|
|
303
|
+
|
|
304
|
+
processed_source.tokens_within(node).any? do |token|
|
|
305
|
+
token.semicolon? && !expression.contains?(token.pos)
|
|
306
|
+
end
|
|
307
|
+
end
|
|
297
308
|
end
|
|
298
309
|
end
|
|
299
310
|
end
|
|
@@ -169,12 +169,20 @@ module RuboCop
|
|
|
169
169
|
location = node.arguments.source_range
|
|
170
170
|
|
|
171
171
|
add_offense(location, message: MSG_MISSING) do |corrector|
|
|
172
|
-
|
|
172
|
+
corrector.replace(range_before_arguments(node), '(')
|
|
173
|
+
corrector.insert_after(location, ')')
|
|
173
174
|
|
|
174
175
|
unexpected_style_detected 'require_no_parentheses'
|
|
175
176
|
end
|
|
176
177
|
end
|
|
177
178
|
|
|
179
|
+
def range_before_arguments(node)
|
|
180
|
+
range = node.loc.name.end.join(node.arguments.source_range.begin)
|
|
181
|
+
last_line_break = range.source.rindex("\n")
|
|
182
|
+
|
|
183
|
+
last_line_break ? range.resize(last_line_break) : range
|
|
184
|
+
end
|
|
185
|
+
|
|
178
186
|
def unwanted_parentheses(args)
|
|
179
187
|
add_offense(args, message: MSG_PRESENT) do |corrector|
|
|
180
188
|
# offense is registered on args node when parentheses are unwanted
|
|
@@ -376,7 +376,7 @@ module RuboCop
|
|
|
376
376
|
(send (const {nil? cbase} :ENV) :[] _)
|
|
377
377
|
(or (send (const {nil? cbase} :ENV) :[] _) _)
|
|
378
378
|
(send _ {:count :length :size} ...)
|
|
379
|
-
(
|
|
379
|
+
(any_block (send _ {:count :length :size} ...) ...)
|
|
380
380
|
}
|
|
381
381
|
PATTERN
|
|
382
382
|
|
|
@@ -64,13 +64,25 @@ module RuboCop
|
|
|
64
64
|
private
|
|
65
65
|
|
|
66
66
|
def autocorrect(corrector, node)
|
|
67
|
-
|
|
68
|
-
range_with_surrounding_space(node.body.source_range)
|
|
69
|
-
else
|
|
70
|
-
range_by_whole_lines(node.body.source_range, include_final_newline: true)
|
|
71
|
-
end
|
|
67
|
+
body_range = node.body.source_range
|
|
72
68
|
|
|
73
|
-
|
|
69
|
+
if node.single_line?
|
|
70
|
+
corrector.replace(range_with_surrounding_space(body_range), node.braces? ? '' : ' ')
|
|
71
|
+
elsif own_lines?(body_range)
|
|
72
|
+
corrector.remove(range_by_whole_lines(body_range, include_final_newline: true))
|
|
73
|
+
else
|
|
74
|
+
corrector.remove(body_range_on_shared_line(body_range))
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def own_lines?(range)
|
|
79
|
+
range_by_whole_lines(range).source.strip == range.source
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def body_range_on_shared_line(body_range)
|
|
83
|
+
side = begins_its_line?(body_range) ? :right : :left
|
|
84
|
+
|
|
85
|
+
range_with_surrounding_space(body_range, side: side, newlines: false)
|
|
74
86
|
end
|
|
75
87
|
end
|
|
76
88
|
end
|
|
@@ -154,7 +154,7 @@ module RuboCop
|
|
|
154
154
|
|
|
155
155
|
def autocorrect_unless_nil(corrector, node, receiver)
|
|
156
156
|
corrector.replace(node.parent.loc.keyword, 'if')
|
|
157
|
-
corrector.replace(node, receiver.source)
|
|
157
|
+
corrector.replace(node, receiver ? receiver.source : 'self')
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
def nil_comparison_style
|
|
@@ -99,7 +99,10 @@ module RuboCop
|
|
|
99
99
|
|
|
100
100
|
message = format(MSG, prefer: replacement, current: node.source)
|
|
101
101
|
add_offense(node, message: message) do |corrector|
|
|
102
|
+
next if inside_corrected_predicate?(node)
|
|
103
|
+
|
|
102
104
|
corrector.replace(node, replacement)
|
|
105
|
+
ignore_node(node)
|
|
103
106
|
end
|
|
104
107
|
end
|
|
105
108
|
|
|
@@ -115,6 +118,10 @@ module RuboCop
|
|
|
115
118
|
BitwisePredicate.support_target_ruby_version?(target_ruby_version)
|
|
116
119
|
end
|
|
117
120
|
|
|
121
|
+
def inside_corrected_predicate?(node)
|
|
122
|
+
node.each_ancestor(:send).any? { |ancestor| ignored_node?(ancestor) }
|
|
123
|
+
end
|
|
124
|
+
|
|
118
125
|
def check(node)
|
|
119
126
|
numeric, operator =
|
|
120
127
|
if style == :predicate
|
|
@@ -5,7 +5,10 @@ module RuboCop
|
|
|
5
5
|
module Style
|
|
6
6
|
# Checks for redundant dot before operator method call.
|
|
7
7
|
# The target operator methods are `|`, `^`, `&`, ``<=>``, `==`, `===`, `=~`, `>`, `>=`, `<`,
|
|
8
|
-
# ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`,
|
|
8
|
+
# ``<=``, `<<`, `>>`, `+`, `-`, `*`, `/`, `%`, `**`, `!=`, and `!~`.
|
|
9
|
+
#
|
|
10
|
+
# `!` and `~` are not targeted because they have no binary operator form,
|
|
11
|
+
# so `foo.! bar` cannot be written as `foo ! bar`, which means `foo(!bar)`.
|
|
9
12
|
#
|
|
10
13
|
# @example
|
|
11
14
|
#
|
|
@@ -21,7 +24,7 @@ module RuboCop
|
|
|
21
24
|
extend AutoCorrector
|
|
22
25
|
|
|
23
26
|
MSG = 'Redundant dot detected.'
|
|
24
|
-
RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % **
|
|
27
|
+
RESTRICT_ON_SEND = %i[| ^ & <=> == === =~ > >= < <= << >> + - * / % ** != !~].freeze
|
|
25
28
|
INVALID_SYNTAX_ARG_TYPES = %i[
|
|
26
29
|
splat kwsplat forwarded_args forwarded_restarg forwarded_kwrestarg block_pass
|
|
27
30
|
].freeze
|
|
@@ -29,8 +32,7 @@ module RuboCop
|
|
|
29
32
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
30
33
|
def on_send(node)
|
|
31
34
|
return unless (dot = node.loc.dot)
|
|
32
|
-
return if
|
|
33
|
-
return if node.receiver.const_type? || !node.arguments.one?
|
|
35
|
+
return if node.receiver.const_type? || !node.arguments.one? || node.block_literal?
|
|
34
36
|
|
|
35
37
|
return unless (rhs = node.first_argument)
|
|
36
38
|
return if method_call_with_parenthesized_arg?(rhs)
|
|
@@ -47,14 +49,6 @@ module RuboCop
|
|
|
47
49
|
|
|
48
50
|
private
|
|
49
51
|
|
|
50
|
-
# `foo.~@` and `foo.!@` call the method `~` and `!` respectively. While those
|
|
51
|
-
# are operator methods, we don't want to actually consider them as such.
|
|
52
|
-
def unary_method_no_operator?(node)
|
|
53
|
-
return false unless node.nonmutating_unary_operator_method?
|
|
54
|
-
|
|
55
|
-
node.method_name.to_s != node.selector.source
|
|
56
|
-
end
|
|
57
|
-
|
|
58
52
|
# Checks for an acceptable case of `foo.+(bar).baz`.
|
|
59
53
|
def method_call_with_parenthesized_arg?(argument)
|
|
60
54
|
return false unless argument.parent.parent&.send_type?
|
|
@@ -69,13 +63,12 @@ module RuboCop
|
|
|
69
63
|
end
|
|
70
64
|
|
|
71
65
|
def wrap_in_parentheses_if_chained(corrector, node)
|
|
72
|
-
return unless node
|
|
73
|
-
return if node.parent.first_argument == node
|
|
74
|
-
|
|
75
|
-
operator = node.loc.selector
|
|
66
|
+
return unless chained?(node)
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
if node.parenthesized?
|
|
69
|
+
ParenthesesCorrector.correct(corrector, node)
|
|
70
|
+
corrector.insert_after(node.loc.selector, ' ')
|
|
71
|
+
end
|
|
79
72
|
corrector.wrap(node, '(', ')')
|
|
80
73
|
end
|
|
81
74
|
|
|
@@ -84,14 +77,20 @@ module RuboCop
|
|
|
84
77
|
selector = node.loc.selector
|
|
85
78
|
|
|
86
79
|
return true if selector.end_pos == rhs.source_range.begin_pos
|
|
87
|
-
return false if node
|
|
80
|
+
return false if chained?(node) # if chained, a space is already added
|
|
81
|
+
|
|
82
|
+
# Without a space, `foo +(bar)` is parsed as `foo(+(bar))` and `foo /(bar)` starts
|
|
83
|
+
# a regexp literal.
|
|
84
|
+
node.parenthesized?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def chained?(node)
|
|
88
|
+
parent = node.parent
|
|
89
|
+
return false unless parent&.call_type?
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
# add one to avoid a syntax error.
|
|
91
|
-
range = selector.end.join(rhs.source_range.begin)
|
|
92
|
-
return true if node.method?(:/) && range.source == '('
|
|
91
|
+
return true unless parent.first_argument.equal?(node)
|
|
93
92
|
|
|
94
|
-
|
|
93
|
+
parent.binary_operation? && !parent.method?(:[])
|
|
95
94
|
end
|
|
96
95
|
end
|
|
97
96
|
end
|