rubocop 1.29.1 → 1.45.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +6 -4
- data/config/default.yml +336 -47
- data/config/obsoletion.yml +25 -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/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +20 -5
- data/lib/rubocop/cli/command/execute_runner.rb +8 -8
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +61 -16
- data/lib/rubocop/cli.rb +57 -9
- data/lib/rubocop/comment_config.rb +41 -1
- data/lib/rubocop/config.rb +45 -17
- data/lib/rubocop/config_finder.rb +68 -0
- data/lib/rubocop/config_loader.rb +41 -61
- data/lib/rubocop/config_loader_resolver.rb +11 -12
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +5 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +4 -0
- data/lib/rubocop/config_obsoletion.rb +7 -2
- data/lib/rubocop/config_validator.rb +22 -5
- data/lib/rubocop/cop/autocorrect_logic.rb +4 -2
- data/lib/rubocop/cop/badge.rb +9 -4
- data/lib/rubocop/cop/base.rb +114 -82
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/bundler/gem_filename.rb +5 -5
- data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -2
- data/lib/rubocop/cop/commissioner.rb +19 -6
- data/lib/rubocop/cop/cop.rb +52 -32
- data/lib/rubocop/cop/corrector.rb +32 -12
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +4 -2
- data/lib/rubocop/cop/correctors/if_then_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +7 -1
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +25 -9
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +2 -7
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +59 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/punctuation_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +92 -0
- 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 +1 -1
- data/lib/rubocop/cop/gemspec/require_mfa.rb +21 -21
- data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
- data/lib/rubocop/cop/generator.rb +6 -3
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +98 -0
- data/lib/rubocop/cop/internal_affairs/create_empty_file.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +111 -0
- data/lib/rubocop/cop/internal_affairs/lambda_or_proc.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -5
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +12 -4
- 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/single_line_comparison.rb +62 -0
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +60 -0
- data/lib/rubocop/cop/internal_affairs.rb +7 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +22 -1
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/begin_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +17 -13
- data/lib/rubocop/cop/layout/block_end_newline.rb +42 -6
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/class_structure.rb +31 -24
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +3 -7
- data/lib/rubocop/cop/layout/comment_indentation.rb +4 -2
- data/lib/rubocop/cop/layout/condition_position.rb +1 -1
- 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_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
- data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +7 -5
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +10 -10
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +10 -10
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +10 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +36 -30
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +21 -14
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +39 -3
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +52 -13
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +50 -3
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +62 -3
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +53 -3
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +7 -10
- data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_style.rb +8 -3
- data/lib/rubocop/cop/layout/indentation_width.rb +14 -7
- data/lib/rubocop/cop/layout/initial_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +138 -0
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +136 -0
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +52 -3
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +5 -3
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +50 -3
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +58 -4
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +101 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/redundant_line_break.rb +4 -4
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +1 -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_operators.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +5 -3
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +32 -30
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +33 -15
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +30 -3
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +34 -0
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +10 -6
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +6 -5
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +9 -9
- 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_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +28 -8
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +5 -1
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +3 -3
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +1 -1
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +12 -2
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +1 -1
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +4 -0
- data/lib/rubocop/cop/lint/debugger.rb +25 -32
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +67 -111
- data/lib/rubocop/cop/lint/deprecated_constants.rb +9 -2
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -3
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_hash_key.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +48 -18
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +26 -7
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/else_layout.rb +5 -9
- data/lib/rubocop/cop/lint/empty_block.rb +3 -7
- data/lib/rubocop/cop/lint/empty_class.rb +4 -2
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +109 -3
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
- data/lib/rubocop/cop/lint/empty_file.rb +1 -1
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/empty_when.rb +1 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
- data/lib/rubocop/cop/lint/flip_flop.rb +1 -1
- data/lib/rubocop/cop/lint/float_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +16 -18
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +2 -2
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
- data/lib/rubocop/cop/lint/interpolation_check.rb +6 -5
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +2 -2
- data/lib/rubocop/cop/lint/literal_as_condition.rb +6 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +5 -1
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +19 -4
- data/lib/rubocop/cop/lint/missing_super.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +58 -6
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +162 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +1 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +32 -10
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -3
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +20 -1
- 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/raise_exception.rb +1 -1
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +44 -10
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +3 -3
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +48 -7
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +16 -3
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_with_index.rb +14 -11
- data/lib/rubocop/cop/lint/redundant_with_object.rb +13 -12
- data/lib/rubocop/cop/lint/refinement_import_methods.rb +1 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +9 -3
- data/lib/rubocop/cop/lint/require_parentheses.rb +4 -2
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +57 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/return_in_void_context.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +44 -8
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
- data/lib/rubocop/cop/lint/script_permission.rb +1 -1
- data/lib/rubocop/cop/lint/self_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +5 -4
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/lint/shadowed_exception.rb +16 -11
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +29 -4
- data/lib/rubocop/cop/lint/struct_new_override.rb +3 -3
- data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/to_json.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +2 -2
- data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +1 -1
- data/lib/rubocop/cop/lint/unified_integer.rb +3 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +10 -4
- data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +7 -2
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -1
- data/lib/rubocop/cop/lint/uri_regexp.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +15 -10
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
- data/lib/rubocop/cop/lint/useless_method_definition.rb +4 -4
- data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +16 -6
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +27 -16
- data/lib/rubocop/cop/metrics/abc_size.rb +5 -3
- 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 +10 -5
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- 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 +28 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +7 -4
- 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/annotation_comment.rb +13 -6
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +5 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +22 -4
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
- data/lib/rubocop/cop/mixin/def_node.rb +2 -7
- 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_shorthand_syntax.rb +152 -12
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -2
- data/lib/rubocop/cop/mixin/method_complexity.rb +13 -16
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +34 -12
- data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -3
- data/lib/rubocop/cop/mixin/percent_array.rb +58 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +33 -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/statement_modifier.rb +16 -1
- data/lib/rubocop/cop/mixin/string_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +13 -11
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/visibility_help.rb +40 -5
- data/lib/rubocop/cop/naming/accessor_method_name.rb +4 -2
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +7 -3
- data/lib/rubocop/cop/naming/block_parameter_name.rb +2 -2
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +4 -2
- data/lib/rubocop/cop/naming/constant_name.rb +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +5 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +30 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +18 -18
- data/lib/rubocop/cop/registry.rb +70 -44
- data/lib/rubocop/cop/security/compound_hash.rb +3 -2
- data/lib/rubocop/cop/security/eval.rb +1 -1
- data/lib/rubocop/cop/security/json_load.rb +1 -1
- data/lib/rubocop/cop/security/marshal_load.rb +1 -1
- data/lib/rubocop/cop/security/open.rb +1 -1
- data/lib/rubocop/cop/security/yaml_load.rb +1 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +92 -3
- data/lib/rubocop/cop/style/accessor_grouping.rb +11 -7
- data/lib/rubocop/cop/style/alias.rb +10 -2
- data/lib/rubocop/cop/style/and_or.rb +11 -11
- data/lib/rubocop/cop/style/arguments_forwarding.rb +4 -3
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +111 -0
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -1
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +38 -11
- data/lib/rubocop/cop/style/case_equality.rb +41 -11
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -15
- data/lib/rubocop/cop/style/class_check.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +55 -6
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/class_methods_definitions.rb +3 -2
- data/lib/rubocop/cop/style/class_vars.rb +1 -1
- data/lib/rubocop/cop/style/collection_compact.rb +13 -4
- data/lib/rubocop/cop/style/collection_methods.rb +3 -1
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
- data/lib/rubocop/cop/style/colon_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +4 -2
- data/lib/rubocop/cop/style/command_literal.rb +2 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +4 -4
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +86 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -7
- data/lib/rubocop/cop/style/constant_visibility.rb +1 -1
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/def_with_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +4 -1
- data/lib/rubocop/cop/style/documentation.rb +13 -7
- data/lib/rubocop/cop/style/documentation_method.rb +7 -1
- data/lib/rubocop/cop/style/double_negation.rb +3 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +42 -7
- 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 +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +40 -3
- 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 +18 -3
- data/lib/rubocop/cop/style/encoding.rb +2 -2
- data/lib/rubocop/cop/style/end_block.rb +1 -1
- data/lib/rubocop/cop/style/endless_method.rb +2 -2
- data/lib/rubocop/cop/style/env_home.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +6 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +1 -1
- data/lib/rubocop/cop/style/fetch_env_var.rb +37 -224
- data/lib/rubocop/cop/style/float_division.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +3 -1
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/format_string_token.rb +73 -23
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +6 -3
- data/lib/rubocop/cop/style/global_std_stream.rb +1 -1
- data/lib/rubocop/cop/style/global_vars.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +132 -43
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +1 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +49 -13
- data/lib/rubocop/cop/style/hash_except.rb +89 -9
- data/lib/rubocop/cop/style/hash_like_case.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +24 -3
- data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +17 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +29 -4
- data/lib/rubocop/cop/style/if_with_semicolon.rb +4 -4
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +3 -3
- data/lib/rubocop/cop/style/in_pattern_then.rb +1 -1
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +12 -8
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +2 -2
- data/lib/rubocop/cop/style/lambda.rb +1 -1
- data/lib/rubocop/cop/style/lambda_call.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +6 -3
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +136 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +3 -3
- data/lib/rubocop/cop/style/map_to_set.rb +61 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +39 -19
- 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 +8 -8
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +22 -3
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +5 -2
- data/lib/rubocop/cop/style/method_def_parentheses.rb +12 -5
- data/lib/rubocop/cop/style/min_max.rb +1 -1
- data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
- data/lib/rubocop/cop/style/missing_else.rb +37 -25
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +1 -1
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/mixin_usage.rb +1 -1
- 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 +3 -5
- data/lib/rubocop/cop/style/multiline_memoization.rb +3 -3
- data/lib/rubocop/cop/style/multiline_method_signature.rb +2 -2
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +21 -6
- data/lib/rubocop/cop/style/multiline_when_then.rb +2 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -7
- data/lib/rubocop/cop/style/nested_file_dirname.rb +1 -1
- data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +11 -2
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +20 -8
- data/lib/rubocop/cop/style/next.rb +3 -5
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +4 -4
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/not.rb +2 -2
- data/lib/rubocop/cop/style/numbered_parameters.rb +1 -1
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +12 -4
- 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 +6 -1
- 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 +67 -0
- data/lib/rubocop/cop/style/option_hash.rb +1 -1
- data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +1 -1
- data/lib/rubocop/cop/style/or_assignment.rb +1 -1
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +23 -2
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +5 -2
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +5 -2
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +20 -6
- data/lib/rubocop/cop/style/redundant_capital_w.rb +1 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +48 -11
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -5
- data/lib/rubocop/cop/style/redundant_constant_base.rb +85 -0
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +45 -0
- data/lib/rubocop/cop/style/redundant_each.rb +116 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -2
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- 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 +22 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +22 -24
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +10 -3
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -4
- data/lib/rubocop/cop/style/redundant_return.rb +9 -2
- data/lib/rubocop/cop/style/redundant_self.rb +3 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +23 -8
- data/lib/rubocop/cop/style/redundant_sort_by.rb +25 -9
- data/lib/rubocop/cop/style/redundant_string_escape.rb +184 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/require_order.rb +135 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
- data/lib/rubocop/cop/style/rescue_standard_error.rb +13 -13
- data/lib/rubocop/cop/style/return_nil.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +43 -9
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/select_by_regexp.rb +14 -6
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/semicolon.rb +53 -6
- data/lib/rubocop/cop/style/send.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +9 -7
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
- data/lib/rubocop/cop/style/single_line_methods.rb +2 -2
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +42 -14
- data/lib/rubocop/cop/style/static_class.rb +33 -2
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +6 -7
- data/lib/rubocop/cop/style/string_hash_keys.rb +5 -2
- data/lib/rubocop/cop/style/string_literals.rb +1 -5
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +1 -1
- data/lib/rubocop/cop/style/strip.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -4
- data/lib/rubocop/cop/style/swap_values.rb +2 -2
- data/lib/rubocop/cop/style/symbol_array.rb +12 -7
- data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +46 -14
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -14
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -1
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/trailing_body_on_module.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +5 -5
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +2 -2
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
- data/lib/rubocop/cop/style/trailing_method_end_statement.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_else.rb +1 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +5 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +48 -6
- data/lib/rubocop/cop/style/yoda_condition.rb +13 -6
- data/lib/rubocop/cop/style/yoda_expression.rb +90 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +32 -15
- data/lib/rubocop/cop/team.rb +54 -50
- data/lib/rubocop/cop/util.rb +34 -7
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -1
- data/lib/rubocop/cop/variable_force/scope.rb +3 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +4 -2
- data/lib/rubocop/cop/variable_force.rb +18 -30
- data/lib/rubocop/cops_documentation_generator.rb +53 -13
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/ext/processed_source.rb +2 -0
- data/lib/rubocop/ext/range.rb +15 -0
- data/lib/rubocop/feature_loader.rb +94 -0
- data/lib/rubocop/file_patterns.rb +43 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +36 -16
- data/lib/rubocop/formatter/formatter_set.rb +20 -19
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +15 -2
- data/lib/rubocop/formatter/html_formatter.rb +4 -5
- data/lib/rubocop/formatter/markdown_formatter.rb +3 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +10 -5
- data/lib/rubocop/formatter/simple_text_formatter.rb +9 -8
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +6 -3
- data/lib/rubocop/formatter.rb +34 -0
- data/lib/rubocop/magic_comment.rb +27 -2
- data/lib/rubocop/options.rb +128 -53
- data/lib/rubocop/path_util.rb +50 -22
- data/lib/rubocop/rake_task.rb +34 -9
- data/lib/rubocop/result_cache.rb +26 -23
- data/lib/rubocop/rspec/cop_helper.rb +25 -2
- data/lib/rubocop/rspec/expect_offense.rb +9 -7
- data/lib/rubocop/rspec/shared_contexts.rb +42 -9
- data/lib/rubocop/rspec/support.rb +16 -2
- data/lib/rubocop/runner.rb +78 -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 +34 -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/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +8 -7
- data/lib/rubocop/version.rb +22 -9
- data/lib/rubocop.rb +45 -35
- metadata +89 -36
- data/lib/rubocop/cop/gemspec/date_assignment.rb +0 -49
- data/lib/rubocop/cop/mixin/ignored_methods.rb +0 -52
@@ -6,7 +6,7 @@
|
|
6
6
|
module RuboCop
|
7
7
|
module Cop
|
8
8
|
module Lint
|
9
|
-
#
|
9
|
+
# Detects instances of rubocop:disable comments that can be
|
10
10
|
# removed without causing any offenses to be reported. It's implemented
|
11
11
|
# as a cop in that it inherits from the Cop base class and calls
|
12
12
|
# add_offense. The unusual part of its implementation is that it doesn't
|
@@ -71,16 +71,16 @@ module RuboCop
|
|
71
71
|
processed_source.comment_config.comment_only_line?(directive_comment_range.line) &&
|
72
72
|
directive_comment_range.begin_pos == line_comment_range.begin_pos
|
73
73
|
# When the previous line is blank, it should be retained
|
74
|
-
range_with_surrounding_space(
|
74
|
+
range_with_surrounding_space(directive_comment_range, side: :right)
|
75
75
|
else
|
76
76
|
# Eat the entire comment, the preceding space, and the preceding
|
77
77
|
# newline if there is one.
|
78
78
|
original_begin = directive_comment_range.begin_pos
|
79
79
|
range = range_with_surrounding_space(
|
80
|
-
|
80
|
+
directive_comment_range, side: :left, newlines: true
|
81
81
|
)
|
82
82
|
|
83
|
-
range_with_surrounding_space(range
|
83
|
+
range_with_surrounding_space(range,
|
84
84
|
side: :right,
|
85
85
|
# Special for a comment that
|
86
86
|
# begins the file: remove
|
@@ -94,13 +94,13 @@ module RuboCop
|
|
94
94
|
# is NOT being removed?
|
95
95
|
if ends_its_line?(ranges.last) && trailing_range?(ranges, range)
|
96
96
|
# Eat the comma on the left.
|
97
|
-
range = range_with_surrounding_space(range
|
97
|
+
range = range_with_surrounding_space(range, side: :left)
|
98
98
|
range = range_with_surrounding_comma(range, :left)
|
99
99
|
end
|
100
100
|
|
101
101
|
range = range_with_surrounding_comma(range, :right)
|
102
102
|
# Eat following spaces up to EOL, but not the newline itself.
|
103
|
-
range_with_surrounding_space(range
|
103
|
+
range_with_surrounding_space(range, side: :right, newlines: false)
|
104
104
|
end
|
105
105
|
|
106
106
|
def each_redundant_disable(&block)
|
@@ -113,6 +113,7 @@ module RuboCop
|
|
113
113
|
def each_line_range(cop, line_ranges)
|
114
114
|
line_ranges.each_with_index do |line_range, line_range_index|
|
115
115
|
next if ignore_offense?(line_range)
|
116
|
+
next if expected_final_disable?(cop, line_range)
|
116
117
|
|
117
118
|
comment = processed_source.comment_at_line(line_range.begin)
|
118
119
|
redundant = if all_disabled?(comment)
|
@@ -127,23 +128,35 @@ module RuboCop
|
|
127
128
|
end
|
128
129
|
end
|
129
130
|
|
131
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
130
132
|
def each_already_disabled(cop, line_ranges)
|
131
133
|
line_ranges.each_cons(2) do |previous_range, range|
|
132
134
|
next if ignore_offense?(range)
|
133
|
-
next unless followed_ranges?(previous_range, range)
|
134
|
-
|
135
135
|
# If a cop is disabled in a range that begins on the same line as
|
136
136
|
# the end of the previous range, it means that the cop was
|
137
137
|
# already disabled by an earlier comment. So it's redundant
|
138
138
|
# whether there are offenses or not.
|
139
|
+
next unless followed_ranges?(previous_range, range)
|
140
|
+
|
139
141
|
comment = processed_source.comment_at_line(range.begin)
|
140
142
|
|
143
|
+
next unless comment
|
141
144
|
# Comments disabling all cops don't count since it's reasonable
|
142
145
|
# to disable a few select cops first and then all cops further
|
143
146
|
# down in the code.
|
144
|
-
|
147
|
+
next if all_disabled?(comment)
|
148
|
+
|
149
|
+
redundant =
|
150
|
+
if department_disabled?(cop, comment)
|
151
|
+
find_redundant_department(cop, range)
|
152
|
+
else
|
153
|
+
cop
|
154
|
+
end
|
155
|
+
|
156
|
+
yield comment, redundant if redundant
|
145
157
|
end
|
146
158
|
end
|
159
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
147
160
|
|
148
161
|
def find_redundant_cop(cop, range)
|
149
162
|
cop_offenses = offenses_to_check.select { |offense| offense.cop_name == cop }
|
@@ -179,11 +192,21 @@ module RuboCop
|
|
179
192
|
end
|
180
193
|
|
181
194
|
def ignore_offense?(line_range)
|
195
|
+
return true if line_range.min == CommentConfig::CONFIG_DISABLED_LINE_RANGE_MIN
|
196
|
+
|
182
197
|
disabled_ranges.any? do |range|
|
183
198
|
range.cover?(line_range.min) && range.cover?(line_range.max)
|
184
199
|
end
|
185
200
|
end
|
186
201
|
|
202
|
+
def expected_final_disable?(cop, line_range)
|
203
|
+
# A cop which is disabled in the config is being re-disabled until end of file
|
204
|
+
cop_class = RuboCop::Cop::Registry.global.find_by_cop_name cop
|
205
|
+
cop_class &&
|
206
|
+
!processed_source.registry.enabled?(cop_class, config) &&
|
207
|
+
line_range.max == Float::INFINITY
|
208
|
+
end
|
209
|
+
|
187
210
|
def department_disabled?(cop, comment)
|
188
211
|
directive = DirectiveComment.new(comment)
|
189
212
|
directive.in_directive_department?(cop) && !directive.overridden_by_department?(cop)
|
@@ -209,7 +232,12 @@ module RuboCop
|
|
209
232
|
|
210
233
|
add_offense(location, message: message(cop_names)) do |corrector|
|
211
234
|
range = comment_range_with_surrounding_space(location, comment.loc.expression)
|
212
|
-
|
235
|
+
|
236
|
+
if leave_free_comment?(comment, range)
|
237
|
+
corrector.replace(range, ' # ')
|
238
|
+
else
|
239
|
+
corrector.remove(range)
|
240
|
+
end
|
213
241
|
end
|
214
242
|
end
|
215
243
|
|
@@ -227,6 +255,12 @@ module RuboCop
|
|
227
255
|
end
|
228
256
|
end
|
229
257
|
|
258
|
+
def leave_free_comment?(comment, range)
|
259
|
+
free_comment = comment.text.gsub(range.source.strip, '')
|
260
|
+
|
261
|
+
!free_comment.empty? && !free_comment.start_with?('#')
|
262
|
+
end
|
263
|
+
|
230
264
|
def cop_range(comment, cop)
|
231
265
|
cop = remove_department_marker(cop)
|
232
266
|
matching_range(comment.loc.expression, cop) ||
|
@@ -9,7 +9,7 @@
|
|
9
9
|
module RuboCop
|
10
10
|
module Cop
|
11
11
|
module Lint
|
12
|
-
#
|
12
|
+
# Detects instances of rubocop:enable comments that can be
|
13
13
|
# removed.
|
14
14
|
#
|
15
15
|
# When comment enables all cops at once `rubocop:enable all`
|
@@ -42,7 +42,7 @@ module RuboCop
|
|
42
42
|
MSG = 'Unnecessary enabling of %<cop>s.'
|
43
43
|
|
44
44
|
def on_new_investigation
|
45
|
-
return if processed_source.blank?
|
45
|
+
return if processed_source.blank? || !processed_source.raw_source.include?('enable')
|
46
46
|
|
47
47
|
offenses = processed_source.comment_config.extra_enabled_comments
|
48
48
|
offenses.each { |comment, cop_names| register_offense(comment, cop_names) }
|
@@ -60,7 +60,7 @@ module RuboCop
|
|
60
60
|
message: format(MSG, cop: all_or_name(name))
|
61
61
|
) do |corrector|
|
62
62
|
if directive.match?(cop_names)
|
63
|
-
corrector.remove(range_with_surrounding_space(
|
63
|
+
corrector.remove(range_with_surrounding_space(directive.range, side: :right))
|
64
64
|
else
|
65
65
|
corrector.remove(range_with_comma(comment, name))
|
66
66
|
end
|
@@ -41,6 +41,7 @@ module RuboCop
|
|
41
41
|
return unless (receiver = node.receiver)
|
42
42
|
return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
|
43
43
|
return unless GLOB_METHODS.include?(receiver.method_name)
|
44
|
+
return if multiple_argument?(receiver)
|
44
45
|
|
45
46
|
selector = node.loc.selector
|
46
47
|
|
@@ -49,6 +50,12 @@ module RuboCop
|
|
49
50
|
corrector.remove(node.loc.dot)
|
50
51
|
end
|
51
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def multiple_argument?(glob_method)
|
57
|
+
glob_method.arguments.count >= 2 || glob_method.first_argument&.splat_type?
|
58
|
+
end
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
@@ -6,13 +6,23 @@ module RuboCop
|
|
6
6
|
# Checks for unnecessary `require` statement.
|
7
7
|
#
|
8
8
|
# The following features are unnecessary `require` statement because
|
9
|
-
# they are already loaded.
|
9
|
+
# they are already loaded. e.g. Ruby 2.2:
|
10
10
|
#
|
11
11
|
# ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
|
12
12
|
# ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
|
13
13
|
# ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# Below are the features that each `TargetRubyVersion` targets.
|
16
|
+
#
|
17
|
+
# * 2.0+ ... `enumerator`
|
18
|
+
# * 2.1+ ... `thread`
|
19
|
+
# * 2.2+ ... Add `rational` and `complex` above
|
20
|
+
# * 2.5+ ... Add `pp` above
|
21
|
+
# * 2.7+ ... Add `ruby2_keywords` above
|
22
|
+
# * 3.1+ ... Add `fiber` above
|
23
|
+
# * 3.2+ ... `set`
|
24
|
+
#
|
25
|
+
# This cop target those features.
|
16
26
|
#
|
17
27
|
# @example
|
18
28
|
# # bad
|
@@ -27,22 +37,53 @@ module RuboCop
|
|
27
37
|
|
28
38
|
MSG = 'Remove unnecessary `require` statement.'
|
29
39
|
RESTRICT_ON_SEND = %i[require].freeze
|
40
|
+
RUBY_22_LOADED_FEATURES = %w[rational complex].freeze
|
41
|
+
PRETTY_PRINT_METHODS = %i[
|
42
|
+
pretty_inspect pretty_print pretty_print_cycle
|
43
|
+
pretty_print_inspect pretty_print_instance_variables
|
44
|
+
].freeze
|
30
45
|
|
31
|
-
# @!method
|
32
|
-
def_node_matcher :
|
46
|
+
# @!method redundant_require_statement?(node)
|
47
|
+
def_node_matcher :redundant_require_statement?, <<~PATTERN
|
33
48
|
(send nil? :require
|
34
|
-
(str
|
49
|
+
(str #redundant_feature?))
|
35
50
|
PATTERN
|
36
51
|
|
37
52
|
def on_send(node)
|
38
|
-
return unless
|
53
|
+
return unless redundant_require_statement?(node)
|
39
54
|
|
40
55
|
add_offense(node) do |corrector|
|
41
|
-
|
56
|
+
if node.parent.respond_to?(:modifier_form?) && node.parent.modifier_form?
|
57
|
+
corrector.insert_after(node.parent, "\nend")
|
58
|
+
|
59
|
+
range = range_with_surrounding_space(node.loc.expression, side: :right)
|
60
|
+
else
|
61
|
+
range = range_by_whole_lines(node.source_range, include_final_newline: true)
|
62
|
+
end
|
42
63
|
|
43
64
|
corrector.remove(range)
|
44
65
|
end
|
45
66
|
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
71
|
+
def redundant_feature?(feature_name)
|
72
|
+
feature_name == 'enumerator' ||
|
73
|
+
(target_ruby_version >= 2.1 && feature_name == 'thread') ||
|
74
|
+
(target_ruby_version >= 2.2 && RUBY_22_LOADED_FEATURES.include?(feature_name)) ||
|
75
|
+
(target_ruby_version >= 2.5 && feature_name == 'pp' && !use_pretty_print_method?) ||
|
76
|
+
(target_ruby_version >= 2.7 && feature_name == 'ruby2_keywords') ||
|
77
|
+
(target_ruby_version >= 3.1 && feature_name == 'fiber') ||
|
78
|
+
(target_ruby_version >= 3.2 && feature_name == 'set')
|
79
|
+
end
|
80
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
81
|
+
|
82
|
+
def use_pretty_print_method?
|
83
|
+
processed_source.ast.each_descendant(:send).any? do |node|
|
84
|
+
PRETTY_PRINT_METHODS.include?(node.method_name)
|
85
|
+
end
|
86
|
+
end
|
46
87
|
end
|
47
88
|
end
|
48
89
|
end
|
@@ -3,17 +3,22 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks for redundant safe navigation calls.
|
7
7
|
# `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
|
8
8
|
# are checked by default. These are customizable with `AllowedMethods` option.
|
9
9
|
#
|
10
|
+
# The `AllowedMethods` option specifies nil-safe methods,
|
11
|
+
# in other words, it is a method that is allowed to skip safe navigation.
|
12
|
+
# Note that the `AllowedMethod` option is not an option that specifies methods
|
13
|
+
# for which to suppress (allow) this cop's check.
|
14
|
+
#
|
10
15
|
# In the example below, the safe navigation operator (`&.`) is unnecessary
|
11
16
|
# because `NilClass` has methods like `respond_to?` and `is_a?`.
|
12
17
|
#
|
13
18
|
# @safety
|
14
|
-
# This cop is unsafe, because
|
19
|
+
# This cop is unsafe, because autocorrection can change the return type of
|
15
20
|
# the expression. An offending expression that previously could return `nil`
|
16
|
-
# will be
|
21
|
+
# will be autocorrected to never return `nil`.
|
17
22
|
#
|
18
23
|
# @example
|
19
24
|
# # bad
|
@@ -35,6 +40,14 @@ module RuboCop
|
|
35
40
|
# # good - without `&.` this will always return `true`
|
36
41
|
# foo&.respond_to?(:to_a)
|
37
42
|
#
|
43
|
+
# @example AllowedMethods: [nil_safe_method]
|
44
|
+
# # bad
|
45
|
+
# do_something if attrs&.nil_safe_method(:[])
|
46
|
+
#
|
47
|
+
# # good
|
48
|
+
# do_something if attrs.nil_safe_method(:[])
|
49
|
+
# do_something if attrs&.not_nil_safe_method(:[])
|
50
|
+
#
|
38
51
|
class RedundantSafeNavigation < Base
|
39
52
|
include AllowedMethods
|
40
53
|
include RangeHelp
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks for redundant `with_index`.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# # bad
|
@@ -33,16 +33,6 @@ module RuboCop
|
|
33
33
|
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'
|
34
34
|
MSG_WITH_INDEX = 'Remove redundant `with_index`.'
|
35
35
|
|
36
|
-
# @!method redundant_with_index?(node)
|
37
|
-
def_node_matcher :redundant_with_index?, <<~PATTERN
|
38
|
-
(block
|
39
|
-
$(send
|
40
|
-
_ {:each_with_index :with_index} ...)
|
41
|
-
(args
|
42
|
-
(arg _))
|
43
|
-
...)
|
44
|
-
PATTERN
|
45
|
-
|
46
36
|
def on_block(node)
|
47
37
|
return unless (send = redundant_with_index?(node))
|
48
38
|
|
@@ -58,8 +48,21 @@ module RuboCop
|
|
58
48
|
end
|
59
49
|
end
|
60
50
|
|
51
|
+
alias on_numblock on_block
|
52
|
+
|
61
53
|
private
|
62
54
|
|
55
|
+
# @!method redundant_with_index?(node)
|
56
|
+
def_node_matcher :redundant_with_index?, <<~PATTERN
|
57
|
+
{
|
58
|
+
(block
|
59
|
+
$(send _ {:each_with_index :with_index} ...)
|
60
|
+
(args (arg _)) ...)
|
61
|
+
(numblock
|
62
|
+
$(send _ {:each_with_index :with_index} ...) 1 ...)
|
63
|
+
}
|
64
|
+
PATTERN
|
65
|
+
|
63
66
|
def message(node)
|
64
67
|
if node.method?(:each_with_index)
|
65
68
|
MSG_EACH_WITH_INDEX
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks for redundant `with_object`.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# # bad
|
@@ -31,19 +31,8 @@ module RuboCop
|
|
31
31
|
extend AutoCorrector
|
32
32
|
|
33
33
|
MSG_EACH_WITH_OBJECT = 'Use `each` instead of `each_with_object`.'
|
34
|
-
|
35
34
|
MSG_WITH_OBJECT = 'Remove redundant `with_object`.'
|
36
35
|
|
37
|
-
# @!method redundant_with_object?(node)
|
38
|
-
def_node_matcher :redundant_with_object?, <<~PATTERN
|
39
|
-
(block
|
40
|
-
$(send _ {:each_with_object :with_object}
|
41
|
-
_)
|
42
|
-
(args
|
43
|
-
(arg _))
|
44
|
-
...)
|
45
|
-
PATTERN
|
46
|
-
|
47
36
|
def on_block(node)
|
48
37
|
return unless (send = redundant_with_object?(node))
|
49
38
|
|
@@ -59,8 +48,20 @@ module RuboCop
|
|
59
48
|
end
|
60
49
|
end
|
61
50
|
|
51
|
+
alias on_numblock on_block
|
52
|
+
|
62
53
|
private
|
63
54
|
|
55
|
+
# @!method redundant_with_object?(node)
|
56
|
+
def_node_matcher :redundant_with_object?, <<~PATTERN
|
57
|
+
{
|
58
|
+
(block
|
59
|
+
$(send _ {:each_with_object :with_object} _) (args (arg _)) ...)
|
60
|
+
(numblock
|
61
|
+
$(send _ {:each_with_object :with_object} _) 1 ...)
|
62
|
+
}
|
63
|
+
PATTERN
|
64
|
+
|
64
65
|
def message(node)
|
65
66
|
if node.method?(:each_with_object)
|
66
67
|
MSG_EACH_WITH_OBJECT
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks if `include` or `prepend` is called in `refine` block.
|
7
7
|
# These methods are deprecated and should be replaced with `Refinement#import_methods`.
|
8
8
|
#
|
9
9
|
# It emulates deprecation warnings in Ruby 3.1.
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks for regexp literals used as `match-current-line`.
|
7
7
|
# If a regexp literal is in condition, the regexp matches `$_` implicitly.
|
8
8
|
#
|
9
9
|
# @example
|
@@ -17,13 +17,19 @@ module RuboCop
|
|
17
17
|
# do_something
|
18
18
|
# end
|
19
19
|
class RegexpAsCondition < Base
|
20
|
+
include IgnoredNode
|
20
21
|
extend AutoCorrector
|
21
22
|
|
22
|
-
MSG = 'Do not use regexp literal as a condition.' \
|
23
|
-
'
|
23
|
+
MSG = 'Do not use regexp literal as a condition. ' \
|
24
|
+
'The regexp literal matches `$_` implicitly.'
|
24
25
|
|
25
26
|
def on_match_current_line(node)
|
27
|
+
return if node.ancestors.none?(&:conditional?)
|
28
|
+
return if part_of_ignored_node?(node)
|
29
|
+
|
26
30
|
add_offense(node) { |corrector| corrector.replace(node, "#{node.source} =~ $_") }
|
31
|
+
|
32
|
+
ignore_node(node)
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks for expressions where there is a call to a predicate
|
7
7
|
# method with at least one argument, where no parentheses are used around
|
8
8
|
# the parameter list, and a boolean operator, && or ||, is used in the
|
9
9
|
# last argument.
|
@@ -46,7 +46,9 @@ module RuboCop
|
|
46
46
|
private
|
47
47
|
|
48
48
|
def check_ternary(ternary, node)
|
49
|
-
|
49
|
+
if node.method?(:[]) || node.assignment_method? || !ternary.condition.operator_keyword?
|
50
|
+
return
|
51
|
+
end
|
50
52
|
|
51
53
|
range = range_between(node.source_range.begin_pos, ternary.condition.source_range.end_pos)
|
52
54
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Checks that a range literal is enclosed in parentheses when the end of the range is
|
7
|
+
# at a line break.
|
8
|
+
#
|
9
|
+
# NOTE: The following is maybe intended for `(42..)`. But, compatible is `42..do_something`.
|
10
|
+
# So, this cop does not provide autocorrection because it is left to user.
|
11
|
+
#
|
12
|
+
# [source,ruby]
|
13
|
+
# ----
|
14
|
+
# case condition
|
15
|
+
# when 42..
|
16
|
+
# do_something
|
17
|
+
# end
|
18
|
+
# ----
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
#
|
22
|
+
# # bad - Represents `(1..42)`, not endless range.
|
23
|
+
# 1..
|
24
|
+
# 42
|
25
|
+
#
|
26
|
+
# # good - It's incompatible, but your intentions when using endless range may be:
|
27
|
+
# (1..)
|
28
|
+
# 42
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# 1..42
|
32
|
+
#
|
33
|
+
# # good
|
34
|
+
# (1..42)
|
35
|
+
#
|
36
|
+
# # good
|
37
|
+
# (1..
|
38
|
+
# 42)
|
39
|
+
#
|
40
|
+
class RequireRangeParentheses < Base
|
41
|
+
MSG = 'Wrap the endless range literal `%<range>s` to avoid precedence ambiguity.'
|
42
|
+
|
43
|
+
def on_irange(node)
|
44
|
+
return if node.parent&.begin_type?
|
45
|
+
return unless node.begin && node.end
|
46
|
+
return if same_line?(node.begin, node.end)
|
47
|
+
|
48
|
+
message = format(MSG, range: "#{node.begin.source}#{node.loc.operator.source}")
|
49
|
+
|
50
|
+
add_offense(node, message: message)
|
51
|
+
end
|
52
|
+
|
53
|
+
alias on_erange on_irange
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -25,8 +25,13 @@ module RuboCop
|
|
25
25
|
# x&.foo || bar
|
26
26
|
class SafeNavigationChain < Base
|
27
27
|
include NilMethods
|
28
|
+
extend AutoCorrector
|
29
|
+
extend TargetRubyVersion
|
30
|
+
|
31
|
+
minimum_target_ruby_version 2.3
|
28
32
|
|
29
33
|
MSG = 'Do not chain ordinary method call after safe navigation operator.'
|
34
|
+
PLUS_MINUS_METHODS = %i[+@ -@].freeze
|
30
35
|
|
31
36
|
# @!method bad_method?(node)
|
32
37
|
def_node_matcher :bad_method?, <<~PATTERN
|
@@ -38,23 +43,54 @@ module RuboCop
|
|
38
43
|
|
39
44
|
def on_send(node)
|
40
45
|
bad_method?(node) do |safe_nav, method|
|
41
|
-
return if nil_methods.include?(method)
|
46
|
+
return if nil_methods.include?(method) || PLUS_MINUS_METHODS.include?(node.method_name)
|
42
47
|
|
43
|
-
method_chain = method_chain(node)
|
44
48
|
location =
|
45
49
|
Parser::Source::Range.new(node.source_range.source_buffer,
|
46
50
|
safe_nav.source_range.end_pos,
|
47
|
-
|
48
|
-
add_offense(location)
|
51
|
+
node.source_range.end_pos)
|
52
|
+
add_offense(location) do |corrector|
|
53
|
+
autocorrect(corrector, offense_range: location, send_node: node)
|
54
|
+
end
|
49
55
|
end
|
50
56
|
end
|
51
57
|
|
52
58
|
private
|
53
59
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
60
|
+
# @param [Parser::Source::Range] offense_range
|
61
|
+
# @param [RuboCop::AST::SendNode] send_node
|
62
|
+
# @return [String]
|
63
|
+
def add_safe_navigation_operator(offense_range:, send_node:)
|
64
|
+
source =
|
65
|
+
if brackets?(send_node)
|
66
|
+
format(
|
67
|
+
'%<method_name>s(%<arguments>s)%<method_chain>s',
|
68
|
+
arguments: send_node.arguments.map(&:source).join(', '),
|
69
|
+
method_name: send_node.method_name,
|
70
|
+
method_chain: send_node.source_range.end.join(send_node.source_range.end).source
|
71
|
+
)
|
72
|
+
else
|
73
|
+
offense_range.source
|
74
|
+
end
|
75
|
+
source.prepend('.') unless source.start_with?('.')
|
76
|
+
source.prepend('&')
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param [RuboCop::Cop::Corrector] corrector
|
80
|
+
# @param [Parser::Source::Range] offense_range
|
81
|
+
# @param [RuboCop::AST::SendNode] send_node
|
82
|
+
def autocorrect(corrector, offense_range:, send_node:)
|
83
|
+
corrector.replace(
|
84
|
+
offense_range,
|
85
|
+
add_safe_navigation_operator(
|
86
|
+
offense_range: offense_range,
|
87
|
+
send_node: send_node
|
88
|
+
)
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
def brackets?(send_node)
|
93
|
+
send_node.method?(:[]) || send_node.method?(:[]=)
|
58
94
|
end
|
59
95
|
end
|
60
96
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Check to make sure that if safe navigation is used for a method
|
7
7
|
# call in an `&&` or `||` condition that safe navigation is used for all
|
8
8
|
# method calls on that same object.
|
9
9
|
#
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
#
|
6
|
+
# Checks to make sure safe navigation isn't used with `empty?` in
|
7
7
|
# a conditional.
|
8
8
|
#
|
9
9
|
# While the safe navigation operator is generally a good idea, when
|