rubocop 1.22.3 → 1.26.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -3
  4. data/config/default.yml +110 -19
  5. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  6. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  7. data/lib/rubocop/cli/command/show_docs_url.rb +48 -0
  8. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  9. data/lib/rubocop/cli.rb +2 -1
  10. data/lib/rubocop/config_loader_resolver.rb +1 -1
  11. data/lib/rubocop/cop/badge.rb +7 -1
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  13. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
  14. data/lib/rubocop/cop/correctors/if_then_corrector.rb +55 -0
  15. data/lib/rubocop/cop/documentation.rb +19 -2
  16. data/lib/rubocop/cop/gemspec/date_assignment.rb +2 -10
  17. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -10
  18. data/lib/rubocop/cop/gemspec/require_mfa.rb +145 -0
  19. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -3
  20. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -10
  21. data/lib/rubocop/cop/generator.rb +7 -11
  22. data/lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb +46 -0
  23. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +47 -0
  24. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +3 -1
  25. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  26. data/lib/rubocop/cop/layout/argument_alignment.rb +36 -9
  27. data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
  28. data/lib/rubocop/cop/layout/comment_indentation.rb +31 -2
  29. data/lib/rubocop/cop/layout/dot_position.rb +4 -0
  30. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +22 -1
  31. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +5 -1
  32. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -2
  33. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +7 -8
  34. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
  35. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  36. data/lib/rubocop/cop/layout/space_before_first_arg.rb +4 -0
  37. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +11 -5
  38. data/lib/rubocop/cop/lint/ambiguous_range.rb +2 -2
  39. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -1
  40. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +1 -1
  41. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +16 -4
  42. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +6 -0
  43. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  44. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +10 -5
  45. data/lib/rubocop/cop/lint/inherit_exception.rb +19 -28
  46. data/lib/rubocop/cop/lint/number_conversion.rb +5 -2
  47. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -6
  48. data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -0
  49. data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -2
  50. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +117 -0
  51. data/lib/rubocop/cop/lint/useless_times.rb +13 -9
  52. data/lib/rubocop/cop/metrics/block_length.rb +1 -0
  53. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +0 -9
  54. data/lib/rubocop/cop/metrics/method_length.rb +1 -0
  55. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  56. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
  57. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  58. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +1 -1
  59. data/lib/rubocop/cop/mixin/enforce_superclass.rb +5 -0
  60. data/lib/rubocop/cop/mixin/gemspec_help.rb +30 -0
  61. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +4 -3
  62. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +82 -0
  63. data/lib/rubocop/cop/mixin/line_length_help.rb +17 -6
  64. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -1
  65. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -5
  66. data/lib/rubocop/cop/naming/block_forwarding.rb +121 -0
  67. data/lib/rubocop/cop/naming/file_name.rb +37 -4
  68. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  69. data/lib/rubocop/cop/security/open.rb +11 -1
  70. data/lib/rubocop/cop/security/yaml_load.rb +9 -3
  71. data/lib/rubocop/cop/style/character_literal.rb +8 -1
  72. data/lib/rubocop/cop/style/collection_compact.rb +31 -13
  73. data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
  74. data/lib/rubocop/cop/style/def_with_parentheses.rb +16 -11
  75. data/lib/rubocop/cop/style/documentation.rb +1 -1
  76. data/lib/rubocop/cop/style/empty_case_condition.rb +10 -0
  77. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  78. data/lib/rubocop/cop/style/file_read.rb +112 -0
  79. data/lib/rubocop/cop/style/file_write.rb +124 -0
  80. data/lib/rubocop/cop/style/for.rb +4 -0
  81. data/lib/rubocop/cop/style/format_string_token.rb +2 -1
  82. data/lib/rubocop/cop/style/hash_conversion.rb +2 -1
  83. data/lib/rubocop/cop/style/hash_syntax.rb +36 -0
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -6
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +6 -6
  86. data/lib/rubocop/cop/style/if_inside_else.rb +15 -0
  87. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  88. data/lib/rubocop/cop/style/lambda_call.rb +12 -20
  89. data/lib/rubocop/cop/style/map_to_hash.rb +68 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -0
  91. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +3 -1
  92. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -13
  93. data/lib/rubocop/cop/style/nested_file_dirname.rb +66 -0
  94. data/lib/rubocop/cop/style/numeric_literals.rb +10 -1
  95. data/lib/rubocop/cop/style/one_line_conditional.rb +18 -39
  96. data/lib/rubocop/cop/style/open_struct_use.rb +69 -0
  97. data/lib/rubocop/cop/style/optional_boolean_parameter.rb +3 -2
  98. data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -2
  99. data/lib/rubocop/cop/style/quoted_symbols.rb +11 -1
  100. data/lib/rubocop/cop/style/redundant_begin.rb +2 -6
  101. data/lib/rubocop/cop/style/redundant_interpolation.rb +17 -3
  102. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +5 -1
  103. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  104. data/lib/rubocop/cop/style/safe_navigation.rb +1 -5
  105. data/lib/rubocop/cop/style/sample.rb +5 -3
  106. data/lib/rubocop/cop/style/select_by_regexp.rb +6 -1
  107. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
  108. data/lib/rubocop/cop/style/sole_nested_conditional.rb +53 -13
  109. data/lib/rubocop/cop/style/string_concatenation.rb +7 -1
  110. data/lib/rubocop/cop/style/swap_values.rb +2 -0
  111. data/lib/rubocop/cop/style/ternary_parentheses.rb +16 -2
  112. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
  113. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
  114. data/lib/rubocop/cop/style/unless_else.rb +4 -0
  115. data/lib/rubocop/cop/team.rb +1 -1
  116. data/lib/rubocop/cop/util.rb +9 -1
  117. data/lib/rubocop/cop/variable_force.rb +1 -5
  118. data/lib/rubocop/cops_documentation_generator.rb +2 -2
  119. data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -2
  120. data/lib/rubocop/formatter/html_formatter.rb +5 -2
  121. data/lib/rubocop/formatter/json_formatter.rb +4 -1
  122. data/lib/rubocop/options.rb +14 -3
  123. data/lib/rubocop/remote_config.rb +2 -4
  124. data/lib/rubocop/result_cache.rb +1 -1
  125. data/lib/rubocop/rspec/shared_contexts.rb +4 -0
  126. data/lib/rubocop/runner.rb +1 -1
  127. data/lib/rubocop/target_ruby.rb +1 -1
  128. data/lib/rubocop/version.rb +1 -1
  129. data/lib/rubocop/yaml_duplication_checker.rb +1 -1
  130. data/lib/rubocop.rb +12 -0
  131. 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
- corrector.wrap(node.condition, '(', ')') if node.condition.or_type?
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 correct_from_unless_to_if(corrector, node)
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, replacement_condition(and_operator, 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 replacement_condition(and_operator, condition)
167
- if wrap_condition?(condition)
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.str_type? && part.heredoc?) || part.each_descendant(:block).any?
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)
@@ -58,6 +58,8 @@ module RuboCop
58
58
  end
59
59
 
60
60
  def simple_assignment?(node)
61
+ return false unless node.respond_to?(:type)
62
+
61
63
  SIMPLE_ASSIGNMENT_TYPES.include?(node.type)
62
64
  end
63
65
 
@@ -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
- return if only_closing_parenthesis_is_last_line?(node.condition)
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
- private
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 requires a comma after the
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 requires a comma after the
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)
@@ -131,7 +131,7 @@ module RuboCop
131
131
  @options[:stdin] = new_source
132
132
  else
133
133
  filename = processed_source.buffer.name
134
- File.open(filename, 'w') { |f| f.write(new_source) }
134
+ File.write(filename, new_source)
135
135
  end
136
136
  @updated_source_file = true
137
137
  end
@@ -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 !node.respond_to?(:arguments)
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
- regexp_string = node.children.select(&:str_type?).map do |child|
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.start_with?('*') ? "`+#{value}+`" : "`#{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 = OpenStruct.new(offense_count: 0)
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 << OpenStruct.new(path: file, offenses: offenses)
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.
@@ -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 (A/I/R/C/W/E/F) for exit',
468
- 'with error code.'],
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.open cache_path, 'w' do |io|
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
 
@@ -179,7 +179,7 @@ module RuboCop
179
179
  .select { |path| File.file?(path) }
180
180
  .sort!
181
181
  .each do |path|
182
- content = File.open(path, 'rb', &:read)
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
@@ -135,3 +135,7 @@ end
135
135
  RSpec.shared_context 'ruby 3.1', :ruby31 do
136
136
  let(:ruby_version) { 3.1 }
137
137
  end
138
+
139
+ RSpec.shared_context 'ruby 3.2', :ruby32 do
140
+ let(:ruby_version) { 3.2 }
141
+ end
@@ -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 < RuntimeError
11
+ class InfiniteCorrectionLoop < StandardError
12
12
  attr_reader :offenses
13
13
 
14
14
  def initialize(path, offenses_by_iteration, loop_start: -1)
@@ -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 = {
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.22.3'
6
+ STRING = '1.26.1'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, '\
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -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
- on_duplicated.call(exist, key) if exist
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'