rubocop 1.56.4 → 1.60.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 (163) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +4 -4
  4. data/config/default.yml +57 -3
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
  6. data/lib/rubocop/cli.rb +1 -1
  7. data/lib/rubocop/config.rb +0 -2
  8. data/lib/rubocop/config_loader.rb +0 -1
  9. data/lib/rubocop/config_obsoletion.rb +11 -8
  10. data/lib/rubocop/config_validator.rb +0 -2
  11. data/lib/rubocop/cop/base.rb +6 -0
  12. data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
  13. data/lib/rubocop/cop/exclude_limit.rb +1 -1
  14. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
  15. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
  16. data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
  17. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
  18. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  19. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/end_alignment.rb +12 -2
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
  22. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +22 -7
  23. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
  24. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  25. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  26. data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
  27. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
  28. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +16 -1
  29. data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -1
  30. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
  31. data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
  32. data/lib/rubocop/cop/layout/space_around_operators.rb +50 -20
  33. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  34. data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -4
  35. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
  36. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  37. data/lib/rubocop/cop/lint/debugger.rb +11 -1
  38. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
  39. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  40. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -3
  41. data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
  42. data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
  43. data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
  44. data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
  45. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  46. data/lib/rubocop/cop/lint/mixed_case_range.rb +1 -1
  47. data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
  48. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +0 -1
  49. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
  50. data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
  51. data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -0
  52. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +63 -4
  53. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  54. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  55. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
  56. data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
  57. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
  58. data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
  59. data/lib/rubocop/cop/lint/syntax.rb +6 -3
  60. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  61. data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +2 -2
  62. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
  63. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  64. data/lib/rubocop/cop/lint/void.rb +43 -12
  65. data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
  66. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  67. data/lib/rubocop/cop/metrics/class_length.rb +8 -3
  68. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  69. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  70. data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
  71. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
  72. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
  73. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  74. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  75. data/lib/rubocop/cop/naming/block_forwarding.rb +12 -4
  76. data/lib/rubocop/cop/naming/constant_name.rb +1 -2
  77. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  78. data/lib/rubocop/cop/security/open.rb +2 -2
  79. data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -2
  80. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  81. data/lib/rubocop/cop/style/arguments_forwarding.rb +120 -17
  82. data/lib/rubocop/cop/style/array_first_last.rb +64 -0
  83. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
  84. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
  85. data/lib/rubocop/cop/style/case_like_if.rb +4 -4
  86. data/lib/rubocop/cop/style/class_check.rb +1 -0
  87. data/lib/rubocop/cop/style/class_equality_comparison.rb +5 -0
  88. data/lib/rubocop/cop/style/collection_compact.rb +18 -8
  89. data/lib/rubocop/cop/style/combinable_loops.rb +13 -7
  90. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
  91. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  92. data/lib/rubocop/cop/style/date_time.rb +5 -4
  93. data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
  94. data/lib/rubocop/cop/style/each_with_object.rb +2 -2
  95. data/lib/rubocop/cop/style/empty_literal.rb +1 -1
  96. data/lib/rubocop/cop/style/eval_with_location.rb +3 -14
  97. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  98. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  99. data/lib/rubocop/cop/style/format_string.rb +24 -3
  100. data/lib/rubocop/cop/style/guard_clause.rb +26 -0
  101. data/lib/rubocop/cop/style/hash_each_methods.rb +83 -10
  102. data/lib/rubocop/cop/style/hash_except.rb +2 -1
  103. data/lib/rubocop/cop/style/identical_conditional_branches.rb +28 -3
  104. data/lib/rubocop/cop/style/inverse_methods.rb +6 -5
  105. data/lib/rubocop/cop/style/invertible_unless_condition.rb +39 -2
  106. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +3 -2
  107. data/lib/rubocop/cop/style/map_to_hash.rb +17 -7
  108. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +14 -5
  109. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
  110. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
  111. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  112. data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
  113. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  114. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -3
  115. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  116. data/lib/rubocop/cop/style/next.rb +1 -1
  117. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  118. data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
  119. data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
  121. data/lib/rubocop/cop/style/redundant_argument.rb +3 -2
  122. data/lib/rubocop/cop/style/redundant_begin.rb +9 -1
  123. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
  124. data/lib/rubocop/cop/style/redundant_each.rb +7 -4
  125. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  126. data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
  127. data/lib/rubocop/cop/style/redundant_filter_chain.rb +22 -5
  128. data/lib/rubocop/cop/style/redundant_line_continuation.rb +10 -1
  129. data/lib/rubocop/cop/style/redundant_parentheses.rb +68 -21
  130. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  131. data/lib/rubocop/cop/style/redundant_self.rb +17 -2
  132. data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
  133. data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
  134. data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
  135. data/lib/rubocop/cop/style/sample.rb +2 -1
  136. data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
  137. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  138. data/lib/rubocop/cop/style/semicolon.rb +8 -0
  139. data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
  140. data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
  141. data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
  142. data/lib/rubocop/cop/style/string_chars.rb +1 -0
  143. data/lib/rubocop/cop/style/strip.rb +7 -4
  144. data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
  145. data/lib/rubocop/cop/style/symbol_proc.rb +36 -0
  146. data/lib/rubocop/cop/style/unpack_first.rb +11 -14
  147. data/lib/rubocop/cops_documentation_generator.rb +11 -1
  148. data/lib/rubocop/ext/regexp_node.rb +9 -4
  149. data/lib/rubocop/formatter/disabled_config_formatter.rb +17 -6
  150. data/lib/rubocop/formatter/html_formatter.rb +5 -4
  151. data/lib/rubocop/formatter/json_formatter.rb +0 -1
  152. data/lib/rubocop/formatter.rb +1 -1
  153. data/lib/rubocop/lsp/routes.rb +1 -1
  154. data/lib/rubocop/options.rb +0 -8
  155. data/lib/rubocop/result_cache.rb +0 -1
  156. data/lib/rubocop/rspec/shared_contexts.rb +6 -0
  157. data/lib/rubocop/rspec/support.rb +1 -0
  158. data/lib/rubocop/runner.rb +1 -1
  159. data/lib/rubocop/server/cache.rb +1 -1
  160. data/lib/rubocop/version.rb +1 -1
  161. data/lib/rubocop.rb +5 -0
  162. metadata +16 -24
  163. /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
@@ -3,8 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks that arrays are sliced with endless ranges instead of
7
- # `ary[start..-1]` on Ruby 2.6+.
6
+ # Checks that arrays are not sliced with the redundant `ary[0..-1]`, replacing it with `ary`,
7
+ # and ensures arrays are sliced with endless ranges instead of `ary[start..-1]` on Ruby 2.6+,
8
+ # and with beginless ranges instead of `ary[nil..end]` on Ruby 2.7+.
8
9
  #
9
10
  # @safety
10
11
  # This cop is unsafe because `x..-1` and `x..` are only guaranteed to
@@ -21,29 +22,94 @@ module RuboCop
21
22
  #
22
23
  # @example
23
24
  # # bad
24
- # items[1..-1]
25
+ # items[0..-1]
26
+ # items[0..nil]
27
+ # items[0...nil]
25
28
  #
26
29
  # # good
27
- # items[1..]
30
+ # items
31
+ #
32
+ # # bad
33
+ # items[1..-1] # Ruby 2.6+
34
+ # items[1..nil] # Ruby 2.6+
35
+ #
36
+ # # good
37
+ # items[1..] # Ruby 2.6+
38
+ #
39
+ # # bad
40
+ # items[nil..42] # Ruby 2.7+
41
+ #
42
+ # # good
43
+ # items[..42] # Ruby 2.7+
44
+ # items[0..42] # Ruby 2.7+
45
+ #
28
46
  class SlicingWithRange < Base
29
47
  extend AutoCorrector
30
48
  extend TargetRubyVersion
31
49
 
32
50
  minimum_target_ruby_version 2.6
33
51
 
34
- MSG = 'Prefer ary[n..] over ary[n..-1].'
52
+ MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
53
+ MSG_USELESS_RANGE = 'Remove the useless `%<prefer>s`.'
35
54
  RESTRICT_ON_SEND = %i[[]].freeze
36
55
 
56
+ # @!method range_from_zero_till_minus_one?(node)
57
+ def_node_matcher :range_from_zero_till_minus_one?, <<~PATTERN
58
+ {
59
+ (irange (int 0) {(int -1) nil})
60
+ (erange (int 0) nil)
61
+ }
62
+ PATTERN
63
+
37
64
  # @!method range_till_minus_one?(node)
38
- def_node_matcher :range_till_minus_one?, '(irange !nil? (int -1))'
65
+ def_node_matcher :range_till_minus_one?, <<~PATTERN
66
+ {
67
+ (irange !nil? {(int -1) nil})
68
+ (erange !nil? nil)
69
+ }
70
+ PATTERN
71
+
72
+ # @!method range_from_zero?(node)
73
+ def_node_matcher :range_from_zero?, <<~PATTERN
74
+ (irange nil !nil?)
75
+ PATTERN
39
76
 
40
77
  def on_send(node)
41
- return unless node.arguments.count == 1
42
- return unless range_till_minus_one?(node.arguments.first)
78
+ return unless node.arguments.one?
43
79
 
44
- add_offense(node.first_argument) do |corrector|
45
- corrector.remove(node.first_argument.end)
80
+ range_node = node.first_argument
81
+ selector = node.loc.selector
82
+ unless (message, removal_range = offense_message_with_removal_range(range_node, selector))
83
+ return
46
84
  end
85
+
86
+ add_offense(selector, message: message) do |corrector|
87
+ corrector.remove(removal_range)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def offense_message_with_removal_range(range_node, selector)
94
+ if range_from_zero_till_minus_one?(range_node)
95
+ [format(MSG_USELESS_RANGE, prefer: selector.source), selector]
96
+ elsif range_till_minus_one?(range_node)
97
+ [
98
+ format(MSG, prefer: endless(range_node), current: selector.source), range_node.end
99
+ ]
100
+ elsif range_from_zero?(range_node) && target_ruby_version >= 2.7
101
+ [
102
+ format(MSG, prefer: beginless(range_node), current: selector.source), range_node.begin
103
+ ]
104
+ end
105
+ end
106
+
107
+ def endless(range_node)
108
+ "[#{range_node.begin.source}#{range_node.loc.operator.source}]"
109
+ end
110
+
111
+ def beginless(range_node)
112
+ "[#{range_node.loc.operator.source}#{range_node.end.source}]"
47
113
  end
48
114
  end
49
115
  end
@@ -35,6 +35,7 @@ module RuboCop
35
35
  corrector.replace(range, 'chars')
36
36
  end
37
37
  end
38
+ alias on_csend on_send
38
39
  end
39
40
  end
40
41
  end
@@ -22,20 +22,23 @@ module RuboCop
22
22
 
23
23
  # @!method lstrip_rstrip(node)
24
24
  def_node_matcher :lstrip_rstrip, <<~PATTERN
25
- {(send $(send _ $:rstrip) $:lstrip)
26
- (send $(send _ $:lstrip) $:rstrip)}
25
+ {
26
+ (call $(call _ :rstrip) :lstrip)
27
+ (call $(call _ :lstrip) :rstrip)
28
+ }
27
29
  PATTERN
28
30
 
29
31
  def on_send(node)
30
- lstrip_rstrip(node) do |first_send, method_one, method_two|
32
+ lstrip_rstrip(node) do |first_send|
31
33
  range = range_between(first_send.loc.selector.begin_pos, node.source_range.end_pos)
32
- message = format(MSG, methods: "#{method_one}.#{method_two}")
34
+ message = format(MSG, methods: range.source)
33
35
 
34
36
  add_offense(range, message: message) do |corrector|
35
37
  corrector.replace(range, 'strip')
36
38
  end
37
39
  end
38
40
  end
41
+ alias on_csend on_send
39
42
  end
40
43
  end
41
44
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Enforces the presence of parentheses in `super` containing arguments.
7
+ #
8
+ # `super` is a keyword and is provided as a distinct cop from those designed for method call.
9
+ #
10
+ # @example
11
+ #
12
+ # # bad
13
+ # super name, age
14
+ #
15
+ # # good
16
+ # super(name, age)
17
+ #
18
+ class SuperWithArgsParentheses < Base
19
+ extend AutoCorrector
20
+
21
+ MSG = 'Use parentheses for `super` with arguments.'
22
+
23
+ def on_super(node)
24
+ return if node.parenthesized?
25
+
26
+ add_offense(node) do |corrector|
27
+ range = node.loc.keyword.end.join(node.first_argument.source_range.begin)
28
+ corrector.replace(range, '(')
29
+ corrector.insert_after(node.last_argument, ')')
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -37,6 +37,42 @@ module RuboCop
37
37
  # # ArgumentError: wrong number of arguments (given 1, expected 0)
38
38
  # ----
39
39
  #
40
+ # It is also unsafe because `Symbol#to_proc` does not work with
41
+ # `protected` methods which would otherwise be accessible.
42
+ #
43
+ # For example:
44
+ #
45
+ # [source,ruby]
46
+ # ----
47
+ # class Box
48
+ # def initialize
49
+ # @secret = rand
50
+ # end
51
+ #
52
+ # def normal_matches?(*others)
53
+ # others.map { |other| other.secret }.any?(secret)
54
+ # end
55
+ #
56
+ # def symbol_to_proc_matches?(*others)
57
+ # others.map(&:secret).any?(secret)
58
+ # end
59
+ #
60
+ # protected
61
+ #
62
+ # attr_reader :secret
63
+ # end
64
+ #
65
+ # boxes = [Box.new, Box.new]
66
+ # Box.new.normal_matches?(*boxes)
67
+ # # => false
68
+ # boxes.first.normal_matches?(*boxes)
69
+ # # => true
70
+ # Box.new.symbol_to_proc_matches?(*boxes)
71
+ # # => NoMethodError: protected method `secret' called for #<Box...>
72
+ # boxes.first.symbol_to_proc_matches?(*boxes)
73
+ # # => NoMethodError: protected method `secret' called for #<Box...>
74
+ # ----
75
+ #
40
76
  # @example
41
77
  # # bad
42
78
  # something.map { |s| s.upcase }
@@ -23,38 +23,35 @@ module RuboCop
23
23
 
24
24
  minimum_target_ruby_version 2.4
25
25
 
26
- MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of ' \
27
- '`%<receiver>s.unpack(%<format>s)%<method>s`.'
26
+ MSG = 'Use `unpack1(%<format>s)` instead of `%<current>s`.'
28
27
  RESTRICT_ON_SEND = %i[first [] slice at].freeze
29
28
 
30
29
  # @!method unpack_and_first_element?(node)
31
30
  def_node_matcher :unpack_and_first_element?, <<~PATTERN
32
31
  {
33
- (send $(send (...) :unpack $(...)) :first)
34
- (send $(send (...) :unpack $(...)) {:[] :slice :at} (int 0))
32
+ (call $(call (...) :unpack $(...)) :first)
33
+ (call $(call (...) :unpack $(...)) {:[] :slice :at} (int 0))
35
34
  }
36
35
  PATTERN
37
36
 
38
37
  def on_send(node)
39
38
  unpack_and_first_element?(node) do |unpack_call, unpack_arg|
40
- range = first_element_range(node, unpack_call)
41
- message = format(MSG,
42
- receiver: unpack_call.receiver.source,
43
- format: unpack_arg.source,
44
- method: range.source)
45
- add_offense(node, message: message) do |corrector|
46
- corrector.remove(first_element_range(node, unpack_call))
39
+ first_element_range = first_element_range(node, unpack_call)
40
+ offense_range = unpack_call.loc.selector.join(node.source_range.end)
41
+ message = format(MSG, format: unpack_arg.source, current: offense_range.source)
42
+
43
+ add_offense(offense_range, message: message) do |corrector|
44
+ corrector.remove(first_element_range)
47
45
  corrector.replace(unpack_call.loc.selector, 'unpack1')
48
46
  end
49
47
  end
50
48
  end
49
+ alias on_csend on_send
51
50
 
52
51
  private
53
52
 
54
53
  def first_element_range(node, unpack_call)
55
- Parser::Source::Range.new(node.source_range.source_buffer,
56
- unpack_call.source_range.end_pos,
57
- node.source_range.end_pos)
54
+ unpack_call.source_range.end.join(node.source_range.end)
58
55
  end
59
56
  end
60
57
  end
@@ -251,9 +251,18 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
251
251
  "\ninclude::../partials/#{filename}[]\n"
252
252
  end
253
253
 
254
+ # rubocop:disable Metrics/MethodLength
254
255
  def print_cops_of_department(department)
255
256
  selected_cops = cops_of_department(department)
256
- content = +"= #{department}\n"
257
+ content = +<<~HEADER
258
+ ////
259
+ Do NOT edit this file by hand directly, as it is automatically generated.
260
+
261
+ Please make any necessary changes to the cop documentation within the source files themselves.
262
+ ////
263
+
264
+ = #{department}
265
+ HEADER
257
266
  selected_cops.each { |cop| content << print_cop_with_doc(cop) }
258
267
  content << footer_for_department(department)
259
268
  file_name = "#{docs_path}/#{department_to_basename(department)}.adoc"
@@ -262,6 +271,7 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
262
271
  file.write("#{content.strip}\n")
263
272
  end
264
273
  end
274
+ # rubocop:enable Metrics/MethodLength
265
275
 
266
276
  def print_cop_with_doc(cop) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
267
277
  cop_config = config.for_cop(cop)
@@ -54,10 +54,7 @@ module RuboCop
54
54
  return enum_for(__method__, named: named) unless block_given?
55
55
 
56
56
  parsed_tree&.traverse do |event, exp, _index|
57
- yield(exp) if event == :enter &&
58
- named == exp.respond_to?(:name) &&
59
- exp.respond_to?(:capturing?) &&
60
- exp.capturing?
57
+ yield(exp) if named_capturing?(exp, event, named)
61
58
  end
62
59
 
63
60
  self
@@ -65,6 +62,14 @@ module RuboCop
65
62
 
66
63
  private
67
64
 
65
+ def named_capturing?(exp, event, named)
66
+ event == :enter &&
67
+ named == exp.respond_to?(:name) &&
68
+ !exp.text.start_with?('(?<=') &&
69
+ exp.respond_to?(:capturing?) &&
70
+ exp.capturing?
71
+ end
72
+
68
73
  def with_interpolations_blanked
69
74
  # Ignore the trailing regopt node
70
75
  children[0...-1].map do |child|
@@ -30,7 +30,8 @@ module RuboCop
30
30
  @files_with_offenses ||= {}
31
31
  end
32
32
 
33
- def file_started(_file, _file_info)
33
+ def file_started(_file, options)
34
+ @config_for_pwd = options[:config_store].for_pwd
34
35
  @exclude_limit_option = @options[:exclude_limit]
35
36
  @exclude_limit = Integer(@exclude_limit_option ||
36
37
  RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS)
@@ -115,9 +116,13 @@ module RuboCop
115
116
  def set_max(cfg, cop_name)
116
117
  return unless cfg[:exclude_limit]
117
118
 
118
- # In case auto_gen_only_exclude is set, only modify the maximum if the
119
- # files are not excluded one by one.
120
- if !@options[:auto_gen_only_exclude] || @files_with_offenses[cop_name].size > @exclude_limit
119
+ max_set_in_user_config =
120
+ @config_for_pwd.for_cop(cop_name)['Max'] != default_config(cop_name)['Max']
121
+ if !max_set_in_user_config &&
122
+ # In case auto_gen_only_exclude is set, only modify the maximum if the files are not
123
+ # excluded one by one.
124
+ (!@options[:auto_gen_only_exclude] ||
125
+ @files_with_offenses[cop_name].size > @exclude_limit)
121
126
  cfg.merge!(cfg[:exclude_limit])
122
127
  end
123
128
 
@@ -192,8 +197,8 @@ module RuboCop
192
197
  # 'Enabled' option will be put into file only if exclude
193
198
  # limit is exceeded.
194
199
  rejected_keys = ['Enabled']
195
- rejected_keys << 'EnforcedStyle' unless auto_gen_enforced_style?
196
- cfg.reject { |key| rejected_keys.include?(key) }
200
+ rejected_keys << /\AEnforcedStyle\w*/ unless auto_gen_enforced_style?
201
+ cfg.reject { |key| include_or_match?(rejected_keys, key) }
197
202
  end
198
203
 
199
204
  def output_offending_files(output_buffer, cfg, cop_name)
@@ -262,6 +267,12 @@ module RuboCop
262
267
  def no_exclude_limit?
263
268
  @options[:no_exclude_limit] == false
264
269
  end
270
+
271
+ # Returns true if the given arr include the given elm or if any of the
272
+ # given arr is a regexp that matches the given elm.
273
+ def include_or_match?(arr, elm)
274
+ arr.include?(elm) || arr.any? { |x| x.is_a?(Regexp) && x.match?(elm) }
275
+ end
265
276
  end
266
277
  end
267
278
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
3
  require 'cgi'
5
4
  require 'erb'
6
- require 'ostruct'
7
5
 
8
6
  module RuboCop
9
7
  module Formatter
@@ -88,7 +86,7 @@ module RuboCop
88
86
  # rubocop:enable Lint/UselessMethodDefinition
89
87
 
90
88
  def decorated_message(offense)
91
- offense.message.gsub(/`(.+?)`/) { "<code>#{Regexp.last_match(1)}</code>" }
89
+ offense.message.gsub(/`(.+?)`/) { "<code>#{escape(Regexp.last_match(1))}</code>" }
92
90
  end
93
91
 
94
92
  def highlighted_source_line(offense)
@@ -124,7 +122,10 @@ module RuboCop
124
122
 
125
123
  def base64_encoded_logo_image
126
124
  image = File.read(LOGO_IMAGE_PATH, binmode: true)
127
- Base64.encode64(image)
125
+
126
+ # `Base64.encode64` compatible:
127
+ # https://github.com/ruby/base64/blob/v0.1.1/lib/base64.rb#L27-L40
128
+ [image].pack('m')
128
129
  end
129
130
  end
130
131
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require 'pathname'
5
4
 
6
5
  module RuboCop
7
6
  module Formatter
@@ -14,7 +14,7 @@ module RuboCop
14
14
  require_relative 'formatter/emacs_style_formatter'
15
15
  require_relative 'formatter/file_list_formatter'
16
16
  require_relative 'formatter/fuubar_style_formatter'
17
- require_relative 'formatter/git_hub_actions_formatter'
17
+ require_relative 'formatter/github_actions_formatter'
18
18
  require_relative 'formatter/html_formatter'
19
19
  require_relative 'formatter/json_formatter'
20
20
  require_relative 'formatter/junit_formatter'
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # @api private
18
18
  class Routes
19
19
  def self.handle(name, &block)
20
- define_method("handle_#{name}", &block)
20
+ define_method(:"handle_#{name}", &block)
21
21
  end
22
22
 
23
23
  private_class_method :handle
@@ -364,10 +364,6 @@ module RuboCop
364
364
  raise OptionArgumentError, '-C/--cache argument must be true or false'
365
365
  end
366
366
 
367
- if display_only_fail_level_offenses_with_autocorrect?
368
- raise OptionArgumentError, '--autocorrect cannot be used with ' \
369
- '--display-only-fail-level-offenses.'
370
- end
371
367
  validate_auto_gen_config
372
368
  validate_autocorrect
373
369
  validate_display_only_failed
@@ -460,10 +456,6 @@ module RuboCop
460
456
  (@options[:only] & %w[Lint/RedundantCopDisableDirective RedundantCopDisableDirective]).any?
461
457
  end
462
458
 
463
- def display_only_fail_level_offenses_with_autocorrect?
464
- @options.key?(:display_only_fail_level_offenses) && @options.key?(:autocorrect)
465
- end
466
-
467
459
  def except_syntax?
468
460
  @options.key?(:except) && (@options[:except] & %w[Lint/Syntax Syntax]).any?
469
461
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'digest/sha1'
4
- require 'etc'
5
4
  require 'find'
6
5
  require 'zlib'
7
6
  require_relative 'cache_config'
@@ -128,6 +128,12 @@ RSpec.shared_context 'mock console output' do
128
128
  end
129
129
  end
130
130
 
131
+ RSpec.shared_context 'lsp mode' do
132
+ before do
133
+ allow(cop).to receive(:lsp_mode?).and_return(true)
134
+ end
135
+ end
136
+
131
137
  RSpec.shared_context 'ruby 2.0' do
132
138
  let(:ruby_version) { 2.0 }
133
139
  end
@@ -13,6 +13,7 @@ RSpec.configure do |config|
13
13
  config.include HostEnvironmentSimulatorHelper
14
14
  config.include_context 'config', :config
15
15
  config.include_context 'isolated environment', :isolated_environment
16
+ config.include_context 'lsp mode', :lsp_mode
16
17
  config.include_context 'maintain registry', :restore_registry
17
18
  config.include_context 'ruby 2.0', :ruby20
18
19
  config.include_context 'ruby 2.1', :ruby21
@@ -20,7 +20,7 @@ module RuboCop
20
20
  message = 'Infinite loop detected'
21
21
  message += " in #{path}" if path
22
22
  message += " and caused by #{root_cause}" if root_cause
23
- super message
23
+ super(message)
24
24
  end
25
25
  end
26
26
 
@@ -116,7 +116,7 @@ module RuboCop
116
116
 
117
117
  def pid_running?
118
118
  Process.kill(0, pid_path.read.to_i) == 1
119
- rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES
119
+ rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES, Errno::EROFS
120
120
  false
121
121
  end
122
122
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.56.4'
6
+ STRING = '1.60.1'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
data/lib/rubocop.rb CHANGED
@@ -332,8 +332,10 @@ require_relative 'rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler'
332
332
  require_relative 'rubocop/cop/lint/ineffective_access_modifier'
333
333
  require_relative 'rubocop/cop/lint/inherit_exception'
334
334
  require_relative 'rubocop/cop/lint/interpolation_check'
335
+ require_relative 'rubocop/cop/lint/it_without_arguments_in_block'
335
336
  require_relative 'rubocop/cop/lint/lambda_without_literal_block'
336
337
  require_relative 'rubocop/cop/lint/literal_as_condition'
338
+ require_relative 'rubocop/cop/lint/literal_assignment_in_condition'
337
339
  require_relative 'rubocop/cop/lint/literal_in_interpolation'
338
340
  require_relative 'rubocop/cop/lint/loop'
339
341
  require_relative 'rubocop/cop/lint/missing_cop_enable_directive'
@@ -454,6 +456,7 @@ require_relative 'rubocop/cop/style/alias'
454
456
  require_relative 'rubocop/cop/style/and_or'
455
457
  require_relative 'rubocop/cop/style/arguments_forwarding'
456
458
  require_relative 'rubocop/cop/style/array_coercion'
459
+ require_relative 'rubocop/cop/style/array_first_last'
457
460
  require_relative 'rubocop/cop/style/array_intersect'
458
461
  require_relative 'rubocop/cop/style/array_join'
459
462
  require_relative 'rubocop/cop/style/ascii_comments'
@@ -580,6 +583,7 @@ require_relative 'rubocop/cop/style/redundant_regexp_constructor'
580
583
  require_relative 'rubocop/cop/style/redundant_self_assignment'
581
584
  require_relative 'rubocop/cop/style/redundant_self_assignment_branch'
582
585
  require_relative 'rubocop/cop/style/require_order'
586
+ require_relative 'rubocop/cop/style/single_line_do_end_block'
583
587
  require_relative 'rubocop/cop/style/sole_nested_conditional'
584
588
  require_relative 'rubocop/cop/style/static_class'
585
589
  require_relative 'rubocop/cop/style/map_compact_with_conditional_block'
@@ -677,6 +681,7 @@ require_relative 'rubocop/cop/style/string_literals_in_interpolation'
677
681
  require_relative 'rubocop/cop/style/string_methods'
678
682
  require_relative 'rubocop/cop/style/strip'
679
683
  require_relative 'rubocop/cop/style/struct_inheritance'
684
+ require_relative 'rubocop/cop/style/super_with_args_parentheses'
680
685
  require_relative 'rubocop/cop/style/swap_values'
681
686
  require_relative 'rubocop/cop/style/symbol_array'
682
687
  require_relative 'rubocop/cop/style/symbol_literal'