rubocop 1.41.1 → 1.57.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +6 -4
- data/config/default.yml +243 -48
- data/config/obsoletion.yml +5 -0
- data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
- data/lib/rubocop/cli/command/execute_runner.rb +7 -2
- data/lib/rubocop/cli/command/lsp.rb +19 -0
- data/lib/rubocop/cli.rb +59 -10
- data/lib/rubocop/comment_config.rb +19 -0
- data/lib/rubocop/config.rb +14 -10
- data/lib/rubocop/config_finder.rb +2 -2
- data/lib/rubocop/config_loader.rb +20 -23
- data/lib/rubocop/config_loader_resolver.rb +5 -1
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +31 -13
- data/lib/rubocop/cop/base.rb +96 -73
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
- data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
- data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
- data/lib/rubocop/cop/commissioner.rb +8 -2
- data/lib/rubocop/cop/cop.rb +53 -33
- data/lib/rubocop/cop/corrector.rb +31 -11
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +2 -7
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/dependency_version.rb +19 -21
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +37 -13
- data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +3 -3
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +6 -6
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +42 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +66 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -15
- data/lib/rubocop/cop/layout/class_structure.rb +44 -26
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +2 -3
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -5
- data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +28 -5
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +9 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +8 -3
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +10 -4
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +11 -11
- data/lib/rubocop/cop/layout/indentation_style.rb +5 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +18 -12
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +17 -13
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
- data/lib/rubocop/cop/layout/redundant_line_break.rb +20 -11
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -2
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +3 -3
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +6 -3
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +22 -25
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +47 -22
- data/lib/rubocop/cop/lint/else_layout.rb +3 -7
- data/lib/rubocop/cop/lint/empty_block.rb +2 -2
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +16 -18
- data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +47 -5
- data/lib/rubocop/cop/lint/missing_super.rb +63 -5
- data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +4 -9
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
- data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -0
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +16 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +11 -5
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +21 -2
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
- data/lib/rubocop/cop/lint/refinement_import_methods.rb +2 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
- data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
- data/lib/rubocop/cop/lint/script_permission.rb +1 -1
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -12
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
- data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +4 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +18 -6
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +10 -7
- data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
- data/lib/rubocop/cop/lint/useless_method_definition.rb +12 -4
- data/lib/rubocop/cop/lint/useless_rescue.rb +89 -0
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +79 -16
- data/lib/rubocop/cop/metrics/block_length.rb +2 -2
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/class_length.rb +3 -2
- data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +4 -8
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +33 -5
- data/lib/rubocop/cop/migration/department_name.rb +3 -3
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/annotation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +13 -7
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +62 -23
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
- data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
- data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
- data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +0 -3
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -8
- data/lib/rubocop/cop/mixin/range_help.rb +1 -6
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
- data/lib/rubocop/cop/mixin/string_help.rb +4 -2
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +5 -1
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
- data/lib/rubocop/cop/naming/method_name.rb +3 -3
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +12 -4
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/registry.rb +37 -30
- data/lib/rubocop/cop/security/compound_hash.rb +2 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
- data/lib/rubocop/cop/style/accessor_grouping.rb +43 -17
- data/lib/rubocop/cop/style/alias.rb +9 -8
- data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -62
- data/lib/rubocop/cop/style/array_intersect.rb +14 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +3 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +22 -6
- data/lib/rubocop/cop/style/case_like_if.rb +20 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +5 -12
- data/lib/rubocop/cop/style/class_equality_comparison.rb +58 -40
- data/lib/rubocop/cop/style/collection_compact.rb +20 -7
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +30 -8
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +32 -4
- data/lib/rubocop/cop/style/conditional_assignment.rb +11 -15
- data/lib/rubocop/cop/style/copyright.rb +6 -3
- data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/dir_empty.rb +54 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +3 -3
- data/lib/rubocop/cop/style/documentation.rb +12 -6
- data/lib/rubocop/cop/style/documentation_method.rb +10 -4
- data/lib/rubocop/cop/style/double_negation.rb +2 -2
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +8 -8
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/file_empty.rb +71 -0
- data/lib/rubocop/cop/style/file_read.rb +3 -3
- data/lib/rubocop/cop/style/file_write.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +24 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +4 -2
- data/lib/rubocop/cop/style/guard_clause.rb +40 -8
- data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -10
- data/lib/rubocop/cop/style/hash_except.rb +23 -12
- data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -9
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +36 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +111 -15
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +118 -0
- data/lib/rubocop/cop/style/lambda.rb +3 -3
- data/lib/rubocop/cop/style/lambda_call.rb +5 -0
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
- data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
- data/lib/rubocop/cop/style/map_to_set.rb +64 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +35 -24
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +44 -37
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
- data/lib/rubocop/cop/style/min_max.rb +3 -3
- data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
- data/lib/rubocop/cop/style/missing_else.rb +13 -1
- data/lib/rubocop/cop/style/mixin_grouping.rb +5 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/multiline_method_signature.rb +7 -4
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +19 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +13 -12
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
- data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
- data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +22 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +29 -19
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
- data/lib/rubocop/cop/style/redundant_condition.rb +18 -3
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -14
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +96 -9
- data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +117 -0
- data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +183 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -8
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -8
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +13 -4
- data/lib/rubocop/cop/style/redundant_return.rb +7 -2
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +4 -4
- data/lib/rubocop/cop/style/redundant_string_escape.rb +9 -6
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +16 -17
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
- data/lib/rubocop/cop/style/return_nil.rb +6 -2
- data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
- data/lib/rubocop/cop/style/select_by_regexp.rb +20 -6
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +35 -5
- data/lib/rubocop/cop/style/signal_exception.rb +9 -7
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +65 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +9 -5
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +35 -15
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
- data/lib/rubocop/cop/style/unpack_first.rb +3 -3
- data/lib/rubocop/cop/style/word_array.rb +54 -1
- data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +17 -8
- data/lib/rubocop/cop/style/yoda_expression.rb +91 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +40 -19
- data/lib/rubocop/cop/team.rb +60 -52
- data/lib/rubocop/cop/util.rb +14 -5
- data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
- data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
- data/lib/rubocop/cop/variable_force/scope.rb +3 -3
- data/lib/rubocop/cop/variable_force/variable.rb +5 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +5 -3
- data/lib/rubocop/cop/variable_force.rb +2 -4
- data/lib/rubocop/cops_documentation_generator.rb +11 -4
- data/lib/rubocop/directive_comment.rb +3 -3
- data/lib/rubocop/ext/comment.rb +18 -0
- data/lib/rubocop/ext/regexp_node.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +5 -2
- data/lib/rubocop/file_finder.rb +4 -7
- data/lib/rubocop/formatter/junit_formatter.rb +4 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/formatter.rb +0 -1
- data/lib/rubocop/lsp/logger.rb +22 -0
- data/lib/rubocop/lsp/routes.rb +246 -0
- data/lib/rubocop/lsp/runtime.rb +99 -0
- data/lib/rubocop/lsp/server.rb +68 -0
- data/lib/rubocop/lsp/severity.rb +27 -0
- data/lib/rubocop/magic_comment.rb +12 -10
- data/lib/rubocop/options.rb +37 -3
- data/lib/rubocop/path_util.rb +17 -7
- data/lib/rubocop/result_cache.rb +7 -3
- data/lib/rubocop/rspec/cop_helper.rb +2 -2
- data/lib/rubocop/rspec/expect_offense.rb +6 -4
- data/lib/rubocop/rspec/shared_contexts.rb +6 -3
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/runner.rb +55 -10
- data/lib/rubocop/server/cache.rb +12 -4
- data/lib/rubocop/server/cli.rb +37 -18
- data/lib/rubocop/server/client_command/exec.rb +4 -3
- data/lib/rubocop/server/client_command/start.rb +6 -1
- data/lib/rubocop/server/core.rb +24 -9
- data/lib/rubocop/server/helper.rb +1 -1
- data/lib/rubocop/server/server_command/exec.rb +1 -1
- data/lib/rubocop/string_interpreter.rb +3 -3
- data/lib/rubocop/target_finder.rb +7 -3
- data/lib/rubocop/target_ruby.rb +13 -9
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +31 -0
- metadata +84 -37
@@ -11,7 +11,7 @@ module RuboCop
|
|
11
11
|
#
|
12
12
|
# Moving from compact to nested children requires knowledge of whether the
|
13
13
|
# outer parent is a module or a class. Moving from nested to compact requires
|
14
|
-
# verification that the outer parent is defined elsewhere.
|
14
|
+
# verification that the outer parent is defined elsewhere. RuboCop does not
|
15
15
|
# have the knowledge to perform either operation safely and thus requires
|
16
16
|
# manual oversight.
|
17
17
|
#
|
@@ -31,6 +31,7 @@ module RuboCop
|
|
31
31
|
#
|
32
32
|
# The compact style is only forced for classes/modules with one child.
|
33
33
|
class ClassAndModuleChildren < Base
|
34
|
+
include Alignment
|
34
35
|
include ConfigurableEnforcedStyle
|
35
36
|
include RangeHelp
|
36
37
|
extend AutoCorrector
|
@@ -59,7 +60,7 @@ module RuboCop
|
|
59
60
|
end
|
60
61
|
|
61
62
|
def nest_definition(corrector, node)
|
62
|
-
padding = (
|
63
|
+
padding = indentation(node) + leading_spaces(node)
|
63
64
|
padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')
|
64
65
|
|
65
66
|
replace_namespace_keyword(corrector, node)
|
@@ -124,10 +125,6 @@ module RuboCop
|
|
124
125
|
corrector.remove(range)
|
125
126
|
end
|
126
127
|
|
127
|
-
def configured_indentation_width
|
128
|
-
config.for_badge(Layout::IndentationWidth.badge).fetch('Width', 2)
|
129
|
-
end
|
130
|
-
|
131
128
|
def unindent(corrector, node)
|
132
129
|
return if node.body.children.last.nil?
|
133
130
|
|
@@ -141,12 +138,8 @@ module RuboCop
|
|
141
138
|
node.source_range.source_line[/\A\s*/]
|
142
139
|
end
|
143
140
|
|
144
|
-
def indent_width
|
145
|
-
@config.for_cop('Layout/IndentationWidth')['Width'] || 2
|
146
|
-
end
|
147
|
-
|
148
141
|
def check_style(node, body)
|
149
|
-
return if node.identifier.
|
142
|
+
return if node.identifier.namespace&.cbase_type?
|
150
143
|
|
151
144
|
if style == :nested
|
152
145
|
check_nested_style(node)
|
@@ -185,7 +178,7 @@ module RuboCop
|
|
185
178
|
end
|
186
179
|
|
187
180
|
def compact_node_name?(node)
|
188
|
-
|
181
|
+
node.identifier.source.include?('::')
|
189
182
|
end
|
190
183
|
end
|
191
184
|
end
|
@@ -5,9 +5,14 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# Enforces the use of `Object#instance_of?` instead of class comparison
|
7
7
|
# for equality.
|
8
|
-
# `==`, `equal?`, and `eql?`
|
8
|
+
# `==`, `equal?`, and `eql?` custom method definitions are allowed by default.
|
9
9
|
# These are customizable with `AllowedMethods` option.
|
10
10
|
#
|
11
|
+
# @safety
|
12
|
+
# This cop's autocorrection is unsafe because there is no guarantee that
|
13
|
+
# the constant `Foo` exists when autocorrecting `var.class.name == 'Foo'` to
|
14
|
+
# `var.instance_of?(Foo)`.
|
15
|
+
#
|
11
16
|
# @example
|
12
17
|
# # bad
|
13
18
|
# var.class == Date
|
@@ -18,45 +23,31 @@ module RuboCop
|
|
18
23
|
# # good
|
19
24
|
# var.instance_of?(Date)
|
20
25
|
#
|
21
|
-
# @example AllowedMethods: [] (default)
|
26
|
+
# @example AllowedMethods: ['==', 'equal?', 'eql?'] (default)
|
22
27
|
# # good
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# var.class == Date
|
27
|
-
# var.class.equal?(Date)
|
28
|
-
# var.class.eql?(Date)
|
29
|
-
# var.class.name == 'Date'
|
28
|
+
# def ==(other)
|
29
|
+
# self.class == other.class && name == other.name
|
30
|
+
# end
|
30
31
|
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
# var.class == Date
|
35
|
-
# var.class.name == 'Date'
|
32
|
+
# def equal?(other)
|
33
|
+
# self.class.equal?(other.class) && name.equal?(other.name)
|
34
|
+
# end
|
36
35
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
36
|
+
# def eql?(other)
|
37
|
+
# self.class.eql?(other.class) && name.eql?(other.name)
|
38
|
+
# end
|
40
39
|
#
|
41
40
|
# @example AllowedPatterns: [] (default)
|
42
|
-
# # good
|
43
|
-
# var.instance_of?(Date)
|
44
|
-
#
|
45
41
|
# # bad
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
# var.class.name == 'Date'
|
42
|
+
# def eq(other)
|
43
|
+
# self.class.eq(other.class) && name.eq(other.name)
|
44
|
+
# end
|
50
45
|
#
|
51
46
|
# @example AllowedPatterns: ['eq']
|
52
47
|
# # good
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
# # bad
|
58
|
-
# var.class == Date
|
59
|
-
# var.class.name == 'Date'
|
48
|
+
# def eq(other)
|
49
|
+
# self.class.eq(other.class) && name.eq(other.name)
|
50
|
+
# end
|
60
51
|
#
|
61
52
|
class ClassEqualityComparison < Base
|
62
53
|
include RangeHelp
|
@@ -64,14 +55,15 @@ module RuboCop
|
|
64
55
|
include AllowedPattern
|
65
56
|
extend AutoCorrector
|
66
57
|
|
67
|
-
MSG = 'Use `instance_of
|
58
|
+
MSG = 'Use `instance_of?%<class_argument>s` instead of comparing classes.'
|
68
59
|
|
69
60
|
RESTRICT_ON_SEND = %i[== equal? eql?].freeze
|
61
|
+
CLASS_NAME_METHODS = %i[name to_s inspect].freeze
|
70
62
|
|
71
63
|
# @!method class_comparison_candidate?(node)
|
72
64
|
def_node_matcher :class_comparison_candidate?, <<~PATTERN
|
73
65
|
(send
|
74
|
-
{$(send _ :class) (send $(send _ :class)
|
66
|
+
{$(send _ :class) (send $(send _ :class) #class_name_method?)}
|
75
67
|
{:== :equal? :eql?} $_)
|
76
68
|
PATTERN
|
77
69
|
|
@@ -82,11 +74,15 @@ module RuboCop
|
|
82
74
|
matches_allowed_pattern?(def_node.method_name))
|
83
75
|
|
84
76
|
class_comparison_candidate?(node) do |receiver_node, class_node|
|
77
|
+
return if class_node.dstr_type?
|
78
|
+
|
85
79
|
range = offense_range(receiver_node, node)
|
86
|
-
class_name = class_name(class_node, node)
|
80
|
+
class_argument = (class_name = class_name(class_node, node)) ? "(#{class_name})" : ''
|
87
81
|
|
88
|
-
add_offense(range, message: format(MSG,
|
89
|
-
|
82
|
+
add_offense(range, message: format(MSG, class_argument: class_argument)) do |corrector|
|
83
|
+
next unless class_name
|
84
|
+
|
85
|
+
corrector.replace(range, "instance_of?#{class_argument}")
|
90
86
|
end
|
91
87
|
end
|
92
88
|
end
|
@@ -94,19 +90,41 @@ module RuboCop
|
|
94
90
|
private
|
95
91
|
|
96
92
|
def class_name(class_node, node)
|
97
|
-
if node.children.first.
|
98
|
-
|
93
|
+
if class_name_method?(node.children.first.method_name)
|
94
|
+
if (receiver = class_node.receiver) && class_name_method?(class_node.method_name)
|
95
|
+
return receiver.source
|
96
|
+
end
|
99
97
|
|
100
98
|
if class_node.str_type?
|
101
|
-
value = class_node
|
102
|
-
value.prepend('::') if class_node
|
99
|
+
value = trim_string_quotes(class_node)
|
100
|
+
value.prepend('::') if require_cbase?(class_node)
|
103
101
|
return value
|
102
|
+
elsif unable_to_determine_type?(class_node)
|
103
|
+
# When a variable or return value of a method is used, it returns nil
|
104
|
+
# because the type is not known and cannot be suggested.
|
105
|
+
return
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
107
109
|
class_node.source
|
108
110
|
end
|
109
111
|
|
112
|
+
def class_name_method?(method_name)
|
113
|
+
CLASS_NAME_METHODS.include?(method_name)
|
114
|
+
end
|
115
|
+
|
116
|
+
def require_cbase?(class_node)
|
117
|
+
class_node.each_ancestor(:class, :module).any?
|
118
|
+
end
|
119
|
+
|
120
|
+
def unable_to_determine_type?(class_node)
|
121
|
+
class_node.variable? || class_node.call_type?
|
122
|
+
end
|
123
|
+
|
124
|
+
def trim_string_quotes(class_node)
|
125
|
+
class_node.source.delete('"').delete("'")
|
126
|
+
end
|
127
|
+
|
110
128
|
def offense_range(receiver_node, node)
|
111
129
|
range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
|
112
130
|
end
|
@@ -9,8 +9,8 @@ module RuboCop
|
|
9
9
|
# @safety
|
10
10
|
# It is unsafe by default because false positives may occur in the
|
11
11
|
# `nil` check of block arguments to the receiver object. Additionally,
|
12
|
-
#
|
13
|
-
#
|
12
|
+
# we can't know the type of the receiver object for sure, which may
|
13
|
+
# result in false positives as well.
|
14
14
|
#
|
15
15
|
# For example, `[[1, 2], [3, nil]].reject { |first, second| second.nil? }`
|
16
16
|
# and `[[1, 2], [3, nil]].compact` are not compatible. This will work fine
|
@@ -19,7 +19,9 @@ module RuboCop
|
|
19
19
|
# @example
|
20
20
|
# # bad
|
21
21
|
# array.reject(&:nil?)
|
22
|
+
# array.delete_if(&:nil?)
|
22
23
|
# array.reject { |e| e.nil? }
|
24
|
+
# array.delete_if { |e| e.nil? }
|
23
25
|
# array.select { |e| !e.nil? }
|
24
26
|
#
|
25
27
|
# # good
|
@@ -33,17 +35,25 @@ module RuboCop
|
|
33
35
|
# # good
|
34
36
|
# hash.compact!
|
35
37
|
#
|
38
|
+
# @example AllowedReceivers: ['params']
|
39
|
+
# # good
|
40
|
+
# params.reject(&:nil?)
|
41
|
+
#
|
36
42
|
class CollectionCompact < Base
|
43
|
+
include AllowedReceivers
|
37
44
|
include RangeHelp
|
38
45
|
extend AutoCorrector
|
46
|
+
extend TargetRubyVersion
|
39
47
|
|
40
48
|
MSG = 'Use `%<good>s` instead of `%<bad>s`.'
|
41
|
-
RESTRICT_ON_SEND = %i[reject reject! select select!].freeze
|
49
|
+
RESTRICT_ON_SEND = %i[reject delete_if reject! select select!].freeze
|
42
50
|
TO_ENUM_METHODS = %i[to_enum lazy].freeze
|
43
51
|
|
52
|
+
minimum_target_ruby_version 2.4
|
53
|
+
|
44
54
|
# @!method reject_method_with_block_pass?(node)
|
45
55
|
def_node_matcher :reject_method_with_block_pass?, <<~PATTERN
|
46
|
-
(send !nil? {:reject :reject!}
|
56
|
+
(send !nil? {:reject :delete_if :reject!}
|
47
57
|
(block_pass
|
48
58
|
(sym :nil?)))
|
49
59
|
PATTERN
|
@@ -52,7 +62,7 @@ module RuboCop
|
|
52
62
|
def_node_matcher :reject_method?, <<~PATTERN
|
53
63
|
(block
|
54
64
|
(send
|
55
|
-
!nil? {:reject :reject!})
|
65
|
+
!nil? {:reject :delete_if :reject!})
|
56
66
|
$(args ...)
|
57
67
|
(send
|
58
68
|
$(lvar _) :nil?))
|
@@ -71,7 +81,10 @@ module RuboCop
|
|
71
81
|
|
72
82
|
def on_send(node)
|
73
83
|
return unless (range = offense_range(node))
|
74
|
-
return if
|
84
|
+
return if allowed_receiver?(node.receiver)
|
85
|
+
if (target_ruby_version <= 3.0 || node.method?(:delete_if)) && to_enum_method?(node)
|
86
|
+
return
|
87
|
+
end
|
75
88
|
|
76
89
|
good = good_method_name(node)
|
77
90
|
message = format(MSG, good: good, bad: range.source)
|
@@ -112,7 +125,7 @@ module RuboCop
|
|
112
125
|
end
|
113
126
|
|
114
127
|
def range(begin_pos_node, end_pos_node)
|
115
|
-
range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.
|
128
|
+
range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.source_range.end_pos)
|
116
129
|
end
|
117
130
|
end
|
118
131
|
end
|
@@ -25,6 +25,7 @@ module RuboCop
|
|
25
25
|
# # bad
|
26
26
|
# items.collect
|
27
27
|
# items.collect!
|
28
|
+
# items.collect_concat
|
28
29
|
# items.inject
|
29
30
|
# items.detect
|
30
31
|
# items.find_all
|
@@ -33,6 +34,7 @@ module RuboCop
|
|
33
34
|
# # good
|
34
35
|
# items.map
|
35
36
|
# items.map!
|
37
|
+
# items.flat_map
|
36
38
|
# items.reduce
|
37
39
|
# items.find
|
38
40
|
# items.select
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for methods invoked via the
|
7
|
-
# of the
|
6
|
+
# Checks for methods invoked via the `::` operator instead
|
7
|
+
# of the `.` operator (like `FileUtils::rmdir` instead of `FileUtils.rmdir`).
|
8
8
|
#
|
9
9
|
# @example
|
10
10
|
# # bad
|
@@ -57,38 +57,60 @@ module RuboCop
|
|
57
57
|
# end
|
58
58
|
#
|
59
59
|
class CombinableLoops < Base
|
60
|
+
extend AutoCorrector
|
61
|
+
|
62
|
+
include RangeHelp
|
63
|
+
|
60
64
|
MSG = 'Combine this loop with the previous loop.'
|
61
65
|
|
62
66
|
def on_block(node)
|
63
67
|
return unless node.parent&.begin_type?
|
64
68
|
return unless collection_looping_method?(node)
|
69
|
+
return unless same_collection_looping_block?(node, node.left_sibling)
|
70
|
+
return unless node.body && node.left_sibling.body
|
65
71
|
|
66
|
-
add_offense(node)
|
72
|
+
add_offense(node) do |corrector|
|
73
|
+
combine_with_left_sibling(corrector, node)
|
74
|
+
end
|
67
75
|
end
|
68
76
|
|
69
77
|
alias on_numblock on_block
|
70
78
|
|
71
79
|
def on_for(node)
|
72
80
|
return unless node.parent&.begin_type?
|
81
|
+
return unless same_collection_looping_for?(node, node.left_sibling)
|
73
82
|
|
74
|
-
|
75
|
-
|
83
|
+
add_offense(node) do |corrector|
|
84
|
+
combine_with_left_sibling(corrector, node)
|
85
|
+
end
|
76
86
|
end
|
77
87
|
|
78
88
|
private
|
79
89
|
|
80
90
|
def collection_looping_method?(node)
|
81
|
-
|
82
|
-
method_name = node.method_name.to_s
|
91
|
+
method_name = node.method_name
|
83
92
|
method_name.start_with?('each') || method_name.end_with?('_each')
|
84
93
|
end
|
85
94
|
|
86
|
-
def
|
87
|
-
(sibling
|
88
|
-
|
95
|
+
def same_collection_looping_block?(node, sibling)
|
96
|
+
return false if sibling.nil? || (!sibling.block_type? && !sibling.numblock_type?)
|
97
|
+
|
98
|
+
sibling.method?(node.method_name) &&
|
89
99
|
sibling.receiver == node.receiver &&
|
90
100
|
sibling.send_node.arguments == node.send_node.arguments
|
91
101
|
end
|
102
|
+
|
103
|
+
def same_collection_looping_for?(node, sibling)
|
104
|
+
sibling&.for_type? && node.collection == sibling.collection
|
105
|
+
end
|
106
|
+
|
107
|
+
def combine_with_left_sibling(corrector, node)
|
108
|
+
corrector.replace(
|
109
|
+
node.left_sibling.body,
|
110
|
+
"#{node.left_sibling.body.source}\n#{node.body.source}"
|
111
|
+
)
|
112
|
+
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
|
113
|
+
end
|
92
114
|
end
|
93
115
|
end
|
94
116
|
end
|
@@ -66,7 +66,7 @@ module RuboCop
|
|
66
66
|
|
67
67
|
def register_offense(comment, matched_keyword)
|
68
68
|
add_offense(comment, message: format(MSG, keyword: matched_keyword)) do |corrector|
|
69
|
-
range = range_with_surrounding_space(comment.
|
69
|
+
range = range_with_surrounding_space(comment.source_range, newlines: false)
|
70
70
|
corrector.remove(range)
|
71
71
|
|
72
72
|
unless matched_keyword == 'end'
|
@@ -84,7 +84,7 @@ module RuboCop
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def source_line(comment)
|
87
|
-
comment.
|
87
|
+
comment.source_range.source_line
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Enforces the use of `Comparable#clamp` instead of comparison by minimum and maximum.
|
7
|
+
#
|
8
|
+
# This cop supports autocorrection for `if/elsif/else` bad style only.
|
9
|
+
# Because `ArgumentError` occurs if the minimum and maximum of `clamp` arguments are reversed.
|
10
|
+
# When these are variables, it is not possible to determine which is the minimum and maximum:
|
11
|
+
#
|
12
|
+
# [source,ruby]
|
13
|
+
# ----
|
14
|
+
# [1, [2, 3].max].min # => 1
|
15
|
+
# 1.clamp(3, 1) # => min argument must be smaller than max argument (ArgumentError)
|
16
|
+
# ----
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
#
|
20
|
+
# # bad
|
21
|
+
# [[x, low].max, high].min
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
# if x < low
|
25
|
+
# low
|
26
|
+
# elsif high < x
|
27
|
+
# high
|
28
|
+
# else
|
29
|
+
# x
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # good
|
33
|
+
# x.clamp(low, high)
|
34
|
+
#
|
35
|
+
class ComparableClamp < Base
|
36
|
+
include Alignment
|
37
|
+
extend AutoCorrector
|
38
|
+
extend TargetRubyVersion
|
39
|
+
|
40
|
+
minimum_target_ruby_version 2.4
|
41
|
+
|
42
|
+
MSG = 'Use `%<prefer>s` instead of `if/elsif/else`.'
|
43
|
+
MSG_MIN_MAX = 'Use `Comparable#clamp` instead.'
|
44
|
+
RESTRICT_ON_SEND = %i[min max].freeze
|
45
|
+
|
46
|
+
# @!method if_elsif_else_condition?(node)
|
47
|
+
def_node_matcher :if_elsif_else_condition?, <<~PATTERN
|
48
|
+
{
|
49
|
+
(if (send _x :< _min) _min (if (send _max :< _x) _max _x))
|
50
|
+
(if (send _min :> _x) _min (if (send _max :< _x) _max _x))
|
51
|
+
(if (send _x :< _min) _min (if (send _x :> _max) _max _x))
|
52
|
+
(if (send _min :> _x) _min (if (send _x :> _max) _max _x))
|
53
|
+
(if (send _max :< _x) _max (if (send _x :< _min) _min _x))
|
54
|
+
(if (send _x :> _max) _max (if (send _x :< _min) _min _x))
|
55
|
+
(if (send _max :< _x) _max (if (send _min :> _x) _min _x))
|
56
|
+
(if (send _x :> _max) _max (if (send _min :> _x) _min _x))
|
57
|
+
}
|
58
|
+
PATTERN
|
59
|
+
|
60
|
+
# @!method array_min_max?(node)
|
61
|
+
def_node_matcher :array_min_max?, <<~PATTERN
|
62
|
+
{
|
63
|
+
(send
|
64
|
+
(array
|
65
|
+
(send (array _ _) :max) _) :min)
|
66
|
+
(send
|
67
|
+
(array
|
68
|
+
_ (send (array _ _) :max)) :min)
|
69
|
+
(send
|
70
|
+
(array
|
71
|
+
(send (array _ _) :min) _) :max)
|
72
|
+
(send
|
73
|
+
(array
|
74
|
+
_ (send (array _ _) :min)) :max)
|
75
|
+
}
|
76
|
+
PATTERN
|
77
|
+
|
78
|
+
def on_if(node)
|
79
|
+
return unless if_elsif_else_condition?(node)
|
80
|
+
|
81
|
+
if_body, elsif_body, else_body = *node.branches
|
82
|
+
|
83
|
+
else_body_source = else_body.source
|
84
|
+
|
85
|
+
if min_condition?(node.condition, else_body_source)
|
86
|
+
min = if_body.source
|
87
|
+
max = elsif_body.source
|
88
|
+
else
|
89
|
+
min = elsif_body.source
|
90
|
+
max = if_body.source
|
91
|
+
end
|
92
|
+
|
93
|
+
prefer = "#{else_body_source}.clamp(#{min}, #{max})"
|
94
|
+
|
95
|
+
add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
|
96
|
+
autocorrect(corrector, node, prefer)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def on_send(node)
|
101
|
+
return unless array_min_max?(node)
|
102
|
+
|
103
|
+
add_offense(node, message: MSG_MIN_MAX)
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def autocorrect(corrector, node, prefer)
|
109
|
+
if node.elsif?
|
110
|
+
corrector.insert_before(node, "else\n")
|
111
|
+
corrector.replace(node, "#{indentation(node)}#{prefer}")
|
112
|
+
else
|
113
|
+
corrector.replace(node, prefer)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def min_condition?(if_condition, else_body)
|
118
|
+
lhs, op, rhs = *if_condition
|
119
|
+
|
120
|
+
(lhs.source == else_body && op == :<) || (rhs.source == else_body && op == :>)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -30,6 +30,7 @@ module RuboCop
|
|
30
30
|
'Use `push` with elements as arguments without array brackets instead of `%<current>s`.'
|
31
31
|
RESTRICT_ON_SEND = %i[concat].freeze
|
32
32
|
|
33
|
+
# rubocop:disable Metrics
|
33
34
|
def on_send(node)
|
34
35
|
return if node.arguments.empty?
|
35
36
|
return unless node.arguments.all?(&:array_type?)
|
@@ -37,17 +38,31 @@ module RuboCop
|
|
37
38
|
offense = offense_range(node)
|
38
39
|
current = offense.source
|
39
40
|
|
40
|
-
if node.arguments.any?(&:percent_literal?)
|
41
|
-
|
41
|
+
if (use_percent_literal = node.arguments.any?(&:percent_literal?))
|
42
|
+
if percent_literals_includes_only_basic_literals?(node)
|
43
|
+
prefer = preferred_method(node)
|
44
|
+
message = format(MSG, prefer: prefer, current: current)
|
45
|
+
else
|
46
|
+
message = format(MSG_FOR_PERCENT_LITERALS, current: current)
|
47
|
+
end
|
42
48
|
else
|
43
49
|
prefer = preferred_method(node)
|
44
50
|
message = format(MSG, prefer: prefer, current: current)
|
45
51
|
end
|
46
52
|
|
47
53
|
add_offense(offense, message: message) do |corrector|
|
48
|
-
|
54
|
+
if use_percent_literal
|
55
|
+
corrector.replace(offense, prefer)
|
56
|
+
else
|
57
|
+
corrector.replace(node.loc.selector, 'push')
|
58
|
+
node.arguments.each do |argument|
|
59
|
+
corrector.remove(argument.loc.begin)
|
60
|
+
corrector.remove(argument.loc.end)
|
61
|
+
end
|
62
|
+
end
|
49
63
|
end
|
50
64
|
end
|
65
|
+
# rubocop:enable Metrics
|
51
66
|
|
52
67
|
private
|
53
68
|
|
@@ -56,10 +71,23 @@ module RuboCop
|
|
56
71
|
end
|
57
72
|
|
58
73
|
def preferred_method(node)
|
59
|
-
new_arguments =
|
74
|
+
new_arguments =
|
75
|
+
node.arguments.map do |arg|
|
76
|
+
if arg.percent_literal?
|
77
|
+
arg.children.map { |child| child.value.inspect }
|
78
|
+
else
|
79
|
+
arg.children.map(&:source)
|
80
|
+
end
|
81
|
+
end.join(', ')
|
60
82
|
|
61
83
|
"push(#{new_arguments})"
|
62
84
|
end
|
85
|
+
|
86
|
+
def percent_literals_includes_only_basic_literals?(node)
|
87
|
+
node.arguments.select(&:percent_literal?).all? do |arg|
|
88
|
+
arg.children.all? { |child| child.str_type? || child.sym_type? }
|
89
|
+
end
|
90
|
+
end
|
63
91
|
end
|
64
92
|
end
|
65
93
|
end
|