rubocop 0.48.1 → 1.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +84 -50
- data/assets/output.html.erb +1 -1
- data/config/default.yml +4311 -902
- data/config/obsoletion.yml +200 -0
- data/exe/rubocop +15 -0
- data/lib/rubocop.rb +679 -481
- data/lib/rubocop/ast_aliases.rb +8 -0
- data/lib/rubocop/cached_data.rb +5 -10
- data/lib/rubocop/cli.rb +73 -129
- data/lib/rubocop/cli/command.rb +22 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +132 -0
- data/lib/rubocop/cli/command/base.rb +35 -0
- data/lib/rubocop/cli/command/execute_runner.rb +100 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +44 -0
- data/lib/rubocop/cli/command/show_cops.rb +74 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +81 -0
- data/lib/rubocop/cli/command/version.rb +18 -0
- data/lib/rubocop/cli/environment.rb +22 -0
- data/lib/rubocop/comment_config.rb +80 -78
- data/lib/rubocop/config.rb +124 -259
- data/lib/rubocop/config_loader.rb +182 -133
- data/lib/rubocop/config_loader_resolver.rb +237 -14
- data/lib/rubocop/config_obsoletion.rb +103 -0
- data/lib/rubocop/config_obsoletion/changed_enforced_styles.rb +33 -0
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +21 -0
- data/lib/rubocop/config_obsoletion/cop_rule.rb +33 -0
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +44 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +44 -0
- data/lib/rubocop/config_obsoletion/removed_cop.rb +41 -0
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +34 -0
- data/lib/rubocop/config_obsoletion/rule.rb +41 -0
- data/lib/rubocop/config_obsoletion/split_cop.rb +27 -0
- data/lib/rubocop/config_regeneration.rb +33 -0
- data/lib/rubocop/config_store.rb +31 -7
- data/lib/rubocop/config_validator.rb +234 -0
- data/lib/rubocop/cop/autocorrect_logic.rb +83 -4
- data/lib/rubocop/cop/badge.rb +16 -32
- data/lib/rubocop/cop/base.rb +450 -0
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +44 -16
- data/lib/rubocop/cop/bundler/gem_comment.rb +171 -0
- data/lib/rubocop/cop/bundler/gem_version.rb +99 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +62 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +18 -60
- data/lib/rubocop/cop/commissioner.rb +119 -68
- data/lib/rubocop/cop/cop.rb +93 -169
- data/lib/rubocop/cop/corrector.rb +81 -98
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +136 -0
- data/lib/rubocop/cop/correctors/condition_corrector.rb +25 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +51 -0
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +25 -0
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +72 -0
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +136 -0
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +58 -0
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +97 -0
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +42 -0
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +28 -0
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +106 -0
- data/lib/rubocop/cop/correctors/punctuation_corrector.rb +27 -0
- data/lib/rubocop/cop/correctors/space_corrector.rb +46 -0
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +23 -0
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +40 -0
- data/lib/rubocop/cop/documentation.rb +22 -0
- data/lib/rubocop/cop/exclude_limit.rb +26 -0
- data/lib/rubocop/cop/force.rb +3 -1
- data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +105 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +102 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +113 -0
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +57 -0
- data/lib/rubocop/cop/generator.rb +212 -0
- data/lib/rubocop/cop/generator/configuration_injector.rb +65 -0
- data/lib/rubocop/cop/generator/require_file_injector.rb +75 -0
- data/lib/rubocop/cop/ignored_node.rb +1 -3
- data/lib/rubocop/cop/internal_affairs.rb +15 -0
- data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +92 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +50 -0
- data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +44 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +41 -0
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +56 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +62 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +53 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +76 -0
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +148 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +52 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +100 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +110 -0
- data/lib/rubocop/cop/layout/array_alignment.rb +84 -0
- data/lib/rubocop/cop/{style/indent_assignment.rb → layout/assignment_indentation.rb} +21 -7
- data/lib/rubocop/cop/layout/begin_end_alignment.rb +74 -0
- data/lib/rubocop/cop/{lint → layout}/block_alignment.rb +67 -60
- data/lib/rubocop/cop/layout/block_end_newline.rb +60 -0
- data/lib/rubocop/cop/layout/case_indentation.rb +203 -0
- data/lib/rubocop/cop/layout/class_structure.rb +324 -0
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +124 -0
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +191 -0
- data/lib/rubocop/cop/layout/comment_indentation.rb +137 -0
- data/lib/rubocop/cop/{lint → layout}/condition_position.rb +20 -8
- data/lib/rubocop/cop/{lint → layout}/def_end_alignment.rb +19 -24
- data/lib/rubocop/cop/{style → layout}/dot_position.rb +47 -27
- data/lib/rubocop/cop/layout/else_alignment.rb +156 -0
- data/lib/rubocop/cop/layout/empty_comment.rb +151 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +174 -0
- data/lib/rubocop/cop/{style → layout}/empty_line_after_magic_comment.rb +18 -17
- data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +136 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +252 -0
- data/lib/rubocop/cop/{style → layout}/empty_lines.rb +26 -15
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +206 -0
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +103 -0
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +118 -0
- data/lib/rubocop/cop/{style → layout}/empty_lines_around_begin_body.rb +7 -7
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +38 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +85 -0
- data/lib/rubocop/cop/{style → layout}/empty_lines_around_exception_handling_keywords.rb +16 -17
- data/lib/rubocop/cop/{style → layout}/empty_lines_around_method_body.rb +10 -9
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +59 -0
- data/lib/rubocop/cop/{lint → layout}/end_alignment.rb +59 -33
- data/lib/rubocop/cop/layout/end_of_line.rb +92 -0
- data/lib/rubocop/cop/layout/extra_spacing.rb +182 -0
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +276 -0
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +173 -0
- data/lib/rubocop/cop/{style → layout}/first_array_element_line_break.rb +5 -5
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +200 -0
- data/lib/rubocop/cop/{style → layout}/first_hash_element_line_break.rb +4 -4
- data/lib/rubocop/cop/{style → layout}/first_method_argument_line_break.rb +9 -10
- data/lib/rubocop/cop/{style → layout}/first_method_parameter_line_break.rb +7 -7
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +101 -0
- data/lib/rubocop/cop/layout/hash_alignment.rb +382 -0
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +305 -0
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +172 -0
- data/lib/rubocop/cop/layout/indentation_consistency.rb +205 -0
- data/lib/rubocop/cop/layout/indentation_style.rb +110 -0
- data/lib/rubocop/cop/{style → layout}/indentation_width.rb +138 -66
- data/lib/rubocop/cop/layout/initial_indentation.rb +55 -0
- data/lib/rubocop/cop/layout/leading_comment_space.rb +121 -0
- data/lib/rubocop/cop/layout/leading_empty_lines.rb +48 -0
- data/lib/rubocop/cop/layout/line_length.rb +267 -0
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +115 -0
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +35 -0
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +117 -0
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +159 -0
- data/lib/rubocop/cop/{style → layout}/multiline_hash_brace_layout.rb +53 -19
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +46 -0
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +47 -0
- data/lib/rubocop/cop/{style → layout}/multiline_method_call_brace_layout.rb +69 -35
- data/lib/rubocop/cop/{style → layout}/multiline_method_call_indentation.rb +57 -41
- data/lib/rubocop/cop/{style → layout}/multiline_method_definition_brace_layout.rb +84 -38
- data/lib/rubocop/cop/{style → layout}/multiline_operation_indentation.rb +51 -11
- data/lib/rubocop/cop/layout/parameter_alignment.rb +119 -0
- data/lib/rubocop/cop/layout/redundant_line_break.rb +137 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +198 -0
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +53 -0
- data/lib/rubocop/cop/{style → layout}/space_after_colon.rb +19 -10
- data/lib/rubocop/cop/layout/space_after_comma.rb +32 -0
- data/lib/rubocop/cop/layout/space_after_method_name.rb +39 -0
- data/lib/rubocop/cop/{style → layout}/space_after_not.rb +14 -14
- data/lib/rubocop/cop/{style → layout}/space_after_semicolon.rb +12 -4
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +162 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +88 -0
- data/lib/rubocop/cop/{style → layout}/space_around_keyword.rb +69 -37
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +98 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +241 -0
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +152 -0
- data/lib/rubocop/cop/layout/space_before_brackets.rb +65 -0
- data/lib/rubocop/cop/layout/space_before_comma.rb +29 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +34 -0
- data/lib/rubocop/cop/{style → layout}/space_before_first_arg.rb +24 -17
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +24 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +72 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +222 -0
- data/lib/rubocop/cop/{style → layout}/space_inside_array_percent_literal.rb +10 -20
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +243 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +181 -0
- data/lib/rubocop/cop/layout/space_inside_parens.rb +130 -0
- data/lib/rubocop/cop/{style → layout}/space_inside_percent_literal_delimiters.rb +16 -20
- data/lib/rubocop/cop/{style → layout}/space_inside_range_literal.rb +8 -17
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +139 -0
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +64 -0
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +108 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +101 -0
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +43 -0
- data/lib/rubocop/cop/legacy/corrector.rb +27 -0
- data/lib/rubocop/cop/lint/ambiguous_assignment.rb +54 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +30 -22
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +57 -9
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +37 -7
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +40 -23
- data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +55 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +58 -0
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +5 -16
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +100 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/debugger.rb +63 -33
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +101 -41
- data/lib/rubocop/cop/lint/deprecated_constants.rb +80 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +148 -0
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +87 -0
- data/lib/rubocop/cop/lint/duplicate_branch.rb +154 -0
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +4 -14
- data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
- data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +3 -5
- data/lib/rubocop/cop/lint/duplicate_methods.rb +108 -24
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
- data/lib/rubocop/cop/lint/duplicate_require.rb +47 -0
- data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +47 -0
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +9 -4
- data/lib/rubocop/cop/lint/else_layout.rb +33 -8
- data/lib/rubocop/cop/lint/empty_block.rb +98 -0
- data/lib/rubocop/cop/lint/empty_class.rb +93 -0
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +67 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +6 -8
- data/lib/rubocop/cop/lint/empty_expression.rb +3 -3
- data/lib/rubocop/cop/lint/empty_file.rb +48 -0
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
- data/lib/rubocop/cop/lint/empty_interpolation.rb +8 -11
- data/lib/rubocop/cop/lint/empty_when.rb +32 -9
- data/lib/rubocop/cop/lint/ensure_return.rb +31 -15
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +162 -0
- data/lib/rubocop/cop/lint/flip_flop.rb +38 -0
- data/lib/rubocop/cop/lint/float_comparison.rb +93 -0
- data/lib/rubocop/cop/lint/float_out_of_range.rb +6 -7
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +93 -81
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +38 -0
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +154 -0
- data/lib/rubocop/cop/lint/identity_comparison.rb +51 -0
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +27 -21
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +44 -66
- data/lib/rubocop/cop/lint/inherit_exception.rb +43 -21
- data/lib/rubocop/cop/lint/interpolation_check.rb +53 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +44 -0
- data/lib/rubocop/cop/lint/{literal_in_condition.rb → literal_as_condition.rb} +48 -28
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +71 -20
- data/lib/rubocop/cop/lint/loop.rb +31 -9
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +81 -0
- data/lib/rubocop/cop/lint/missing_super.rb +101 -0
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +36 -0
- data/lib/rubocop/cop/lint/multiple_comparison.rb +48 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +23 -28
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +63 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +5 -4
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +169 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +20 -19
- data/lib/rubocop/cop/lint/number_conversion.rb +155 -0
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -0
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +83 -0
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +96 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +40 -17
- data/lib/rubocop/cop/lint/percent_string_array.rb +18 -34
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +19 -17
- data/lib/rubocop/cop/lint/raise_exception.rb +87 -0
- data/lib/rubocop/cop/lint/rand_one.rb +13 -6
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +262 -0
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +128 -0
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +50 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +49 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +77 -0
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +209 -0
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +52 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +78 -0
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +31 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +11 -8
- data/lib/rubocop/cop/lint/rescue_exception.rb +5 -5
- data/lib/rubocop/cop/lint/rescue_type.rb +88 -0
- data/lib/rubocop/cop/lint/return_in_void_context.rb +71 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +22 -30
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +92 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +46 -0
- data/lib/rubocop/cop/lint/script_permission.rb +73 -0
- data/lib/rubocop/cop/lint/self_assignment.rb +78 -0
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +83 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +176 -0
- data/lib/rubocop/cop/lint/shadowed_exception.rb +77 -43
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +24 -10
- data/lib/rubocop/cop/lint/struct_new_override.rb +59 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +132 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +178 -0
- data/lib/rubocop/cop/lint/syntax.rb +17 -32
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +89 -0
- data/lib/rubocop/cop/lint/to_json.rb +49 -0
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +34 -0
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +55 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +71 -0
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +31 -13
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
- data/lib/rubocop/cop/lint/unified_integer.rb +10 -10
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +204 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +57 -12
- data/lib/rubocop/cop/lint/unreachable_loop.rb +196 -0
- data/lib/rubocop/cop/lint/unused_block_argument.rb +41 -14
- data/lib/rubocop/cop/lint/unused_method_argument.rb +70 -14
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +79 -0
- data/lib/rubocop/cop/lint/uri_regexp.rb +38 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +120 -59
- data/lib/rubocop/cop/lint/useless_assignment.rb +13 -20
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +10 -11
- data/lib/rubocop/cop/lint/useless_method_definition.rb +68 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +30 -22
- data/lib/rubocop/cop/lint/useless_times.rb +109 -0
- data/lib/rubocop/cop/lint/void.rb +82 -50
- data/lib/rubocop/cop/message_annotator.rb +48 -24
- data/lib/rubocop/cop/metrics/abc_size.rb +29 -20
- data/lib/rubocop/cop/metrics/block_length.rb +54 -6
- data/lib/rubocop/cop/metrics/block_nesting.rb +7 -11
- data/lib/rubocop/cop/metrics/class_length.rb +44 -3
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +38 -5
- data/lib/rubocop/cop/metrics/method_length.rb +43 -6
- data/lib/rubocop/cop/metrics/module_length.rb +35 -3
- data/lib/rubocop/cop/metrics/parameter_lists.rb +80 -8
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +8 -10
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +145 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +160 -0
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +143 -0
- data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +42 -0
- data/lib/rubocop/cop/migration/department_name.rb +79 -0
- data/lib/rubocop/cop/mixin/alignment.rb +81 -0
- data/lib/rubocop/cop/mixin/allowed_identifiers.rb +18 -0
- data/lib/rubocop/cop/mixin/allowed_methods.rb +21 -0
- data/lib/rubocop/cop/mixin/annotation_comment.rb +6 -0
- data/lib/rubocop/cop/mixin/array_min_size.rb +57 -0
- data/lib/rubocop/cop/mixin/array_syntax.rb +3 -1
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/check_assignment.rb +13 -9
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +214 -0
- data/lib/rubocop/cop/mixin/code_length.rb +33 -6
- data/lib/rubocop/cop/mixin/comments_help.rb +42 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +20 -18
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +5 -13
- data/lib/rubocop/cop/mixin/configurable_max.rb +7 -2
- data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
- data/lib/rubocop/cop/mixin/def_node.rb +12 -8
- data/lib/rubocop/cop/mixin/documentation_comment.rb +13 -10
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +45 -34
- data/lib/rubocop/cop/mixin/empty_parameter.rb +25 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +29 -33
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +38 -0
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +17 -12
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +35 -10
- data/lib/rubocop/cop/mixin/gem_declaration.rb +13 -0
- data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +55 -16
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +183 -0
- data/lib/rubocop/cop/mixin/heredoc.rb +32 -0
- data/lib/rubocop/cop/mixin/ignored_methods.rb +52 -0
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +3 -1
- data/lib/rubocop/cop/mixin/integer_node.rb +3 -1
- data/lib/rubocop/cop/mixin/interpolation.rb +25 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +90 -0
- data/lib/rubocop/cop/mixin/match_range.rb +6 -5
- data/lib/rubocop/cop/mixin/method_complexity.rb +58 -11
- data/lib/rubocop/cop/mixin/method_preference.rb +1 -1
- data/lib/rubocop/cop/mixin/min_body_length.rb +3 -1
- data/lib/rubocop/cop/mixin/{array_hash_indentation.rb → multiline_element_indentation.rb} +11 -6
- data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +33 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +108 -61
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +31 -42
- data/lib/rubocop/cop/mixin/negative_conditional.rb +9 -18
- data/lib/rubocop/cop/mixin/nil_methods.rb +23 -0
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +58 -0
- data/lib/rubocop/cop/mixin/parentheses.rb +3 -9
- data/lib/rubocop/cop/mixin/percent_array.rb +57 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +3 -71
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +95 -9
- data/lib/rubocop/cop/mixin/preferred_delimiters.rb +50 -0
- data/lib/rubocop/cop/mixin/range_help.rb +128 -0
- data/lib/rubocop/cop/mixin/rational_literal.rb +19 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +15 -8
- data/lib/rubocop/cop/mixin/safe_assignment.rb +9 -2
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +15 -16
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +19 -18
- data/lib/rubocop/cop/mixin/statement_modifier.rb +50 -25
- data/lib/rubocop/cop/mixin/string_help.rb +6 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +5 -17
- data/lib/rubocop/cop/mixin/surrounding_space.rb +113 -23
- data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
- data/lib/rubocop/cop/mixin/trailing_body.rb +25 -0
- data/lib/rubocop/cop/mixin/trailing_comma.rb +100 -52
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +104 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +6 -19
- data/lib/rubocop/cop/mixin/visibility_help.rb +37 -0
- data/lib/rubocop/cop/naming/accessor_method_name.rb +71 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +93 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +53 -0
- data/lib/rubocop/cop/naming/block_parameter_name.rb +49 -0
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +43 -0
- data/lib/rubocop/cop/naming/constant_name.rb +84 -0
- data/lib/rubocop/cop/{style → naming}/file_name.rb +79 -48
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +55 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +279 -0
- data/lib/rubocop/cop/naming/method_name.rb +81 -0
- data/lib/rubocop/cop/naming/method_parameter_name.rb +58 -0
- data/lib/rubocop/cop/naming/predicate_name.rb +105 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +156 -0
- data/lib/rubocop/cop/naming/variable_name.rb +54 -0
- data/lib/rubocop/cop/naming/variable_number.rb +146 -0
- data/lib/rubocop/cop/offense.rb +74 -23
- data/lib/rubocop/cop/registry.rb +140 -23
- data/lib/rubocop/cop/security/eval.rb +14 -5
- data/lib/rubocop/cop/security/json_load.rb +14 -12
- data/lib/rubocop/cop/security/marshal_load.rb +9 -7
- data/lib/rubocop/cop/security/open.rb +75 -0
- data/lib/rubocop/cop/security/yaml_load.rb +12 -10
- data/lib/rubocop/cop/severity.rb +5 -14
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +136 -0
- data/lib/rubocop/cop/style/accessor_grouping.rb +150 -0
- data/lib/rubocop/cop/style/alias.rb +86 -57
- data/lib/rubocop/cop/style/and_or.rb +81 -53
- data/lib/rubocop/cop/style/arguments_forwarding.rb +143 -0
- data/lib/rubocop/cop/style/array_coercion.rb +69 -0
- data/lib/rubocop/cop/style/array_join.rb +19 -10
- data/lib/rubocop/cop/style/ascii_comments.rb +31 -9
- data/lib/rubocop/cop/style/attr.rb +33 -13
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +19 -13
- data/lib/rubocop/cop/style/bare_percent_literals.rb +31 -12
- data/lib/rubocop/cop/style/begin_block.rb +9 -3
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +125 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +60 -0
- data/lib/rubocop/cop/style/block_comments.rb +40 -20
- data/lib/rubocop/cop/style/block_delimiters.rb +213 -59
- data/lib/rubocop/cop/style/case_equality.rb +67 -4
- data/lib/rubocop/cop/style/case_like_if.rb +258 -0
- data/lib/rubocop/cop/style/character_literal.rb +22 -15
- data/lib/rubocop/cop/style/class_and_module_children.rb +118 -17
- data/lib/rubocop/cop/style/class_check.rb +31 -17
- data/lib/rubocop/cop/style/class_equality_comparison.rb +67 -0
- data/lib/rubocop/cop/style/class_methods.rb +18 -31
- data/lib/rubocop/cop/style/class_methods_definitions.rb +157 -0
- data/lib/rubocop/cop/style/class_vars.rb +48 -7
- data/lib/rubocop/cop/style/collection_compact.rb +91 -0
- data/lib/rubocop/cop/style/collection_methods.rb +52 -17
- data/lib/rubocop/cop/style/colon_method_call.rb +26 -13
- data/lib/rubocop/cop/style/colon_method_definition.rb +37 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +92 -0
- data/lib/rubocop/cop/style/command_literal.rb +104 -42
- data/lib/rubocop/cop/style/comment_annotation.rb +62 -24
- data/lib/rubocop/cop/style/commented_keyword.rb +86 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +143 -182
- data/lib/rubocop/cop/style/constant_visibility.rb +106 -0
- data/lib/rubocop/cop/style/copyright.rb +42 -35
- data/lib/rubocop/cop/style/date_time.rb +88 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +36 -13
- data/lib/rubocop/cop/style/dir.rb +47 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +87 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
- data/lib/rubocop/cop/style/documentation.rb +114 -22
- data/lib/rubocop/cop/style/documentation_method.rb +56 -10
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +52 -0
- data/lib/rubocop/cop/style/double_negation.rb +52 -8
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +16 -18
- data/lib/rubocop/cop/style/each_with_object.rb +34 -17
- data/lib/rubocop/cop/style/empty_block_parameter.rb +47 -0
- data/lib/rubocop/cop/style/empty_case_condition.rb +32 -13
- data/lib/rubocop/cop/style/empty_else.rb +56 -27
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +44 -0
- data/lib/rubocop/cop/style/empty_literal.rb +63 -33
- data/lib/rubocop/cop/style/empty_method.rb +37 -35
- data/lib/rubocop/cop/style/encoding.rb +20 -60
- data/lib/rubocop/cop/style/end_block.rb +15 -4
- data/lib/rubocop/cop/style/endless_method.rb +101 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +237 -0
- data/lib/rubocop/cop/style/even_odd.rb +16 -16
- data/lib/rubocop/cop/style/expand_path_arguments.rb +196 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +114 -0
- data/lib/rubocop/cop/style/exponential_notation.rb +116 -0
- data/lib/rubocop/cop/style/float_division.rb +143 -0
- data/lib/rubocop/cop/style/for.rb +49 -17
- data/lib/rubocop/cop/style/format_string.rb +97 -10
- data/lib/rubocop/cop/style/format_string_token.rb +177 -0
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +155 -47
- data/lib/rubocop/cop/style/global_std_stream.rb +66 -0
- data/lib/rubocop/cop/style/global_vars.rb +16 -6
- data/lib/rubocop/cop/style/guard_clause.rb +69 -18
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +88 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +133 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +103 -0
- data/lib/rubocop/cop/style/hash_except.rb +96 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +77 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +99 -94
- data/lib/rubocop/cop/style/hash_transform_keys.rb +95 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +92 -0
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +53 -29
- data/lib/rubocop/cop/style/if_inside_else.rb +101 -11
- data/lib/rubocop/cop/style/if_unless_modifier.rb +162 -44
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +13 -4
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +62 -4
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +10 -7
- data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
- data/lib/rubocop/cop/style/infinite_loop.rb +65 -27
- data/lib/rubocop/cop/style/inline_comment.rb +7 -5
- data/lib/rubocop/cop/style/inverse_methods.rb +99 -44
- data/lib/rubocop/cop/style/ip_addresses.rb +75 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +75 -0
- data/lib/rubocop/cop/style/lambda.rb +26 -110
- data/lib/rubocop/cop/style/lambda_call.rb +29 -19
- data/lib/rubocop/cop/style/line_end_concatenation.rb +40 -34
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +185 -52
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +189 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +44 -0
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +27 -18
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +19 -10
- data/lib/rubocop/cop/style/method_def_parentheses.rb +122 -45
- data/lib/rubocop/cop/style/min_max.rb +64 -0
- data/lib/rubocop/cop/style/missing_else.rb +88 -20
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +52 -0
- data/lib/rubocop/cop/style/mixin_grouping.rb +66 -28
- data/lib/rubocop/cop/style/mixin_usage.rb +72 -0
- data/lib/rubocop/cop/style/module_function.rb +111 -26
- data/lib/rubocop/cop/style/multiline_block_chain.rb +21 -15
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +8 -11
- data/lib/rubocop/cop/style/multiline_if_then.rb +14 -17
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +44 -25
- data/lib/rubocop/cop/style/multiline_method_signature.rb +91 -0
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +45 -5
- data/lib/rubocop/cop/style/multiline_when_then.rb +75 -0
- data/lib/rubocop/cop/style/multiple_comparison.rb +157 -0
- data/lib/rubocop/cop/style/mutable_constant.rb +132 -18
- data/lib/rubocop/cop/style/negated_if.rb +33 -38
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +128 -0
- data/lib/rubocop/cop/style/negated_unless.rb +88 -0
- data/lib/rubocop/cop/style/negated_while.rb +25 -17
- data/lib/rubocop/cop/style/nested_modifier.rb +36 -23
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +38 -29
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +41 -4
- data/lib/rubocop/cop/style/next.rb +77 -67
- data/lib/rubocop/cop/style/nil_comparison.rb +62 -14
- data/lib/rubocop/cop/style/nil_lambda.rb +70 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +100 -63
- data/lib/rubocop/cop/style/not.rb +38 -31
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +47 -25
- data/lib/rubocop/cop/style/numeric_literals.rb +40 -35
- data/lib/rubocop/cop/style/numeric_predicate.rb +33 -43
- data/lib/rubocop/cop/style/one_line_conditional.rb +100 -28
- data/lib/rubocop/cop/style/option_hash.rb +21 -25
- data/lib/rubocop/cop/style/optional_arguments.rb +4 -11
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +53 -0
- data/lib/rubocop/cop/style/or_assignment.rb +97 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +42 -48
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +76 -14
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +42 -23
- data/lib/rubocop/cop/style/percent_q_literals.rb +31 -13
- data/lib/rubocop/cop/style/perl_backrefs.rb +91 -16
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +15 -24
- data/lib/rubocop/cop/style/proc.rb +18 -10
- data/lib/rubocop/cop/style/quoted_symbols.rb +105 -0
- data/lib/rubocop/cop/style/raise_args.rb +53 -46
- data/lib/rubocop/cop/style/random_with_offset.rb +153 -0
- data/lib/rubocop/cop/style/redundant_argument.rb +85 -0
- data/lib/rubocop/cop/style/redundant_assignment.rb +106 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +119 -9
- data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +17 -11
- data/lib/rubocop/cop/style/redundant_condition.rb +137 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +96 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +28 -18
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +115 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +52 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +37 -13
- data/lib/rubocop/cop/style/redundant_interpolation.rb +103 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +99 -54
- data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +39 -26
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +104 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +118 -0
- data/lib/rubocop/cop/style/redundant_return.rb +109 -59
- data/lib/rubocop/cop/style/redundant_self.rb +75 -50
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +118 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +160 -0
- data/lib/rubocop/cop/{performance → style}/redundant_sort_by.rb +15 -13
- data/lib/rubocop/cop/style/regexp_literal.rb +142 -43
- data/lib/rubocop/cop/style/rescue_modifier.rb +77 -14
- data/lib/rubocop/cop/style/rescue_standard_error.rb +126 -0
- data/lib/rubocop/cop/style/return_nil.rb +94 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +193 -58
- data/lib/rubocop/cop/{performance → style}/sample.rb +29 -26
- data/lib/rubocop/cop/style/self_assignment.rb +24 -16
- data/lib/rubocop/cop/style/semicolon.rb +48 -17
- data/lib/rubocop/cop/style/send.rb +15 -7
- data/lib/rubocop/cop/style/signal_exception.rb +134 -27
- data/lib/rubocop/cop/style/single_argument_dig.rb +55 -0
- data/lib/rubocop/cop/style/single_line_block_params.rb +72 -23
- data/lib/rubocop/cop/style/single_line_methods.rb +95 -37
- data/lib/rubocop/cop/style/slicing_with_range.rb +38 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +176 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +80 -58
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +35 -69
- data/lib/rubocop/cop/style/static_class.rb +97 -0
- data/lib/rubocop/cop/style/stderr_puts.rb +57 -0
- data/lib/rubocop/cop/style/string_chars.rb +39 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +142 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +51 -0
- data/lib/rubocop/cop/style/string_literals.rb +44 -13
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +24 -3
- data/lib/rubocop/cop/style/string_methods.rb +17 -15
- data/lib/rubocop/cop/{performance/lstrip_rstrip.rb → style/strip.rb} +18 -17
- data/lib/rubocop/cop/style/struct_inheritance.rb +45 -9
- data/lib/rubocop/cop/style/swap_values.rb +108 -0
- data/lib/rubocop/cop/style/symbol_array.rb +56 -46
- data/lib/rubocop/cop/style/symbol_literal.rb +6 -10
- data/lib/rubocop/cop/style/symbol_proc.rb +63 -78
- data/lib/rubocop/cop/style/ternary_parentheses.rb +95 -77
- data/lib/rubocop/cop/style/top_level_method_definition.rb +83 -0
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +40 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +55 -0
- data/lib/rubocop/cop/style/trailing_body_on_module.rb +40 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +69 -27
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +96 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +86 -0
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +97 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +65 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +79 -40
- data/lib/rubocop/cop/style/trivial_accessors.rb +158 -82
- data/lib/rubocop/cop/style/unless_else.rb +25 -13
- data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
- data/lib/rubocop/cop/style/unpack_first.rb +59 -0
- data/lib/rubocop/cop/style/variable_interpolation.rb +26 -25
- data/lib/rubocop/cop/style/when_then.rb +20 -7
- data/lib/rubocop/cop/style/while_until_do.rb +31 -18
- data/lib/rubocop/cop/style/while_until_modifier.rb +36 -24
- data/lib/rubocop/cop/style/word_array.rb +40 -91
- data/lib/rubocop/cop/style/yoda_condition.rb +155 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +60 -31
- data/lib/rubocop/cop/team.rb +171 -105
- data/lib/rubocop/cop/util.rb +44 -194
- data/lib/rubocop/cop/utils/format_string.rb +133 -0
- data/lib/rubocop/cop/variable_force.rb +47 -51
- data/lib/rubocop/cop/variable_force/assignment.rb +10 -4
- data/lib/rubocop/cop/variable_force/branch.rb +22 -9
- data/lib/rubocop/cop/variable_force/branchable.rb +2 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -3
- data/lib/rubocop/cop/variable_force/scope.rb +13 -14
- data/lib/rubocop/cop/variable_force/variable.rb +25 -14
- data/lib/rubocop/cop/variable_force/variable_table.rb +4 -3
- data/lib/rubocop/cops_documentation_generator.rb +270 -0
- data/lib/rubocop/core_ext/string.rb +23 -0
- data/lib/rubocop/directive_comment.rb +96 -0
- data/lib/rubocop/error.rb +24 -0
- data/lib/rubocop/ext/processed_source.rb +18 -0
- data/lib/rubocop/ext/regexp_node.rb +87 -0
- data/lib/rubocop/ext/regexp_parser.rb +92 -0
- data/lib/rubocop/file_finder.rb +41 -0
- data/lib/rubocop/formatter/auto_gen_config_formatter.rb +17 -0
- data/lib/rubocop/formatter/base_formatter.rb +3 -7
- data/lib/rubocop/formatter/clang_style_formatter.rb +13 -8
- data/lib/rubocop/formatter/disabled_config_formatter.rb +110 -58
- data/lib/rubocop/formatter/emacs_style_formatter.rb +24 -9
- data/lib/rubocop/formatter/file_list_formatter.rb +2 -1
- data/lib/rubocop/formatter/formatter_set.rb +20 -16
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -2
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +44 -0
- data/lib/rubocop/formatter/html_formatter.rb +18 -15
- data/lib/rubocop/formatter/json_formatter.rb +17 -14
- data/lib/rubocop/formatter/junit_formatter.rb +93 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +21 -2
- data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
- data/lib/rubocop/formatter/progress_formatter.rb +4 -5
- data/lib/rubocop/formatter/quiet_formatter.rb +13 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +61 -26
- data/lib/rubocop/formatter/tap_formatter.rb +86 -0
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -2
- data/lib/rubocop/lockfile.rb +40 -0
- data/lib/rubocop/magic_comment.rb +45 -12
- data/lib/rubocop/name_similarity.rb +18 -10
- data/lib/rubocop/options.rb +311 -140
- data/lib/rubocop/path_util.rb +57 -5
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rake_task.rb +27 -32
- data/lib/rubocop/remote_config.rb +32 -8
- data/lib/rubocop/result_cache.rb +89 -39
- data/lib/rubocop/rspec/cop_helper.rb +18 -46
- data/lib/rubocop/rspec/expect_offense.rb +329 -0
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +1 -5
- data/lib/rubocop/rspec/shared_contexts.rb +92 -53
- data/lib/rubocop/rspec/support.rb +9 -4
- data/lib/rubocop/runner.rb +174 -86
- data/lib/rubocop/string_interpreter.rb +11 -8
- data/lib/rubocop/target_finder.rb +102 -93
- data/lib/rubocop/target_ruby.rb +264 -0
- data/lib/rubocop/util.rb +16 -0
- data/lib/rubocop/version.rb +71 -5
- data/lib/rubocop/yaml_duplication_checker.rb +40 -0
- metadata +495 -233
- data/bin/rubocop +0 -17
- data/config/disabled.yml +0 -119
- data/config/enabled.yml +0 -1713
- data/lib/rubocop/ast/builder.rb +0 -64
- data/lib/rubocop/ast/node.rb +0 -610
- data/lib/rubocop/ast/node/and_node.rb +0 -37
- data/lib/rubocop/ast/node/array_node.rb +0 -48
- data/lib/rubocop/ast/node/case_node.rb +0 -64
- data/lib/rubocop/ast/node/ensure_node.rb +0 -25
- data/lib/rubocop/ast/node/for_node.rb +0 -53
- data/lib/rubocop/ast/node/hash_node.rb +0 -109
- data/lib/rubocop/ast/node/if_node.rb +0 -138
- data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -23
- data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
- data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
- data/lib/rubocop/ast/node/or_node.rb +0 -37
- data/lib/rubocop/ast/node/pair_node.rb +0 -64
- data/lib/rubocop/ast/node/resbody_node.rb +0 -25
- data/lib/rubocop/ast/node/send_node.rb +0 -195
- data/lib/rubocop/ast/node/until_node.rb +0 -43
- data/lib/rubocop/ast/node/when_node.rb +0 -61
- data/lib/rubocop/ast/node/while_node.rb +0 -43
- data/lib/rubocop/ast/sexp.rb +0 -16
- data/lib/rubocop/ast/traversal.rb +0 -171
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -56
- data/lib/rubocop/cop/lint/invalid_character_literal.rb +0 -41
- data/lib/rubocop/cop/lint/multiple_compare.rb +0 -48
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +0 -58
- data/lib/rubocop/cop/lint/unneeded_disable.rb +0 -231
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +0 -141
- data/lib/rubocop/cop/lint/useless_comparison.rb +0 -28
- data/lib/rubocop/cop/metrics/line_length.rb +0 -168
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +0 -149
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -36
- data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
- data/lib/rubocop/cop/mixin/parser_diagnostic.rb +0 -34
- data/lib/rubocop/cop/mixin/safe_mode.rb +0 -22
- data/lib/rubocop/cop/mixin/space_inside.rb +0 -76
- data/lib/rubocop/cop/mixin/target_rails_version.rb +0 -16
- data/lib/rubocop/cop/mixin/too_many_lines.rb +0 -39
- data/lib/rubocop/cop/performance/case_when_splat.rb +0 -176
- data/lib/rubocop/cop/performance/casecmp.rb +0 -107
- data/lib/rubocop/cop/performance/compare_with_block.rb +0 -61
- data/lib/rubocop/cop/performance/count.rb +0 -98
- data/lib/rubocop/cop/performance/detect.rb +0 -107
- data/lib/rubocop/cop/performance/double_start_end_with.rb +0 -102
- data/lib/rubocop/cop/performance/end_with.rb +0 -55
- data/lib/rubocop/cop/performance/fixed_size.rb +0 -56
- data/lib/rubocop/cop/performance/flat_map.rb +0 -73
- data/lib/rubocop/cop/performance/hash_each_methods.rb +0 -84
- data/lib/rubocop/cop/performance/range_include.rb +0 -41
- data/lib/rubocop/cop/performance/redundant_block_call.rb +0 -93
- data/lib/rubocop/cop/performance/redundant_match.rb +0 -55
- data/lib/rubocop/cop/performance/redundant_merge.rb +0 -149
- data/lib/rubocop/cop/performance/regexp_match.rb +0 -215
- data/lib/rubocop/cop/performance/reverse_each.rb +0 -40
- data/lib/rubocop/cop/performance/size.rb +0 -71
- data/lib/rubocop/cop/performance/start_with.rb +0 -58
- data/lib/rubocop/cop/performance/string_replacement.rb +0 -170
- data/lib/rubocop/cop/performance/times_map.rb +0 -61
- data/lib/rubocop/cop/rails/action_filter.rb +0 -96
- data/lib/rubocop/cop/rails/active_support_aliases.rb +0 -68
- data/lib/rubocop/cop/rails/blank.rb +0 -132
- data/lib/rubocop/cop/rails/date.rb +0 -127
- data/lib/rubocop/cop/rails/delegate.rb +0 -106
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +0 -51
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +0 -81
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +0 -43
- data/lib/rubocop/cop/rails/exit.rb +0 -61
- data/lib/rubocop/cop/rails/file_path.rb +0 -75
- data/lib/rubocop/cop/rails/find_by.rb +0 -51
- data/lib/rubocop/cop/rails/find_each.rb +0 -47
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +0 -18
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +0 -106
- data/lib/rubocop/cop/rails/not_null_column.rb +0 -67
- data/lib/rubocop/cop/rails/output.rb +0 -23
- data/lib/rubocop/cop/rails/output_safety.rb +0 -54
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +0 -106
- data/lib/rubocop/cop/rails/present.rb +0 -137
- data/lib/rubocop/cop/rails/read_write_attribute.rb +0 -65
- data/lib/rubocop/cop/rails/relative_date_constant.rb +0 -57
- data/lib/rubocop/cop/rails/request_referer.rb +0 -56
- data/lib/rubocop/cop/rails/reversible_migration.rb +0 -216
- data/lib/rubocop/cop/rails/safe_navigation.rb +0 -91
- data/lib/rubocop/cop/rails/save_bang.rb +0 -155
- data/lib/rubocop/cop/rails/scope_args.rb +0 -29
- data/lib/rubocop/cop/rails/skips_model_validations.rb +0 -63
- data/lib/rubocop/cop/rails/time_zone.rb +0 -197
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +0 -93
- data/lib/rubocop/cop/rails/validation.rb +0 -64
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +0 -80
- data/lib/rubocop/cop/style/accessor_method_name.rb +0 -45
- data/lib/rubocop/cop/style/align_array.rb +0 -20
- data/lib/rubocop/cop/style/align_hash.rb +0 -235
- data/lib/rubocop/cop/style/align_parameters.rb +0 -69
- data/lib/rubocop/cop/style/ascii_identifiers.rb +0 -36
- data/lib/rubocop/cop/style/block_end_newline.rb +0 -56
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -138
- data/lib/rubocop/cop/style/case_indentation.rb +0 -100
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -29
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +0 -88
- data/lib/rubocop/cop/style/comment_indentation.rb +0 -71
- data/lib/rubocop/cop/style/constant_name.rb +0 -29
- data/lib/rubocop/cop/style/else_alignment.rb +0 -105
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -143
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +0 -95
- data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +0 -41
- data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +0 -39
- data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +0 -44
- data/lib/rubocop/cop/style/end_of_line.rb +0 -52
- data/lib/rubocop/cop/style/extra_spacing.rb +0 -237
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +0 -109
- data/lib/rubocop/cop/style/flip_flop.rb +0 -20
- data/lib/rubocop/cop/style/indent_array.rb +0 -114
- data/lib/rubocop/cop/style/indent_hash.rb +0 -134
- data/lib/rubocop/cop/style/indent_heredoc.rb +0 -173
- data/lib/rubocop/cop/style/indentation_consistency.rb +0 -51
- data/lib/rubocop/cop/style/initial_indentation.rb +0 -42
- data/lib/rubocop/cop/style/leading_comment_space.rb +0 -43
- data/lib/rubocop/cop/style/method_missing.rb +0 -81
- data/lib/rubocop/cop/style/method_name.rb +0 -28
- data/lib/rubocop/cop/style/multiline_array_brace_layout.rb +0 -81
- data/lib/rubocop/cop/style/multiline_assignment_layout.rb +0 -88
- data/lib/rubocop/cop/style/multiline_block_layout.rb +0 -134
- data/lib/rubocop/cop/style/op_method.rb +0 -41
- data/lib/rubocop/cop/style/predicate_name.rb +0 -67
- data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +0 -86
- data/lib/rubocop/cop/style/space_after_comma.rb +0 -21
- data/lib/rubocop/cop/style/space_after_method_name.rb +0 -37
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +0 -109
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +0 -68
- data/lib/rubocop/cop/style/space_around_operators.rb +0 -142
- data/lib/rubocop/cop/style/space_before_block_braces.rb +0 -54
- data/lib/rubocop/cop/style/space_before_comma.rb +0 -16
- data/lib/rubocop/cop/style/space_before_comment.rb +0 -27
- data/lib/rubocop/cop/style/space_before_semicolon.rb +0 -16
- data/lib/rubocop/cop/style/space_in_lambda_literal.rb +0 -87
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +0 -158
- data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -20
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +0 -150
- data/lib/rubocop/cop/style/space_inside_parens.rb +0 -16
- data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +0 -65
- data/lib/rubocop/cop/style/tab.rb +0 -57
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +0 -78
- data/lib/rubocop/cop/style/trailing_comma_in_literal.rb +0 -56
- data/lib/rubocop/cop/style/trailing_whitespace.rb +0 -28
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +0 -98
- data/lib/rubocop/cop/style/variable_name.rb +0 -39
- data/lib/rubocop/cop/style/variable_number.rb +0 -78
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
- data/lib/rubocop/node_pattern.rb +0 -543
- data/lib/rubocop/processed_source.rb +0 -151
- data/lib/rubocop/rspec/shared_examples.rb +0 -100
- data/lib/rubocop/string_util.rb +0 -156
- data/lib/rubocop/token.rb +0 -25
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop enforces consistency between 'return nil' and 'return'.
|
7
|
+
#
|
8
|
+
# Supported styles are: return, return_nil.
|
9
|
+
#
|
10
|
+
# @example EnforcedStyle: return (default)
|
11
|
+
# # bad
|
12
|
+
# def foo(arg)
|
13
|
+
# return nil if arg
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# def foo(arg)
|
18
|
+
# return if arg
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# @example EnforcedStyle: return_nil
|
22
|
+
# # bad
|
23
|
+
# def foo(arg)
|
24
|
+
# return if arg
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# # good
|
28
|
+
# def foo(arg)
|
29
|
+
# return nil if arg
|
30
|
+
# end
|
31
|
+
class ReturnNil < Base
|
32
|
+
include ConfigurableEnforcedStyle
|
33
|
+
extend AutoCorrector
|
34
|
+
|
35
|
+
RETURN_MSG = 'Use `return` instead of `return nil`.'
|
36
|
+
RETURN_NIL_MSG = 'Use `return nil` instead of `return`.'
|
37
|
+
|
38
|
+
# @!method return_node?(node)
|
39
|
+
def_node_matcher :return_node?, '(return)'
|
40
|
+
|
41
|
+
# @!method return_nil_node?(node)
|
42
|
+
def_node_matcher :return_nil_node?, '(return nil)'
|
43
|
+
|
44
|
+
def on_return(node)
|
45
|
+
# Check Lint/NonLocalExitFromIterator first before this cop
|
46
|
+
node.each_ancestor(:block, :def, :defs) do |n|
|
47
|
+
break if scoped_node?(n)
|
48
|
+
|
49
|
+
send_node, args_node, _body_node = *n
|
50
|
+
|
51
|
+
# if a proc is passed to `Module#define_method` or
|
52
|
+
# `Object#define_singleton_method`, `return` will not cause a
|
53
|
+
# non-local exit error
|
54
|
+
break if define_method?(send_node)
|
55
|
+
|
56
|
+
next if args_node.children.empty?
|
57
|
+
|
58
|
+
return nil if chained_send?(send_node)
|
59
|
+
end
|
60
|
+
|
61
|
+
return if correct_style?(node)
|
62
|
+
|
63
|
+
add_offense(node) do |corrector|
|
64
|
+
corrected = style == :return ? 'return' : 'return nil'
|
65
|
+
|
66
|
+
corrector.replace(node, corrected)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def message(_node)
|
73
|
+
style == :return ? RETURN_MSG : RETURN_NIL_MSG
|
74
|
+
end
|
75
|
+
|
76
|
+
def correct_style?(node)
|
77
|
+
style == :return && !return_nil_node?(node) || style == :return_nil && !return_node?(node)
|
78
|
+
end
|
79
|
+
|
80
|
+
def scoped_node?(node)
|
81
|
+
node.def_type? || node.defs_type? || node.lambda?
|
82
|
+
end
|
83
|
+
|
84
|
+
# @!method chained_send?(node)
|
85
|
+
def_node_matcher :chained_send?, '(send !nil? ...)'
|
86
|
+
|
87
|
+
# @!method define_method?(node)
|
88
|
+
def_node_matcher :define_method?, <<~PATTERN
|
89
|
+
(send _ {:define_method :define_singleton_method} _)
|
90
|
+
PATTERN
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -5,7 +5,10 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop transforms usages of a method call safeguarded by a non `nil`
|
7
7
|
# check for the variable whose method is being called to
|
8
|
-
# safe navigation (`&.`).
|
8
|
+
# safe navigation (`&.`). If there is a method chain, all of the methods
|
9
|
+
# in the chain need to be checked for safety, and all of the methods will
|
10
|
+
# need to be changed to use safe navigation. We have limited the cop to
|
11
|
+
# not register an offense for method chains that exceed 2 methods.
|
9
12
|
#
|
10
13
|
# Configuration option: ConvertCodeThatCanStartToReturnNil
|
11
14
|
# The default for this is `false`. When configured to `true`, this will
|
@@ -18,6 +21,7 @@ module RuboCop
|
|
18
21
|
# @example
|
19
22
|
# # bad
|
20
23
|
# foo.bar if foo
|
24
|
+
# foo.bar.baz if foo
|
21
25
|
# foo.bar(param1, param2) if foo
|
22
26
|
# foo.bar { |e| e.something } if foo
|
23
27
|
# foo.bar(param) { |e| e.something } if foo
|
@@ -27,53 +31,68 @@ module RuboCop
|
|
27
31
|
# foo.bar unless foo.nil?
|
28
32
|
#
|
29
33
|
# foo && foo.bar
|
34
|
+
# foo && foo.bar.baz
|
30
35
|
# foo && foo.bar(param1, param2)
|
31
36
|
# foo && foo.bar { |e| e.something }
|
32
37
|
# foo && foo.bar(param) { |e| e.something }
|
33
38
|
#
|
34
39
|
# # good
|
35
40
|
# foo&.bar
|
41
|
+
# foo&.bar&.baz
|
36
42
|
# foo&.bar(param1, param2)
|
37
43
|
# foo&.bar { |e| e.something }
|
38
44
|
# foo&.bar(param) { |e| e.something }
|
45
|
+
# foo && foo.bar.baz.qux # method chain with more than 2 methods
|
46
|
+
# foo && foo.nil? # method that `nil` responds to
|
39
47
|
#
|
48
|
+
# # Method calls that do not use `.`
|
49
|
+
# foo && foo < bar
|
50
|
+
# foo < bar if foo
|
51
|
+
#
|
52
|
+
# # When checking `foo&.empty?` in a conditional, `foo` being `nil` will actually
|
53
|
+
# # do the opposite of what the author intends.
|
54
|
+
# foo && foo.empty?
|
55
|
+
#
|
56
|
+
# # This could start returning `nil` as well as the return of the method
|
40
57
|
# foo.nil? || foo.bar
|
41
58
|
# !foo || foo.bar
|
42
59
|
#
|
43
|
-
# # Methods that
|
44
|
-
# # use safe navigation
|
45
|
-
# foo.
|
46
|
-
|
47
|
-
|
60
|
+
# # Methods that are used on assignment, arithmetic operation or
|
61
|
+
# # comparison should not be converted to use safe navigation
|
62
|
+
# foo.baz = bar if foo
|
63
|
+
# foo.baz + bar if foo
|
64
|
+
# foo.bar > 2 if foo
|
65
|
+
class SafeNavigation < Base
|
66
|
+
include NilMethods
|
67
|
+
include RangeHelp
|
68
|
+
extend AutoCorrector
|
48
69
|
|
49
70
|
MSG = 'Use safe navigation (`&.`) instead of checking if an object ' \
|
50
|
-
'exists before calling the method.'
|
51
|
-
|
52
|
-
|
53
|
-
minimum_target_ruby_version 2.3
|
71
|
+
'exists before calling the method.'
|
72
|
+
LOGIC_JUMP_KEYWORDS = %i[break fail next raise return throw yield].freeze
|
54
73
|
|
55
|
-
|
74
|
+
# if format: (if checked_variable body nil)
|
75
|
+
# unless format: (if checked_variable nil body)
|
76
|
+
# @!method modifier_if_safe_navigation_candidate(node)
|
77
|
+
def_node_matcher :modifier_if_safe_navigation_candidate, <<~PATTERN
|
56
78
|
{
|
57
|
-
(if
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
79
|
+
(if {
|
80
|
+
(send $_ {:nil? :!})
|
81
|
+
$_
|
82
|
+
} nil? $_)
|
83
|
+
|
84
|
+
(if {
|
85
|
+
(send (send $_ :nil?) :!)
|
86
|
+
$_
|
87
|
+
} $_ nil?)
|
65
88
|
}
|
66
89
|
PATTERN
|
67
90
|
|
68
|
-
|
69
|
-
|
70
|
-
{(send (send $_ :nil?) :!) $_}
|
71
|
-
{(send $_ $_ ...) (block (send $_ $_ ...) ...)}
|
72
|
-
...)
|
73
|
-
PATTERN
|
91
|
+
# @!method not_nil_check?(node)
|
92
|
+
def_node_matcher :not_nil_check?, '(send (send $_ :nil?) :!)'
|
74
93
|
|
75
94
|
def on_if(node)
|
76
|
-
return if node
|
95
|
+
return if allowed_if_condition?(node)
|
77
96
|
|
78
97
|
check_node(node)
|
79
98
|
end
|
@@ -82,62 +101,178 @@ module RuboCop
|
|
82
101
|
check_node(node)
|
83
102
|
end
|
84
103
|
|
85
|
-
def on_or(node)
|
86
|
-
check_node(node)
|
87
|
-
end
|
88
|
-
|
89
104
|
def check_node(node)
|
90
|
-
|
91
|
-
return if allowed_if_condition?(node)
|
92
|
-
checked_variable, receiver, method = extract_parts(node)
|
105
|
+
checked_variable, receiver, method_chain, method = extract_parts(node)
|
93
106
|
return unless receiver == checked_variable
|
94
|
-
return if
|
95
|
-
|
107
|
+
return if use_var_only_in_unless_modifier?(node, checked_variable)
|
108
|
+
# method is already a method call so this is actually checking for a
|
109
|
+
# chain greater than 2
|
110
|
+
return if chain_size(method_chain, method) > 1
|
111
|
+
return if unsafe_method_used?(method_chain, method)
|
112
|
+
return if method_chain.method?(:empty?)
|
113
|
+
|
114
|
+
add_offense(node) { |corrector| autocorrect(corrector, node) }
|
115
|
+
end
|
96
116
|
|
97
|
-
|
117
|
+
def use_var_only_in_unless_modifier?(node, variable)
|
118
|
+
node.if_type? && node.unless? && !method_called?(variable)
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def autocorrect(corrector, node)
|
124
|
+
body = node.node_parts[1]
|
125
|
+
method_call = method_call(node)
|
126
|
+
|
127
|
+
corrector.remove(begin_range(node, body))
|
128
|
+
corrector.remove(end_range(node, body))
|
129
|
+
corrector.insert_before(method_call.loc.dot, '&')
|
130
|
+
handle_comments(corrector, node, method_call)
|
131
|
+
|
132
|
+
add_safe_nav_to_all_methods_in_chain(corrector, method_call, body)
|
133
|
+
end
|
134
|
+
|
135
|
+
def handle_comments(corrector, node, method_call)
|
136
|
+
comments = comments(node)
|
137
|
+
return if comments.empty?
|
138
|
+
|
139
|
+
corrector.insert_before(method_call, "#{comments.map(&:text).join("\n")}\n")
|
140
|
+
end
|
141
|
+
|
142
|
+
def comments(node)
|
143
|
+
relevant_comment_ranges(node).each.with_object([]) do |range, comments|
|
144
|
+
comments.concat(processed_source.each_comment_in_lines(range).to_a)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def relevant_comment_ranges(node)
|
149
|
+
# Get source lines ranges inside the if node that aren't inside an inner node
|
150
|
+
# Comments inside an inner node should remain attached to that node, and not
|
151
|
+
# moved.
|
152
|
+
begin_pos = node.loc.first_line
|
153
|
+
end_pos = node.loc.last_line
|
154
|
+
|
155
|
+
node.child_nodes.each.with_object([]) do |child, ranges|
|
156
|
+
ranges << (begin_pos...child.loc.first_line)
|
157
|
+
begin_pos = child.loc.last_line
|
158
|
+
end << (begin_pos...end_pos)
|
98
159
|
end
|
99
160
|
|
100
161
|
def allowed_if_condition?(node)
|
101
|
-
node.
|
162
|
+
node.else? || node.elsif? || node.ternary?
|
163
|
+
end
|
164
|
+
|
165
|
+
def method_call(node)
|
166
|
+
_checked_variable, matching_receiver, = extract_parts(node)
|
167
|
+
matching_receiver.parent
|
102
168
|
end
|
103
169
|
|
104
170
|
def extract_parts(node)
|
171
|
+
case node.type
|
172
|
+
when :if
|
173
|
+
extract_parts_from_if(node)
|
174
|
+
when :and
|
175
|
+
extract_parts_from_and(node)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def extract_parts_from_if(node)
|
180
|
+
variable, receiver = modifier_if_safe_navigation_candidate(node)
|
181
|
+
|
182
|
+
checked_variable, matching_receiver, method = extract_common_parts(receiver, variable)
|
183
|
+
|
184
|
+
matching_receiver = nil if receiver && LOGIC_JUMP_KEYWORDS.include?(receiver.type)
|
185
|
+
|
186
|
+
[checked_variable, matching_receiver, receiver, method]
|
187
|
+
end
|
188
|
+
|
189
|
+
def extract_parts_from_and(node)
|
190
|
+
checked_variable, rhs = *node
|
105
191
|
if cop_config['ConvertCodeThatCanStartToReturnNil']
|
106
|
-
|
107
|
-
candidate_that_may_introduce_nil(node)
|
108
|
-
else
|
109
|
-
safe_navigation_candidate(node)
|
192
|
+
checked_variable = not_nil_check?(checked_variable) || checked_variable
|
110
193
|
end
|
194
|
+
|
195
|
+
checked_variable, matching_receiver, method = extract_common_parts(rhs, checked_variable)
|
196
|
+
[checked_variable, matching_receiver, rhs, method]
|
111
197
|
end
|
112
198
|
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
199
|
+
def extract_common_parts(method_chain, checked_variable)
|
200
|
+
matching_receiver = find_matching_receiver_invocation(method_chain, checked_variable)
|
201
|
+
|
202
|
+
method = matching_receiver.parent if matching_receiver
|
203
|
+
|
204
|
+
[checked_variable, matching_receiver, method]
|
205
|
+
end
|
206
|
+
|
207
|
+
def find_matching_receiver_invocation(method_chain, checked_variable)
|
208
|
+
return nil unless method_chain
|
209
|
+
|
210
|
+
receiver = if method_chain.block_type?
|
211
|
+
method_chain.send_node.receiver
|
212
|
+
else
|
213
|
+
method_chain.receiver
|
214
|
+
end
|
215
|
+
|
216
|
+
return receiver if receiver == checked_variable
|
217
|
+
|
218
|
+
find_matching_receiver_invocation(receiver, checked_variable)
|
219
|
+
end
|
220
|
+
|
221
|
+
def chain_size(method_chain, method)
|
222
|
+
method.each_ancestor(:send).inject(0) do |total, ancestor|
|
223
|
+
break total + 1 if ancestor == method_chain
|
224
|
+
|
225
|
+
total + 1
|
118
226
|
end
|
227
|
+
end
|
119
228
|
|
120
|
-
|
229
|
+
def unsafe_method_used?(method_chain, method)
|
230
|
+
return true if unsafe_method?(method)
|
121
231
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
232
|
+
method.each_ancestor(:send).any? do |ancestor|
|
233
|
+
break true unless config.for_cop('Lint/SafeNavigationChain')['Enabled']
|
234
|
+
|
235
|
+
break true if unsafe_method?(ancestor)
|
236
|
+
break true if nil_methods.include?(ancestor.method_name)
|
237
|
+
break false if ancestor == method_chain
|
126
238
|
end
|
127
239
|
end
|
128
240
|
|
129
|
-
|
241
|
+
def unsafe_method?(send_node)
|
242
|
+
negated?(send_node) || send_node.assignment? || !send_node.dot?
|
243
|
+
end
|
244
|
+
|
245
|
+
def negated?(send_node)
|
246
|
+
if method_called?(send_node)
|
247
|
+
negated?(send_node.parent)
|
248
|
+
else
|
249
|
+
send_node.send_type? && send_node.method?(:!)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def method_called?(send_node)
|
254
|
+
send_node&.parent&.send_type?
|
255
|
+
end
|
130
256
|
|
131
257
|
def begin_range(node, method_call)
|
132
|
-
|
133
|
-
node.loc.expression.begin_pos,
|
134
|
-
method_call.loc.expression.begin_pos)
|
258
|
+
range_between(node.loc.expression.begin_pos, method_call.loc.expression.begin_pos)
|
135
259
|
end
|
136
260
|
|
137
261
|
def end_range(node, method_call)
|
138
|
-
|
139
|
-
|
140
|
-
|
262
|
+
range_between(method_call.loc.expression.end_pos, node.loc.expression.end_pos)
|
263
|
+
end
|
264
|
+
|
265
|
+
def add_safe_nav_to_all_methods_in_chain(corrector,
|
266
|
+
start_method,
|
267
|
+
method_chain)
|
268
|
+
start_method.each_ancestor do |ancestor|
|
269
|
+
break unless %i[send block].include?(ancestor.type)
|
270
|
+
next unless ancestor.send_type?
|
271
|
+
|
272
|
+
corrector.insert_before(ancestor.loc.dot, '&')
|
273
|
+
|
274
|
+
break if ancestor == method_chain
|
275
|
+
end
|
141
276
|
end
|
142
277
|
end
|
143
278
|
end
|
@@ -2,15 +2,18 @@
|
|
2
2
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
|
-
module
|
6
|
-
# This cop is used to identify usages of `shuffle.first`,
|
7
|
-
# and `shuffle[]` and change them to use
|
5
|
+
module Style
|
6
|
+
# This cop is used to identify usages of `shuffle.first`,
|
7
|
+
# `shuffle.last`, and `shuffle[]` and change them to use
|
8
|
+
# `sample` instead.
|
8
9
|
#
|
9
10
|
# @example
|
10
11
|
# # bad
|
11
12
|
# [1, 2, 3].shuffle.first
|
12
13
|
# [1, 2, 3].shuffle.first(2)
|
13
14
|
# [1, 2, 3].shuffle.last
|
15
|
+
# [2, 1, 3].shuffle.at(0)
|
16
|
+
# [2, 1, 3].shuffle.slice(0)
|
14
17
|
# [1, 2, 3].shuffle[2]
|
15
18
|
# [1, 2, 3].shuffle[0, 2] # sample(2) will do the same
|
16
19
|
# [1, 2, 3].shuffle[0..2] # sample(3) will do the same
|
@@ -24,30 +27,29 @@ module RuboCop
|
|
24
27
|
# [1, 2, 3].shuffle[1..3] # sample(3) might return a longer Array
|
25
28
|
# [1, 2, 3].shuffle[foo, bar]
|
26
29
|
# [1, 2, 3].shuffle(random: Random.new)
|
27
|
-
class Sample <
|
28
|
-
|
30
|
+
class Sample < Base
|
31
|
+
extend AutoCorrector
|
29
32
|
|
30
|
-
|
31
|
-
|
33
|
+
MSG = 'Use `%<correct>s` instead of `%<incorrect>s`.'
|
34
|
+
RESTRICT_ON_SEND = %i[first last [] at slice].freeze
|
35
|
+
|
36
|
+
# @!method sample_candidate?(node)
|
37
|
+
def_node_matcher :sample_candidate?, <<~PATTERN
|
38
|
+
(send $(send _ :shuffle $...) ${:#{RESTRICT_ON_SEND.join(' :')}} $...)
|
32
39
|
PATTERN
|
33
40
|
|
34
41
|
def on_send(node)
|
35
|
-
sample_candidate?(node) do |
|
42
|
+
sample_candidate?(node) do |shuffle_node, shuffle_arg, method, method_args|
|
36
43
|
return unless offensive?(method, method_args)
|
37
44
|
|
38
|
-
range = source_range(
|
45
|
+
range = source_range(shuffle_node, node)
|
39
46
|
message = message(shuffle_arg, method, method_args, range)
|
40
|
-
add_offense(node, range, message)
|
41
|
-
end
|
42
|
-
end
|
43
47
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
corrector.replace(source_range(shuffle_node, node),
|
50
|
-
correction(shuffle_arg, method, method_args))
|
48
|
+
add_offense(range, message: message) do |corrector|
|
49
|
+
corrector.replace(
|
50
|
+
source_range(shuffle_node, node), correction(shuffle_arg, method, method_args)
|
51
|
+
)
|
52
|
+
end
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
@@ -57,7 +59,7 @@ module RuboCop
|
|
57
59
|
case method
|
58
60
|
when :first, :last
|
59
61
|
true
|
60
|
-
when :[]
|
62
|
+
when :[], :at, :slice
|
61
63
|
sample_size(method_args) != :unknown
|
62
64
|
else
|
63
65
|
false
|
@@ -74,11 +76,10 @@ module RuboCop
|
|
74
76
|
end
|
75
77
|
|
76
78
|
def sample_size_for_one_arg(arg)
|
77
|
-
|
78
|
-
when :erange, :irange
|
79
|
+
if arg.range_type?
|
79
80
|
range_size(arg)
|
80
|
-
|
81
|
-
arg.to_a.first
|
81
|
+
elsif arg.int_type?
|
82
|
+
[0, -1].include?(arg.to_a.first) ? nil : :unknown
|
82
83
|
else
|
83
84
|
:unknown
|
84
85
|
end
|
@@ -86,12 +87,14 @@ module RuboCop
|
|
86
87
|
|
87
88
|
def sample_size_for_two_args(first, second)
|
88
89
|
return :unknown unless first.int_type? && first.to_a.first.zero?
|
90
|
+
|
89
91
|
second.int_type? ? second.to_a.first : :unknown
|
90
92
|
end
|
91
93
|
|
92
|
-
def range_size(range_node)
|
94
|
+
def range_size(range_node) # rubocop:todo Metrics/CyclomaticComplexity
|
93
95
|
vals = range_node.to_a
|
94
96
|
return :unknown unless vals.all?(&:int_type?)
|
97
|
+
|
95
98
|
low, high = vals.map { |val| val.children[0] }
|
96
99
|
return :unknown unless low.zero? && high >= 0
|
97
100
|
|
@@ -126,7 +129,7 @@ module RuboCop
|
|
126
129
|
case method
|
127
130
|
when :first, :last
|
128
131
|
extract_source(method_args)
|
129
|
-
when :[]
|
132
|
+
when :[], :slice
|
130
133
|
sample_size(method_args)
|
131
134
|
end
|
132
135
|
end
|