rubocop 1.60.2 → 1.62.1
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/assets/output.css.erb +159 -0
- data/assets/output.html.erb +1 -160
- data/config/default.yml +41 -12
- data/lib/rubocop/cli/command/auto_generate_config.rb +12 -3
- data/lib/rubocop/cli/command/lsp.rb +2 -2
- data/lib/rubocop/cli.rb +6 -1
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_finder.rb +12 -2
- data/lib/rubocop/config_validator.rb +14 -5
- data/lib/rubocop/cop/autocorrect_logic.rb +6 -1
- data/lib/rubocop/cop/base.rb +17 -8
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
- data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +122 -28
- data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -3
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +14 -9
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
- data/lib/rubocop/cop/lint/script_permission.rb +3 -3
- data/lib/rubocop/cop/lint/syntax.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -2
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +6 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -2
- data/lib/rubocop/cop/registry.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +29 -8
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +3 -3
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +4 -5
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +6 -2
- data/lib/rubocop/cop/style/inverse_methods.rb +8 -8
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -5
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +5 -8
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +4 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
- data/lib/rubocop/cop/style/object_then.rb +5 -3
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -3
- data/lib/rubocop/cop/style/raise_args.rb +4 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +2 -2
- data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +4 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +17 -6
- data/lib/rubocop/cop/style/redundant_return.rb +6 -0
- data/lib/rubocop/cop/style/sample.rb +1 -3
- data/lib/rubocop/cop/utils/regexp_ranges.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +4 -2
- data/lib/rubocop/directive_comment.rb +10 -8
- data/lib/rubocop/formatter/html_formatter.rb +30 -10
- data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
- data/lib/rubocop/lsp/logger.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +1 -1
- data/lib/rubocop/lsp/runtime.rb +1 -1
- data/lib/rubocop/lsp/server.rb +5 -2
- data/lib/rubocop/lsp/severity.rb +1 -1
- data/lib/rubocop/lsp.rb +29 -0
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/options.rb +11 -0
- data/lib/rubocop/path_util.rb +6 -2
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/expect_offense.rb +8 -8
- data/lib/rubocop/rspec/shared_contexts.rb +36 -17
- data/lib/rubocop/rspec/support.rb +2 -1
- data/lib/rubocop/runner.rb +9 -2
- data/lib/rubocop/target_finder.rb +84 -78
- data/lib/rubocop/target_ruby.rb +82 -80
- data/lib/rubocop/version.rb +18 -3
- metadata +9 -6
@@ -18,6 +18,7 @@ module RuboCop
|
|
18
18
|
# @api private
|
19
19
|
CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect].to_set.freeze
|
20
20
|
CONFIG_CHECK_DEPARTMENTS = %w[pending override_department].freeze
|
21
|
+
CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
|
21
22
|
private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
|
22
23
|
|
23
24
|
def_delegators :@config, :smart_loaded_path, :for_all_cops
|
@@ -248,23 +249,31 @@ module RuboCop
|
|
248
249
|
end
|
249
250
|
end
|
250
251
|
|
252
|
+
# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
|
251
253
|
def check_cop_config_value(hash, parent = nil)
|
252
254
|
hash.each do |key, value|
|
253
255
|
check_cop_config_value(value, key) if value.is_a?(Hash)
|
254
256
|
|
255
257
|
next unless CONFIG_CHECK_KEYS.include?(key) && value.is_a?(String)
|
256
258
|
|
257
|
-
|
259
|
+
if key == 'Enabled' && !CONFIG_CHECK_DEPARTMENTS.include?(value)
|
260
|
+
supposed_values = 'a boolean'
|
261
|
+
elsif key == 'AutoCorrect' && !CONFIG_CHECK_AUTOCORRECTS.include?(value)
|
262
|
+
supposed_values = '`always`, `contextual`, `disabled`, or a boolean'
|
263
|
+
else
|
264
|
+
next
|
265
|
+
end
|
258
266
|
|
259
|
-
raise ValidationError,
|
267
|
+
raise ValidationError, param_error_message(parent, key, value, supposed_values)
|
260
268
|
end
|
261
269
|
end
|
270
|
+
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
|
262
271
|
|
263
272
|
# FIXME: Handling colors in exception messages like this is ugly.
|
264
|
-
def
|
273
|
+
def param_error_message(parent, key, value, supposed_values)
|
265
274
|
"#{Rainbow('').reset}" \
|
266
|
-
"Property #{Rainbow(key).yellow} of
|
267
|
-
"is supposed to be
|
275
|
+
"Property #{Rainbow(key).yellow} of #{Rainbow(parent).yellow} cop " \
|
276
|
+
"is supposed to be #{supposed_values} and #{Rainbow(value).yellow} is not."
|
268
277
|
end
|
269
278
|
end
|
270
279
|
end
|
@@ -32,7 +32,12 @@ module RuboCop
|
|
32
32
|
# allow turning off autocorrect on a cop by cop basis
|
33
33
|
return true unless cop_config
|
34
34
|
|
35
|
-
|
35
|
+
# `false` is the same as `disabled` for backward compatibility.
|
36
|
+
return false if ['disabled', false].include?(cop_config['AutoCorrect'])
|
37
|
+
|
38
|
+
# When LSP is enabled, it is considered as editing source code,
|
39
|
+
# and autocorrection with `AutoCorrect: contextual` will not be performed.
|
40
|
+
return false if contextual_autocorrect? && LSP.enabled?
|
36
41
|
|
37
42
|
# :safe_autocorrect is a derived option based on several command-line
|
38
43
|
# arguments - see RuboCop::Options#add_autocorrection_options
|
data/lib/rubocop/cop/base.rb
CHANGED
@@ -232,6 +232,10 @@ module RuboCop
|
|
232
232
|
@config.target_ruby_version
|
233
233
|
end
|
234
234
|
|
235
|
+
def parser_engine
|
236
|
+
@config.parser_engine
|
237
|
+
end
|
238
|
+
|
235
239
|
def target_rails_version
|
236
240
|
@config.target_rails_version
|
237
241
|
end
|
@@ -254,7 +258,7 @@ module RuboCop
|
|
254
258
|
|
255
259
|
# There should be very limited reasons for a Cop to do it's own parsing
|
256
260
|
def parse(source, path = nil)
|
257
|
-
ProcessedSource.new(source, target_ruby_version, path)
|
261
|
+
ProcessedSource.new(source, target_ruby_version, path, parser_engine: parser_engine)
|
258
262
|
end
|
259
263
|
|
260
264
|
# @api private
|
@@ -305,6 +309,17 @@ module RuboCop
|
|
305
309
|
@current_original = original
|
306
310
|
end
|
307
311
|
|
312
|
+
# @api private
|
313
|
+
def always_autocorrect?
|
314
|
+
# `true` is the same as `'always'` for backward compatibility.
|
315
|
+
['always', true].include?(cop_config.fetch('AutoCorrect', 'always'))
|
316
|
+
end
|
317
|
+
|
318
|
+
# @api private
|
319
|
+
def contextual_autocorrect?
|
320
|
+
cop_config.fetch('AutoCorrect', 'always') == 'contextual'
|
321
|
+
end
|
322
|
+
|
308
323
|
def inspect # :nodoc:
|
309
324
|
"#<#{self.class.name}:#{object_id} @config=#{@config} @options=#{@options}>"
|
310
325
|
end
|
@@ -389,7 +404,7 @@ module RuboCop
|
|
389
404
|
def use_corrector(range, corrector)
|
390
405
|
if autocorrect?
|
391
406
|
attempt_correction(range, corrector)
|
392
|
-
elsif corrector &&
|
407
|
+
elsif corrector && (always_autocorrect? || (contextual_autocorrect? && !LSP.enabled?))
|
393
408
|
:uncorrected
|
394
409
|
else
|
395
410
|
:unsupported
|
@@ -481,12 +496,6 @@ module RuboCop
|
|
481
496
|
range.end_pos + @current_offset
|
482
497
|
)
|
483
498
|
end
|
484
|
-
|
485
|
-
# This experimental feature has been under consideration for a while.
|
486
|
-
# @api private
|
487
|
-
def lsp_mode?
|
488
|
-
ARGV.include?('--lsp')
|
489
|
-
end
|
490
499
|
end
|
491
500
|
end
|
492
501
|
end
|
@@ -34,18 +34,14 @@ module RuboCop
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def offending_range
|
37
|
+
begin_range = block_node.source_range.begin
|
38
|
+
|
37
39
|
if block_node.arguments?
|
38
|
-
|
40
|
+
begin_range.join(argument_node.source_range.end)
|
39
41
|
else
|
40
|
-
|
42
|
+
begin_range.join(block_node.loc.begin.end)
|
41
43
|
end
|
42
44
|
end
|
43
|
-
|
44
|
-
def replacement_range(end_pos)
|
45
|
-
Parser::Source::Range.new(block_node.source_range.source_buffer,
|
46
|
-
block_node.source_range.begin_pos,
|
47
|
-
end_pos)
|
48
|
-
end
|
49
45
|
end
|
50
46
|
end
|
51
47
|
end
|
@@ -15,6 +15,8 @@ module RuboCop
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def call(corrector)
|
18
|
+
offending_range = for_node.source_range.begin.join(end_range)
|
19
|
+
|
18
20
|
corrector.replace(offending_range, correction)
|
19
21
|
end
|
20
22
|
|
@@ -40,11 +42,11 @@ module RuboCop
|
|
40
42
|
collection_node.range_type? || collection_node.or_type? || collection_node.and_type?
|
41
43
|
end
|
42
44
|
|
43
|
-
def
|
45
|
+
def end_range
|
44
46
|
if for_node.do?
|
45
|
-
keyword_begin.
|
47
|
+
keyword_begin.end
|
46
48
|
else
|
47
|
-
collection_end.
|
49
|
+
collection_end.end
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
@@ -59,16 +61,6 @@ module RuboCop
|
|
59
61
|
collection_node.source_range
|
60
62
|
end
|
61
63
|
end
|
62
|
-
|
63
|
-
def offending_range
|
64
|
-
replacement_range(end_position)
|
65
|
-
end
|
66
|
-
|
67
|
-
def replacement_range(end_pos)
|
68
|
-
Parser::Source::Range.new(for_node.source_range.source_buffer,
|
69
|
-
for_node.source_range.begin_pos,
|
70
|
-
end_pos)
|
71
|
-
end
|
72
64
|
end
|
73
65
|
end
|
74
66
|
end
|
@@ -76,7 +76,9 @@ module RuboCop
|
|
76
76
|
PATTERN
|
77
77
|
|
78
78
|
def on_new_investigation
|
79
|
-
|
79
|
+
return if processed_source.ast && required_ruby_version?(processed_source.ast)
|
80
|
+
|
81
|
+
add_global_offense(MISSING_MSG)
|
80
82
|
end
|
81
83
|
|
82
84
|
def on_send(node)
|
@@ -110,6 +112,8 @@ module RuboCop
|
|
110
112
|
end
|
111
113
|
end
|
112
114
|
|
115
|
+
return unless required_ruby_version
|
116
|
+
|
113
117
|
required_ruby_version.str_content.scan(/\d/).first(2).join('.')
|
114
118
|
end
|
115
119
|
|
@@ -30,6 +30,7 @@ module RuboCop
|
|
30
30
|
extend AutoCorrector
|
31
31
|
|
32
32
|
MSG = 'Use `%<method_name>s` instead of `%<method_suffix>s`.'
|
33
|
+
RESTRICT_ON_SEND = %i[end_with?].freeze
|
33
34
|
SUGGEST_METHOD_FOR_SUFFIX = {
|
34
35
|
'=' => 'assignment_method?',
|
35
36
|
'!' => 'bang_method?',
|
@@ -51,14 +52,15 @@ module RuboCop
|
|
51
52
|
|
52
53
|
def on_send(node)
|
53
54
|
method_name_end_with?(node) do |method_name_node, end_with_arg|
|
55
|
+
next unless method_name_node.receiver
|
56
|
+
|
57
|
+
preferred_method = SUGGEST_METHOD_FOR_SUFFIX[end_with_arg.value]
|
54
58
|
range = range(method_name_node, node)
|
55
|
-
message = format(
|
56
|
-
MSG,
|
57
|
-
method_name: SUGGEST_METHOD_FOR_SUFFIX[end_with_arg.value],
|
58
|
-
method_suffix: range.source
|
59
|
-
)
|
59
|
+
message = format(MSG, method_name: preferred_method, method_suffix: range.source)
|
60
60
|
|
61
|
-
add_offense(range, message: message)
|
61
|
+
add_offense(range, message: message) do |corrector|
|
62
|
+
corrector.replace(range, preferred_method)
|
63
|
+
end
|
62
64
|
end
|
63
65
|
end
|
64
66
|
alias on_csend on_send
|
@@ -26,10 +26,18 @@ module RuboCop
|
|
26
26
|
MSG = 'Precede `%<method>s` with a `@!method` YARD directive.'
|
27
27
|
MSG_WRONG_NAME = '`@!method` YARD directive has invalid method name, ' \
|
28
28
|
'use `%<expected>s` instead of `%<actual>s`.'
|
29
|
+
MSG_MISSING_SCOPE_SELF = 'Follow the `@!method` YARD directive with ' \
|
30
|
+
'`@!scope class` if it is a class method.'
|
31
|
+
MSG_WRONG_SCOPE_SELF = 'Do not use the `@!scope class` YARD directive if it ' \
|
32
|
+
'is not a class method.'
|
29
33
|
MSG_TOO_MANY = 'Multiple `@!method` YARD directives found for this matcher.'
|
30
34
|
|
31
35
|
RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].to_set.freeze
|
32
|
-
|
36
|
+
REGEXP_METHOD = /
|
37
|
+
^\s*\#\s*
|
38
|
+
@!method\s+(?<receiver>self\.)?(?<method_name>[a-z0-9_]+[?!]?)(?:\((?<args>.*)\))?
|
39
|
+
/x.freeze
|
40
|
+
REGEXP_SCOPE = /^\s*\#\s*@!scope class/.freeze
|
33
41
|
|
34
42
|
# @!method pattern_matcher?(node)
|
35
43
|
def_node_matcher :pattern_matcher?, <<~PATTERN
|
@@ -40,14 +48,15 @@ module RuboCop
|
|
40
48
|
return if node.arguments.none?
|
41
49
|
return unless valid_method_name?(node)
|
42
50
|
|
43
|
-
actual_name = node.first_argument.value
|
51
|
+
actual_name = node.first_argument.value.to_s
|
52
|
+
|
53
|
+
# Ignore cases where the method has a receiver that isn't self
|
54
|
+
return if actual_name.include?('.') && !actual_name.start_with?('self.')
|
55
|
+
|
44
56
|
directives = method_directives(node)
|
45
57
|
return too_many_directives(node) if directives.size > 1
|
46
58
|
|
47
|
-
|
48
|
-
return if directive_correct?(directive, actual_name)
|
49
|
-
|
50
|
-
register_offense(node, directive, actual_name)
|
59
|
+
process_directive(node, actual_name, directives.first)
|
51
60
|
end
|
52
61
|
|
53
62
|
private
|
@@ -58,44 +67,112 @@ module RuboCop
|
|
58
67
|
|
59
68
|
def method_directives(node)
|
60
69
|
comments = processed_source.ast_with_comments[node]
|
61
|
-
|
62
|
-
|
63
|
-
match = comment.text.match(REGEXP)
|
70
|
+
group_comments(comments).filter_map do |comment_method, comment_scope|
|
71
|
+
match = comment_method.text.match(REGEXP_METHOD)
|
64
72
|
next unless match
|
65
73
|
|
66
|
-
{
|
74
|
+
{
|
75
|
+
node_method: comment_method,
|
76
|
+
node_scope: comment_scope,
|
77
|
+
method_name: match[:method_name],
|
78
|
+
args: match[:args],
|
79
|
+
receiver: match[:receiver],
|
80
|
+
has_scope_directive: comment_scope&.text&.match?(REGEXP_SCOPE)
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def group_comments(comments)
|
86
|
+
result = []
|
87
|
+
comments.each.with_index do |comment, index|
|
88
|
+
# Grab the scope directive if it is preceded by a method directive
|
89
|
+
if comment.text.include?('@!method')
|
90
|
+
result << if (next_comment = comments[index + 1])&.text&.include?('@!scope')
|
91
|
+
[comment, next_comment]
|
92
|
+
else
|
93
|
+
[comment, nil]
|
94
|
+
end
|
95
|
+
end
|
67
96
|
end
|
97
|
+
result
|
68
98
|
end
|
69
99
|
|
70
100
|
def too_many_directives(node)
|
71
101
|
add_offense(node, message: MSG_TOO_MANY)
|
72
102
|
end
|
73
103
|
|
74
|
-
def
|
75
|
-
|
104
|
+
def process_directive(node, actual_name, directive)
|
105
|
+
return unless (offense_type = directive_offense_type(directive, actual_name))
|
106
|
+
|
107
|
+
register_offense(offense_type, node, directive, actual_name)
|
108
|
+
end
|
109
|
+
|
110
|
+
def directive_offense_type(directive, actual_name)
|
111
|
+
return :missing_directive unless directive
|
112
|
+
|
113
|
+
return :wrong_scope if wrong_scope(directive, actual_name)
|
114
|
+
return :no_scope if no_scope(directive, actual_name)
|
115
|
+
|
116
|
+
# The method directive being prefixed by 'self.' is always an offense.
|
117
|
+
# The matched method_name does not contain the receiver but the
|
118
|
+
# def_node_match method name may so it must be removed.
|
119
|
+
if directive[:method_name] != remove_receiver(actual_name) || directive[:receiver]
|
120
|
+
:wrong_name
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def wrong_scope(directive, actual_name)
|
125
|
+
!actual_name.start_with?('self.') && directive[:has_scope_directive]
|
126
|
+
end
|
127
|
+
|
128
|
+
def no_scope(directive, actual_name)
|
129
|
+
actual_name.start_with?('self.') && !directive[:has_scope_directive]
|
76
130
|
end
|
77
131
|
|
78
|
-
def register_offense(node, directive, actual_name)
|
79
|
-
message = formatted_message(directive, actual_name, node.method_name)
|
132
|
+
def register_offense(offense_type, node, directive, actual_name)
|
133
|
+
message = formatted_message(offense_type, directive, actual_name, node.method_name)
|
80
134
|
|
81
135
|
add_offense(node, message: message) do |corrector|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
136
|
+
case offense_type
|
137
|
+
when :wrong_name
|
138
|
+
correct_method_directive(corrector, directive, actual_name)
|
139
|
+
when :wrong_scope
|
140
|
+
remove_scope_directive(corrector, directive)
|
141
|
+
when :no_scope
|
142
|
+
insert_scope_directive(corrector, directive[:node_method])
|
143
|
+
when :missing_directive
|
144
|
+
insert_method_directive(corrector, node, actual_name)
|
86
145
|
end
|
87
146
|
end
|
88
147
|
end
|
89
148
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
149
|
+
# rubocop:disable Metrics/MethodLength
|
150
|
+
def formatted_message(offense_type, directive, actual_name, method_name)
|
151
|
+
case offense_type
|
152
|
+
when :wrong_name
|
153
|
+
# Add the receiver to the name when showing an offense
|
154
|
+
current_name = if directive[:receiver]
|
155
|
+
directive[:receiver] + directive[:method_name]
|
156
|
+
else
|
157
|
+
directive[:method_name]
|
158
|
+
end
|
159
|
+
# The correct name will never include a receiver, remove it
|
160
|
+
format(MSG_WRONG_NAME, expected: remove_receiver(actual_name), actual: current_name)
|
161
|
+
when :wrong_scope
|
162
|
+
MSG_WRONG_SCOPE_SELF
|
163
|
+
when :no_scope
|
164
|
+
MSG_MISSING_SCOPE_SELF
|
165
|
+
when :missing_directive
|
94
166
|
format(MSG, method: method_name)
|
95
167
|
end
|
96
168
|
end
|
169
|
+
# rubocop:enable Metrics/MethodLength
|
170
|
+
|
171
|
+
def remove_receiver(current)
|
172
|
+
current.delete_prefix('self.')
|
173
|
+
end
|
97
174
|
|
98
|
-
def
|
175
|
+
def insert_method_directive(corrector, node, actual_name)
|
99
176
|
# If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
|
100
177
|
arguments = pattern_arguments(node.arguments[1].source)
|
101
178
|
|
@@ -107,6 +184,14 @@ module RuboCop
|
|
107
184
|
corrector.insert_before(range, directive)
|
108
185
|
end
|
109
186
|
|
187
|
+
def insert_scope_directive(corrector, node)
|
188
|
+
range = range_with_surrounding_space(node.source_range, side: :left, newlines: false)
|
189
|
+
indentation = range.source.match(/^\s*/)[0]
|
190
|
+
directive = "\n#{indentation}# @!scope class"
|
191
|
+
|
192
|
+
corrector.insert_after(node, directive)
|
193
|
+
end
|
194
|
+
|
110
195
|
def pattern_arguments(pattern)
|
111
196
|
arguments = %w[node]
|
112
197
|
max_pattern_var = pattern.scan(/(?<=%)\d+/).map(&:to_i).max
|
@@ -134,12 +219,21 @@ module RuboCop
|
|
134
219
|
end
|
135
220
|
end
|
136
221
|
|
137
|
-
def
|
138
|
-
correct = "@!method #{actual_name}"
|
139
|
-
|
222
|
+
def correct_method_directive(corrector, directive, actual_name)
|
223
|
+
correct = "@!method #{remove_receiver(actual_name)}"
|
224
|
+
current_name = (directive[:receiver] || '') + directive[:method_name]
|
225
|
+
regexp = /@!method\s+#{Regexp.escape(current_name)}/
|
226
|
+
|
227
|
+
replacement = directive[:node_method].text.gsub(regexp, correct)
|
228
|
+
corrector.replace(directive[:node_method], replacement)
|
229
|
+
end
|
140
230
|
|
141
|
-
|
142
|
-
|
231
|
+
def remove_scope_directive(corrector, directive)
|
232
|
+
range = range_by_whole_lines(
|
233
|
+
directive[:node_scope].source_range,
|
234
|
+
include_final_newline: true
|
235
|
+
)
|
236
|
+
corrector.remove(range)
|
143
237
|
end
|
144
238
|
end
|
145
239
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# expect_no_offenses('code', keyword: keyword)
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# expect_no_offenses('code')
|
15
|
+
#
|
16
|
+
class RedundantExpectOffenseArguments < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
19
|
+
MSG = 'Remove the redundant arguments.'
|
20
|
+
RESTRICT_ON_SEND = %i[expect_no_offenses].freeze
|
21
|
+
|
22
|
+
def on_send(node)
|
23
|
+
return if node.arguments.one? || !node.arguments[1]&.hash_type?
|
24
|
+
|
25
|
+
range = node.first_argument.source_range.end.join(node.last_argument.source_range.end)
|
26
|
+
|
27
|
+
add_offense(range) do |corrector|
|
28
|
+
corrector.remove(range)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -20,6 +20,7 @@ require_relative 'internal_affairs/offense_location_keyword'
|
|
20
20
|
require_relative 'internal_affairs/processed_source_buffer_name'
|
21
21
|
require_relative 'internal_affairs/redundant_context_config_parameter'
|
22
22
|
require_relative 'internal_affairs/redundant_described_class_as_subject'
|
23
|
+
require_relative 'internal_affairs/redundant_expect_offense_arguments'
|
23
24
|
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
|
24
25
|
require_relative 'internal_affairs/redundant_location_argument'
|
25
26
|
require_relative 'internal_affairs/redundant_message_argument'
|
@@ -27,9 +27,9 @@ module RuboCop
|
|
27
27
|
MSG = 'Add an empty line after magic comments.'
|
28
28
|
|
29
29
|
def on_new_investigation
|
30
|
-
return unless processed_source
|
31
|
-
|
32
|
-
return if
|
30
|
+
return unless (last_magic_comment = last_magic_comment(processed_source))
|
31
|
+
return unless (next_line = processed_source[last_magic_comment.loc.line])
|
32
|
+
return if next_line.strip.empty?
|
33
33
|
|
34
34
|
offending_range = offending_range(last_magic_comment)
|
35
35
|
|
@@ -46,18 +46,25 @@ module RuboCop
|
|
46
46
|
|
47
47
|
# Find the last magic comment in the source file.
|
48
48
|
#
|
49
|
-
# Take all comments that precede the first line of code
|
49
|
+
# Take all comments that precede the first line of code (or just take
|
50
|
+
# them all in the case when there is no code), select the
|
50
51
|
# magic comments, and return the last magic comment in the file.
|
51
52
|
#
|
52
53
|
# @return [Parser::Source::Comment] if magic comments exist before code
|
53
54
|
# @return [nil] otherwise
|
54
55
|
def last_magic_comment(source)
|
55
|
-
source
|
56
|
-
.comments
|
57
|
-
.take_while { |comment| comment.loc.line < source.ast.loc.line }
|
56
|
+
comments_before_code(source)
|
58
57
|
.reverse
|
59
58
|
.find { |comment| MagicComment.parse(comment.text).any? }
|
60
59
|
end
|
60
|
+
|
61
|
+
def comments_before_code(source)
|
62
|
+
if source.ast
|
63
|
+
source.comments.take_while { |comment| comment.loc.line < source.ast.loc.line }
|
64
|
+
else
|
65
|
+
source.comments
|
66
|
+
end
|
67
|
+
end
|
61
68
|
end
|
62
69
|
end
|
63
70
|
end
|
@@ -20,7 +20,9 @@ module RuboCop
|
|
20
20
|
# This `Layout/EndAlignment` cop aligns with keywords (e.g. `if`, `while`, `case`)
|
21
21
|
# by default. On the other hand, `Layout/BeginEndAlignment` cop aligns with
|
22
22
|
# `EnforcedStyleAlignWith: start_of_line` by default due to `||= begin` tends
|
23
|
-
# to align with the start of the line.
|
23
|
+
# to align with the start of the line. `Layout/DefEndAlignment` cop also aligns with
|
24
|
+
# `EnforcedStyleAlignWith: start_of_line` by default.
|
25
|
+
# These style can be configured by each cop.
|
24
26
|
#
|
25
27
|
# @example EnforcedStyleAlignWith: keyword (default)
|
26
28
|
# # bad
|
@@ -84,12 +84,20 @@ module RuboCop
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def offense?(node)
|
87
|
-
node.multiline?
|
88
|
-
|
87
|
+
return false if !node.multiline? || too_long?(node) || !suitable_as_single_line?(node)
|
88
|
+
return require_backslash?(node) if node.and_type? || node.or_type?
|
89
|
+
|
90
|
+
!index_access_call_chained?(node) && !configured_to_not_be_inspected?(node)
|
91
|
+
end
|
92
|
+
|
93
|
+
def require_backslash?(node)
|
94
|
+
processed_source.lines[node.loc.operator.line - 1].end_with?('\\')
|
89
95
|
end
|
90
96
|
|
91
97
|
def index_access_call_chained?(node)
|
92
|
-
|
98
|
+
return false unless node.send_type? && node.method?(:[])
|
99
|
+
|
100
|
+
node.children.first.send_type? && node.children.first.method?(:[])
|
93
101
|
end
|
94
102
|
|
95
103
|
def configured_to_not_be_inspected?(node)
|
@@ -81,19 +81,28 @@ module RuboCop
|
|
81
81
|
private
|
82
82
|
|
83
83
|
def check_empty(left_brace, space_plus_brace, used_style)
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
if style_for_empty_braces == used_style
|
85
|
+
handle_different_styles_for_empty_braces(used_style)
|
86
|
+
return
|
87
|
+
elsif !config_to_allow_offenses.key?('Enabled')
|
88
|
+
config_to_allow_offenses['EnforcedStyleForEmptyBraces'] = used_style.to_s
|
89
|
+
end
|
87
90
|
|
88
91
|
if style_for_empty_braces == :space
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
+
range = left_brace
|
93
|
+
msg = MISSING_MSG
|
92
94
|
else
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
95
|
+
range = range_between(space_plus_brace.begin_pos, left_brace.begin_pos)
|
96
|
+
msg = DETECTED_MSG
|
97
|
+
end
|
98
|
+
add_offense(range, message: msg) { |corrector| autocorrect(corrector, range) }
|
99
|
+
end
|
100
|
+
|
101
|
+
def handle_different_styles_for_empty_braces(used_style)
|
102
|
+
if config_to_allow_offenses['EnforcedStyleForEmptyBraces'] &&
|
103
|
+
config_to_allow_offenses['EnforcedStyleForEmptyBraces'].to_sym != used_style
|
104
|
+
config_to_allow_offenses.clear
|
105
|
+
config_to_allow_offenses['Enabled'] = false
|
97
106
|
end
|
98
107
|
end
|
99
108
|
|
@@ -116,7 +116,7 @@ module RuboCop
|
|
116
116
|
|
117
117
|
def incorrect_style_detected(token1, token2,
|
118
118
|
expect_space, is_empty_braces)
|
119
|
-
brace = (token1.
|
119
|
+
brace = (token1.left_brace? ? token1 : token2).pos
|
120
120
|
range = expect_space ? brace : space_range(brace)
|
121
121
|
detected_style = expect_space ? 'no_space' : 'space'
|
122
122
|
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
def correct_other_branches(corrector, node)
|
105
105
|
return unless require_other_branches_correction?(node)
|
106
106
|
|
107
|
-
if node.else_branch&.if_type?
|
107
|
+
if node.else_branch&.if_type? && !node.else_branch.modifier_form?
|
108
108
|
# Replace an orphaned `elsif` with `if`
|
109
109
|
corrector.replace(node.else_branch.loc.keyword, 'if')
|
110
110
|
else
|