rubocop 1.40.0 → 1.41.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +18 -0
  4. data/lib/rubocop/config.rb +28 -5
  5. data/lib/rubocop/config_loader.rb +9 -0
  6. data/lib/rubocop/config_validator.rb +1 -1
  7. data/lib/rubocop/cop/badge.rb +9 -4
  8. data/lib/rubocop/cop/base.rb +25 -9
  9. data/lib/rubocop/cop/commissioner.rb +8 -3
  10. data/lib/rubocop/cop/cop.rb +1 -1
  11. data/lib/rubocop/cop/internal_affairs/cop_description.rb +3 -1
  12. data/lib/rubocop/cop/layout/empty_lines.rb +2 -0
  13. data/lib/rubocop/cop/layout/extra_spacing.rb +10 -6
  14. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +38 -2
  15. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +49 -2
  16. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +61 -2
  17. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +52 -2
  18. data/lib/rubocop/cop/layout/indentation_style.rb +3 -1
  19. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +5 -0
  20. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +7 -1
  21. data/lib/rubocop/cop/layout/line_length.rb +2 -0
  22. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +51 -2
  23. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +49 -2
  24. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +53 -2
  25. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +58 -2
  26. data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -2
  27. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
  28. data/lib/rubocop/cop/layout/trailing_whitespace.rb +6 -2
  29. data/lib/rubocop/cop/lint/constant_resolution.rb +4 -0
  30. data/lib/rubocop/cop/lint/debugger.rb +3 -1
  31. data/lib/rubocop/cop/lint/duplicate_branch.rb +0 -2
  32. data/lib/rubocop/cop/lint/duplicate_methods.rb +19 -8
  33. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -5
  34. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  35. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -19
  36. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  37. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  38. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  39. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  40. data/lib/rubocop/cop/mixin/allowed_identifiers.rb +2 -2
  41. data/lib/rubocop/cop/mixin/annotation_comment.rb +13 -6
  42. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
  43. data/lib/rubocop/cop/mixin/first_element_line_break.rb +11 -7
  44. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +5 -1
  45. data/lib/rubocop/cop/mixin/line_length_help.rb +8 -1
  46. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -3
  47. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -3
  48. data/lib/rubocop/cop/mixin/percent_array.rb +3 -5
  49. data/lib/rubocop/cop/mixin/require_library.rb +2 -0
  50. data/lib/rubocop/cop/mixin/rescue_node.rb +3 -3
  51. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  52. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +2 -0
  53. data/lib/rubocop/cop/naming/inclusive_language.rb +4 -1
  54. data/lib/rubocop/cop/registry.rb +6 -3
  55. data/lib/rubocop/cop/style/concat_array_literals.rb +66 -0
  56. data/lib/rubocop/cop/style/documentation.rb +1 -1
  57. data/lib/rubocop/cop/style/guard_clause.rb +5 -1
  58. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -3
  59. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  60. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -1
  61. data/lib/rubocop/cop/style/redundant_constant_base.rb +13 -0
  62. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +39 -0
  63. data/lib/rubocop/cop/style/require_order.rb +61 -9
  64. data/lib/rubocop/cop/style/semicolon.rb +2 -1
  65. data/lib/rubocop/cop/util.rb +31 -4
  66. data/lib/rubocop/cops_documentation_generator.rb +22 -3
  67. data/lib/rubocop/directive_comment.rb +1 -1
  68. data/lib/rubocop/file_patterns.rb +43 -0
  69. data/lib/rubocop/options.rb +1 -1
  70. data/lib/rubocop/path_util.rb +20 -14
  71. data/lib/rubocop/target_finder.rb +1 -1
  72. data/lib/rubocop/version.rb +1 -1
  73. data/lib/rubocop.rb +3 -1
  74. metadata +6 -4
  75. data/lib/rubocop/optimized_patterns.rb +0 -38
@@ -45,13 +45,12 @@ module RuboCop
45
45
  bad_method?(node) do |safe_nav, method|
46
46
  return if nil_methods.include?(method) || PLUS_MINUS_METHODS.include?(node.method_name)
47
47
 
48
- method_chain = method_chain(node)
49
48
  location =
50
49
  Parser::Source::Range.new(node.source_range.source_buffer,
51
50
  safe_nav.source_range.end_pos,
52
- method_chain.source_range.end_pos)
51
+ node.source_range.end_pos)
53
52
  add_offense(location) do |corrector|
54
- autocorrect(corrector, offense_range: location, send_node: method_chain)
53
+ autocorrect(corrector, offense_range: location, send_node: node)
55
54
  end
56
55
  end
57
56
  end
@@ -63,12 +62,12 @@ module RuboCop
63
62
  # @return [String]
64
63
  def add_safe_navigation_operator(offense_range:, send_node:)
65
64
  source =
66
- if (brackets = find_brackets(send_node))
65
+ if brackets?(send_node)
67
66
  format(
68
67
  '%<method_name>s(%<arguments>s)%<method_chain>s',
69
- arguments: brackets.arguments.map(&:source).join(', '),
70
- method_name: brackets.method_name,
71
- method_chain: brackets.source_range.end.join(send_node.source_range.end).source
68
+ arguments: send_node.arguments.map(&:source).join(', '),
69
+ method_name: send_node.method_name,
70
+ method_chain: send_node.source_range.end.join(send_node.source_range.end).source
72
71
  )
73
72
  else
74
73
  offense_range.source
@@ -90,18 +89,8 @@ module RuboCop
90
89
  )
91
90
  end
92
91
 
93
- def method_chain(node)
94
- chain = node
95
- chain = chain.parent if chain.send_type? && chain.parent&.call_type?
96
- chain
97
- end
98
-
99
- def find_brackets(send_node)
100
- return send_node if send_node.method?(:[]) || send_node.method?(:[]=)
101
-
102
- send_node.descendants.detect do |node|
103
- node.send_type? && (node.method?(:[]) || node.method?(:[]=))
104
- end
92
+ def brackets?(send_node)
93
+ send_node.method?(:[]) || send_node.method?(:[]=)
105
94
  end
106
95
  end
107
96
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
- # Checks if the length a class exceeds some maximum value.
6
+ # Checks if the length of a class exceeds some maximum value.
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
- # Checks if the length a module exceeds some maximum value.
6
+ # Checks if the length of a module exceeds some maximum value.
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
9
  #
@@ -58,8 +58,8 @@ module RuboCop
58
58
  end
59
59
 
60
60
  def normalize_foldable_types(types)
61
- types.concat(%i[str dstr]) if types.delete(:heredoc)
62
- types.concat(%i[send csend]) if types.delete(:method_call)
61
+ types.push(:str, :dstr) if types.delete(:heredoc)
62
+ types.push(:send, :csend) if types.delete(:method_call)
63
63
  types
64
64
  end
65
65
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  each_bad_alignment(items, base_column) do |current|
30
30
  expr = current.source_range
31
- if @current_offenses.any? { |o| within?(expr, o.location) }
31
+ if @current_offenses&.any? { |o| within?(expr, o.location) }
32
32
  # If this offense is within a line range that is already being
33
33
  # realigned by autocorrect, we report the offense without
34
34
  # autocorrecting it. Two rewrites in the same area by the same
@@ -7,11 +7,11 @@ module RuboCop
7
7
  SIGILS = '@$' # if a variable starts with a sigil it will be removed
8
8
 
9
9
  def allowed_identifier?(name)
10
- allowed_identifiers.include?(name.to_s.delete(SIGILS))
10
+ !allowed_identifiers.empty? && allowed_identifiers.include?(name.to_s.delete(SIGILS))
11
11
  end
12
12
 
13
13
  def allowed_identifiers
14
- cop_config.fetch('AllowedIdentifiers', [])
14
+ cop_config.fetch('AllowedIdentifiers') { [] }
15
15
  end
16
16
  end
17
17
  end
@@ -41,18 +41,25 @@ module RuboCop
41
41
  def split_comment(comment)
42
42
  # Sort keywords by reverse length so that if a keyword is in a phrase
43
43
  # but also on its own, both will match properly.
44
- keywords_regex = Regexp.new(
45
- Regexp.union(keywords.sort_by { |w| -w.length }).source,
46
- Regexp::IGNORECASE
47
- )
48
- regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
49
-
50
44
  match = comment.text.match(regex)
51
45
  return false unless match
52
46
 
53
47
  match.captures
54
48
  end
55
49
 
50
+ KEYWORDS_REGEX_CACHE = {} # rubocop:disable Layout/ClassStructure, Style/MutableConstant
51
+ private_constant :KEYWORDS_REGEX_CACHE
52
+
53
+ def regex
54
+ KEYWORDS_REGEX_CACHE[keywords] ||= begin
55
+ keywords_regex = Regexp.new(
56
+ Regexp.union(keywords.sort_by { |w| -w.length }).source,
57
+ Regexp::IGNORECASE
58
+ )
59
+ /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
60
+ end
61
+ end
62
+
56
63
  def keyword_appearance?
57
64
  keyword && (colon || space)
58
65
  end
@@ -20,19 +20,30 @@ module RuboCop
20
20
  style_detected(possibilities)
21
21
  end
22
22
 
23
+ SYMBOL_TO_STRING_CACHE = Hash.new do |hash, key|
24
+ hash[key] = key.to_s if key.is_a?(Symbol)
25
+ end
26
+ private_constant :SYMBOL_TO_STRING_CACHE
27
+
28
+ # rubocop:disable Metrics
23
29
  def style_detected(detected)
24
30
  return if no_acceptable_style?
25
31
 
26
- # `detected` can be a single style or an Array of possible styles
27
- # (if there is more than one which matches the observed code)
28
- detected_as_strings = Array(detected).map(&:to_s)
32
+ # This logic is more complex than it needs to be
33
+ # to avoid allocating Arrays in the hot code path.
34
+ updated_list =
35
+ if detected_style
36
+ if detected_style.size == 1 && detected_style.include?(SYMBOL_TO_STRING_CACHE[detected])
37
+ detected_style
38
+ else
39
+ detected_as_strings = SYMBOL_TO_STRING_CACHE.values_at(*detected)
40
+ detected_style & detected_as_strings
41
+ end
42
+ else
43
+ # We haven't observed any specific style yet.
44
+ SYMBOL_TO_STRING_CACHE.values_at(*detected)
45
+ end
29
46
 
30
- updated_list = if detected_style
31
- detected_style & detected_as_strings
32
- else
33
- # We haven't observed any specific style yet.
34
- detected_as_strings
35
- end
36
47
  if updated_list.empty?
37
48
  no_acceptable_style!
38
49
  else
@@ -40,6 +51,7 @@ module RuboCop
40
51
  config_to_allow_offenses[style_parameter_name] = updated_list.first
41
52
  end
42
53
  end
54
+ # rubocop:enable Metrics
43
55
 
44
56
  def no_acceptable_style?
45
57
  config_to_allow_offenses['Enabled'] == false
@@ -7,12 +7,12 @@ module RuboCop
7
7
  module FirstElementLineBreak
8
8
  private
9
9
 
10
- def check_method_line_break(node, children)
10
+ def check_method_line_break(node, children, ignore_last: false)
11
11
  return if children.empty?
12
12
 
13
13
  return unless method_uses_parens?(node, children.first)
14
14
 
15
- check_children_line_break(node, children)
15
+ check_children_line_break(node, children, ignore_last: ignore_last)
16
16
  end
17
17
 
18
18
  def method_uses_parens?(node, limit)
@@ -20,7 +20,7 @@ module RuboCop
20
20
  /\s*\(\s*$/.match?(source)
21
21
  end
22
22
 
23
- def check_children_line_break(node, children, start = node)
23
+ def check_children_line_break(node, children, start = node, ignore_last: false)
24
24
  return if children.empty?
25
25
 
26
26
  line = start.first_line
@@ -28,8 +28,8 @@ module RuboCop
28
28
  min = first_by_line(children)
29
29
  return if line != min.first_line
30
30
 
31
- max = last_by_line(children)
32
- return if line == max.last_line
31
+ max_line = last_line(children, ignore_last: ignore_last)
32
+ return if line == max_line
33
33
 
34
34
  add_offense(min) { |corrector| EmptyLineCorrector.insert_before(corrector, min) }
35
35
  end
@@ -38,8 +38,12 @@ module RuboCop
38
38
  nodes.min_by(&:first_line)
39
39
  end
40
40
 
41
- def last_by_line(nodes)
42
- nodes.max_by(&:last_line)
41
+ def last_line(nodes, ignore_last:)
42
+ if ignore_last
43
+ nodes.map(&:first_line)
44
+ else
45
+ nodes.map(&:last_line)
46
+ end.max
43
47
  end
44
48
  end
45
49
  end
@@ -45,17 +45,21 @@ module RuboCop
45
45
 
46
46
  private
47
47
 
48
+ # rubocop:disable Metrics/AbcSize
48
49
  def register_offense(node, message, replacement)
49
50
  add_offense(node.value, message: message) do |corrector|
50
51
  if (def_node = def_node_that_require_parentheses(node))
51
52
  white_spaces = range_between(def_node.loc.selector.end_pos,
52
53
  def_node.first_argument.source_range.begin_pos)
53
54
  corrector.replace(white_spaces, '(')
54
- corrector.insert_after(def_node.arguments.last, ')')
55
+
56
+ last_argument = def_node.arguments.last
57
+ corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
55
58
  end
56
59
  corrector.replace(node, replacement)
57
60
  end
58
61
  end
62
+ # rubocop:enable Metrics/AbcSize
59
63
 
60
64
  def ignore_mixed_hash_shorthand_syntax?(hash_node)
61
65
  target_ruby_version <= 3.0 || enforced_shorthand_syntax != 'consistent' ||
@@ -57,7 +57,14 @@ module RuboCop
57
57
  def indentation_difference(line)
58
58
  return 0 unless tab_indentation_width
59
59
 
60
- (line.index(/[^\t]/) || 0) * (tab_indentation_width - 1)
60
+ index =
61
+ if line.match?(/^[^\t]/)
62
+ 0
63
+ else
64
+ line.index(/[^\t]/) || 0
65
+ end
66
+
67
+ index * (tab_indentation_width - 1)
61
68
  end
62
69
 
63
70
  def extend_uri_end_position(line, end_position)
@@ -59,13 +59,15 @@ module RuboCop
59
59
  end
60
60
 
61
61
  def complexity(body)
62
- body.each_node(:lvasgn, *self.class::COUNTED_NODES).reduce(1) do |score, node|
62
+ score = 1
63
+ body.each_node(:lvasgn, *self.class::COUNTED_NODES) do |node|
63
64
  if node.lvasgn_type?
64
65
  reset_on_lvasgn(node)
65
- next score
66
+ else
67
+ score += complexity_score_for(node)
66
68
  end
67
- score + complexity_score_for(node)
68
69
  end
70
+ score
69
71
  end
70
72
  end
71
73
  end
@@ -10,8 +10,8 @@ module RuboCop
10
10
  module MultilineElementLineBreaks
11
11
  private
12
12
 
13
- def check_line_breaks(_node, children)
14
- return if all_on_same_line?(children)
13
+ def check_line_breaks(_node, children, ignore_last: false)
14
+ return if all_on_same_line?(children, ignore_last: ignore_last)
15
15
 
16
16
  last_seen_line = -1
17
17
  children.each do |child|
@@ -23,9 +23,11 @@ module RuboCop
23
23
  end
24
24
  end
25
25
 
26
- def all_on_same_line?(nodes)
26
+ def all_on_same_line?(nodes, ignore_last: false)
27
27
  return true if nodes.empty?
28
28
 
29
+ return same_line?(nodes.first, nodes.last) if ignore_last
30
+
29
31
  nodes.first.first_line == nodes.last.last_line
30
32
  end
31
33
  end
@@ -97,9 +97,7 @@ module RuboCop
97
97
  # @return [String]
98
98
  def whitespace_between(node)
99
99
  if node.children.length >= 2
100
- node.source[
101
- node.children[0].loc.expression.end_pos...node.children[1].loc.expression.begin_pos
102
- ]
100
+ node.children[0].source_range.end.join(node.children[1].source_range.begin).source
103
101
  else
104
102
  ' '
105
103
  end
@@ -111,7 +109,7 @@ module RuboCop
111
109
  # @param [RuboCop::AST::ArrayNode] node
112
110
  # @return [String]
113
111
  def whitespace_leading(node)
114
- node.source[node.loc.begin.end_pos...node.children[0].loc.expression.begin_pos]
112
+ node.loc.begin.end.join(node.children[0].source_range.begin).source
115
113
  end
116
114
 
117
115
  # Provides trailing whitespace for building a bracketed array.
@@ -120,7 +118,7 @@ module RuboCop
120
118
  # @param [RuboCop::AST::ArrayNode] node
121
119
  # @return [String]
122
120
  def whitespace_trailing(node)
123
- node.source[node.children[-1].loc.expression.end_pos...node.loc.end.begin_pos]
121
+ node.children[-1].source_range.end.join(node.loc.end.begin).source
124
122
  end
125
123
  end
126
124
  end
@@ -7,6 +7,8 @@ module RuboCop
7
7
  module RequireLibrary
8
8
  extend NodePattern::Macros
9
9
 
10
+ RESTRICT_ON_SEND = [:require].freeze
11
+
10
12
  def ensure_required(corrector, node, library_name)
11
13
  node = node.parent while node.parent&.parent?
12
14
 
@@ -4,8 +4,8 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for checking `rescue` nodes.
6
6
  module RescueNode
7
- def on_new_investigation
8
- @modifier_locations = processed_source.tokens.select(&:rescue_modifier?).map(&:pos)
7
+ def modifier_locations
8
+ @modifier_locations ||= processed_source.tokens.select(&:rescue_modifier?).map!(&:pos)
9
9
  end
10
10
 
11
11
  private
@@ -13,7 +13,7 @@ module RuboCop
13
13
  def rescue_modifier?(node)
14
14
  return false unless node.respond_to?(:resbody_type?)
15
15
 
16
- node.resbody_type? && @modifier_locations.include?(node.loc.keyword)
16
+ node.resbody_type? && modifier_locations.include?(node.loc.keyword)
17
17
  end
18
18
 
19
19
  # @deprecated Use ResbodyNode#exceptions instead
@@ -56,7 +56,7 @@ module RuboCop
56
56
 
57
57
  def if_body_source(if_body)
58
58
  if if_body.call_type? &&
59
- if_body.last_argument&.hash_type? && if_body.last_argument.pairs.last.value_omission?
59
+ if_body.last_argument&.hash_type? && if_body.last_argument.pairs.last&.value_omission?
60
60
  "#{method_source(if_body)}(#{if_body.arguments.map(&:source).join(', ')})"
61
61
  else
62
62
  if_body.source
@@ -30,6 +30,8 @@ module RuboCop
30
30
  MSG = 'Use CamelCase for classes and modules.'
31
31
 
32
32
  def on_class(node)
33
+ return unless node.loc.name.source.include?('_')
34
+
33
35
  allowed = /#{cop_config['AllowedNames'].join('|')}/
34
36
  name = node.loc.name.source.gsub(allowed, '')
35
37
  return unless /_/.match?(name)
@@ -207,7 +207,10 @@ module RuboCop
207
207
  end
208
208
 
209
209
  def scan_for_words(input)
210
- mask_input(input).enum_for(:scan, @flagged_terms_regex).map do
210
+ masked_input = mask_input(input)
211
+ return EMPTY_ARRAY unless masked_input.match?(@flagged_terms_regex)
212
+
213
+ masked_input.enum_for(:scan, @flagged_terms_regex).map do
211
214
  match = Regexp.last_match
212
215
  WordLocation.new(match.to_s, match.offset(0).first)
213
216
  end
@@ -28,6 +28,9 @@ module RuboCop
28
28
 
29
29
  @enrollment_queue = cops
30
30
  @options = options
31
+
32
+ @enabled_cache = {}.compare_by_identity
33
+ @disabled_cache = {}.compare_by_identity
31
34
  end
32
35
 
33
36
  def enlist(cop)
@@ -61,7 +64,7 @@ module RuboCop
61
64
 
62
65
  # @return [Boolean] Checks if given name is department
63
66
  def department?(name)
64
- departments.include? name.to_sym
67
+ departments.include?(name.to_sym)
65
68
  end
66
69
 
67
70
  def contains_cop_matching?(names)
@@ -150,11 +153,11 @@ module RuboCop
150
153
  end
151
154
 
152
155
  def enabled(config)
153
- select { |cop| enabled?(cop, config) }
156
+ @enabled_cache[config] ||= select { |cop| enabled?(cop, config) }
154
157
  end
155
158
 
156
159
  def disabled(config)
157
- reject { |cop| enabled?(cop, config) }
160
+ @disabled_cache[config] ||= reject { |cop| enabled?(cop, config) }
158
161
  end
159
162
 
160
163
  def enabled?(cop, config)
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Enforces the use of `Array#push(item)` instead of `Array#concat([item])`
7
+ # to avoid redundant array literals.
8
+ #
9
+ # @safety
10
+ # This cop is unsafe, as it can produce false positives if the receiver
11
+ # is not an `Array` object.
12
+ #
13
+ # @example
14
+ #
15
+ # # bad
16
+ # list.concat([foo])
17
+ # list.concat([bar, baz])
18
+ # list.concat([qux, quux], [corge])
19
+ #
20
+ # # good
21
+ # list.push(foo)
22
+ # list.push(bar, baz)
23
+ # list.push(qux, quux, corge)
24
+ #
25
+ class ConcatArrayLiterals < Base
26
+ extend AutoCorrector
27
+
28
+ MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
29
+ MSG_FOR_PERCENT_LITERALS =
30
+ 'Use `push` with elements as arguments without array brackets instead of `%<current>s`.'
31
+ RESTRICT_ON_SEND = %i[concat].freeze
32
+
33
+ def on_send(node)
34
+ return if node.arguments.empty?
35
+ return unless node.arguments.all?(&:array_type?)
36
+
37
+ offense = offense_range(node)
38
+ current = offense.source
39
+
40
+ if node.arguments.any?(&:percent_literal?)
41
+ message = format(MSG_FOR_PERCENT_LITERALS, current: current)
42
+ else
43
+ prefer = preferred_method(node)
44
+ message = format(MSG, prefer: prefer, current: current)
45
+ end
46
+
47
+ add_offense(offense, message: message) do |corrector|
48
+ corrector.replace(offense, prefer)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def offense_range(node)
55
+ node.loc.selector.join(node.source_range.end)
56
+ end
57
+
58
+ def preferred_method(node)
59
+ new_arguments = node.arguments.map { |arg| arg.children.map(&:source) }.join(', ')
60
+
61
+ "push(#{new_arguments})"
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -176,7 +176,7 @@ module RuboCop
176
176
  end
177
177
 
178
178
  def qualify_const(node)
179
- return if node.nil? || node.cbase_type?
179
+ return if node.nil? || node.cbase_type? || node.self_type? || node.send_type?
180
180
 
181
181
  [qualify_const(node.namespace), node.short_name].compact
182
182
  end
@@ -187,7 +187,7 @@ module RuboCop
187
187
  if_branch = node.if_branch
188
188
  else_branch = node.else_branch
189
189
 
190
- if if_branch&.send_type? && if_branch.last_argument&.heredoc?
190
+ if if_branch&.send_type? && heredoc?(if_branch.last_argument)
191
191
  autocorrect_heredoc_argument(corrector, node, if_branch, else_branch, guard)
192
192
  elsif else_branch&.send_type? && else_branch.last_argument&.heredoc?
193
193
  autocorrect_heredoc_argument(corrector, node, else_branch, if_branch, guard)
@@ -201,6 +201,10 @@ module RuboCop
201
201
  end
202
202
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
203
203
 
204
+ def heredoc?(argument)
205
+ argument.respond_to?(:heredoc?) && argument.heredoc?
206
+ end
207
+
204
208
  def autocorrect_heredoc_argument(corrector, node, heredoc_branch, leave_branch, guard)
205
209
  remove_whole_lines(corrector, leave_branch.source_range)
206
210
  remove_whole_lines(corrector, node.loc.else)
@@ -21,13 +21,12 @@ module RuboCop
21
21
  MSG_TERNARY = 'Do not use `if %<expr>s;` - use a ternary operator instead.'
22
22
 
23
23
  def on_normal_if_unless(node)
24
- return unless node.else_branch
25
24
  return if node.parent&.if_type?
26
25
 
27
26
  beginning = node.loc.begin
28
27
  return unless beginning&.is?(';')
29
28
 
30
- message = node.else_branch.if_type? ? MSG_IF_ELSE : MSG_TERNARY
29
+ message = node.else_branch&.if_type? ? MSG_IF_ELSE : MSG_TERNARY
31
30
 
32
31
  add_offense(node, message: format(message, expr: node.condition.source)) do |corrector|
33
32
  corrector.replace(node, autocorrect(node))
@@ -37,7 +36,7 @@ module RuboCop
37
36
  private
38
37
 
39
38
  def autocorrect(node)
40
- return correct_elsif(node) if node.else_branch.if_type?
39
+ return correct_elsif(node) if node.else_branch&.if_type?
41
40
 
42
41
  then_code = node.if_branch ? node.if_branch.source : 'nil'
43
42
  else_code = node.else_branch ? node.else_branch.source : 'nil'
@@ -51,6 +51,8 @@ module RuboCop
51
51
  NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
52
52
  CAMEL_CASE = /[A-Z]+[a-z]+/.freeze
53
53
 
54
+ RESTRICT_ON_SEND = [:!].freeze
55
+
54
56
  def self.autocorrect_incompatible_with
55
57
  [Style::Not, Style::SymbolProc]
56
58
  end
@@ -55,7 +55,10 @@ module RuboCop
55
55
  private
56
56
 
57
57
  def check_token_set(index)
58
- predecessor, operator, successor = processed_source.tokens[index, 3]
58
+ tokens = processed_source.tokens
59
+ predecessor = tokens[index]
60
+ operator = tokens[index + 1]
61
+ successor = tokens[index + 2]
59
62
 
60
63
  return unless eligible_token_set?(predecessor, operator, successor)
61
64
 
@@ -10,6 +10,10 @@ module RuboCop
10
10
  # is empty, there is no need to prepend `::`, so it would be nice to consistently
11
11
  # avoid such meaningless `::` prefix to avoid confusion.
12
12
  #
13
+ # NOTE: This cop is disabled if `Lint/ConstantResolution` cop is enabled to prevent
14
+ # conflicting rules. Because it respects user configurations that want to enable
15
+ # `Lint/ConstantResolution` cop which is disabled by default.
16
+ #
13
17
  # @example
14
18
  # # bad
15
19
  # ::Const
@@ -42,6 +46,7 @@ module RuboCop
42
46
  MSG = 'Remove redundant `::`.'
43
47
 
44
48
  def on_cbase(node)
49
+ return if lint_constant_resolution_cop_enabled?
45
50
  return unless bad?(node)
46
51
 
47
52
  add_offense(node) do |corrector|
@@ -51,6 +56,14 @@ module RuboCop
51
56
 
52
57
  private
53
58
 
59
+ def lint_constant_resolution_cop_enabled?
60
+ lint_constant_resolution_config.fetch('Enabled', false)
61
+ end
62
+
63
+ def lint_constant_resolution_config
64
+ config.for_cop('Lint/ConstantResolution')
65
+ end
66
+
54
67
  def bad?(node)
55
68
  module_nesting_ancestors_of(node).none?
56
69
  end