rubocop 1.30.1 → 1.64.0
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 +8 -6
- data/assets/output.css.erb +159 -0
- data/assets/output.html.erb +1 -160
- data/config/default.yml +584 -73
- data/config/obsoletion.yml +30 -1
- data/exe/rubocop +15 -7
- data/lib/rubocop/arguments_env.rb +17 -0
- data/lib/rubocop/arguments_file.rb +17 -0
- data/lib/rubocop/cache_config.rb +29 -0
- data/lib/rubocop/cached_data.rb +11 -3
- data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +30 -9
- data/lib/rubocop/cli/command/execute_runner.rb +14 -9
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/lsp.rb +19 -0
- data/lib/rubocop/cli/command/show_docs_url.rb +2 -2
- data/lib/rubocop/cli/command/suggest_extensions.rb +61 -16
- data/lib/rubocop/cli.rb +70 -11
- data/lib/rubocop/comment_config.rb +60 -1
- data/lib/rubocop/config.rb +84 -28
- data/lib/rubocop/config_finder.rb +78 -0
- data/lib/rubocop/config_loader.rb +46 -68
- data/lib/rubocop/config_loader_resolver.rb +8 -8
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +5 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +13 -1
- data/lib/rubocop/config_obsoletion.rb +15 -7
- data/lib/rubocop/config_validator.rb +16 -9
- data/lib/rubocop/cop/autocorrect_logic.rb +37 -14
- data/lib/rubocop/cop/badge.rb +9 -4
- data/lib/rubocop/cop/base.rb +181 -98
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
- data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
- data/lib/rubocop/cop/bundler/gem_filename.rb +4 -4
- data/lib/rubocop/cop/bundler/gem_version.rb +5 -7
- data/lib/rubocop/cop/bundler/ordered_gems.rb +11 -3
- data/lib/rubocop/cop/commissioner.rb +19 -6
- data/lib/rubocop/cop/cop.rb +54 -34
- data/lib/rubocop/cop/corrector.rb +33 -13
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +6 -14
- 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 +26 -10
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +2 -7
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +58 -0
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
- data/lib/rubocop/cop/documentation.rb +16 -6
- data/lib/rubocop/cop/exclude_limit.rb +1 -1
- data/lib/rubocop/cop/gemspec/dependency_version.rb +22 -26
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +33 -18
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +10 -2
- data/lib/rubocop/cop/gemspec/require_mfa.rb +21 -21
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +3 -3
- data/lib/rubocop/cop/generator.rb +6 -3
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +38 -12
- data/lib/rubocop/cop/internal_affairs/create_empty_file.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +46 -24
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +111 -0
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb +46 -0
- 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/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 +128 -38
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +42 -0
- data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -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/single_line_comparison.rb +62 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +60 -0
- data/lib/rubocop/cop/internal_affairs.rb +11 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +16 -12
- data/lib/rubocop/cop/layout/block_end_newline.rb +31 -9
- data/lib/rubocop/cop/layout/class_structure.rb +44 -27
- 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 +4 -2
- data/lib/rubocop/cop/layout/dot_position.rb +1 -5
- data/lib/rubocop/cop/layout/empty_comment.rb +6 -4
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +28 -5
- data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -2
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.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 +23 -3
- data/lib/rubocop/cop/layout/end_of_line.rb +4 -4
- data/lib/rubocop/cop/layout/extra_spacing.rb +12 -9
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +15 -4
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +44 -20
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +35 -8
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +51 -12
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +36 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +57 -8
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +52 -19
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +12 -6
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +12 -12
- data/lib/rubocop/cop/layout/indentation_style.rb +8 -3
- data/lib/rubocop/cop/layout/indentation_width.rb +9 -5
- data/lib/rubocop/cop/layout/initial_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +144 -0
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +140 -0
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
- data/lib/rubocop/cop/layout/line_length.rb +8 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +31 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +4 -2
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +29 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +39 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -5
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +77 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +35 -13
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +6 -6
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
- 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_block_parameters.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -3
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +54 -22
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +22 -11
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +2 -2
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +22 -20
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +32 -12
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +31 -4
- data/lib/rubocop/cop/layout/space_inside_parens.rb +3 -3
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +34 -0
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +10 -6
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +6 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +13 -6
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
- data/lib/rubocop/cop/legacy/corrector.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +40 -8
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +17 -7
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +5 -1
- data/lib/rubocop/cop/lint/debugger.rb +69 -34
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +66 -110
- data/lib/rubocop/cop/lint/deprecated_constants.rb +8 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +0 -2
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +49 -19
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +55 -11
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/else_layout.rb +3 -7
- data/lib/rubocop/cop/lint/empty_block.rb +4 -8
- data/lib/rubocop/cop/lint/empty_class.rb +3 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +110 -2
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +15 -16
- data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
- 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/interpolation_check.rb +5 -4
- data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -0
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +49 -3
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +18 -3
- data/lib/rubocop/cop/lint/missing_super.rb +63 -5
- data/lib/rubocop/cop/lint/mixed_case_range.rb +116 -0
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +53 -9
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +11 -7
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +165 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +15 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +45 -13
- 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 +4 -6
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +17 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +6 -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 +54 -14
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +7 -7
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +57 -10
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +85 -8
- 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 +18 -11
- data/lib/rubocop/cop/lint/redundant_with_object.rb +13 -12
- data/lib/rubocop/cop/lint/refinement_import_methods.rb +2 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +8 -2
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +57 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +2 -4
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +50 -11
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
- 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/send_with_mixin_argument.rb +6 -6
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowed_exception.rb +21 -22
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +35 -4
- data/lib/rubocop/cop/lint/struct_new_override.rb +14 -14
- data/lib/rubocop/cop/lint/suppressed_exception.rb +3 -3
- data/lib/rubocop/cop/lint/symbol_conversion.rb +8 -3
- data/lib/rubocop/cop/lint/syntax.rb +10 -3
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +23 -6
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +2 -2
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +4 -2
- data/lib/rubocop/cop/lint/unreachable_loop.rb +20 -8
- data/lib/rubocop/cop/lint/unused_method_argument.rb +6 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +19 -14
- data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
- 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 +15 -5
- data/lib/rubocop/cop/lint/useless_times.rb +3 -3
- data/lib/rubocop/cop/lint/void.rb +126 -25
- data/lib/rubocop/cop/metrics/abc_size.rb +7 -5
- data/lib/rubocop/cop/metrics/block_length.rb +17 -12
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/class_length.rb +17 -6
- 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 +18 -12
- data/lib/rubocop/cop/metrics/module_length.rb +10 -5
- 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 +43 -12
- data/lib/rubocop/cop/migration/department_name.rb +3 -3
- data/lib/rubocop/cop/mixin/alignment.rb +2 -2
- data/lib/rubocop/cop/mixin/allowed_identifiers.rb +2 -2
- data/lib/rubocop/cop/mixin/allowed_methods.rb +23 -2
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +17 -1
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/annotation_comment.rb +14 -7
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -1
- data/lib/rubocop/cop/mixin/code_length.rb +12 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +37 -11
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
- data/lib/rubocop/cop/mixin/def_node.rb +3 -8
- 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/enforce_superclass.rb +2 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +11 -7
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +162 -12
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +13 -9
- data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -2
- data/lib/rubocop/cop/mixin/method_complexity.rb +28 -22
- data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +34 -12
- data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -3
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/percent_array.rb +58 -1
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +7 -9
- data/lib/rubocop/cop/mixin/range_help.rb +28 -7
- data/lib/rubocop/cop/mixin/require_library.rb +2 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +5 -3
- data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +18 -3
- data/lib/rubocop/cop/mixin/string_help.rb +4 -2
- data/lib/rubocop/cop/mixin/surrounding_space.rb +13 -11
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
- data/lib/rubocop/cop/mixin/visibility_help.rb +40 -5
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +39 -8
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +3 -1
- data/lib/rubocop/cop/naming/constant_name.rb +4 -5
- data/lib/rubocop/cop/naming/file_name.rb +3 -3
- 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 +29 -8
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +26 -11
- data/lib/rubocop/cop/naming/method_name.rb +3 -3
- data/lib/rubocop/cop/naming/predicate_name.rb +33 -4
- 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 +74 -46
- data/lib/rubocop/cop/security/compound_hash.rb +4 -3
- data/lib/rubocop/cop/security/open.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +142 -3
- data/lib/rubocop/cop/style/accessor_grouping.rb +50 -20
- data/lib/rubocop/cop/style/alias.rb +19 -9
- data/lib/rubocop/cop/style/arguments_forwarding.rb +414 -62
- data/lib/rubocop/cop/style/array_first_last.rb +64 -0
- data/lib/rubocop/cop/style/array_intersect.rb +119 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +3 -3
- data/lib/rubocop/cop/style/block_comments.rb +3 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +52 -15
- data/lib/rubocop/cop/style/case_equality.rb +40 -10
- data/lib/rubocop/cop/style/case_like_if.rb +25 -8
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +9 -16
- data/lib/rubocop/cop/style/class_check.rb +1 -0
- data/lib/rubocop/cop/style/class_equality_comparison.rb +79 -12
- data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
- data/lib/rubocop/cop/style/class_vars.rb +3 -3
- data/lib/rubocop/cop/style/collection_compact.rb +44 -12
- data/lib/rubocop/cop/style/collection_methods.rb +4 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +38 -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 +7 -4
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +95 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +16 -20
- data/lib/rubocop/cop/style/copyright.rb +32 -22
- data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
- data/lib/rubocop/cop/style/date_time.rb +5 -4
- 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 +22 -10
- data/lib/rubocop/cop/style/documentation_method.rb +30 -4
- data/lib/rubocop/cop/style/double_negation.rb +4 -2
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +45 -10
- data/lib/rubocop/cop/style/each_with_object.rb +40 -9
- data/lib/rubocop/cop/style/empty_block_parameter.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
- data/lib/rubocop/cop/style/empty_else.rb +37 -0
- data/lib/rubocop/cop/style/empty_heredoc.rb +73 -0
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -1
- data/lib/rubocop/cop/style/empty_method.rb +17 -2
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/endless_method.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +14 -23
- data/lib/rubocop/cop/style/exact_regexp_match.rb +70 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +8 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +10 -177
- 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 +5 -1
- data/lib/rubocop/cop/style/format_string.rb +33 -12
- data/lib/rubocop/cop/style/format_string_token.rb +73 -23
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +5 -3
- data/lib/rubocop/cop/style/guard_clause.rb +159 -42
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +131 -22
- data/lib/rubocop/cop/style/hash_except.rb +101 -9
- data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
- data/lib/rubocop/cop/style/hash_syntax.rb +51 -5
- 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 +47 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +112 -16
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +34 -5
- data/lib/rubocop/cop/style/if_with_semicolon.rb +6 -6
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +2 -2
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/inverse_methods.rb +21 -16
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +160 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
- data/lib/rubocop/cop/style/lambda.rb +3 -3
- data/lib/rubocop/cop/style/lambda_call.rb +5 -0
- data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -2
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +8 -10
- data/lib/rubocop/cop/style/map_into_array.rb +175 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +21 -8
- data/lib/rubocop/cop/style/map_to_set.rb +64 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +63 -32
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +50 -45
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +41 -2
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +12 -5
- 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/missing_respond_to_missing.rb +2 -2
- data/lib/rubocop/cop/style/mixin_grouping.rb +5 -5
- data/lib/rubocop/cop/style/module_function.rb +30 -8
- data/lib/rubocop/cop/style/multiline_block_chain.rb +4 -2
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -6
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +2 -4
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/multiline_method_signature.rb +18 -6
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +22 -5
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -3
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +17 -10
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +10 -1
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +18 -14
- data/lib/rubocop/cop/style/next.rb +4 -6
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +4 -4
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +53 -11
- data/lib/rubocop/cop/style/object_then.rb +10 -3
- data/lib/rubocop/cop/style/one_line_conditional.rb +4 -7
- data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +73 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +32 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
- 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/perl_backrefs.rb +22 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +4 -1
- 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 +37 -5
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +13 -2
- data/lib/rubocop/cop/style/redundant_condition.rb +41 -8
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -14
- data/lib/rubocop/cop/style/redundant_constant_base.rb +85 -0
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +39 -0
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +133 -0
- data/lib/rubocop/cop/style/redundant_each.rb +119 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +10 -8
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +118 -0
- data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
- data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +199 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +91 -44
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +9 -3
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +22 -4
- data/lib/rubocop/cop/style/redundant_return.rb +21 -3
- data/lib/rubocop/cop/style/redundant_self.rb +19 -2
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +33 -17
- data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
- data/lib/rubocop/cop/style/redundant_string_escape.rb +185 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +139 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -4
- 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 +41 -10
- data/lib/rubocop/cop/style/sample.rb +3 -4
- data/lib/rubocop/cop/style/select_by_regexp.rb +30 -11
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/semicolon.rb +71 -8
- data/lib/rubocop/cop/style/send.rb +4 -4
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +83 -0
- data/lib/rubocop/cop/style/signal_exception.rb +9 -7
- data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +77 -11
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +25 -12
- data/lib/rubocop/cop/style/special_global_vars.rb +4 -6
- data/lib/rubocop/cop/style/static_class.rb +32 -1
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/string_chars.rb +1 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -5
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
- data/lib/rubocop/cop/style/strip.rb +7 -4
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -3
- data/lib/rubocop/cop/style/super_arguments.rb +137 -0
- data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
- data/lib/rubocop/cop/style/swap_values.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +40 -19
- data/lib/rubocop/cop/style/symbol_proc.rb +110 -15
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -13
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +4 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
- data/lib/rubocop/cop/style/unpack_first.rb +11 -14
- data/lib/rubocop/cop/style/word_array.rb +59 -5
- 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 +66 -56
- data/lib/rubocop/cop/util.rb +46 -10
- 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 +6 -4
- data/lib/rubocop/cop/variable_force.rb +19 -30
- data/lib/rubocop/cops_documentation_generator.rb +60 -18
- data/lib/rubocop/directive_comment.rb +14 -12
- data/lib/rubocop/ext/comment.rb +18 -0
- data/lib/rubocop/ext/processed_source.rb +2 -0
- data/lib/rubocop/ext/range.rb +15 -0
- data/lib/rubocop/ext/regexp_node.rb +10 -5
- data/lib/rubocop/ext/regexp_parser.rb +5 -2
- data/lib/rubocop/feature_loader.rb +94 -0
- data/lib/rubocop/file_finder.rb +4 -7
- data/lib/rubocop/file_patterns.rb +43 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +4 -8
- data/lib/rubocop/formatter/disabled_config_formatter.rb +47 -15
- data/lib/rubocop/formatter/formatter_set.rb +27 -20
- data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +15 -2
- data/lib/rubocop/formatter/html_formatter.rb +38 -18
- data/lib/rubocop/formatter/json_formatter.rb +0 -1
- data/lib/rubocop/formatter/junit_formatter.rb +4 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +21 -6
- data/lib/rubocop/formatter/simple_text_formatter.rb +7 -8
- data/lib/rubocop/formatter/tap_formatter.rb +4 -8
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +6 -3
- data/lib/rubocop/formatter.rb +34 -0
- data/lib/rubocop/lockfile.rb +56 -7
- data/lib/rubocop/lsp/logger.rb +22 -0
- data/lib/rubocop/lsp/routes.rb +243 -0
- data/lib/rubocop/lsp/runtime.rb +99 -0
- data/lib/rubocop/lsp/server.rb +73 -0
- data/lib/rubocop/lsp/severity.rb +27 -0
- data/lib/rubocop/lsp.rb +36 -0
- data/lib/rubocop/magic_comment.rb +13 -11
- data/lib/rubocop/options.rb +101 -32
- data/lib/rubocop/path_util.rb +54 -22
- data/lib/rubocop/rake_task.rb +35 -10
- data/lib/rubocop/result_cache.rb +29 -24
- data/lib/rubocop/rspec/cop_helper.rb +32 -3
- data/lib/rubocop/rspec/expect_offense.rb +18 -8
- data/lib/rubocop/rspec/shared_contexts.rb +86 -33
- data/lib/rubocop/rspec/support.rb +19 -2
- data/lib/rubocop/runner.rb +91 -23
- data/lib/rubocop/server/cache.rb +155 -0
- data/lib/rubocop/server/cli.rb +147 -0
- data/lib/rubocop/server/client_command/base.rb +44 -0
- data/lib/rubocop/server/client_command/exec.rb +64 -0
- data/lib/rubocop/server/client_command/restart.rb +25 -0
- data/lib/rubocop/server/client_command/start.rb +48 -0
- data/lib/rubocop/server/client_command/status.rb +28 -0
- data/lib/rubocop/server/client_command/stop.rb +31 -0
- data/lib/rubocop/server/client_command.rb +26 -0
- data/lib/rubocop/server/core.rb +111 -0
- data/lib/rubocop/server/errors.rb +23 -0
- data/lib/rubocop/server/helper.rb +34 -0
- data/lib/rubocop/server/server_command/base.rb +50 -0
- data/lib/rubocop/server/server_command/exec.rb +33 -0
- data/lib/rubocop/server/server_command/stop.rb +24 -0
- data/lib/rubocop/server/server_command.rb +21 -0
- data/lib/rubocop/server/socket_reader.rb +69 -0
- data/lib/rubocop/server.rb +53 -0
- data/lib/rubocop/string_interpreter.rb +3 -3
- data/lib/rubocop/target_finder.rb +92 -82
- data/lib/rubocop/target_ruby.rb +86 -80
- data/lib/rubocop/version.rb +44 -12
- data/lib/rubocop.rb +73 -35
- metadata +145 -39
- data/lib/rubocop/cop/gemspec/date_assignment.rb +0 -49
- data/lib/rubocop/cop/mixin/ignored_methods.rb +0 -52
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for usages of `each` with `<<`, `push`, or `append` which
|
7
|
+
# can be replaced by `map`.
|
8
|
+
#
|
9
|
+
# If `PreferredMethods` is configured for `map` in `Style/CollectionMethods`,
|
10
|
+
# this cop uses the specified method for replacement.
|
11
|
+
#
|
12
|
+
# NOTE: The return value of `Enumerable#each` is `self`, whereas the
|
13
|
+
# return value of `Enumerable#map` is an `Array`. They are not autocorrected
|
14
|
+
# when a return value could be used because these types differ.
|
15
|
+
#
|
16
|
+
# NOTE: It only detects when the mapping destination is a local variable
|
17
|
+
# initialized as an empty array and referred to only by the pushing operation.
|
18
|
+
# This is because, if not, it's challenging to statically guarantee that the
|
19
|
+
# mapping destination variable remains an empty array:
|
20
|
+
#
|
21
|
+
# [source,ruby]
|
22
|
+
# ----
|
23
|
+
# ret = []
|
24
|
+
# src.each { |e| ret << e * 2 } # `<<` method may mutate `ret`
|
25
|
+
#
|
26
|
+
# dest = []
|
27
|
+
# src.each { |e| dest << transform(e, dest) } # `transform` method may mutate `dest`
|
28
|
+
# ----
|
29
|
+
#
|
30
|
+
# @safety
|
31
|
+
# This cop is unsafe because not all objects that have an `each`
|
32
|
+
# method also have a `map` method (e.g. `ENV`). Additionally, for calls
|
33
|
+
# with a block, not all objects that have a `map` method return an array
|
34
|
+
# (e.g. `Enumerator::Lazy`).
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# # bad
|
38
|
+
# dest = []
|
39
|
+
# src.each { |e| dest << e * 2 }
|
40
|
+
# dest
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# dest = src.map { |e| e * 2 }
|
44
|
+
#
|
45
|
+
# # good - contains another operation
|
46
|
+
# dest = []
|
47
|
+
# src.each { |e| dest << e * 2; puts e }
|
48
|
+
# dest
|
49
|
+
#
|
50
|
+
class MapIntoArray < Base
|
51
|
+
include RangeHelp
|
52
|
+
extend AutoCorrector
|
53
|
+
|
54
|
+
MSG = 'Use `%<new_method_name>s` instead of `each` to map elements into an array.'
|
55
|
+
|
56
|
+
# @!method each_block_with_push?(node)
|
57
|
+
def_node_matcher :each_block_with_push?, <<-PATTERN
|
58
|
+
[
|
59
|
+
^({begin kwbegin} ...)
|
60
|
+
({block numblock} (send !{nil? self} :each) _
|
61
|
+
(send (lvar _) {:<< :push :append} _))
|
62
|
+
]
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
# @!method empty_array_asgn?(node)
|
66
|
+
def_node_matcher :empty_array_asgn?, '(lvasgn _ (array))'
|
67
|
+
|
68
|
+
# @!method lvar_ref?(node, name)
|
69
|
+
def_node_matcher :lvar_ref?, '(lvar %1)'
|
70
|
+
|
71
|
+
def self.joining_forces
|
72
|
+
VariableForce
|
73
|
+
end
|
74
|
+
|
75
|
+
def after_leaving_scope(scope, _variable_table)
|
76
|
+
(@scopes ||= []) << scope
|
77
|
+
end
|
78
|
+
|
79
|
+
def on_block(node)
|
80
|
+
return unless each_block_with_push?(node)
|
81
|
+
|
82
|
+
dest_var = find_dest_var(node)
|
83
|
+
return unless (asgn = find_closest_assignment(node, dest_var))
|
84
|
+
return unless empty_array_asgn?(asgn)
|
85
|
+
return unless dest_used_only_for_mapping?(node, dest_var, asgn)
|
86
|
+
|
87
|
+
register_offense(node, dest_var, asgn)
|
88
|
+
end
|
89
|
+
|
90
|
+
alias on_numblock on_block
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def find_dest_var(block)
|
95
|
+
node = block.body.receiver
|
96
|
+
name = node.children.first
|
97
|
+
|
98
|
+
candidates = @scopes.lazy.filter_map { |s| s.variables[name] }
|
99
|
+
candidates.find { |v| v.references.any? { |n| n.node.equal?(node) } }
|
100
|
+
end
|
101
|
+
|
102
|
+
def find_closest_assignment(block, dest_var)
|
103
|
+
dest_var.assignments.reverse_each.lazy.map(&:node).find do |node|
|
104
|
+
node.source_range.end_pos < block.source_range.begin_pos
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def dest_used_only_for_mapping?(block, dest_var, asgn)
|
109
|
+
range = asgn.source_range.join(block.source_range)
|
110
|
+
|
111
|
+
asgn.parent.equal?(block.parent) &&
|
112
|
+
dest_var.references.one? { |r| range.contains?(r.node.source_range) } &&
|
113
|
+
dest_var.assignments.one? { |a| range.contains?(a.node.source_range) }
|
114
|
+
end
|
115
|
+
|
116
|
+
def register_offense(block, dest_var, asgn)
|
117
|
+
add_offense(block, message: format(MSG, new_method_name: new_method_name)) do |corrector|
|
118
|
+
next if return_value_used?(block)
|
119
|
+
|
120
|
+
corrector.replace(block.send_node.selector, new_method_name)
|
121
|
+
remove_assignment(corrector, asgn)
|
122
|
+
correct_push_node(corrector, block.body)
|
123
|
+
correct_return_value_handling(corrector, block, dest_var)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def new_method_name
|
128
|
+
default = 'map'
|
129
|
+
alternative = config.for_cop('Style/CollectionMethods').dig('PreferredMethods', default)
|
130
|
+
alternative || default
|
131
|
+
end
|
132
|
+
|
133
|
+
def return_value_used?(node)
|
134
|
+
parent = node.parent
|
135
|
+
|
136
|
+
case parent&.type
|
137
|
+
when nil
|
138
|
+
false
|
139
|
+
when :begin, :kwbegin
|
140
|
+
!node.right_sibling && return_value_used?(parent)
|
141
|
+
when :block, :numblock
|
142
|
+
!parent.void_context?
|
143
|
+
else
|
144
|
+
true
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def remove_assignment(corrector, asgn)
|
149
|
+
range = range_with_surrounding_space(asgn.source_range, side: :right)
|
150
|
+
range = range_with_surrounding_space(range, side: :right, newlines: false)
|
151
|
+
|
152
|
+
corrector.remove(range)
|
153
|
+
end
|
154
|
+
|
155
|
+
def correct_push_node(corrector, push_node)
|
156
|
+
range = push_node.source_range
|
157
|
+
arg_range = push_node.first_argument.source_range
|
158
|
+
|
159
|
+
corrector.remove(range_between(range.begin_pos, arg_range.begin_pos))
|
160
|
+
corrector.remove(range_between(arg_range.end_pos, range.end_pos))
|
161
|
+
end
|
162
|
+
|
163
|
+
def correct_return_value_handling(corrector, block, dest_var)
|
164
|
+
next_node = block.right_sibling
|
165
|
+
|
166
|
+
if lvar_ref?(next_node, dest_var.name)
|
167
|
+
corrector.remove(range_with_surrounding_space(next_node.source_range, side: :left))
|
168
|
+
end
|
169
|
+
|
170
|
+
corrector.insert_before(block, "#{dest_var.name} = ")
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -34,34 +34,47 @@ module RuboCop
|
|
34
34
|
|
35
35
|
minimum_target_ruby_version 2.6
|
36
36
|
|
37
|
-
MSG = 'Pass a block to `to_h` instead of calling `%<method>s
|
37
|
+
MSG = 'Pass a block to `to_h` instead of calling `%<method>s%<dot>sto_h`.'
|
38
38
|
RESTRICT_ON_SEND = %i[to_h].freeze
|
39
39
|
|
40
|
-
# @!method map_to_h
|
41
|
-
def_node_matcher :map_to_h
|
42
|
-
|
40
|
+
# @!method map_to_h(node)
|
41
|
+
def_node_matcher :map_to_h, <<~PATTERN
|
42
|
+
{
|
43
|
+
$(call ({block numblock} $(call _ {:map :collect}) ...) :to_h)
|
44
|
+
$(call $(call _ {:map :collect} (block_pass sym)) :to_h)
|
45
|
+
}
|
43
46
|
PATTERN
|
44
47
|
|
48
|
+
def self.autocorrect_incompatible_with
|
49
|
+
[Layout::SingleLineBlockChain]
|
50
|
+
end
|
51
|
+
|
45
52
|
def on_send(node)
|
46
|
-
return unless (to_h_node, map_node = map_to_h
|
53
|
+
return unless (to_h_node, map_node = map_to_h(node))
|
47
54
|
|
48
|
-
message = format(MSG, method: map_node.loc.selector.source)
|
55
|
+
message = format(MSG, method: map_node.loc.selector.source, dot: to_h_node.loc.dot.source)
|
49
56
|
add_offense(map_node.loc.selector, message: message) do |corrector|
|
50
57
|
# If the `to_h` call already has a block, do not autocorrect.
|
51
|
-
next if to_h_node.
|
58
|
+
next if to_h_node.block_literal?
|
52
59
|
|
53
60
|
autocorrect(corrector, to_h_node, map_node)
|
54
61
|
end
|
55
62
|
end
|
63
|
+
alias on_csend on_send
|
56
64
|
|
57
65
|
private
|
58
66
|
|
67
|
+
# rubocop:disable Metrics/AbcSize
|
59
68
|
def autocorrect(corrector, to_h, map)
|
60
69
|
removal_range = range_between(to_h.loc.dot.begin_pos, to_h.loc.selector.end_pos)
|
61
70
|
|
62
|
-
corrector.remove(range_with_surrounding_space(
|
71
|
+
corrector.remove(range_with_surrounding_space(removal_range, side: :left))
|
72
|
+
if (map_dot = map.loc.dot)
|
73
|
+
corrector.replace(map_dot, to_h.loc.dot.source)
|
74
|
+
end
|
63
75
|
corrector.replace(map.loc.selector, 'to_h')
|
64
76
|
end
|
77
|
+
# rubocop:enable Metrics/AbcSize
|
65
78
|
end
|
66
79
|
end
|
67
80
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Looks for uses of `map.to_set` or `collect.to_set` that could be
|
7
|
+
# written with just `to_set`.
|
8
|
+
#
|
9
|
+
# @safety
|
10
|
+
# This cop is unsafe, as it can produce false positives if the receiver
|
11
|
+
# is not an `Enumerable`.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# # bad
|
15
|
+
# something.map { |i| i * 2 }.to_set
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# something.to_set { |i| i * 2 }
|
19
|
+
#
|
20
|
+
# # bad
|
21
|
+
# [1, 2, 3].collect { |i| i.to_s }.to_set
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# [1, 2, 3].to_set { |i| i.to_s }
|
25
|
+
#
|
26
|
+
class MapToSet < Base
|
27
|
+
extend AutoCorrector
|
28
|
+
include RangeHelp
|
29
|
+
|
30
|
+
MSG = 'Pass a block to `to_set` instead of calling `%<method>s.to_set`.'
|
31
|
+
RESTRICT_ON_SEND = %i[to_set].freeze
|
32
|
+
|
33
|
+
# @!method map_to_set?(node)
|
34
|
+
def_node_matcher :map_to_set?, <<~PATTERN
|
35
|
+
{
|
36
|
+
$(send ({block numblock} $(send _ {:map :collect}) ...) :to_set)
|
37
|
+
$(send $(send _ {:map :collect} (block_pass sym)) :to_set)
|
38
|
+
}
|
39
|
+
PATTERN
|
40
|
+
|
41
|
+
def on_send(node)
|
42
|
+
return unless (to_set_node, map_node = map_to_set?(node))
|
43
|
+
|
44
|
+
message = format(MSG, method: map_node.loc.selector.source)
|
45
|
+
add_offense(map_node.loc.selector, message: message) do |corrector|
|
46
|
+
# If the `to_set` call already has a block, do not autocorrect.
|
47
|
+
next if to_set_node.block_literal?
|
48
|
+
|
49
|
+
autocorrect(corrector, to_set_node, map_node)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def autocorrect(corrector, to_set, map)
|
56
|
+
removal_range = range_between(to_set.loc.dot.begin_pos, to_set.loc.selector.end_pos)
|
57
|
+
|
58
|
+
corrector.remove(range_with_surrounding_space(removal_range, side: :left))
|
59
|
+
corrector.replace(map.loc.selector, 'to_set')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -13,24 +13,22 @@ module RuboCop
|
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
# rubocop:disable Metrics/PerceivedComplexity
|
17
|
-
def omit_parentheses(node)
|
16
|
+
def omit_parentheses(node) # rubocop:disable Metrics/PerceivedComplexity
|
18
17
|
return unless node.parenthesized?
|
19
18
|
return if inside_endless_method_def?(node)
|
20
19
|
return if require_parentheses_for_hash_value_omission?(node)
|
21
20
|
return if syntax_like_method_call?(node)
|
22
21
|
return if super_call_without_arguments?(node)
|
23
|
-
return if allowed_camel_case_method_call?(node)
|
24
22
|
return if legitimate_call_with_parentheses?(node)
|
23
|
+
return if allowed_camel_case_method_call?(node)
|
25
24
|
return if allowed_string_interpolation_method_call?(node)
|
26
25
|
|
27
26
|
add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
|
28
|
-
|
27
|
+
autocorrect(corrector, node)
|
29
28
|
end
|
30
29
|
end
|
31
|
-
# rubocop:enable Metrics/PerceivedComplexity
|
32
30
|
|
33
|
-
def
|
31
|
+
def autocorrect(corrector, node)
|
34
32
|
if parentheses_at_the_end_of_multiline_call?(node)
|
35
33
|
corrector.replace(args_begin(node), ' \\')
|
36
34
|
else
|
@@ -52,17 +50,13 @@ module RuboCop
|
|
52
50
|
return false unless (last_argument = node.last_argument)
|
53
51
|
return false if !last_argument.hash_type? || !last_argument.pairs.last&.value_omission?
|
54
52
|
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
def modifier_form?(node)
|
59
|
-
node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
|
53
|
+
node.parent&.conditional? || !last_expression?(node)
|
60
54
|
end
|
61
55
|
|
62
56
|
# Require hash value omission be enclosed in parentheses to prevent the following issue:
|
63
57
|
# https://bugs.ruby-lang.org/issues/18396.
|
64
|
-
def
|
65
|
-
node.parent&.assignment? ? node.parent.right_sibling : node.right_sibling
|
58
|
+
def last_expression?(node)
|
59
|
+
!(node.parent&.assignment? ? node.parent.right_sibling : node.right_sibling)
|
66
60
|
end
|
67
61
|
|
68
62
|
def syntax_like_method_call?(node)
|
@@ -90,31 +84,37 @@ module RuboCop
|
|
90
84
|
.end_with?('(')
|
91
85
|
end
|
92
86
|
|
93
|
-
def legitimate_call_with_parentheses?(node)
|
87
|
+
def legitimate_call_with_parentheses?(node) # rubocop:disable Metrics/PerceivedComplexity
|
94
88
|
call_in_literals?(node) ||
|
89
|
+
node.parent&.when_type? ||
|
95
90
|
call_with_ambiguous_arguments?(node) ||
|
96
91
|
call_in_logical_operators?(node) ||
|
97
92
|
call_in_optional_arguments?(node) ||
|
98
93
|
call_in_single_line_inheritance?(node) ||
|
99
94
|
allowed_multiline_call_with_parentheses?(node) ||
|
100
|
-
allowed_chained_call_with_parentheses?(node)
|
95
|
+
allowed_chained_call_with_parentheses?(node) ||
|
96
|
+
assignment_in_condition?(node) ||
|
97
|
+
forwards_anonymous_rest_arguments?(node)
|
101
98
|
end
|
102
99
|
|
103
100
|
def call_in_literals?(node)
|
104
|
-
node.parent
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
101
|
+
parent = node.parent&.block_type? ? node.parent.parent : node.parent
|
102
|
+
return false unless parent
|
103
|
+
|
104
|
+
parent.pair_type? ||
|
105
|
+
parent.array_type? ||
|
106
|
+
parent.range_type? ||
|
107
|
+
splat?(parent) ||
|
108
|
+
ternary_if?(parent)
|
110
109
|
end
|
111
110
|
|
112
111
|
def call_in_logical_operators?(node)
|
113
112
|
parent = node.parent&.block_type? ? node.parent.parent : node.parent
|
114
|
-
parent
|
115
|
-
|
113
|
+
return false unless parent
|
114
|
+
|
115
|
+
logical_operator?(parent) ||
|
116
116
|
(parent.send_type? &&
|
117
|
-
parent.arguments.any? { |argument| logical_operator?(argument) })
|
117
|
+
parent.arguments.any? { |argument| logical_operator?(argument) })
|
118
118
|
end
|
119
119
|
|
120
120
|
def call_in_optional_arguments?(node)
|
@@ -125,25 +125,39 @@ module RuboCop
|
|
125
125
|
node.parent&.class_type? && node.parent&.single_line?
|
126
126
|
end
|
127
127
|
|
128
|
-
def call_with_ambiguous_arguments?(node)
|
128
|
+
def call_with_ambiguous_arguments?(node) # rubocop:disable Metrics/PerceivedComplexity
|
129
129
|
call_with_braced_block?(node) ||
|
130
|
+
call_in_argument_with_block?(node) ||
|
130
131
|
call_as_argument_or_chain?(node) ||
|
132
|
+
call_in_match_pattern?(node) ||
|
131
133
|
hash_literal_in_arguments?(node) ||
|
132
134
|
node.descendants.any? do |n|
|
133
|
-
n.forwarded_args_type? ||
|
134
|
-
|
135
|
+
n.forwarded_args_type? || n.block_type? || n.numblock_type? ||
|
136
|
+
ambiguous_literal?(n) || logical_operator?(n)
|
135
137
|
end
|
136
138
|
end
|
137
139
|
|
138
140
|
def call_with_braced_block?(node)
|
139
|
-
(node.
|
140
|
-
|
141
|
+
(node.call_type? || node.super_type?) && node.block_node&.braces?
|
142
|
+
end
|
143
|
+
|
144
|
+
def call_in_argument_with_block?(node)
|
145
|
+
parent = node.parent&.block_type? && node.parent&.parent
|
146
|
+
return false unless parent
|
147
|
+
|
148
|
+
parent.call_type? || parent.super_type? || parent.yield_type?
|
141
149
|
end
|
142
150
|
|
143
151
|
def call_as_argument_or_chain?(node)
|
144
152
|
node.parent &&
|
145
|
-
(
|
146
|
-
|
153
|
+
(node.parent.call_type? || node.parent.super_type? || node.parent.yield_type?) &&
|
154
|
+
!assigned_before?(node.parent, node)
|
155
|
+
end
|
156
|
+
|
157
|
+
def call_in_match_pattern?(node)
|
158
|
+
return false unless (parent = node.parent)
|
159
|
+
|
160
|
+
parent.match_pattern_type? || parent.match_pattern_p_type?
|
147
161
|
end
|
148
162
|
|
149
163
|
def hash_literal_in_arguments?(node)
|
@@ -166,7 +180,7 @@ module RuboCop
|
|
166
180
|
previous.parenthesized? || allowed_chained_call_with_parentheses?(previous)
|
167
181
|
end
|
168
182
|
|
169
|
-
def
|
183
|
+
def ambiguous_literal?(node)
|
170
184
|
splat?(node) || ternary_if?(node) || regexp_slash_literal?(node) || unary_literal?(node)
|
171
185
|
end
|
172
186
|
|
@@ -202,6 +216,23 @@ module RuboCop
|
|
202
216
|
def inside_string_interpolation?(node)
|
203
217
|
node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
|
204
218
|
end
|
219
|
+
|
220
|
+
def assignment_in_condition?(node)
|
221
|
+
parent = node.parent
|
222
|
+
return false unless parent
|
223
|
+
|
224
|
+
grandparent = parent.parent
|
225
|
+
return false unless grandparent
|
226
|
+
|
227
|
+
parent.assignment? && (grandparent.conditional? || grandparent.when_type?)
|
228
|
+
end
|
229
|
+
|
230
|
+
def forwards_anonymous_rest_arguments?(node)
|
231
|
+
return false unless (last_argument = node.last_argument)
|
232
|
+
return true if last_argument.forwarded_restarg_type?
|
233
|
+
|
234
|
+
last_argument.hash_type? && last_argument.children.first&.forwarded_kwrestarg_type?
|
235
|
+
end
|
205
236
|
end
|
206
237
|
# rubocop:enable Metrics/ModuleLength, Metrics/CyclomaticComplexity
|
207
238
|
end
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def require_parentheses(node)
|
15
|
-
return if
|
15
|
+
return if allowed_method_name?(node.method_name)
|
16
16
|
return if matches_allowed_pattern?(node.method_name)
|
17
17
|
return if eligible_for_parentheses_omission?(node)
|
18
18
|
return unless node.arguments? && !node.parenthesized?
|
@@ -24,6 +24,10 @@ module RuboCop
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
def allowed_method_name?(name)
|
28
|
+
allowed_method?(name) || matches_allowed_pattern?(name)
|
29
|
+
end
|
30
|
+
|
27
31
|
def eligible_for_parentheses_omission?(node)
|
28
32
|
node.operator_method? || node.setter_method? || ignored_macro?(node)
|
29
33
|
end
|
@@ -6,22 +6,20 @@ module RuboCop
|
|
6
6
|
# Enforces the presence (default) or absence of parentheses in
|
7
7
|
# method calls containing parameters.
|
8
8
|
#
|
9
|
-
# In the default style (require_parentheses), macro methods are
|
10
|
-
# Additional methods can be added to the `
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# the `IncludedMacros` list.
|
9
|
+
# In the default style (require_parentheses), macro methods are allowed.
|
10
|
+
# Additional methods can be added to the `AllowedMethods` or
|
11
|
+
# `AllowedPatterns` list. These options are valid only in the default
|
12
|
+
# style. Macros can be included by either setting `IgnoreMacros` to false
|
13
|
+
# or adding specific macros to the `IncludedMacros` list.
|
15
14
|
#
|
16
|
-
# Precedence of options is
|
15
|
+
# Precedence of options is as follows:
|
17
16
|
#
|
18
|
-
# 1. `
|
17
|
+
# 1. `AllowedMethods`
|
19
18
|
# 2. `AllowedPatterns`
|
20
19
|
# 3. `IncludedMacros`
|
21
20
|
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# precedence (that is, the method is ignored).
|
21
|
+
# If a method is listed in both `IncludedMacros` and `AllowedMethods`,
|
22
|
+
# then the latter takes precedence (that is, the method is allowed).
|
25
23
|
#
|
26
24
|
# In the alternative style (omit_parentheses), there are three additional
|
27
25
|
# options.
|
@@ -40,14 +38,29 @@ module RuboCop
|
|
40
38
|
# to `true` allows the presence of parentheses in such a method call
|
41
39
|
# even with arguments.
|
42
40
|
#
|
43
|
-
# NOTE:
|
44
|
-
# results in ambiguous or syntactically incorrect code.
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
41
|
+
# NOTE: The style of `omit_parentheses` allows parentheses in cases where
|
42
|
+
# omitting them results in ambiguous or syntactically incorrect code.
|
43
|
+
#
|
44
|
+
# Non-exhaustive list of examples:
|
45
|
+
#
|
46
|
+
# - Parentheses are required allowed in method calls with arguments inside
|
47
|
+
# literals, logical operators, setting default values in position and
|
48
|
+
# keyword arguments, chaining and more.
|
49
|
+
# - Parentheses are allowed in method calls with arguments inside
|
50
|
+
# operators to avoid ambiguity.
|
51
|
+
# triple-dot syntax introduced in Ruby 2.7 as omitting them starts an
|
52
|
+
# endless range.
|
53
|
+
# - Parentheses are allowed when forwarding arguments with the
|
54
|
+
# triple-dot syntax introduced in Ruby 2.7 as omitting them starts an
|
55
|
+
# endless range.
|
56
|
+
# - Parentheses are required in calls with arguments when inside an
|
57
|
+
# endless method definition introduced in Ruby 3.0.
|
58
|
+
# - Ruby 3.1's hash omission syntax allows parentheses if the method call
|
59
|
+
# is in conditionals and requires parentheses if the call
|
60
|
+
# is not the value-returning expression. See
|
61
|
+
# https://bugs.ruby-lang.org/issues/18396.
|
62
|
+
# - Parentheses are required in anonymous arguments, keyword arguments
|
63
|
+
# and block passing in Ruby 3.2.
|
51
64
|
#
|
52
65
|
# @example EnforcedStyle: require_parentheses (default)
|
53
66
|
#
|
@@ -65,7 +78,7 @@ module RuboCop
|
|
65
78
|
# # Setter methods don't need parens
|
66
79
|
# foo.bar = baz
|
67
80
|
#
|
68
|
-
# # okay with `puts` listed in `
|
81
|
+
# # okay with `puts` listed in `AllowedMethods`
|
69
82
|
# puts 'test'
|
70
83
|
#
|
71
84
|
# # okay with `^assert` listed in `AllowedPatterns`
|
@@ -80,34 +93,28 @@ module RuboCop
|
|
80
93
|
# array.delete e
|
81
94
|
#
|
82
95
|
# # bad
|
83
|
-
#
|
96
|
+
# action.enforce(strict: true)
|
84
97
|
#
|
85
98
|
# # good
|
86
|
-
#
|
99
|
+
# action.enforce strict: true
|
87
100
|
#
|
88
101
|
# # good
|
89
|
-
# #
|
90
|
-
#
|
102
|
+
# # Parentheses are allowed for code that can be ambiguous without
|
103
|
+
# # them.
|
104
|
+
# action.enforce(condition) || other_condition
|
91
105
|
#
|
92
106
|
# # good
|
93
|
-
# #
|
107
|
+
# # Parentheses are allowed for calls that won't produce valid Ruby
|
108
|
+
# # without them.
|
94
109
|
# yield path, File.basename(path)
|
95
110
|
#
|
96
111
|
# # good
|
97
|
-
# #
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
# # good
|
105
|
-
# # Operators methods calls with parens
|
106
|
-
# array&.[](index)
|
107
|
-
#
|
108
|
-
# # good
|
109
|
-
# # Operators methods without parens, if you prefer
|
110
|
-
# array.[] index
|
112
|
+
# # Omitting the parentheses in Ruby 3.1 hash omission syntax can lead
|
113
|
+
# # to ambiguous code. We allow them in conditionals and non-last
|
114
|
+
# # expressions. See https://bugs.ruby-lang.org/issues/18396
|
115
|
+
# if meets(criteria:, action:)
|
116
|
+
# safe_action(action) || dangerous_action(action)
|
117
|
+
# end
|
111
118
|
#
|
112
119
|
# @example IgnoreMacros: true (default)
|
113
120
|
#
|
@@ -197,7 +204,7 @@ module RuboCop
|
|
197
204
|
require_relative 'method_call_with_args_parentheses/require_parentheses'
|
198
205
|
|
199
206
|
include ConfigurableEnforcedStyle
|
200
|
-
include
|
207
|
+
include AllowedMethods
|
201
208
|
include AllowedPattern
|
202
209
|
include RequireParentheses
|
203
210
|
include OmitParentheses
|
@@ -211,28 +218,26 @@ module RuboCop
|
|
211
218
|
send(style, node) # call require_parentheses or omit_parentheses
|
212
219
|
end
|
213
220
|
alias on_csend on_send
|
214
|
-
alias on_super on_send
|
215
221
|
alias on_yield on_send
|
216
222
|
|
217
223
|
private
|
218
224
|
|
219
225
|
def args_begin(node)
|
220
226
|
loc = node.loc
|
221
|
-
selector =
|
222
|
-
node.super_type? || node.yield_type? ? loc.keyword : loc.selector
|
227
|
+
selector = node.yield_type? ? loc.keyword : loc.selector
|
223
228
|
|
224
229
|
resize_by = args_parenthesized?(node) ? 2 : 1
|
225
230
|
selector.end.resize(resize_by)
|
226
231
|
end
|
227
232
|
|
228
233
|
def args_end(node)
|
229
|
-
node.
|
234
|
+
node.source_range.end
|
230
235
|
end
|
231
236
|
|
232
237
|
def args_parenthesized?(node)
|
233
238
|
return false unless node.arguments.one?
|
234
239
|
|
235
|
-
first_node = node.
|
240
|
+
first_node = node.first_argument
|
236
241
|
first_node.begin_type? && first_node.parenthesized_call?
|
237
242
|
end
|
238
243
|
end
|