rubocop 1.46.0 → 1.52.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/config/default.yml +83 -11
- data/lib/rubocop/cli/command/auto_generate_config.rb +7 -0
- data/lib/rubocop/cli/command/execute_runner.rb +7 -2
- data/lib/rubocop/cli.rb +6 -6
- data/lib/rubocop/comment_config.rb +2 -0
- data/lib/rubocop/config.rb +7 -3
- data/lib/rubocop/config_loader.rb +8 -8
- data/lib/rubocop/config_obsoletion.rb +2 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +29 -13
- data/lib/rubocop/cop/base.rb +6 -2
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/dependency_version.rb +1 -1
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +37 -13
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +3 -3
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/location_expression.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +3 -3
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +66 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -21
- data/lib/rubocop/cop/layout/class_structure.rb +6 -3
- data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
- data/lib/rubocop/cop/layout/empty_comment.rb +3 -3
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -4
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -3
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
- data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +1 -3
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +4 -2
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
- data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +46 -4
- data/lib/rubocop/cop/lint/missing_super.rb +34 -2
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +2 -0
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -4
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -5
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_type.rb +3 -3
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +1 -1
- data/lib/rubocop/cop/lint/script_permission.rb +1 -1
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +6 -12
- data/lib/rubocop/cop/lint/syntax.rb +4 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
- data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +9 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +59 -1
- data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
- data/lib/rubocop/cop/lint/useless_rescue.rb +4 -1
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +69 -9
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +1 -0
- data/lib/rubocop/cop/metrics/collection_literal_length.rb +76 -0
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +3 -3
- data/lib/rubocop/cop/migration/department_name.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
- data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +9 -5
- data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +4 -2
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/mixin/min_branches_count.rb +40 -0
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -6
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +22 -7
- data/lib/rubocop/cop/naming/method_name.rb +3 -3
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +12 -4
- data/lib/rubocop/cop/naming/variable_name.rb +6 -1
- data/lib/rubocop/cop/registry.rb +3 -1
- data/lib/rubocop/cop/style/accessor_grouping.rb +32 -7
- data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +11 -1
- data/lib/rubocop/cop/style/begin_block.rb +1 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +11 -2
- data/lib/rubocop/cop/style/case_like_if.rb +20 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
- data/lib/rubocop/cop/style/class_equality_comparison.rb +51 -40
- data/lib/rubocop/cop/style/collection_compact.rb +20 -7
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +10 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
- data/lib/rubocop/cop/style/copyright.rb +6 -3
- data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/dir_empty.rb +54 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +2 -2
- data/lib/rubocop/cop/style/documentation.rb +11 -5
- data/lib/rubocop/cop/style/double_negation.rb +2 -2
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +8 -8
- data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/file_empty.rb +71 -0
- data/lib/rubocop/cop/style/file_read.rb +3 -3
- data/lib/rubocop/cop/style/file_write.rb +1 -1
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +3 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
- data/lib/rubocop/cop/style/hash_except.rb +23 -12
- data/lib/rubocop/cop/style/hash_like_case.rb +3 -9
- data/lib/rubocop/cop/style/hash_syntax.rb +5 -2
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +111 -15
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +5 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +9 -5
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +2 -2
- data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
- data/lib/rubocop/cop/style/map_to_set.rb +4 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -9
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +44 -37
- data/lib/rubocop/cop/style/min_max.rb +3 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +4 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +7 -4
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +12 -7
- data/lib/rubocop/cop/style/nil_lambda.rb +2 -2
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/parallel_assignment.rb +26 -18
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -2
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +2 -2
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +183 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +11 -4
- data/lib/rubocop/cop/style/redundant_sort.rb +3 -3
- data/lib/rubocop/cop/style/redundant_string_escape.rb +3 -4
- data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
- data/lib/rubocop/cop/style/require_order.rb +12 -8
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
- data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -2
- data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
- data/lib/rubocop/cop/style/semicolon.rb +12 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -4
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
- data/lib/rubocop/cop/style/unpack_first.rb +3 -3
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +9 -5
- data/lib/rubocop/cop/team.rb +2 -2
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/variable_force/assignment.rb +33 -1
- data/lib/rubocop/cop/variable_force/variable.rb +5 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +1 -0
- data/lib/rubocop/cops_documentation_generator.rb +10 -3
- data/lib/rubocop/directive_comment.rb +3 -3
- data/lib/rubocop/ext/comment.rb +18 -0
- data/lib/rubocop/ext/regexp_node.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +4 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/options.rb +4 -1
- data/lib/rubocop/result_cache.rb +2 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/server/cache.rb +1 -1
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/server/core.rb +1 -1
- data/lib/rubocop/server/helper.rb +1 -1
- data/lib/rubocop/server/server_command/exec.rb +1 -1
- data/lib/rubocop/target_ruby.rb +3 -2
- data/lib/rubocop/version.rb +10 -6
- data/lib/rubocop.rb +13 -0
- metadata +23 -8
@@ -37,7 +37,7 @@ module RuboCop
|
|
37
37
|
|
38
38
|
def autocorrect(corrector, node)
|
39
39
|
block = node.parent
|
40
|
-
range = range_between(block.loc.begin.end_pos, node.
|
40
|
+
range = range_between(block.loc.begin.end_pos, node.source_range.end_pos)
|
41
41
|
|
42
42
|
corrector.remove(range)
|
43
43
|
end
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def autocorrect(corrector, node)
|
36
36
|
send_node = node.parent.send_node
|
37
|
-
range = range_between(send_node.
|
37
|
+
range = range_between(send_node.source_range.end_pos, node.source_range.end_pos)
|
38
38
|
|
39
39
|
corrector.remove(range)
|
40
40
|
end
|
@@ -4,12 +4,12 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Ensures that eval methods (`eval`, `instance_eval`, `class_eval`
|
7
|
-
# and `module_eval`) are given filename and line number values (`
|
8
|
-
# and `
|
7
|
+
# and `module_eval`) are given filename and line number values (`\_\_FILE\_\_`
|
8
|
+
# and `\_\_LINE\_\_`). This data is used to ensure that any errors raised
|
9
9
|
# within the evaluated code will be given the correct identification
|
10
10
|
# in a backtrace.
|
11
11
|
#
|
12
|
-
# The cop also checks that the line number given relative to `
|
12
|
+
# The cop also checks that the line number given relative to `\_\_LINE\_\_` is
|
13
13
|
# correct.
|
14
14
|
#
|
15
15
|
# This cop will autocorrect incorrect or missing filename and line number
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
extend AutoCorrector
|
58
58
|
|
59
59
|
MSG = 'Pass `__FILE__` and `__LINE__` to `%<method_name>s`.'
|
60
|
-
MSG_EVAL = 'Pass a binding, `__FILE__
|
60
|
+
MSG_EVAL = 'Pass a binding, `__FILE__`, and `__LINE__` to `eval`.'
|
61
61
|
MSG_INCORRECT_FILE = 'Incorrect file for `%<method_name>s`; ' \
|
62
62
|
'use `%<expected>s` instead of `%<actual>s`.'
|
63
63
|
MSG_INCORRECT_LINE = 'Incorrect line number for `%<method_name>s`; ' \
|
@@ -146,7 +146,7 @@ module RuboCop
|
|
146
146
|
actual: line_node.source,
|
147
147
|
expected: expected)
|
148
148
|
|
149
|
-
add_offense(line_node.
|
149
|
+
add_offense(line_node.source_range, message: message) do |corrector|
|
150
150
|
corrector.replace(line_node, expected)
|
151
151
|
end
|
152
152
|
end
|
@@ -175,14 +175,14 @@ module RuboCop
|
|
175
175
|
end
|
176
176
|
|
177
177
|
def line_difference(line_node, code)
|
178
|
-
string_first_line(code) - line_node.
|
178
|
+
string_first_line(code) - line_node.source_range.first_line
|
179
179
|
end
|
180
180
|
|
181
181
|
def string_first_line(str_node)
|
182
182
|
if str_node.heredoc?
|
183
183
|
str_node.loc.heredoc_body.first_line
|
184
184
|
else
|
185
|
-
str_node.
|
185
|
+
str_node.source_range.first_line
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
@@ -210,7 +210,7 @@ module RuboCop
|
|
210
210
|
def add_offense_for_missing_line(node, code)
|
211
211
|
register_offense(node) do |corrector|
|
212
212
|
line_str = missing_line(node, code)
|
213
|
-
corrector.insert_after(node.
|
213
|
+
corrector.insert_after(node.last_argument.source_range.end, ", #{line_str}")
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for exact regexp match inside Regexp literals.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# string =~ /\Astring\z/
|
12
|
+
# string === /\Astring\z/
|
13
|
+
# string.match(/\Astring\z/)
|
14
|
+
# string.match?(/\Astring\z/)
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# string == 'string'
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# string !~ /\Astring\z/
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# string != 'string'
|
24
|
+
#
|
25
|
+
class ExactRegexpMatch < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
|
28
|
+
MSG = 'Use `%<prefer>s`.'
|
29
|
+
RESTRICT_ON_SEND = %i[=~ === !~ match match?].freeze
|
30
|
+
|
31
|
+
# @!method exact_regexp_match(node)
|
32
|
+
def_node_matcher :exact_regexp_match, <<~PATTERN
|
33
|
+
(send
|
34
|
+
_ {:=~ :=== :!~ :match :match?}
|
35
|
+
(regexp
|
36
|
+
(str $_)
|
37
|
+
(regopt)))
|
38
|
+
PATTERN
|
39
|
+
|
40
|
+
def on_send(node)
|
41
|
+
return unless (regexp = exact_regexp_match(node))
|
42
|
+
|
43
|
+
parsed_regexp = Regexp::Parser.parse(regexp)
|
44
|
+
return unless exact_match_pattern?(parsed_regexp)
|
45
|
+
|
46
|
+
prefer = "#{node.receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
|
47
|
+
|
48
|
+
add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
|
49
|
+
corrector.replace(node, prefer)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def exact_match_pattern?(parsed_regexp)
|
56
|
+
tokens = parsed_regexp.map(&:token)
|
57
|
+
return false unless tokens[0] == :bos && tokens[1] == :literal && tokens[2] == :eos
|
58
|
+
|
59
|
+
!parsed_regexp[1].quantifier
|
60
|
+
end
|
61
|
+
|
62
|
+
def new_method(node)
|
63
|
+
node.method?(:!~) ? '!=' : '=='
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -145,7 +145,7 @@ module RuboCop
|
|
145
145
|
end
|
146
146
|
|
147
147
|
def block_body_range(block_node, send_node)
|
148
|
-
range_between(send_node.
|
148
|
+
range_between(send_node.source_range.end_pos, block_node.loc.end.end_pos)
|
149
149
|
end
|
150
150
|
end
|
151
151
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Prefer to use `File.empty?('path/to/file')` when checking if a file is empty.
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# This cop is unsafe, because `File.size`, `File.read`, and `File.binread`
|
10
|
+
# raise `ENOENT` exception when there is no file corresponding to the path,
|
11
|
+
# while `File.empty?` does not raise an exception.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# # bad
|
15
|
+
# File.zero?('path/to/file')
|
16
|
+
# File.size('path/to/file') == 0
|
17
|
+
# File.size('path/to/file') >= 0
|
18
|
+
# File.size('path/to/file').zero?
|
19
|
+
# File.read('path/to/file').empty?
|
20
|
+
# File.binread('path/to/file') == ''
|
21
|
+
# FileTest.zero?('path/to/file')
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# File.empty?('path/to/file')
|
25
|
+
# FileTest.empty?('path/to/file')
|
26
|
+
#
|
27
|
+
class FileEmpty < Base
|
28
|
+
extend AutoCorrector
|
29
|
+
extend TargetRubyVersion
|
30
|
+
|
31
|
+
MSG = 'Use `%<file_class>s.empty?(%<arg>s)` instead.'
|
32
|
+
RESTRICT_ON_SEND = %i[>= != == zero? empty?].freeze
|
33
|
+
|
34
|
+
minimum_target_ruby_version 2.4
|
35
|
+
|
36
|
+
# @!method offensive?(node)
|
37
|
+
def_node_matcher :offensive?, <<~PATTERN
|
38
|
+
{
|
39
|
+
(send $(const {nil? cbase} {:File :FileTest}) :zero? $_)
|
40
|
+
(send (send $(const {nil? cbase} {:File :FileTest}) :size $_) {:== :>=} (int 0))
|
41
|
+
(send (send (send $(const {nil? cbase} {:File :FileTest}) :size $_) :!) {:== :>=} (int 0))
|
42
|
+
(send (send $(const {nil? cbase} {:File :FileTest}) :size $_) :zero?)
|
43
|
+
(send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) {:!= :==} (str empty?))
|
44
|
+
(send (send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) :!) {:!= :==} (str empty?))
|
45
|
+
(send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) :empty?)
|
46
|
+
}
|
47
|
+
PATTERN
|
48
|
+
|
49
|
+
def on_send(node)
|
50
|
+
offensive?(node) do |const_node, arg_node|
|
51
|
+
add_offense(node,
|
52
|
+
message: format(MSG, file_class: const_node.source,
|
53
|
+
arg: arg_node.source)) do |corrector|
|
54
|
+
corrector.replace(node,
|
55
|
+
"#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def bang(node)
|
63
|
+
if (node.method?(:==) && node.child_nodes.first.method?(:!)) ||
|
64
|
+
(%i[>= !=].include?(node.method_name) && !node.child_nodes.first.method?(:!))
|
65
|
+
'!'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -63,7 +63,7 @@ module RuboCop
|
|
63
63
|
|
64
64
|
# @!method block_read?(node)
|
65
65
|
def_node_matcher :block_read?, <<~PATTERN
|
66
|
-
(block _ (args (arg
|
66
|
+
(block _ (args (arg _name)) (send (lvar _name) :read))
|
67
67
|
PATTERN
|
68
68
|
|
69
69
|
def on_send(node)
|
@@ -71,7 +71,7 @@ module RuboCop
|
|
71
71
|
message = format(MSG, read_method: read_method(mode))
|
72
72
|
|
73
73
|
add_offense(read_node, message: message) do |corrector|
|
74
|
-
range = range_between(node.loc.selector.begin_pos, read_node.
|
74
|
+
range = range_between(node.loc.selector.begin_pos, read_node.source_range.end_pos)
|
75
75
|
replacement = "#{read_method(mode)}(#{filename.source})"
|
76
76
|
|
77
77
|
corrector.replace(range, replacement)
|
@@ -100,7 +100,7 @@ module RuboCop
|
|
100
100
|
def file_open_read?(node)
|
101
101
|
return true if send_read?(node)
|
102
102
|
|
103
|
-
block_read?(node)
|
103
|
+
block_read?(node)
|
104
104
|
end
|
105
105
|
|
106
106
|
def read_method(mode)
|
@@ -74,7 +74,7 @@ module RuboCop
|
|
74
74
|
message = format(MSG, write_method: write_method(mode))
|
75
75
|
|
76
76
|
add_offense(write_node, message: message) do |corrector|
|
77
|
-
range = range_between(node.loc.selector.begin_pos, write_node.
|
77
|
+
range = range_between(node.loc.selector.begin_pos, write_node.source_range.end_pos)
|
78
78
|
replacement = replacement(mode, filename, content, write_node)
|
79
79
|
|
80
80
|
corrector.replace(range, replacement)
|
@@ -182,11 +182,13 @@ module RuboCop
|
|
182
182
|
|
183
183
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
184
184
|
def autocorrect(corrector, node, condition, replacement, guard)
|
185
|
-
corrector.replace(node.loc.keyword.join(condition.
|
185
|
+
corrector.replace(node.loc.keyword.join(condition.source_range), replacement)
|
186
186
|
|
187
187
|
if_branch = node.if_branch
|
188
188
|
else_branch = node.else_branch
|
189
189
|
|
190
|
+
corrector.replace(node.loc.begin, "\n") if node.loc.begin&.is?('then')
|
191
|
+
|
190
192
|
if if_branch&.send_type? && heredoc?(if_branch.last_argument)
|
191
193
|
autocorrect_heredoc_argument(corrector, node, if_branch, else_branch, guard)
|
192
194
|
elsif else_branch&.send_type? && heredoc?(else_branch.last_argument)
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
# execute(sql).keys.each { |v| p v }
|
29
29
|
# execute(sql).values.each { |v| p v }
|
30
30
|
class HashEachMethods < Base
|
31
|
+
include AllowedReceivers
|
31
32
|
include Lint::UnusedArgument
|
32
33
|
extend AutoCorrector
|
33
34
|
|
@@ -116,28 +117,6 @@ module RuboCop
|
|
116
117
|
def kv_range(outer_node)
|
117
118
|
outer_node.receiver.loc.selector.join(outer_node.loc.selector)
|
118
119
|
end
|
119
|
-
|
120
|
-
def allowed_receiver?(receiver)
|
121
|
-
receiver_name = receiver_name(receiver)
|
122
|
-
|
123
|
-
allowed_receivers.include?(receiver_name)
|
124
|
-
end
|
125
|
-
|
126
|
-
def receiver_name(receiver)
|
127
|
-
if receiver.send_type?
|
128
|
-
if receiver.receiver
|
129
|
-
"#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
|
130
|
-
else
|
131
|
-
receiver.method_name.to_s
|
132
|
-
end
|
133
|
-
else
|
134
|
-
receiver.source
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def allowed_receivers
|
139
|
-
cop_config.fetch('AllowedReceivers', [])
|
140
|
-
end
|
141
120
|
end
|
142
121
|
end
|
143
122
|
end
|
@@ -45,13 +45,13 @@ module RuboCop
|
|
45
45
|
(block
|
46
46
|
(send _ _)
|
47
47
|
(args
|
48
|
-
(arg _)
|
48
|
+
$(arg _)
|
49
49
|
(arg _))
|
50
50
|
{
|
51
|
-
(send
|
51
|
+
$(send
|
52
52
|
_ {:== :!= :eql? :include?} _)
|
53
53
|
(send
|
54
|
-
(send
|
54
|
+
$(send
|
55
55
|
_ {:== :!= :eql? :include?} _) :!)
|
56
56
|
})
|
57
57
|
PATTERN
|
@@ -61,13 +61,13 @@ module RuboCop
|
|
61
61
|
(block
|
62
62
|
(send _ _)
|
63
63
|
(args
|
64
|
-
(arg _)
|
64
|
+
$(arg _)
|
65
65
|
(arg _))
|
66
66
|
{
|
67
|
-
(send
|
67
|
+
$(send
|
68
68
|
_ {:== :!= :eql? :in? :include? :exclude?} _)
|
69
69
|
(send
|
70
|
-
(send
|
70
|
+
$(send
|
71
71
|
_ {:== :!= :eql? :in? :include? :exclude?} _) :!)
|
72
72
|
})
|
73
73
|
PATTERN
|
@@ -89,13 +89,24 @@ module RuboCop
|
|
89
89
|
|
90
90
|
private
|
91
91
|
|
92
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
92
93
|
def bad_method?(block)
|
93
94
|
if active_support_extensions_enabled?
|
94
|
-
bad_method_with_active_support?(block)
|
95
|
+
bad_method_with_active_support?(block) do |key_arg, send_node|
|
96
|
+
if send_node.method?(:in?) && send_node.receiver&.source != key_arg.source
|
97
|
+
return false
|
98
|
+
end
|
99
|
+
return true if !send_node.method?(:include?) && !send_node.method?(:exclude?)
|
100
|
+
|
101
|
+
send_node.first_argument&.source == key_arg.source
|
102
|
+
end
|
95
103
|
else
|
96
|
-
bad_method_with_poro?(block)
|
104
|
+
bad_method_with_poro?(block) do |key_arg, send_node|
|
105
|
+
!send_node.method?(:include?) || send_node.first_argument&.source == key_arg.source
|
106
|
+
end
|
97
107
|
end
|
98
108
|
end
|
109
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
99
110
|
|
100
111
|
def semantically_except_method?(send, block)
|
101
112
|
body = block.body
|
@@ -122,8 +133,8 @@ module RuboCop
|
|
122
133
|
end
|
123
134
|
|
124
135
|
def safe_to_register_offense?(block, except_key)
|
125
|
-
extracted =
|
126
|
-
if extracted.method?('in?') || extracted.method?('include?') ||
|
136
|
+
extracted = extract_body_if_negated(block.body)
|
137
|
+
if extracted.method?('in?') || extracted.method?('include?') ||
|
127
138
|
extracted.method?('exclude?')
|
128
139
|
return true
|
129
140
|
end
|
@@ -132,7 +143,7 @@ module RuboCop
|
|
132
143
|
except_key.sym_type? || except_key.str_type?
|
133
144
|
end
|
134
145
|
|
135
|
-
def
|
146
|
+
def extract_body_if_negated(body)
|
136
147
|
return body unless body.method?('!')
|
137
148
|
|
138
149
|
body.receiver
|
@@ -161,7 +172,7 @@ module RuboCop
|
|
161
172
|
|
162
173
|
def except_key(node)
|
163
174
|
key_argument = node.argument_list.first.source
|
164
|
-
body =
|
175
|
+
body = extract_body_if_negated(node.body)
|
165
176
|
lhs, _method_name, rhs = *body
|
166
177
|
return if [lhs, rhs].map(&:source).none?(key_argument)
|
167
178
|
|
@@ -37,6 +37,8 @@ module RuboCop
|
|
37
37
|
# end
|
38
38
|
#
|
39
39
|
class HashLikeCase < Base
|
40
|
+
include MinBranchesCount
|
41
|
+
|
40
42
|
MSG = 'Consider replacing `case-when` with a hash lookup.'
|
41
43
|
|
42
44
|
# @!method hash_like_case?(node)
|
@@ -49,7 +51,7 @@ module RuboCop
|
|
49
51
|
PATTERN
|
50
52
|
|
51
53
|
def on_case(node)
|
52
|
-
return
|
54
|
+
return unless min_branches_count?(node)
|
53
55
|
|
54
56
|
hash_like_case?(node) do |condition_nodes, body_nodes|
|
55
57
|
if nodes_of_same_type?(condition_nodes) && nodes_of_same_type?(body_nodes)
|
@@ -63,14 +65,6 @@ module RuboCop
|
|
63
65
|
def nodes_of_same_type?(nodes)
|
64
66
|
nodes.all? { |node| node.type == nodes.first.type }
|
65
67
|
end
|
66
|
-
|
67
|
-
def min_branches_count
|
68
|
-
length = cop_config['MinBranchesCount'] || 3
|
69
|
-
return length if length.is_a?(Integer) && length.positive?
|
70
|
-
|
71
|
-
warn Rainbow('`MinBranchesCount` needs to be a positive integer!').red
|
72
|
-
exit!
|
73
|
-
end
|
74
68
|
end
|
75
69
|
end
|
76
70
|
end
|
@@ -88,6 +88,9 @@ module RuboCop
|
|
88
88
|
# {foo: foo, bar: bar}
|
89
89
|
#
|
90
90
|
# # good
|
91
|
+
# {foo: foo, bar:}
|
92
|
+
#
|
93
|
+
# # good
|
91
94
|
# {foo:, bar:}
|
92
95
|
#
|
93
96
|
# @example EnforcedShorthandSyntax: consistent
|
@@ -207,7 +210,7 @@ module RuboCop
|
|
207
210
|
return true if /\A[_a-z]\w*[?!]?\z/i.match?(sym_name)
|
208
211
|
|
209
212
|
# For more complicated hash keys, let the parser validate the syntax.
|
210
|
-
|
213
|
+
parse("{ #{sym_name}: :foo }").valid_syntax?
|
211
214
|
end
|
212
215
|
|
213
216
|
def check(pairs, delim, msg)
|
@@ -247,7 +250,7 @@ module RuboCop
|
|
247
250
|
end
|
248
251
|
|
249
252
|
def argument_without_space?(node)
|
250
|
-
node.argument? && node.
|
253
|
+
node.argument? && node.source_range.begin_pos == node.parent.loc.selector.end_pos
|
251
254
|
end
|
252
255
|
|
253
256
|
def autocorrect_hash_rockets(corrector, pair_node)
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Looks for uses of
|
7
|
-
#
|
6
|
+
# Looks for uses of `\_.each_with_object({}) {...}`,
|
7
|
+
# `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
|
8
8
|
# transforming the keys of a hash, and tries to use a simpler & faster
|
9
9
|
# call to `transform_keys` instead.
|
10
10
|
# It should only be enabled on Ruby version 2.5 or newer.
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Looks for uses of
|
7
|
-
#
|
6
|
+
# Looks for uses of `\_.each_with_object({}) {...}`,
|
7
|
+
# `\_.map {...}.to_h`, and `Hash[\_.map {...}]` that are actually just
|
8
8
|
# transforming the values of a hash, and tries to use a simpler & faster
|
9
9
|
# call to `transform_values` instead.
|
10
10
|
#
|
@@ -59,11 +59,13 @@ module RuboCop
|
|
59
59
|
# end
|
60
60
|
#
|
61
61
|
class IfInsideElse < Base
|
62
|
+
include IgnoredNode
|
62
63
|
include RangeHelp
|
63
64
|
extend AutoCorrector
|
64
65
|
|
65
66
|
MSG = 'Convert `if` nested inside `else` to `elsif`.'
|
66
67
|
|
68
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
67
69
|
def on_if(node)
|
68
70
|
return if node.ternary? || node.unless?
|
69
71
|
|
@@ -73,9 +75,13 @@ module RuboCop
|
|
73
75
|
return if allow_if_modifier_in_else_branch?(else_branch)
|
74
76
|
|
75
77
|
add_offense(else_branch.loc.keyword) do |corrector|
|
78
|
+
next if part_of_ignored_node?(node)
|
79
|
+
|
76
80
|
autocorrect(corrector, else_branch)
|
81
|
+
ignore_node(node)
|
77
82
|
end
|
78
83
|
end
|
84
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
79
85
|
|
80
86
|
private
|
81
87
|
|