rubocop 1.50.2 → 1.54.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/config/default.yml +80 -6
- data/lib/rubocop/cli/command/lsp.rb +19 -0
- data/lib/rubocop/cli.rb +3 -0
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_loader_resolver.rb +4 -3
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
- data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
- data/lib/rubocop/cop/layout/class_structure.rb +7 -0
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
- data/lib/rubocop/cop/lint/debugger.rb +9 -5
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
- data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
- data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/missing_super.rb +34 -5
- data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +8 -3
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/useless_assignment.rb +59 -1
- data/lib/rubocop/cop/lint/void.rb +57 -7
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +30 -2
- data/lib/rubocop/cop/migration/department_name.rb +2 -2
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +3 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +17 -39
- data/lib/rubocop/cop/style/collection_compact.rb +16 -6
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
- data/lib/rubocop/cop/style/conditional_assignment.rb +5 -3
- data/lib/rubocop/cop/style/copyright.rb +5 -2
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/dir_empty.rb +8 -14
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/file_read.rb +2 -2
- data/lib/rubocop/cop/style/guard_clause.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
- data/lib/rubocop/cop/style/hash_except.rb +19 -8
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +6 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -6
- data/lib/rubocop/cop/style/lambda.rb +3 -3
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -3
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +3 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +11 -5
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +81 -0
- data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
- data/lib/rubocop/cop/style/semicolon.rb +12 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +3 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
- data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/regexp_ranges.rb +100 -0
- data/lib/rubocop/cop/variable_force/assignment.rb +47 -4
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +1 -0
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +4 -1
- data/lib/rubocop/lsp/logger.rb +22 -0
- data/lib/rubocop/lsp/routes.rb +231 -0
- data/lib/rubocop/lsp/runtime.rb +82 -0
- data/lib/rubocop/lsp/server.rb +66 -0
- data/lib/rubocop/lsp/severity.rb +27 -0
- data/lib/rubocop/options.rb +11 -1
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/target_ruby.rb +3 -2
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +13 -0
- metadata +38 -6
@@ -26,11 +26,15 @@ module RuboCop
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def delimiter_string(node)
|
29
|
-
node.source.match(OPENING_DELIMITER)
|
29
|
+
return '' unless (match = node.source.match(OPENING_DELIMITER))
|
30
|
+
|
31
|
+
match.captures[1]
|
30
32
|
end
|
31
33
|
|
32
34
|
def heredoc_type(node)
|
33
|
-
node.source.match(OPENING_DELIMITER)
|
35
|
+
return '' unless (match = node.source.match(OPENING_DELIMITER))
|
36
|
+
|
37
|
+
match.captures[0]
|
34
38
|
end
|
35
39
|
end
|
36
40
|
end
|
@@ -109,7 +109,7 @@ module RuboCop
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def use_block_argument_as_local_variable?(node, last_argument)
|
112
|
-
return if node.body.nil?
|
112
|
+
return false if node.body.nil?
|
113
113
|
|
114
114
|
node.body.each_descendant(:lvar, :lvasgn).any? do |lvar|
|
115
115
|
!lvar.parent.block_pass_type? && lvar.node_parts[0].to_s == last_argument
|
@@ -17,7 +17,8 @@ module RuboCop
|
|
17
17
|
# @safety
|
18
18
|
# This cop relies on the pattern `@instance_var ||= ...`,
|
19
19
|
# but this is sometimes used for other purposes than memoization
|
20
|
-
# so this cop is considered unsafe.
|
20
|
+
# so this cop is considered unsafe. Also, its autocorrection is unsafe
|
21
|
+
# because it may conflict with instance variable names already in use.
|
21
22
|
#
|
22
23
|
# @example EnforcedStyleForLeadingUnderscores: disallowed (default)
|
23
24
|
# # bad
|
@@ -145,6 +146,8 @@ module RuboCop
|
|
145
146
|
# @_foo ||= calculate_expensive_thing
|
146
147
|
# end
|
147
148
|
class MemoizedInstanceVariableName < Base
|
149
|
+
extend AutoCorrector
|
150
|
+
|
148
151
|
include ConfigurableEnforcedStyle
|
149
152
|
|
150
153
|
MSG = 'Memoized variable `%<var>s` does not match ' \
|
@@ -163,6 +166,7 @@ module RuboCop
|
|
163
166
|
PATTERN
|
164
167
|
|
165
168
|
# rubocop:disable Metrics/AbcSize
|
169
|
+
# rubocop:disable Metrics/MethodLength
|
166
170
|
def on_or_asgn(node)
|
167
171
|
lhs, _value = *node
|
168
172
|
return unless lhs.ivasgn_type?
|
@@ -175,14 +179,18 @@ module RuboCop
|
|
175
179
|
|
176
180
|
return if matches?(method_name, lhs)
|
177
181
|
|
182
|
+
suggested_var = suggested_var(method_name)
|
178
183
|
msg = format(
|
179
184
|
message(lhs.children.first.to_s),
|
180
185
|
var: lhs.children.first.to_s,
|
181
|
-
suggested_var: suggested_var
|
186
|
+
suggested_var: suggested_var,
|
182
187
|
method: method_name
|
183
188
|
)
|
184
|
-
add_offense(lhs, message: msg)
|
189
|
+
add_offense(lhs, message: msg) do |corrector|
|
190
|
+
corrector.replace(lhs.loc.name, "@#{suggested_var}")
|
191
|
+
end
|
185
192
|
end
|
193
|
+
# rubocop:enable Metrics/MethodLength
|
186
194
|
# rubocop:enable Metrics/AbcSize
|
187
195
|
|
188
196
|
# @!method defined_memoized?(node, ivar)
|
@@ -196,24 +204,31 @@ module RuboCop
|
|
196
204
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
197
205
|
def on_defined?(node)
|
198
206
|
arg = node.arguments.first
|
199
|
-
return unless arg.ivar_type?
|
207
|
+
return false unless arg.ivar_type?
|
200
208
|
|
201
209
|
method_node, method_name = find_definition(node)
|
202
|
-
return unless method_node
|
210
|
+
return false unless method_node
|
203
211
|
|
204
212
|
var_name = arg.children.first
|
205
213
|
defined_memoized?(method_node.body, var_name) do |defined_ivar, return_ivar, ivar_assign|
|
206
|
-
return if matches?(method_name, ivar_assign)
|
214
|
+
return false if matches?(method_name, ivar_assign)
|
207
215
|
|
216
|
+
suggested_var = suggested_var(method_name)
|
208
217
|
msg = format(
|
209
218
|
message(var_name.to_s),
|
210
219
|
var: var_name.to_s,
|
211
|
-
suggested_var: suggested_var
|
220
|
+
suggested_var: suggested_var,
|
212
221
|
method: method_name
|
213
222
|
)
|
214
|
-
add_offense(defined_ivar, message: msg)
|
215
|
-
|
216
|
-
|
223
|
+
add_offense(defined_ivar, message: msg) do |corrector|
|
224
|
+
corrector.replace(defined_ivar, "@#{suggested_var}")
|
225
|
+
end
|
226
|
+
add_offense(return_ivar, message: msg) do |corrector|
|
227
|
+
corrector.replace(return_ivar, "@#{suggested_var}")
|
228
|
+
end
|
229
|
+
add_offense(ivar_assign.loc.name, message: msg) do |corrector|
|
230
|
+
corrector.replace(ivar_assign.loc.name, "@#{suggested_var}")
|
231
|
+
end
|
217
232
|
end
|
218
233
|
end
|
219
234
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
@@ -82,9 +82,7 @@ module RuboCop
|
|
82
82
|
message = message(node)
|
83
83
|
|
84
84
|
add_offense(range, message: message) do |corrector|
|
85
|
-
corrector
|
86
|
-
|
87
|
-
correct_node(corrector, node.body, offending_name, preferred_name)
|
85
|
+
autocorrect(corrector, node, range, offending_name, preferred_name)
|
88
86
|
end
|
89
87
|
end
|
90
88
|
|
@@ -95,6 +93,16 @@ module RuboCop
|
|
95
93
|
variable.source_range
|
96
94
|
end
|
97
95
|
|
96
|
+
def autocorrect(corrector, node, range, offending_name, preferred_name)
|
97
|
+
corrector.replace(range, preferred_name)
|
98
|
+
correct_node(corrector, node.body, offending_name, preferred_name)
|
99
|
+
return unless (kwbegin_node = node.parent.each_ancestor(:kwbegin).first)
|
100
|
+
|
101
|
+
kwbegin_node.right_siblings.each do |child_node|
|
102
|
+
correct_node(corrector, child_node, offending_name, preferred_name)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
98
106
|
def variable_name_matches?(node, name)
|
99
107
|
if node.masgn_type?
|
100
108
|
node.each_descendant(:lvasgn).any? do |lvasgn_node|
|
@@ -20,9 +20,14 @@ module RuboCop
|
|
20
20
|
# # good
|
21
21
|
# fooBar = 1
|
22
22
|
#
|
23
|
+
# @example AllowedIdentifiers: ['fooBar']
|
24
|
+
# # good (with EnforcedStyle: snake_case)
|
25
|
+
# fooBar = 1
|
26
|
+
#
|
23
27
|
# @example AllowedPatterns: ['_v\d+\z']
|
24
|
-
# # good
|
28
|
+
# # good (with EnforcedStyle: camelCase)
|
25
29
|
# :release_v1
|
30
|
+
#
|
26
31
|
class VariableName < Base
|
27
32
|
include AllowedIdentifiers
|
28
33
|
include ConfigurableNaming
|
@@ -92,6 +92,7 @@ module RuboCop
|
|
92
92
|
comment_line?(processed_source[node.first_line - 2])
|
93
93
|
end
|
94
94
|
|
95
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
95
96
|
def groupable_accessor?(node)
|
96
97
|
return true unless (previous_expression = node.left_siblings.last)
|
97
98
|
|
@@ -104,8 +105,11 @@ module RuboCop
|
|
104
105
|
|
105
106
|
return true unless previous_expression.send_type?
|
106
107
|
|
107
|
-
previous_expression.attribute_accessor? ||
|
108
|
+
previous_expression.attribute_accessor? ||
|
109
|
+
previous_expression.access_modifier? ||
|
110
|
+
node.first_line - previous_expression.last_line > 1 # there is a space between nodes
|
108
111
|
end
|
112
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
109
113
|
|
110
114
|
def class_send_elements(class_node)
|
111
115
|
class_def = class_node.body
|
@@ -24,7 +24,7 @@ module RuboCop
|
|
24
24
|
def on_send(node)
|
25
25
|
return unless node.command?(:attr) && node.arguments?
|
26
26
|
# check only for method definitions in class/module body
|
27
|
-
return if
|
27
|
+
return if allowed_context?(node)
|
28
28
|
|
29
29
|
message = message(node)
|
30
30
|
add_offense(node.loc.selector, message: message) do |corrector|
|
@@ -34,6 +34,16 @@ module RuboCop
|
|
34
34
|
|
35
35
|
private
|
36
36
|
|
37
|
+
def allowed_context?(node)
|
38
|
+
return false unless (class_node = node.each_ancestor(:class, :block).first)
|
39
|
+
|
40
|
+
(!class_node.class_type? && !class_eval?(class_node)) || define_attr_method?(class_node)
|
41
|
+
end
|
42
|
+
|
43
|
+
def define_attr_method?(node)
|
44
|
+
node.each_descendant(:def).any? { |def_node| def_node.method?(:attr) }
|
45
|
+
end
|
46
|
+
|
37
47
|
def autocorrect(corrector, node)
|
38
48
|
attr_name, setter = *node.arguments
|
39
49
|
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
unless contents.empty?
|
36
36
|
corrector.replace(
|
37
37
|
contents,
|
38
|
-
contents.source.gsub(/\A/, '# ').gsub(
|
38
|
+
contents.source.gsub(/\A/, '# ').gsub("\n\n", "\n#\n").gsub(/\n(?=[^#])/, "\n# ")
|
39
39
|
)
|
40
40
|
end
|
41
41
|
corrector.remove(eq_end)
|
@@ -411,7 +411,7 @@ module RuboCop
|
|
411
411
|
end
|
412
412
|
|
413
413
|
def correction_would_break_code?(node)
|
414
|
-
return unless node.keywords?
|
414
|
+
return false unless node.keywords?
|
415
415
|
|
416
416
|
node.send_node.arguments? && !node.send_node.parenthesized?
|
417
417
|
end
|
@@ -433,7 +433,7 @@ module RuboCop
|
|
433
433
|
end
|
434
434
|
|
435
435
|
def return_value_used?(node)
|
436
|
-
return unless node.parent
|
436
|
+
return false unless node.parent
|
437
437
|
|
438
438
|
# If there are parentheses around the block, check if that
|
439
439
|
# is being used.
|
@@ -445,7 +445,7 @@ module RuboCop
|
|
445
445
|
end
|
446
446
|
|
447
447
|
def return_value_of_scope?(node)
|
448
|
-
return unless node.parent
|
448
|
+
return false unless node.parent
|
449
449
|
|
450
450
|
conditional?(node.parent) || array_or_range?(node.parent) ||
|
451
451
|
node.parent.children.last == node
|
@@ -5,7 +5,7 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Enforces the use of `Object#instance_of?` instead of class comparison
|
7
7
|
# for equality.
|
8
|
-
# `==`, `equal?`, and `eql?`
|
8
|
+
# `==`, `equal?`, and `eql?` custom method definitions are allowed by default.
|
9
9
|
# These are customizable with `AllowedMethods` option.
|
10
10
|
#
|
11
11
|
# @example
|
@@ -18,53 +18,31 @@ module RuboCop
|
|
18
18
|
# # good
|
19
19
|
# var.instance_of?(Date)
|
20
20
|
#
|
21
|
-
# @example AllowedMethods: [] (default)
|
21
|
+
# @example AllowedMethods: ['==', 'equal?', 'eql?'] (default)
|
22
22
|
# # good
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# var.class == Date
|
27
|
-
# var.class.equal?(Date)
|
28
|
-
# var.class.eql?(Date)
|
29
|
-
# var.class.name == 'Date'
|
30
|
-
# var.class.to_s == 'Date'
|
31
|
-
# var.class.inspect == 'Date'
|
23
|
+
# def ==(other)
|
24
|
+
# self.class == other.class && name == other.name
|
25
|
+
# end
|
32
26
|
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
# var.class == Date
|
37
|
-
# var.class.name == 'Date'
|
38
|
-
# var.class.to_s == 'Date'
|
39
|
-
# var.class.inspect == 'Date'
|
27
|
+
# def equal?(other)
|
28
|
+
# self.class.equal?(other.class) && name.equal?(other.name)
|
29
|
+
# end
|
40
30
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
31
|
+
# def eql?(other)
|
32
|
+
# self.class.eql?(other.class) && name.eql?(other.name)
|
33
|
+
# end
|
44
34
|
#
|
45
35
|
# @example AllowedPatterns: [] (default)
|
46
|
-
# # good
|
47
|
-
# var.instance_of?(Date)
|
48
|
-
#
|
49
36
|
# # bad
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
# var.class.name == 'Date'
|
54
|
-
# var.class.to_s == 'Date'
|
55
|
-
# var.class.inspect == 'Date'
|
37
|
+
# def eq(other)
|
38
|
+
# self.class.eq(other.class) && name.eq(other.name)
|
39
|
+
# end
|
56
40
|
#
|
57
41
|
# @example AllowedPatterns: ['eq']
|
58
42
|
# # good
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
# # bad
|
64
|
-
# var.class == Date
|
65
|
-
# var.class.name == 'Date'
|
66
|
-
# var.class.to_s == 'Date'
|
67
|
-
# var.class.inspect == 'Date'
|
43
|
+
# def eq(other)
|
44
|
+
# self.class.eq(other.class) && name.eq(other.name)
|
45
|
+
# end
|
68
46
|
#
|
69
47
|
class ClassEqualityComparison < Base
|
70
48
|
include RangeHelp
|
@@ -9,8 +9,8 @@ module RuboCop
|
|
9
9
|
# @safety
|
10
10
|
# It is unsafe by default because false positives may occur in the
|
11
11
|
# `nil` check of block arguments to the receiver object. Additionally,
|
12
|
-
#
|
13
|
-
#
|
12
|
+
# we can't know the type of the receiver object for sure, which may
|
13
|
+
# result in false positives as well.
|
14
14
|
#
|
15
15
|
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.nil? }`
|
16
16
|
# and `[[1, 2], [3, nil]].compact` are not compatible. This will work fine
|
@@ -19,7 +19,9 @@ module RuboCop
|
|
19
19
|
# @example
|
20
20
|
# # bad
|
21
21
|
# array.reject(&:nil?)
|
22
|
+
# array.delete_if(&:nil?)
|
22
23
|
# array.reject { |e| e.nil? }
|
24
|
+
# array.delete_if { |e| e.nil? }
|
23
25
|
# array.select { |e| !e.nil? }
|
24
26
|
#
|
25
27
|
# # good
|
@@ -33,20 +35,25 @@ module RuboCop
|
|
33
35
|
# # good
|
34
36
|
# hash.compact!
|
35
37
|
#
|
38
|
+
# @example AllowedReceivers: ['params']
|
39
|
+
# # good
|
40
|
+
# params.reject(&:nil?)
|
41
|
+
#
|
36
42
|
class CollectionCompact < Base
|
43
|
+
include AllowedReceivers
|
37
44
|
include RangeHelp
|
38
45
|
extend AutoCorrector
|
39
46
|
extend TargetRubyVersion
|
40
47
|
|
41
48
|
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
42
|
-
RESTRICT_ON_SEND = %i[reject reject! select select!].freeze
|
49
|
+
RESTRICT_ON_SEND = %i[reject delete_if reject! select select!].freeze
|
43
50
|
TO_ENUM_METHODS = %i[to_enum lazy].freeze
|
44
51
|
|
45
52
|
minimum_target_ruby_version 2.4
|
46
53
|
|
47
54
|
# @!method reject_method_with_block_pass?(node)
|
48
55
|
def_node_matcher :reject_method_with_block_pass?, <<~PATTERN
|
49
|
-
(send !nil? {:reject :reject!}
|
56
|
+
(send !nil? {:reject :delete_if :reject!}
|
50
57
|
(block_pass
|
51
58
|
(sym :nil?)))
|
52
59
|
PATTERN
|
@@ -55,7 +62,7 @@ module RuboCop
|
|
55
62
|
def_node_matcher :reject_method?, <<~PATTERN
|
56
63
|
(block
|
57
64
|
(send
|
58
|
-
!nil? {:reject :reject!})
|
65
|
+
!nil? {:reject :delete_if :reject!})
|
59
66
|
$(args ...)
|
60
67
|
(send
|
61
68
|
$(lvar _) :nil?))
|
@@ -74,7 +81,10 @@ module RuboCop
|
|
74
81
|
|
75
82
|
def on_send(node)
|
76
83
|
return unless (range = offense_range(node))
|
77
|
-
return if
|
84
|
+
return if allowed_receiver?(node.receiver)
|
85
|
+
if (target_ruby_version <= 3.0 || node.method?(:delete_if)) && to_enum_method?(node)
|
86
|
+
return
|
87
|
+
end
|
78
88
|
|
79
89
|
good = good_method_name(node)
|
80
90
|
message = format(MSG, good: good, bad: range.source)
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for methods invoked via the
|
7
|
-
# of the
|
6
|
+
# Checks for methods invoked via the `::` operator instead
|
7
|
+
# of the `.` operator (like `FileUtils::rmdir` instead of `FileUtils.rmdir`).
|
8
8
|
#
|
9
9
|
# @example
|
10
10
|
# # bad
|
@@ -57,38 +57,58 @@ module RuboCop
|
|
57
57
|
# end
|
58
58
|
#
|
59
59
|
class CombinableLoops < Base
|
60
|
+
extend AutoCorrector
|
61
|
+
|
62
|
+
include RangeHelp
|
63
|
+
|
60
64
|
MSG = 'Combine this loop with the previous loop.'
|
61
65
|
|
62
66
|
def on_block(node)
|
63
67
|
return unless node.parent&.begin_type?
|
64
68
|
return unless collection_looping_method?(node)
|
69
|
+
return unless same_collection_looping_block?(node, node.left_sibling)
|
65
70
|
|
66
|
-
add_offense(node)
|
71
|
+
add_offense(node) do |corrector|
|
72
|
+
combine_with_left_sibling(corrector, node)
|
73
|
+
end
|
67
74
|
end
|
68
75
|
|
69
76
|
alias on_numblock on_block
|
70
77
|
|
71
78
|
def on_for(node)
|
72
79
|
return unless node.parent&.begin_type?
|
80
|
+
return unless same_collection_looping_for?(node, node.left_sibling)
|
73
81
|
|
74
|
-
|
75
|
-
|
82
|
+
add_offense(node) do |corrector|
|
83
|
+
combine_with_left_sibling(corrector, node)
|
84
|
+
end
|
76
85
|
end
|
77
86
|
|
78
87
|
private
|
79
88
|
|
80
89
|
def collection_looping_method?(node)
|
81
|
-
|
82
|
-
method_name = node.method_name.to_s
|
90
|
+
method_name = node.method_name
|
83
91
|
method_name.start_with?('each') || method_name.end_with?('_each')
|
84
92
|
end
|
85
93
|
|
86
|
-
def
|
94
|
+
def same_collection_looping_block?(node, sibling)
|
87
95
|
(sibling&.block_type? || sibling&.numblock_type?) &&
|
88
96
|
sibling.send_node.method?(node.method_name) &&
|
89
97
|
sibling.receiver == node.receiver &&
|
90
98
|
sibling.send_node.arguments == node.send_node.arguments
|
91
99
|
end
|
100
|
+
|
101
|
+
def same_collection_looping_for?(node, sibling)
|
102
|
+
sibling&.for_type? && node.collection == sibling.collection
|
103
|
+
end
|
104
|
+
|
105
|
+
def combine_with_left_sibling(corrector, node)
|
106
|
+
corrector.replace(
|
107
|
+
node.left_sibling.body,
|
108
|
+
"#{node.left_sibling.body.source}\n#{node.body.source}"
|
109
|
+
)
|
110
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
111
|
+
end
|
92
112
|
end
|
93
113
|
end
|
94
114
|
end
|
@@ -350,7 +350,7 @@ module RuboCop
|
|
350
350
|
end
|
351
351
|
|
352
352
|
def ternary_condition?(node)
|
353
|
-
[node, node.children.first].any? { |n| n.if_type? && n.ternary? }
|
353
|
+
[node, node.children.first].compact.any? { |n| n.if_type? && n.ternary? }
|
354
354
|
end
|
355
355
|
|
356
356
|
def lhs_all_match?(branches)
|
@@ -361,7 +361,7 @@ module RuboCop
|
|
361
361
|
end
|
362
362
|
|
363
363
|
def assignment_types_match?(*nodes)
|
364
|
-
return unless assignment_type?(nodes.first)
|
364
|
+
return false unless assignment_type?(nodes.first)
|
365
365
|
|
366
366
|
nodes.map(&:type).uniq.one?
|
367
367
|
end
|
@@ -386,7 +386,7 @@ module RuboCop
|
|
386
386
|
def allowed_statements?(branches)
|
387
387
|
return false unless branches.all?
|
388
388
|
|
389
|
-
statements = branches.
|
389
|
+
statements = branches.filter_map { |branch| tail(branch) }
|
390
390
|
|
391
391
|
lhs_all_match?(statements) && statements.none?(&:masgn_type?) &&
|
392
392
|
assignment_types_match?(*statements)
|
@@ -440,6 +440,8 @@ module RuboCop
|
|
440
440
|
module ConditionalCorrectorHelper
|
441
441
|
def remove_whitespace_in_branches(corrector, branch, condition, column)
|
442
442
|
branch.each_node do |child|
|
443
|
+
next if child.source_range.nil?
|
444
|
+
|
443
445
|
white_space = white_space_range(child, column)
|
444
446
|
corrector.remove(white_space) if white_space.source.strip.empty?
|
445
447
|
end
|
@@ -8,8 +8,11 @@ module RuboCop
|
|
8
8
|
# The default regexp for an acceptable copyright notice can be found in
|
9
9
|
# config/default.yml. The default can be changed as follows:
|
10
10
|
#
|
11
|
-
#
|
12
|
-
#
|
11
|
+
# [source,yaml]
|
12
|
+
# ----
|
13
|
+
# Style/Copyright:
|
14
|
+
# Notice: '^Copyright (\(c\) )?2\d{3} Acme Inc'
|
15
|
+
# ----
|
13
16
|
#
|
14
17
|
# This regex string is treated as an unanchored regex. For each file
|
15
18
|
# that RuboCop scans, a comment that matches this regex must be found or
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for places where the
|
6
|
+
# Checks for places where the `#\_\_dir\_\_` method can replace more
|
7
7
|
# complex constructs to retrieve a canonicalized absolute path to the
|
8
8
|
# current file.
|
9
9
|
#
|
@@ -19,18 +19,16 @@ module RuboCop
|
|
19
19
|
extend AutoCorrector
|
20
20
|
extend TargetRubyVersion
|
21
21
|
|
22
|
-
MSG = 'Use
|
23
|
-
RESTRICT_ON_SEND = %i[== > empty? none?].freeze
|
22
|
+
MSG = 'Use `%<replacement>s` instead.'
|
23
|
+
RESTRICT_ON_SEND = %i[== != > empty? none?].freeze
|
24
24
|
|
25
25
|
minimum_target_ruby_version 2.4
|
26
26
|
|
27
27
|
# @!method offensive?(node)
|
28
28
|
def_node_matcher :offensive?, <<~PATTERN
|
29
29
|
{
|
30
|
-
(send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :>} (int 2))
|
31
|
-
(send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :>} (int 0))
|
32
|
-
(send (send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) :!) {:== :>} (int 2))
|
33
|
-
(send (send (send (send $(const {nil? cbase} :Dir) :children $_) :size) :!) {:== :>} (int 0))
|
30
|
+
(send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :!= :>} (int 2))
|
31
|
+
(send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :!= :>} (int 0))
|
34
32
|
(send (send $(const {nil? cbase} :Dir) :children $_) :empty?)
|
35
33
|
(send (send $(const {nil? cbase} :Dir) :each_child $_) :none?)
|
36
34
|
}
|
@@ -38,10 +36,9 @@ module RuboCop
|
|
38
36
|
|
39
37
|
def on_send(node)
|
40
38
|
offensive?(node) do |const_node, arg_node|
|
41
|
-
|
42
|
-
|
43
|
-
corrector.replace(node,
|
44
|
-
"#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})")
|
39
|
+
replacement = "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})"
|
40
|
+
add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
|
41
|
+
corrector.replace(node, replacement)
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
@@ -49,10 +46,7 @@ module RuboCop
|
|
49
46
|
private
|
50
47
|
|
51
48
|
def bang(node)
|
52
|
-
if
|
53
|
-
(node.method?(:>) && !node.child_nodes.first.method?(:!))
|
54
|
-
'!'
|
55
|
-
end
|
49
|
+
'!' if %i[!= >].include? node.method_name
|
56
50
|
end
|
57
51
|
end
|
58
52
|
end
|
@@ -108,7 +108,7 @@ module RuboCop
|
|
108
108
|
comments = heredoc_comment_blocks(arg_node.loc.heredoc_body.line_span)
|
109
109
|
.concat(preceding_comment_blocks(arg_node.parent))
|
110
110
|
|
111
|
-
return if comments.none?
|
111
|
+
return false if comments.none?
|
112
112
|
|
113
113
|
regexp = comment_regexp(arg_node)
|
114
114
|
comments.any?(regexp) || regexp.match?(comments.join)
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# declarations.
|
11
11
|
#
|
12
12
|
# The documentation requirement is annulled if the class or module has
|
13
|
-
# a
|
13
|
+
# a `#:nodoc:` comment next to it. Likewise, `#:nodoc: all` does the
|
14
14
|
# same for all its children.
|
15
15
|
#
|
16
16
|
# @example
|