rubocop 1.42.0 → 1.45.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +73 -31
  4. data/lib/rubocop/cli.rb +54 -8
  5. data/lib/rubocop/config_loader.rb +12 -15
  6. data/lib/rubocop/config_loader_resolver.rb +3 -4
  7. data/lib/rubocop/cop/base.rb +27 -9
  8. data/lib/rubocop/cop/commissioner.rb +8 -2
  9. data/lib/rubocop/cop/cop.rb +23 -3
  10. data/lib/rubocop/cop/corrector.rb +10 -2
  11. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
  12. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  13. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  14. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  15. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
  16. data/lib/rubocop/cop/layout/class_structure.rb +2 -16
  17. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
  18. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  19. data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
  20. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  21. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  22. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
  23. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  24. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  25. data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
  26. data/lib/rubocop/cop/lint/debugger.rb +8 -27
  27. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  28. data/lib/rubocop/cop/lint/else_layout.rb +2 -6
  29. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
  30. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
  31. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  32. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  33. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
  34. data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
  35. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
  36. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
  37. data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
  38. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +9 -1
  39. data/lib/rubocop/cop/lint/void.rb +19 -10
  40. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  41. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  42. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  43. data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
  44. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +2 -5
  45. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  46. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  47. data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
  48. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +57 -23
  49. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  50. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
  51. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  52. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  53. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
  54. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  55. data/lib/rubocop/cop/registry.rb +12 -7
  56. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
  57. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
  58. data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
  59. data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
  60. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  61. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  62. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
  63. data/lib/rubocop/cop/style/documentation.rb +1 -1
  64. data/lib/rubocop/cop/style/documentation_method.rb +6 -0
  65. data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
  66. data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
  67. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  68. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  69. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
  70. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  71. data/lib/rubocop/cop/style/min_max_comparison.rb +11 -1
  72. data/lib/rubocop/cop/style/missing_else.rb +13 -1
  73. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  74. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  75. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  76. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
  77. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  78. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
  79. data/lib/rubocop/cop/style/operator_method_call.rb +16 -2
  80. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
  81. data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
  82. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  83. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  84. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  85. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  86. data/lib/rubocop/cop/style/require_order.rb +2 -9
  87. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  88. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  89. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
  90. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  91. data/lib/rubocop/cop/style/word_array.rb +1 -1
  92. data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
  93. data/lib/rubocop/cop/style/yoda_expression.rb +18 -2
  94. data/lib/rubocop/cop/team.rb +19 -14
  95. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  96. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
  97. data/lib/rubocop/cop/variable_force.rb +1 -1
  98. data/lib/rubocop/formatter.rb +0 -1
  99. data/lib/rubocop/options.rb +22 -1
  100. data/lib/rubocop/path_util.rb +11 -6
  101. data/lib/rubocop/rspec/expect_offense.rb +6 -4
  102. data/lib/rubocop/runner.rb +40 -4
  103. data/lib/rubocop/server/cache.rb +10 -3
  104. data/lib/rubocop/server/cli.rb +37 -18
  105. data/lib/rubocop/server/client_command/exec.rb +1 -1
  106. data/lib/rubocop/server/client_command/start.rb +6 -1
  107. data/lib/rubocop/server/core.rb +23 -8
  108. data/lib/rubocop/version.rb +1 -1
  109. data/lib/rubocop.rb +5 -0
  110. metadata +12 -27
@@ -9,6 +9,20 @@ module RuboCop
9
9
  # Keyword arguments can optionally be excluded from the total count,
10
10
  # as they add less complexity than positional or optional parameters.
11
11
  #
12
+ # Any number of arguments for `initialize` method inside a block of
13
+ # `Struct.new` and `Data.define` like this is always allowed:
14
+ #
15
+ # [source,ruby]
16
+ # ----
17
+ # Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
18
+ # def initialize(one:, two:, three:, four:, five:)
19
+ # end
20
+ # end
21
+ # ----
22
+ #
23
+ # This is because checking the number of arguments of the `initialize` method
24
+ # does not make sense.
25
+ #
12
26
  # NOTE: Explicit block argument `&block` is not counted to prevent
13
27
  # erroneous change that is avoided by making block argument implicit.
14
28
  #
@@ -63,6 +77,16 @@ module RuboCop
63
77
  NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
64
78
  private_constant :NAMED_KEYWORD_TYPES
65
79
 
80
+ # @!method struct_new_or_data_define_block?(node)
81
+ def_node_matcher :struct_new_or_data_define_block?, <<~PATTERN
82
+ (block
83
+ {
84
+ (send (const {nil? cbase} :Struct) :new ...)
85
+ (send (const {nil? cbase} :Data) :define ...)
86
+ }
87
+ (args) ...)
88
+ PATTERN
89
+
66
90
  def on_def(node)
67
91
  optargs = node.arguments.select(&:optarg_type?)
68
92
  return if optargs.count <= max_optional_parameters
@@ -78,6 +102,9 @@ module RuboCop
78
102
  alias on_defs on_def
79
103
 
80
104
  def on_args(node)
105
+ parent = node.parent
106
+ return if parent.method?(:initialize) && struct_new_or_data_define_block?(parent.parent)
107
+
81
108
  count = args_count(node)
82
109
  return unless count > max_params
83
110
 
@@ -25,10 +25,7 @@ module RuboCop
25
25
  # > http://c2.com/cgi/wiki?AbcMetric
26
26
  CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
27
27
 
28
- # TODO: move to rubocop-ast
29
- ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].freeze
30
-
31
- private_constant :BRANCH_NODES, :CONDITION_NODES, :ARGUMENT_TYPES
28
+ private_constant :BRANCH_NODES, :CONDITION_NODES
32
29
 
33
30
  def self.calculate(node, discount_repeated_attributes: false)
34
31
  new(node, discount_repeated_attributes: discount_repeated_attributes).calculate
@@ -129,7 +126,7 @@ module RuboCop
129
126
  end
130
127
 
131
128
  def argument?(node)
132
- ARGUMENT_TYPES.include?(node.type) && capturing_variable?(node.children.first)
129
+ node.argument_type? && capturing_variable?(node.children.first)
133
130
  end
134
131
 
135
132
  def condition?(node)
@@ -12,7 +12,7 @@ module RuboCop
12
12
  attr_reader :column_delta
13
13
 
14
14
  def configured_indentation_width
15
- cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width']
15
+ cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width'] || 2
16
16
  end
17
17
 
18
18
  def indentation(node)
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # This module encapsulates the ability to allow certain methods when
6
- # parsing.
6
+ # parsing. Even if the code is in offense, if it contains methods
7
+ # that are allowed. This module is equivalent to the IgnoredMethods module,
8
+ # which will be deprecated in RuboCop 2.0.
7
9
  module AllowedMethods
8
10
  private
9
11
 
@@ -62,10 +62,12 @@ module RuboCop
62
62
  # Returns the end line of a node, which might be a comment and not part of the AST
63
63
  # End line is considered either the line at which another node starts, or
64
64
  # the line at which the parent node ends.
65
- # rubocop:disable Metrics/AbcSize
65
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
66
66
  def find_end_line(node)
67
- if node.if_type? && node.loc.else
67
+ if node.if_type? && node.else?
68
68
  node.loc.else.line
69
+ elsif node.if_type? && node.ternary?
70
+ node.else_branch.loc.line
69
71
  elsif (next_sibling = node.right_sibling)
70
72
  next_sibling.loc.line
71
73
  elsif (parent = node.parent)
@@ -74,7 +76,7 @@ module RuboCop
74
76
  node.loc.end.line
75
77
  end
76
78
  end
77
- # rubocop:enable Metrics/AbcSize
79
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
78
80
  end
79
81
  end
80
82
  end
@@ -3,6 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # This module checks for Ruby 3.1's hash value omission syntax.
6
+ # rubocop:disable Metrics/ModuleLength
6
7
  module HashShorthandSyntax
7
8
  OMIT_HASH_VALUE_MSG = 'Omit the hash value.'
8
9
  EXPLICIT_HASH_VALUE_MSG = 'Include the hash value.'
@@ -45,21 +46,22 @@ module RuboCop
45
46
 
46
47
  private
47
48
 
48
- # rubocop:disable Metrics/AbcSize
49
- def register_offense(node, message, replacement)
49
+ def register_offense(node, message, replacement) # rubocop:disable Metrics/AbcSize
50
50
  add_offense(node.value, message: message) do |corrector|
51
51
  if (def_node = def_node_that_require_parentheses(node))
52
- white_spaces = range_between(def_node.loc.selector.end_pos,
52
+ last_argument = def_node.last_argument
53
+ if last_argument.nil? || !last_argument.hash_type?
54
+ next corrector.replace(node, replacement)
55
+ end
56
+
57
+ white_spaces = range_between(def_node.selector.end_pos,
53
58
  def_node.first_argument.source_range.begin_pos)
54
59
  corrector.replace(white_spaces, '(')
55
-
56
- last_argument = def_node.arguments.last
57
60
  corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
58
61
  end
59
62
  corrector.replace(node, replacement)
60
63
  end
61
64
  end
62
- # rubocop:enable Metrics/AbcSize
63
65
 
64
66
  def ignore_mixed_hash_shorthand_syntax?(hash_node)
65
67
  target_ruby_version <= 3.0 || enforced_shorthand_syntax != 'consistent' ||
@@ -86,25 +88,37 @@ module RuboCop
86
88
  end
87
89
 
88
90
  def require_hash_value_for_around_hash_literal?(node)
89
- return false unless (send_node = find_ancestor_send_node(node))
91
+ return false unless (method_dispatch_node = find_ancestor_method_dispatch_node(node))
90
92
 
91
- !node.parent.braces? && !use_element_of_hash_literal_as_receiver?(send_node, node.parent) &&
92
- use_modifier_form_without_parenthesized_method_call?(send_node)
93
+ !node.parent.braces? &&
94
+ !use_element_of_hash_literal_as_receiver?(method_dispatch_node, node.parent) &&
95
+ use_modifier_form_without_parenthesized_method_call?(method_dispatch_node)
93
96
  end
94
97
 
98
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
95
99
  def def_node_that_require_parentheses(node)
96
- return unless (send_node = find_ancestor_send_node(node))
97
- return unless without_parentheses_call_expr_follows?(send_node)
100
+ last_pair = node.parent.pairs.last
101
+ return unless last_pair.key.source == last_pair.value.source
102
+ return unless (dispatch_node = find_ancestor_method_dispatch_node(node))
103
+ return if node.respond_to?(:parenthesized?) && !node.parenthesized?
104
+ return unless last_expression?(dispatch_node) || method_dispatch_as_argument?(dispatch_node)
98
105
 
99
- def_node = node.each_ancestor(:send, :csend).first
106
+ def_node = node.each_ancestor(:send, :csend, :super, :yield).first
100
107
 
101
- def_node unless def_node && def_node.arguments.empty?
108
+ DefNode.new(def_node) unless def_node && def_node.arguments.empty?
102
109
  end
110
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
103
111
 
104
- def find_ancestor_send_node(node)
105
- ancestor = node.parent.parent
112
+ def find_ancestor_method_dispatch_node(node)
113
+ return unless (ancestor = node.parent.parent)
114
+ return unless ancestor.call_type? || ancestor.super_type? || ancestor.yield_type?
115
+ return if brackets?(ancestor)
116
+
117
+ ancestor
118
+ end
106
119
 
107
- ancestor if ancestor&.call_type? && !ancestor&.method?(:[])
120
+ def brackets?(method_dispatch_node)
121
+ method_dispatch_node.method?(:[]) || method_dispatch_node.method?(:[]=)
108
122
  end
109
123
 
110
124
  def use_element_of_hash_literal_as_receiver?(ancestor, parent)
@@ -118,15 +132,16 @@ module RuboCop
118
132
  ancestor.ancestors.any? { |node| node.respond_to?(:modifier_form?) && node.modifier_form? }
119
133
  end
120
134
 
121
- def without_parentheses_call_expr_follows?(ancestor)
122
- return false unless ancestor.respond_to?(:parenthesized?) && !ancestor.parenthesized?
135
+ def last_expression?(ancestor)
136
+ ancestor.right_sibling ||
137
+ ancestor.each_ancestor.find { |node| node.assignment? || node.send_type? }&.right_sibling
138
+ end
123
139
 
124
- right_sibling = ancestor.right_sibling
125
- right_sibling ||= ancestor.each_ancestor.find do |node|
126
- node.assignment? || node.send_type?
127
- end&.right_sibling
140
+ def method_dispatch_as_argument?(method_dispatch_node)
141
+ parent = method_dispatch_node.parent
142
+ return false unless parent
128
143
 
129
- !!right_sibling
144
+ parent.call_type? || parent.super_type? || parent.yield_type?
130
145
  end
131
146
 
132
147
  def breakdown_value_types_of_hash(hash_node)
@@ -182,6 +197,25 @@ module RuboCop
182
197
  register_offense(pair_node, OMIT_HASH_VALUE_MSG, replacement)
183
198
  end
184
199
  end
200
+
201
+ DefNode = Struct.new(:node) do
202
+ def selector
203
+ if node.loc.respond_to?(:selector)
204
+ node.loc.selector
205
+ else
206
+ node.loc.keyword
207
+ end
208
+ end
209
+
210
+ def first_argument
211
+ node.first_argument
212
+ end
213
+
214
+ def last_argument
215
+ node.last_argument
216
+ end
217
+ end
185
218
  end
186
219
  end
220
+ # rubocop:enable Metrics/ModuleLength
187
221
  end
@@ -4,6 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  # Help methods for determining if a line is too long.
6
6
  module LineLengthHelp
7
+ include Alignment
8
+
7
9
  private
8
10
 
9
11
  def ignore_cop_directives?
@@ -85,7 +87,7 @@ module RuboCop
85
87
 
86
88
  def tab_indentation_width
87
89
  config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
88
- config.for_cop('Layout/IndentationWidth')['Width']
90
+ configured_indentation_width
89
91
  end
90
92
 
91
93
  def uri_regexp
@@ -5,6 +5,7 @@ module RuboCop
5
5
  # Common functionality for modifier cops.
6
6
  module StatementModifier
7
7
  include LineLengthHelp
8
+ include RangeHelp
8
9
 
9
10
  private
10
11
 
@@ -107,9 +107,9 @@ module RuboCop
107
107
  end
108
108
  end
109
109
 
110
- def empty_brackets?(left_bracket_token, right_bracket_token)
111
- left_index = processed_source.tokens.index(left_bracket_token)
112
- right_index = processed_source.tokens.index(right_bracket_token)
110
+ def empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens)
111
+ left_index = tokens.index(left_bracket_token)
112
+ right_index = tokens.index(right_bracket_token)
113
113
  right_index && left_index == right_index - 1
114
114
  end
115
115
 
@@ -132,7 +132,7 @@ module RuboCop
132
132
 
133
133
  def avoid_comma(kind, comma_begin_pos, extra_info)
134
134
  range = range_between(comma_begin_pos, comma_begin_pos + 1)
135
- article = /array/.match?(kind) ? 'an' : 'a'
135
+ article = kind.include?('array') ? 'an' : 'a'
136
136
  msg = format(
137
137
  MSG,
138
138
  command: 'Avoid',
@@ -47,6 +47,10 @@ module RuboCop
47
47
 
48
48
  MSG = 'Use %<style>s block forwarding.'
49
49
 
50
+ def self.autocorrect_incompatible_with
51
+ [Lint::AmbiguousOperator]
52
+ end
53
+
50
54
  def on_def(node)
51
55
  return if node.arguments.empty?
52
56
 
@@ -34,7 +34,7 @@ module RuboCop
34
34
 
35
35
  allowed = /#{cop_config['AllowedNames'].join('|')}/
36
36
  name = node.loc.name.source.gsub(allowed, '')
37
- return unless /_/.match?(name)
37
+ return unless name.include?('_')
38
38
 
39
39
  add_offense(node.loc.name)
40
40
  end
@@ -41,6 +41,11 @@ module RuboCop
41
41
  @global = new
42
42
  end
43
43
 
44
+ def self.qualified_cop?(name)
45
+ badge = Badge.parse(name)
46
+ global.qualify_badge(badge).first == badge
47
+ end
48
+
44
49
  attr_reader :options
45
50
 
46
51
  def initialize(cops = [], options = {})
@@ -158,6 +163,13 @@ module RuboCop
158
163
  'RedundantCopDisableDirective'
159
164
  end
160
165
 
166
+ def qualify_badge(badge)
167
+ clear_enrollment_queue
168
+ @departments
169
+ .map { |department, _| badge.with_department(department) }
170
+ .select { |potential_badge| registered?(potential_badge) }
171
+ end
172
+
161
173
  # @return [Hash{String => Array<Class>}]
162
174
  def to_h
163
175
  clear_enrollment_queue
@@ -282,13 +294,6 @@ module RuboCop
282
294
  self.class.new(cops)
283
295
  end
284
296
 
285
- def qualify_badge(badge)
286
- clear_enrollment_queue
287
- @departments
288
- .map { |department, _| badge.with_department(department) }
289
- .select { |potential_badge| registered?(potential_badge) }
290
- end
291
-
292
297
  def resolve_badge(given_badge, real_badge, source_path)
293
298
  unless given_badge.match?(real_badge)
294
299
  path = PathUtil.smart_path(source_path)
@@ -115,9 +115,7 @@ module RuboCop
115
115
  def_node = find_corresponding_def_node(node)
116
116
  return unless def_node
117
117
 
118
- remove_node(corrector, def_node)
119
- remove_node(corrector, node)
120
- insert_def(corrector, node, def_node.source)
118
+ replace_def(corrector, node, def_node)
121
119
  when :inline
122
120
  remove_node(corrector, node)
123
121
  select_grouped_def_nodes(node).each do |grouped_def_node|
@@ -131,7 +129,8 @@ module RuboCop
131
129
  end
132
130
 
133
131
  def offense?(node)
134
- (group_style? && access_modifier_is_inlined?(node)) ||
132
+ (group_style? && access_modifier_is_inlined?(node) &&
133
+ !right_siblings_same_inline_method?(node)) ||
135
134
  (inline_style? && access_modifier_is_not_inlined?(node))
136
135
  end
137
136
 
@@ -151,6 +150,12 @@ module RuboCop
151
150
  !access_modifier_is_inlined?(node)
152
151
  end
153
152
 
153
+ def right_siblings_same_inline_method?(node)
154
+ node.right_siblings.any? do |sibling|
155
+ sibling.send_type? && sibling.method?(node.method_name) && !sibling.arguments.empty?
156
+ end
157
+ end
158
+
154
159
  def message(range)
155
160
  access_modifier = range.source
156
161
 
@@ -173,7 +178,9 @@ module RuboCop
173
178
  end
174
179
 
175
180
  def find_argument_less_modifier_node(node)
176
- node.parent.each_child_node(:send).find do |child|
181
+ return unless (parent = node.parent)
182
+
183
+ parent.each_child_node(:send).find do |child|
177
184
  child.method?(node.method_name) && child.arguments.empty?
178
185
  end
179
186
  end
@@ -184,17 +191,21 @@ module RuboCop
184
191
  end.select(&:def_type?)
185
192
  end
186
193
 
187
- def insert_def(corrector, node, source)
188
- source = [*processed_source.ast_with_comments[node].map(&:text), source].join("\n")
194
+ def replace_def(corrector, node, def_node)
195
+ source = def_source(node, def_node)
189
196
  argument_less_modifier_node = find_argument_less_modifier_node(node)
190
197
  if argument_less_modifier_node
191
198
  corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
199
+ elsif (ancestor = node.each_ancestor(:block, :class, :module).first)
200
+
201
+ corrector.insert_before(ancestor.loc.end, "#{node.method_name}\n\n#{source}\n")
192
202
  else
193
- corrector.insert_before(
194
- node.each_ancestor(:block, :class, :module).first.location.end,
195
- "#{node.method_name}\n\n#{source}\n"
196
- )
203
+ corrector.replace(node, "#{node.method_name}\n\n#{source}")
204
+ return
197
205
  end
206
+
207
+ remove_node(corrector, def_node)
208
+ remove_node(corrector, node)
198
209
  end
199
210
 
200
211
  def insert_inline_modifier(corrector, node, modifier_name)
@@ -204,6 +215,10 @@ module RuboCop
204
215
  def remove_node(corrector, node)
205
216
  corrector.remove(range_with_comments_and_lines(node))
206
217
  end
218
+
219
+ def def_source(node, def_node)
220
+ [*processed_source.ast_with_comments[node].map(&:text), def_node.source].join("\n")
221
+ end
207
222
  end
208
223
  end
209
224
  end
@@ -84,6 +84,7 @@ module RuboCop
84
84
  def on_def(node)
85
85
  return unless node.body
86
86
  return unless (rest_args_name, args = use_rest_arguments?(node.arguments))
87
+ return if args.any?(&:default?)
87
88
 
88
89
  node.each_descendant(:send) do |send_node|
89
90
  kwargs_name, block_name = extract_argument_names_from(args)
@@ -299,8 +299,8 @@ module RuboCop
299
299
 
300
300
  def move_comment_before_block(corrector, comment, block_node, closing_brace)
301
301
  range = block_node.chained? ? end_of_chain(block_node.parent).source_range : closing_brace
302
- comment_range = range_between(range.end_pos, comment.loc.expression.end_pos)
303
- corrector.remove(range_with_surrounding_space(comment_range, side: :right))
302
+ corrector.remove(range_with_surrounding_space(comment.loc.expression, side: :right))
303
+ remove_trailing_whitespace(corrector, range, comment)
304
304
  corrector.insert_after(range, "\n")
305
305
 
306
306
  corrector.insert_before(block_node, "#{comment.text}\n")
@@ -313,6 +313,12 @@ module RuboCop
313
313
  end_of_chain(node.parent)
314
314
  end
315
315
 
316
+ def remove_trailing_whitespace(corrector, range, comment)
317
+ range_of_trailing = range.end.join(comment.loc.expression.begin)
318
+
319
+ corrector.remove(range_of_trailing) if range_of_trailing.source.match?(/\A\s+\z/)
320
+ end
321
+
316
322
  def with_block?(node)
317
323
  node.respond_to?(:block_node) && node.block_node
318
324
  end
@@ -31,6 +31,7 @@ module RuboCop
31
31
  #
32
32
  # The compact style is only forced for classes/modules with one child.
33
33
  class ClassAndModuleChildren < Base
34
+ include Alignment
34
35
  include ConfigurableEnforcedStyle
35
36
  include RangeHelp
36
37
  extend AutoCorrector
@@ -59,7 +60,7 @@ module RuboCop
59
60
  end
60
61
 
61
62
  def nest_definition(corrector, node)
62
- padding = ((' ' * indent_width) + leading_spaces(node)).to_s
63
+ padding = indentation(node) + leading_spaces(node)
63
64
  padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')
64
65
 
65
66
  replace_namespace_keyword(corrector, node)
@@ -124,10 +125,6 @@ module RuboCop
124
125
  corrector.remove(range)
125
126
  end
126
127
 
127
- def configured_indentation_width
128
- config.for_badge(Layout::IndentationWidth.badge).fetch('Width', 2)
129
- end
130
-
131
128
  def unindent(corrector, node)
132
129
  return if node.body.children.last.nil?
133
130
 
@@ -141,10 +138,6 @@ module RuboCop
141
138
  node.source_range.source_line[/\A\s*/]
142
139
  end
143
140
 
144
- def indent_width
145
- @config.for_cop('Layout/IndentationWidth')['Width'] || 2
146
- end
147
-
148
141
  def check_style(node, body)
149
142
  return if node.identifier.children[0]&.cbase_type?
150
143
 
@@ -185,7 +178,7 @@ module RuboCop
185
178
  end
186
179
 
187
180
  def compact_node_name?(node)
188
- /::/.match?(node.identifier.source)
181
+ node.identifier.source.include?('::')
189
182
  end
190
183
  end
191
184
  end
@@ -148,7 +148,7 @@ module RuboCop
148
148
  end
149
149
 
150
150
  def contains_backtick?(node)
151
- /`/.match?(node_body(node))
151
+ node_body(node).include?('`')
152
152
  end
153
153
 
154
154
  def node_body(node)
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Enforces the use of `Comparable#clamp` instead of comparison by minimum and maximum.
7
+ #
8
+ # This cop supports autocorrection for `if/elsif/else` bad style only.
9
+ # Because `ArgumentError` occurs if the minimum and maximum of `clamp` arguments are reversed.
10
+ # When these are variables, it is not possible to determine which is the minimum and maximum:
11
+ #
12
+ # [source,ruby]
13
+ # ----
14
+ # [1, [2, 3].max].min # => 1
15
+ # 1.clamp(3, 1) # => min argument must be smaller than max argument (ArgumentError)
16
+ # ----
17
+ #
18
+ # @example
19
+ #
20
+ # # bad
21
+ # [[x, low].max, high].min
22
+ #
23
+ # # bad
24
+ # if x < low
25
+ # low
26
+ # elsif high < x
27
+ # high
28
+ # else
29
+ # x
30
+ # end
31
+ #
32
+ # # good
33
+ # x.clamp(low, high)
34
+ #
35
+ class ComparableClamp < Base
36
+ include Alignment
37
+ extend AutoCorrector
38
+ extend TargetRubyVersion
39
+
40
+ minimum_target_ruby_version 2.4
41
+
42
+ MSG = 'Use `%<prefer>s` instead of `if/elsif/else`.'
43
+ MSG_MIN_MAX = 'Use `Comparable#clamp` instead.'
44
+ RESTRICT_ON_SEND = %i[min max].freeze
45
+
46
+ # @!method if_elsif_else_condition?(node)
47
+ def_node_matcher :if_elsif_else_condition?, <<~PATTERN
48
+ {
49
+ (if (send _x :< _min) _min (if (send _max :< _x) _max _x))
50
+ (if (send _min :> _x) _min (if (send _max :< _x) _max _x))
51
+ (if (send _x :< _min) _min (if (send _x :> _max) _max _x))
52
+ (if (send _min :> _x) _min (if (send _x :> _max) _max _x))
53
+ (if (send _max :< _x) _max (if (send _x :< _min) _min _x))
54
+ (if (send _x :> _max) _max (if (send _x :< _min) _min _x))
55
+ (if (send _max :< _x) _max (if (send _min :> _x) _min _x))
56
+ (if (send _x :> _max) _max (if (send _min :> _x) _min _x))
57
+ }
58
+ PATTERN
59
+
60
+ # @!method array_min_max?(node)
61
+ def_node_matcher :array_min_max?, <<~PATTERN
62
+ {
63
+ (send
64
+ (array
65
+ (send (array _ _) :max) _) :min)
66
+ (send
67
+ (array
68
+ _ (send (array _ _) :max)) :min)
69
+ (send
70
+ (array
71
+ (send (array _ _) :min) _) :max)
72
+ (send
73
+ (array
74
+ _ (send (array _ _) :min)) :max)
75
+ }
76
+ PATTERN
77
+
78
+ def on_if(node)
79
+ return unless if_elsif_else_condition?(node)
80
+
81
+ if_body, elsif_body, else_body = *node.branches
82
+
83
+ else_body_source = else_body.source
84
+
85
+ if min_condition?(node.condition, else_body_source)
86
+ min = if_body.source
87
+ max = elsif_body.source
88
+ else
89
+ min = elsif_body.source
90
+ max = if_body.source
91
+ end
92
+
93
+ prefer = "#{else_body_source}.clamp(#{min}, #{max})"
94
+
95
+ add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
96
+ autocorrect(corrector, node, prefer)
97
+ end
98
+ end
99
+
100
+ def on_send(node)
101
+ return unless array_min_max?(node)
102
+
103
+ add_offense(node, message: MSG_MIN_MAX)
104
+ end
105
+
106
+ private
107
+
108
+ def autocorrect(corrector, node, prefer)
109
+ if node.elsif?
110
+ corrector.insert_before(node, "else\n")
111
+ corrector.replace(node, "#{indentation(node)}#{prefer}")
112
+ else
113
+ corrector.replace(node, prefer)
114
+ end
115
+ end
116
+
117
+ def min_condition?(if_condition, else_body)
118
+ lhs, op, rhs = *if_condition
119
+
120
+ (lhs.source == else_body && op == :<) || (rhs.source == else_body && op == :>)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end