rubocop 1.32.0 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/config/default.yml +104 -16
- data/config/obsoletion.yml +23 -1
- data/lib/rubocop/arguments_env.rb +17 -0
- data/lib/rubocop/arguments_file.rb +17 -0
- data/lib/rubocop/cache_config.rb +29 -0
- data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +2 -2
- data/lib/rubocop/cli/command/execute_runner.rb +7 -7
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +53 -15
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_finder.rb +68 -0
- data/lib/rubocop/config_loader.rb +12 -40
- data/lib/rubocop/config_loader_resolver.rb +1 -5
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +5 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +4 -0
- data/lib/rubocop/config_obsoletion.rb +7 -2
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +58 -0
- data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
- data/lib/rubocop/cop/generator.rb +1 -2
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +62 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +16 -12
- data/lib/rubocop/cop/layout/block_end_newline.rb +35 -5
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +4 -4
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +6 -2
- data/lib/rubocop/cop/layout/line_length.rb +4 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +13 -9
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +25 -9
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +28 -3
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
- data/lib/rubocop/cop/legacy/corrector.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +21 -8
- data/lib/rubocop/cop/lint/debugger.rb +26 -16
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +11 -1
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +25 -6
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_class.rb +3 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +107 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +4 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +50 -1
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +6 -6
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +24 -8
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +12 -1
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +29 -9
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +9 -3
- data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
- data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +15 -10
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +27 -3
- data/lib/rubocop/cop/lint/unreachable_loop.rb +9 -3
- data/lib/rubocop/cop/lint/unused_method_argument.rb +4 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +8 -6
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +2 -0
- data/lib/rubocop/cop/metrics/abc_size.rb +3 -1
- data/lib/rubocop/cop/metrics/block_length.rb +6 -7
- data/lib/rubocop/cop/metrics/method_length.rb +8 -8
- data/lib/rubocop/cop/mixin/allowed_methods.rb +20 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +17 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +17 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +82 -4
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
- data/lib/rubocop/cop/mixin/method_complexity.rb +8 -13
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +4 -5
- data/lib/rubocop/cop/mixin/rescue_node.rb +3 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +6 -5
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +2 -2
- data/lib/rubocop/cop/naming/inclusive_language.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +24 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +97 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +7 -3
- data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +26 -7
- data/lib/rubocop/cop/style/case_equality.rb +40 -10
- data/lib/rubocop/cop/style/class_and_module_children.rb +4 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +32 -7
- data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
- data/lib/rubocop/cop/style/collection_compact.rb +6 -1
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
- data/lib/rubocop/cop/style/double_negation.rb +2 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +41 -6
- data/lib/rubocop/cop/style/each_with_object.rb +39 -8
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_heredoc.rb +15 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/endless_method.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +4 -0
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/format_string_token.rb +21 -8
- data/lib/rubocop/cop/style/guard_clause.rb +27 -16
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
- data/lib/rubocop/cop/style/hash_except.rb +0 -4
- data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +15 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -7
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -6
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +7 -1
- data/lib/rubocop/cop/style/next.rb +3 -5
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +28 -8
- data/lib/rubocop/cop/style/object_then.rb +2 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +39 -0
- data/lib/rubocop/cop/style/perl_backrefs.rb +22 -1
- data/lib/rubocop/cop/style/proc.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +3 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +24 -6
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +19 -22
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +8 -1
- data/lib/rubocop/cop/style/redundant_self.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +21 -6
- data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
- data/lib/rubocop/cop/style/redundant_string_escape.rb +173 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +4 -2
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +14 -5
- data/lib/rubocop/cop/style/static_class.rb +32 -1
- data/lib/rubocop/cop/style/symbol_array.rb +3 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +38 -12
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -13
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +3 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/ext/range.rb +15 -0
- data/lib/rubocop/feature_loader.rb +94 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +9 -3
- data/lib/rubocop/formatter/html_formatter.rb +3 -3
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/options.rb +13 -13
- data/lib/rubocop/result_cache.rb +22 -20
- data/lib/rubocop/rspec/shared_contexts.rb +13 -1
- data/lib/rubocop/runner.rb +4 -0
- data/lib/rubocop/server/cache.rb +41 -2
- data/lib/rubocop/server/cli.rb +26 -2
- data/lib/rubocop/server/client_command/exec.rb +5 -0
- data/lib/rubocop/server/core.rb +2 -1
- data/lib/rubocop/server/socket_reader.rb +5 -1
- data/lib/rubocop/server.rb +1 -1
- data/lib/rubocop/version.rb +8 -2
- data/lib/rubocop.rb +8 -3
- metadata +20 -9
- data/lib/rubocop/cop/mixin/ignored_methods.rb +0 -52
@@ -85,7 +85,9 @@ module RuboCop
|
|
85
85
|
private
|
86
86
|
|
87
87
|
def body_or_allowed_comment_lines?(node)
|
88
|
-
|
88
|
+
return true if node.body
|
89
|
+
|
90
|
+
cop_config['AllowComments'] && processed_source.contains_comment?(node.source_range)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
@@ -4,6 +4,14 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
6
|
# Checks for the presence of `if`, `elsif` and `unless` branches without a body.
|
7
|
+
#
|
8
|
+
# NOTE: empty `else` branches are handled by `Style/EmptyElse`.
|
9
|
+
#
|
10
|
+
# @safety
|
11
|
+
# Autocorrection for this cop is not safe. The conditions for empty branches that
|
12
|
+
# the autocorrection removes may have side effects, or the logic in subsequent
|
13
|
+
# branches may change due to the removal of a previous condition.
|
14
|
+
#
|
7
15
|
# @example
|
8
16
|
# # bad
|
9
17
|
# if condition
|
@@ -53,7 +61,9 @@ module RuboCop
|
|
53
61
|
# end
|
54
62
|
#
|
55
63
|
class EmptyConditionalBody < Base
|
64
|
+
extend AutoCorrector
|
56
65
|
include CommentsHelp
|
66
|
+
include RangeHelp
|
57
67
|
|
58
68
|
MSG = 'Avoid `%<keyword>s` branches without a body.'
|
59
69
|
|
@@ -61,7 +71,103 @@ module RuboCop
|
|
61
71
|
return if node.body
|
62
72
|
return if cop_config['AllowComments'] && contains_comments?(node)
|
63
73
|
|
64
|
-
add_offense(node, message: format(MSG, keyword: node.keyword))
|
74
|
+
add_offense(node, message: format(MSG, keyword: node.keyword)) do |corrector|
|
75
|
+
autocorrect(corrector, node)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def autocorrect(corrector, node)
|
82
|
+
remove_comments(corrector, node)
|
83
|
+
remove_empty_branch(corrector, node)
|
84
|
+
correct_other_branches(corrector, node)
|
85
|
+
end
|
86
|
+
|
87
|
+
def remove_comments(corrector, node)
|
88
|
+
comments_in_range(node).each do |comment|
|
89
|
+
range = range_by_whole_lines(comment.loc.expression, include_final_newline: true)
|
90
|
+
corrector.remove(range)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def remove_empty_branch(corrector, node)
|
95
|
+
if empty_if_branch?(node) && else_branch?(node)
|
96
|
+
corrector.remove(branch_range(node))
|
97
|
+
else
|
98
|
+
corrector.remove(deletion_range(branch_range(node)))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def correct_other_branches(corrector, node)
|
103
|
+
return unless require_other_branches_correction?(node)
|
104
|
+
|
105
|
+
if node.else_branch&.if_type?
|
106
|
+
# Replace an orphaned `elsif` with `if`
|
107
|
+
corrector.replace(node.else_branch.loc.keyword, 'if')
|
108
|
+
else
|
109
|
+
# Flip orphaned `else`
|
110
|
+
corrector.replace(node.loc.else, "#{node.inverse_keyword} #{node.condition.source}")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def require_other_branches_correction?(node)
|
115
|
+
return false unless node.if_type? && node.else?
|
116
|
+
return false if !empty_if_branch?(node) && node.elsif?
|
117
|
+
|
118
|
+
!empty_elsif_branch?(node)
|
119
|
+
end
|
120
|
+
|
121
|
+
def empty_if_branch?(node)
|
122
|
+
return false unless (parent = node.parent)
|
123
|
+
return true unless parent.if_type?
|
124
|
+
return true unless (if_branch = parent.if_branch)
|
125
|
+
|
126
|
+
if_branch.if_type? && !if_branch.body
|
127
|
+
end
|
128
|
+
|
129
|
+
def empty_elsif_branch?(node)
|
130
|
+
return false unless (else_branch = node.else_branch)
|
131
|
+
|
132
|
+
else_branch.if_type? && !else_branch.body
|
133
|
+
end
|
134
|
+
|
135
|
+
def else_branch?(node)
|
136
|
+
node.else_branch && !node.else_branch.if_type?
|
137
|
+
end
|
138
|
+
|
139
|
+
# rubocop:disable Metrics/AbcSize
|
140
|
+
def branch_range(node)
|
141
|
+
if empty_if_branch?(node) && else_branch?(node)
|
142
|
+
node.source_range.with(end_pos: node.loc.else.begin_pos)
|
143
|
+
elsif node.loc.else
|
144
|
+
node.source_range.with(end_pos: node.loc.else.begin_pos - 1)
|
145
|
+
elsif all_branches_body_missing?(node)
|
146
|
+
if_node = node.ancestors.detect(&:if?)
|
147
|
+
node.source_range.with(end_pos: if_node.loc.end.end_pos)
|
148
|
+
else
|
149
|
+
node.source_range
|
150
|
+
end
|
151
|
+
end
|
152
|
+
# rubocop:enable Metrics/AbcSize
|
153
|
+
|
154
|
+
def all_branches_body_missing?(node)
|
155
|
+
return false unless node.parent&.if_type?
|
156
|
+
|
157
|
+
node.parent.branches.compact.empty?
|
158
|
+
end
|
159
|
+
|
160
|
+
def deletion_range(range)
|
161
|
+
# Collect a range between the start of the `if` node and the next relevant node,
|
162
|
+
# including final new line.
|
163
|
+
# Based on `RangeHelp#range_by_whole_lines` but allows the `if` to not start
|
164
|
+
# on the first column.
|
165
|
+
buffer = @processed_source.buffer
|
166
|
+
|
167
|
+
last_line = buffer.source_line(range.last_line)
|
168
|
+
end_offset = last_line.length - range.last_column + 1
|
169
|
+
|
170
|
+
range.adjust(end_pos: end_offset).intersect(buffer.source_range)
|
65
171
|
end
|
66
172
|
end
|
67
173
|
end
|
@@ -6,18 +6,18 @@ module RuboCop
|
|
6
6
|
#
|
7
7
|
# This cop emulates the following Ruby warnings in Ruby 2.6.
|
8
8
|
#
|
9
|
+
# [source,console]
|
10
|
+
# ----
|
9
11
|
# % cat example.rb
|
10
12
|
# ERB.new('hi', nil, '-', '@output_buffer')
|
11
13
|
# % ruby -rerb example.rb
|
12
|
-
# example.rb:1: warning: Passing safe_level with the 2nd argument of
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# ERB.new(str,
|
18
|
-
#
|
19
|
-
# is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...)
|
20
|
-
# instead.
|
14
|
+
# example.rb:1: warning: Passing safe_level with the 2nd argument of ERB.new is
|
15
|
+
# deprecated. Do not use it, and specify other arguments as keyword arguments.
|
16
|
+
# example.rb:1: warning: Passing trim_mode with the 3rd argument of ERB.new is
|
17
|
+
# deprecated. Use keyword argument like ERB.new(str, trim_mode:...) instead.
|
18
|
+
# example.rb:1: warning: Passing eoutvar with the 4th argument of ERB.new is
|
19
|
+
# deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.
|
20
|
+
# ----
|
21
21
|
#
|
22
22
|
# Now non-keyword arguments other than first one are softly deprecated
|
23
23
|
# and will be removed when Ruby 2.5 becomes EOL.
|
@@ -58,6 +58,7 @@ module RuboCop
|
|
58
58
|
(node.str_type? && !node.loc.respond_to?(:begin)) || node.source_range.is?('__LINE__')
|
59
59
|
end
|
60
60
|
|
61
|
+
# rubocop:disable Metrics/MethodLength
|
61
62
|
def autocorrected_value(node)
|
62
63
|
case node.type
|
63
64
|
when :int
|
@@ -70,10 +71,13 @@ module RuboCop
|
|
70
71
|
autocorrected_value_for_symbol(node)
|
71
72
|
when :array
|
72
73
|
autocorrected_value_for_array(node)
|
74
|
+
when :nil
|
75
|
+
''
|
73
76
|
else
|
74
77
|
node.source.gsub('"', '\"')
|
75
78
|
end
|
76
79
|
end
|
80
|
+
# rubocop:enable Metrics/MethodLength
|
77
81
|
|
78
82
|
def autocorrected_value_for_string(node)
|
79
83
|
if node.source.start_with?("'", '%q')
|
@@ -30,6 +30,9 @@ module RuboCop
|
|
30
30
|
#
|
31
31
|
# # good
|
32
32
|
#
|
33
|
+
# # `class_eval`, `instance_eval`, `module_eval`, `class_exec`, `instance_exec`, and
|
34
|
+
# # `module_exec` blocks are allowed by default.
|
35
|
+
#
|
33
36
|
# def foo
|
34
37
|
# self.class.class_eval do
|
35
38
|
# def bar
|
@@ -54,7 +57,47 @@ module RuboCop
|
|
54
57
|
# end
|
55
58
|
# end
|
56
59
|
# end
|
60
|
+
#
|
61
|
+
# @example AllowedMethods: [] (default)
|
62
|
+
# # bad
|
63
|
+
# def do_something
|
64
|
+
# has_many :articles do
|
65
|
+
# def find_or_create_by_name(name)
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# @example AllowedMethods: ['has_many']
|
71
|
+
# # bad
|
72
|
+
# def do_something
|
73
|
+
# has_many :articles do
|
74
|
+
# def find_or_create_by_name(name)
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# @example AllowedPatterns: [] (default)
|
80
|
+
# # bad
|
81
|
+
# def foo(obj)
|
82
|
+
# obj.do_baz do
|
83
|
+
# def bar
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# @example AllowedPatterns: ['baz']
|
89
|
+
# # good
|
90
|
+
# def foo(obj)
|
91
|
+
# obj.do_baz do
|
92
|
+
# def bar
|
93
|
+
# end
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
57
97
|
class NestedMethodDefinition < Base
|
98
|
+
include AllowedMethods
|
99
|
+
include AllowedPattern
|
100
|
+
|
58
101
|
MSG = 'Method definitions must not be nested. Use `lambda` instead.'
|
59
102
|
|
60
103
|
def on_def(node)
|
@@ -77,7 +120,13 @@ module RuboCop
|
|
77
120
|
|
78
121
|
def scoping_method_call?(child)
|
79
122
|
child.sclass_type? || eval_call?(child) || exec_call?(child) ||
|
80
|
-
class_or_module_or_struct_new_call?(child)
|
123
|
+
class_or_module_or_struct_new_call?(child) || allowed_method_name?(child)
|
124
|
+
end
|
125
|
+
|
126
|
+
def allowed_method_name?(node)
|
127
|
+
name = node.method_name
|
128
|
+
|
129
|
+
allowed_method?(name) || matches_allowed_pattern?(name)
|
81
130
|
end
|
82
131
|
|
83
132
|
# @!method eval_call?(node)
|
@@ -25,13 +25,8 @@ module RuboCop
|
|
25
25
|
class NextWithoutAccumulator < Base
|
26
26
|
MSG = 'Use `next` with an accumulator argument in a `reduce`.'
|
27
27
|
|
28
|
-
# @!method on_body_of_reduce(node)
|
29
|
-
def_node_matcher :on_body_of_reduce, <<~PATTERN
|
30
|
-
(block (send _recv {:reduce :inject} !sym) _blockargs $(begin ...))
|
31
|
-
PATTERN
|
32
|
-
|
33
28
|
def on_block(node)
|
34
|
-
|
29
|
+
on_block_body_of_reduce(node) do |body|
|
35
30
|
void_next = body.each_node(:next).find do |n|
|
36
31
|
n.children.empty? && parent_block_node(n) == node
|
37
32
|
end
|
@@ -40,11 +35,35 @@ module RuboCop
|
|
40
35
|
end
|
41
36
|
end
|
42
37
|
|
38
|
+
def on_numblock(node)
|
39
|
+
on_numblock_body_of_reduce(node) do |body|
|
40
|
+
void_next = body.each_node(:next).find do |n|
|
41
|
+
n.children.empty? && parent_numblock_node(n) == node
|
42
|
+
end
|
43
|
+
|
44
|
+
add_offense(void_next) if void_next
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
43
48
|
private
|
44
49
|
|
50
|
+
# @!method on_block_body_of_reduce(node)
|
51
|
+
def_node_matcher :on_block_body_of_reduce, <<~PATTERN
|
52
|
+
(block (send _recv {:reduce :inject} !sym) _blockargs $(begin ...))
|
53
|
+
PATTERN
|
54
|
+
|
55
|
+
# @!method on_numblock_body_of_reduce(node)
|
56
|
+
def_node_matcher :on_numblock_body_of_reduce, <<~PATTERN
|
57
|
+
(numblock (send _recv {:reduce :inject} !sym) _argscount $(begin ...))
|
58
|
+
PATTERN
|
59
|
+
|
45
60
|
def parent_block_node(node)
|
46
61
|
node.each_ancestor(:block).first
|
47
62
|
end
|
63
|
+
|
64
|
+
def parent_numblock_node(node)
|
65
|
+
node.each_ancestor(:numblock).first
|
66
|
+
end
|
48
67
|
end
|
49
68
|
end
|
50
69
|
end
|
@@ -99,19 +99,19 @@ module RuboCop
|
|
99
99
|
end
|
100
100
|
|
101
101
|
def register_offense(node, exist_node)
|
102
|
-
unless force_method?(node)
|
103
|
-
add_offense(node,
|
104
|
-
message: format(MSG_CHANGE_FORCE_METHOD,
|
105
|
-
method_name: replacement_method(node)))
|
106
|
-
end
|
102
|
+
add_offense(node, message: message_change_force_method(node)) unless force_method?(node)
|
107
103
|
|
108
104
|
range = range_between(node.parent.loc.keyword.begin_pos,
|
109
105
|
exist_node.loc.expression.end_pos)
|
110
106
|
add_offense(range, message: message_remove_file_exist_check(exist_node)) do |corrector|
|
111
|
-
autocorrect(corrector, node, range)
|
107
|
+
autocorrect(corrector, node, range) unless node.parent.elsif?
|
112
108
|
end
|
113
109
|
end
|
114
110
|
|
111
|
+
def message_change_force_method(node)
|
112
|
+
format(MSG_CHANGE_FORCE_METHOD, method_name: replacement_method(node))
|
113
|
+
end
|
114
|
+
|
115
115
|
def message_remove_file_exist_check(node)
|
116
116
|
receiver, method_name = receiver_and_method_name(node)
|
117
117
|
format(MSG_REMOVE_FILE_EXIST_CHECK, receiver: receiver, method_name: method_name)
|
@@ -74,6 +74,18 @@ module RuboCop
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
+
def on_numblock(node)
|
78
|
+
return if target_ruby_version >= 3.0
|
79
|
+
return unless node.body
|
80
|
+
return unless unsorted_dir_loop?(node.send_node)
|
81
|
+
|
82
|
+
node.argument_list
|
83
|
+
.filter { |argument| var_is_required?(node.body, argument.name) }
|
84
|
+
.each do
|
85
|
+
add_offense(node.send_node) { |corrector| correct_block(corrector, node.send_node) }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
77
89
|
def on_block_pass(node)
|
78
90
|
return if target_ruby_version >= 3.0
|
79
91
|
return unless method_require?(node)
|
@@ -16,8 +16,8 @@ module RuboCop
|
|
16
16
|
# NOTE: Some values cannot be converted properly using one of the `Kernel`
|
17
17
|
# method (for instance, `Time` and `DateTime` values are allowed by this
|
18
18
|
# cop by default). Similarly, Rails' duration methods do not work well
|
19
|
-
# with `Integer()` and can be
|
20
|
-
# there are no methods to
|
19
|
+
# with `Integer()` and can be allowed with `AllowedMethods`. By default,
|
20
|
+
# there are no methods to allowed.
|
21
21
|
#
|
22
22
|
# @safety
|
23
23
|
# Autocorrection is unsafe because it is not guaranteed that the
|
@@ -46,12 +46,22 @@ module RuboCop
|
|
46
46
|
# foo.try { |i| Float(i) }
|
47
47
|
# bar.send { |i| Complex(i) }
|
48
48
|
#
|
49
|
-
# @example
|
49
|
+
# @example AllowedMethods: [] (default)
|
50
50
|
#
|
51
51
|
# # bad
|
52
52
|
# 10.minutes.to_i
|
53
53
|
#
|
54
|
-
# @example
|
54
|
+
# @example AllowedMethods: [minutes]
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# 10.minutes.to_i
|
58
|
+
#
|
59
|
+
# @example AllowedPatterns: [] (default)
|
60
|
+
#
|
61
|
+
# # bad
|
62
|
+
# 10.minutes.to_i
|
63
|
+
#
|
64
|
+
# @example AllowedPatterns: ['min*']
|
55
65
|
#
|
56
66
|
# # good
|
57
67
|
# 10.minutes.to_i
|
@@ -62,7 +72,8 @@ module RuboCop
|
|
62
72
|
# Time.now.to_datetime.to_i
|
63
73
|
class NumberConversion < Base
|
64
74
|
extend AutoCorrector
|
65
|
-
include
|
75
|
+
include AllowedMethods
|
76
|
+
include AllowedPattern
|
66
77
|
|
67
78
|
CONVERSION_METHOD_CLASS_MAPPING = {
|
68
79
|
to_i: "#{Integer.name}(%<number_object>s, 10)",
|
@@ -97,7 +108,7 @@ module RuboCop
|
|
97
108
|
|
98
109
|
def handle_conversion_method(node)
|
99
110
|
to_method(node) do |receiver, to_method|
|
100
|
-
next if receiver.nil? ||
|
111
|
+
next if receiver.nil? || allow_receiver?(receiver)
|
101
112
|
|
102
113
|
message = format(
|
103
114
|
MSG,
|
@@ -141,9 +152,10 @@ module RuboCop
|
|
141
152
|
corrector.remove(node.loc.end)
|
142
153
|
end
|
143
154
|
|
144
|
-
def
|
155
|
+
def allow_receiver?(receiver)
|
145
156
|
if receiver.numeric_type? || (receiver.send_type? &&
|
146
|
-
(conversion_method?(receiver.method_name) ||
|
157
|
+
(conversion_method?(receiver.method_name) ||
|
158
|
+
allowed_method_name?(receiver.method_name)))
|
147
159
|
true
|
148
160
|
elsif (receiver = top_receiver(receiver))
|
149
161
|
receiver.const_type? && ignored_class?(receiver.const_name)
|
@@ -152,6 +164,10 @@ module RuboCop
|
|
152
164
|
end
|
153
165
|
end
|
154
166
|
|
167
|
+
def allowed_method_name?(name)
|
168
|
+
allowed_method?(name) || matches_allowed_pattern?(name)
|
169
|
+
end
|
170
|
+
|
155
171
|
def top_receiver(node)
|
156
172
|
receiver = node
|
157
173
|
receiver = receiver.receiver until receiver.receiver.nil?
|
@@ -7,6 +7,9 @@ module RuboCop
|
|
7
7
|
# Checks the proper ordering of magic comments and whether
|
8
8
|
# a magic comment is not placed before a shebang.
|
9
9
|
#
|
10
|
+
# @safety
|
11
|
+
# This cop's autocorrection is unsafe because file encoding may change.
|
12
|
+
#
|
10
13
|
# @example
|
11
14
|
# # bad
|
12
15
|
#
|
@@ -61,7 +64,7 @@ module RuboCop
|
|
61
64
|
def magic_comment_lines
|
62
65
|
lines = [nil, nil]
|
63
66
|
|
64
|
-
|
67
|
+
leading_magic_comments.each.with_index do |comment, index|
|
65
68
|
if comment.encoding_specified?
|
66
69
|
lines[0] = index
|
67
70
|
elsif comment.frozen_string_literal_specified?
|
@@ -73,10 +76,6 @@ module RuboCop
|
|
73
76
|
|
74
77
|
lines
|
75
78
|
end
|
76
|
-
|
77
|
-
def magic_comments
|
78
|
-
leading_comment_lines.map { |line| MagicComment.parse(line) }
|
79
|
-
end
|
80
79
|
end
|
81
80
|
end
|
82
81
|
end
|
@@ -209,7 +209,12 @@ module RuboCop
|
|
209
209
|
|
210
210
|
add_offense(location, message: message(cop_names)) do |corrector|
|
211
211
|
range = comment_range_with_surrounding_space(location, comment.loc.expression)
|
212
|
-
|
212
|
+
|
213
|
+
if leave_free_comment?(comment, range)
|
214
|
+
corrector.replace(range, ' # ')
|
215
|
+
else
|
216
|
+
corrector.remove(range)
|
217
|
+
end
|
213
218
|
end
|
214
219
|
end
|
215
220
|
|
@@ -227,6 +232,12 @@ module RuboCop
|
|
227
232
|
end
|
228
233
|
end
|
229
234
|
|
235
|
+
def leave_free_comment?(comment, range)
|
236
|
+
free_comment = comment.text.gsub(range.source.strip, '')
|
237
|
+
|
238
|
+
!free_comment.empty? && !free_comment.start_with?('#')
|
239
|
+
end
|
240
|
+
|
230
241
|
def cop_range(comment, cop)
|
231
242
|
cop = remove_department_marker(cop)
|
232
243
|
matching_range(comment.loc.expression, cop) ||
|
@@ -41,6 +41,7 @@ module RuboCop
|
|
41
41
|
return unless (receiver = node.receiver)
|
42
42
|
return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
|
43
43
|
return unless GLOB_METHODS.include?(receiver.method_name)
|
44
|
+
return if multiple_argument?(receiver)
|
44
45
|
|
45
46
|
selector = node.loc.selector
|
46
47
|
|
@@ -49,6 +50,12 @@ module RuboCop
|
|
49
50
|
corrector.remove(node.loc.dot)
|
50
51
|
end
|
51
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def multiple_argument?(glob_method)
|
57
|
+
glob_method.arguments.count >= 2 || glob_method.first_argument&.splat_type?
|
58
|
+
end
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
@@ -6,13 +6,22 @@ module RuboCop
|
|
6
6
|
# Checks for unnecessary `require` statement.
|
7
7
|
#
|
8
8
|
# The following features are unnecessary `require` statement because
|
9
|
-
# they are already loaded.
|
9
|
+
# they are already loaded. e.g. Ruby 2.2:
|
10
10
|
#
|
11
11
|
# ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
|
12
12
|
# ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
|
13
13
|
# ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# Below are the features that each `TargetRubyVersion` targets.
|
16
|
+
#
|
17
|
+
# * 2.0+ ... `enumerator`
|
18
|
+
# * 2.1+ ... `thread`
|
19
|
+
# * 2.2+ ... Add `rational` and `complex` above
|
20
|
+
# * 2.5+ ... Add `pp` above
|
21
|
+
# * 2.7+ ... Add `ruby2_keywords` above
|
22
|
+
# * 3.1+ ... Add `fiber` above
|
23
|
+
#
|
24
|
+
# This cop target those features.
|
16
25
|
#
|
17
26
|
# @example
|
18
27
|
# # bad
|
@@ -24,21 +33,19 @@ module RuboCop
|
|
24
33
|
class RedundantRequireStatement < Base
|
25
34
|
include RangeHelp
|
26
35
|
extend AutoCorrector
|
27
|
-
extend TargetRubyVersion
|
28
|
-
|
29
|
-
minimum_target_ruby_version 2.2
|
30
36
|
|
31
37
|
MSG = 'Remove unnecessary `require` statement.'
|
32
38
|
RESTRICT_ON_SEND = %i[require].freeze
|
39
|
+
RUBY_22_LOADED_FEATURES = %w[rational complex].freeze
|
33
40
|
|
34
|
-
# @!method
|
35
|
-
def_node_matcher :
|
41
|
+
# @!method redundant_require_statement?(node)
|
42
|
+
def_node_matcher :redundant_require_statement?, <<~PATTERN
|
36
43
|
(send nil? :require
|
37
|
-
(str
|
44
|
+
(str #redundant_feature?))
|
38
45
|
PATTERN
|
39
46
|
|
40
47
|
def on_send(node)
|
41
|
-
return unless
|
48
|
+
return unless redundant_require_statement?(node)
|
42
49
|
|
43
50
|
add_offense(node) do |corrector|
|
44
51
|
range = range_with_surrounding_space(node.loc.expression, side: :right)
|
@@ -46,6 +53,19 @@ module RuboCop
|
|
46
53
|
corrector.remove(range)
|
47
54
|
end
|
48
55
|
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
60
|
+
def redundant_feature?(feature_name)
|
61
|
+
feature_name == 'enumerator' ||
|
62
|
+
(target_ruby_version >= 2.1 && feature_name == 'thread') ||
|
63
|
+
(target_ruby_version >= 2.2 && RUBY_22_LOADED_FEATURES.include?(feature_name)) ||
|
64
|
+
(target_ruby_version >= 2.5 && feature_name == 'pp') ||
|
65
|
+
(target_ruby_version >= 2.7 && feature_name == 'ruby2_keywords') ||
|
66
|
+
(target_ruby_version >= 3.1 && feature_name == 'fiber')
|
67
|
+
end
|
68
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
49
69
|
end
|
50
70
|
end
|
51
71
|
end
|
@@ -7,6 +7,11 @@ module RuboCop
|
|
7
7
|
# `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
|
8
8
|
# are checked by default. These are customizable with `AllowedMethods` option.
|
9
9
|
#
|
10
|
+
# The `AllowedMethods` option specifies nil-safe methods,
|
11
|
+
# in other words, it is a method that is allowed to skip safe navigation.
|
12
|
+
# Note that the `AllowedMethod` option is not an option that specifies methods
|
13
|
+
# for which to suppress (allow) this cop's check.
|
14
|
+
#
|
10
15
|
# In the example below, the safe navigation operator (`&.`) is unnecessary
|
11
16
|
# because `NilClass` has methods like `respond_to?` and `is_a?`.
|
12
17
|
#
|
@@ -35,12 +40,13 @@ module RuboCop
|
|
35
40
|
# # good - without `&.` this will always return `true`
|
36
41
|
# foo&.respond_to?(:to_a)
|
37
42
|
#
|
38
|
-
# @example AllowedMethods: [
|
43
|
+
# @example AllowedMethods: [nil_safe_method]
|
39
44
|
# # bad
|
40
|
-
# do_something if attrs&.
|
45
|
+
# do_something if attrs&.nil_safe_method(:[])
|
41
46
|
#
|
42
47
|
# # good
|
43
|
-
# do_something if attrs
|
48
|
+
# do_something if attrs.nil_safe_method(:[])
|
49
|
+
# do_something if attrs&.not_nil_safe_method(:[])
|
44
50
|
#
|
45
51
|
class RedundantSafeNavigation < Base
|
46
52
|
include AllowedMethods
|
@@ -33,16 +33,6 @@ module RuboCop
|
|
33
33
|
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'
|
34
34
|
MSG_WITH_INDEX = 'Remove redundant `with_index`.'
|
35
35
|
|
36
|
-
# @!method redundant_with_index?(node)
|
37
|
-
def_node_matcher :redundant_with_index?, <<~PATTERN
|
38
|
-
(block
|
39
|
-
$(send
|
40
|
-
_ {:each_with_index :with_index} ...)
|
41
|
-
(args
|
42
|
-
(arg _))
|
43
|
-
...)
|
44
|
-
PATTERN
|
45
|
-
|
46
36
|
def on_block(node)
|
47
37
|
return unless (send = redundant_with_index?(node))
|
48
38
|
|
@@ -58,8 +48,21 @@ module RuboCop
|
|
58
48
|
end
|
59
49
|
end
|
60
50
|
|
51
|
+
alias on_numblock on_block
|
52
|
+
|
61
53
|
private
|
62
54
|
|
55
|
+
# @!method redundant_with_index?(node)
|
56
|
+
def_node_matcher :redundant_with_index?, <<~PATTERN
|
57
|
+
{
|
58
|
+
(block
|
59
|
+
$(send _ {:each_with_index :with_index} ...)
|
60
|
+
(args (arg _)) ...)
|
61
|
+
(numblock
|
62
|
+
$(send _ {:each_with_index :with_index} ...) 1 ...)
|
63
|
+
}
|
64
|
+
PATTERN
|
65
|
+
|
63
66
|
def message(node)
|
64
67
|
if node.method?(:each_with_index)
|
65
68
|
MSG_EACH_WITH_INDEX
|