rubocop 1.22.3 → 1.26.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -3
- data/config/default.yml +110 -19
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_docs_url.rb +48 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
- data/lib/rubocop/cli.rb +2 -1
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/badge.rb +7 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/if_then_corrector.rb +55 -0
- data/lib/rubocop/cop/documentation.rb +19 -2
- data/lib/rubocop/cop/gemspec/date_assignment.rb +2 -10
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -10
- data/lib/rubocop/cop/gemspec/require_mfa.rb +145 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -3
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -10
- data/lib/rubocop/cop/generator.rb +7 -11
- data/lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +47 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +3 -1
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +36 -9
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +31 -2
- data/lib/rubocop/cop/layout/dot_position.rb +4 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +22 -1
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +5 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +7 -8
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +4 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +11 -5
- data/lib/rubocop/cop/lint/ambiguous_range.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -1
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +16 -4
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +6 -0
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +10 -5
- data/lib/rubocop/cop/lint/inherit_exception.rb +19 -28
- data/lib/rubocop/cop/lint/number_conversion.rb +5 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -2
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +117 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -9
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +0 -9
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/module_length.rb +1 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +5 -0
- data/lib/rubocop/cop/mixin/gemspec_help.rb +30 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +4 -3
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +82 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +17 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -5
- data/lib/rubocop/cop/naming/block_forwarding.rb +121 -0
- data/lib/rubocop/cop/naming/file_name.rb +37 -4
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/security/open.rb +11 -1
- data/lib/rubocop/cop/security/yaml_load.rb +9 -3
- data/lib/rubocop/cop/style/character_literal.rb +8 -1
- data/lib/rubocop/cop/style/collection_compact.rb +31 -13
- data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
- data/lib/rubocop/cop/style/def_with_parentheses.rb +16 -11
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +10 -0
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/file_read.rb +112 -0
- data/lib/rubocop/cop/style/file_write.rb +124 -0
- data/lib/rubocop/cop/style/for.rb +4 -0
- data/lib/rubocop/cop/style/format_string_token.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +2 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +36 -0
- data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -6
- data/lib/rubocop/cop/style/hash_transform_values.rb +6 -6
- data/lib/rubocop/cop/style/if_inside_else.rb +15 -0
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/lambda_call.rb +12 -20
- data/lib/rubocop/cop/style/map_to_hash.rb +68 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +3 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -13
- data/lib/rubocop/cop/style/nested_file_dirname.rb +66 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +10 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +18 -39
- data/lib/rubocop/cop/style/open_struct_use.rb +69 -0
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +3 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -2
- data/lib/rubocop/cop/style/quoted_symbols.rb +11 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -6
- data/lib/rubocop/cop/style/redundant_interpolation.rb +17 -3
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +5 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -5
- data/lib/rubocop/cop/style/sample.rb +5 -3
- data/lib/rubocop/cop/style/select_by_regexp.rb +6 -1
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +53 -13
- data/lib/rubocop/cop/style/string_concatenation.rb +7 -1
- data/lib/rubocop/cop/style/swap_values.rb +2 -0
- data/lib/rubocop/cop/style/ternary_parentheses.rb +16 -2
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +4 -0
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +9 -1
- data/lib/rubocop/cop/variable_force.rb +1 -5
- data/lib/rubocop/cops_documentation_generator.rb +2 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -2
- data/lib/rubocop/formatter/html_formatter.rb +5 -2
- data/lib/rubocop/formatter/json_formatter.rb +4 -1
- data/lib/rubocop/options.rb +14 -3
- data/lib/rubocop/remote_config.rb +2 -4
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop/yaml_duplication_checker.rb +1 -1
- data/lib/rubocop.rb +12 -0
- metadata +22 -7
@@ -15,6 +15,11 @@ module RuboCop
|
|
15
15
|
# end
|
16
16
|
# end
|
17
17
|
#
|
18
|
+
# # bad
|
19
|
+
# if condition_b
|
20
|
+
# do_something
|
21
|
+
# end if condition_a
|
22
|
+
#
|
18
23
|
# # good
|
19
24
|
# if condition_a && condition_b
|
20
25
|
# do_something
|
@@ -26,12 +31,21 @@ module RuboCop
|
|
26
31
|
# do_something if condition_b
|
27
32
|
# end
|
28
33
|
#
|
34
|
+
# # bad
|
35
|
+
# if condition_b
|
36
|
+
# do_something
|
37
|
+
# end if condition_a
|
38
|
+
#
|
29
39
|
# @example AllowModifier: true
|
30
40
|
# # good
|
31
41
|
# if condition_a
|
32
42
|
# do_something if condition_b
|
33
43
|
# end
|
34
44
|
#
|
45
|
+
# # good
|
46
|
+
# if condition_b
|
47
|
+
# do_something
|
48
|
+
# end if condition_a
|
35
49
|
class SoleNestedConditional < Base
|
36
50
|
include RangeHelp
|
37
51
|
extend AutoCorrector
|
@@ -47,7 +61,7 @@ module RuboCop
|
|
47
61
|
|
48
62
|
if_branch = node.if_branch
|
49
63
|
return if use_variable_assignment_in_condition?(node.condition, if_branch)
|
50
|
-
return unless offending_branch?(if_branch)
|
64
|
+
return unless offending_branch?(node, if_branch)
|
51
65
|
|
52
66
|
message = format(MSG, conditional_type: node.keyword)
|
53
67
|
add_offense(if_branch.loc.keyword, message: message) do |corrector|
|
@@ -72,18 +86,28 @@ module RuboCop
|
|
72
86
|
end
|
73
87
|
end
|
74
88
|
|
75
|
-
def offending_branch?(branch)
|
89
|
+
def offending_branch?(node, branch)
|
76
90
|
return false unless branch
|
77
91
|
|
78
92
|
branch.if_type? &&
|
79
93
|
!branch.else? &&
|
80
94
|
!branch.ternary? &&
|
81
|
-
!(branch.modifier_form? && allow_modifier?)
|
95
|
+
!((node.modifier_form? || branch.modifier_form?) && allow_modifier?)
|
82
96
|
end
|
83
97
|
|
84
98
|
def autocorrect(corrector, node, if_branch)
|
85
|
-
|
99
|
+
if node.condition.or_type? || node.condition.assignment?
|
100
|
+
corrector.wrap(node.condition, '(', ')')
|
101
|
+
end
|
86
102
|
|
103
|
+
if outer_condition_modify_form?(node, if_branch)
|
104
|
+
autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
105
|
+
else
|
106
|
+
autocorrect_outer_condition_basic(corrector, node, if_branch)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def autocorrect_outer_condition_basic(corrector, node, if_branch)
|
87
111
|
correct_from_unless_to_if(corrector, node) if node.unless?
|
88
112
|
|
89
113
|
and_operator = if_branch.unless? ? ' && !' : ' && '
|
@@ -95,11 +119,17 @@ module RuboCop
|
|
95
119
|
end
|
96
120
|
end
|
97
121
|
|
98
|
-
def
|
122
|
+
def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
|
123
|
+
correct_from_unless_to_if(corrector, if_branch, is_modify_form: true) if if_branch.unless?
|
124
|
+
correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
|
125
|
+
end
|
126
|
+
|
127
|
+
def correct_from_unless_to_if(corrector, node, is_modify_form: false)
|
99
128
|
corrector.replace(node.loc.keyword, 'if')
|
100
129
|
|
101
130
|
condition = node.condition
|
102
|
-
if condition.send_type? && condition.comparison_method? && !condition.parenthesized?
|
131
|
+
if (condition.send_type? && condition.comparison_method? && !condition.parenthesized?) ||
|
132
|
+
(is_modify_form && wrap_condition?(condition))
|
103
133
|
corrector.wrap(node.condition, '!(', ')')
|
104
134
|
else
|
105
135
|
corrector.insert_before(node.condition, '!')
|
@@ -111,7 +141,7 @@ module RuboCop
|
|
111
141
|
correct_outer_condition(corrector, outer_condition)
|
112
142
|
|
113
143
|
condition = if_branch.condition
|
114
|
-
corrector.insert_after(outer_condition,
|
144
|
+
corrector.insert_after(outer_condition, "#{and_operator}#{replace_condition(condition)}")
|
115
145
|
|
116
146
|
range = range_between(if_branch.loc.keyword.begin_pos, condition.source_range.end_pos)
|
117
147
|
corrector.remove(range_with_surrounding_space(range: range, newlines: false))
|
@@ -127,6 +157,16 @@ module RuboCop
|
|
127
157
|
corrector.wrap(if_branch.condition, '(', ')') if wrap_condition?(if_branch.condition)
|
128
158
|
end
|
129
159
|
|
160
|
+
def correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
|
161
|
+
condition = if_branch.condition
|
162
|
+
corrector.insert_before(condition,
|
163
|
+
"#{'!' if node.unless?}#{replace_condition(node.condition)} && ")
|
164
|
+
|
165
|
+
corrector.remove(node.condition.loc.expression)
|
166
|
+
corrector.remove(range_with_surrounding_space(range: node.loc.keyword, newlines: false))
|
167
|
+
corrector.replace(if_branch.loc.keyword, 'if')
|
168
|
+
end
|
169
|
+
|
130
170
|
def correct_for_comment(corrector, node, if_branch)
|
131
171
|
return if config.for_cop('Style/IfUnlessModifier')['Enabled']
|
132
172
|
|
@@ -163,17 +203,17 @@ module RuboCop
|
|
163
203
|
(node.send_type? && node.arguments.any? && !node.parenthesized?)
|
164
204
|
end
|
165
205
|
|
166
|
-
def
|
167
|
-
|
168
|
-
"#{and_operator}(#{condition.source})"
|
169
|
-
else
|
170
|
-
"#{and_operator}#{condition.source}"
|
171
|
-
end
|
206
|
+
def replace_condition(condition)
|
207
|
+
wrap_condition?(condition) ? "(#{condition.source})" : condition.source
|
172
208
|
end
|
173
209
|
|
174
210
|
def allow_modifier?
|
175
211
|
cop_config['AllowModifier']
|
176
212
|
end
|
213
|
+
|
214
|
+
def outer_condition_modify_form?(node, if_branch)
|
215
|
+
node.condition.loc.expression.begin_pos > if_branch.condition.loc.expression.begin_pos
|
216
|
+
end
|
177
217
|
end
|
178
218
|
end
|
179
219
|
end
|
@@ -134,7 +134,13 @@ module RuboCop
|
|
134
134
|
end
|
135
135
|
|
136
136
|
def uncorrectable?(part)
|
137
|
-
part.multiline? || (part
|
137
|
+
part.multiline? || heredoc?(part) || part.each_descendant(:block).any?
|
138
|
+
end
|
139
|
+
|
140
|
+
def heredoc?(node)
|
141
|
+
return false unless node.str_type? || node.dstr_type?
|
142
|
+
|
143
|
+
node.heredoc?
|
138
144
|
end
|
139
145
|
|
140
146
|
def corrected_ancestor?(node)
|
@@ -67,7 +67,10 @@ module RuboCop
|
|
67
67
|
MSG_COMPLEX = '%<command>s parentheses for ternary expressions with complex conditions.'
|
68
68
|
|
69
69
|
def on_if(node)
|
70
|
-
|
70
|
+
condition = node.condition
|
71
|
+
|
72
|
+
return if only_closing_parenthesis_is_last_line?(condition)
|
73
|
+
return if condition_as_parenthesized_one_line_pattern_matching?(condition)
|
71
74
|
return unless node.ternary? && !infinite_loop? && offense?(node)
|
72
75
|
|
73
76
|
message = message(node)
|
@@ -77,11 +80,22 @@ module RuboCop
|
|
77
80
|
end
|
78
81
|
end
|
79
82
|
|
83
|
+
private
|
84
|
+
|
80
85
|
def only_closing_parenthesis_is_last_line?(condition)
|
81
86
|
condition.source.split("\n").last == ')'
|
82
87
|
end
|
83
88
|
|
84
|
-
|
89
|
+
def condition_as_parenthesized_one_line_pattern_matching?(condition)
|
90
|
+
return false unless condition.parenthesized_call?
|
91
|
+
return false unless (first_child = condition.children.first)
|
92
|
+
|
93
|
+
if target_ruby_version >= 3.0
|
94
|
+
first_child.match_pattern_p_type?
|
95
|
+
else
|
96
|
+
first_child.match_pattern_type? # For Ruby 2.7's one line pattern matching AST.
|
97
|
+
end
|
98
|
+
end
|
85
99
|
|
86
100
|
def autocorrect(corrector, node)
|
87
101
|
condition = node.condition
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# last item of all non-empty, multiline array literals.
|
11
11
|
# * `comma`: Requires a comma after last item in an array,
|
12
12
|
# but only when each item is on its own line.
|
13
|
-
# * `no_comma`: Does not
|
13
|
+
# * `no_comma`: Does not require a comma after the
|
14
14
|
# last item in an array
|
15
15
|
#
|
16
16
|
# @example EnforcedStyleForMultiline: consistent_comma
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# last item of all non-empty, multiline hash literals.
|
11
11
|
# * `comma`: Requires a comma after the last item in a hash,
|
12
12
|
# but only when each item is on its own line.
|
13
|
-
# * `no_comma`: Does not
|
13
|
+
# * `no_comma`: Does not require a comma after the
|
14
14
|
# last item in a hash
|
15
15
|
#
|
16
16
|
# @example EnforcedStyleForMultiline: consistent_comma
|
@@ -32,10 +32,14 @@ module RuboCop
|
|
32
32
|
body_range = range_between_condition_and_else(node, node.condition)
|
33
33
|
else_range = range_between_else_and_end(node)
|
34
34
|
|
35
|
+
next if part_of_ignored_node?(node)
|
36
|
+
|
35
37
|
corrector.replace(node.loc.keyword, 'if')
|
36
38
|
corrector.replace(body_range, else_range.source)
|
37
39
|
corrector.replace(else_range, body_range.source)
|
38
40
|
end
|
41
|
+
|
42
|
+
ignore_node(node)
|
39
43
|
end
|
40
44
|
|
41
45
|
def range_between_condition_and_else(node, condition)
|
data/lib/rubocop/cop/team.rb
CHANGED
data/lib/rubocop/cop/util.rb
CHANGED
@@ -30,8 +30,15 @@ module RuboCop
|
|
30
30
|
node.loc.respond_to?(:end) && node.loc.end && node.loc.end.is?(')')
|
31
31
|
end
|
32
32
|
|
33
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
33
34
|
def add_parentheses(node, corrector)
|
34
|
-
if
|
35
|
+
if node.args_type?
|
36
|
+
arguments_range = node.source_range
|
37
|
+
args_with_space = range_with_surrounding_space(range: arguments_range, side: :left)
|
38
|
+
leading_space = range_between(args_with_space.begin_pos, arguments_range.begin_pos)
|
39
|
+
corrector.replace(leading_space, '(')
|
40
|
+
corrector.insert_after(arguments_range, ')')
|
41
|
+
elsif !node.respond_to?(:arguments)
|
35
42
|
corrector.wrap(node, '(', ')')
|
36
43
|
elsif node.arguments.empty?
|
37
44
|
corrector.insert_after(node, '()')
|
@@ -43,6 +50,7 @@ module RuboCop
|
|
43
50
|
corrector.insert_after(args_end(node), ')')
|
44
51
|
end
|
45
52
|
end
|
53
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
46
54
|
|
47
55
|
def args_begin(node)
|
48
56
|
loc = node.loc
|
@@ -186,11 +186,7 @@ module RuboCop
|
|
186
186
|
end
|
187
187
|
|
188
188
|
def regexp_captured_names(node)
|
189
|
-
|
190
|
-
child.children.first
|
191
|
-
end.join || ''
|
192
|
-
|
193
|
-
regexp = Regexp.new(regexp_string)
|
189
|
+
regexp = node.to_regexp
|
194
190
|
|
195
191
|
regexp.named_captures.keys
|
196
192
|
end
|
@@ -191,8 +191,8 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
191
191
|
|
192
192
|
def wrap_backtick(value)
|
193
193
|
if value.is_a?(String)
|
194
|
-
# Use `+` to prevent text like `**/*.gemspec` from being bold.
|
195
|
-
value.
|
194
|
+
# Use `+` to prevent text like `**/*.gemspec`, `spec/**/*` from being bold.
|
195
|
+
value.include?('*') ? "`+#{value}+`" : "`#{value}`"
|
196
196
|
else
|
197
197
|
"`#{value}`"
|
198
198
|
end
|
@@ -121,9 +121,14 @@ module RuboCop
|
|
121
121
|
output_buffer.puts "# Offense count: #{offense_count}" if show_offense_counts?
|
122
122
|
|
123
123
|
cop_class = Cop::Registry.global.find_by_cop_name(cop_name)
|
124
|
-
output_buffer.puts '# Cop supports --auto-correct.' if cop_class&.support_autocorrect?
|
125
|
-
|
126
124
|
default_cfg = default_config(cop_name)
|
125
|
+
|
126
|
+
if supports_safe_auto_correct?(cop_class, default_cfg)
|
127
|
+
output_buffer.puts '# This cop supports safe auto-correction (--auto-correct).'
|
128
|
+
elsif supports_unsafe_autocorrect?(cop_class, default_cfg)
|
129
|
+
output_buffer.puts '# This cop supports unsafe auto-correction (--auto-correct-all).'
|
130
|
+
end
|
131
|
+
|
127
132
|
return unless default_cfg
|
128
133
|
|
129
134
|
params = cop_config_params(default_cfg, cfg)
|
@@ -132,6 +137,15 @@ module RuboCop
|
|
132
137
|
output_cop_param_comments(output_buffer, params, default_cfg)
|
133
138
|
end
|
134
139
|
|
140
|
+
def supports_safe_auto_correct?(cop_class, default_cfg)
|
141
|
+
cop_class&.support_autocorrect? &&
|
142
|
+
(default_cfg.nil? || default_cfg['Safe'] || default_cfg['Safe'].nil?)
|
143
|
+
end
|
144
|
+
|
145
|
+
def supports_unsafe_autocorrect?(cop_class, default_cfg)
|
146
|
+
cop_class&.support_autocorrect? && !default_cfg.nil? && default_cfg['Safe'] == false
|
147
|
+
end
|
148
|
+
|
135
149
|
def cop_config_params(default_cfg, cfg)
|
136
150
|
default_cfg.keys -
|
137
151
|
%w[Description StyleGuide Reference Enabled Exclude Safe
|
@@ -23,12 +23,15 @@ module RuboCop
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
Summary = Struct.new(:offense_count, :inspected_files, :target_files, keyword_init: true)
|
27
|
+
FileOffenses = Struct.new(:path, :offenses, keyword_init: true)
|
28
|
+
|
26
29
|
attr_reader :files, :summary
|
27
30
|
|
28
31
|
def initialize(output, options = {})
|
29
32
|
super
|
30
33
|
@files = []
|
31
|
-
@summary =
|
34
|
+
@summary = Summary.new(offense_count: 0)
|
32
35
|
end
|
33
36
|
|
34
37
|
def started(target_files)
|
@@ -36,7 +39,7 @@ module RuboCop
|
|
36
39
|
end
|
37
40
|
|
38
41
|
def file_finished(file, offenses)
|
39
|
-
files <<
|
42
|
+
files << FileOffenses.new(path: file, offenses: offenses)
|
40
43
|
summary.offense_count += offenses.count
|
41
44
|
end
|
42
45
|
|
@@ -59,12 +59,15 @@ module RuboCop
|
|
59
59
|
end
|
60
60
|
|
61
61
|
# TODO: Consider better solution for Offense#real_column.
|
62
|
+
# The minimum value of `start_column: real_column` is 1.
|
63
|
+
# So, the minimum value of `last_column` should be 1.
|
64
|
+
# And non-zero value of `last_column` should be used as is.
|
62
65
|
def hash_for_location(offense)
|
63
66
|
{
|
64
67
|
start_line: offense.line,
|
65
68
|
start_column: offense.real_column,
|
66
69
|
last_line: offense.last_line,
|
67
|
-
last_column: offense.last_column,
|
70
|
+
last_column: offense.last_column.zero? ? 1 : offense.last_column,
|
68
71
|
length: offense.location.length,
|
69
72
|
# `line` and `column` exist for compatibility.
|
70
73
|
# Use `start_line` and `start_column` instead.
|
data/lib/rubocop/options.rb
CHANGED
@@ -15,7 +15,7 @@ module RuboCop
|
|
15
15
|
'root of the project. RuboCop will use this path to determine which ' \
|
16
16
|
'cops are enabled (via eg. Include/Exclude), and so that certain cops ' \
|
17
17
|
'like Naming/FileName can be checked.'
|
18
|
-
EXITING_OPTIONS = %i[version verbose_version show_cops].freeze
|
18
|
+
EXITING_OPTIONS = %i[version verbose_version show_cops show_docs_url].freeze
|
19
19
|
DEFAULT_MAXIMUM_EXCLUSION_ITEMS = 15
|
20
20
|
|
21
21
|
def initialize
|
@@ -188,6 +188,9 @@ module RuboCop
|
|
188
188
|
option(opts, '--show-cops [COP1,COP2,...]') do |list|
|
189
189
|
@options[:show_cops] = list.nil? ? [] : list.split(',')
|
190
190
|
end
|
191
|
+
option(opts, '--show-docs-url [COP1,COP2,...]') do |list|
|
192
|
+
@options[:show_docs_url] = list.nil? ? [] : list.split(',')
|
193
|
+
end
|
191
194
|
end
|
192
195
|
end
|
193
196
|
|
@@ -464,8 +467,14 @@ module RuboCop
|
|
464
467
|
'This option applies to the previously',
|
465
468
|
'specified --format, or the default format',
|
466
469
|
'if no format is specified.'],
|
467
|
-
fail_level: ['Minimum severity
|
468
|
-
'
|
470
|
+
fail_level: ['Minimum severity for exit with error code.',
|
471
|
+
' [A] autocorrect',
|
472
|
+
' [I] info',
|
473
|
+
' [R] refactor',
|
474
|
+
' [C] convention',
|
475
|
+
' [W] warning',
|
476
|
+
' [E] error',
|
477
|
+
' [F] fatal'],
|
469
478
|
display_time: 'Display elapsed time in seconds.',
|
470
479
|
display_only_failed: ['Only output offense messages. Omit passing',
|
471
480
|
'cops. Only valid for --format junit.'],
|
@@ -475,6 +484,8 @@ module RuboCop
|
|
475
484
|
show_cops: ['Shows the given cops, or all cops by',
|
476
485
|
'default, and their configurations for the',
|
477
486
|
'current directory.'],
|
487
|
+
show_docs_url: ['Display url to documentation for the given',
|
488
|
+
'cops, or base url by default.'],
|
478
489
|
fail_fast: ['Inspect files in order of modification',
|
479
490
|
'time and stop after the first file',
|
480
491
|
'containing offenses.'],
|
@@ -23,9 +23,7 @@ module RuboCop
|
|
23
23
|
next if response.is_a?(Net::HTTPNotModified)
|
24
24
|
next if response.is_a?(SocketError)
|
25
25
|
|
26
|
-
File.
|
27
|
-
io.write response.body
|
28
|
-
end
|
26
|
+
File.write(cache_path, response.body)
|
29
27
|
end
|
30
28
|
|
31
29
|
cache_path
|
@@ -33,7 +31,7 @@ module RuboCop
|
|
33
31
|
|
34
32
|
def inherit_from_remote(file, path)
|
35
33
|
new_uri = @uri.dup
|
36
|
-
new_uri.path.gsub!(%r{/[^/]*$}, "/#{file}")
|
34
|
+
new_uri.path.gsub!(%r{/[^/]*$}, "/#{file.delete_prefix('./')}")
|
37
35
|
RemoteConfig.new(new_uri.to_s, File.dirname(path))
|
38
36
|
end
|
39
37
|
|
data/lib/rubocop/result_cache.rb
CHANGED
@@ -179,7 +179,7 @@ module RuboCop
|
|
179
179
|
.select { |path| File.file?(path) }
|
180
180
|
.sort!
|
181
181
|
.each do |path|
|
182
|
-
content = File.
|
182
|
+
content = File.binread(path)
|
183
183
|
digest << Zlib.crc32(content).to_s # mtime not reliable
|
184
184
|
end
|
185
185
|
digest << RuboCop::Version::STRING << RuboCop::AST::Version::STRING
|
data/lib/rubocop/runner.rb
CHANGED
@@ -8,7 +8,7 @@ module RuboCop
|
|
8
8
|
class Runner # rubocop:disable Metrics/ClassLength
|
9
9
|
# An exception indicating that the inspection loop got stuck correcting
|
10
10
|
# offenses back and forth.
|
11
|
-
class InfiniteCorrectionLoop <
|
11
|
+
class InfiniteCorrectionLoop < StandardError
|
12
12
|
attr_reader :offenses
|
13
13
|
|
14
14
|
def initialize(path, offenses_by_iteration, loop_start: -1)
|
data/lib/rubocop/target_ruby.rb
CHANGED
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
# The kind of Ruby that code inspected by RuboCop is written in.
|
5
5
|
# @api private
|
6
6
|
class TargetRuby
|
7
|
-
KNOWN_RUBIES = [2.5, 2.6, 2.7, 3.0, 3.1].freeze
|
7
|
+
KNOWN_RUBIES = [2.5, 2.6, 2.7, 3.0, 3.1, 3.2].freeze
|
8
8
|
DEFAULT_VERSION = KNOWN_RUBIES.first
|
9
9
|
|
10
10
|
OBSOLETE_RUBIES = {
|
data/lib/rubocop/version.rb
CHANGED
@@ -23,7 +23,7 @@ module RuboCop
|
|
23
23
|
when Psych::Nodes::Mapping
|
24
24
|
tree.children.each_slice(2).with_object([]) do |(key, value), keys|
|
25
25
|
exist = keys.find { |key2| key2.value == key.value }
|
26
|
-
|
26
|
+
yield(exist, key) if exist
|
27
27
|
keys << key
|
28
28
|
traverse(value, &on_duplicated)
|
29
29
|
end
|
data/lib/rubocop.rb
CHANGED
@@ -83,6 +83,7 @@ require_relative 'rubocop/cop/mixin/enforce_superclass'
|
|
83
83
|
require_relative 'rubocop/cop/mixin/first_element_line_break'
|
84
84
|
require_relative 'rubocop/cop/mixin/frozen_string_literal'
|
85
85
|
require_relative 'rubocop/cop/mixin/gem_declaration'
|
86
|
+
require_relative 'rubocop/cop/mixin/gemspec_help'
|
86
87
|
require_relative 'rubocop/cop/mixin/hash_alignment_styles'
|
87
88
|
require_relative 'rubocop/cop/mixin/hash_transform_method'
|
88
89
|
require_relative 'rubocop/cop/mixin/ignored_pattern'
|
@@ -93,6 +94,7 @@ require_relative 'rubocop/cop/mixin/line_length_help'
|
|
93
94
|
require_relative 'rubocop/cop/mixin/match_range'
|
94
95
|
require_relative 'rubocop/cop/metrics/utils/repeated_csend_discount'
|
95
96
|
require_relative 'rubocop/cop/metrics/utils/repeated_attribute_discount'
|
97
|
+
require_relative 'rubocop/cop/mixin/hash_shorthand_syntax'
|
96
98
|
require_relative 'rubocop/cop/mixin/method_complexity'
|
97
99
|
require_relative 'rubocop/cop/mixin/method_preference'
|
98
100
|
require_relative 'rubocop/cop/mixin/min_body_length'
|
@@ -138,6 +140,7 @@ require_relative 'rubocop/cop/correctors/condition_corrector'
|
|
138
140
|
require_relative 'rubocop/cop/correctors/each_to_for_corrector'
|
139
141
|
require_relative 'rubocop/cop/correctors/empty_line_corrector'
|
140
142
|
require_relative 'rubocop/cop/correctors/for_to_each_corrector'
|
143
|
+
require_relative 'rubocop/cop/correctors/if_then_corrector'
|
141
144
|
require_relative 'rubocop/cop/correctors/lambda_literal_to_method_corrector'
|
142
145
|
require_relative 'rubocop/cop/correctors/line_break_corrector'
|
143
146
|
require_relative 'rubocop/cop/correctors/multiline_literal_brace_corrector'
|
@@ -160,6 +163,7 @@ require_relative 'rubocop/cop/bundler/ordered_gems'
|
|
160
163
|
require_relative 'rubocop/cop/gemspec/date_assignment'
|
161
164
|
require_relative 'rubocop/cop/gemspec/duplicated_assignment'
|
162
165
|
require_relative 'rubocop/cop/gemspec/ordered_dependencies'
|
166
|
+
require_relative 'rubocop/cop/gemspec/require_mfa'
|
163
167
|
require_relative 'rubocop/cop/gemspec/required_ruby_version'
|
164
168
|
require_relative 'rubocop/cop/gemspec/ruby_version_globals_usage'
|
165
169
|
|
@@ -382,6 +386,7 @@ require_relative 'rubocop/cop/lint/useless_access_modifier'
|
|
382
386
|
require_relative 'rubocop/cop/lint/useless_assignment'
|
383
387
|
require_relative 'rubocop/cop/lint/useless_else_without_rescue'
|
384
388
|
require_relative 'rubocop/cop/lint/useless_method_definition'
|
389
|
+
require_relative 'rubocop/cop/lint/useless_ruby2_keywords'
|
385
390
|
require_relative 'rubocop/cop/lint/useless_setter_call'
|
386
391
|
require_relative 'rubocop/cop/lint/useless_times'
|
387
392
|
require_relative 'rubocop/cop/lint/void'
|
@@ -402,6 +407,7 @@ require_relative 'rubocop/cop/metrics/perceived_complexity'
|
|
402
407
|
|
403
408
|
require_relative 'rubocop/cop/naming/accessor_method_name'
|
404
409
|
require_relative 'rubocop/cop/naming/ascii_identifiers'
|
410
|
+
require_relative 'rubocop/cop/naming/block_forwarding'
|
405
411
|
require_relative 'rubocop/cop/naming/block_parameter_name'
|
406
412
|
require_relative 'rubocop/cop/naming/class_and_module_camel_case'
|
407
413
|
require_relative 'rubocop/cop/naming/constant_name'
|
@@ -478,6 +484,8 @@ require_relative 'rubocop/cop/style/even_odd'
|
|
478
484
|
require_relative 'rubocop/cop/style/expand_path_arguments'
|
479
485
|
require_relative 'rubocop/cop/style/explicit_block_argument'
|
480
486
|
require_relative 'rubocop/cop/style/exponential_notation'
|
487
|
+
require_relative 'rubocop/cop/style/file_read'
|
488
|
+
require_relative 'rubocop/cop/style/file_write'
|
481
489
|
require_relative 'rubocop/cop/style/float_division'
|
482
490
|
require_relative 'rubocop/cop/style/for'
|
483
491
|
require_relative 'rubocop/cop/style/format_string'
|
@@ -510,10 +518,12 @@ require_relative 'rubocop/cop/style/keyword_parameters_order'
|
|
510
518
|
require_relative 'rubocop/cop/style/lambda'
|
511
519
|
require_relative 'rubocop/cop/style/lambda_call'
|
512
520
|
require_relative 'rubocop/cop/style/line_end_concatenation'
|
521
|
+
require_relative 'rubocop/cop/style/map_to_hash'
|
513
522
|
require_relative 'rubocop/cop/style/method_call_without_args_parentheses'
|
514
523
|
require_relative 'rubocop/cop/style/method_call_with_args_parentheses'
|
515
524
|
require_relative 'rubocop/cop/style/multiline_in_pattern_then'
|
516
525
|
require_relative 'rubocop/cop/style/numbered_parameters'
|
526
|
+
require_relative 'rubocop/cop/style/open_struct_use'
|
517
527
|
require_relative 'rubocop/cop/style/redundant_assignment'
|
518
528
|
require_relative 'rubocop/cop/style/redundant_fetch_block'
|
519
529
|
require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
|
@@ -542,6 +552,7 @@ require_relative 'rubocop/cop/style/negated_if'
|
|
542
552
|
require_relative 'rubocop/cop/style/negated_if_else_condition'
|
543
553
|
require_relative 'rubocop/cop/style/negated_unless'
|
544
554
|
require_relative 'rubocop/cop/style/negated_while'
|
555
|
+
require_relative 'rubocop/cop/style/nested_file_dirname'
|
545
556
|
require_relative 'rubocop/cop/style/nested_modifier'
|
546
557
|
require_relative 'rubocop/cop/style/nested_parenthesized_calls'
|
547
558
|
require_relative 'rubocop/cop/style/nested_ternary_operator'
|
@@ -701,6 +712,7 @@ require_relative 'rubocop/cli/command/auto_genenerate_config'
|
|
701
712
|
require_relative 'rubocop/cli/command/execute_runner'
|
702
713
|
require_relative 'rubocop/cli/command/init_dotfile'
|
703
714
|
require_relative 'rubocop/cli/command/show_cops'
|
715
|
+
require_relative 'rubocop/cli/command/show_docs_url'
|
704
716
|
require_relative 'rubocop/cli/command/suggest_extensions'
|
705
717
|
require_relative 'rubocop/cli/command/version'
|
706
718
|
require_relative 'rubocop/config_regeneration'
|