rubocop 1.18.2 → 1.19.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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +23 -6
  4. data/lib/rubocop.rb +4 -0
  5. data/lib/rubocop/cli.rb +18 -0
  6. data/lib/rubocop/config_loader.rb +1 -1
  7. data/lib/rubocop/config_loader_resolver.rb +22 -7
  8. data/lib/rubocop/config_validator.rb +18 -5
  9. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  10. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  11. data/lib/rubocop/cop/documentation.rb +1 -1
  12. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  14. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  15. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  16. data/lib/rubocop/cop/layout/class_structure.rb +5 -1
  17. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  18. data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
  19. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  20. data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
  21. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  22. data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
  23. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  24. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -22
  25. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
  26. data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
  27. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  28. data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
  29. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  30. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  31. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  32. data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
  33. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  34. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  35. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  36. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  37. data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
  38. data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
  39. data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
  40. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  41. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  42. data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
  43. data/lib/rubocop/cop/style/block_delimiters.rb +31 -0
  44. data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
  45. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  46. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
  47. data/lib/rubocop/cop/style/double_negation.rb +12 -1
  48. data/lib/rubocop/cop/style/encoding.rb +26 -15
  49. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  50. data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
  51. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +8 -2
  52. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
  53. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  54. data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
  55. data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
  56. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  57. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  58. data/lib/rubocop/cop/style/mutable_constant.rb +6 -8
  59. data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
  60. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
  61. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  62. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  63. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  64. data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
  65. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
  66. data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
  67. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  68. data/lib/rubocop/cop/style/word_array.rb +23 -5
  69. data/lib/rubocop/cop/util.rb +7 -2
  70. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
  71. data/lib/rubocop/magic_comment.rb +44 -15
  72. data/lib/rubocop/options.rb +1 -1
  73. data/lib/rubocop/version.rb +1 -1
  74. metadata +15 -9
@@ -6,6 +6,11 @@ module RuboCop
6
6
  # Check for uses of braces or do/end around single line or
7
7
  # multi-line blocks.
8
8
  #
9
+ # Methods that can be either procedural or functional and cannot be
10
+ # categorised from their usage alone is ignored.
11
+ # `lambda`, `proc`, and `it` are their defaults.
12
+ # Additional methods can be added to the `IgnoredMethods`.
13
+ #
9
14
  # @example EnforcedStyle: line_count_based (default)
10
15
  # # bad - single line block
11
16
  # items.each do |item| item / 5 end
@@ -132,9 +137,21 @@ module RuboCop
132
137
  # puts foo
133
138
  # end
134
139
  #
140
+ # @example IgnoredMethods: ['lambda', 'proc', 'it' ] (default)
141
+ #
142
+ # # good
143
+ # foo = lambda do |x|
144
+ # puts "Hello, #{x}"
145
+ # end
146
+ #
147
+ # foo = lambda do |x|
148
+ # x * 100
149
+ # end
150
+ #
135
151
  class BlockDelimiters < Base
136
152
  include ConfigurableEnforcedStyle
137
153
  include IgnoredMethods
154
+ include RangeHelp
138
155
  extend AutoCorrector
139
156
 
140
157
  ALWAYS_BRACES_MESSAGE = 'Prefer `{...}` over `do...end` for blocks.'
@@ -231,6 +248,11 @@ module RuboCop
231
248
  corrector.insert_before(e, ' ') unless whitespace_before?(e)
232
249
  corrector.insert_after(b, ' ') unless whitespace_after?(b)
233
250
  corrector.replace(b, 'do')
251
+
252
+ if (comment = processed_source.comment_at_line(e.line))
253
+ move_comment_before_block(corrector, comment, loc.node, e)
254
+ end
255
+
234
256
  corrector.replace(e, 'end')
235
257
  end
236
258
 
@@ -252,6 +274,15 @@ module RuboCop
252
274
  /\s/.match?(range.source_buffer.source[range.begin_pos + length, 1])
253
275
  end
254
276
 
277
+ def move_comment_before_block(corrector, comment, block_node, closing_brace)
278
+ range = range_between(closing_brace.end_pos, comment.loc.expression.end_pos)
279
+
280
+ corrector.remove(range_with_surrounding_space(range: range, side: :right))
281
+ corrector.insert_after(closing_brace, "\n")
282
+
283
+ corrector.insert_before(block_node, "#{comment.text}\n")
284
+ end
285
+
255
286
  def get_blocks(node, &block)
256
287
  case node.type
257
288
  when :block
@@ -12,6 +12,7 @@ module RuboCop
12
12
  #
13
13
  # Auto-correction removes comments from `end` keyword and keeps comments
14
14
  # for `class`, `module`, `def` and `begin` above the keyword.
15
+ # It is marked as unsafe auto-correction as it may remove meaningful comments.
15
16
  #
16
17
  # @example
17
18
  # # bad
@@ -50,7 +51,7 @@ module RuboCop
50
51
 
51
52
  def on_new_investigation
52
53
  processed_source.comments.each do |comment|
53
- next unless (match = line(comment).match(/(?<keyword>\S+).*#/)) && offensive?(comment)
54
+ next unless offensive?(comment) && (match = line(comment).match(/(?<keyword>\S+).*#/))
54
55
 
55
56
  register_offense(comment, match[:keyword])
56
57
  end
@@ -26,7 +26,7 @@ module RuboCop
26
26
  # `when` nodes contain the entire branch including the condition.
27
27
  # We only need the contents of the branch, not the condition.
28
28
  def expand_when_branches(when_branches)
29
- when_branches.map { |branch| branch.children[1] }
29
+ when_branches.map(&:body)
30
30
  end
31
31
 
32
32
  def tail(branch)
@@ -272,6 +272,16 @@ module RuboCop
272
272
  check_node(node, branches)
273
273
  end
274
274
 
275
+ def on_case_match(node)
276
+ return unless style == :assign_to_condition
277
+ return unless node.else_branch
278
+
279
+ in_pattern_branches = expand_when_branches(node.in_pattern_branches)
280
+ branches = [*in_pattern_branches, node.else_branch]
281
+
282
+ check_node(node, branches)
283
+ end
284
+
275
285
  private
276
286
 
277
287
  def check_assignment_to_condition(node)
@@ -297,7 +307,7 @@ module RuboCop
297
307
  end
298
308
 
299
309
  # @!method candidate_condition?(node)
300
- def_node_matcher :candidate_condition?, '[{if case} !#allowed_ternary?]'
310
+ def_node_matcher :candidate_condition?, '[{if case case_match} !#allowed_ternary?]'
301
311
 
302
312
  def allowed_ternary?(assignment)
303
313
  assignment.if_type? && assignment.ternary? && !include_ternary?
@@ -319,7 +329,7 @@ module RuboCop
319
329
  end
320
330
 
321
331
  def move_assignment_outside_condition(corrector, node)
322
- if node.case_type?
332
+ if node.case_type? || node.case_match_type?
323
333
  CaseCorrector.correct(corrector, self, node)
324
334
  elsif node.ternary?
325
335
  TernaryCorrector.correct(corrector, node)
@@ -333,7 +343,7 @@ module RuboCop
333
343
 
334
344
  if ternary_condition?(condition)
335
345
  TernaryCorrector.move_assignment_inside_condition(corrector, node)
336
- elsif condition.case_type?
346
+ elsif condition.case_type? || condition.case_match_type?
337
347
  CaseCorrector.move_assignment_inside_condition(corrector, node)
338
348
  elsif condition.if_type?
339
349
  IfCorrector.move_assignment_inside_condition(corrector, node)
@@ -631,7 +641,11 @@ module RuboCop
631
641
  end
632
642
 
633
643
  def extract_branches(case_node)
634
- when_branches = expand_when_branches(case_node.when_branches)
644
+ when_branches = if case_node.case_type?
645
+ expand_when_branches(case_node.when_branches)
646
+ else
647
+ expand_when_branches(case_node.in_pattern_branches)
648
+ end
635
649
 
636
650
  [when_branches, case_node.else_branch]
637
651
  end
@@ -36,13 +36,7 @@ module RuboCop
36
36
  next unless comment.text.scan(/# rubocop:(?:disable|todo)/).size > 1
37
37
 
38
38
  add_offense(comment) do |corrector|
39
- prefix = if comment.text.start_with?('# rubocop:disable')
40
- '# rubocop:disable'
41
- else
42
- '# rubocop:todo'
43
- end
44
-
45
- corrector.replace(comment, comment.text[/#{prefix} \S+/])
39
+ corrector.replace(comment, comment.text.gsub(%r{ # rubocop:(disable|todo)}, ','))
46
40
  end
47
41
  end
48
42
  end
@@ -62,7 +62,7 @@ module RuboCop
62
62
  def end_of_method_definition?(node)
63
63
  return false unless (def_node = find_def_node_from_ascendant(node))
64
64
 
65
- last_child = def_node.child_nodes.last
65
+ last_child = find_last_child(def_node.body)
66
66
 
67
67
  last_child.last_line == node.last_line
68
68
  end
@@ -73,6 +73,17 @@ module RuboCop
73
73
 
74
74
  find_def_node_from_ascendant(node.parent)
75
75
  end
76
+
77
+ def find_last_child(node)
78
+ case node.type
79
+ when :rescue
80
+ find_last_child(node.body)
81
+ when :ensure
82
+ find_last_child(node.child_nodes.first)
83
+ else
84
+ node.child_nodes.last
85
+ end
86
+ end
76
87
  end
77
88
  end
78
89
  end
@@ -13,38 +13,49 @@ module RuboCop
13
13
  include RangeHelp
14
14
  extend AutoCorrector
15
15
 
16
- MSG_UNNECESSARY = 'Unnecessary utf-8 encoding comment.'
16
+ MSG = 'Unnecessary utf-8 encoding comment.'
17
17
  ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/.freeze
18
18
  SHEBANG = '#!'
19
19
 
20
20
  def on_new_investigation
21
21
  return if processed_source.buffer.source.empty?
22
22
 
23
- line_number = encoding_line_number(processed_source)
24
- return unless (@message = offense(processed_source, line_number))
23
+ comments.each do |line_number, comment|
24
+ next unless offense?(comment)
25
25
 
26
- range = processed_source.buffer.line_range(line_number + 1)
27
- add_offense(range, message: @message) do |corrector|
28
- corrector.remove(range_with_surrounding_space(range: range, side: :right))
26
+ register_offense(line_number, comment)
29
27
  end
30
28
  end
31
29
 
32
30
  private
33
31
 
34
- def offense(processed_source, line_number)
35
- line = processed_source[line_number]
32
+ def comments
33
+ processed_source.lines.each.with_index.with_object({}) do |(line, line_number), comments|
34
+ next if line.start_with?(SHEBANG)
36
35
 
37
- MSG_UNNECESSARY if encoding_omitable?(line)
36
+ comment = MagicComment.parse(line)
37
+ return comments unless comment.valid?
38
+
39
+ comments[line_number + 1] = comment
40
+ end
38
41
  end
39
42
 
40
- def encoding_omitable?(line)
41
- ENCODING_PATTERN.match?(line)
43
+ def offense?(comment)
44
+ comment.encoding_specified? && comment.encoding.casecmp('utf-8').zero?
42
45
  end
43
46
 
44
- def encoding_line_number(processed_source)
45
- line_number = 0
46
- line_number += 1 if processed_source[line_number].start_with?(SHEBANG)
47
- line_number
47
+ def register_offense(line_number, comment)
48
+ range = processed_source.buffer.line_range(line_number)
49
+
50
+ add_offense(range) do |corrector|
51
+ text = comment.without(:encoding)
52
+
53
+ if text.blank?
54
+ corrector.remove(range_with_surrounding_space(range: range, side: :right))
55
+ else
56
+ corrector.replace(range, text)
57
+ end
58
+ end
48
59
  end
49
60
  end
50
61
  end
@@ -43,7 +43,7 @@ module RuboCop
43
43
  # RUBY
44
44
  #
45
45
  # This cop works only when a string literal is given as a code string.
46
- # No offence is reported if a string variable is given as below:
46
+ # No offense is reported if a string variable is given as below:
47
47
  #
48
48
  # @example
49
49
  # # not checked
@@ -93,18 +93,43 @@ module RuboCop
93
93
 
94
94
  def add_block_argument(node, corrector)
95
95
  if node.arguments?
96
- last_arg = node.arguments.last
97
- arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
98
- replacement = ' &block'
99
- replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
100
- corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
101
- elsif node.call_type? || node.zsuper_type?
102
- corrector.insert_after(node, '(&block)')
96
+ insert_argument(node, corrector)
97
+ elsif empty_arguments?(node)
98
+ corrector.replace(node.arguments, '(&block)')
99
+ elsif call_like?(node)
100
+ correct_call_node(node, corrector)
103
101
  else
104
102
  corrector.insert_after(node.loc.name, '(&block)')
105
103
  end
106
104
  end
107
105
 
106
+ def empty_arguments?(node)
107
+ # Is there an arguments node with only parentheses?
108
+ node.arguments.is_a?(RuboCop::AST::Node) && node.arguments.loc.begin
109
+ end
110
+
111
+ def call_like?(node)
112
+ node.call_type? || node.zsuper_type? || node.super_type?
113
+ end
114
+
115
+ def insert_argument(node, corrector)
116
+ last_arg = node.arguments.last
117
+ arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
118
+ replacement = ' &block'
119
+ replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
120
+ corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
121
+ end
122
+
123
+ def correct_call_node(node, corrector)
124
+ corrector.insert_after(node, '(&block)')
125
+ return unless node.parenthesized?
126
+
127
+ args_begin = Util.args_begin(node)
128
+ args_end = Util.args_end(node)
129
+ range = range_between(args_begin.begin_pos, args_end.end_pos)
130
+ corrector.remove(range)
131
+ end
132
+
108
133
  def block_body_range(block_node, send_node)
109
134
  range_between(send_node.loc.expression.end_pos, block_node.loc.end.end_pos)
110
135
  end
@@ -5,11 +5,17 @@ module RuboCop
5
5
  module Style
6
6
  # This cop is designed to help you transition from mutable string literals
7
7
  # to frozen string literals.
8
- # It will add the comment `# frozen_string_literal: true` to the top of
9
- # files to enable frozen string literals. Frozen string literals may be
8
+ # It will add the `# frozen_string_literal: true` magic comment to the top
9
+ # of files to enable frozen string literals. Frozen string literals may be
10
10
  # default in future Ruby. The comment will be added below a shebang and
11
11
  # encoding comment.
12
12
  #
13
+ # Note that the cop will ignore files where the comment exists but is set
14
+ # to `false` instead of `true`.
15
+ #
16
+ # To require a blank line after this comment, please see
17
+ # `Layout/EmptyLineAfterMagicComment` cop.
18
+ #
13
19
  # @example EnforcedStyle: always (default)
14
20
  # # The `always` style will always add the frozen string literal comment
15
21
  # # to a file, regardless of the Ruby version or if `freeze` or `<<` are
@@ -29,6 +29,7 @@ module RuboCop
29
29
  # # good
30
30
  # [{ one: 1 }, { two: 2 }]
31
31
  class HashAsLastArrayItem < Base
32
+ include RangeHelp
32
33
  include ConfigurableEnforcedStyle
33
34
  extend AutoCorrector
34
35
 
@@ -74,6 +75,7 @@ module RuboCop
74
75
  return if node.children.empty? # Empty hash cannot be "unbraced"
75
76
 
76
77
  add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
78
+ remove_last_element_trailing_comma(corrector, node.parent)
77
79
  corrector.remove(node.loc.begin)
78
80
  corrector.remove(node.loc.end)
79
81
  end
@@ -82,6 +84,15 @@ module RuboCop
82
84
  def braces_style?
83
85
  style == :braces
84
86
  end
87
+
88
+ def remove_last_element_trailing_comma(corrector, node)
89
+ range = range_with_surrounding_space(
90
+ range: node.children.last.source_range,
91
+ side: :right
92
+ ).end.resize(1)
93
+
94
+ corrector.remove(range) if range.source == ','
95
+ end
85
96
  end
86
97
  end
87
98
  end
@@ -74,7 +74,7 @@ module RuboCop
74
74
  ruby19_no_mixed_keys_check(pairs)
75
75
  elsif style == :no_mixed_keys
76
76
  no_mixed_keys_check(pairs)
77
- elsif node.source.include?('=>')
77
+ else
78
78
  ruby19_check(pairs)
79
79
  end
80
80
  end
@@ -27,11 +27,8 @@ module RuboCop
27
27
  # {a: 1, b: 2}.transform_keys { |k| k.to_s }
28
28
  class HashTransformKeys < Base
29
29
  include HashTransformMethod
30
- extend TargetRubyVersion
31
30
  extend AutoCorrector
32
31
 
33
- minimum_target_ruby_version 2.5
34
-
35
32
  # @!method on_bad_each_with_object(node)
36
33
  def_node_matcher :on_bad_each_with_object, <<~PATTERN
37
34
  (block
@@ -7,6 +7,22 @@ module RuboCop
7
7
  # each branch of a conditional expression. Such expressions should normally
8
8
  # be placed outside the conditional expression - before or after it.
9
9
  #
10
+ # This cop is marked unsafe auto-correction as the order of method invocations
11
+ # must be guaranteed in the following case:
12
+ #
13
+ # [source,ruby]
14
+ # ----
15
+ # if method_that_modifies_global_state # 1
16
+ # method_that_relies_on_global_state # 2
17
+ # foo # 3
18
+ # else
19
+ # method_that_relies_on_global_state # 2
20
+ # bar # 3
21
+ # end
22
+ # ----
23
+ #
24
+ # In such a case, auto-correction may change the invocation order.
25
+ #
10
26
  # NOTE: The cop is poorly named and some people might think that it actually
11
27
  # checks for duplicated conditional branches. The name will probably be changed
12
28
  # in a future major RuboCop release.
@@ -124,21 +140,30 @@ module RuboCop
124
140
  return if branches.any?(&:nil?)
125
141
 
126
142
  tails = branches.map { |branch| tail(branch) }
127
- check_expressions(node, tails, :after_condition) if duplicated_expressions?(tails)
143
+ check_expressions(node, tails, :after_condition) if duplicated_expressions?(node, tails)
128
144
 
129
145
  heads = branches.map { |branch| head(branch) }
130
- check_expressions(node, heads, :before_condition) if duplicated_expressions?(heads)
146
+ check_expressions(node, heads, :before_condition) if duplicated_expressions?(node, heads)
131
147
  end
132
148
 
133
- def duplicated_expressions?(expressions)
134
- expressions.size > 1 && expressions.uniq.one?
149
+ def duplicated_expressions?(node, expressions)
150
+ unique_expressions = expressions.uniq
151
+ return false unless expressions.size >= 1 && unique_expressions.one?
152
+
153
+ unique_expression = unique_expressions.first
154
+ return true unless unique_expression.assignment?
155
+
156
+ lhs = unique_expression.child_nodes.first
157
+ node.condition.child_nodes.none? { |n| n.source == lhs.source if n.variable? }
135
158
  end
136
159
 
137
- def check_expressions(node, expressions, insert_position)
160
+ def check_expressions(node, expressions, insert_position) # rubocop:disable Metrics/MethodLength
138
161
  inserted_expression = false
139
162
 
140
163
  expressions.each do |expression|
141
164
  add_offense(expression) do |corrector|
165
+ next if node.if_type? && node.ternary?
166
+
142
167
  range = range_by_whole_lines(expression.source_range, include_final_newline: true)
143
168
  corrector.remove(range)
144
169
  next if inserted_expression