rubocop 1.57.1 → 1.65.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/LICENSE.txt +1 -1
- data/README.md +4 -5
- data/assets/output.css.erb +159 -0
- data/assets/output.html.erb +1 -160
- data/config/default.yml +136 -19
- data/lib/rubocop/cached_data.rb +11 -3
- data/lib/rubocop/cli/command/auto_generate_config.rb +22 -8
- data/lib/rubocop/cli/command/lsp.rb +2 -2
- data/lib/rubocop/cli/command/show_docs_url.rb +2 -2
- data/lib/rubocop/cli.rb +10 -1
- data/lib/rubocop/config.rb +36 -12
- data/lib/rubocop/config_finder.rb +12 -2
- data/lib/rubocop/config_loader.rb +1 -2
- data/lib/rubocop/config_loader_resolver.rb +9 -3
- data/lib/rubocop/config_obsoletion.rb +11 -8
- data/lib/rubocop/config_validator.rb +14 -7
- data/lib/rubocop/cop/autocorrect_logic.rb +6 -1
- data/lib/rubocop/cop/base.rb +63 -16
- data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_version.rb +3 -5
- data/lib/rubocop/cop/cop.rb +20 -2
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
- data/lib/rubocop/cop/documentation.rb +16 -6
- data/lib/rubocop/cop/exclude_limit.rb +1 -1
- data/lib/rubocop/cop/force.rb +12 -0
- data/lib/rubocop/cop/gemspec/add_runtime_dependency.rb +38 -0
- data/lib/rubocop/cop/gemspec/dependency_version.rb +3 -5
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
- data/lib/rubocop/cop/internal_affairs/example_description.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
- data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +123 -29
- data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +3 -1
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
- data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +15 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +24 -7
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +20 -20
- data/lib/rubocop/cop/layout/redundant_line_break.rb +16 -3
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +53 -20
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +3 -4
- data/lib/rubocop/cop/legacy/corrector.rb +12 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +6 -6
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +29 -3
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +2 -2
- data/lib/rubocop/cop/lint/empty_when.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +24 -17
- data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +14 -7
- data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
- data/lib/rubocop/cop/lint/mixed_case_range.rb +9 -4
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +54 -6
- data/lib/rubocop/cop/lint/redundant_with_index.rb +6 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
- data/lib/rubocop/cop/lint/script_permission.rb +3 -3
- data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
- data/lib/rubocop/cop/lint/syntax.rb +6 -3
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -3
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +3 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +4 -2
- data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_times.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +53 -12
- data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
- data/lib/rubocop/cop/metrics/block_nesting.rb +19 -7
- data/lib/rubocop/cop/metrics/class_length.rb +6 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +5 -5
- data/lib/rubocop/cop/mixin/alignment.rb +5 -1
- data/lib/rubocop/cop/mixin/allowed_methods.rb +7 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +15 -3
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/code_length.rb +12 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_max.rb +5 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +23 -13
- data/lib/rubocop/cop/mixin/method_complexity.rb +15 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/rescue_node.rb +4 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +34 -7
- data/lib/rubocop/cop/naming/constant_name.rb +1 -2
- data/lib/rubocop/cop/naming/file_name.rb +2 -2
- data/lib/rubocop/cop/naming/inclusive_language.rb +1 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -2
- data/lib/rubocop/cop/registry.rb +1 -1
- data/lib/rubocop/cop/security/compound_hash.rb +2 -2
- data/lib/rubocop/cop/security/open.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +52 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +155 -21
- data/lib/rubocop/cop/style/array_first_last.rb +64 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
- data/lib/rubocop/cop/style/case_like_if.rb +5 -5
- data/lib/rubocop/cop/style/class_check.rb +1 -0
- data/lib/rubocop/cop/style/class_vars.rb +3 -3
- data/lib/rubocop/cop/style/collection_compact.rb +21 -11
- data/lib/rubocop/cop/style/combinable_loops.rb +13 -7
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +7 -8
- data/lib/rubocop/cop/style/copyright.rb +31 -21
- data/lib/rubocop/cop/style/date_time.rb +5 -4
- data/lib/rubocop/cop/style/documentation.rb +24 -24
- data/lib/rubocop/cop/style/documentation_method.rb +20 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +6 -15
- data/lib/rubocop/cop/style/exact_regexp_match.rb +4 -2
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/format_string.rb +9 -9
- data/lib/rubocop/cop/style/hash_each_methods.rb +105 -11
- data/lib/rubocop/cop/style/hash_except.rb +10 -6
- data/lib/rubocop/cop/style/hash_syntax.rb +24 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +12 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +5 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +14 -13
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +44 -2
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +82 -50
- data/lib/rubocop/cop/style/map_into_array.rb +175 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +18 -8
- data/lib/rubocop/cop/style/map_to_set.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
- data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +5 -3
- data/lib/rubocop/cop/style/next.rb +1 -1
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +10 -2
- data/lib/rubocop/cop/style/object_then.rb +5 -3
- data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -5
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +4 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +27 -3
- data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +5 -4
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +17 -10
- data/lib/rubocop/cop/style/redundant_each.rb +7 -4
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +5 -4
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +19 -2
- data/lib/rubocop/cop/style/redundant_parentheses.rb +50 -19
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +7 -1
- data/lib/rubocop/cop/style/redundant_self.rb +17 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
- data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
- data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
- data/lib/rubocop/cop/style/require_order.rb +1 -1
- data/lib/rubocop/cop/style/sample.rb +3 -4
- data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +8 -0
- data/lib/rubocop/cop/style/send.rb +4 -4
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +104 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -2
- data/lib/rubocop/cop/style/string_chars.rb +1 -0
- data/lib/rubocop/cop/style/strip.rb +7 -4
- data/lib/rubocop/cop/style/super_arguments.rb +174 -0
- data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +75 -5
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +11 -14
- data/lib/rubocop/cop/style/zero_length_predicate.rb +28 -24
- data/lib/rubocop/cop/team.rb +13 -0
- data/lib/rubocop/cop/util.rb +7 -1
- data/lib/rubocop/cop/utils/regexp_ranges.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +16 -4
- data/lib/rubocop/directive_comment.rb +10 -8
- data/lib/rubocop/ext/regexp_node.rb +9 -4
- data/lib/rubocop/ext/regexp_parser.rb +4 -21
- data/lib/rubocop/formatter/clang_style_formatter.rb +3 -7
- data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -8
- data/lib/rubocop/formatter/formatter_set.rb +7 -1
- data/lib/rubocop/formatter/html_formatter.rb +37 -14
- data/lib/rubocop/formatter/json_formatter.rb +0 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
- data/lib/rubocop/formatter/tap_formatter.rb +3 -7
- data/lib/rubocop/formatter.rb +1 -1
- data/lib/rubocop/lockfile.rb +56 -7
- data/lib/rubocop/lsp/logger.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +12 -15
- data/lib/rubocop/lsp/runtime.rb +1 -1
- data/lib/rubocop/lsp/server.rb +7 -2
- data/lib/rubocop/lsp/severity.rb +1 -1
- data/lib/rubocop/lsp.rb +36 -0
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/options.rb +14 -11
- data/lib/rubocop/path_util.rb +6 -2
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/result_cache.rb +0 -1
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/expect_offense.rb +16 -8
- data/lib/rubocop/rspec/shared_contexts.rb +73 -16
- data/lib/rubocop/rspec/support.rb +3 -0
- data/lib/rubocop/runner.rb +14 -3
- data/lib/rubocop/server/cache.rb +11 -2
- data/lib/rubocop/server/client_command/exec.rb +2 -3
- data/lib/rubocop/server/client_command/start.rb +1 -1
- data/lib/rubocop/server/core.rb +4 -0
- data/lib/rubocop/server/server_command/exec.rb +0 -1
- data/lib/rubocop/target_finder.rb +84 -78
- data/lib/rubocop/target_ruby.rb +82 -80
- data/lib/rubocop/version.rb +19 -4
- data/lib/rubocop.rb +8 -0
- metadata +27 -29
- /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
|
@@ -67,12 +67,13 @@ module RuboCop
|
|
|
67
67
|
return unless redundant_argument?(node)
|
|
68
68
|
|
|
69
69
|
offense_range = argument_range(node)
|
|
70
|
-
message = format(MSG, arg: node.
|
|
70
|
+
message = format(MSG, arg: node.first_argument.source)
|
|
71
71
|
|
|
72
72
|
add_offense(offense_range, message: message) do |corrector|
|
|
73
73
|
corrector.remove(offense_range)
|
|
74
74
|
end
|
|
75
75
|
end
|
|
76
|
+
alias on_csend on_send
|
|
76
77
|
|
|
77
78
|
private
|
|
78
79
|
|
|
@@ -80,14 +81,20 @@ module RuboCop
|
|
|
80
81
|
redundant_argument = redundant_arg_for_method(node.method_name.to_s)
|
|
81
82
|
return false if redundant_argument.nil?
|
|
82
83
|
|
|
83
|
-
node.
|
|
84
|
+
target_argument = if node.first_argument.respond_to?(:value)
|
|
85
|
+
node.first_argument.value
|
|
86
|
+
else
|
|
87
|
+
node.first_argument
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
argument_matched?(target_argument, redundant_argument)
|
|
84
91
|
end
|
|
85
92
|
|
|
86
93
|
def redundant_arg_for_method(method_name)
|
|
87
94
|
arg = cop_config['Methods'].fetch(method_name) { return }
|
|
88
95
|
|
|
89
96
|
@mem ||= {}
|
|
90
|
-
@mem[method_name] ||=
|
|
97
|
+
@mem[method_name] ||= arg.inspect
|
|
91
98
|
end
|
|
92
99
|
|
|
93
100
|
def argument_range(node)
|
|
@@ -97,6 +104,23 @@ module RuboCop
|
|
|
97
104
|
range_with_surrounding_space(node.first_argument.source_range, newlines: false)
|
|
98
105
|
end
|
|
99
106
|
end
|
|
107
|
+
|
|
108
|
+
def argument_matched?(target_argument, redundant_argument)
|
|
109
|
+
argument = if target_argument.is_a?(AST::Node)
|
|
110
|
+
target_argument.source
|
|
111
|
+
elsif exclude_cntrl_character?(target_argument, redundant_argument)
|
|
112
|
+
target_argument.inspect
|
|
113
|
+
else
|
|
114
|
+
target_argument.to_s
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
argument == redundant_argument
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def exclude_cntrl_character?(target_argument, redundant_argument)
|
|
121
|
+
!target_argument.to_s.sub(/\A'/, '"').sub(/'\z/, '"').match?(/[[:cntrl:]]/) ||
|
|
122
|
+
!redundant_argument.match?(/[[:cntrl:]]/)
|
|
123
|
+
end
|
|
100
124
|
end
|
|
101
125
|
end
|
|
102
126
|
end
|
|
@@ -54,12 +54,14 @@ module RuboCop
|
|
|
54
54
|
|
|
55
55
|
private
|
|
56
56
|
|
|
57
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
|
57
58
|
def check_branch(node)
|
|
58
59
|
return unless node
|
|
59
60
|
|
|
60
61
|
case node.type
|
|
61
|
-
when :case
|
|
62
|
-
when :
|
|
62
|
+
when :case then check_case_node(node)
|
|
63
|
+
when :case_match then check_case_match_node(node)
|
|
64
|
+
when :if then check_if_node(node)
|
|
63
65
|
when :rescue, :resbody
|
|
64
66
|
check_rescue_node(node)
|
|
65
67
|
when :ensure then check_ensure_node(node)
|
|
@@ -67,12 +69,18 @@ module RuboCop
|
|
|
67
69
|
check_begin_node(node)
|
|
68
70
|
end
|
|
69
71
|
end
|
|
72
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
|
70
73
|
|
|
71
74
|
def check_case_node(node)
|
|
72
75
|
node.when_branches.each { |when_node| check_branch(when_node.body) }
|
|
73
76
|
check_branch(node.else_branch)
|
|
74
77
|
end
|
|
75
78
|
|
|
79
|
+
def check_case_match_node(node)
|
|
80
|
+
node.in_pattern_branches.each { |in_pattern_node| check_branch(in_pattern_node.body) }
|
|
81
|
+
check_branch(node.else_branch)
|
|
82
|
+
end
|
|
83
|
+
|
|
76
84
|
def check_if_node(node)
|
|
77
85
|
return if node.modifier_form? || node.ternary?
|
|
78
86
|
|
|
@@ -18,14 +18,15 @@ module RuboCop
|
|
|
18
18
|
extend AutoCorrector
|
|
19
19
|
|
|
20
20
|
MSG = 'Remove the redundant current directory path.'
|
|
21
|
+
RESTRICT_ON_SEND = %i[require_relative].freeze
|
|
21
22
|
CURRENT_DIRECTORY_PATH = './'
|
|
22
23
|
|
|
23
24
|
def on_send(node)
|
|
24
|
-
return unless node.
|
|
25
|
-
return unless
|
|
26
|
-
return unless (index =
|
|
25
|
+
return unless (first_argument = node.first_argument)
|
|
26
|
+
return unless first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)
|
|
27
|
+
return unless (index = first_argument.source.index(CURRENT_DIRECTORY_PATH))
|
|
27
28
|
|
|
28
|
-
begin_pos =
|
|
29
|
+
begin_pos = first_argument.source_range.begin.begin_pos + index
|
|
29
30
|
range = range_between(begin_pos, begin_pos + 2)
|
|
30
31
|
|
|
31
32
|
add_offense(range) do |corrector|
|
|
@@ -25,26 +25,29 @@ module RuboCop
|
|
|
25
25
|
MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
|
|
26
26
|
MERGE_METHODS = %i[merge merge!].freeze
|
|
27
27
|
|
|
28
|
-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
28
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
29
29
|
def on_hash(node)
|
|
30
30
|
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
|
|
31
31
|
return unless (parent = node.parent)
|
|
32
|
-
return
|
|
32
|
+
return unless parent.call_type? || parent.kwsplat_type?
|
|
33
|
+
return unless mergeable?(parent)
|
|
33
34
|
return unless (kwsplat = node.each_ancestor(:kwsplat).first)
|
|
34
|
-
return if allowed_double_splat_receiver?(kwsplat)
|
|
35
|
+
return if !node.braces? || allowed_double_splat_receiver?(kwsplat)
|
|
35
36
|
|
|
36
37
|
add_offense(kwsplat) do |corrector|
|
|
37
38
|
autocorrect(corrector, node, kwsplat)
|
|
38
39
|
end
|
|
39
40
|
end
|
|
40
|
-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
41
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
41
42
|
|
|
42
43
|
private
|
|
43
44
|
|
|
44
45
|
def allowed_double_splat_receiver?(kwsplat)
|
|
45
|
-
|
|
46
|
+
first_child = kwsplat.children.first
|
|
47
|
+
return true if first_child.block_type? || first_child.numblock_type?
|
|
48
|
+
return false unless first_child.call_type?
|
|
46
49
|
|
|
47
|
-
root_receiver = root_receiver(
|
|
50
|
+
root_receiver = root_receiver(first_child)
|
|
48
51
|
|
|
49
52
|
!root_receiver&.hash_type?
|
|
50
53
|
end
|
|
@@ -71,7 +74,7 @@ module RuboCop
|
|
|
71
74
|
|
|
72
75
|
def select_merge_method_nodes(kwsplat)
|
|
73
76
|
extract_send_methods(kwsplat).select do |node|
|
|
74
|
-
|
|
77
|
+
mergeable?(node)
|
|
75
78
|
end
|
|
76
79
|
end
|
|
77
80
|
|
|
@@ -106,7 +109,7 @@ module RuboCop
|
|
|
106
109
|
end
|
|
107
110
|
|
|
108
111
|
def convert_to_new_arguments(node)
|
|
109
|
-
return unless
|
|
112
|
+
return unless mergeable?(node)
|
|
110
113
|
|
|
111
114
|
node.arguments.map do |arg|
|
|
112
115
|
if arg.hash_type?
|
|
@@ -117,8 +120,12 @@ module RuboCop
|
|
|
117
120
|
end
|
|
118
121
|
end
|
|
119
122
|
|
|
120
|
-
def
|
|
121
|
-
|
|
123
|
+
def mergeable?(node)
|
|
124
|
+
return true unless node.call_type?
|
|
125
|
+
return false unless MERGE_METHODS.include?(node.method_name)
|
|
126
|
+
return true unless (parent = node.parent)
|
|
127
|
+
|
|
128
|
+
mergeable?(parent)
|
|
122
129
|
end
|
|
123
130
|
end
|
|
124
131
|
end
|
|
@@ -56,6 +56,7 @@ module RuboCop
|
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
|
+
alias on_csend on_send
|
|
59
60
|
|
|
60
61
|
private
|
|
61
62
|
|
|
@@ -64,7 +65,7 @@ module RuboCop
|
|
|
64
65
|
return if node.last_argument&.block_pass_type?
|
|
65
66
|
|
|
66
67
|
if node.method?(:each) && !node.parent&.block_type?
|
|
67
|
-
ancestor_node = node.each_ancestor(:send).detect do |ancestor|
|
|
68
|
+
ancestor_node = node.each_ancestor(:send, :csend).detect do |ancestor|
|
|
68
69
|
ancestor.receiver == node &&
|
|
69
70
|
(RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each))
|
|
70
71
|
end
|
|
@@ -83,10 +84,12 @@ module RuboCop
|
|
|
83
84
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
84
85
|
|
|
85
86
|
def range(node)
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
return node.selector unless node.method?(:each)
|
|
88
|
+
|
|
89
|
+
if node.parent&.call_type?
|
|
90
|
+
node.selector.join(node.parent.loc.dot)
|
|
88
91
|
else
|
|
89
|
-
node.loc.selector
|
|
92
|
+
node.loc.dot.join(node.selector)
|
|
90
93
|
end
|
|
91
94
|
end
|
|
92
95
|
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
|
47
47
|
# @!method redundant_fetch_block_candidate?(node)
|
|
48
48
|
def_node_matcher :redundant_fetch_block_candidate?, <<~PATTERN
|
|
49
49
|
(block
|
|
50
|
-
$(
|
|
50
|
+
$(call _ :fetch _)
|
|
51
51
|
(args)
|
|
52
52
|
${nil? #basic_literal? #const_type?})
|
|
53
53
|
PATTERN
|
|
@@ -61,10 +61,10 @@ module RuboCop
|
|
|
61
61
|
bad = build_bad_method(send, body)
|
|
62
62
|
|
|
63
63
|
add_offense(range, message: format(MSG, good: good, bad: bad)) do |corrector|
|
|
64
|
-
|
|
64
|
+
_, _, key = send.children
|
|
65
65
|
default_value = body ? body.source : 'nil'
|
|
66
66
|
|
|
67
|
-
corrector.replace(
|
|
67
|
+
corrector.replace(range, "fetch(#{key.source}, #{default_value})")
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
70
|
end
|
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
|
6
6
|
# Checks for the presence of superfluous `.rb` extension in
|
|
7
7
|
# the filename provided to `require` and `require_relative`.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
9
|
+
# NOTE: If the extension is omitted, Ruby tries adding '.rb', '.so',
|
|
10
10
|
# and so on to the name until found. If the file named cannot be found,
|
|
11
11
|
# a `LoadError` will be raised.
|
|
12
12
|
# There is an edge case where `foo.so` file is loaded instead of a `LoadError`
|
|
@@ -60,10 +60,10 @@ module RuboCop
|
|
|
60
60
|
|
|
61
61
|
# @!method select_predicate?(node)
|
|
62
62
|
def_node_matcher :select_predicate?, <<~PATTERN
|
|
63
|
-
(
|
|
63
|
+
(call
|
|
64
64
|
{
|
|
65
|
-
(block $(
|
|
66
|
-
$(
|
|
65
|
+
(block $(call _ {:select :filter :find_all}) ...)
|
|
66
|
+
$(call _ {:select :filter :find_all} block_pass_type?)
|
|
67
67
|
}
|
|
68
68
|
${:#{RESTRICT_ON_SEND.join(' :')}})
|
|
69
69
|
PATTERN
|
|
@@ -79,7 +79,7 @@ module RuboCop
|
|
|
79
79
|
private_constant :REPLACEMENT_METHODS
|
|
80
80
|
|
|
81
81
|
def on_send(node)
|
|
82
|
-
return if node.arguments? || node.
|
|
82
|
+
return if node.arguments? || node.block_literal?
|
|
83
83
|
|
|
84
84
|
select_predicate?(node) do |select_node, filter_method|
|
|
85
85
|
return if RAILS_METHODS.include?(filter_method) && !active_support_extensions_enabled?
|
|
@@ -87,6 +87,7 @@ module RuboCop
|
|
|
87
87
|
register_offense(select_node, node)
|
|
88
88
|
end
|
|
89
89
|
end
|
|
90
|
+
alias on_csend on_send
|
|
90
91
|
|
|
91
92
|
private
|
|
92
93
|
|
|
@@ -74,6 +74,7 @@ module RuboCop
|
|
|
74
74
|
kFALSE kNIL kSELF kTRUE tCONSTANT tCVAR tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR
|
|
75
75
|
tLBRACK tLCURLY tLPAREN_ARG tSTRING tSTRING_BEG tSYMBOL tXSTRING_BEG
|
|
76
76
|
].freeze
|
|
77
|
+
ARGUMENT_TAKING_FLOW_TOKEN_TYPES = %i[tIDENTIFIER kRETURN kBREAK kNEXT kYIELD].freeze
|
|
77
78
|
|
|
78
79
|
def on_new_investigation
|
|
79
80
|
return unless processed_source.ast
|
|
@@ -94,7 +95,8 @@ module RuboCop
|
|
|
94
95
|
!ends_with_backslash_without_comment?(range.source_line) ||
|
|
95
96
|
string_concatenation?(range.source_line) ||
|
|
96
97
|
start_with_arithmetic_operator?(processed_source[range.line]) ||
|
|
97
|
-
inside_string_literal_or_method_with_argument?(range)
|
|
98
|
+
inside_string_literal_or_method_with_argument?(range) ||
|
|
99
|
+
leading_dot_method_chain_with_blank_line?(range)
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
def ends_with_backslash_without_comment?(source_line)
|
|
@@ -107,10 +109,18 @@ module RuboCop
|
|
|
107
109
|
|
|
108
110
|
def inside_string_literal_or_method_with_argument?(range)
|
|
109
111
|
processed_source.tokens.each_cons(2).any? do |token, next_token|
|
|
112
|
+
next if token.line == next_token.line
|
|
113
|
+
|
|
110
114
|
inside_string_literal?(range, token) || method_with_argument?(token, next_token)
|
|
111
115
|
end
|
|
112
116
|
end
|
|
113
117
|
|
|
118
|
+
def leading_dot_method_chain_with_blank_line?(range)
|
|
119
|
+
return false unless range.source_line.strip.start_with?('.', '&.')
|
|
120
|
+
|
|
121
|
+
processed_source[range.line].strip.empty?
|
|
122
|
+
end
|
|
123
|
+
|
|
114
124
|
def redundant_line_continuation?(range)
|
|
115
125
|
return true unless (node = find_node_for_line(range.line))
|
|
116
126
|
return false if argument_newline?(node)
|
|
@@ -128,10 +138,16 @@ module RuboCop
|
|
|
128
138
|
# do_something \
|
|
129
139
|
# argument
|
|
130
140
|
def method_with_argument?(current_token, next_token)
|
|
131
|
-
|
|
141
|
+
return false unless ARGUMENT_TAKING_FLOW_TOKEN_TYPES.include?(current_token.type)
|
|
142
|
+
|
|
143
|
+
ARGUMENT_TYPES.include?(next_token.type)
|
|
132
144
|
end
|
|
133
145
|
|
|
146
|
+
# rubocop:disable Metrics/AbcSize
|
|
134
147
|
def argument_newline?(node)
|
|
148
|
+
node = node.to_a.last if node.assignment?
|
|
149
|
+
return false if node.parenthesized_call?
|
|
150
|
+
|
|
135
151
|
node = node.children.first if node.root? && node.begin_type?
|
|
136
152
|
|
|
137
153
|
if argument_is_method?(node)
|
|
@@ -142,6 +158,7 @@ module RuboCop
|
|
|
142
158
|
node.loc.selector.line != node.first_argument.loc.line
|
|
143
159
|
end
|
|
144
160
|
end
|
|
161
|
+
# rubocop:enable Metrics/AbcSize
|
|
145
162
|
|
|
146
163
|
def find_node_for_line(line)
|
|
147
164
|
processed_source.ast.each_node do |node|
|
|
@@ -17,12 +17,11 @@ module RuboCop
|
|
|
17
17
|
include Parentheses
|
|
18
18
|
extend AutoCorrector
|
|
19
19
|
|
|
20
|
+
ALLOWED_NODE_TYPES = %i[and or send splat kwsplat].freeze
|
|
21
|
+
|
|
20
22
|
# @!method square_brackets?(node)
|
|
21
23
|
def_node_matcher :square_brackets?, '(send {(send _recv _msg) str array hash} :[] ...)'
|
|
22
24
|
|
|
23
|
-
# @!method range_end?(node)
|
|
24
|
-
def_node_matcher :range_end?, '^^{irange erange}'
|
|
25
|
-
|
|
26
25
|
# @!method method_node_and_args(node)
|
|
27
26
|
def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
|
|
28
27
|
|
|
@@ -54,15 +53,16 @@ module RuboCop
|
|
|
54
53
|
def ignore_syntax?(node)
|
|
55
54
|
return false unless (parent = node.parent)
|
|
56
55
|
|
|
57
|
-
parent.while_post_type? || parent.until_post_type? ||
|
|
58
|
-
like_method_argument_parentheses?(parent)
|
|
56
|
+
parent.while_post_type? || parent.until_post_type? || parent.match_with_lvasgn_type? ||
|
|
57
|
+
like_method_argument_parentheses?(parent) || multiline_control_flow_statements?(node)
|
|
59
58
|
end
|
|
60
59
|
|
|
61
60
|
def allowed_expression?(node)
|
|
62
61
|
allowed_ancestor?(node) ||
|
|
63
62
|
allowed_method_call?(node) ||
|
|
64
63
|
allowed_multiple_expression?(node) ||
|
|
65
|
-
allowed_ternary?(node)
|
|
64
|
+
allowed_ternary?(node) ||
|
|
65
|
+
node.parent&.range_type?
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def allowed_ancestor?(node)
|
|
@@ -104,6 +104,13 @@ module RuboCop
|
|
|
104
104
|
!node.arithmetic_operation? && node.first_argument.begin_type?
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
+
def multiline_control_flow_statements?(node)
|
|
108
|
+
return false unless (parent = node.parent)
|
|
109
|
+
return false if parent.single_line?
|
|
110
|
+
|
|
111
|
+
parent.return_type? || parent.next_type? || parent.break_type?
|
|
112
|
+
end
|
|
113
|
+
|
|
107
114
|
def empty_parentheses?(node)
|
|
108
115
|
# Don't flag `()`
|
|
109
116
|
node.children.empty?
|
|
@@ -111,17 +118,19 @@ module RuboCop
|
|
|
111
118
|
|
|
112
119
|
def first_arg_begins_with_hash_literal?(node)
|
|
113
120
|
# Don't flag `method ({key: value})` or `method ({key: value}.method)`
|
|
114
|
-
method_chain_begins_with_hash_literal
|
|
115
|
-
|
|
116
|
-
|
|
121
|
+
hash_literal = method_chain_begins_with_hash_literal(node.children.first)
|
|
122
|
+
if (root_method = node.each_ancestor(:send).to_a.last)
|
|
123
|
+
parenthesized = root_method.parenthesized_call?
|
|
124
|
+
end
|
|
125
|
+
hash_literal && first_argument?(node) && !parentheses?(hash_literal) && !parenthesized
|
|
117
126
|
end
|
|
118
127
|
|
|
119
|
-
def method_chain_begins_with_hash_literal
|
|
120
|
-
return
|
|
121
|
-
return
|
|
122
|
-
return
|
|
128
|
+
def method_chain_begins_with_hash_literal(node)
|
|
129
|
+
return if node.nil?
|
|
130
|
+
return node if node.hash_type?
|
|
131
|
+
return unless node.send_type?
|
|
123
132
|
|
|
124
|
-
method_chain_begins_with_hash_literal
|
|
133
|
+
method_chain_begins_with_hash_literal(node.children.first)
|
|
125
134
|
end
|
|
126
135
|
|
|
127
136
|
def check(begin_node)
|
|
@@ -134,27 +143,44 @@ module RuboCop
|
|
|
134
143
|
check_send(begin_node, node) if node.call_type?
|
|
135
144
|
end
|
|
136
145
|
|
|
137
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
146
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
138
147
|
def find_offense_message(begin_node, node)
|
|
139
148
|
return 'a keyword' if keyword_with_redundant_parentheses?(node)
|
|
140
149
|
return 'a literal' if disallowed_literal?(begin_node, node)
|
|
141
150
|
return 'a variable' if node.variable?
|
|
142
151
|
return 'a constant' if node.const_type?
|
|
152
|
+
if node.lambda_or_proc? && (node.braces? || node.send_node.lambda_literal?)
|
|
153
|
+
return 'an expression'
|
|
154
|
+
end
|
|
143
155
|
return 'an interpolated expression' if interpolation?(begin_node)
|
|
144
156
|
|
|
145
|
-
return if begin_node.chained?
|
|
157
|
+
return if begin_node.chained?
|
|
146
158
|
|
|
147
159
|
if node.and_type? || node.or_type?
|
|
160
|
+
return if node.semantic_operator? && begin_node.parent
|
|
161
|
+
return if node.multiline? && allow_in_multiline_conditions?
|
|
162
|
+
return if ALLOWED_NODE_TYPES.include?(begin_node.parent&.type)
|
|
163
|
+
return if begin_node.parent&.if_type? && begin_node.parent&.ternary?
|
|
164
|
+
|
|
148
165
|
'a logical expression'
|
|
149
166
|
elsif node.respond_to?(:comparison_method?) && node.comparison_method?
|
|
167
|
+
return unless begin_node.parent.nil?
|
|
168
|
+
|
|
150
169
|
'a comparison expression'
|
|
151
170
|
end
|
|
152
171
|
end
|
|
153
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
172
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
154
173
|
|
|
155
174
|
# @!method interpolation?(node)
|
|
156
175
|
def_node_matcher :interpolation?, '[^begin ^^dstr]'
|
|
157
176
|
|
|
177
|
+
def allow_in_multiline_conditions?
|
|
178
|
+
parentheses_around_condition_config = config.for_cop('Style/ParenthesesAroundCondition')
|
|
179
|
+
return false unless parentheses_around_condition_config['Enabled']
|
|
180
|
+
|
|
181
|
+
!!parentheses_around_condition_config['AllowInMultilineConditions']
|
|
182
|
+
end
|
|
183
|
+
|
|
158
184
|
def check_send(begin_node, node)
|
|
159
185
|
return check_unary(begin_node, node) if node.unary_operation?
|
|
160
186
|
|
|
@@ -219,7 +245,6 @@ module RuboCop
|
|
|
219
245
|
def method_call_with_redundant_parentheses?(node)
|
|
220
246
|
return false unless node.call_type?
|
|
221
247
|
return false if node.prefix_not?
|
|
222
|
-
return false if range_end?(node)
|
|
223
248
|
|
|
224
249
|
send_node, args = method_node_and_args(node)
|
|
225
250
|
|
|
@@ -231,7 +256,13 @@ module RuboCop
|
|
|
231
256
|
end
|
|
232
257
|
|
|
233
258
|
def first_argument?(node)
|
|
234
|
-
first_send_argument?(node) ||
|
|
259
|
+
if first_send_argument?(node) ||
|
|
260
|
+
first_super_argument?(node) ||
|
|
261
|
+
first_yield_argument?(node)
|
|
262
|
+
return true
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
node.each_ancestor.any? { |ancestor| first_argument?(ancestor) }
|
|
235
266
|
end
|
|
236
267
|
|
|
237
268
|
# @!method first_send_argument?(node)
|
|
@@ -53,7 +53,7 @@ module RuboCop
|
|
|
53
53
|
return if interpolated_quotes?(node) || allowed_percent_q?(node)
|
|
54
54
|
|
|
55
55
|
add_offense(node) do |corrector|
|
|
56
|
-
delimiter =
|
|
56
|
+
delimiter = /\A%Q[^"]+\z|'/.match?(node.source) ? QUOTE : SINGLE_QUOTE
|
|
57
57
|
|
|
58
58
|
corrector.replace(node.loc.begin, delimiter)
|
|
59
59
|
corrector.replace(node.loc.end, delimiter)
|
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
|
58
58
|
|
|
59
59
|
MSG = 'Redundant `return` detected.'
|
|
60
60
|
MULTI_RETURN_MSG = 'To return multiple values, use an array.'
|
|
61
|
-
RESTRICT_ON_SEND = %i[define_method define_singleton_method].freeze
|
|
61
|
+
RESTRICT_ON_SEND = %i[define_method define_singleton_method lambda].freeze
|
|
62
62
|
|
|
63
63
|
def on_send(node)
|
|
64
64
|
return unless (parent = node.parent) && parent.block_type?
|
|
@@ -113,6 +113,7 @@ module RuboCop
|
|
|
113
113
|
case node.type
|
|
114
114
|
when :return then check_return_node(node)
|
|
115
115
|
when :case then check_case_node(node)
|
|
116
|
+
when :case_match then check_case_match_node(node)
|
|
116
117
|
when :if then check_if_node(node)
|
|
117
118
|
when :rescue then check_rescue_node(node)
|
|
118
119
|
when :resbody then check_resbody_node(node)
|
|
@@ -140,6 +141,11 @@ module RuboCop
|
|
|
140
141
|
check_branch(node.else_branch)
|
|
141
142
|
end
|
|
142
143
|
|
|
144
|
+
def check_case_match_node(node)
|
|
145
|
+
node.in_pattern_branches.each { |in_pattern_node| check_branch(in_pattern_node.body) }
|
|
146
|
+
check_branch(node.else_branch)
|
|
147
|
+
end
|
|
148
|
+
|
|
143
149
|
def check_if_node(node)
|
|
144
150
|
return if node.ternary?
|
|
145
151
|
|
|
@@ -17,7 +17,8 @@ module RuboCop
|
|
|
17
17
|
# protected scope, you cannot send private messages this way.
|
|
18
18
|
#
|
|
19
19
|
# Note we allow uses of `self` with operators because it would be awkward
|
|
20
|
-
# otherwise.
|
|
20
|
+
# otherwise. Also allows the use of `self.it` without arguments in blocks,
|
|
21
|
+
# as in `0.times { self.it }`, following `Lint/ItWithoutArgumentsInBlock` cop.
|
|
21
22
|
#
|
|
22
23
|
# @example
|
|
23
24
|
#
|
|
@@ -107,8 +108,8 @@ module RuboCop
|
|
|
107
108
|
def on_send(node)
|
|
108
109
|
return unless node.self_receiver? && regular_method_call?(node)
|
|
109
110
|
return if node.parent&.mlhs_type?
|
|
110
|
-
|
|
111
111
|
return if allowed_send_node?(node)
|
|
112
|
+
return if it_method_in_block?(node)
|
|
112
113
|
|
|
113
114
|
add_offense(node.receiver) do |corrector|
|
|
114
115
|
corrector.remove(node.receiver)
|
|
@@ -155,6 +156,20 @@ module RuboCop
|
|
|
155
156
|
KERNEL_METHODS.include?(node.method_name)
|
|
156
157
|
end
|
|
157
158
|
|
|
159
|
+
# Respects `Lint/ItWithoutArgumentsInBlock` cop and the following Ruby 3.3's warning:
|
|
160
|
+
#
|
|
161
|
+
# $ ruby -e '0.times { begin; it; end }'
|
|
162
|
+
# -e:1: warning: `it` calls without arguments will refer to the first block param in
|
|
163
|
+
# Ruby 3.4; use it() or self.it
|
|
164
|
+
#
|
|
165
|
+
def it_method_in_block?(node)
|
|
166
|
+
return false unless node.method?(:it)
|
|
167
|
+
return false unless (block_node = node.each_ancestor(:block).first)
|
|
168
|
+
return false unless block_node.arguments.empty_and_without_delimiters?
|
|
169
|
+
|
|
170
|
+
node.arguments.empty? && !node.block_literal?
|
|
171
|
+
end
|
|
172
|
+
|
|
158
173
|
def regular_method_call?(node)
|
|
159
174
|
!(node.operator_method? ||
|
|
160
175
|
KEYWORDS.include?(node.method_name) ||
|
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
|
16
16
|
# This cop is unsafe, because `sort...last` and `max` may not return the
|
|
17
17
|
# same element in all cases.
|
|
18
18
|
#
|
|
19
|
-
# In an enumerable where there are multiple elements where
|
|
19
|
+
# In an enumerable where there are multiple elements where ``a <=> b == 0``,
|
|
20
20
|
# or where the transformation done by the `sort_by` block has the
|
|
21
21
|
# same result, `sort.last` and `max` (or `sort_by.last` and `max_by`)
|
|
22
22
|
# will return different elements. `sort.last` will return the last
|
|
@@ -87,15 +87,15 @@ module RuboCop
|
|
|
87
87
|
# @!method redundant_sort?(node)
|
|
88
88
|
def_node_matcher :redundant_sort?, <<~MATCHER
|
|
89
89
|
{
|
|
90
|
-
(
|
|
91
|
-
(
|
|
90
|
+
(call $(call _ $:sort) ${:last :first})
|
|
91
|
+
(call $(call _ $:sort) ${:[] :at :slice} {(int 0) (int -1)})
|
|
92
92
|
|
|
93
|
-
(
|
|
93
|
+
(call $(call _ $:sort_by _) ${:last :first})
|
|
94
94
|
(send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
|
|
95
95
|
|
|
96
|
-
(
|
|
97
|
-
(
|
|
98
|
-
({block numblock} $(
|
|
96
|
+
(call ({block numblock} $(call _ ${:sort_by :sort}) ...) ${:last :first})
|
|
97
|
+
(call
|
|
98
|
+
({block numblock} $(call _ ${:sort_by :sort}) ...)
|
|
99
99
|
${:[] :at :slice} {(int 0) (int -1)}
|
|
100
100
|
)
|
|
101
101
|
}
|
|
@@ -108,6 +108,7 @@ module RuboCop
|
|
|
108
108
|
|
|
109
109
|
register_offense(ancestor, sort_node, sorter, accessor)
|
|
110
110
|
end
|
|
111
|
+
alias on_csend on_send
|
|
111
112
|
|
|
112
113
|
private
|
|
113
114
|
|
|
@@ -180,7 +181,7 @@ module RuboCop
|
|
|
180
181
|
end
|
|
181
182
|
|
|
182
183
|
def arg_node(node)
|
|
183
|
-
node.
|
|
184
|
+
node.first_argument
|
|
184
185
|
end
|
|
185
186
|
|
|
186
187
|
def arg_value(node)
|
|
@@ -46,12 +46,12 @@ module RuboCop
|
|
|
46
46
|
|
|
47
47
|
# @!method redundant_sort_by_block(node)
|
|
48
48
|
def_node_matcher :redundant_sort_by_block, <<~PATTERN
|
|
49
|
-
(block $(
|
|
49
|
+
(block $(call _ :sort_by) (args (arg $_x)) (lvar _x))
|
|
50
50
|
PATTERN
|
|
51
51
|
|
|
52
52
|
# @!method redundant_sort_by_numblock(node)
|
|
53
53
|
def_node_matcher :redundant_sort_by_numblock, <<~PATTERN
|
|
54
|
-
(numblock $(
|
|
54
|
+
(numblock $(call _ :sort_by) 1 (lvar :_1))
|
|
55
55
|
PATTERN
|
|
56
56
|
|
|
57
57
|
def sort_by_range(send, node)
|
|
@@ -133,7 +133,7 @@ module RuboCop
|
|
|
133
133
|
end
|
|
134
134
|
|
|
135
135
|
def percent_array_literal?(node)
|
|
136
|
-
|
|
136
|
+
percent_w_literal?(node) || percent_w_upper_literal?(node)
|
|
137
137
|
end
|
|
138
138
|
|
|
139
139
|
def heredoc_with_disabled_interpolation?(node)
|
|
@@ -131,7 +131,7 @@ module RuboCop
|
|
|
131
131
|
end
|
|
132
132
|
|
|
133
133
|
def in_same_section?(node1, node2)
|
|
134
|
-
!node1.source_range.
|
|
134
|
+
!node1.source_range.join(node2.source_range.end).source.include?("\n\n")
|
|
135
135
|
end
|
|
136
136
|
end
|
|
137
137
|
end
|