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
|
@@ -84,7 +84,10 @@ module RuboCop
|
|
|
84
84
|
|
|
85
85
|
def autocorrect(corrector, node)
|
|
86
86
|
if (preferred_accessors = preferred_accessors(node))
|
|
87
|
-
corrector.replace(
|
|
87
|
+
corrector.replace(
|
|
88
|
+
grouped_style? ? node : range_with_trailing_argument_comment(node),
|
|
89
|
+
preferred_accessors
|
|
90
|
+
)
|
|
88
91
|
else
|
|
89
92
|
range = range_with_surrounding_space(node.source_range, side: :left)
|
|
90
93
|
corrector.remove(range)
|
|
@@ -139,12 +142,16 @@ module RuboCop
|
|
|
139
142
|
style == :separated
|
|
140
143
|
end
|
|
141
144
|
|
|
145
|
+
def groupable_sibling_accessor?(node, sibling)
|
|
146
|
+
sibling.attribute_accessor? &&
|
|
147
|
+
sibling.method?(node.method_name) &&
|
|
148
|
+
node_visibility(sibling) == node_visibility(node) &&
|
|
149
|
+
groupable_accessor?(sibling) && !previous_line_comment?(sibling)
|
|
150
|
+
end
|
|
151
|
+
|
|
142
152
|
def groupable_sibling_accessors(send_node)
|
|
143
153
|
send_node.parent.each_child_node(:send).select do |sibling|
|
|
144
|
-
|
|
145
|
-
sibling.method?(send_node.method_name) &&
|
|
146
|
-
node_visibility(sibling) == node_visibility(send_node) &&
|
|
147
|
-
groupable_accessor?(sibling) && !previous_line_comment?(sibling)
|
|
154
|
+
groupable_sibling_accessor?(send_node, sibling)
|
|
148
155
|
end
|
|
149
156
|
end
|
|
150
157
|
|
|
@@ -155,13 +162,23 @@ module RuboCop
|
|
|
155
162
|
|
|
156
163
|
def preferred_accessors(node)
|
|
157
164
|
if grouped_style?
|
|
165
|
+
return if skip_for_grouping?(node)
|
|
166
|
+
|
|
158
167
|
accessors = groupable_sibling_accessors(node)
|
|
159
|
-
|
|
168
|
+
if node.loc == accessors.first.loc || skip_for_grouping?(accessors.first)
|
|
169
|
+
group_accessors(node, accessors)
|
|
170
|
+
end
|
|
160
171
|
else
|
|
161
172
|
separate_accessors(node)
|
|
162
173
|
end
|
|
163
174
|
end
|
|
164
175
|
|
|
176
|
+
# Group after constants
|
|
177
|
+
def skip_for_grouping?(node)
|
|
178
|
+
node.right_siblings.any?(&:casgn_type?) &&
|
|
179
|
+
node.right_siblings.any? { |n| n.send_type? && groupable_sibling_accessor?(node, n) }
|
|
180
|
+
end
|
|
181
|
+
|
|
165
182
|
def group_accessors(node, accessors)
|
|
166
183
|
accessor_names = accessors.flat_map { |accessor| accessor.arguments.map(&:source) }.uniq
|
|
167
184
|
|
|
@@ -182,6 +199,15 @@ module RuboCop
|
|
|
182
199
|
end
|
|
183
200
|
end.join("\n")
|
|
184
201
|
end
|
|
202
|
+
|
|
203
|
+
def range_with_trailing_argument_comment(node)
|
|
204
|
+
comment = processed_source.ast_with_comments[node.last_argument].last
|
|
205
|
+
if comment
|
|
206
|
+
add_range(node.source_range, comment.source_range)
|
|
207
|
+
else
|
|
208
|
+
node
|
|
209
|
+
end
|
|
210
|
+
end
|
|
185
211
|
end
|
|
186
212
|
end
|
|
187
213
|
end
|
|
@@ -16,26 +16,26 @@ module RuboCop
|
|
|
16
16
|
#
|
|
17
17
|
# In Ruby 3.2, anonymous args/kwargs forwarding has been added.
|
|
18
18
|
#
|
|
19
|
-
# This cop also identifies places where
|
|
20
|
-
# replaced by
|
|
21
|
-
# by setting `UseAnonymousForwarding: false`.
|
|
19
|
+
# This cop also identifies places where `+use_args(*args)+`/`+use_kwargs(**kwargs)+` can be
|
|
20
|
+
# replaced by `+use_args(*)+`/`+use_kwargs(**)+`; if desired, this functionality can be
|
|
21
|
+
# disabled by setting `UseAnonymousForwarding: false`.
|
|
22
22
|
#
|
|
23
23
|
# And this cop has `RedundantRestArgumentNames`, `RedundantKeywordRestArgumentNames`,
|
|
24
24
|
# and `RedundantBlockArgumentNames` options. This configuration is a list of redundant names
|
|
25
25
|
# that are sufficient for anonymizing meaningless naming.
|
|
26
26
|
#
|
|
27
27
|
# Meaningless names that are commonly used can be anonymized by default:
|
|
28
|
-
# e.g.,
|
|
28
|
+
# e.g., `+*args+`, `+**options+`, `&block`, and so on.
|
|
29
29
|
#
|
|
30
30
|
# Names not on this list are likely to be meaningful and are allowed by default.
|
|
31
31
|
#
|
|
32
32
|
# This cop handles not only method forwarding but also forwarding to `super`.
|
|
33
33
|
#
|
|
34
34
|
# [NOTE]
|
|
35
|
-
#
|
|
35
|
+
# ====
|
|
36
36
|
# Because of a bug in Ruby 3.3.0, when a block is referenced inside of another block,
|
|
37
37
|
# no offense will be registered until Ruby 3.4:
|
|
38
|
-
|
|
38
|
+
#
|
|
39
39
|
# [source,ruby]
|
|
40
40
|
# ----
|
|
41
41
|
# def foo(&block)
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
|
43
43
|
# block_method { bar(&block) }
|
|
44
44
|
# end
|
|
45
45
|
# ----
|
|
46
|
-
#
|
|
46
|
+
# ====
|
|
47
47
|
#
|
|
48
48
|
# @example
|
|
49
49
|
# # bad
|
|
@@ -146,7 +146,6 @@ module RuboCop
|
|
|
146
146
|
minimum_target_ruby_version 2.7
|
|
147
147
|
|
|
148
148
|
FORWARDING_LVAR_TYPES = %i[splat kwsplat block_pass].freeze
|
|
149
|
-
ADDITIONAL_ARG_TYPES = %i[lvar arg].freeze
|
|
150
149
|
|
|
151
150
|
FORWARDING_MSG = 'Use shorthand syntax `...` for arguments forwarding.'
|
|
152
151
|
ARGS_MSG = 'Use anonymous positional arguments forwarding (`*`).'
|
|
@@ -198,9 +197,9 @@ module RuboCop
|
|
|
198
197
|
send_classifications.all? { |_, c, _, _| all_classifications.include?(c) }
|
|
199
198
|
end
|
|
200
199
|
|
|
201
|
-
# rubocop:disable Metrics/MethodLength
|
|
200
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
202
201
|
def add_forward_all_offenses(node, send_classifications, forwardable_args)
|
|
203
|
-
|
|
202
|
+
rest_arg, kwrest_arg, block_arg = *forwardable_args
|
|
204
203
|
registered_block_arg_offense = false
|
|
205
204
|
|
|
206
205
|
send_classifications.each do |send_node, c, forward_rest, forward_kwrest, forward_block_arg| # rubocop:disable Layout/LineLength
|
|
@@ -212,16 +211,20 @@ module RuboCop
|
|
|
212
211
|
registered_block_arg_offense = true
|
|
213
212
|
break
|
|
214
213
|
else
|
|
215
|
-
|
|
214
|
+
first_arg = forward_rest || forward_kwrest || forward_all_first_argument(send_node)
|
|
215
|
+
register_forward_all_offense(send_node, send_node, first_arg)
|
|
216
216
|
end
|
|
217
217
|
end
|
|
218
218
|
|
|
219
219
|
return if registered_block_arg_offense
|
|
220
220
|
|
|
221
|
-
|
|
222
|
-
|
|
221
|
+
register_forward_all_offense(node, node.arguments, rest_arg || kwrest_arg)
|
|
222
|
+
end
|
|
223
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
|
224
|
+
|
|
225
|
+
def forward_all_first_argument(node)
|
|
226
|
+
node.arguments.reverse_each.find(&:forwarded_restarg_type?)
|
|
223
227
|
end
|
|
224
|
-
# rubocop:enable Metrics/MethodLength
|
|
225
228
|
|
|
226
229
|
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
227
230
|
def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
|
|
@@ -361,18 +364,9 @@ module RuboCop
|
|
|
361
364
|
end
|
|
362
365
|
end
|
|
363
366
|
|
|
364
|
-
# rubocop:disable Metrics/AbcSize
|
|
365
367
|
def arguments_range(node, first_node)
|
|
366
|
-
|
|
367
|
-
next true if ADDITIONAL_ARG_TYPES.include?(arg.type) || arg.variable? || arg.call_type?
|
|
368
|
-
|
|
369
|
-
arg.literal? && arg.each_descendant(:kwsplat).none?
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
start_node = first_node || arguments.first
|
|
373
|
-
start_node.source_range.begin.join(arguments.last.source_range.end)
|
|
368
|
+
first_node.source_range.begin.join(node.last_argument.source_range.end)
|
|
374
369
|
end
|
|
375
|
-
# rubocop:enable Metrics/AbcSize
|
|
376
370
|
|
|
377
371
|
def allow_only_rest_arguments?
|
|
378
372
|
cop_config.fetch('AllowOnlyRestArgument', true)
|
|
@@ -479,6 +473,9 @@ module RuboCop
|
|
|
479
473
|
end
|
|
480
474
|
|
|
481
475
|
def ruby_32_only_anonymous_forwarding?
|
|
476
|
+
# A block argument and an anonymous block argument are never passed together.
|
|
477
|
+
return false if @send_node.each_ancestor(:any_block).any?
|
|
478
|
+
|
|
482
479
|
def_all_anonymous_args?(@def_node) && send_all_anonymous_args?(@send_node)
|
|
483
480
|
end
|
|
484
481
|
|
|
@@ -5,11 +5,17 @@ module RuboCop
|
|
|
5
5
|
module Style
|
|
6
6
|
# In Ruby 3.1, `Array#intersect?` has been added.
|
|
7
7
|
#
|
|
8
|
-
# This cop identifies places where
|
|
9
|
-
# can be replaced by `array1.intersect?(array2)`.
|
|
8
|
+
# This cop identifies places where:
|
|
10
9
|
#
|
|
11
|
-
#
|
|
12
|
-
# `(array1
|
|
10
|
+
# * `(array1 & array2).any?`
|
|
11
|
+
# * `(array1.intersection(array2)).any?`
|
|
12
|
+
# * `array1.any? { |elem| array2.member?(elem) }`
|
|
13
|
+
# * `(array1 & array2).count > 0`
|
|
14
|
+
# * `(array1 & array2).size > 0`
|
|
15
|
+
#
|
|
16
|
+
# can be replaced with `array1.intersect?(array2)`.
|
|
17
|
+
#
|
|
18
|
+
# `array1.intersect?(array2)` is faster and more readable.
|
|
13
19
|
#
|
|
14
20
|
# In cases like the following, compatibility is not ensured,
|
|
15
21
|
# so it will not be detected when using block argument.
|
|
@@ -20,6 +26,10 @@ module RuboCop
|
|
|
20
26
|
# [1].intersect?([1,2]) { |x| false } # => true
|
|
21
27
|
# ----
|
|
22
28
|
#
|
|
29
|
+
# NOTE: Although `Array#intersection` can take zero or multiple arguments,
|
|
30
|
+
# only cases where exactly one argument is provided can be replaced with
|
|
31
|
+
# `Array#intersect?` and are handled by this cop.
|
|
32
|
+
#
|
|
23
33
|
# @safety
|
|
24
34
|
# This cop cannot guarantee that `array1` and `array2` are
|
|
25
35
|
# actually arrays while method `intersect?` is for arrays only.
|
|
@@ -30,10 +40,32 @@ module RuboCop
|
|
|
30
40
|
# (array1 & array2).empty?
|
|
31
41
|
# (array1 & array2).none?
|
|
32
42
|
#
|
|
43
|
+
# # bad
|
|
44
|
+
# array1.intersection(array2).any?
|
|
45
|
+
# array1.intersection(array2).empty?
|
|
46
|
+
# array1.intersection(array2).none?
|
|
47
|
+
#
|
|
48
|
+
# # bad
|
|
49
|
+
# array1.any? { |elem| array2.member?(elem) }
|
|
50
|
+
# array1.none? { |elem| array2.member?(elem) }
|
|
51
|
+
#
|
|
33
52
|
# # good
|
|
34
53
|
# array1.intersect?(array2)
|
|
35
54
|
# !array1.intersect?(array2)
|
|
36
55
|
#
|
|
56
|
+
# # bad
|
|
57
|
+
# (array1 & array2).count > 0
|
|
58
|
+
# (array1 & array2).count.positive?
|
|
59
|
+
# (array1 & array2).count != 0
|
|
60
|
+
#
|
|
61
|
+
# (array1 & array2).count == 0
|
|
62
|
+
# (array1 & array2).count.zero?
|
|
63
|
+
#
|
|
64
|
+
# # good
|
|
65
|
+
# array1.intersect?(array2)
|
|
66
|
+
#
|
|
67
|
+
# !array1.intersect?(array2)
|
|
68
|
+
#
|
|
37
69
|
# @example AllCops:ActiveSupportExtensionsEnabled: false (default)
|
|
38
70
|
# # good
|
|
39
71
|
# (array1 & array2).present?
|
|
@@ -53,50 +85,96 @@ module RuboCop
|
|
|
53
85
|
|
|
54
86
|
minimum_target_ruby_version 3.1
|
|
55
87
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
88
|
+
PREDICATES = %i[any? empty? none?].to_set.freeze
|
|
89
|
+
ACTIVE_SUPPORT_PREDICATES = (PREDICATES + %i[present? blank?]).freeze
|
|
90
|
+
|
|
91
|
+
ARRAY_SIZE_METHODS = %i[count length size].to_set.freeze
|
|
92
|
+
|
|
93
|
+
# @!method bad_intersection_check?(node, predicates)
|
|
94
|
+
def_node_matcher :bad_intersection_check?, <<~PATTERN
|
|
95
|
+
$(call
|
|
96
|
+
{
|
|
97
|
+
(begin (send $_ :& $_))
|
|
98
|
+
(call $!nil? :intersection $_)
|
|
99
|
+
}
|
|
100
|
+
$%1
|
|
62
101
|
)
|
|
63
102
|
PATTERN
|
|
64
103
|
|
|
65
|
-
# @!method
|
|
66
|
-
def_node_matcher :
|
|
67
|
-
(
|
|
68
|
-
(
|
|
69
|
-
|
|
70
|
-
|
|
104
|
+
# @!method intersection_size_check?(node, predicates)
|
|
105
|
+
def_node_matcher :intersection_size_check?, <<~PATTERN
|
|
106
|
+
(call
|
|
107
|
+
$(call
|
|
108
|
+
{
|
|
109
|
+
(begin (send $_ :& $_))
|
|
110
|
+
(call $!nil? :intersection $_)
|
|
111
|
+
}
|
|
112
|
+
%ARRAY_SIZE_METHODS
|
|
113
|
+
)
|
|
114
|
+
{$:> (int 0) | $:positive? | $:!= (int 0) | $:== (int 0) | $:zero?}
|
|
71
115
|
)
|
|
72
116
|
PATTERN
|
|
73
117
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
118
|
+
# @!method any_none_block_intersection(node)
|
|
119
|
+
def_node_matcher :any_none_block_intersection, <<~PATTERN
|
|
120
|
+
{
|
|
121
|
+
(block
|
|
122
|
+
(call $_receiver ${:any? :none?})
|
|
123
|
+
(args (arg _key))
|
|
124
|
+
(send $_argument :member? (lvar _key))
|
|
125
|
+
)
|
|
126
|
+
(numblock
|
|
127
|
+
(call $_receiver ${:any? :none?}) 1
|
|
128
|
+
(send $_argument :member? (lvar :_1))
|
|
129
|
+
)
|
|
130
|
+
(itblock
|
|
131
|
+
(call $_receiver ${:any? :none?}) :it
|
|
132
|
+
(send $_argument :member? (lvar :it))
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
PATTERN
|
|
136
|
+
|
|
137
|
+
MSG = 'Use `%<replacement>s` instead of `%<existing>s`.'
|
|
138
|
+
STRAIGHT_METHODS = %i[present? any? > positive? !=].freeze
|
|
139
|
+
NEGATED_METHODS = %i[blank? empty? none? == zero?].freeze
|
|
78
140
|
RESTRICT_ON_SEND = (STRAIGHT_METHODS + NEGATED_METHODS).freeze
|
|
79
141
|
|
|
80
142
|
def on_send(node)
|
|
81
143
|
return if node.block_literal?
|
|
82
|
-
return unless (receiver, argument, method_name =
|
|
144
|
+
return unless (dot_node, receiver, argument, method_name = bad_intersection?(node))
|
|
83
145
|
|
|
84
|
-
|
|
146
|
+
dot = dot_node.loc.dot.source
|
|
147
|
+
bang = straight?(method_name) ? '' : '!'
|
|
148
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
|
85
149
|
|
|
86
|
-
|
|
87
|
-
|
|
150
|
+
register_offense(node, replacement)
|
|
151
|
+
end
|
|
152
|
+
alias on_csend on_send
|
|
88
153
|
|
|
89
|
-
|
|
90
|
-
|
|
154
|
+
def on_block(node)
|
|
155
|
+
return unless (receiver, method_name, argument = any_none_block_intersection(node))
|
|
156
|
+
|
|
157
|
+
dot = node.send_node.loc.dot.source
|
|
158
|
+
bang = method_name == :any? ? '' : '!'
|
|
159
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
|
160
|
+
|
|
161
|
+
register_offense(node, replacement)
|
|
91
162
|
end
|
|
163
|
+
alias on_numblock on_block
|
|
164
|
+
alias on_itblock on_block
|
|
92
165
|
|
|
93
166
|
private
|
|
94
167
|
|
|
95
|
-
def
|
|
168
|
+
def bad_intersection?(node)
|
|
169
|
+
bad_intersection_check?(node, bad_intersection_predicates) ||
|
|
170
|
+
intersection_size_check?(node)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def bad_intersection_predicates
|
|
96
174
|
if active_support_extensions_enabled?
|
|
97
|
-
|
|
175
|
+
ACTIVE_SUPPORT_PREDICATES
|
|
98
176
|
else
|
|
99
|
-
|
|
177
|
+
PREDICATES
|
|
100
178
|
end
|
|
101
179
|
end
|
|
102
180
|
|
|
@@ -104,15 +182,12 @@ module RuboCop
|
|
|
104
182
|
STRAIGHT_METHODS.include?(method_name.to_sym)
|
|
105
183
|
end
|
|
106
184
|
|
|
107
|
-
def
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
argument: argument,
|
|
114
|
-
method_name: method_name
|
|
115
|
-
)
|
|
185
|
+
def register_offense(node, replacement)
|
|
186
|
+
message = format(MSG, replacement: replacement, existing: node.source)
|
|
187
|
+
|
|
188
|
+
add_offense(node, message: message) do |corrector|
|
|
189
|
+
corrector.replace(node, replacement)
|
|
190
|
+
end
|
|
116
191
|
end
|
|
117
192
|
end
|
|
118
193
|
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Style
|
|
6
|
+
# Use `include?(element)` instead of `intersect?([element])`.
|
|
7
|
+
#
|
|
8
|
+
# @safety
|
|
9
|
+
# The receiver might not be an array.
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# # bad
|
|
13
|
+
# array.intersect?([element])
|
|
14
|
+
#
|
|
15
|
+
# # good
|
|
16
|
+
# array.include?(element)
|
|
17
|
+
class ArrayIntersectWithSingleElement < Base
|
|
18
|
+
extend AutoCorrector
|
|
19
|
+
|
|
20
|
+
MSG = 'Use `include?(element)` instead of `intersect?([element])`.'
|
|
21
|
+
|
|
22
|
+
RESTRICT_ON_SEND = %i[intersect?].freeze
|
|
23
|
+
|
|
24
|
+
# @!method single_element(node)
|
|
25
|
+
def_node_matcher :single_element, <<~PATTERN
|
|
26
|
+
(send _ _ $(array $_))
|
|
27
|
+
PATTERN
|
|
28
|
+
|
|
29
|
+
def on_send(node)
|
|
30
|
+
array, element = single_element(node)
|
|
31
|
+
return unless array
|
|
32
|
+
|
|
33
|
+
add_offense(
|
|
34
|
+
node.source_range.with(begin_pos: node.loc.selector.begin_pos)
|
|
35
|
+
) do |corrector|
|
|
36
|
+
corrector.replace(node.loc.selector, 'include?')
|
|
37
|
+
corrector.replace(
|
|
38
|
+
array,
|
|
39
|
+
array.percent_literal? ? element.value.inspect : element.source
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
alias on_csend on_send
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -70,18 +70,25 @@ module RuboCop
|
|
|
70
70
|
(send _ :& _))
|
|
71
71
|
PATTERN
|
|
72
72
|
|
|
73
|
+
# rubocop:disable Metrics/AbcSize
|
|
73
74
|
def on_send(node)
|
|
74
75
|
return unless node.receiver&.begin_type?
|
|
75
76
|
return unless (preferred_method = preferred_method(node))
|
|
76
77
|
|
|
77
78
|
bit_operation = node.receiver.children.first
|
|
78
79
|
lhs, _operator, rhs = *bit_operation
|
|
79
|
-
|
|
80
|
+
|
|
81
|
+
preferred = if preferred_method == 'allbits?' && lhs.source == node.first_argument.source
|
|
82
|
+
"#{rhs.source}.allbits?(#{lhs.source})"
|
|
83
|
+
else
|
|
84
|
+
"#{lhs.source}.#{preferred_method}(#{rhs.source})"
|
|
85
|
+
end
|
|
80
86
|
|
|
81
87
|
add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
|
|
82
88
|
corrector.replace(node, preferred)
|
|
83
89
|
end
|
|
84
90
|
end
|
|
91
|
+
# rubocop:enable Metrics/AbcSize
|
|
85
92
|
|
|
86
93
|
private
|
|
87
94
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
module RuboCop
|
|
5
5
|
module Cop
|
|
6
6
|
module Style
|
|
7
|
-
#
|
|
7
|
+
# Checks for uses of braces or do/end around single line or
|
|
8
8
|
# multi-line blocks.
|
|
9
9
|
#
|
|
10
10
|
# Methods that can be either procedural or functional and cannot be
|
|
@@ -208,6 +208,7 @@ module RuboCop
|
|
|
208
208
|
end
|
|
209
209
|
|
|
210
210
|
alias on_numblock on_block
|
|
211
|
+
alias on_itblock on_block
|
|
211
212
|
|
|
212
213
|
private
|
|
213
214
|
|
|
@@ -347,7 +348,7 @@ module RuboCop
|
|
|
347
348
|
# rubocop:disable Metrics/CyclomaticComplexity
|
|
348
349
|
def get_blocks(node, &block)
|
|
349
350
|
case node.type
|
|
350
|
-
when :block, :numblock
|
|
351
|
+
when :block, :numblock, :itblock
|
|
351
352
|
yield node
|
|
352
353
|
when :send, :csend
|
|
353
354
|
# When a method has an argument which is another method with a block,
|
|
@@ -3,14 +3,26 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
|
-
# Checks
|
|
7
|
-
#
|
|
6
|
+
# Checks that namespaced classes and modules are defined with a consistent style.
|
|
7
|
+
#
|
|
8
|
+
# With `nested` style, classes and modules should be defined separately (one constant
|
|
9
|
+
# on each line, without `::`). With `compact` style, classes and modules should be
|
|
10
|
+
# defined with fully qualified names (using `::` for namespaces).
|
|
11
|
+
#
|
|
12
|
+
# NOTE: The style chosen will affect `Module.nesting` for the class or module. Using
|
|
13
|
+
# `nested` style will result in each level being added, whereas `compact` style will
|
|
14
|
+
# only include the fully qualified class or module name.
|
|
15
|
+
#
|
|
16
|
+
# By default, `EnforcedStyle` applies to both classes and modules. If desired, separate
|
|
17
|
+
# styles can be defined for classes and modules by using `EnforcedStyleForClasses` and
|
|
18
|
+
# `EnforcedStyleForModules` respectively. If not set, or set to nil, the `EnforcedStyle`
|
|
19
|
+
# value will be used.
|
|
8
20
|
#
|
|
9
21
|
# @safety
|
|
10
22
|
# Autocorrection is unsafe.
|
|
11
23
|
#
|
|
12
|
-
# Moving from compact to nested children requires knowledge of whether the
|
|
13
|
-
# outer parent is a module or a class. Moving from nested to compact requires
|
|
24
|
+
# Moving from `compact` to `nested` children requires knowledge of whether the
|
|
25
|
+
# outer parent is a module or a class. Moving from `nested` to `compact` requires
|
|
14
26
|
# verification that the outer parent is defined elsewhere. RuboCop does not
|
|
15
27
|
# have the knowledge to perform either operation safely and thus requires
|
|
16
28
|
# manual oversight.
|
|
@@ -42,16 +54,18 @@ module RuboCop
|
|
|
42
54
|
def on_class(node)
|
|
43
55
|
return if node.parent_class && style != :nested
|
|
44
56
|
|
|
45
|
-
check_style(node, node.body)
|
|
57
|
+
check_style(node, node.body, style_for_classes)
|
|
46
58
|
end
|
|
47
59
|
|
|
48
60
|
def on_module(node)
|
|
49
|
-
check_style(node, node.body)
|
|
61
|
+
check_style(node, node.body, style_for_modules)
|
|
50
62
|
end
|
|
51
63
|
|
|
52
64
|
private
|
|
53
65
|
|
|
54
66
|
def nest_or_compact(corrector, node)
|
|
67
|
+
style = node.class_type? ? style_for_classes : style_for_modules
|
|
68
|
+
|
|
55
69
|
if style == :nested
|
|
56
70
|
nest_definition(corrector, node)
|
|
57
71
|
else
|
|
@@ -117,21 +131,27 @@ module RuboCop
|
|
|
117
131
|
"#{node.body.children.first.const_name}"
|
|
118
132
|
end
|
|
119
133
|
|
|
134
|
+
# rubocop:disable Metrics/AbcSize
|
|
120
135
|
def remove_end(corrector, body)
|
|
121
|
-
remove_begin_pos = body.loc.
|
|
136
|
+
remove_begin_pos = if same_line?(body.loc.name, body.loc.end)
|
|
137
|
+
body.loc.name.end_pos
|
|
138
|
+
else
|
|
139
|
+
body.loc.end.begin_pos - leading_spaces(body).size
|
|
140
|
+
end
|
|
122
141
|
adjustment = processed_source.raw_source[remove_begin_pos] == ';' ? 0 : 1
|
|
123
142
|
range = range_between(remove_begin_pos, body.loc.end.end_pos + adjustment)
|
|
124
143
|
|
|
125
144
|
corrector.remove(range)
|
|
126
145
|
end
|
|
146
|
+
# rubocop:enable Metrics/AbcSize
|
|
127
147
|
|
|
128
148
|
def unindent(corrector, node)
|
|
129
149
|
return unless node.body.children.last
|
|
130
150
|
|
|
131
151
|
last_child_leading_spaces = leading_spaces(node.body.children.last)
|
|
132
|
-
return if leading_spaces(node)
|
|
152
|
+
return if spaces_size(leading_spaces(node)) == spaces_size(last_child_leading_spaces)
|
|
133
153
|
|
|
134
|
-
column_delta = configured_indentation_width - last_child_leading_spaces
|
|
154
|
+
column_delta = configured_indentation_width - spaces_size(last_child_leading_spaces)
|
|
135
155
|
return if column_delta.zero?
|
|
136
156
|
|
|
137
157
|
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
|
@@ -141,7 +161,17 @@ module RuboCop
|
|
|
141
161
|
node.source_range.source_line[/\A\s*/]
|
|
142
162
|
end
|
|
143
163
|
|
|
144
|
-
def
|
|
164
|
+
def spaces_size(spaces_string)
|
|
165
|
+
mapping = { "\t" => tab_indentation_width }
|
|
166
|
+
spaces_string.chars.sum { |character| mapping.fetch(character, 1) }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def tab_indentation_width
|
|
170
|
+
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
|
171
|
+
configured_indentation_width
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def check_style(node, body, style)
|
|
145
175
|
return if node.identifier.namespace&.cbase_type?
|
|
146
176
|
|
|
147
177
|
if style == :nested
|
|
@@ -183,6 +213,14 @@ module RuboCop
|
|
|
183
213
|
def compact_node_name?(node)
|
|
184
214
|
node.identifier.source.include?('::')
|
|
185
215
|
end
|
|
216
|
+
|
|
217
|
+
def style_for_classes
|
|
218
|
+
cop_config['EnforcedStyleForClasses'] || style
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def style_for_modules
|
|
222
|
+
cop_config['EnforcedStyleForModules'] || style
|
|
223
|
+
end
|
|
186
224
|
end
|
|
187
225
|
end
|
|
188
226
|
end
|
|
@@ -68,7 +68,7 @@ module RuboCop
|
|
|
68
68
|
PATTERN
|
|
69
69
|
|
|
70
70
|
def on_send(node)
|
|
71
|
-
def_node = node.each_ancestor(:
|
|
71
|
+
def_node = node.each_ancestor(:any_def).first
|
|
72
72
|
return if def_node &&
|
|
73
73
|
(allowed_method?(def_node.method_name) ||
|
|
74
74
|
matches_allowed_pattern?(def_node.method_name))
|