rubocop 1.92.2 → 1.93.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +5 -1
- data/lib/rubocop/#directive_comment.rb# +224 -0
- data/lib/rubocop/cache_config.rb +3 -0
- data/lib/rubocop/cli.rb +5 -4
- data/lib/rubocop/config.rb +22 -0
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/config_validator.rb +45 -1
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +32 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
- data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/block_end_newline.rb +51 -5
- data/lib/rubocop/cop/layout/class_structure.rb +91 -13
- data/lib/rubocop/cop/layout/def_end_alignment.rb +9 -5
- data/lib/rubocop/cop/layout/else_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +5 -19
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +26 -6
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +55 -16
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +5 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +21 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +4 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +53 -15
- data/lib/rubocop/cop/lint/duplicate_require.rb +9 -2
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/literal_as_condition.rb +29 -18
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +50 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +53 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +14 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +45 -3
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +18 -0
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +14 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +19 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +27 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +62 -9
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +21 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +4 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +4 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +71 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
- data/lib/rubocop/cop/lint/useless_times.rb +52 -12
- data/lib/rubocop/cop/lint/void.rb +20 -9
- data/lib/rubocop/cop/message_annotator.rb +6 -8
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +49 -18
- data/lib/rubocop/cop/mixin/endless_method_body_help.rb +45 -6
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +35 -2
- data/lib/rubocop/cop/mixin/project_index_help.rb +5 -6
- data/lib/rubocop/cop/mixin/range_help.rb +13 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +11 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +7 -3
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +6 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/and_or.rb +8 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +15 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +5 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -12
- data/lib/rubocop/cop/style/class_methods_definitions.rb +9 -1
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +13 -4
- data/lib/rubocop/cop/style/data_inheritance.rb +82 -9
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/dig_chain.rb +13 -2
- data/lib/rubocop/cop/style/each_with_object.rb +21 -5
- data/lib/rubocop/cop/style/empty_class_definition.rb +21 -4
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +27 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +30 -10
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
- data/lib/rubocop/cop/style/file_read.rb +5 -1
- data/lib/rubocop/cop/style/file_write.rb +89 -20
- data/lib/rubocop/cop/style/for.rb +59 -4
- data/lib/rubocop/cop/style/guard_clause.rb +56 -11
- data/lib/rubocop/cop/style/hash_conversion.rb +3 -1
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +1 -1
- data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -5
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +78 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +1 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +32 -2
- data/lib/rubocop/cop/style/it_block_parameter.rb +11 -1
- data/lib/rubocop/cop/style/lambda.rb +13 -5
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +10 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +38 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +11 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
- data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +22 -17
- data/lib/rubocop/cop/style/non_nil_check.rb +14 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +37 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -10
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +1 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +3 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +18 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +117 -17
- data/lib/rubocop/cop/style/redundant_each.rb +26 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
- data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +37 -16
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +15 -2
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
- data/lib/rubocop/cop/style/redundant_sort.rb +6 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
- data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
- data/lib/rubocop/cop/style/select_by_range.rb +1 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
- data/lib/rubocop/cop/style/self_assignment.rb +0 -4
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +8 -3
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +56 -19
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -0
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +16 -7
- data/lib/rubocop/cop/style/struct_inheritance.rb +82 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
- data/lib/rubocop/cop/style/word_array.rb +1 -3
- data/lib/rubocop/cop/util.rb +2 -0
- data/lib/rubocop/cop/variable_force.rb +14 -1
- data/lib/rubocop/cops_documentation_generator.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +13 -6
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +21 -7
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -5
|
@@ -54,8 +54,7 @@ module RuboCop
|
|
|
54
54
|
add_offense(node, message: message) do |corrector|
|
|
55
55
|
next if part_of_ignored_node?(node)
|
|
56
56
|
|
|
57
|
-
correct_access
|
|
58
|
-
corrector.replace(node, correct_access)
|
|
57
|
+
correct_access(corrector, node, expression)
|
|
59
58
|
end
|
|
60
59
|
|
|
61
60
|
ignore_node(node)
|
|
@@ -67,6 +66,27 @@ module RuboCop
|
|
|
67
66
|
dig_chain_enabled? &&
|
|
68
67
|
(dig?(node.receiver) || dig?(node.parent))
|
|
69
68
|
end
|
|
69
|
+
|
|
70
|
+
def correct_access(corrector, node, argument)
|
|
71
|
+
key_start = node.loc.begin || argument.source_range.begin
|
|
72
|
+
opening = node.receiver.source_range.end.join(key_start)
|
|
73
|
+
return if unsafe_opening?(opening)
|
|
74
|
+
|
|
75
|
+
corrector.replace(opening, '[')
|
|
76
|
+
if node.parenthesized?
|
|
77
|
+
corrector.replace(node.loc.end, ']')
|
|
78
|
+
else
|
|
79
|
+
corrector.insert_after(argument, ']')
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def unsafe_opening?(range)
|
|
84
|
+
return false if range.single_line?
|
|
85
|
+
|
|
86
|
+
processed_source.tokens_within(range).any? do |token|
|
|
87
|
+
token.comment? || token.type == :tSTRING_END
|
|
88
|
+
end
|
|
89
|
+
end
|
|
70
90
|
end
|
|
71
91
|
end
|
|
72
92
|
end
|
|
@@ -77,7 +77,9 @@ module RuboCop
|
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
def eligible_arguments?(node)
|
|
80
|
-
node.arguments? && node.arguments.to_a.all?(&:arg_type?)
|
|
80
|
+
return false unless node.arguments? && node.arguments.to_a.all?(&:arg_type?)
|
|
81
|
+
|
|
82
|
+
node.arguments.size <= target_args(node.method_name).size
|
|
81
83
|
end
|
|
82
84
|
|
|
83
85
|
def eligible_method?(node)
|
|
@@ -36,22 +36,18 @@ module RuboCop
|
|
|
36
36
|
|
|
37
37
|
MSG = 'Prefer multiline `do`...`end` block.'
|
|
38
38
|
|
|
39
|
-
# rubocop:disable-next Metrics/AbcSize
|
|
40
39
|
def on_block(node)
|
|
41
40
|
return if node.multiline? || node.braces?
|
|
42
41
|
return if single_line_blocks_preferred? && suitable_as_single_line?(node)
|
|
43
42
|
|
|
44
43
|
add_offense(node) do |corrector|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
# otherwise move it into the heredoc body and break the syntax.
|
|
51
|
-
corrector.remove(node.loc.end)
|
|
52
|
-
corrector.insert_after(heredoc.loc.heredoc_end, "\nend")
|
|
44
|
+
if (heredoc = heredoc_opened_before_do(node))
|
|
45
|
+
# Code after the `end`, such as a method chain, would be left behind after the `do`.
|
|
46
|
+
next if code_after_end?(node.loc.end)
|
|
47
|
+
|
|
48
|
+
move_body_after_heredoc(corrector, node, heredoc)
|
|
53
49
|
else
|
|
54
|
-
corrector
|
|
50
|
+
break_block_body(corrector, node)
|
|
55
51
|
end
|
|
56
52
|
end
|
|
57
53
|
end
|
|
@@ -60,29 +56,70 @@ module RuboCop
|
|
|
60
56
|
|
|
61
57
|
private
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
def single_line_blocks_preferred?
|
|
60
|
+
@config.for_enabled_cop('Layout/RedundantLineBreak')['InspectBlocks']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def code_after_end?(end_keyword)
|
|
64
|
+
rest = end_keyword.source_line[end_keyword.last_column..].strip
|
|
65
|
+
|
|
66
|
+
!rest.empty? && !rest.start_with?('#')
|
|
67
|
+
end
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
def move_body_after_heredoc(corrector, node, heredoc)
|
|
70
|
+
body = node.body
|
|
71
|
+
|
|
72
|
+
corrector.remove(do_line(node).end.join(node.loc.end.end))
|
|
73
|
+
corrector.insert_after(heredoc.loc.heredoc_end, "\n#{body.source}") if body
|
|
74
|
+
|
|
75
|
+
last_heredoc = trailing_heredoc(body) || heredoc
|
|
76
|
+
corrector.insert_after(last_heredoc.loc.heredoc_end, "\nend")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def break_block_body(corrector, node)
|
|
80
|
+
corrector.insert_after(do_line(node), "\n")
|
|
81
|
+
|
|
82
|
+
if (heredoc = trailing_heredoc(node.body))
|
|
83
|
+
corrector.remove(node.loc.end)
|
|
84
|
+
corrector.insert_after(heredoc.loc.heredoc_end, "\nend")
|
|
85
|
+
else
|
|
86
|
+
corrector.insert_before(node.loc.end, "\n")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Returns the heredoc opened before the `do` on the block's line whose body extends
|
|
91
|
+
# the furthest down. It can be opened outside the block, such as in a preceding argument.
|
|
92
|
+
def heredoc_opened_before_do(node)
|
|
93
|
+
do_begin_pos = node.loc.begin.begin_pos
|
|
94
|
+
|
|
95
|
+
heredocs = processed_source.ast.each_node(:any_str).select do |str_node|
|
|
96
|
+
heredoc_opened_on_line_before?(str_node, node.first_line, do_begin_pos)
|
|
70
97
|
end
|
|
71
98
|
|
|
72
99
|
heredocs.max_by { |heredoc| heredoc.loc.heredoc_end.line }
|
|
73
100
|
end
|
|
74
101
|
|
|
102
|
+
def heredoc_opened_on_line_before?(str_node, line, pos)
|
|
103
|
+
return false unless str_node.heredoc?
|
|
104
|
+
|
|
105
|
+
str_node.first_line == line && str_node.source_range.end_pos <= pos
|
|
106
|
+
end
|
|
107
|
+
|
|
75
108
|
def do_line(node)
|
|
76
109
|
if node.type?(:numblock, :itblock) ||
|
|
77
110
|
node.arguments.children.empty? || node.send_node.lambda_literal?
|
|
78
111
|
node.loc.begin
|
|
79
112
|
else
|
|
80
|
-
node.arguments
|
|
113
|
+
node.arguments.source_range
|
|
81
114
|
end
|
|
82
115
|
end
|
|
83
116
|
|
|
84
|
-
def
|
|
85
|
-
|
|
117
|
+
def trailing_heredoc(root)
|
|
118
|
+
return unless root
|
|
119
|
+
|
|
120
|
+
heredocs = root.each_node(:any_str).select(&:heredoc?)
|
|
121
|
+
|
|
122
|
+
heredocs.max_by { |heredoc| heredoc.loc.heredoc_end.line }
|
|
86
123
|
end
|
|
87
124
|
end
|
|
88
125
|
end
|
|
@@ -53,6 +53,8 @@ module RuboCop
|
|
|
53
53
|
private
|
|
54
54
|
|
|
55
55
|
def autocorrect(corrector, node)
|
|
56
|
+
return if part_of_ignored_node?(node)
|
|
57
|
+
|
|
56
58
|
if correct_to_endless?(node.body)
|
|
57
59
|
correct_to_endless(corrector, node)
|
|
58
60
|
else
|
|
@@ -100,7 +102,7 @@ module RuboCop
|
|
|
100
102
|
end
|
|
101
103
|
|
|
102
104
|
# The body gets parenthesized when it becomes endless, so it is no longer a command.
|
|
103
|
-
def
|
|
105
|
+
def command_body_outside_statement?(body)
|
|
104
106
|
!require_parentheses?(body) && super
|
|
105
107
|
end
|
|
106
108
|
|
|
@@ -119,12 +121,13 @@ module RuboCop
|
|
|
119
121
|
end
|
|
120
122
|
|
|
121
123
|
def correct_to_endless(corrector, node)
|
|
122
|
-
receiver =
|
|
124
|
+
receiver = node.loc.keyword.end.join(node.loc.name.begin).source.lstrip if node.receiver
|
|
123
125
|
body_source = method_body_source(node.body)
|
|
124
126
|
arguments = endless_arguments(node)
|
|
125
127
|
replacement = "def #{receiver}#{node.method_name}#{arguments} = #{body_source}"
|
|
126
128
|
|
|
127
129
|
corrector.replace(node, replacement)
|
|
130
|
+
ignore_node(node)
|
|
128
131
|
end
|
|
129
132
|
|
|
130
133
|
def break_line_before(corrector, node, range, indent_steps: 1)
|
|
@@ -157,12 +157,18 @@ module RuboCop
|
|
|
157
157
|
|
|
158
158
|
corrector.replace(if_branch.condition, chainable_condition(if_branch))
|
|
159
159
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
160
|
+
corrector.remove(redundant_end_range(node.loc.end, if_branch.loc.end))
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def redundant_end_range(outer_end, inner_end)
|
|
164
|
+
return inner_end.begin.join(outer_end.begin) if same_line?(outer_end, inner_end)
|
|
165
|
+
|
|
166
|
+
[outer_end, inner_end].each do |keyword|
|
|
167
|
+
line = range_by_whole_lines(keyword, include_final_newline: true)
|
|
168
|
+
return line if line.source.strip == 'end'
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
range_with_surrounding_space(inner_end, side: :right, newlines: false)
|
|
166
172
|
end
|
|
167
173
|
|
|
168
174
|
def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
|
@@ -57,6 +57,8 @@ module RuboCop
|
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def arguments_require_parentheses?(arguments)
|
|
60
|
+
return true if arguments.first.mlhs_type? || arguments.any?(&:shadowarg_type?)
|
|
61
|
+
|
|
60
62
|
arguments.each_descendant(:any_block, :hash).any? do |node|
|
|
61
63
|
!node.hash_type? || node.braces?
|
|
62
64
|
end
|
|
@@ -15,6 +15,10 @@ module RuboCop
|
|
|
15
15
|
# lines, this cop does not register an offense; instead,
|
|
16
16
|
# `Style/LineEndConcatenation` will pick up the offense if enabled.
|
|
17
17
|
#
|
|
18
|
+
# This cop does not register an offense for concatenation with a heredoc
|
|
19
|
+
# either, because interpolating the heredoc into a string literal would not
|
|
20
|
+
# make the code easier to read.
|
|
21
|
+
#
|
|
18
22
|
# Two modes are supported:
|
|
19
23
|
# 1. `aggressive` style checks and corrects all occurrences of `+` where
|
|
20
24
|
# either the left or right side of `+` is a string literal.
|
|
@@ -41,6 +45,11 @@ module RuboCop
|
|
|
41
45
|
# name = 'First' +
|
|
42
46
|
# 'Last'
|
|
43
47
|
#
|
|
48
|
+
# # accepted, concatenation with a heredoc
|
|
49
|
+
# 'Hello' + <<~TEXT
|
|
50
|
+
# world
|
|
51
|
+
# TEXT
|
|
52
|
+
#
|
|
44
53
|
# @example Mode: conservative
|
|
45
54
|
# # bad
|
|
46
55
|
# 'Hello' + user.name
|
|
@@ -73,6 +82,8 @@ module RuboCop
|
|
|
73
82
|
return if line_end_concatenation?(node)
|
|
74
83
|
|
|
75
84
|
topmost_plus_node = find_topmost_plus_node(node)
|
|
85
|
+
return if heredoc?(topmost_plus_node)
|
|
86
|
+
|
|
76
87
|
parts = collect_parts(topmost_plus_node)
|
|
77
88
|
return if mode == :conservative && !parts.first.str_type?
|
|
78
89
|
|
|
@@ -111,6 +122,10 @@ module RuboCop
|
|
|
111
122
|
current
|
|
112
123
|
end
|
|
113
124
|
|
|
125
|
+
def heredoc?(node)
|
|
126
|
+
node.each_descendant(:any_str).any?(&:heredoc?)
|
|
127
|
+
end
|
|
128
|
+
|
|
114
129
|
def collect_parts(node, parts = [])
|
|
115
130
|
return unless node
|
|
116
131
|
|
|
@@ -127,13 +142,7 @@ module RuboCop
|
|
|
127
142
|
end
|
|
128
143
|
|
|
129
144
|
def uncorrectable?(part)
|
|
130
|
-
part.multiline? ||
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def heredoc?(node)
|
|
134
|
-
return false unless node.type?(:str, :dstr)
|
|
135
|
-
|
|
136
|
-
node.heredoc?
|
|
145
|
+
part.multiline? || part.each_descendant(:any_block).any?
|
|
137
146
|
end
|
|
138
147
|
|
|
139
148
|
def corrected_ancestor?(node)
|
|
@@ -40,6 +40,9 @@ module RuboCop
|
|
|
40
40
|
|
|
41
41
|
MSG = "Don't extend an instance initialized by `Struct.new`. " \
|
|
42
42
|
'Use a block to customize the struct.'
|
|
43
|
+
LEFT_OPERAND_TYPES = %i[and or irange erange match_pattern match_pattern_p].freeze
|
|
44
|
+
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
|
|
45
|
+
BODY_TYPES = %i[module class sclass any_def any_block when in_pattern resbody for].freeze
|
|
43
46
|
|
|
44
47
|
# When the closing parenthesis of `Struct.new` is on its own line, omitting the
|
|
45
48
|
# parentheses in the same pass leaves a line break before the appended `do`.
|
|
@@ -52,12 +55,13 @@ module RuboCop
|
|
|
52
55
|
return if defines_constants?(node.body)
|
|
53
56
|
|
|
54
57
|
add_offense(node.parent_class) do |corrector|
|
|
55
|
-
|
|
58
|
+
keyword_range =
|
|
56
59
|
range_with_surrounding_space(node.loc.keyword, side: :right, newlines: false)
|
|
57
|
-
)
|
|
60
|
+
corrector.remove(keyword_range)
|
|
58
61
|
corrector.replace(node.loc.operator, '=')
|
|
59
62
|
|
|
60
63
|
correct_parent(node.parent_class, corrector)
|
|
64
|
+
parenthesize_left_operand(corrector, node, keyword_range)
|
|
61
65
|
end
|
|
62
66
|
end
|
|
63
67
|
|
|
@@ -80,33 +84,95 @@ module RuboCop
|
|
|
80
84
|
|
|
81
85
|
def correct_parent(parent, corrector)
|
|
82
86
|
if parent.block_type?
|
|
87
|
+
replace_opening_brace_with_do(corrector, parent) if parent.braces?
|
|
83
88
|
corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
|
|
84
|
-
elsif (
|
|
85
|
-
corrector.remove(
|
|
86
|
-
|
|
87
|
-
wrap_unparenthesized_call_with_do(corrector, parent)
|
|
89
|
+
elsif (range = range_for_empty_class_body(parent))
|
|
90
|
+
corrector.remove(range)
|
|
91
|
+
parenthesize_arguments(corrector, parent) unless statement?(parent.parent)
|
|
88
92
|
else
|
|
93
|
+
parenthesize_arguments(corrector, parent)
|
|
89
94
|
corrector.insert_after(parent, ' do')
|
|
90
95
|
end
|
|
91
96
|
end
|
|
92
97
|
|
|
93
|
-
def
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
def replace_opening_brace_with_do(corrector, block)
|
|
99
|
+
range = range_with_surrounding_space(block.loc.begin, side: :right, newlines: false)
|
|
100
|
+
return corrector.replace(range, 'do ') if block_content_follows?(range, block)
|
|
101
|
+
|
|
102
|
+
corrector.replace(block.loc.begin, 'do')
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def block_content_follows?(opening_range, block)
|
|
106
|
+
return false if opening_range.end_pos == block.loc.end.begin_pos
|
|
107
|
+
|
|
108
|
+
opening_range.last_column < opening_range.source_line.length
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def range_for_empty_class_body(struct_new)
|
|
112
|
+
class_node = struct_new.parent
|
|
113
|
+
return if class_node.body
|
|
114
|
+
|
|
115
|
+
range = range_between(struct_new.source_range.end_pos, class_node.source_range.end_pos)
|
|
116
|
+
return range if /\A[\s;]*end\z/.match?(range.source)
|
|
117
|
+
return if end_line_kept?(class_node)
|
|
118
|
+
|
|
119
|
+
range_by_whole_lines(class_node.loc.end, include_final_newline: true)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def end_line_kept?(class_node)
|
|
123
|
+
return true if left_operand?(class_node)
|
|
124
|
+
|
|
125
|
+
class_node.loc.end.source_line.strip != 'end'
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def left_operand?(class_node)
|
|
129
|
+
parent = class_node.parent
|
|
130
|
+
return false if parent.nil?
|
|
131
|
+
return parent.receiver.equal?(class_node) if parent.call_type?
|
|
132
|
+
return parent.condition.equal?(class_node) if parent.if_type? && parent.ternary?
|
|
133
|
+
|
|
134
|
+
LEFT_OPERAND_TYPES.include?(parent.type) && parent.children.first.equal?(class_node)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def statement?(class_node)
|
|
138
|
+
parent = class_node.parent
|
|
139
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
140
|
+
return !parent.condition.equal?(class_node) if conditional_statement?(parent)
|
|
141
|
+
|
|
142
|
+
statement_child(parent).equal?(class_node)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def conditional_statement?(node)
|
|
146
|
+
return true if node.type?(:while, :until)
|
|
147
|
+
|
|
148
|
+
node.if_type? && !node.ternary?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def statement_child(node)
|
|
152
|
+
return node.expression if node.type?(*ASSIGNMENT_TYPES)
|
|
153
|
+
return node.else_branch if node.type?(:case, :case_match)
|
|
154
|
+
return if node.any_def_type? && node.endless?
|
|
155
|
+
|
|
156
|
+
node.body if node.type?(*BODY_TYPES)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def parenthesize_arguments(corrector, struct_new)
|
|
160
|
+
return unless unparenthesized_struct_new?(struct_new)
|
|
161
|
+
|
|
162
|
+
arguments_begin = struct_new.first_argument.source_range.begin
|
|
163
|
+
corrector.replace(struct_new.loc.selector.end.join(arguments_begin), '(')
|
|
164
|
+
corrector.insert_after(struct_new.last_argument, ')')
|
|
99
165
|
end
|
|
100
166
|
|
|
101
167
|
def unparenthesized_struct_new?(parent)
|
|
102
168
|
parent.send_type? && parent.arguments.any? && !parent.parenthesized?
|
|
103
169
|
end
|
|
104
170
|
|
|
105
|
-
def
|
|
106
|
-
|
|
107
|
-
range = parent.loc.selector.end.join(parent.source_range.end)
|
|
171
|
+
def parenthesize_left_operand(corrector, class_node, keyword_range)
|
|
172
|
+
return unless left_operand?(class_node)
|
|
108
173
|
|
|
109
|
-
corrector.
|
|
174
|
+
corrector.insert_before(keyword_range, '(')
|
|
175
|
+
corrector.insert_after(class_node.source_range.end, ')')
|
|
110
176
|
end
|
|
111
177
|
end
|
|
112
178
|
end
|
|
@@ -19,7 +19,6 @@ module RuboCop
|
|
|
19
19
|
# x, y = y, x
|
|
20
20
|
#
|
|
21
21
|
class SwapValues < Base
|
|
22
|
-
include RangeHelp
|
|
23
22
|
extend AutoCorrector
|
|
24
23
|
|
|
25
24
|
MSG = 'Replace this and assignments at lines %<x_line>d ' \
|
|
@@ -28,14 +27,16 @@ module RuboCop
|
|
|
28
27
|
SIMPLE_ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn].to_set.freeze
|
|
29
28
|
|
|
30
29
|
def on_asgn(node)
|
|
31
|
-
return
|
|
30
|
+
return unless node.parent&.type?(:begin, :kwbegin)
|
|
32
31
|
|
|
33
32
|
tmp_assign = node
|
|
34
33
|
x_assign, y_assign = *node.right_siblings.take(2)
|
|
35
34
|
return unless x_assign && y_assign && swapping_values?(tmp_assign, x_assign, y_assign)
|
|
36
35
|
|
|
37
36
|
add_offense(node, message: message(x_assign, y_assign)) do |corrector|
|
|
38
|
-
range =
|
|
37
|
+
range = tmp_assign.source_range.join(y_assign.source_range)
|
|
38
|
+
next if contains_comment_or_heredoc?(range)
|
|
39
|
+
|
|
39
40
|
corrector.replace(range, replacement(x_assign))
|
|
40
41
|
end
|
|
41
42
|
end
|
|
@@ -44,10 +45,6 @@ module RuboCop
|
|
|
44
45
|
|
|
45
46
|
private
|
|
46
47
|
|
|
47
|
-
def allowed_assignment?(node)
|
|
48
|
-
node.parent&.mlhs_type? || node.parent&.shorthand_asgn?
|
|
49
|
-
end
|
|
50
|
-
|
|
51
48
|
def swapping_values?(tmp_assign, x_assign, y_assign)
|
|
52
49
|
simple_assignment?(tmp_assign) &&
|
|
53
50
|
simple_assignment?(x_assign) &&
|
|
@@ -57,6 +54,12 @@ module RuboCop
|
|
|
57
54
|
rhs(y_assign) == lhs(tmp_assign)
|
|
58
55
|
end
|
|
59
56
|
|
|
57
|
+
def contains_comment_or_heredoc?(range)
|
|
58
|
+
processed_source.tokens_within(range).any? do |token|
|
|
59
|
+
token.comment? || token.type == :tSTRING_END
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
60
63
|
def simple_assignment?(node)
|
|
61
64
|
return false unless node.respond_to?(:type)
|
|
62
65
|
|
|
@@ -89,12 +92,6 @@ module RuboCop
|
|
|
89
92
|
def rhs(node)
|
|
90
93
|
node.expression.source
|
|
91
94
|
end
|
|
92
|
-
|
|
93
|
-
def correction_range(tmp_assign, y_assign)
|
|
94
|
-
range_by_whole_lines(
|
|
95
|
-
range_between(tmp_assign.source_range.begin_pos, y_assign.source_range.end_pos)
|
|
96
|
-
)
|
|
97
|
-
end
|
|
98
95
|
end
|
|
99
96
|
end
|
|
100
97
|
end
|
|
@@ -98,9 +98,7 @@ module RuboCop
|
|
|
98
98
|
|
|
99
99
|
syms = node.children.map do |c|
|
|
100
100
|
if c.dsym_type?
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
":#{trim_string_interpolation_escape_character(string_literal)}"
|
|
101
|
+
":#{build_interpolated_string_literal(c)}"
|
|
104
102
|
else
|
|
105
103
|
to_symbol_literal(c.value.to_s)
|
|
106
104
|
end
|
|
@@ -166,7 +166,7 @@ module RuboCop
|
|
|
166
166
|
PATTERN
|
|
167
167
|
|
|
168
168
|
def self.autocorrect_incompatible_with
|
|
169
|
-
[
|
|
169
|
+
[Style::TrailingCommaInArguments]
|
|
170
170
|
end
|
|
171
171
|
|
|
172
172
|
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
@@ -142,10 +142,6 @@ module RuboCop
|
|
|
142
142
|
include TrailingComma
|
|
143
143
|
extend AutoCorrector
|
|
144
144
|
|
|
145
|
-
def self.autocorrect_incompatible_with
|
|
146
|
-
[Layout::HeredocArgumentClosingParenthesis]
|
|
147
|
-
end
|
|
148
|
-
|
|
149
145
|
def on_send(node)
|
|
150
146
|
return unless node.arguments? && (node.parenthesized? || node.method?(:[]))
|
|
151
147
|
|
|
@@ -141,9 +141,7 @@ module RuboCop
|
|
|
141
141
|
|
|
142
142
|
words = node.children.map do |word|
|
|
143
143
|
if word.dstr_type?
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
trim_string_interpolation_escape_character(string_literal)
|
|
144
|
+
build_interpolated_string_literal(word)
|
|
147
145
|
else
|
|
148
146
|
to_string_literal(word.children[0])
|
|
149
147
|
end
|
data/lib/rubocop/cop/util.rb
CHANGED
|
@@ -32,6 +32,15 @@ module RuboCop
|
|
|
32
32
|
VARIABLE_ASSIGNMENT_TYPE, REGEXP_NAMED_CAPTURE_TYPE, PATTERN_MATCH_VARIABLE_TYPE
|
|
33
33
|
].freeze
|
|
34
34
|
|
|
35
|
+
# Ruby assigns a named capture to a local variable only when its name is a valid
|
|
36
|
+
# local variable name, so `(?<Foo>...)` and `(?<if>...)` declare no variable.
|
|
37
|
+
LOCAL_VARIABLE_NAME = /\A(?![[:upper:]\p{Lt}])[a-z_\P{ASCII}][a-zA-Z0-9_\P{ASCII}]*\z/.freeze
|
|
38
|
+
RESERVED_WORDS = %w[
|
|
39
|
+
__ENCODING__ __FILE__ __LINE__ BEGIN END alias and begin break case class def do else
|
|
40
|
+
elsif end ensure false for if in module next nil not or redo rescue retry return self
|
|
41
|
+
super then true undef unless until when while yield
|
|
42
|
+
].to_set.freeze
|
|
43
|
+
|
|
35
44
|
ARGUMENT_DECLARATION_TYPES = [
|
|
36
45
|
:arg, :optarg, :restarg,
|
|
37
46
|
:kwarg, :kwoptarg, :kwrestarg,
|
|
@@ -192,7 +201,11 @@ module RuboCop
|
|
|
192
201
|
def regexp_captured_names(node)
|
|
193
202
|
regexp = node.to_regexp
|
|
194
203
|
|
|
195
|
-
regexp.
|
|
204
|
+
regexp.names.select { |name| local_variable_name?(name) }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def local_variable_name?(name)
|
|
208
|
+
LOCAL_VARIABLE_NAME.match?(name) && !RESERVED_WORDS.include?(name)
|
|
196
209
|
end
|
|
197
210
|
|
|
198
211
|
def process_variable_operator_assignment(node)
|
|
@@ -297,7 +297,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
|
297
297
|
|
|
298
298
|
def references(cop, see_objects) # rubocop:disable Metrics/AbcSize
|
|
299
299
|
cop_config = config.for_cop(cop)
|
|
300
|
-
urls = RuboCop::Cop::MessageAnnotator.new(config, cop.
|
|
300
|
+
urls = RuboCop::Cop::MessageAnnotator.new(config, cop.cop_name, cop_config, {}).urls
|
|
301
301
|
return '' if urls.empty? && see_objects.empty?
|
|
302
302
|
|
|
303
303
|
content = cop_subsection('References', cop)
|
|
@@ -78,7 +78,8 @@ module RuboCop
|
|
|
78
78
|
# in which case all cops are returned for compatibility.
|
|
79
79
|
return Cop::Registry.all if @config_store.nil?
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
config = @config_store.for_file(file)
|
|
82
|
+
registry_for(config).enabled(config)
|
|
82
83
|
end
|
|
83
84
|
|
|
84
85
|
def relevant_for_output?(options, target_offenses)
|
|
@@ -100,14 +101,20 @@ module RuboCop
|
|
|
100
101
|
|
|
101
102
|
# Filtering by badge keeps lazy-loaded cops unloaded; `Registry#enabled` then loads only
|
|
102
103
|
# the enabled cops, the same set the runner mobilizes.
|
|
103
|
-
def
|
|
104
|
-
@
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
options[:only].nil? || badge.match_name?(options[:only])
|
|
104
|
+
def registry_for(config)
|
|
105
|
+
@registry_by_config ||= {}.compare_by_identity
|
|
106
|
+
@registry_by_config[config] ||= Cop::Registry.global.filter_by_badge(options) do |badge|
|
|
107
|
+
mobilized_cop_badge?(badge, config)
|
|
108
108
|
end
|
|
109
109
|
end
|
|
110
110
|
|
|
111
|
+
def mobilized_cop_badge?(badge, config)
|
|
112
|
+
return false if badge.department == :Test || badge.match_name?(options[:except])
|
|
113
|
+
return false if config.missing_default_config_for?(badge)
|
|
114
|
+
|
|
115
|
+
options[:only].nil? || badge.match_name?(options[:only])
|
|
116
|
+
end
|
|
117
|
+
|
|
111
118
|
def classname_attribute_value(file)
|
|
112
119
|
@classname_attribute_value_cache ||= Hash.new do |hash, key|
|
|
113
120
|
hash[key] = key.delete_suffix('.rb').gsub("#{PathUtil.pwd}/", '').tr('/', '.')
|
data/lib/rubocop/result_cache.rb
CHANGED
|
@@ -33,8 +33,10 @@ module RuboCop
|
|
|
33
33
|
return unless File.exist?(rubocop_cache_dir)
|
|
34
34
|
|
|
35
35
|
# We know the cache entries are 3 level deep, so globing
|
|
36
|
-
# for `*/*/*` only returns files.
|
|
37
|
-
|
|
36
|
+
# for `*/*/*` only returns files. The server keeps its state files at
|
|
37
|
+
# the same depth, under `server`, so they are excluded here.
|
|
38
|
+
files = Dir[File.join(rubocop_cache_dir, '*/*/*')] -
|
|
39
|
+
Dir[File.join(rubocop_cache_dir, CacheConfig::SERVER_DIR, '*/*')]
|
|
38
40
|
return unless requires_file_removal?(files.length, config_store)
|
|
39
41
|
|
|
40
42
|
remove_oldest_files(files, rubocop_cache_dir, verbose)
|
|
@@ -228,7 +230,11 @@ module RuboCop
|
|
|
228
230
|
# don't affect caching.
|
|
229
231
|
def relevant_options_digest(options)
|
|
230
232
|
@relevant_options_digest ||= {}
|
|
231
|
-
|
|
233
|
+
# The memo key must be a snapshot, not the live Hash: callers mutate their options
|
|
234
|
+
# between runs (`--auto-gen-config` adds and removes `:only` between its phases),
|
|
235
|
+
# and a stale entry keyed by the mutated object itself could be returned for options it
|
|
236
|
+
# was not computed from.
|
|
237
|
+
@relevant_options_digest[options.dup] ||= begin
|
|
232
238
|
options = options.reject { |key, _| NON_CHANGING.include?(key) }
|
|
233
239
|
options.to_s.gsub(/[^a-z]+/i, '_')
|
|
234
240
|
end
|