rubocop 1.8.0 → 1.11.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 +14 -13
- data/assets/output.html.erb +1 -1
- data/config/default.yml +89 -22
- data/config/obsoletion.yml +4 -0
- data/lib/rubocop.rb +9 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
- data/lib/rubocop/cli/command/execute_runner.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
- data/lib/rubocop/config.rb +5 -2
- data/lib/rubocop/config_loader.rb +7 -14
- data/lib/rubocop/config_store.rb +12 -1
- data/lib/rubocop/cop/base.rb +2 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
- data/lib/rubocop/cop/exclude_limit.rb +26 -0
- data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
- data/lib/rubocop/cop/generator.rb +3 -5
- data/lib/rubocop/cop/internal_affairs.rb +6 -1
- data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +8 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
- data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/line_length.rb +2 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
- data/lib/rubocop/cop/lint/debugger.rb +60 -14
- data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
- data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
- data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +43 -6
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
- data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
- data/lib/rubocop/cop/lint/rand_one.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -3
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
- data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +103 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
- data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
- data/lib/rubocop/cop/lint/useless_times.rb +3 -0
- data/lib/rubocop/cop/message_annotator.rb +4 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/module_length.rb +1 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -0
- data/lib/rubocop/cop/mixin/code_length.rb +3 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
- data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
- data/lib/rubocop/cop/mixin/def_node.rb +1 -0
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
- data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
- data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
- data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
- data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +5 -1
- data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
- data/lib/rubocop/cop/naming/constant_name.rb +2 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -0
- data/lib/rubocop/cop/naming/method_name.rb +3 -0
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +38 -5
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/registry.rb +1 -1
- data/lib/rubocop/cop/security/eval.rb +1 -0
- data/lib/rubocop/cop/security/json_load.rb +1 -0
- data/lib/rubocop/cop/security/marshal_load.rb +1 -0
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/security/yaml_load.rb +1 -0
- data/lib/rubocop/cop/severity.rb +3 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
- data/lib/rubocop/cop/style/alias.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
- data/lib/rubocop/cop/style/array_coercion.rb +2 -0
- data/lib/rubocop/cop/style/array_join.rb +1 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -0
- data/lib/rubocop/cop/style/case_equality.rb +2 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -0
- data/lib/rubocop/cop/style/collection_compact.rb +2 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
- data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
- data/lib/rubocop/cop/style/date_time.rb +3 -0
- data/lib/rubocop/cop/style/dir.rb +1 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -9
- data/lib/rubocop/cop/style/documentation.rb +5 -0
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/double_negation.rb +3 -2
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
- data/lib/rubocop/cop/style/each_with_object.rb +1 -0
- data/lib/rubocop/cop/style/empty_literal.rb +9 -0
- data/lib/rubocop/cop/style/endless_method.rb +1 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +140 -49
- data/lib/rubocop/cop/style/even_odd.rb +1 -0
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +12 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
- data/lib/rubocop/cop/style/float_division.rb +7 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -0
- data/lib/rubocop/cop/style/format_string_token.rb +19 -2
- data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
- data/lib/rubocop/cop/style/hash_except.rb +1 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
- data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
- data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
- data/lib/rubocop/cop/style/min_max.rb +1 -0
- data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
- data/lib/rubocop/cop/style/module_function.rb +5 -0
- data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
- data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
- data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
- data/lib/rubocop/cop/style/option_hash.rb +1 -0
- data/lib/rubocop/cop/style/or_assignment.rb +2 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +7 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
- data/lib/rubocop/cop/style/return_nil.rb +6 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
- data/lib/rubocop/cop/style/sample.rb +1 -0
- data/lib/rubocop/cop/style/signal_exception.rb +3 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +2 -1
- data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
- data/lib/rubocop/cop/style/strip.rb +1 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
- data/lib/rubocop/cop/style/unless_logical_operators.rb +99 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -0
- data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +30 -1
- data/lib/rubocop/name_similarity.rb +1 -1
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +5 -2
- data/lib/rubocop/runner.rb +1 -0
- data/lib/rubocop/target_ruby.rb +21 -13
- data/lib/rubocop/version.rb +2 -2
- metadata +21 -7
@@ -85,7 +85,9 @@ module RuboCop
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def on_kwbegin(node)
|
88
|
-
return if
|
88
|
+
return if empty_begin?(node) ||
|
89
|
+
contain_rescue_or_ensure?(node) ||
|
90
|
+
valid_context_using_only_begin?(node)
|
89
91
|
|
90
92
|
register_offense(node)
|
91
93
|
end
|
@@ -99,6 +101,10 @@ module RuboCop
|
|
99
101
|
end
|
100
102
|
end
|
101
103
|
|
104
|
+
def empty_begin?(node)
|
105
|
+
node.children.empty?
|
106
|
+
end
|
107
|
+
|
102
108
|
def contain_rescue_or_ensure?(node)
|
103
109
|
first_child = node.children.first
|
104
110
|
|
@@ -53,10 +53,12 @@ module RuboCop
|
|
53
53
|
format(MSG, msg: msg)
|
54
54
|
end
|
55
55
|
|
56
|
+
# @!method redundant_condition?(node)
|
56
57
|
def_node_matcher :redundant_condition?, <<~RUBY
|
57
58
|
(if (send _ #{COMPARISON_OPERATOR_MATCHER} _) true false)
|
58
59
|
RUBY
|
59
60
|
|
61
|
+
# @!method redundant_condition_inverted?(node)
|
60
62
|
def_node_matcher :redundant_condition_inverted?, <<~RUBY
|
61
63
|
(if (send _ #{COMPARISON_OPERATOR_MATCHER} _) false true)
|
62
64
|
RUBY
|
@@ -53,10 +53,12 @@ module RuboCop
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
# @!method exploded?(node)
|
56
57
|
def_node_matcher :exploded?, <<~PATTERN
|
57
58
|
(send nil? ${:raise :fail} (const {nil? cbase} :RuntimeError) $_)
|
58
59
|
PATTERN
|
59
60
|
|
61
|
+
# @!method compact?(node)
|
60
62
|
def_node_matcher :compact?, <<~PATTERN
|
61
63
|
(send nil? {:raise :fail} $(send (const {nil? cbase} :RuntimeError) :new $_))
|
62
64
|
PATTERN
|
@@ -38,6 +38,7 @@ module RuboCop
|
|
38
38
|
|
39
39
|
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
40
40
|
|
41
|
+
# @!method redundant_fetch_block_candidate?(node)
|
41
42
|
def_node_matcher :redundant_fetch_block_candidate?, <<~PATTERN
|
42
43
|
(block
|
43
44
|
$(send _ :fetch _)
|
@@ -78,6 +79,7 @@ module RuboCop
|
|
78
79
|
rails_cache?(send.receiver)
|
79
80
|
end
|
80
81
|
|
82
|
+
# @!method rails_cache?(node)
|
81
83
|
def_node_matcher :rails_cache?, <<~PATTERN
|
82
84
|
(send (const _ :Rails) :cache)
|
83
85
|
PATTERN
|
@@ -30,6 +30,7 @@ module RuboCop
|
|
30
30
|
MSG = 'Redundant `.rb` file extension detected.'
|
31
31
|
RESTRICT_ON_SEND = %i[require require_relative].freeze
|
32
32
|
|
33
|
+
# @!method require_call?(node)
|
33
34
|
def_node_matcher :require_call?, <<~PATTERN
|
34
35
|
(send nil? {:require :require_relative} $str_type?)
|
35
36
|
PATTERN
|
@@ -17,11 +17,20 @@ module RuboCop
|
|
17
17
|
include Parentheses
|
18
18
|
extend AutoCorrector
|
19
19
|
|
20
|
+
# @!method square_brackets?(node)
|
20
21
|
def_node_matcher :square_brackets?,
|
21
22
|
'(send {(send _recv _msg) str array hash} :[] ...)'
|
23
|
+
|
24
|
+
# @!method range_end?(node)
|
22
25
|
def_node_matcher :range_end?, '^^{irange erange}'
|
26
|
+
|
27
|
+
# @!method method_node_and_args(node)
|
23
28
|
def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
|
29
|
+
|
30
|
+
# @!method rescue?(node)
|
24
31
|
def_node_matcher :rescue?, '{^resbody ^^resbody}'
|
32
|
+
|
33
|
+
# @!method arg_in_call_with_block?(node)
|
25
34
|
def_node_matcher :arg_in_call_with_block?,
|
26
35
|
'^^(block (send _ _ equal?(%0) ...) ...)'
|
27
36
|
|
@@ -109,6 +118,7 @@ module RuboCop
|
|
109
118
|
check_send(begin_node, node) if node.call_type?
|
110
119
|
end
|
111
120
|
|
121
|
+
# @!method interpolation?(node)
|
112
122
|
def_node_matcher :interpolation?, '[^begin ^^dstr]'
|
113
123
|
|
114
124
|
def check_send(begin_node, node)
|
@@ -220,14 +230,17 @@ module RuboCop
|
|
220
230
|
first_yield_argument?(node)
|
221
231
|
end
|
222
232
|
|
233
|
+
# @!method first_send_argument?(node)
|
223
234
|
def_node_matcher :first_send_argument?, <<~PATTERN
|
224
235
|
^(send _ _ equal?(%0) ...)
|
225
236
|
PATTERN
|
226
237
|
|
238
|
+
# @!method first_super_argument?(node)
|
227
239
|
def_node_matcher :first_super_argument?, <<~PATTERN
|
228
240
|
^(super equal?(%0) ...)
|
229
241
|
PATTERN
|
230
242
|
|
243
|
+
# @!method first_yield_argument?(node)
|
231
244
|
def_node_matcher :first_yield_argument?, <<~PATTERN
|
232
245
|
^(yield equal?(%0) ...)
|
233
246
|
PATTERN
|
@@ -82,6 +82,7 @@ module RuboCop
|
|
82
82
|
METHODS_RETURNING_SELF.include?(method_name)
|
83
83
|
end
|
84
84
|
|
85
|
+
# @!method redundant_self_assignment?(node, method_name)
|
85
86
|
def_node_matcher :redundant_self_assignment?, <<~PATTERN
|
86
87
|
(send
|
87
88
|
(self) _
|
@@ -91,6 +92,7 @@ module RuboCop
|
|
91
92
|
...))
|
92
93
|
PATTERN
|
93
94
|
|
95
|
+
# @!method redundant_nonself_assignment?(node, receiver, method_name)
|
94
96
|
def_node_matcher :redundant_nonself_assignment?, <<~PATTERN
|
95
97
|
(send
|
96
98
|
%1 _
|
@@ -81,10 +81,12 @@ module RuboCop
|
|
81
81
|
MSG_EXPLICIT = 'Avoid rescuing without specifying ' \
|
82
82
|
'an error class.'
|
83
83
|
|
84
|
+
# @!method rescue_without_error_class?(node)
|
84
85
|
def_node_matcher :rescue_without_error_class?, <<~PATTERN
|
85
86
|
(resbody nil? _ _)
|
86
87
|
PATTERN
|
87
88
|
|
89
|
+
# @!method rescue_standard_error?(node)
|
88
90
|
def_node_matcher :rescue_standard_error?, <<~PATTERN
|
89
91
|
(resbody $(array (const {nil? cbase} :StandardError)) _ _)
|
90
92
|
PATTERN
|
@@ -35,7 +35,10 @@ module RuboCop
|
|
35
35
|
RETURN_MSG = 'Use `return` instead of `return nil`.'
|
36
36
|
RETURN_NIL_MSG = 'Use `return nil` instead of `return`.'
|
37
37
|
|
38
|
+
# @!method return_node?(node)
|
38
39
|
def_node_matcher :return_node?, '(return)'
|
40
|
+
|
41
|
+
# @!method return_nil_node?(node)
|
39
42
|
def_node_matcher :return_nil_node?, '(return nil)'
|
40
43
|
|
41
44
|
def on_return(node)
|
@@ -79,7 +82,10 @@ module RuboCop
|
|
79
82
|
node.def_type? || node.defs_type? || node.lambda?
|
80
83
|
end
|
81
84
|
|
85
|
+
# @!method chained_send?(node)
|
82
86
|
def_node_matcher :chained_send?, '(send !nil? ...)'
|
87
|
+
|
88
|
+
# @!method define_method?(node)
|
83
89
|
def_node_matcher :define_method?, <<~PATTERN
|
84
90
|
(send _ {:define_method :define_singleton_method} _)
|
85
91
|
PATTERN
|
@@ -74,6 +74,7 @@ module RuboCop
|
|
74
74
|
|
75
75
|
# if format: (if checked_variable body nil)
|
76
76
|
# unless format: (if checked_variable nil body)
|
77
|
+
# @!method modifier_if_safe_navigation_candidate(node)
|
77
78
|
def_node_matcher :modifier_if_safe_navigation_candidate, <<~PATTERN
|
78
79
|
{
|
79
80
|
(if {
|
@@ -88,6 +89,7 @@ module RuboCop
|
|
88
89
|
}
|
89
90
|
PATTERN
|
90
91
|
|
92
|
+
# @!method not_nil_check?(node)
|
91
93
|
def_node_matcher :not_nil_check?, '(send (send $_ :nil?) :!)'
|
92
94
|
|
93
95
|
def on_if(node)
|
@@ -33,6 +33,7 @@ module RuboCop
|
|
33
33
|
MSG = 'Use `%<correct>s` instead of `%<incorrect>s`.'
|
34
34
|
RESTRICT_ON_SEND = %i[first last [] at slice].freeze
|
35
35
|
|
36
|
+
# @!method sample_candidate?(node)
|
36
37
|
def_node_matcher :sample_candidate?, <<~PATTERN
|
37
38
|
(send $(send _ :shuffle $...) ${:#{RESTRICT_ON_SEND.join(' :')}} $...)
|
38
39
|
PATTERN
|
@@ -114,7 +114,10 @@ module RuboCop
|
|
114
114
|
|
115
115
|
RESTRICT_ON_SEND = %i[raise fail].freeze
|
116
116
|
|
117
|
+
# @!method kernel_call?(node, name)
|
117
118
|
def_node_matcher :kernel_call?, '(send (const {nil? cbase} :Kernel) %1 ...)'
|
119
|
+
|
120
|
+
# @!method custom_fail_methods(node)
|
118
121
|
def_node_search :custom_fail_methods,
|
119
122
|
'{(def :fail ...) (defs _ :fail ...)}'
|
120
123
|
|
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
#
|
9
9
|
# Endless methods added in Ruby 3.0 are also accepted by this cop.
|
10
10
|
#
|
11
|
-
# If `Style/EndlessMethod` is enabled with `EnforcedStyle:
|
11
|
+
# If `Style/EndlessMethod` is enabled with `EnforcedStyle: allow_single_line` or
|
12
12
|
# `allow_always`, single-line methods will be auto-corrected to endless
|
13
13
|
# methods if there is only one statement in the body.
|
14
14
|
#
|
@@ -63,6 +63,8 @@ module RuboCop
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def correct_to_endless?(body_node)
|
66
|
+
return false if target_ruby_version < 3.0
|
67
|
+
|
66
68
|
endless_method_config = config.for_cop('Style/EndlessMethod')
|
67
69
|
|
68
70
|
return false unless endless_method_config['Enabled']
|
@@ -89,7 +91,8 @@ module RuboCop
|
|
89
91
|
end
|
90
92
|
|
91
93
|
def correct_to_endless(corrector, node)
|
92
|
-
|
94
|
+
arguments = node.arguments.any? ? node.arguments.source : '()'
|
95
|
+
replacement = "def #{node.method_name}#{arguments} = #{node.body.source}"
|
93
96
|
corrector.replace(node, replacement)
|
94
97
|
end
|
95
98
|
|
@@ -63,13 +63,13 @@ module RuboCop
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def autocorrect(corrector, node, if_branch)
|
66
|
+
corrector.wrap(node.condition, '(', ')') if node.condition.or_type?
|
67
|
+
|
66
68
|
if node.unless?
|
67
69
|
corrector.replace(node.loc.keyword, 'if')
|
68
70
|
corrector.insert_before(node.condition, '!')
|
69
71
|
end
|
70
72
|
|
71
|
-
corrector.wrap(node.condition, '(', ')') if node.condition.or_type?
|
72
|
-
|
73
73
|
and_operator = if_branch.unless? ? ' && !' : ' && '
|
74
74
|
if if_branch.modifier_form?
|
75
75
|
correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
|
@@ -80,8 +80,11 @@ module RuboCop
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
|
83
|
+
outer_condition = node.condition
|
84
|
+
correct_outer_condition(corrector, outer_condition)
|
85
|
+
|
83
86
|
condition = if_branch.condition
|
84
|
-
corrector.insert_after(
|
87
|
+
corrector.insert_after(outer_condition, replacement_condition(and_operator, condition))
|
85
88
|
|
86
89
|
range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
|
87
90
|
corrector.remove(range_with_surrounding_space(range: range, newlines: false))
|
@@ -106,8 +109,29 @@ module RuboCop
|
|
106
109
|
corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
|
107
110
|
end
|
108
111
|
|
112
|
+
def correct_outer_condition(corrector, condition)
|
113
|
+
return unless requrie_parentheses?(condition)
|
114
|
+
|
115
|
+
end_pos = condition.loc.selector.end_pos
|
116
|
+
begin_pos = condition.first_argument.source_range.begin_pos
|
117
|
+
return if end_pos > begin_pos
|
118
|
+
|
119
|
+
corrector.replace(range_between(end_pos, begin_pos), '(')
|
120
|
+
corrector.insert_after(condition.last_argument.source_range, ')')
|
121
|
+
end
|
122
|
+
|
123
|
+
def requrie_parentheses?(condition)
|
124
|
+
condition.send_type? && !condition.arguments.empty? && !condition.parenthesized?
|
125
|
+
end
|
126
|
+
|
127
|
+
def arguments_range(node)
|
128
|
+
range_between(
|
129
|
+
node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
109
133
|
def wrap_condition?(node)
|
110
|
-
node.or_type? ||
|
134
|
+
node.and_type? || node.or_type? ||
|
111
135
|
(node.send_type? && node.arguments.any? && !node.parenthesized?)
|
112
136
|
end
|
113
137
|
|
@@ -81,13 +81,13 @@ module RuboCop
|
|
81
81
|
}
|
82
82
|
|
83
83
|
PERL_VARS =
|
84
|
-
|
84
|
+
ENGLISH_VARS.flat_map { |k, vs| vs.map { |v| [v, [k]] } }.to_h
|
85
85
|
|
86
86
|
ENGLISH_VARS.merge!(
|
87
|
-
|
87
|
+
ENGLISH_VARS.flat_map { |_, vs| vs.map { |v| [v, [v]] } }.to_h
|
88
88
|
)
|
89
89
|
PERL_VARS.merge!(
|
90
|
-
|
90
|
+
PERL_VARS.flat_map { |_, vs| vs.map { |v| [v, [v]] } }.to_h
|
91
91
|
)
|
92
92
|
ENGLISH_VARS.each_value(&:freeze).freeze
|
93
93
|
PERL_VARS.each_value(&:freeze).freeze
|
@@ -22,6 +22,7 @@ module RuboCop
|
|
22
22
|
'Use `warn` instead of `%<bad>s` to allow such output to be disabled.'
|
23
23
|
RESTRICT_ON_SEND = %i[puts].freeze
|
24
24
|
|
25
|
+
# @!method stderr_puts?(node)
|
25
26
|
def_node_matcher :stderr_puts?, <<~PATTERN
|
26
27
|
(send
|
27
28
|
{(gvar #stderr_gvar?) (const {nil? cbase} :STDERR)}
|
@@ -34,6 +34,7 @@ module RuboCop
|
|
34
34
|
MSG = 'Prefer string interpolation to string concatenation.'
|
35
35
|
RESTRICT_ON_SEND = %i[+].freeze
|
36
36
|
|
37
|
+
# @!method string_concatenation?(node)
|
37
38
|
def_node_matcher :string_concatenation?, <<~PATTERN
|
38
39
|
{
|
39
40
|
(send str_type? :+ _)
|
@@ -116,7 +117,7 @@ module RuboCop
|
|
116
117
|
parts.map do |part|
|
117
118
|
if part.str_type?
|
118
119
|
if single_quoted?(part)
|
119
|
-
part.value.gsub(
|
120
|
+
part.value.gsub(/(\\|")/, '\\\\\&')
|
120
121
|
else
|
121
122
|
part.value.inspect[1..-2]
|
122
123
|
end
|
@@ -17,10 +17,12 @@ module RuboCop
|
|
17
17
|
|
18
18
|
MSG = 'Prefer symbols instead of strings as hash keys.'
|
19
19
|
|
20
|
+
# @!method string_hash_key?(node)
|
20
21
|
def_node_matcher :string_hash_key?, <<~PATTERN
|
21
22
|
(pair (str _) _)
|
22
23
|
PATTERN
|
23
24
|
|
25
|
+
# @!method receive_environments_method?(node)
|
24
26
|
def_node_matcher :receive_environments_method?, <<~PATTERN
|
25
27
|
{
|
26
28
|
^^(send (const {nil? cbase} :IO) :popen ...)
|
@@ -20,6 +20,7 @@ module RuboCop
|
|
20
20
|
MSG = 'Use `strip` instead of `%<methods>s`.'
|
21
21
|
RESTRICT_ON_SEND = %i[lstrip rstrip].freeze
|
22
22
|
|
23
|
+
# @!method lstrip_rstrip(node)
|
23
24
|
def_node_matcher :lstrip_rstrip, <<~PATTERN
|
24
25
|
{(send $(send _ $:rstrip) $:lstrip)
|
25
26
|
(send $(send _ $:lstrip) $:rstrip)}
|