rubocop 1.48.1 → 1.50.0

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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +19 -7
  4. data/lib/rubocop/cli/command/execute_runner.rb +7 -2
  5. data/lib/rubocop/cli.rb +6 -6
  6. data/lib/rubocop/config.rb +1 -1
  7. data/lib/rubocop/config_loader.rb +8 -8
  8. data/lib/rubocop/cop/autocorrect_logic.rb +28 -12
  9. data/lib/rubocop/cop/cop.rb +2 -2
  10. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  11. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  12. data/lib/rubocop/cop/internal_affairs/cop_description.rb +1 -1
  13. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
  14. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
  16. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  17. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  18. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  19. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  20. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  21. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
  23. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  24. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  25. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  26. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  27. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  28. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
  29. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +102 -0
  30. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  31. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  32. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  33. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
  34. data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
  35. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  36. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  37. data/lib/rubocop/cop/lint/void.rb +7 -3
  38. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  39. data/lib/rubocop/cop/metrics/class_length.rb +1 -0
  40. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  41. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  42. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  43. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  44. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  45. data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
  46. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  47. data/lib/rubocop/cop/style/class_equality_comparison.rb +42 -9
  48. data/lib/rubocop/cop/style/copyright.rb +1 -1
  49. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  50. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  51. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  52. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  53. data/lib/rubocop/cop/style/file_empty.rb +3 -3
  54. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  55. data/lib/rubocop/cop/style/hash_except.rb +4 -4
  56. data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
  57. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -12
  58. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  59. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  60. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -7
  61. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
  62. data/lib/rubocop/cop/style/multiline_method_signature.rb +6 -3
  63. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  64. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  65. data/lib/rubocop/cop/style/redundant_line_continuation.rb +167 -0
  66. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  67. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  68. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  69. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  70. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
  71. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  72. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  73. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
  74. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  75. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  76. data/lib/rubocop/cops_documentation_generator.rb +10 -3
  77. data/lib/rubocop/ext/regexp_node.rb +1 -1
  78. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  79. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  80. data/lib/rubocop/options.rb +4 -1
  81. data/lib/rubocop/result_cache.rb +1 -1
  82. data/lib/rubocop/server/cache.rb +1 -1
  83. data/lib/rubocop/server/helper.rb +1 -1
  84. data/lib/rubocop/server/server_command/exec.rb +1 -1
  85. data/lib/rubocop/version.rb +1 -1
  86. data/lib/rubocop.rb +3 -0
  87. metadata +9 -6
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ # Checks that there are no repeated patterns used in `in` keywords.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # case x
12
+ # in 'first'
13
+ # do_something
14
+ # in 'first'
15
+ # do_something_else
16
+ # end
17
+ #
18
+ # # good
19
+ # case x
20
+ # in 'first'
21
+ # do_something
22
+ # in 'second'
23
+ # do_something_else
24
+ # end
25
+ #
26
+ # # bad - repeated alternate patterns with the same conditions don't depend on the order
27
+ # case x
28
+ # in foo | bar
29
+ # first_method
30
+ # in bar | foo
31
+ # second_method
32
+ # end
33
+ #
34
+ # # good
35
+ # case x
36
+ # in foo | bar
37
+ # first_method
38
+ # in bar | baz
39
+ # second_method
40
+ # end
41
+ #
42
+ # # bad - repeated hash patterns with the same conditions don't depend on the order
43
+ # case x
44
+ # in foo: a, bar: b
45
+ # first_method
46
+ # in bar: b, foo: a
47
+ # second_method
48
+ # end
49
+ #
50
+ # # good
51
+ # case x
52
+ # in foo: a, bar: b
53
+ # first_method
54
+ # in bar: b, baz: c
55
+ # second_method
56
+ # end
57
+ #
58
+ # # bad - repeated array patterns with elements in the same order
59
+ # case x
60
+ # in [foo, bar]
61
+ # first_method
62
+ # in [foo, bar]
63
+ # second_method
64
+ # end
65
+ #
66
+ # # good
67
+ # case x
68
+ # in [foo, bar]
69
+ # first_method
70
+ # in [bar, foo]
71
+ # second_method
72
+ # end
73
+ #
74
+ class DuplicateMatchPattern < Base
75
+ extend TargetRubyVersion
76
+
77
+ MSG = 'Duplicate `in` pattern detected.'
78
+
79
+ minimum_target_ruby_version 2.7
80
+
81
+ def on_case_match(case_node)
82
+ case_node.in_pattern_branches.each_with_object(Set.new) do |in_pattern_node, previous|
83
+ pattern = in_pattern_node.pattern
84
+ next if previous.add?(pattern_identity(pattern))
85
+
86
+ add_offense(pattern)
87
+ end
88
+ end
89
+
90
+ private
91
+
92
+ def pattern_identity(pattern)
93
+ if pattern.hash_pattern_type? || pattern.match_alt_type?
94
+ pattern.children.map(&:source).sort
95
+ else
96
+ pattern.source
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  def on_interpolation(begin_node)
26
26
  return unless begin_node.children.empty?
27
27
 
28
- add_offense(begin_node) { |corrector| corrector.remove(begin_node.source_range) }
28
+ add_offense(begin_node) { |corrector| corrector.remove(begin_node) }
29
29
  end
30
30
  end
31
31
  end
@@ -131,12 +131,12 @@ module RuboCop
131
131
 
132
132
  # @!method eval_call?(node)
133
133
  def_node_matcher :eval_call?, <<~PATTERN
134
- (block (send _ {:instance_eval :class_eval :module_eval} ...) ...)
134
+ ({block numblock} (send _ {:instance_eval :class_eval :module_eval} ...) ...)
135
135
  PATTERN
136
136
 
137
137
  # @!method exec_call?(node)
138
138
  def_node_matcher :exec_call?, <<~PATTERN
139
- (block (send _ {:instance_exec :class_exec :module_exec} ...) ...)
139
+ ({block numblock} (send _ {:instance_exec :class_exec :module_exec} ...) ...)
140
140
  PATTERN
141
141
  end
142
142
  end
@@ -281,7 +281,7 @@ module RuboCop
281
281
  .drop_while { |r| !r.equal?(range) }
282
282
  .each_cons(2)
283
283
  .map { |range1, range2| range1.end.join(range2.begin).source }
284
- .all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
284
+ .all?(/\A\s*,\s*\z/)
285
285
  end
286
286
 
287
287
  SIMILAR_COP_NAMES_CACHE = Hash.new do |hash, cop_name|
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # Checks for string conversion in string interpolation,
6
+ # Checks for string conversion in string interpolation, `print`, `puts`, and `warn` arguments,
7
7
  # which is redundant.
8
8
  #
9
9
  # @example
@@ -11,18 +11,26 @@ module RuboCop
11
11
  # # bad
12
12
  #
13
13
  # "result is #{something.to_s}"
14
+ # print something.to_s
15
+ # puts something.to_s
16
+ # warn something.to_s
14
17
  #
15
18
  # @example
16
19
  #
17
20
  # # good
18
21
  #
19
22
  # "result is #{something}"
23
+ # print something
24
+ # puts something
25
+ # warn something
26
+ #
20
27
  class RedundantStringCoercion < Base
21
28
  include Interpolation
22
29
  extend AutoCorrector
23
30
 
24
- MSG_DEFAULT = 'Redundant use of `Object#to_s` in interpolation.'
25
- MSG_SELF = 'Use `self` instead of `Object#to_s` in interpolation.'
31
+ MSG_DEFAULT = 'Redundant use of `Object#to_s` in %<context>s.'
32
+ MSG_SELF = 'Use `self` instead of `Object#to_s` in %<context>s.'
33
+ RESTRICT_ON_SEND = %i[print puts warn].freeze
26
34
 
27
35
  # @!method to_s_without_args?(node)
28
36
  def_node_matcher :to_s_without_args?, '(send _ :to_s)'
@@ -32,18 +40,30 @@ module RuboCop
32
40
 
33
41
  return unless to_s_without_args?(final_node)
34
42
 
35
- message = final_node.receiver ? MSG_DEFAULT : MSG_SELF
36
-
37
- add_offense(final_node.loc.selector, message: message) do |corrector|
38
- receiver = final_node.receiver
39
- corrector.replace(
40
- final_node,
41
- if receiver
42
- receiver.source
43
- else
44
- 'self'
45
- end
46
- )
43
+ register_offense(final_node, 'interpolation')
44
+ end
45
+
46
+ def on_send(node)
47
+ return if node.receiver
48
+
49
+ node.each_child_node(:send) do |child|
50
+ next unless child.method?(:to_s)
51
+
52
+ register_offense(child, "`#{node.method_name}`")
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def register_offense(node, context)
59
+ receiver = node.receiver
60
+ template = receiver ? MSG_DEFAULT : MSG_SELF
61
+ message = format(template, context: context)
62
+
63
+ add_offense(node.loc.selector, message: message) do |corrector|
64
+ replacement = receiver ? receiver.source : 'self'
65
+
66
+ corrector.replace(node, replacement)
47
67
  end
48
68
  end
49
69
  end
@@ -14,8 +14,14 @@ module RuboCop
14
14
  #
15
15
  # # good
16
16
  # def foo(x, y = 1)
17
+ # # Alternatives to `__callee__` are `__method__` and `:foo`.
17
18
  # return to_enum(__callee__, x, y)
18
- # # alternatives to `__callee__` are `__method__` and `:foo`
19
+ # end
20
+ #
21
+ # # good
22
+ # def foo(x, y = 1)
23
+ # # It is also allowed if it is wrapped in some method like Sorbet.
24
+ # return to_enum(T.must(__callee__), x, y)
19
25
  # end
20
26
  #
21
27
  class ToEnumArguments < Base
@@ -111,9 +111,9 @@ module RuboCop
111
111
  return false unless node.block_type? || node.numblock_type?
112
112
 
113
113
  send_node = node.send_node
114
- return false if matches_allowed_pattern?(send_node.source)
115
-
116
- send_node.enumerable_method? || send_node.enumerator_method? || send_node.method?(:loop)
114
+ loopable = send_node.enumerable_method? || send_node.enumerator_method? ||
115
+ send_node.method?(:loop)
116
+ loopable && !matches_allowed_pattern?(send_node.source)
117
117
  end
118
118
 
119
119
  def check(node)
@@ -41,15 +41,23 @@ module RuboCop
41
41
  MSG = 'Useless method definition detected.'
42
42
 
43
43
  def on_def(node)
44
- return if use_rest_or_optional_args?(node)
44
+ return if method_definition_with_modifier?(node) || use_rest_or_optional_args?(node)
45
45
  return unless delegating?(node.body, node)
46
46
 
47
- add_offense(node) { |corrector| corrector.remove(node) }
47
+ add_offense(node) do |corrector|
48
+ range = node.parent&.send_type? ? node.parent : node
49
+
50
+ corrector.remove(range)
51
+ end
48
52
  end
49
53
  alias on_defs on_def
50
54
 
51
55
  private
52
56
 
57
+ def method_definition_with_modifier?(node)
58
+ node.parent&.send_type? && !node.parent&.non_bare_access_modifier?
59
+ end
60
+
53
61
  def use_rest_or_optional_args?(node)
54
62
  node.arguments.any? { |arg| arg.restarg_type? || arg.optarg_type? || arg.kwoptarg_type? }
55
63
  end
@@ -59,10 +59,10 @@ module RuboCop
59
59
  shuffle slice sort sort_by squeeze strip sub
60
60
  succ swapcase tr tr_s transform_values
61
61
  unicode_normalize uniq upcase].freeze
62
- METHODS_REPLACABLE_BY_EACH = %i[collect map].freeze
62
+ METHODS_REPLACEABLE_BY_EACH = %i[collect map].freeze
63
63
 
64
64
  NONMUTATING_METHODS = (NONMUTATING_METHODS_WITH_BANG_VERSION +
65
- METHODS_REPLACABLE_BY_EACH).freeze
65
+ METHODS_REPLACEABLE_BY_EACH).freeze
66
66
 
67
67
  def on_block(node)
68
68
  return unless node.body && !node.body.begin_type?
@@ -133,7 +133,11 @@ module RuboCop
133
133
  method_name = node.method_name
134
134
  return unless NONMUTATING_METHODS.include?(method_name)
135
135
 
136
- suggestion = METHODS_REPLACABLE_BY_EACH.include?(method_name) ? 'each' : "#{method_name}!"
136
+ suggestion = if METHODS_REPLACEABLE_BY_EACH.include?(method_name)
137
+ 'each'
138
+ else
139
+ "#{method_name}!"
140
+ end
137
141
  add_offense(node,
138
142
  message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
139
143
  end
@@ -44,7 +44,7 @@ module RuboCop
44
44
  def consider_node?(node)
45
45
  return true if NESTING_BLOCKS.include?(node.type)
46
46
 
47
- count_blocks? && node.block_type?
47
+ count_blocks? && (node.block_type? || node.numblock_type?)
48
48
  end
49
49
 
50
50
  def message(max)
@@ -42,6 +42,7 @@ module RuboCop
42
42
  def on_class(node)
43
43
  check_code_length(node)
44
44
  end
45
+ alias on_sclass on_class
45
46
 
46
47
  def on_casgn(node)
47
48
  parent = node.parent
@@ -10,7 +10,7 @@ module RuboCop
10
10
  include Util
11
11
 
12
12
  FOLDABLE_TYPES = %i[array hash heredoc send csend].freeze
13
- CLASSLIKE_TYPES = %i[class module].freeze
13
+ CLASSLIKE_TYPES = %i[class module sclass].freeze
14
14
  private_constant :FOLDABLE_TYPES, :CLASSLIKE_TYPES
15
15
 
16
16
  def initialize(node, processed_source, count_comments: false, foldable_types: [])
@@ -71,7 +71,7 @@ module RuboCop
71
71
  elsif (next_sibling = node.right_sibling) && next_sibling.is_a?(AST::Node)
72
72
  next_sibling.loc.line
73
73
  elsif (parent = node.parent)
74
- parent.loc.end ? parent.loc.end.line : parent.loc.line
74
+ parent.loc.respond_to?(:end) && parent.loc.end ? parent.loc.end.line : parent.loc.line
75
75
  else
76
76
  node.loc.end.line
77
77
  end
@@ -175,7 +175,7 @@ module RuboCop
175
175
  end
176
176
 
177
177
  def set_new_arg_name(transformed_argname, corrector)
178
- corrector.replace(block_node.arguments.source_range, "|#{transformed_argname}|")
178
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
179
179
  end
180
180
 
181
181
  def set_new_body_expression(transforming_body_expr, corrector)
@@ -69,7 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def first_line_comment(node)
72
- comment = processed_source.find_comment { |c| same_line?(c, node) }
72
+ comment = processed_source.comments.find { |c| same_line?(c, node) }
73
73
  return unless comment
74
74
 
75
75
  comment_source = comment.source
@@ -57,7 +57,7 @@ module RuboCop
57
57
  CONSTANT_MSG = 'Use only ascii symbols in constants.'
58
58
 
59
59
  def on_new_investigation
60
- processed_source.each_token do |token|
60
+ processed_source.tokens.each do |token|
61
61
  next if !should_check?(token) || token.text.ascii_only?
62
62
 
63
63
  message = token.type == :tIDENTIFIER ? IDENTIFIER_MSG : CONSTANT_MSG
@@ -5,6 +5,7 @@ module RuboCop
5
5
  module Naming
6
6
  # Recommends the use of inclusive language instead of problematic terms.
7
7
  # The cop can check the following locations for offenses:
8
+ #
8
9
  # - identifiers
9
10
  # - constants
10
11
  # - variables
@@ -12,6 +13,7 @@ module RuboCop
12
13
  # - symbols
13
14
  # - comments
14
15
  # - file paths
16
+ #
15
17
  # Each of these locations can be individually enabled/disabled via configuration,
16
18
  # for example CheckIdentifiers = true/false.
17
19
  #
@@ -22,6 +24,9 @@ module RuboCop
22
24
  # `WholeWord: true` can be set on a flagged term to indicate the cop should only match when
23
25
  # a term matches the whole word (partial matches will not be offenses).
24
26
  #
27
+ # The cop supports autocorrection when there is only one suggestion. When there are multiple
28
+ # suggestions, the best suggestion cannot be identified and will not be autocorrected.
29
+ #
25
30
  # @example FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }
26
31
  # # Suggest replacing identifier whitelist with allowlist
27
32
  #
@@ -68,6 +73,7 @@ module RuboCop
68
73
  # TeslaVehicle
69
74
  class InclusiveLanguage < Base
70
75
  include RangeHelp
76
+ extend AutoCorrector
71
77
 
72
78
  EMPTY_ARRAY = [].freeze
73
79
  MSG = "Consider replacing '%<term>s'%<suffix>s."
@@ -92,7 +98,7 @@ module RuboCop
92
98
  private
93
99
 
94
100
  def investigate_tokens
95
- processed_source.each_token do |token|
101
+ processed_source.tokens.each do |token|
96
102
  next unless check_token?(token.type)
97
103
 
98
104
  word_locations = scan_for_words(token.text)
@@ -104,9 +110,16 @@ module RuboCop
104
110
 
105
111
  def add_offenses_for_token(token, word_locations)
106
112
  word_locations.each do |word_location|
107
- start_position = token.pos.begin_pos + token.pos.source.index(word_location.word)
108
- range = range_between(start_position, start_position + word_location.word.length)
109
- add_offense(range, message: create_message(word_location.word))
113
+ word = word_location.word
114
+ range = offense_range(token, word)
115
+
116
+ add_offense(range, message: create_message(word)) do |corrector|
117
+ suggestions = find_flagged_term(word)['Suggestions']
118
+
119
+ next unless suggestions.is_a?(String)
120
+
121
+ corrector.replace(range, suggestions)
122
+ end
110
123
  end
111
124
  end
112
125
 
@@ -264,6 +277,12 @@ module RuboCop
264
277
  end
265
278
  " with #{suggestion_str}"
266
279
  end
280
+
281
+ def offense_range(token, word)
282
+ start_position = token.pos.begin_pos + token.pos.source.index(word)
283
+
284
+ range_between(start_position, start_position + word.length)
285
+ end
267
286
  end
268
287
  end
269
288
  end
@@ -11,7 +11,7 @@ module RuboCop
11
11
  #
12
12
  # Moving from compact to nested children requires knowledge of whether the
13
13
  # outer parent is a module or a class. Moving from nested to compact requires
14
- # verification that the outer parent is defined elsewhere. Rubocop does not
14
+ # verification that the outer parent is defined elsewhere. RuboCop does not
15
15
  # have the knowledge to perform either operation safely and thus requires
16
16
  # manual oversight.
17
17
  #
@@ -27,12 +27,16 @@ module RuboCop
27
27
  # var.class.equal?(Date)
28
28
  # var.class.eql?(Date)
29
29
  # var.class.name == 'Date'
30
+ # var.class.to_s == 'Date'
31
+ # var.class.inspect == 'Date'
30
32
  #
31
33
  # @example AllowedMethods: [`==`]
32
34
  # # good
33
35
  # var.instance_of?(Date)
34
36
  # var.class == Date
35
37
  # var.class.name == 'Date'
38
+ # var.class.to_s == 'Date'
39
+ # var.class.inspect == 'Date'
36
40
  #
37
41
  # # bad
38
42
  # var.class.equal?(Date)
@@ -47,6 +51,8 @@ module RuboCop
47
51
  # var.class.equal?(Date)
48
52
  # var.class.eql?(Date)
49
53
  # var.class.name == 'Date'
54
+ # var.class.to_s == 'Date'
55
+ # var.class.inspect == 'Date'
50
56
  #
51
57
  # @example AllowedPatterns: ['eq']
52
58
  # # good
@@ -57,6 +63,8 @@ module RuboCop
57
63
  # # bad
58
64
  # var.class == Date
59
65
  # var.class.name == 'Date'
66
+ # var.class.to_s == 'Date'
67
+ # var.class.inspect == 'Date'
60
68
  #
61
69
  class ClassEqualityComparison < Base
62
70
  include RangeHelp
@@ -64,14 +72,15 @@ module RuboCop
64
72
  include AllowedPattern
65
73
  extend AutoCorrector
66
74
 
67
- MSG = 'Use `instance_of?(%<class_name>s)` instead of comparing classes.'
75
+ MSG = 'Use `instance_of?%<class_argument>s` instead of comparing classes.'
68
76
 
69
77
  RESTRICT_ON_SEND = %i[== equal? eql?].freeze
78
+ CLASS_NAME_METHODS = %i[name to_s inspect].freeze
70
79
 
71
80
  # @!method class_comparison_candidate?(node)
72
81
  def_node_matcher :class_comparison_candidate?, <<~PATTERN
73
82
  (send
74
- {$(send _ :class) (send $(send _ :class) :name)}
83
+ {$(send _ :class) (send $(send _ :class) #class_name_method?)}
75
84
  {:== :equal? :eql?} $_)
76
85
  PATTERN
77
86
 
@@ -83,10 +92,12 @@ module RuboCop
83
92
 
84
93
  class_comparison_candidate?(node) do |receiver_node, class_node|
85
94
  range = offense_range(receiver_node, node)
86
- class_name = class_name(class_node, node)
95
+ class_argument = (class_name = class_name(class_node, node)) ? "(#{class_name})" : ''
87
96
 
88
- add_offense(range, message: format(MSG, class_name: class_name)) do |corrector|
89
- corrector.replace(range, "instance_of?(#{class_name})")
97
+ add_offense(range, message: format(MSG, class_argument: class_argument)) do |corrector|
98
+ next unless class_name
99
+
100
+ corrector.replace(range, "instance_of?#{class_argument}")
90
101
  end
91
102
  end
92
103
  end
@@ -94,19 +105,41 @@ module RuboCop
94
105
  private
95
106
 
96
107
  def class_name(class_node, node)
97
- if node.children.first.method?(:name)
98
- return class_node.receiver.source if class_node.receiver
108
+ if class_name_method?(node.children.first.method_name)
109
+ if (receiver = class_node.receiver) && class_name_method?(class_node.method_name)
110
+ return receiver.source
111
+ end
99
112
 
100
113
  if class_node.str_type?
101
- value = class_node.source.delete('"').delete("'")
102
- value.prepend('::') if class_node.each_ancestor(:class, :module).any?
114
+ value = trim_string_quotes(class_node)
115
+ value.prepend('::') if require_cbase?(class_node)
103
116
  return value
117
+ elsif unable_to_determine_type?(class_node)
118
+ # When a variable or return value of a method is used, it returns nil
119
+ # because the type is not known and cannot be suggested.
120
+ return
104
121
  end
105
122
  end
106
123
 
107
124
  class_node.source
108
125
  end
109
126
 
127
+ def class_name_method?(method_name)
128
+ CLASS_NAME_METHODS.include?(method_name)
129
+ end
130
+
131
+ def require_cbase?(class_node)
132
+ class_node.each_ancestor(:class, :module).any?
133
+ end
134
+
135
+ def unable_to_determine_type?(class_node)
136
+ class_node.variable? || class_node.call_type?
137
+ end
138
+
139
+ def trim_string_quotes(class_node)
140
+ class_node.source.delete('"').delete("'")
141
+ end
142
+
110
143
  def offense_range(receiver_node, node)
111
144
  range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
112
145
  end
@@ -82,7 +82,7 @@ module RuboCop
82
82
  def notice_found?(processed_source)
83
83
  notice_found = false
84
84
  notice_regexp = Regexp.new(notice)
85
- processed_source.each_token do |token|
85
+ processed_source.tokens.each do |token|
86
86
  break unless token.comment?
87
87
 
88
88
  notice_found = notice_regexp.match?(token.text)
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for inheritance from `Data.define` to avoid creating the anonymous parent class.
7
+ #
8
+ # @safety
9
+ # Autocorrection is unsafe because it will change the inheritance
10
+ # tree (e.g. return value of `Module#ancestors`) of the constant.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # class Person < Data.define(:first_name, :last_name)
15
+ # def age
16
+ # 42
17
+ # end
18
+ # end
19
+ #
20
+ # # good
21
+ # Person = Data.define(:first_name, :last_name) do
22
+ # def age
23
+ # 42
24
+ # end
25
+ # end
26
+ class DataInheritance < Base
27
+ include RangeHelp
28
+ extend AutoCorrector
29
+ extend TargetRubyVersion
30
+
31
+ MSG = "Don't extend an instance initialized by `Data.define`. " \
32
+ 'Use a block to customize the class.'
33
+
34
+ minimum_target_ruby_version 3.2
35
+
36
+ def on_class(node)
37
+ return unless data_define?(node.parent_class)
38
+
39
+ add_offense(node.parent_class.source_range) do |corrector|
40
+ corrector.remove(range_with_surrounding_space(node.loc.keyword, newlines: false))
41
+ corrector.replace(node.loc.operator, '=')
42
+
43
+ correct_parent(node.parent_class, corrector)
44
+ end
45
+ end
46
+
47
+ # @!method data_define?(node)
48
+ def_node_matcher :data_define?, <<~PATTERN
49
+ {(send (const {nil? cbase} :Data) :define ...)
50
+ (block (send (const {nil? cbase} :Data) :define ...) ...)}
51
+ PATTERN
52
+
53
+ private
54
+
55
+ def correct_parent(parent, corrector)
56
+ if parent.block_type?
57
+ corrector.remove(range_with_surrounding_space(parent.loc.end, newlines: false))
58
+ elsif (class_node = parent.parent).body.nil?
59
+ corrector.remove(range_for_empty_class_body(class_node, parent))
60
+ else
61
+ corrector.insert_after(parent, ' do')
62
+ end
63
+ end
64
+
65
+ def range_for_empty_class_body(class_node, data_define)
66
+ if class_node.single_line?
67
+ range_between(data_define.source_range.end_pos, class_node.source_range.end_pos)
68
+ else
69
+ range_by_whole_lines(class_node.loc.end, include_final_newline: true)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -34,8 +34,8 @@ module RuboCop
34
34
  extend AutoCorrector
35
35
 
36
36
  # rubocop:enable Lint/RedundantCopDisableDirective
37
- MSG = 'Rubocop disable/enable directives are not permitted.'
38
- MSG_FOR_COPS = 'Rubocop disable/enable directives for %<cops>s are not permitted.'
37
+ MSG = 'RuboCop disable/enable directives are not permitted.'
38
+ MSG_FOR_COPS = 'RuboCop disable/enable directives for %<cops>s are not permitted.'
39
39
 
40
40
  def on_new_investigation
41
41
  processed_source.comments.each do |comment|