rubocop 1.72.1 → 1.81.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -18
- data/config/default.yml +240 -65
- data/config/internal_affairs.yml +20 -0
- data/config/obsoletion.yml +8 -3
- data/exe/rubocop +1 -8
- data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
- data/lib/rubocop/cli.rb +19 -4
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +8 -40
- data/lib/rubocop/config_loader_resolver.rb +9 -7
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
- data/lib/rubocop/config_obsoletion.rb +46 -2
- data/lib/rubocop/config_store.rb +5 -0
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/autocorrect_logic.rb +22 -14
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -2
- data/lib/rubocop/cop/gemspec/attribute_assignment.rb +91 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +37 -15
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/require_mfa.rb +15 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +4 -4
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +6 -2
- data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +36 -1
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +32 -14
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +34 -4
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -7
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
- data/lib/rubocop/cop/layout/line_length.rb +43 -10
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +9 -5
- data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +11 -5
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
- data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -38
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +12 -3
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
- data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +5 -2
- data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
- data/lib/rubocop/cop/lint/empty_interpolation.rb +14 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
- data/lib/rubocop/cop/lint/float_comparison.rb +32 -10
- data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
- data/lib/rubocop/cop/lint/literal_as_condition.rb +124 -10
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +17 -8
- data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +43 -13
- data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
- data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +4 -4
- data/lib/rubocop/cop/lint/self_assignment.rb +31 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +30 -4
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -0
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +9 -12
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
- data/lib/rubocop/cop/lint/useless_or.rb +98 -0
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +3 -3
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
- data/lib/rubocop/cop/lint/void.rb +16 -2
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -3
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
- data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
- data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -2
- data/lib/rubocop/cop/mixin/gemspec_help.rb +22 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
- data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
- data/lib/rubocop/cop/mixin/line_length_help.rb +24 -8
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +12 -0
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +18 -2
- data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +2 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +187 -15
- data/lib/rubocop/cop/naming/predicate_method.rb +319 -0
- data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +4 -4
- data/lib/rubocop/cop/naming/variable_name.rb +51 -6
- data/lib/rubocop/cop/registry.rb +9 -6
- data/lib/rubocop/cop/security/eval.rb +2 -1
- data/lib/rubocop/cop/security/json_load.rb +33 -11
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
- data/lib/rubocop/cop/style/arguments_forwarding.rb +21 -24
- data/lib/rubocop/cop/style/array_intersect.rb +113 -38
- data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
- data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +3 -2
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +48 -10
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +1 -0
- data/lib/rubocop/cop/style/collection_querying.rb +167 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +12 -5
- data/lib/rubocop/cop/style/comparable_between.rb +78 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +26 -8
- data/lib/rubocop/cop/style/constant_visibility.rb +14 -9
- data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
- data/lib/rubocop/cop/style/dig_chain.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +3 -3
- data/lib/rubocop/cop/style/empty_literal.rb +4 -0
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
- data/lib/rubocop/cop/style/endless_method.rb +176 -18
- data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
- data/lib/rubocop/cop/style/explicit_block_argument.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +5 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +32 -6
- data/lib/rubocop/cop/style/float_division.rb +15 -1
- data/lib/rubocop/cop/style/for.rb +1 -0
- data/lib/rubocop/cop/style/format_string_token.rb +38 -11
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
- data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
- data/lib/rubocop/cop/style/guard_clause.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +16 -8
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
- 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 +3 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
- data/lib/rubocop/cop/style/if_unless_modifier.rb +35 -8
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +10 -6
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
- data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
- data/lib/rubocop/cop/style/it_assignment.rb +69 -12
- data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/lambda_call.rb +7 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
- data/lib/rubocop/cop/style/map_into_array.rb +4 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +12 -3
- data/lib/rubocop/cop/style/map_to_set.rb +1 -3
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +9 -8
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
- data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
- data/lib/rubocop/cop/style/next.rb +44 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
- data/lib/rubocop/cop/style/object_then.rb +1 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
- data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/raise_args.rb +8 -8
- data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +35 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
- data/lib/rubocop/cop/style/redundant_format.rb +79 -18
- data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +12 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -4
- data/lib/rubocop/cop/style/redundant_parentheses.rb +73 -18
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
- data/lib/rubocop/cop/style/redundant_self.rb +9 -5
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
- data/lib/rubocop/cop/style/return_nil.rb +2 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +61 -14
- data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
- data/lib/rubocop/cop/style/semicolon.rb +23 -7
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +10 -7
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +75 -101
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +18 -15
- data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
- data/lib/rubocop/cop/style/super_arguments.rb +1 -2
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +52 -1
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +10 -9
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +15 -2
- data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +3 -8
- data/lib/rubocop/cop/variable_force.rb +26 -9
- data/lib/rubocop/cops_documentation_generator.rb +23 -7
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/ext/regexp_node.rb +0 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +19 -5
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/pacman_formatter.rb +2 -1
- data/lib/rubocop/lsp/diagnostic.rb +25 -24
- data/lib/rubocop/lsp/routes.rb +65 -9
- data/lib/rubocop/lsp/runtime.rb +5 -5
- data/lib/rubocop/lsp/server.rb +2 -2
- data/lib/rubocop/lsp/stdin_runner.rb +3 -17
- data/lib/rubocop/magic_comment.rb +8 -0
- data/lib/rubocop/pending_cops_reporter.rb +56 -0
- data/lib/rubocop/plugin/configuration_integrator.rb +2 -0
- data/lib/rubocop/plugin/load_error.rb +1 -1
- data/lib/rubocop/plugin.rb +9 -2
- data/lib/rubocop/result_cache.rb +14 -12
- data/lib/rubocop/rspec/cop_helper.rb +6 -1
- data/lib/rubocop/rspec/expect_offense.rb +9 -3
- data/lib/rubocop/rspec/shared_contexts.rb +34 -0
- data/lib/rubocop/rspec/support.rb +3 -0
- data/lib/rubocop/runner.rb +10 -4
- data/lib/rubocop/server/cache.rb +17 -12
- data/lib/rubocop/server/client_command/base.rb +10 -0
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/server/client_command/start.rb +11 -1
- data/lib/rubocop/target_finder.rb +13 -9
- data/lib/rubocop/target_ruby.rb +11 -2
- data/lib/rubocop/version.rb +14 -7
- data/lib/rubocop.rb +17 -2
- data/lib/ruby_lsp/rubocop/addon.rb +25 -10
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +57 -5
- metadata +24 -8
- data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Style
|
|
6
|
+
# Checks for blocks with one argument where `it` block parameter can be used.
|
|
7
|
+
#
|
|
8
|
+
# It provides four `EnforcedStyle` options:
|
|
9
|
+
#
|
|
10
|
+
# 1. `allow_single_line` (default) ... Always uses the `it` block parameter in a single line.
|
|
11
|
+
# 2. `only_numbered_parameters` ... Detects only numbered block parameters.
|
|
12
|
+
# 3. `always` ... Always uses the `it` block parameter.
|
|
13
|
+
# 4. `disallow` ... Disallows the `it` block parameter.
|
|
14
|
+
#
|
|
15
|
+
# A single numbered parameter is detected when `allow_single_line`,
|
|
16
|
+
# `only_numbered_parameters`, or `always`.
|
|
17
|
+
#
|
|
18
|
+
# @example EnforcedStyle: allow_single_line (default)
|
|
19
|
+
# # bad
|
|
20
|
+
# block do
|
|
21
|
+
# do_something(it)
|
|
22
|
+
# end
|
|
23
|
+
# block { do_something(_1) }
|
|
24
|
+
#
|
|
25
|
+
# # good
|
|
26
|
+
# block { do_something(it) }
|
|
27
|
+
# block { |named_param| do_something(named_param) }
|
|
28
|
+
#
|
|
29
|
+
# @example EnforcedStyle: only_numbered_parameters
|
|
30
|
+
# # bad
|
|
31
|
+
# block { do_something(_1) }
|
|
32
|
+
#
|
|
33
|
+
# # good
|
|
34
|
+
# block { do_something(it) }
|
|
35
|
+
# block { |named_param| do_something(named_param) }
|
|
36
|
+
#
|
|
37
|
+
# @example EnforcedStyle: always
|
|
38
|
+
# # bad
|
|
39
|
+
# block { do_something(_1) }
|
|
40
|
+
# block { |named_param| do_something(named_param) }
|
|
41
|
+
#
|
|
42
|
+
# # good
|
|
43
|
+
# block { do_something(it) }
|
|
44
|
+
#
|
|
45
|
+
# @example EnforcedStyle: disallow
|
|
46
|
+
# # bad
|
|
47
|
+
# block { do_something(it) }
|
|
48
|
+
#
|
|
49
|
+
# # good
|
|
50
|
+
# block { do_something(_1) }
|
|
51
|
+
# block { |named_param| do_something(named_param) }
|
|
52
|
+
#
|
|
53
|
+
class ItBlockParameter < Base
|
|
54
|
+
include ConfigurableEnforcedStyle
|
|
55
|
+
extend TargetRubyVersion
|
|
56
|
+
extend AutoCorrector
|
|
57
|
+
|
|
58
|
+
MSG_USE_IT_PARAMETER = 'Use `it` block parameter.'
|
|
59
|
+
MSG_AVOID_IT_PARAMETER = 'Avoid using `it` block parameter.'
|
|
60
|
+
MSG_AVOID_IT_PARAMETER_MULTILINE = 'Avoid using `it` block parameter for multi-line blocks.'
|
|
61
|
+
|
|
62
|
+
minimum_target_ruby_version 3.4
|
|
63
|
+
|
|
64
|
+
def on_block(node)
|
|
65
|
+
return unless style == :always
|
|
66
|
+
return unless node.arguments.one?
|
|
67
|
+
|
|
68
|
+
# `restarg`, `kwrestarg`, `blockarg` nodes can return early.
|
|
69
|
+
return unless node.first_argument.arg_type?
|
|
70
|
+
|
|
71
|
+
variables = find_block_variables(node, node.first_argument.source)
|
|
72
|
+
|
|
73
|
+
variables.each do |variable|
|
|
74
|
+
add_offense(variable, message: MSG_USE_IT_PARAMETER) do |corrector|
|
|
75
|
+
corrector.remove(node.arguments)
|
|
76
|
+
corrector.replace(variable, 'it')
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def on_numblock(node)
|
|
82
|
+
return if style == :disallow
|
|
83
|
+
return unless node.children[1] == 1
|
|
84
|
+
|
|
85
|
+
variables = find_block_variables(node, '_1')
|
|
86
|
+
|
|
87
|
+
variables.each do |variable|
|
|
88
|
+
add_offense(variable, message: MSG_USE_IT_PARAMETER) do |corrector|
|
|
89
|
+
corrector.replace(variable, 'it')
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def on_itblock(node)
|
|
95
|
+
case style
|
|
96
|
+
when :allow_single_line
|
|
97
|
+
return if node.single_line?
|
|
98
|
+
|
|
99
|
+
add_offense(node, message: MSG_AVOID_IT_PARAMETER_MULTILINE)
|
|
100
|
+
when :disallow
|
|
101
|
+
variables = find_block_variables(node, 'it')
|
|
102
|
+
|
|
103
|
+
variables.each do |variable|
|
|
104
|
+
add_offense(variable, message: MSG_AVOID_IT_PARAMETER)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def find_block_variables(node, block_argument_name)
|
|
112
|
+
return [] unless node.body
|
|
113
|
+
|
|
114
|
+
node.body.each_descendant(:lvar).select do |descendant|
|
|
115
|
+
descendant.source == block_argument_name
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -42,19 +42,25 @@ module RuboCop
|
|
|
42
42
|
return if kwarg_nodes.empty?
|
|
43
43
|
|
|
44
44
|
add_offense(node) do |corrector|
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
defining_node = node.each_ancestor(:any_def, :block).first
|
|
46
|
+
next if processed_source.contains_comment?(arguments_range(defining_node))
|
|
47
|
+
next unless node.parent.find(&:kwoptarg_type?) == node
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
|
|
50
|
-
|
|
51
|
-
remove_kwargs(kwarg_nodes, corrector)
|
|
52
|
-
end
|
|
49
|
+
autocorrect(corrector, node, defining_node, kwarg_nodes)
|
|
53
50
|
end
|
|
54
51
|
end
|
|
55
52
|
|
|
56
53
|
private
|
|
57
54
|
|
|
55
|
+
def autocorrect(corrector, node, defining_node, kwarg_nodes)
|
|
56
|
+
corrector.insert_before(node, "#{kwarg_nodes.map(&:source).join(', ')}, ")
|
|
57
|
+
|
|
58
|
+
arguments = defining_node.arguments
|
|
59
|
+
append_newline_to_last_kwoptarg(arguments, corrector) unless parentheses?(arguments)
|
|
60
|
+
|
|
61
|
+
remove_kwargs(kwarg_nodes, corrector)
|
|
62
|
+
end
|
|
63
|
+
|
|
58
64
|
def append_newline_to_last_kwoptarg(arguments, corrector)
|
|
59
65
|
last_argument = arguments.last
|
|
60
66
|
return if last_argument.type?(:kwrestarg, :blockarg)
|
|
@@ -54,9 +54,14 @@ module RuboCop
|
|
|
54
54
|
|
|
55
55
|
def prefer(node)
|
|
56
56
|
receiver = node.receiver.source
|
|
57
|
-
arguments = node.arguments.map(&:source).join(', ')
|
|
58
57
|
dot = node.loc.dot.source
|
|
59
|
-
|
|
58
|
+
call_arguments = if node.arguments.empty?
|
|
59
|
+
''
|
|
60
|
+
else
|
|
61
|
+
arguments = node.arguments.map(&:source).join(', ')
|
|
62
|
+
"(#{arguments})"
|
|
63
|
+
end
|
|
64
|
+
method = explicit_style? ? "call#{call_arguments}" : "(#{arguments})"
|
|
60
65
|
|
|
61
66
|
"#{receiver}#{dot}#{method}"
|
|
62
67
|
end
|
|
@@ -36,7 +36,7 @@ module RuboCop
|
|
|
36
36
|
include RangeHelp
|
|
37
37
|
extend AutoCorrector
|
|
38
38
|
|
|
39
|
-
MSG = 'Use `\\` instead of
|
|
39
|
+
MSG = 'Use `\\` instead of `%<operator>s` to concatenate multiline strings.'
|
|
40
40
|
CONCAT_TOKEN_TYPES = %i[tPLUS tLSHFT].freeze
|
|
41
41
|
SIMPLE_STRING_TOKEN_TYPE = :tSTRING
|
|
42
42
|
COMPLEX_STRING_BEGIN_TOKEN = :tSTRING_BEG
|
|
@@ -61,14 +61,20 @@ module RuboCop
|
|
|
61
61
|
successor = tokens[index + 2]
|
|
62
62
|
|
|
63
63
|
return unless eligible_token_set?(predecessor, operator, successor)
|
|
64
|
-
|
|
65
64
|
return if same_line?(operator, successor)
|
|
66
65
|
|
|
67
66
|
next_successor = token_after_last_string(successor, index)
|
|
68
|
-
|
|
69
67
|
return unless eligible_next_successor?(next_successor)
|
|
70
68
|
|
|
71
|
-
|
|
69
|
+
register_offense(operator)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def register_offense(operator)
|
|
73
|
+
message = format(MSG, operator: operator.text)
|
|
74
|
+
|
|
75
|
+
add_offense(operator.pos, message: message) do |corrector|
|
|
76
|
+
autocorrect(corrector, operator.pos)
|
|
77
|
+
end
|
|
72
78
|
end
|
|
73
79
|
|
|
74
80
|
def autocorrect(corrector, operator_range)
|
|
@@ -127,6 +127,7 @@ module RuboCop
|
|
|
127
127
|
end
|
|
128
128
|
|
|
129
129
|
alias on_numblock on_block
|
|
130
|
+
alias on_itblock on_block
|
|
130
131
|
|
|
131
132
|
private
|
|
132
133
|
|
|
@@ -211,9 +212,11 @@ module RuboCop
|
|
|
211
212
|
end
|
|
212
213
|
|
|
213
214
|
def correct_push_node(corrector, push_node)
|
|
215
|
+
arg_node = push_node.first_argument
|
|
214
216
|
range = push_node.source_range
|
|
215
|
-
arg_range =
|
|
217
|
+
arg_range = arg_node.source_range
|
|
216
218
|
|
|
219
|
+
corrector.wrap(arg_node, '{ ', ' }') if arg_node.hash_type? && !arg_node.braces?
|
|
217
220
|
corrector.remove(range_between(range.begin_pos, arg_range.begin_pos))
|
|
218
221
|
corrector.remove(range_between(arg_range.end_pos, range.end_pos))
|
|
219
222
|
end
|
|
@@ -45,18 +45,21 @@ module RuboCop
|
|
|
45
45
|
}
|
|
46
46
|
PATTERN
|
|
47
47
|
|
|
48
|
+
# @!method destructuring_argument(node)
|
|
49
|
+
def_node_matcher :destructuring_argument, <<~PATTERN
|
|
50
|
+
(args $(mlhs (arg _)+))
|
|
51
|
+
PATTERN
|
|
52
|
+
|
|
48
53
|
def self.autocorrect_incompatible_with
|
|
49
54
|
[Layout::SingleLineBlockChain]
|
|
50
55
|
end
|
|
51
56
|
|
|
52
57
|
def on_send(node)
|
|
53
58
|
return unless (to_h_node, map_node = map_to_h(node))
|
|
59
|
+
return if to_h_node.block_literal?
|
|
54
60
|
|
|
55
61
|
message = format(MSG, method: map_node.loc.selector.source, dot: to_h_node.loc.dot.source)
|
|
56
62
|
add_offense(map_node.loc.selector, message: message) do |corrector|
|
|
57
|
-
# If the `to_h` call already has a block, do not autocorrect.
|
|
58
|
-
next if to_h_node.block_literal?
|
|
59
|
-
|
|
60
63
|
autocorrect(corrector, to_h_node, map_node)
|
|
61
64
|
end
|
|
62
65
|
end
|
|
@@ -73,6 +76,12 @@ module RuboCop
|
|
|
73
76
|
corrector.replace(map_dot, to_h.loc.dot.source)
|
|
74
77
|
end
|
|
75
78
|
corrector.replace(map.loc.selector, 'to_h')
|
|
79
|
+
|
|
80
|
+
return unless map.parent.block_type?
|
|
81
|
+
|
|
82
|
+
if (argument = destructuring_argument(map.parent.arguments))
|
|
83
|
+
corrector.replace(argument, argument.source[1..-2])
|
|
84
|
+
end
|
|
76
85
|
end
|
|
77
86
|
# rubocop:enable Metrics/AbcSize
|
|
78
87
|
end
|
|
@@ -40,12 +40,10 @@ module RuboCop
|
|
|
40
40
|
|
|
41
41
|
def on_send(node)
|
|
42
42
|
return unless (to_set_node, map_node = map_to_set?(node))
|
|
43
|
+
return if to_set_node.block_literal?
|
|
43
44
|
|
|
44
45
|
message = format(MSG, method: map_node.loc.selector.source)
|
|
45
46
|
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
47
|
autocorrect(corrector, to_set_node, map_node)
|
|
50
48
|
end
|
|
51
49
|
end
|
|
@@ -49,7 +49,7 @@ module RuboCop
|
|
|
49
49
|
|
|
50
50
|
def inside_endless_method_def?(node)
|
|
51
51
|
# parens are required around arguments inside an endless method
|
|
52
|
-
node.each_ancestor(:
|
|
52
|
+
node.each_ancestor(:any_def).any?(&:endless?) && node.arguments.any?
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def require_parentheses_for_hash_value_omission?(node) # rubocop:disable Metrics/PerceivedComplexity
|
|
@@ -108,7 +108,7 @@ module RuboCop
|
|
|
108
108
|
end
|
|
109
109
|
|
|
110
110
|
def call_in_literals?(node)
|
|
111
|
-
parent = node.parent&.
|
|
111
|
+
parent = node.parent&.any_block_type? ? node.parent.parent : node.parent
|
|
112
112
|
return false unless parent
|
|
113
113
|
|
|
114
114
|
parent.type?(:pair, :array, :range) ||
|
|
@@ -117,7 +117,7 @@ module RuboCop
|
|
|
117
117
|
end
|
|
118
118
|
|
|
119
119
|
def call_in_logical_operators?(node)
|
|
120
|
-
parent = node.parent&.
|
|
120
|
+
parent = node.parent&.any_block_type? ? node.parent.parent : node.parent
|
|
121
121
|
return false unless parent
|
|
122
122
|
|
|
123
123
|
logical_operator?(parent) ||
|
|
@@ -153,7 +153,7 @@ module RuboCop
|
|
|
153
153
|
end
|
|
154
154
|
|
|
155
155
|
def call_in_argument_with_block?(node)
|
|
156
|
-
parent = node.parent&.
|
|
156
|
+
parent = node.parent&.any_block_type? && node.parent.parent
|
|
157
157
|
return false unless parent
|
|
158
158
|
|
|
159
159
|
parent.type?(:call, :super, :yield)
|
|
@@ -167,7 +167,7 @@ module RuboCop
|
|
|
167
167
|
def call_in_match_pattern?(node)
|
|
168
168
|
return false unless (parent = node.parent)
|
|
169
169
|
|
|
170
|
-
parent.
|
|
170
|
+
parent.any_match_pattern_type?
|
|
171
171
|
end
|
|
172
172
|
|
|
173
173
|
def hash_literal_in_arguments?(node)
|
|
@@ -222,8 +222,9 @@ module RuboCop
|
|
|
222
222
|
end
|
|
223
223
|
|
|
224
224
|
def unary_literal?(node)
|
|
225
|
-
|
|
226
|
-
|
|
225
|
+
return true if node.numeric_type? && node.sign?
|
|
226
|
+
|
|
227
|
+
node.parent&.send_type? && node.parent.unary_operation?
|
|
227
228
|
end
|
|
228
229
|
|
|
229
230
|
def assigned_before?(node, target)
|
|
@@ -248,7 +249,7 @@ module RuboCop
|
|
|
248
249
|
return false unless (last_argument = node.last_argument)
|
|
249
250
|
return true if last_argument.forwarded_restarg_type?
|
|
250
251
|
|
|
251
|
-
last_argument.hash_type? && last_argument.children.
|
|
252
|
+
last_argument.hash_type? && last_argument.children.any?(&:forwarded_kwrestarg_type?)
|
|
252
253
|
end
|
|
253
254
|
end
|
|
254
255
|
# rubocop:enable Metrics/ModuleLength, Metrics/CyclomaticComplexity
|
|
@@ -132,6 +132,22 @@ module RuboCop
|
|
|
132
132
|
# bar :baz
|
|
133
133
|
# end
|
|
134
134
|
#
|
|
135
|
+
# @example AllowedMethods: ["puts", "print"]
|
|
136
|
+
#
|
|
137
|
+
# # good
|
|
138
|
+
# puts "Hello world"
|
|
139
|
+
# print "Hello world"
|
|
140
|
+
# # still enforces parentheses on other methods
|
|
141
|
+
# array.delete(e)
|
|
142
|
+
#
|
|
143
|
+
# @example AllowedPatterns: ["^assert"]
|
|
144
|
+
#
|
|
145
|
+
# # good
|
|
146
|
+
# assert_equal 'test', x
|
|
147
|
+
# assert_match(/foo/, bar)
|
|
148
|
+
# # still enforces parentheses on other methods
|
|
149
|
+
# array.delete(e)
|
|
150
|
+
#
|
|
135
151
|
# @example AllowParenthesesInMultilineCall: false (default)
|
|
136
152
|
#
|
|
137
153
|
# # bad
|
|
@@ -36,12 +36,13 @@ module RuboCop
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
alias on_numblock on_block
|
|
39
|
+
alias on_itblock on_block
|
|
39
40
|
|
|
40
41
|
def on_send(node)
|
|
41
42
|
return if ignored_node?(node)
|
|
42
43
|
|
|
43
44
|
return unless (receiver = node.receiver)
|
|
44
|
-
return unless receiver.any_block_type? && receiver.
|
|
45
|
+
return unless receiver.any_block_type? && receiver.keywords?
|
|
45
46
|
|
|
46
47
|
range = range_between(receiver.loc.end.begin_pos, node.source_range.end_pos)
|
|
47
48
|
|
|
@@ -39,13 +39,21 @@ module RuboCop
|
|
|
39
39
|
include RangeHelp
|
|
40
40
|
|
|
41
41
|
MSG = 'Use `%<prefer>s` instead.'
|
|
42
|
-
|
|
42
|
+
GREATER_OPERATORS = %i[> >=].freeze
|
|
43
43
|
LESS_OPERATORS = %i[< <=].freeze
|
|
44
|
-
COMPARISON_OPERATORS =
|
|
44
|
+
COMPARISON_OPERATORS = (GREATER_OPERATORS + LESS_OPERATORS).to_set.freeze
|
|
45
|
+
|
|
46
|
+
# @!method comparison_condition(node, name)
|
|
47
|
+
def_node_matcher :comparison_condition, <<~PATTERN
|
|
48
|
+
{
|
|
49
|
+
(send $_lhs $COMPARISON_OPERATORS $_rhs)
|
|
50
|
+
(begin (send $_lhs $COMPARISON_OPERATORS $_rhs))
|
|
51
|
+
}
|
|
52
|
+
PATTERN
|
|
45
53
|
|
|
46
54
|
def on_if(node)
|
|
47
|
-
lhs, operator, rhs =
|
|
48
|
-
return unless
|
|
55
|
+
lhs, operator, rhs = comparison_condition(node.condition)
|
|
56
|
+
return unless operator
|
|
49
57
|
|
|
50
58
|
if_branch = node.if_branch
|
|
51
59
|
else_branch = node.else_branch
|
|
@@ -63,7 +71,7 @@ module RuboCop
|
|
|
63
71
|
|
|
64
72
|
def preferred_method(operator, lhs, rhs, if_branch, else_branch)
|
|
65
73
|
if lhs == if_branch && rhs == else_branch
|
|
66
|
-
|
|
74
|
+
GREATER_OPERATORS.include?(operator) ? 'max' : 'min'
|
|
67
75
|
elsif lhs == else_branch && rhs == if_branch
|
|
68
76
|
LESS_OPERATORS.include?(operator) ? 'max' : 'min'
|
|
69
77
|
end
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
|
28
28
|
MSG = 'Avoid multi-line chains of blocks.'
|
|
29
29
|
|
|
30
30
|
def on_block(node)
|
|
31
|
-
node.send_node.each_node(:
|
|
31
|
+
node.send_node.each_node(:call) do |send_node|
|
|
32
32
|
receiver = send_node.receiver
|
|
33
33
|
|
|
34
34
|
next unless receiver&.any_block_type? && receiver.multiline?
|
|
@@ -44,6 +44,7 @@ module RuboCop
|
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
alias on_numblock on_block
|
|
47
|
+
alias on_itblock on_block
|
|
47
48
|
end
|
|
48
49
|
end
|
|
49
50
|
end
|
|
@@ -23,11 +23,13 @@ module RuboCop
|
|
|
23
23
|
'clause in a multiline statement.'
|
|
24
24
|
|
|
25
25
|
def on_if(node)
|
|
26
|
+
return if part_of_ignored_node?(node)
|
|
26
27
|
return unless node.modifier_form? && node.body.multiline?
|
|
27
28
|
|
|
28
29
|
add_offense(node, message: format(MSG, keyword: node.keyword)) do |corrector|
|
|
29
30
|
corrector.replace(node, to_normal_if(node))
|
|
30
31
|
end
|
|
32
|
+
ignore_node(node)
|
|
31
33
|
end
|
|
32
34
|
|
|
33
35
|
private
|
|
@@ -50,7 +50,7 @@ module RuboCop
|
|
|
50
50
|
corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
-
arguments_range = arguments_range(node)
|
|
53
|
+
arguments_range = range_with_surrounding_space(arguments_range(node), side: :left)
|
|
54
54
|
# If the method name isn't on the same line as def, move it directly after def
|
|
55
55
|
if arguments_range.first_line != opening_line(node)
|
|
56
56
|
corrector.remove(node.loc.name)
|
|
@@ -66,14 +66,6 @@ module RuboCop
|
|
|
66
66
|
processed_source[arguments.last_line - 1].strip
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
def arguments_range(node)
|
|
70
|
-
range = range_between(
|
|
71
|
-
node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
range_with_surrounding_space(range, side: :left)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
69
|
def opening_line(node)
|
|
78
70
|
node.first_line
|
|
79
71
|
end
|
|
@@ -46,6 +46,38 @@ module RuboCop
|
|
|
46
46
|
# next unless a == 1
|
|
47
47
|
# puts a
|
|
48
48
|
# end
|
|
49
|
+
#
|
|
50
|
+
# @example AllowConsecutiveConditionals: false (default)
|
|
51
|
+
# # bad
|
|
52
|
+
# [1, 2].each do |a|
|
|
53
|
+
# if a == 1
|
|
54
|
+
# puts a
|
|
55
|
+
# end
|
|
56
|
+
# if a == 2
|
|
57
|
+
# puts a
|
|
58
|
+
# end
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# # good
|
|
62
|
+
# [1, 2].each do |a|
|
|
63
|
+
# if a == 1
|
|
64
|
+
# puts a
|
|
65
|
+
# end
|
|
66
|
+
# next unless a == 2
|
|
67
|
+
# puts a
|
|
68
|
+
# end
|
|
69
|
+
#
|
|
70
|
+
# @example AllowConsecutiveConditionals: true
|
|
71
|
+
# # good
|
|
72
|
+
# [1, 2].each do |a|
|
|
73
|
+
# if a == 1
|
|
74
|
+
# puts a
|
|
75
|
+
# end
|
|
76
|
+
# if a == 2
|
|
77
|
+
# puts a
|
|
78
|
+
# end
|
|
79
|
+
# end
|
|
80
|
+
#
|
|
49
81
|
class Next < Base
|
|
50
82
|
include ConfigurableEnforcedStyle
|
|
51
83
|
include MinBodyLength
|
|
@@ -72,6 +104,7 @@ module RuboCop
|
|
|
72
104
|
end
|
|
73
105
|
|
|
74
106
|
alias on_numblock on_block
|
|
107
|
+
alias on_itblock on_block
|
|
75
108
|
|
|
76
109
|
def on_while(node)
|
|
77
110
|
check(node)
|
|
@@ -86,6 +119,9 @@ module RuboCop
|
|
|
86
119
|
|
|
87
120
|
offending_node = offense_node(node.body)
|
|
88
121
|
|
|
122
|
+
return if allowed_consecutive_conditionals? &&
|
|
123
|
+
consecutive_conditionals?(offending_node)
|
|
124
|
+
|
|
89
125
|
add_offense(offense_location(offending_node)) do |corrector|
|
|
90
126
|
if offending_node.modifier_form?
|
|
91
127
|
autocorrect_modifier(corrector, offending_node)
|
|
@@ -227,6 +263,14 @@ module RuboCop
|
|
|
227
263
|
|
|
228
264
|
corrector.remove_leading(buffer.line_range(lineno), adjustment) if adjustment.positive?
|
|
229
265
|
end
|
|
266
|
+
|
|
267
|
+
def consecutive_conditionals?(if_node)
|
|
268
|
+
if_node.parent&.begin_type? && if_node.left_sibling&.if_type?
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def allowed_consecutive_conditionals?
|
|
272
|
+
cop_config.fetch('AllowConsecutiveConditionals', false)
|
|
273
|
+
end
|
|
230
274
|
end
|
|
231
275
|
end
|
|
232
276
|
end
|
|
@@ -43,24 +43,26 @@ module RuboCop
|
|
|
43
43
|
# @!method nil_check?(node)
|
|
44
44
|
def_node_matcher :nil_check?, '(send _ :nil?)'
|
|
45
45
|
|
|
46
|
+
# rubocop:disable Metrics/AbcSize
|
|
46
47
|
def on_send(node)
|
|
47
48
|
return unless node.receiver
|
|
48
49
|
|
|
49
50
|
style_check?(node) do
|
|
50
51
|
add_offense(node.loc.selector) do |corrector|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
if prefer_comparison?
|
|
53
|
+
range = node.loc.dot.join(node.loc.selector.end)
|
|
54
|
+
corrector.replace(range, ' == nil')
|
|
55
|
+
else
|
|
56
|
+
range = node.receiver.source_range.end.join(node.source_range.end)
|
|
57
|
+
corrector.replace(range, '.nil?')
|
|
58
|
+
end
|
|
58
59
|
|
|
59
60
|
parent = node.parent
|
|
60
61
|
corrector.wrap(node, '(', ')') if parent.respond_to?(:method?) && parent.method?(:!)
|
|
61
62
|
end
|
|
62
63
|
end
|
|
63
64
|
end
|
|
65
|
+
# rubocop:enable Metrics/AbcSize
|
|
64
66
|
|
|
65
67
|
private
|
|
66
68
|
|
|
@@ -55,19 +55,21 @@ module RuboCop
|
|
|
55
55
|
include OnNormalIfUnless
|
|
56
56
|
extend AutoCorrector
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
MSG_SUFFIX = 'over single-line `%<keyword>s/then/else/end` constructs.'
|
|
59
|
+
MSG_TERNARY = "Favor the ternary operator (`?:`) #{MSG_SUFFIX}"
|
|
60
|
+
MSG_MULTILINE = "Favor multi-line `%<keyword>s` #{MSG_SUFFIX}"
|
|
60
61
|
|
|
61
62
|
def on_normal_if_unless(node)
|
|
62
63
|
return unless node.single_line?
|
|
63
64
|
return unless node.else_branch
|
|
64
65
|
return if node.elsif? || node.if_branch&.begin_type?
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
multiline = multiline?(node)
|
|
68
|
+
|
|
69
|
+
add_offense(node, message: message(node, multiline)) do |corrector|
|
|
68
70
|
next if part_of_ignored_node?(node)
|
|
69
71
|
|
|
70
|
-
autocorrect(corrector, node)
|
|
72
|
+
autocorrect(corrector, node, multiline)
|
|
71
73
|
|
|
72
74
|
ignore_node(node)
|
|
73
75
|
end
|
|
@@ -75,12 +77,18 @@ module RuboCop
|
|
|
75
77
|
|
|
76
78
|
private
|
|
77
79
|
|
|
78
|
-
def
|
|
79
|
-
|
|
80
|
+
def multiline?(node)
|
|
81
|
+
always_multiline? || cannot_replace_to_ternary?(node)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def message(node, multiline)
|
|
85
|
+
template = multiline ? MSG_MULTILINE : MSG_TERNARY
|
|
86
|
+
|
|
87
|
+
format(template, keyword: node.keyword)
|
|
80
88
|
end
|
|
81
89
|
|
|
82
|
-
def autocorrect(corrector, node)
|
|
83
|
-
if
|
|
90
|
+
def autocorrect(corrector, node, multiline)
|
|
91
|
+
if multiline
|
|
84
92
|
IfThenCorrector.new(node, indentation: configured_indentation_width).call(corrector)
|
|
85
93
|
else
|
|
86
94
|
corrector.replace(node, ternary_correction(node))
|