rubocop 1.92.1 → 1.92.3
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/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 +16 -4
- data/lib/rubocop/config_obsoletion.rb +9 -0
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +9 -19
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +8 -18
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +27 -13
- 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 +15 -7
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +32 -11
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +35 -5
- data/lib/rubocop/cop/layout/end_alignment.rb +6 -12
- data/lib/rubocop/cop/layout/extra_spacing.rb +22 -12
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -1
- 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 +23 -5
- 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/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +11 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +31 -4
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +12 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +15 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +29 -11
- 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 +6 -6
- data/lib/rubocop/cop/lint/duplicate_require.rb +8 -1
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +19 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +27 -18
- 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/nested_percent_literal.rb +3 -0
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +42 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +4 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +66 -3
- data/lib/rubocop/cop/lint/percent_string_array.rb +75 -9
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +45 -8
- 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_splat_expansion.rb +11 -4
- 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/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 +26 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -1
- 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 +2 -2
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +34 -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_alignment_styles.rb +17 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- 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/statement_modifier.rb +14 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +4 -2
- data/lib/rubocop/cop/naming/block_forwarding.rb +9 -1
- 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 +41 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +6 -1
- data/lib/rubocop/cop/style/case_equality.rb +14 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +12 -15
- 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 +4 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +9 -2
- data/lib/rubocop/cop/style/empty_class_definition.rb +2 -3
- data/lib/rubocop/cop/style/empty_literal.rb +7 -0
- data/lib/rubocop/cop/style/empty_method.rb +16 -4
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +11 -5
- data/lib/rubocop/cop/style/endless_method.rb +64 -16
- data/lib/rubocop/cop/style/explicit_block_argument.rb +19 -4
- data/lib/rubocop/cop/style/float_division.rb +16 -1
- 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_as_last_array_item.rb +1 -1
- 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 +21 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -2
- 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/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_into_array.rb +4 -0
- 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 +12 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +9 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +18 -6
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +7 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +23 -24
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +20 -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/quoted_symbols.rb +5 -7
- data/lib/rubocop/cop/style/raise_args.rb +66 -7
- data/lib/rubocop/cop/style/reduce_to_hash.rb +32 -34
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +120 -18
- data/lib/rubocop/cop/style/redundant_each.rb +8 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +13 -9
- data/lib/rubocop/cop/style/redundant_interpolation.rb +36 -16
- data/lib/rubocop/cop/style/redundant_percent_q.rb +11 -2
- data/lib/rubocop/cop/style/redundant_self.rb +9 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +5 -1
- 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_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 +26 -2
- 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 +28 -8
- data/lib/rubocop/cop/style/struct_inheritance.rb +14 -0
- data/lib/rubocop/cop/style/symbol_array.rb +1 -3
- 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/plugin/configuration_integrator.rb +24 -3
- data/lib/rubocop/result_cache.rb +9 -3
- data/lib/rubocop/runner.rb +6 -1
- data/lib/rubocop/server/cache.rb +24 -5
- data/lib/rubocop/todo_audit.rb +61 -15
- data/lib/rubocop/version.rb +1 -1
- metadata +4 -3
|
@@ -126,15 +126,24 @@ module RuboCop
|
|
|
126
126
|
end
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
-
# Whatever sits between the last element and the `]` of an array
|
|
130
|
-
#
|
|
131
|
-
#
|
|
129
|
+
# Whatever sits between the last element and the `]` of an array followed by
|
|
130
|
+
# a comma would end up before that comma (a newline before it is a syntax error),
|
|
131
|
+
# so it's removed along with the `]`.
|
|
132
132
|
def closing_bracket_range(node, argument)
|
|
133
|
-
return argument.loc.end if argument.equal?(node.last_argument)
|
|
133
|
+
return argument.loc.end if argument.equal?(node.last_argument) && !trailing_comma?(node)
|
|
134
134
|
|
|
135
135
|
range_between(argument.children.last.source_range.end_pos, argument.loc.end.end_pos)
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
+
def trailing_comma?(node)
|
|
139
|
+
return false unless node.parenthesized?
|
|
140
|
+
|
|
141
|
+
tokens = processed_source.tokens_within(node)
|
|
142
|
+
closing_bracket_index = tokens.index(processed_source.last_token_of(node.last_argument))
|
|
143
|
+
|
|
144
|
+
tokens[closing_bracket_index + 1].comma?
|
|
145
|
+
end
|
|
146
|
+
|
|
138
147
|
def replace_selector(corrector, node)
|
|
139
148
|
if node.parenthesized? || node.single_line?
|
|
140
149
|
corrector.replace(node.loc.selector, 'push')
|
|
@@ -44,6 +44,10 @@ module RuboCop
|
|
|
44
44
|
|
|
45
45
|
minimum_target_ruby_version 3.2
|
|
46
46
|
|
|
47
|
+
def self.autocorrect_incompatible_with
|
|
48
|
+
[Style::MethodCallWithArgsParentheses]
|
|
49
|
+
end
|
|
50
|
+
|
|
47
51
|
def on_class(node)
|
|
48
52
|
return unless data_define?(node.parent_class)
|
|
49
53
|
return if defines_constants?(node.body)
|
|
@@ -43,6 +43,7 @@ module RuboCop
|
|
|
43
43
|
extend AutoCorrector
|
|
44
44
|
|
|
45
45
|
MSG = "Omit the parentheses in defs when the method doesn't accept any arguments."
|
|
46
|
+
SPACES_BEFORE_ASSIGNMENT = /\A(?:[ \t]|\\\n)+\z/.freeze
|
|
46
47
|
|
|
47
48
|
def on_def(node)
|
|
48
49
|
return unless !node.arguments? && (arguments_range = node.arguments.source_range)
|
|
@@ -64,9 +65,15 @@ module RuboCop
|
|
|
64
65
|
# signature from the body, so the parentheses can be removed (`def foo; end`).
|
|
65
66
|
return false if token_after_argument == ';'
|
|
66
67
|
|
|
67
|
-
return
|
|
68
|
+
return node.single_line? unless node.endless?
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
!spaces_before_assignment?(node, end_pos)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def spaces_before_assignment?(node, end_pos)
|
|
74
|
+
source = range_between(end_pos, node.loc.assignment.begin_pos).source
|
|
75
|
+
|
|
76
|
+
SPACES_BEFORE_ASSIGNMENT.match?(source)
|
|
70
77
|
end
|
|
71
78
|
end
|
|
72
79
|
end
|
|
@@ -107,11 +107,10 @@ module RuboCop
|
|
|
107
107
|
corrector.replace(node, "class #{class_name} < #{parent_class_name}\n#{indent}end")
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
-
# Preserve any namespace on the assigned constant (e.g. `Foo::Bar`),
|
|
110
|
+
# Preserve any namespace on the assigned constant (e.g. `Foo::Bar` or `::Bar`),
|
|
111
111
|
# which `node.name` drops.
|
|
112
112
|
def constant_name(node)
|
|
113
|
-
|
|
114
|
-
namespace ? "#{namespace.source}::#{node.name}" : node.name
|
|
113
|
+
node.loc.name.with(begin_pos: node.source_range.begin_pos).source
|
|
115
114
|
end
|
|
116
115
|
|
|
117
116
|
def autocorrect_class_definition(corrector, node)
|
|
@@ -34,6 +34,12 @@ module RuboCop
|
|
|
34
34
|
|
|
35
35
|
RESTRICT_ON_SEND = %i[new [] Array Hash].freeze
|
|
36
36
|
|
|
37
|
+
# A hash literal as the first argument needs the parentheses of the call,
|
|
38
|
+
# which `Style/MethodCallWithArgsParentheses` removes in the same pass.
|
|
39
|
+
def self.autocorrect_incompatible_with
|
|
40
|
+
[Style::MethodCallWithArgsParentheses]
|
|
41
|
+
end
|
|
42
|
+
|
|
37
43
|
# @!method array_node(node)
|
|
38
44
|
def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new (array)?)'
|
|
39
45
|
|
|
@@ -98,6 +104,7 @@ module RuboCop
|
|
|
98
104
|
def first_argument_unparenthesized?(node)
|
|
99
105
|
parent = node.parent
|
|
100
106
|
return false unless parent && %i[send super zsuper].include?(parent.type)
|
|
107
|
+
return false if parent.operator_method? && !parent.loc.dot
|
|
101
108
|
|
|
102
109
|
node.equal?(parent.first_argument) && !parentheses?(node.parent)
|
|
103
110
|
end
|
|
@@ -81,17 +81,26 @@ module RuboCop
|
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def corrected(node)
|
|
84
|
-
scope = node.receiver ? "#{node.receiver.source}." : ''
|
|
85
84
|
arguments = if node.arguments?
|
|
86
85
|
args = node.arguments.map(&:source).join(', ')
|
|
87
86
|
|
|
88
87
|
parentheses?(node.arguments) ? "(#{args})" : " #{args}"
|
|
89
88
|
end
|
|
90
|
-
signature = [scope, node.method_name, arguments].join
|
|
89
|
+
signature = [scope(node), node.method_name, arguments].join
|
|
91
90
|
|
|
92
91
|
["def #{signature}", 'end'].join(joint(node))
|
|
93
92
|
end
|
|
94
93
|
|
|
94
|
+
def scope(node)
|
|
95
|
+
return '' unless (receiver = node.receiver)
|
|
96
|
+
|
|
97
|
+
parenthesized_receiver?(node) ? "(#{receiver.source})." : "#{receiver.source}."
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def parenthesized_receiver?(node)
|
|
101
|
+
node.loc.keyword.end.join(node.receiver.source_range.begin).source.include?('(')
|
|
102
|
+
end
|
|
103
|
+
|
|
95
104
|
def correction_exceeds_line_length?(correction)
|
|
96
105
|
compact_style? && max_line_length && correction.size > max_line_length
|
|
97
106
|
end
|
|
@@ -102,10 +111,13 @@ module RuboCop
|
|
|
102
111
|
compact_style? ? '; ' : "\n#{indent}"
|
|
103
112
|
end
|
|
104
113
|
|
|
105
|
-
# A method with an argument spanning multiple lines (e.g. a hash default
|
|
114
|
+
# A method with a receiver or an argument spanning multiple lines (e.g. a hash default
|
|
106
115
|
# value) can't be put on a single line, so it's as compact as it gets.
|
|
107
116
|
def compact?(node)
|
|
108
|
-
|
|
117
|
+
return true if node.single_line?
|
|
118
|
+
return true if node.receiver&.multiline?
|
|
119
|
+
|
|
120
|
+
node.arguments.any?(&:multiline?)
|
|
109
121
|
end
|
|
110
122
|
|
|
111
123
|
def expanded?(node)
|
|
@@ -128,14 +128,12 @@ module RuboCop
|
|
|
128
128
|
end
|
|
129
129
|
|
|
130
130
|
def ternary_style_autocorrect(node, outcome, keyword)
|
|
131
|
+
return if outcome.nil? || outcome.multiline?
|
|
132
|
+
|
|
131
133
|
add_offense(node, message: MSG_TERNARY) do |corrector|
|
|
132
134
|
next if empty_string_conditional_in_condition?(node)
|
|
133
135
|
|
|
134
|
-
|
|
135
|
-
tail = outcome.source_range.end.join(node.source_range.end)
|
|
136
|
-
|
|
137
|
-
corrector.remove(head)
|
|
138
|
-
corrector.replace(tail, " #{keyword} #{node.condition.source}")
|
|
136
|
+
autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
|
|
139
137
|
end
|
|
140
138
|
end
|
|
141
139
|
|
|
@@ -146,6 +144,14 @@ module RuboCop
|
|
|
146
144
|
empty_if_outcome?(node) || empty_else_outcome?(node)
|
|
147
145
|
end
|
|
148
146
|
end
|
|
147
|
+
|
|
148
|
+
def autocorrect_to_trailing_conditional(corrector, node, outcome, keyword)
|
|
149
|
+
head = node.source_range.begin.join(outcome.source_range.begin)
|
|
150
|
+
tail = outcome.source_range.end.join(node.source_range.end)
|
|
151
|
+
|
|
152
|
+
corrector.remove(head)
|
|
153
|
+
corrector.replace(tail, " #{keyword} #{node.condition.source}")
|
|
154
|
+
end
|
|
149
155
|
end
|
|
150
156
|
end
|
|
151
157
|
end
|
|
@@ -147,12 +147,26 @@ module RuboCop
|
|
|
147
147
|
|
|
148
148
|
# `Style/MethodDefParentheses` adds the same parentheses a parameter list needs in
|
|
149
149
|
# an endless definition, so letting both correct in the same pass would double them.
|
|
150
|
-
# `Style/GuardClause`
|
|
151
|
-
# which an endless definition cannot hold.
|
|
150
|
+
# `Style/GuardClause` and `Style/IdenticalConditionalBranches` turn a body that is
|
|
151
|
+
# a single conditional into several statements, which an endless definition cannot hold.
|
|
152
152
|
# `Style/RaiseArgs` would drop the parentheses of a `raise` that becomes
|
|
153
153
|
# the body in the same pass, which a method passed as an argument needs.
|
|
154
|
+
# `Style/IfUnlessModifier` and `Style/WhileUntilModifier` would turn the body into
|
|
155
|
+
# a modifier form, which then applies to the whole endless definition.
|
|
156
|
+
# `Lint/LiteralAsCondition` and `Lint/UselessTimes` replace the body with
|
|
157
|
+
# the statements inside it, which an endless definition may not hold.
|
|
154
158
|
def self.autocorrect_incompatible_with
|
|
155
|
-
[
|
|
159
|
+
[
|
|
160
|
+
Lint::LiteralAsCondition,
|
|
161
|
+
Lint::UselessTimes,
|
|
162
|
+
Style::GuardClause,
|
|
163
|
+
Style::IdenticalConditionalBranches,
|
|
164
|
+
Style::IfUnlessModifier,
|
|
165
|
+
Style::MethodCallWithArgsParentheses,
|
|
166
|
+
Style::MethodDefParentheses,
|
|
167
|
+
Style::RaiseArgs,
|
|
168
|
+
Style::WhileUntilModifier
|
|
169
|
+
]
|
|
156
170
|
end
|
|
157
171
|
|
|
158
172
|
def on_def(node)
|
|
@@ -178,7 +192,7 @@ module RuboCop
|
|
|
178
192
|
return if node.single_line? || style == :allow_always
|
|
179
193
|
|
|
180
194
|
add_offense(node, message: MSG_MULTI_LINE) do |corrector|
|
|
181
|
-
next if
|
|
195
|
+
next if nested_in_multiline_correction?(node)
|
|
182
196
|
|
|
183
197
|
correct_to_multiline(corrector, node)
|
|
184
198
|
end
|
|
@@ -187,38 +201,59 @@ module RuboCop
|
|
|
187
201
|
def handle_require_single_line_style(node)
|
|
188
202
|
if node.endless? && node.multiline?
|
|
189
203
|
add_offense(node, message: MSG_MULTI_LINE) do |corrector|
|
|
204
|
+
next if nested_in_multiline_correction?(node)
|
|
205
|
+
|
|
190
206
|
correct_to_multiline(corrector, node)
|
|
191
207
|
end
|
|
192
|
-
elsif
|
|
193
|
-
return if too_long_when_made_endless?(node)
|
|
194
|
-
|
|
208
|
+
elsif convertible_to_endless?(node) && single_line_when_made_endless?(node)
|
|
195
209
|
add_offense(node, message: MSG_REQUIRE_SINGLE) do |corrector|
|
|
210
|
+
next if nested_in_multiline_correction?(node)
|
|
211
|
+
|
|
196
212
|
correct_to_endless(corrector, node)
|
|
197
213
|
end
|
|
198
214
|
end
|
|
199
215
|
end
|
|
200
216
|
|
|
201
217
|
def handle_require_always_style(node)
|
|
202
|
-
return
|
|
203
|
-
return if too_long_when_made_endless?(node)
|
|
218
|
+
return unless convertible_to_endless?(node)
|
|
204
219
|
|
|
205
220
|
add_offense(node, message: MSG_REQUIRE_ALWAYS) do |corrector|
|
|
206
221
|
correct_to_endless(corrector, node)
|
|
207
222
|
end
|
|
208
223
|
end
|
|
209
224
|
|
|
225
|
+
def convertible_to_endless?(node)
|
|
226
|
+
!node.endless? && can_be_made_endless?(node) &&
|
|
227
|
+
!too_long_when_made_endless?(node) && !ambiguous_when_made_endless?(node)
|
|
228
|
+
end
|
|
229
|
+
|
|
210
230
|
def handle_disallow_style(node)
|
|
211
231
|
return unless node.endless?
|
|
212
232
|
|
|
213
233
|
add_offense(node) do |corrector|
|
|
214
|
-
next if
|
|
234
|
+
next if nested_in_multiline_correction?(node)
|
|
215
235
|
|
|
216
236
|
correct_to_multiline(corrector, node)
|
|
217
237
|
end
|
|
218
238
|
end
|
|
219
239
|
|
|
220
|
-
def
|
|
221
|
-
node.
|
|
240
|
+
def nested_in_multiline_correction?(node)
|
|
241
|
+
node.each_ancestor(:any_def).any? do |ancestor|
|
|
242
|
+
corrected_to_multiline?(ancestor)
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def corrected_to_multiline?(node)
|
|
247
|
+
return false if !node.endless? || use_heredoc?(node)
|
|
248
|
+
|
|
249
|
+
case style
|
|
250
|
+
when :disallow
|
|
251
|
+
true
|
|
252
|
+
when :allow_single_line, :require_single_line
|
|
253
|
+
node.multiline?
|
|
254
|
+
else
|
|
255
|
+
false
|
|
256
|
+
end
|
|
222
257
|
end
|
|
223
258
|
|
|
224
259
|
def use_heredoc?(node)
|
|
@@ -263,10 +298,6 @@ module RuboCop
|
|
|
263
298
|
RUBY
|
|
264
299
|
end
|
|
265
300
|
|
|
266
|
-
def receiver(node)
|
|
267
|
-
node.receiver ? "#{node.receiver.source}#{node.loc.operator.source}" : ''
|
|
268
|
-
end
|
|
269
|
-
|
|
270
301
|
def arguments(node, missing = '')
|
|
271
302
|
return missing unless node.arguments.any?
|
|
272
303
|
return parenthesized_arguments(node) if unparenthesized_arguments?(node)
|
|
@@ -297,6 +328,23 @@ module RuboCop
|
|
|
297
328
|
!endless_replacement(node).include?("\n")
|
|
298
329
|
end
|
|
299
330
|
|
|
331
|
+
def ambiguous_when_made_endless?(node)
|
|
332
|
+
(operation = ambiguous_operation(node)) && ambiguous_enclosing_operation?(operation)
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# @!method ambiguous_operation(node)
|
|
336
|
+
def_node_matcher :ambiguous_operation, <<~PATTERN
|
|
337
|
+
^${
|
|
338
|
+
(if _ <any_def _>)
|
|
339
|
+
({and or} any_def _)
|
|
340
|
+
({while until} _ any_def)
|
|
341
|
+
}
|
|
342
|
+
PATTERN
|
|
343
|
+
|
|
344
|
+
def ambiguous_enclosing_operation?(operation)
|
|
345
|
+
operation.operator_keyword? || operation.modifier_form?
|
|
346
|
+
end
|
|
347
|
+
|
|
300
348
|
def too_long_when_made_endless?(node)
|
|
301
349
|
return false unless config.cop_enabled?('Layout/LineLength')
|
|
302
350
|
|
|
@@ -50,11 +50,15 @@ module RuboCop
|
|
|
50
50
|
(block $_ (args $...) (yield $...))
|
|
51
51
|
PATTERN
|
|
52
52
|
|
|
53
|
-
# `Style/LambdaCall` rewrites the `call` that the block argument is appended to,
|
|
54
|
-
# `Style/
|
|
55
|
-
#
|
|
53
|
+
# `Style/LambdaCall` rewrites the `call` that the block argument is appended to,
|
|
54
|
+
# `Style/MethodCallWithArgsParentheses` and `Style/NestedParenthesizedCalls` add the same
|
|
55
|
+
# parentheses around a last argument without them, and `Style/TrailingCommaInArguments`
|
|
56
|
+
# changes the trailing comma where it is appended, in the same pass.
|
|
56
57
|
def self.autocorrect_incompatible_with
|
|
57
|
-
[
|
|
58
|
+
[
|
|
59
|
+
Lint::UnusedMethodArgument, Style::LambdaCall, Style::MethodCallWithArgsParentheses,
|
|
60
|
+
Style::NestedParenthesizedCalls, Style::TrailingCommaInArguments
|
|
61
|
+
]
|
|
58
62
|
end
|
|
59
63
|
|
|
60
64
|
def initialize(config = nil, options = nil)
|
|
@@ -166,12 +170,23 @@ module RuboCop
|
|
|
166
170
|
|
|
167
171
|
def insert_argument(node, corrector, block_name)
|
|
168
172
|
last_arg = node.last_argument
|
|
173
|
+
parenthesize_arguments(last_arg, corrector) if command_call?(last_arg)
|
|
169
174
|
arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
|
|
170
175
|
replacement = " &#{block_name}"
|
|
171
176
|
replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
|
|
172
177
|
corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
|
|
173
178
|
end
|
|
174
179
|
|
|
180
|
+
def command_call?(node)
|
|
181
|
+
node.call_type? && node.arguments? && !node.parenthesized? &&
|
|
182
|
+
!node.operator_method? && !node.setter_method?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def parenthesize_arguments(node, corrector)
|
|
186
|
+
corrector.replace(node.loc.selector.end.join(node.first_argument.source_range.begin), '(')
|
|
187
|
+
corrector.insert_after(node.last_argument, ')')
|
|
188
|
+
end
|
|
189
|
+
|
|
175
190
|
def correct_call_node(node, corrector, block_name)
|
|
176
191
|
new_arguments = if node.zsuper_type?
|
|
177
192
|
args = build_new_arguments_for_zsuper(node) << "&#{block_name}"
|
|
@@ -146,11 +146,26 @@ module RuboCop
|
|
|
146
146
|
end
|
|
147
147
|
|
|
148
148
|
def comment_before_to_f?(node)
|
|
149
|
+
return true if comment_around_fdiv_operator?(node)
|
|
150
|
+
|
|
149
151
|
operands_losing_to_f(node).any? do |operand|
|
|
150
152
|
next false unless to_f_method?(operand)
|
|
151
153
|
|
|
152
154
|
range = operand.receiver.source_range.end.join(operand.loc.selector.begin)
|
|
153
|
-
|
|
155
|
+
comment_in_range?(range)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def comment_around_fdiv_operator?(node)
|
|
160
|
+
return false unless style == :fdiv
|
|
161
|
+
|
|
162
|
+
range = node.receiver.source_range.end.join(node.first_argument.source_range.begin)
|
|
163
|
+
comment_in_range?(range)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def comment_in_range?(range)
|
|
167
|
+
processed_source.each_comment_in_lines(range.line..range.last_line).any? do |comment|
|
|
168
|
+
range.contains?(comment.source_range)
|
|
154
169
|
end
|
|
155
170
|
end
|
|
156
171
|
|
|
@@ -53,13 +53,16 @@ module RuboCop
|
|
|
53
53
|
PREFER_EACH = 'Prefer `each` over `for`.'
|
|
54
54
|
PREFER_FOR = 'Prefer `for` over `each`.'
|
|
55
55
|
IMPLICIT_PARAMETERS = %i[_1 _2 _3 _4 _5 _6 _7 _8 _9 it].to_set.freeze
|
|
56
|
+
SCOPE_TYPES = %i[any_def class module sclass].freeze
|
|
56
57
|
|
|
58
|
+
# `Style/BlockDelimiters` can turn a brace block in the receiver into `do...end`
|
|
59
|
+
# in the same pass, which is a syntax error in the `for` collection.
|
|
57
60
|
# `Style/RedundantBegin` removes the `begin` of a block body in the same pass,
|
|
58
61
|
# leaving a `rescue` or `ensure` clause directly in the `for` body.
|
|
59
62
|
# `Style/ItBlockParameter` replaces block parameter references in the body
|
|
60
63
|
# with `it` in the same pass, while they become `for` loop variables.
|
|
61
64
|
def self.autocorrect_incompatible_with
|
|
62
|
-
[Style::RedundantBegin, Style::ItBlockParameter]
|
|
65
|
+
[Style::BlockDelimiters, Style::RedundantBegin, Style::ItBlockParameter]
|
|
63
66
|
end
|
|
64
67
|
|
|
65
68
|
def on_for(node)
|
|
@@ -80,10 +83,9 @@ module RuboCop
|
|
|
80
83
|
|
|
81
84
|
if style == :for
|
|
82
85
|
return unless node.receiver
|
|
83
|
-
return unless
|
|
84
|
-
return if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
|
|
86
|
+
return unless correctable_to_for?(node)
|
|
85
87
|
|
|
86
|
-
add_offense(node, message: PREFER_FOR) do |corrector|
|
|
88
|
+
add_offense(offense_range(node), message: PREFER_FOR) do |corrector|
|
|
87
89
|
EachToForCorrector.new(node).call(corrector)
|
|
88
90
|
opposite_style_detected
|
|
89
91
|
end
|
|
@@ -126,6 +128,13 @@ module RuboCop
|
|
|
126
128
|
node.multiline? && node.method?(:each) && !node.send_node.arguments?
|
|
127
129
|
end
|
|
128
130
|
|
|
131
|
+
def correctable_to_for?(node)
|
|
132
|
+
return false unless convertible_to_for?(node)
|
|
133
|
+
return false if rescue_or_ensure_body?(node) || do_end_block_in_collection?(node)
|
|
134
|
+
|
|
135
|
+
!leaked_variable_called_later?(node)
|
|
136
|
+
end
|
|
137
|
+
|
|
129
138
|
def convertible_to_for?(node)
|
|
130
139
|
return false if node.type?(:numblock, :itblock)
|
|
131
140
|
return true unless node.arguments?
|
|
@@ -143,6 +152,52 @@ module RuboCop
|
|
|
143
152
|
def do_end_block_in_collection?(node)
|
|
144
153
|
node.receiver.each_node(:any_block).any?(&:keywords?)
|
|
145
154
|
end
|
|
155
|
+
|
|
156
|
+
def leaked_variable_called_later?(node)
|
|
157
|
+
names = leaked_variable_names(node)
|
|
158
|
+
return false if names.empty?
|
|
159
|
+
|
|
160
|
+
calls = innermost_scope(node, :any_block, *SCOPE_TYPES).each_descendant(:send)
|
|
161
|
+
scope = innermost_scope(node, *SCOPE_TYPES)
|
|
162
|
+
|
|
163
|
+
calls.any? { |call| leaked_name_called?(call, node, names, scope) }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def leaked_name_called?(call, node, names, scope)
|
|
167
|
+
return false unless names.include?(call.method_name) && variable_like_call?(call)
|
|
168
|
+
return false if call.source_range.begin_pos < node.source_range.end_pos
|
|
169
|
+
|
|
170
|
+
innermost_scope(call, *SCOPE_TYPES).equal?(scope)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def leaked_variable_names(node)
|
|
174
|
+
names = node.arguments.each_descendant(:arg, :restarg).filter_map(&:name)
|
|
175
|
+
return names unless node.body
|
|
176
|
+
|
|
177
|
+
assigned_names = node.body.each_node(:lvasgn).filter_map do |lvasgn|
|
|
178
|
+
lvasgn.name if innermost_scope(lvasgn, :any_block, *SCOPE_TYPES).equal?(node)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
names + assigned_names
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def innermost_scope(node, *types)
|
|
185
|
+
node.each_ancestor(*types).first || processed_source.ast
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def variable_like_call?(send_node)
|
|
189
|
+
!send_node.receiver && !send_node.parenthesized?
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def offense_range(node)
|
|
193
|
+
range = node.source_range
|
|
194
|
+
selector = node.send_node.loc.selector
|
|
195
|
+
return range if same_line?(range, selector)
|
|
196
|
+
|
|
197
|
+
line = selector.source_buffer.line_range(selector.line)
|
|
198
|
+
|
|
199
|
+
range.with(begin_pos: line.begin_pos + line.source.index(/\S/))
|
|
200
|
+
end
|
|
146
201
|
end
|
|
147
202
|
end
|
|
148
203
|
end
|
|
@@ -100,14 +100,18 @@ module RuboCop
|
|
|
100
100
|
# end
|
|
101
101
|
# end
|
|
102
102
|
#
|
|
103
|
-
class GuardClause < Base
|
|
103
|
+
class GuardClause < Base # rubocop:disable Metrics/ClassLength
|
|
104
104
|
extend AutoCorrector
|
|
105
105
|
include RangeHelp
|
|
106
106
|
include MinBodyLength
|
|
107
107
|
include StatementModifier
|
|
108
|
+
include RescueNode
|
|
108
109
|
|
|
109
110
|
MSG = 'Use a guard clause (`%<example>s`) instead of wrapping the ' \
|
|
110
111
|
'code inside a conditional expression.'
|
|
112
|
+
STATEMENT_PARENT_TYPES = %i[
|
|
113
|
+
begin kwbegin block numblock itblock class module sclass ensure
|
|
114
|
+
].freeze
|
|
111
115
|
|
|
112
116
|
def self.autocorrect_incompatible_with
|
|
113
117
|
[Style::MissingElse]
|
|
@@ -191,6 +195,7 @@ module RuboCop
|
|
|
191
195
|
|
|
192
196
|
add_offense(node.loc.keyword, message: format(MSG, example: example)) do |corrector|
|
|
193
197
|
next if node.else? && guard.nil?
|
|
198
|
+
next if code_after_end_needs_conditional?(node, guard)
|
|
194
199
|
|
|
195
200
|
autocorrect(corrector, node, example, replacement, guard)
|
|
196
201
|
end
|
|
@@ -224,9 +229,7 @@ module RuboCop
|
|
|
224
229
|
end
|
|
225
230
|
|
|
226
231
|
def find_heredoc_argument(node)
|
|
227
|
-
return unless node
|
|
228
|
-
|
|
229
|
-
node = node.children.first while node.begin_type?
|
|
232
|
+
return unless (node = unwrap_begin(node))
|
|
230
233
|
return node if heredoc?(node)
|
|
231
234
|
return unless node.call_type?
|
|
232
235
|
|
|
@@ -238,12 +241,17 @@ module RuboCop
|
|
|
238
241
|
find_heredoc_argument(node.receiver)
|
|
239
242
|
end
|
|
240
243
|
|
|
244
|
+
def unwrap_begin(node)
|
|
245
|
+
node = node.children.first while node&.begin_type?
|
|
246
|
+
node
|
|
247
|
+
end
|
|
248
|
+
|
|
241
249
|
def close_multiline_guard_clause(corrector, range, heredoc, guard)
|
|
242
250
|
corrector.insert_after(guard && heredoc ? heredoc.loc.heredoc_end : range, "\nend")
|
|
243
251
|
end
|
|
244
252
|
|
|
245
253
|
def autocorrect_heredoc_argument(corrector, node, heredoc_node, leave_branch, guard)
|
|
246
|
-
|
|
254
|
+
remove_end(corrector, node.loc.end)
|
|
247
255
|
return unless node.else?
|
|
248
256
|
|
|
249
257
|
if leave_branch
|
|
@@ -255,6 +263,14 @@ module RuboCop
|
|
|
255
263
|
remove_whole_lines(corrector, range_of_branch_to_remove(node, guard))
|
|
256
264
|
end
|
|
257
265
|
|
|
266
|
+
def remove_end(corrector, end_range)
|
|
267
|
+
if range_by_whole_lines(end_range).source.strip == end_range.source
|
|
268
|
+
remove_whole_lines(corrector, end_range)
|
|
269
|
+
else
|
|
270
|
+
corrector.remove(end_range)
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
258
274
|
def range_of_branch_to_remove(node, guard)
|
|
259
275
|
branch = case guard
|
|
260
276
|
when :if then node.if_branch
|
|
@@ -283,15 +299,35 @@ module RuboCop
|
|
|
283
299
|
end
|
|
284
300
|
|
|
285
301
|
def accepted_form?(node, ending: false)
|
|
286
|
-
accepted_if?(node, ending) || node.condition.multiline?
|
|
287
|
-
|
|
302
|
+
return true if accepted_if?(node, ending) || node.condition.multiline?
|
|
303
|
+
|
|
304
|
+
!statement?(node)
|
|
288
305
|
end
|
|
289
306
|
|
|
290
|
-
#
|
|
291
|
-
#
|
|
292
|
-
|
|
307
|
+
# A guard clause is followed by the rest of the code as separate statements,
|
|
308
|
+
# so there is no room for them where a single expression is expected,
|
|
309
|
+
# such as an assignment value, a hash value, or the body of an endless method.
|
|
310
|
+
def statement?(node)
|
|
293
311
|
parent = node.parent
|
|
294
|
-
parent
|
|
312
|
+
return true if parent.nil? || parent.type?(*STATEMENT_PARENT_TYPES)
|
|
313
|
+
return !parent.endless? if parent.any_def_type?
|
|
314
|
+
return !rescue_modifier?(parent) if parent.resbody_type?
|
|
315
|
+
return !rescue_modifier?(parent.resbody_branches.first) if parent.rescue_type?
|
|
316
|
+
|
|
317
|
+
statement_branch?(node, parent)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def statement_branch?(node, parent)
|
|
321
|
+
case parent.type
|
|
322
|
+
when :if
|
|
323
|
+
!parent.ternary? && !node.equal?(parent.condition)
|
|
324
|
+
when :when, :in_pattern, :while, :until, :for
|
|
325
|
+
node.equal?(parent.body)
|
|
326
|
+
when :case, :case_match
|
|
327
|
+
node.equal?(parent.else_branch)
|
|
328
|
+
else
|
|
329
|
+
false
|
|
330
|
+
end
|
|
295
331
|
end
|
|
296
332
|
|
|
297
333
|
def trivial?(node)
|
|
@@ -300,6 +336,15 @@ module RuboCop
|
|
|
300
336
|
node.branches.one? && !node.if_branch.if_type? && !node.if_branch.begin_type?
|
|
301
337
|
end
|
|
302
338
|
|
|
339
|
+
def code_after_end_needs_conditional?(node, guard)
|
|
340
|
+
guard_branch = guard == :else ? node.else_branch : node.if_branch
|
|
341
|
+
return false unless find_heredoc_argument(guard_branch)
|
|
342
|
+
|
|
343
|
+
end_keyword = node.loc.end
|
|
344
|
+
|
|
345
|
+
!end_keyword.source_line[end_keyword.last_column..].match?(/\A\s*(?:\z|[#;)\]}])/)
|
|
346
|
+
end
|
|
347
|
+
|
|
303
348
|
def accepted_if?(node, ending)
|
|
304
349
|
return true if node.modifier_form? || node.ternary? || node.elsif_conditional? ||
|
|
305
350
|
assigned_lvar_used_in_if_branch?(node)
|
|
@@ -100,7 +100,9 @@ module RuboCop
|
|
|
100
100
|
|
|
101
101
|
def register_offense_for_hash(node, hash_argument)
|
|
102
102
|
add_offense(node, message: MSG_LITERAL_HASH_ARG) do |corrector|
|
|
103
|
-
|
|
103
|
+
replacement = hash_argument.source
|
|
104
|
+
replacement = "{#{replacement}}" unless hash_argument.braces?
|
|
105
|
+
corrector.replace(node, replacement)
|
|
104
106
|
add_parentheses_to_parent_call(node, corrector)
|
|
105
107
|
end
|
|
106
108
|
end
|
|
@@ -59,7 +59,7 @@ module RuboCop
|
|
|
59
59
|
return unless last_replaceable_node
|
|
60
60
|
return unless arguments.size > 1
|
|
61
61
|
|
|
62
|
-
range = last_replaceable_node.selector.join(node.
|
|
62
|
+
range = last_replaceable_node.selector.join(node.source_range.end)
|
|
63
63
|
replacement = replacement(arguments)
|
|
64
64
|
message = format(MSG, replacement: replacement)
|
|
65
65
|
|