rubocop 1.92.3 → 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/config.rb +6 -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/for_to_each_corrector.rb +25 -1
- 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/empty_lines_after_module_inclusion.rb +16 -4
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +25 -7
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +47 -9
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +11 -3
- data/lib/rubocop/cop/lint/number_conversion.rb +10 -0
- 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/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +45 -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 +40 -12
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +16 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
- 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 +0 -1
- data/lib/rubocop/cop/style/data_inheritance.rb +78 -9
- 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 +19 -1
- data/lib/rubocop/cop/style/empty_method.rb +11 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- 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/hash_lookup_method.rb +2 -4
- 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/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- 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/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 +4 -11
- data/lib/rubocop/cop/style/non_nil_check.rb +13 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +14 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +14 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +26 -6
- data/lib/rubocop/cop/style/redundant_each.rb +18 -0
- 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 +2 -1
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -0
- 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 +1 -1
- 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/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/struct_inheritance.rb +68 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- 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/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +16 -7
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -6
|
@@ -154,9 +154,7 @@ module RuboCop
|
|
|
154
154
|
# rewrite `!a&.intersect?(b)` returns `true` there, flipping the result.
|
|
155
155
|
return if bang == '!' && dot == '&.'
|
|
156
156
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
register_offense(node, replacement)
|
|
157
|
+
register_offense(node, receiver, argument, dot, bang)
|
|
160
158
|
end
|
|
161
159
|
alias on_csend on_send
|
|
162
160
|
|
|
@@ -168,9 +166,7 @@ module RuboCop
|
|
|
168
166
|
|
|
169
167
|
dot = node.send_node.loc.dot.source
|
|
170
168
|
bang = method_name == :any? ? '' : '!'
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
register_offense(node, replacement)
|
|
169
|
+
register_offense(node, receiver, argument, dot, bang)
|
|
174
170
|
end
|
|
175
171
|
alias on_numblock on_block
|
|
176
172
|
alias on_itblock on_block
|
|
@@ -205,13 +201,25 @@ module RuboCop
|
|
|
205
201
|
block_method == :include? && !argument.array_type?
|
|
206
202
|
end
|
|
207
203
|
|
|
208
|
-
def register_offense(node,
|
|
204
|
+
def register_offense(node, receiver, argument, dot, bang)
|
|
205
|
+
replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})"
|
|
209
206
|
message = format(MSG, replacement: replacement, existing: node.source)
|
|
210
207
|
|
|
211
208
|
add_offense(node, message: message) do |corrector|
|
|
209
|
+
next if drops_heredoc?(receiver, node) || drops_heredoc?(argument, node)
|
|
210
|
+
|
|
212
211
|
corrector.replace(node, replacement)
|
|
213
212
|
end
|
|
214
213
|
end
|
|
214
|
+
|
|
215
|
+
def drops_heredoc?(operand, node)
|
|
216
|
+
return false if operand.last_line == node.last_line
|
|
217
|
+
|
|
218
|
+
removed_lines = (operand.last_line + 1)..node.last_line
|
|
219
|
+
operand.each_node(:any_str).any? do |str|
|
|
220
|
+
str.heredoc? && removed_lines.cover?(str.loc.heredoc_end.line)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
215
223
|
end
|
|
216
224
|
end
|
|
217
225
|
end
|
|
@@ -41,6 +41,9 @@ module RuboCop
|
|
|
41
41
|
|
|
42
42
|
MSG = "Don't extend an instance initialized by `Data.define`. " \
|
|
43
43
|
'Use a block to customize the class.'
|
|
44
|
+
LEFT_OPERAND_TYPES = %i[and or irange erange match_pattern match_pattern_p].freeze
|
|
45
|
+
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn casgn op_asgn or_asgn and_asgn].freeze
|
|
46
|
+
BODY_TYPES = %i[module class sclass any_def any_block when in_pattern resbody for].freeze
|
|
44
47
|
|
|
45
48
|
minimum_target_ruby_version 3.2
|
|
46
49
|
|
|
@@ -53,10 +56,15 @@ module RuboCop
|
|
|
53
56
|
return if defines_constants?(node.body)
|
|
54
57
|
|
|
55
58
|
add_offense(node.parent_class) do |corrector|
|
|
56
|
-
|
|
59
|
+
keyword_range = range_with_surrounding_space(
|
|
60
|
+
node.loc.keyword, side: :right, newlines: false
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
corrector.remove(keyword_range)
|
|
57
64
|
corrector.replace(node.loc.operator, '=')
|
|
58
65
|
|
|
59
66
|
correct_parent(node.parent_class, corrector)
|
|
67
|
+
parenthesize_left_operand(corrector, node, keyword_range)
|
|
60
68
|
end
|
|
61
69
|
end
|
|
62
70
|
|
|
@@ -84,19 +92,80 @@ module RuboCop
|
|
|
84
92
|
# left behind, producing invalid Ruby.
|
|
85
93
|
corrector.replace(parent.loc.begin, 'do') if parent.braces?
|
|
86
94
|
corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
|
|
87
|
-
elsif (
|
|
88
|
-
corrector.remove(
|
|
95
|
+
elsif (range = range_for_empty_class_body(parent))
|
|
96
|
+
corrector.remove(range)
|
|
97
|
+
parenthesize_arguments(corrector, parent) unless statement?(parent.parent)
|
|
89
98
|
else
|
|
99
|
+
parenthesize_arguments(corrector, parent)
|
|
90
100
|
corrector.insert_after(parent, ' do')
|
|
91
101
|
end
|
|
92
102
|
end
|
|
93
103
|
|
|
94
|
-
def range_for_empty_class_body(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
end
|
|
104
|
+
def range_for_empty_class_body(data_define)
|
|
105
|
+
class_node = data_define.parent
|
|
106
|
+
return if class_node.body
|
|
107
|
+
|
|
108
|
+
range = range_between(data_define.source_range.end_pos, class_node.source_range.end_pos)
|
|
109
|
+
return range if /\A[\s;]*end\z/.match?(range.source)
|
|
110
|
+
return if end_line_kept?(class_node)
|
|
111
|
+
|
|
112
|
+
range_by_whole_lines(class_node.loc.end, include_final_newline: true)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def end_line_kept?(class_node)
|
|
116
|
+
return true if left_operand?(class_node)
|
|
117
|
+
|
|
118
|
+
class_node.loc.end.source_line.strip != 'end'
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def left_operand?(class_node)
|
|
122
|
+
parent = class_node.parent
|
|
123
|
+
return false if parent.nil?
|
|
124
|
+
return parent.receiver.equal?(class_node) if parent.call_type?
|
|
125
|
+
return parent.condition.equal?(class_node) if parent.if_type? && parent.ternary?
|
|
126
|
+
|
|
127
|
+
LEFT_OPERAND_TYPES.include?(parent.type) && parent.children.first.equal?(class_node)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def statement?(class_node)
|
|
131
|
+
parent = class_node.parent
|
|
132
|
+
return true if parent.nil? || parent.type?(:begin, :kwbegin, :rescue, :ensure)
|
|
133
|
+
return !parent.condition.equal?(class_node) if conditional_statement?(parent)
|
|
134
|
+
|
|
135
|
+
statement_child(parent).equal?(class_node)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def conditional_statement?(node)
|
|
139
|
+
return true if node.type?(:while, :until)
|
|
140
|
+
|
|
141
|
+
node.if_type? && !node.ternary?
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def statement_child(node)
|
|
145
|
+
return node.expression if node.type?(*ASSIGNMENT_TYPES)
|
|
146
|
+
return node.else_branch if node.type?(:case, :case_match)
|
|
147
|
+
return if node.any_def_type? && node.endless?
|
|
148
|
+
|
|
149
|
+
node.body if node.type?(*BODY_TYPES)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def parenthesize_arguments(corrector, data_define)
|
|
153
|
+
return unless unparenthesized_data_define?(data_define)
|
|
154
|
+
|
|
155
|
+
arguments_begin = data_define.first_argument.source_range.begin
|
|
156
|
+
corrector.replace(data_define.loc.selector.end.join(arguments_begin), '(')
|
|
157
|
+
corrector.insert_after(data_define.last_argument, ')')
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def unparenthesized_data_define?(data_define)
|
|
161
|
+
data_define.arguments.any? && !data_define.parenthesized?
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def parenthesize_left_operand(corrector, class_node, keyword_range)
|
|
165
|
+
return unless left_operand?(class_node)
|
|
166
|
+
|
|
167
|
+
corrector.insert_before(keyword_range, '(')
|
|
168
|
+
corrector.insert_after(class_node.source_range.end, ')')
|
|
100
169
|
end
|
|
101
170
|
end
|
|
102
171
|
end
|
|
@@ -72,10 +72,11 @@ module RuboCop
|
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def register_offense(node, range, arguments)
|
|
75
|
-
|
|
76
|
-
replacement = "dig(#{arguments})"
|
|
75
|
+
replacement = "dig(#{arguments.map(&:source).join(', ')})"
|
|
77
76
|
|
|
78
77
|
add_offense(range, message: format(MSG, replacement: replacement)) do |corrector|
|
|
78
|
+
next if loses_heredoc?(range, arguments)
|
|
79
|
+
|
|
79
80
|
corrector.replace(range, replacement)
|
|
80
81
|
|
|
81
82
|
comments_in_range(node).reverse_each do |comment|
|
|
@@ -88,6 +89,16 @@ module RuboCop
|
|
|
88
89
|
end
|
|
89
90
|
end
|
|
90
91
|
end
|
|
92
|
+
|
|
93
|
+
def loses_heredoc?(range, arguments)
|
|
94
|
+
return false if range.single_line?
|
|
95
|
+
|
|
96
|
+
processed_source.tokens_within(range).any? do |token|
|
|
97
|
+
next unless token.type == :tSTRING_END
|
|
98
|
+
|
|
99
|
+
arguments.none? { |arg| arg.source_range.contains?(token.pos) }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
91
102
|
end
|
|
92
103
|
end
|
|
93
104
|
end
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
|
28
28
|
_, method_name, method_arg = *method
|
|
29
29
|
return if simple_method_arg?(method_arg)
|
|
30
30
|
|
|
31
|
-
return_value =
|
|
31
|
+
return_value = returned_accumulator(body)
|
|
32
32
|
return unless return_value
|
|
33
33
|
return unless first_argument_returned?(args, return_value)
|
|
34
34
|
return if accumulator_param_assigned_to?(body, args)
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
|
47
47
|
_, method_name, method_arg = *method
|
|
48
48
|
return if simple_method_arg?(method_arg)
|
|
49
49
|
|
|
50
|
-
return unless
|
|
50
|
+
return unless returned_accumulator(body)&.source == '_1'
|
|
51
51
|
|
|
52
52
|
message = format(MSG, method: method_name)
|
|
53
53
|
add_offense(method.loc.selector, message: message) do |corrector|
|
|
@@ -112,11 +112,27 @@ module RuboCop
|
|
|
112
112
|
end
|
|
113
113
|
end
|
|
114
114
|
|
|
115
|
-
def
|
|
115
|
+
def returned_accumulator(body)
|
|
116
116
|
return unless body
|
|
117
117
|
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
value = body.begin_type? ? body.children.last : body
|
|
119
|
+
value if value&.lvar_type? && !next_changes_accumulator?(body, value)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def next_changes_accumulator?(body, value)
|
|
123
|
+
body.each_descendant(:next).any? do |jump|
|
|
124
|
+
jump.child_nodes != [value] && next_target(jump).equal?(body.parent)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def next_target(node)
|
|
129
|
+
while (parent = node.parent)
|
|
130
|
+
if (parent.any_block_type? || parent.loop_keyword?) && parent.body.equal?(node)
|
|
131
|
+
return parent
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
node = parent
|
|
135
|
+
end
|
|
120
136
|
end
|
|
121
137
|
|
|
122
138
|
def first_argument_returned?(args, return_value)
|
|
@@ -61,10 +61,13 @@ module RuboCop
|
|
|
61
61
|
MSG_CLASS_KEYWORD =
|
|
62
62
|
'Use the `class` keyword instead of `Class.new` to define an empty class.'
|
|
63
63
|
MSG_CLASS_NEW = 'Use `Class.new` instead of the `class` keyword to define an empty class.'
|
|
64
|
+
STATEMENTS_TYPES = %i[
|
|
65
|
+
begin kwbegin if case case_match while until for any_def class module sclass
|
|
66
|
+
].freeze
|
|
64
67
|
|
|
65
68
|
# @!method class_new_assignment(node)
|
|
66
69
|
def_node_matcher :class_new_assignment, <<~PATTERN
|
|
67
|
-
(casgn _ _ $(send (const
|
|
70
|
+
(casgn _ _ $(send (const {nil? cbase} :Class) :new _))
|
|
68
71
|
PATTERN
|
|
69
72
|
|
|
70
73
|
def on_casgn(node)
|
|
@@ -83,6 +86,7 @@ module RuboCop
|
|
|
83
86
|
return unless node.parent_class
|
|
84
87
|
return if (body = node.body) && !body.children.empty?
|
|
85
88
|
return if allowed_parent_class?(node.parent_class.source)
|
|
89
|
+
return if do_end_block_with_command_call?(node.parent_class)
|
|
86
90
|
|
|
87
91
|
add_offense(node, message: MSG_CLASS_NEW) do |corrector|
|
|
88
92
|
autocorrect_class_definition(corrector, node)
|
|
@@ -99,6 +103,20 @@ module RuboCop
|
|
|
99
103
|
cop_config.fetch('AllowedParentClasses', [])
|
|
100
104
|
end
|
|
101
105
|
|
|
106
|
+
def do_end_block_with_command_call?(node)
|
|
107
|
+
return false if node.type?(*STATEMENTS_TYPES)
|
|
108
|
+
return do_end_block_on_command_call?(node) if node.any_block_type?
|
|
109
|
+
|
|
110
|
+
node.each_child_node.any? { |child| do_end_block_with_command_call?(child) }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def do_end_block_on_command_call?(block)
|
|
114
|
+
send_node = block.send_node
|
|
115
|
+
return true if block.keywords? && send_node.arguments? && !send_node.parenthesized?
|
|
116
|
+
|
|
117
|
+
do_end_block_with_command_call?(send_node)
|
|
118
|
+
end
|
|
119
|
+
|
|
102
120
|
def autocorrect_class_new(corrector, node, class_new_node)
|
|
103
121
|
indent = ' ' * node.loc.column
|
|
104
122
|
class_name = constant_name(node)
|
|
@@ -48,6 +48,8 @@ module RuboCop
|
|
|
48
48
|
include ConfigurableEnforcedStyle
|
|
49
49
|
extend AutoCorrector
|
|
50
50
|
|
|
51
|
+
STRING_BEGIN_TYPES = %i[tSTRING_BEG tXSTRING_BEG].freeze
|
|
52
|
+
|
|
51
53
|
MSG_COMPACT = 'Put empty method definitions on a single line.'
|
|
52
54
|
MSG_EXPANDED = 'Put the `end` of empty method definitions on the next line.'
|
|
53
55
|
|
|
@@ -56,6 +58,8 @@ module RuboCop
|
|
|
56
58
|
return if correct_style?(node) || compact_style_disallowed?
|
|
57
59
|
|
|
58
60
|
add_offense(node) do |corrector|
|
|
61
|
+
next if heredoc_before_end?(node)
|
|
62
|
+
|
|
59
63
|
correction = corrected(node)
|
|
60
64
|
next if correction_exceeds_line_length?(correction)
|
|
61
65
|
|
|
@@ -80,6 +84,13 @@ module RuboCop
|
|
|
80
84
|
config.for_enabled_cop('Style/SingleLineMethods')['AllowIfMethodIsEmpty'] == false
|
|
81
85
|
end
|
|
82
86
|
|
|
87
|
+
def heredoc_before_end?(node)
|
|
88
|
+
range = node.source_range.adjust(begin_pos: -node.source_range.column)
|
|
89
|
+
processed_source.tokens_within(range).any? do |token|
|
|
90
|
+
STRING_BEGIN_TYPES.include?(token.type) && token.text.start_with?('<<')
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
83
94
|
def corrected(node)
|
|
84
95
|
arguments = if node.arguments?
|
|
85
96
|
args = node.arguments.map(&:source).join(', ')
|
|
@@ -56,7 +56,7 @@ module RuboCop
|
|
|
56
56
|
# changes the trailing comma where it is appended, in the same pass.
|
|
57
57
|
def self.autocorrect_incompatible_with
|
|
58
58
|
[
|
|
59
|
-
|
|
59
|
+
Style::LambdaCall, Style::MethodCallWithArgsParentheses,
|
|
60
60
|
Style::NestedParenthesizedCalls, Style::TrailingCommaInArguments
|
|
61
61
|
]
|
|
62
62
|
end
|
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
|
44
44
|
|
|
45
45
|
# @!method env_with_bracket?(node)
|
|
46
46
|
def_node_matcher :env_with_bracket?, <<~PATTERN
|
|
47
|
-
(send (const nil? :ENV) :[] $_)
|
|
47
|
+
(send (const {nil? cbase} :ENV) :[] $_)
|
|
48
48
|
PATTERN
|
|
49
49
|
|
|
50
50
|
def on_send(node)
|
|
@@ -53,7 +53,7 @@ module RuboCop
|
|
|
53
53
|
|
|
54
54
|
message = format(offense_message, key: name_node.source)
|
|
55
55
|
add_offense(node, message: message) do |corrector|
|
|
56
|
-
corrector.replace(node, new_code(name_node))
|
|
56
|
+
corrector.replace(node, new_code(node.receiver, name_node))
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
end
|
|
@@ -146,11 +146,11 @@ module RuboCop
|
|
|
146
146
|
parent.lhs == node || parent.parent&.or_type?
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
-
def new_code(name_node)
|
|
149
|
+
def new_code(env_node, name_node)
|
|
150
150
|
if default_to_nil?
|
|
151
|
-
"
|
|
151
|
+
"#{env_node.source}.fetch(#{name_node.source}, nil)"
|
|
152
152
|
else
|
|
153
|
-
"
|
|
153
|
+
"#{env_node.source}.fetch(#{name_node.source})"
|
|
154
154
|
end
|
|
155
155
|
end
|
|
156
156
|
end
|
|
@@ -60,7 +60,11 @@ module RuboCop
|
|
|
60
60
|
|
|
61
61
|
# @!method block_read?(node)
|
|
62
62
|
def_node_matcher :block_read?, <<~PATTERN
|
|
63
|
-
|
|
63
|
+
{
|
|
64
|
+
(block _ (args (arg _name)) (send (lvar _name) :read))
|
|
65
|
+
(numblock _ 1 (send (lvar :_1) :read))
|
|
66
|
+
(itblock _ :it (send (lvar :it) :read))
|
|
67
|
+
}
|
|
64
68
|
PATTERN
|
|
65
69
|
|
|
66
70
|
def on_send(node)
|
|
@@ -63,7 +63,11 @@ module RuboCop
|
|
|
63
63
|
|
|
64
64
|
# @!method block_write?(node)
|
|
65
65
|
def_node_matcher :block_write?, <<~PATTERN
|
|
66
|
-
|
|
66
|
+
{
|
|
67
|
+
(block _ (args (arg _name)) (send (lvar _name) :write $_))
|
|
68
|
+
(numblock _ 1 (send (lvar :_1) :write $_))
|
|
69
|
+
(itblock _ :it (send (lvar :it) :write $_))
|
|
70
|
+
}
|
|
67
71
|
PATTERN
|
|
68
72
|
|
|
69
73
|
def on_send(node)
|
|
@@ -71,10 +75,16 @@ module RuboCop
|
|
|
71
75
|
message = format(MSG, write_method: write_method(mode))
|
|
72
76
|
|
|
73
77
|
add_offense(write_node, message: message) do |corrector|
|
|
74
|
-
range =
|
|
75
|
-
|
|
78
|
+
range = replaced_range(node, write_node)
|
|
79
|
+
heredocs = removed_heredocs(filename, content, write_node)
|
|
80
|
+
next if foreign_heredoc_body?(range, heredocs)
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
heredocs = restored_heredocs(heredocs, [filename, content])
|
|
83
|
+
next unless restorable?(range, heredocs, write_node)
|
|
84
|
+
|
|
85
|
+
replacement = "#{write_method(mode)}(#{filename.source}, #{content.source})"
|
|
86
|
+
|
|
87
|
+
replace_and_restore_heredocs(corrector, range, replacement, heredocs)
|
|
78
88
|
end
|
|
79
89
|
end
|
|
80
90
|
end
|
|
@@ -90,9 +100,7 @@ module RuboCop
|
|
|
90
100
|
private
|
|
91
101
|
|
|
92
102
|
def file_open_write?(node)
|
|
93
|
-
content = send_write?(node) || block_write?(node)
|
|
94
|
-
write_arg if block_arg == lvar
|
|
95
|
-
end
|
|
103
|
+
content = send_write?(node) || block_write?(node)
|
|
96
104
|
return false if content&.splat_type?
|
|
97
105
|
|
|
98
106
|
yield(content) if content
|
|
@@ -102,27 +110,16 @@ module RuboCop
|
|
|
102
110
|
mode.end_with?('b') ? :binwrite : :write
|
|
103
111
|
end
|
|
104
112
|
|
|
105
|
-
def
|
|
106
|
-
|
|
107
|
-
heredocs = removed_heredocs(filename, content, write_node)
|
|
108
|
-
return replacement if heredocs.empty?
|
|
109
|
-
|
|
110
|
-
[replacement, *heredocs.map { |heredoc| heredoc_range(heredoc).source }].join("\n")
|
|
113
|
+
def replaced_range(node, write_node)
|
|
114
|
+
range_between(node.loc.selector.begin_pos, write_node.source_range.end_pos)
|
|
111
115
|
end
|
|
112
116
|
|
|
113
|
-
# Heredocs opened in the arguments keep working in the replacement, but their
|
|
114
|
-
# bodies are lost when they lie within the replaced range, so they need to be
|
|
115
|
-
# restored after the replacement.
|
|
116
117
|
def removed_heredocs(filename, content, write_node)
|
|
117
118
|
[filename, content].flat_map { |argument| find_heredocs(argument) }
|
|
118
119
|
.select { |heredoc| removed?(heredoc, write_node) }
|
|
119
120
|
.sort_by { |heredoc| heredoc.loc.heredoc_body.begin_pos }
|
|
120
121
|
end
|
|
121
122
|
|
|
122
|
-
def heredoc_range(heredoc)
|
|
123
|
-
range_between(heredoc.loc.heredoc_body.begin_pos, heredoc.loc.heredoc_end.end_pos)
|
|
124
|
-
end
|
|
125
|
-
|
|
126
123
|
def find_heredocs(node)
|
|
127
124
|
[node, *node.each_descendant(:any_str)].select do |child|
|
|
128
125
|
child.respond_to?(:heredoc?) && child.heredoc?
|
|
@@ -132,6 +129,78 @@ module RuboCop
|
|
|
132
129
|
def removed?(heredoc, write_node)
|
|
133
130
|
heredoc.loc.heredoc_end.end_pos <= write_node.source_range.end_pos
|
|
134
131
|
end
|
|
132
|
+
|
|
133
|
+
def foreign_heredoc_body?(range, heredocs)
|
|
134
|
+
find_heredocs(processed_source.ast).any? do |heredoc|
|
|
135
|
+
next false if heredocs.any? { |removed| removed.equal?(heredoc) }
|
|
136
|
+
|
|
137
|
+
range.contains?(heredoc_range(heredoc))
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def heredoc_range(heredoc)
|
|
142
|
+
range_between(heredoc.loc.heredoc_body.begin_pos, heredoc.loc.heredoc_end.end_pos)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def restored_heredocs(heredocs, arguments)
|
|
146
|
+
containers = arguments.map(&:source_range) + heredocs.map { |h| heredoc_range(h) }
|
|
147
|
+
|
|
148
|
+
heredocs.reject do |heredoc|
|
|
149
|
+
body = heredoc_range(heredoc)
|
|
150
|
+
|
|
151
|
+
containers.any? { |container| container != body && container.contains?(body) }
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def restorable?(range, heredocs, write_node)
|
|
156
|
+
return !overlaps_later_replacement?(range, write_node) if heredocs.empty?
|
|
157
|
+
|
|
158
|
+
rest = rest_of_line(range)
|
|
159
|
+
return false if line_ends_inside_literal?(rest)
|
|
160
|
+
|
|
161
|
+
!overlaps_later_replacement?(range.join(rest), write_node)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def rest_of_line(range)
|
|
165
|
+
range.end.join(processed_source.buffer.line_range(range.last_line).end)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def line_ends_inside_literal?(rest)
|
|
169
|
+
line_end = rest.end_pos
|
|
170
|
+
innermost = nil
|
|
171
|
+
processed_source.ast.each_node do |node|
|
|
172
|
+
range = node.source_range
|
|
173
|
+
innermost = node if range && range.begin_pos < line_end && line_end < range.end_pos
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
!innermost.nil? && literal?(innermost)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def literal?(node)
|
|
180
|
+
return node.percent_literal? if node.array_type?
|
|
181
|
+
|
|
182
|
+
node.type?(:any_str, :regexp, :any_sym)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def overlaps_later_replacement?(corrected, write_node)
|
|
186
|
+
processed_source.ast.each_node(:send).any? do |node|
|
|
187
|
+
other_write_node = evidence(node) { |*, other| other }
|
|
188
|
+
next false unless other_write_node
|
|
189
|
+
next false if other_write_node.equal?(write_node)
|
|
190
|
+
|
|
191
|
+
range = replaced_range(node, other_write_node)
|
|
192
|
+
corrected.begin_pos < range.begin_pos && range.begin_pos < corrected.end_pos
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def replace_and_restore_heredocs(corrector, range, replacement, heredocs)
|
|
197
|
+
return corrector.replace(range, replacement) if heredocs.empty?
|
|
198
|
+
|
|
199
|
+
rest = rest_of_line(range)
|
|
200
|
+
bodies = heredocs.map { |heredoc| "\n#{heredoc_range(heredoc).source}" }
|
|
201
|
+
|
|
202
|
+
corrector.replace(range.join(rest), "#{replacement}#{rest.source}#{bodies.join}")
|
|
203
|
+
end
|
|
135
204
|
end
|
|
136
205
|
end
|
|
137
206
|
end
|
|
@@ -62,9 +62,6 @@ module RuboCop
|
|
|
62
62
|
|
|
63
63
|
RESTRICT_ON_SEND = %i[[] fetch].freeze
|
|
64
64
|
|
|
65
|
-
# @!method env_const?(node)
|
|
66
|
-
def_node_matcher :env_const?, '(const {nil? cbase} :ENV)'
|
|
67
|
-
|
|
68
65
|
# @!method array_or_hash_const?(node)
|
|
69
66
|
def_node_matcher :array_or_hash_const?, '(const {nil? cbase} {:Array :Hash})'
|
|
70
67
|
|
|
@@ -115,7 +112,8 @@ module RuboCop
|
|
|
115
112
|
end
|
|
116
113
|
|
|
117
114
|
def env_fetch_preferred?(receiver)
|
|
118
|
-
|
|
115
|
+
receiver.global_const?(:ENV) &&
|
|
116
|
+
processed_source.registry.enabled?(Style::FetchEnvVar, config)
|
|
119
117
|
end
|
|
120
118
|
|
|
121
119
|
def offense_for_fetch?(node)
|
|
@@ -50,10 +50,6 @@ module RuboCop
|
|
|
50
50
|
(args $(mlhs (arg _)+))
|
|
51
51
|
PATTERN
|
|
52
52
|
|
|
53
|
-
def self.autocorrect_incompatible_with
|
|
54
|
-
[Layout::SingleLineBlockChain]
|
|
55
|
-
end
|
|
56
|
-
|
|
57
53
|
def on_send(node)
|
|
58
54
|
return unless (to_h_node, map_node = map_to_h(node))
|
|
59
55
|
return if to_h_node.block_literal?
|
|
@@ -102,10 +102,6 @@ module RuboCop
|
|
|
102
102
|
MSG_PRESENT = 'Use def without parentheses.'
|
|
103
103
|
MSG_MISSING = 'Use def with parentheses when there are parameters.'
|
|
104
104
|
|
|
105
|
-
def self.autocorrect_incompatible_with
|
|
106
|
-
[Style::ArgumentsForwarding]
|
|
107
|
-
end
|
|
108
|
-
|
|
109
105
|
def on_def(node)
|
|
110
106
|
args = node.arguments
|
|
111
107
|
|
|
@@ -43,12 +43,9 @@ module RuboCop
|
|
|
43
43
|
def autocorrect(corrector, node, begin_of_arguments)
|
|
44
44
|
arguments = node.arguments
|
|
45
45
|
joined_arguments = arguments.map(&:source).join(', ')
|
|
46
|
-
last_line_source_of_arguments = last_line_source_of_arguments(arguments)
|
|
47
46
|
|
|
48
|
-
if last_line_source_of_arguments.start_with?(')')
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
|
|
47
|
+
if last_line_source_of_arguments(arguments).start_with?(')')
|
|
48
|
+
remove_line_break_before_closing_parenthesis(corrector, arguments)
|
|
52
49
|
end
|
|
53
50
|
|
|
54
51
|
arguments_range = range_with_surrounding_space(arguments_range(node), side: :left)
|
|
@@ -68,6 +65,19 @@ module RuboCop
|
|
|
68
65
|
processed_source[arguments.last_line - 1].strip
|
|
69
66
|
end
|
|
70
67
|
|
|
68
|
+
def remove_line_break_before_closing_parenthesis(corrector, arguments)
|
|
69
|
+
closing_parenthesis = arguments.loc.end
|
|
70
|
+
closing_line = processed_source.buffer.line_range(closing_parenthesis.line)
|
|
71
|
+
end_of_last_argument = arguments.last.source_range.end
|
|
72
|
+
|
|
73
|
+
corrector.remove(end_of_last_argument.join(closing_parenthesis.begin))
|
|
74
|
+
corrector.insert_after(closing_line.end, text_between(end_of_last_argument, closing_line))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def text_between(range, line_range)
|
|
78
|
+
range.join(line_range.begin).source.delete_suffix("\n")
|
|
79
|
+
end
|
|
80
|
+
|
|
71
81
|
def opening_line(node)
|
|
72
82
|
node.first_line
|
|
73
83
|
end
|