rubocop 1.48.1 → 1.49.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +13 -2
  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/cop/autocorrect_logic.rb +28 -12
  8. data/lib/rubocop/cop/cop.rb +2 -2
  9. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  10. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  11. data/lib/rubocop/cop/internal_affairs/cop_description.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
  13. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  14. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
  15. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  16. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  17. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  18. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
  19. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  20. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  21. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  22. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  23. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
  24. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  25. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  26. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  27. data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
  28. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  29. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  30. data/lib/rubocop/cop/lint/void.rb +7 -3
  31. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  32. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  33. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  34. data/lib/rubocop/cop/naming/inclusive_language.rb +22 -3
  35. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  36. data/lib/rubocop/cop/style/class_equality_comparison.rb +15 -2
  37. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  38. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  39. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  40. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  41. data/lib/rubocop/cop/style/hash_except.rb +4 -4
  42. data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
  43. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -12
  44. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  45. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  46. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -7
  47. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
  48. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  49. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  50. data/lib/rubocop/cop/style/redundant_line_continuation.rb +142 -0
  51. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  52. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  53. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  54. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  55. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
  56. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  57. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  58. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  59. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  60. data/lib/rubocop/ext/regexp_node.rb +1 -1
  61. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  62. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  63. data/lib/rubocop/options.rb +4 -1
  64. data/lib/rubocop/result_cache.rb +1 -1
  65. data/lib/rubocop/server/cache.rb +1 -1
  66. data/lib/rubocop/server/helper.rb +1 -1
  67. data/lib/rubocop/server/server_command/exec.rb +1 -1
  68. data/lib/rubocop/version.rb +1 -1
  69. data/lib/rubocop.rb +2 -0
  70. metadata +8 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39e28428d694fdd8cf940d5e3494f62a0266b3ffd41e823cf50131d16b45cf2a
4
- data.tar.gz: 75eadde7a899742fcb5f08c4b3cd845658dd64087dd80c0dad1bda5fef30804a
3
+ metadata.gz: e52db814b1bd4755ea1be2740adc7513a40d731fcc2ff52e323a4d569dab3bb1
4
+ data.tar.gz: 06c85b14778fad98f2adb724df26b22b423bb65fdb45fcd159e6ed251a5ab678
5
5
  SHA512:
6
- metadata.gz: 21bba5ce4024ff5c66695c56e4261fbefdedc8b293396b8d0687f836d4a56d0f53ae307435cb714a3e4b059def785a1d9862504910f94fe1815223b6036b727b
7
- data.tar.gz: aec136f1e174b57ba2ae6307a3fdeda0257614bcaaf77a6d5c5eca9733cfd185970dd998ecc95a6691d9aa8310419cd69679ba9b021f5b8284b9d12ef675dcc0
6
+ metadata.gz: acc6052f3d94842532268a6dd90f307dde0502e7d126dc39b8dad933187a37523cd5226e5cd0ac14e85eeaa8bf3f5c020eb56a24ebee1735b41011b5279ec98d
7
+ data.tar.gz: 25740c7e44cf713b64146bd4749ea28f302c4fc20cc85c16978749bed82014c9bd8096573149d8fc7528eb4fe31647a6f8d808ca4fed739b90d17286baea8ac8
data/README.md CHANGED
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.48', require: false
56
+ gem 'rubocop', '~> 1.49', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -2779,7 +2779,7 @@ Naming/InclusiveLanguage:
2779
2779
  Description: 'Recommend the use of inclusive language instead of problematic terms.'
2780
2780
  Enabled: false
2781
2781
  VersionAdded: '1.18'
2782
- VersionChanged: '1.21'
2782
+ VersionChanged: '1.49'
2783
2783
  CheckIdentifiers: true
2784
2784
  CheckConstants: true
2785
2785
  CheckVariables: true
@@ -3277,7 +3277,7 @@ Style/ClassAndModuleChildren:
3277
3277
  StyleGuide: '#namespace-definition'
3278
3278
  # Moving from compact to nested children requires knowledge of whether the
3279
3279
  # outer parent is a module or a class. Moving from nested to compact requires
3280
- # verification that the outer parent is defined elsewhere. Rubocop does not
3280
+ # verification that the outer parent is defined elsewhere. RuboCop does not
3281
3281
  # have the knowledge to perform either operation safely and thus requires
3282
3282
  # manual oversight.
3283
3283
  SafeAutoCorrect: false
@@ -3508,6 +3508,12 @@ Style/Copyright:
3508
3508
  Notice: '^Copyright (\(c\) )?2[0-9]{3} .+'
3509
3509
  AutocorrectNotice: ''
3510
3510
 
3511
+ Style/DataInheritance:
3512
+ Description: 'Checks for inheritance from Data.define.'
3513
+ StyleGuide: '#no-extend-data-define'
3514
+ Enabled: pending
3515
+ VersionAdded: '1.49'
3516
+
3511
3517
  Style/DateTime:
3512
3518
  Description: 'Use Time over DateTime.'
3513
3519
  StyleGuide: '#date-time'
@@ -4878,6 +4884,11 @@ Style/RedundantInterpolation:
4878
4884
  VersionAdded: '0.76'
4879
4885
  VersionChanged: '1.30'
4880
4886
 
4887
+ Style/RedundantLineContinuation:
4888
+ Description: 'Check for redundant line continuation.'
4889
+ Enabled: pending
4890
+ VersionAdded: '1.49'
4891
+
4881
4892
  Style/RedundantParentheses:
4882
4893
  Description: "Checks for parentheses that seem not to serve any purpose."
4883
4894
  Enabled: true
@@ -76,13 +76,18 @@ module RuboCop
76
76
  warn <<~WARNING
77
77
  Errors are usually caused by RuboCop bugs.
78
78
  Please, report your problems to RuboCop's issue tracker.
79
- #{Gem.loaded_specs['rubocop'].metadata['bug_tracker_uri']}
80
-
79
+ #{bug_tracker_uri}
81
80
  Mention the following information in the issue report:
82
81
  #{RuboCop::Version.version(debug: true)}
83
82
  WARNING
84
83
  end
85
84
 
85
+ def bug_tracker_uri
86
+ return unless Gem.loaded_specs.key?('rubocop')
87
+
88
+ "#{Gem.loaded_specs['rubocop'].metadata['bug_tracker_uri']}\n"
89
+ end
90
+
86
91
  def maybe_print_corrected_source
87
92
  # Integration tools (like RubyMine) expect to have only the JSON result
88
93
  # when specifying JSON format. Similar HTML and JUnit are targeted as well.
data/lib/rubocop/cli.rb CHANGED
@@ -87,20 +87,20 @@ module RuboCop
87
87
 
88
88
  tmp_dir = File.join(ConfigFinder.project_root, 'tmp')
89
89
  FileUtils.mkdir_p(tmp_dir)
90
+ cpu_profile_file = File.join(tmp_dir, 'rubocop-stackprof.dump')
90
91
  status = nil
91
92
 
92
- StackProf.run(out: File.join(tmp_dir, 'rubocop-stackprof.dump')) do
93
+ StackProf.run(out: cpu_profile_file) do
93
94
  status = yield
94
95
  end
95
- puts 'Profile report generated'
96
+ puts "Profile report generated at #{cpu_profile_file}"
96
97
 
97
98
  if with_memory
98
99
  puts 'Building memory report...'
99
100
  report = MemoryProfiler.stop
100
- report.pretty_print(
101
- to_file: File.join(tmp_dir, 'rubocop-memory_profiler.txt'),
102
- scale_bytes: true
103
- )
101
+ memory_profile_file = File.join(tmp_dir, 'rubocop-memory_profiler.txt')
102
+ report.pretty_print(to_file: memory_profile_file, scale_bytes: true)
103
+ puts "Memory report generated at #{memory_profile_file}"
104
104
  end
105
105
  status
106
106
  end
@@ -29,7 +29,7 @@ module RuboCop
29
29
  end
30
30
 
31
31
  # rubocop:disable Metrics/AbcSize
32
- def initialize(hash = {}, loaded_path = nil)
32
+ def initialize(hash = RuboCop::ConfigLoader.default_configuration, loaded_path = nil)
33
33
  @loaded_path = loaded_path
34
34
  @for_cop = Hash.new do |h, cop|
35
35
  cop_name = cop.respond_to?(:cop_name) ? cop.cop_name : cop
@@ -35,7 +35,7 @@ module RuboCop
35
35
  return false if cop_config['AutoCorrect'] == false
36
36
 
37
37
  # :safe_autocorrect is a derived option based on several command-line
38
- # arguments - see Rubocop::Options#add_autocorrection_options
38
+ # arguments - see RuboCop::Options#add_autocorrection_options
39
39
  return safe_autocorrect? if @options.fetch(:safe_autocorrect, false)
40
40
 
41
41
  true
@@ -43,18 +43,24 @@ module RuboCop
43
43
 
44
44
  private
45
45
 
46
- def disable_offense(range)
47
- heredoc_range = surrounding_heredoc(range)
48
- if heredoc_range
49
- disable_offense_before_and_after(range_by_lines(heredoc_range))
46
+ def disable_offense(offense_range)
47
+ range = surrounding_heredoc(offense_range) || surrounding_percent_array(offense_range)
48
+
49
+ if range
50
+ disable_offense_before_and_after(range_by_lines(range))
51
+ else
52
+ disable_offense_with_eol_or_surround_comment(offense_range)
53
+ end
54
+ end
55
+
56
+ def disable_offense_with_eol_or_surround_comment(range)
57
+ eol_comment = " # rubocop:todo #{cop_name}"
58
+ needed_line_length = (range.source_line + eol_comment).length
59
+
60
+ if needed_line_length <= max_line_length
61
+ disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
50
62
  else
51
- eol_comment = " # rubocop:todo #{cop_name}"
52
- needed_line_length = (range.source_line + eol_comment).length
53
- if needed_line_length <= max_line_length
54
- disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
55
- else
56
- disable_offense_before_and_after(range_by_lines(range))
57
- end
63
+ disable_offense_before_and_after(range_by_lines(range))
58
64
  end
59
65
  end
60
66
 
@@ -69,6 +75,16 @@ module RuboCop
69
75
  .find { |range| range.contains?(offense_range) }
70
76
  end
71
77
 
78
+ def surrounding_percent_array(offense_range)
79
+ return nil if offense_range.empty?
80
+
81
+ percent_array = processed_source.ast.each_descendant.select do |node|
82
+ node.array_type? && node.percent_literal?
83
+ end
84
+
85
+ percent_array.map(&:source_range).find { |range| range.overlaps?(offense_range) }
86
+ end
87
+
72
88
  def range_of_first_line(range)
73
89
  begin_of_first_line = range.begin_pos - range.column
74
90
  end_of_first_line = begin_of_first_line + range.source_line.length
@@ -138,10 +138,10 @@ module RuboCop
138
138
  def correction_lambda
139
139
  return unless support_autocorrect?
140
140
 
141
- dedup_on_node(@v0_argument) { autocorrect(@v0_argument) }
141
+ dedupe_on_node(@v0_argument) { autocorrect(@v0_argument) }
142
142
  end
143
143
 
144
- def dedup_on_node(node)
144
+ def dedupe_on_node(node)
145
145
  @corrected_nodes ||= {}.compare_by_identity
146
146
  yield unless @corrected_nodes.key?(node)
147
147
  ensure
@@ -74,7 +74,7 @@ module RuboCop
74
74
  def add_heredoc_comma(corrector, node)
75
75
  return unless heredoc?(node)
76
76
 
77
- corrector.insert_after(node.child_nodes.last.source_range, ',')
77
+ corrector.insert_after(node.child_nodes.last, ',')
78
78
  end
79
79
 
80
80
  def heredoc?(node)
@@ -66,10 +66,10 @@ module RuboCop
66
66
  end
67
67
  end
68
68
 
69
- def line_breaks(node, source, previous_line_num, base_line_num, node_indx)
69
+ def line_breaks(node, source, previous_line_num, base_line_num, node_index)
70
70
  source_in_lines = source.split("\n")
71
71
  if first_line?(node, previous_line_num)
72
- node_indx.zero? && node.first_line == base_line_num ? '' : ' '
72
+ node_index.zero? && node.first_line == base_line_num ? '' : ' '
73
73
  else
74
74
  process_lines(node, previous_line_num, base_line_num, source_in_lines)
75
75
  end
@@ -24,7 +24,7 @@ module RuboCop
24
24
  MSG = 'Description should be started with %<suggestion>s instead of `This cop ...`.'
25
25
 
26
26
  SPECIAL_WORDS = %w[is can could should will would must may].freeze
27
- COP_DESC_OFFENSE_REGEX = \
27
+ COP_DESC_OFFENSE_REGEX =
28
28
  /^\s+# This cop (?<special>#{SPECIAL_WORDS.join('|')})?\s*(?<word>.+?) .*/.freeze
29
29
  REPLACEMENT_REGEX = /^\s+# This cop (#{SPECIAL_WORDS.join('|')})?\s*(.+?) /.freeze
30
30
 
@@ -52,7 +52,7 @@ module RuboCop
52
52
  # @return [void]
53
53
  def autocorrect(corrector, node)
54
54
  [
55
- heredoc_openning_delimiter_range_from(node),
55
+ heredoc_opening_delimiter_range_from(node),
56
56
  heredoc_closing_delimiter_range_from(node)
57
57
  ].each do |range|
58
58
  corrector.replace(range, EXPECTED_HEREDOC_DELIMITER)
@@ -90,7 +90,7 @@ module RuboCop
90
90
 
91
91
  # @param node [RuboCop::AST::StrNode]
92
92
  # @return [Parser::Source::Range]
93
- def heredoc_openning_delimiter_range_from(node)
93
+ def heredoc_opening_delimiter_range_from(node)
94
94
  match_data = node.source.match(Heredoc::OPENING_DELIMITER)
95
95
  node.source_range.begin.adjust(
96
96
  begin_pos: match_data.begin(2),
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module InternalAffairs
6
- # `RuboCop::Cop::Cop` is deprecated and will be removed in Rubocop 2.0.
6
+ # `RuboCop::Cop::Cop` is deprecated and will be removed in RuboCop 2.0.
7
7
  # Your custom cop class should inherit from `RuboCop::Cop::Base` instead of
8
8
  # `RuboCop::Cop::Cop`.
9
9
  #
@@ -13,19 +13,46 @@ module RuboCop
13
13
  # # good
14
14
  # node.source
15
15
  #
16
+ # # bad
17
+ # add_offense(node) { |corrector| corrector.replace(node.source_range, prefer) }
18
+ # add_offense(node) { |corrector| corrector.insert_before(node.source_range, prefer) }
19
+ # add_offense(node) { |corrector| corrector.insert_before_multi(node.source_range, prefer) }
20
+ # add_offense(node) { |corrector| corrector.insert_after(node.source_range, prefer) }
21
+ # add_offense(node) { |corrector| corrector.insert_after_multi(node.source_range, prefer) }
22
+ # add_offense(node) { |corrector| corrector.swap(node.source_range, before, after) }
23
+ #
24
+ # # good
25
+ # add_offense(node) { |corrector| corrector.replace(node, prefer) }
26
+ # add_offense(node) { |corrector| corrector.insert_before(node, prefer) }
27
+ # add_offense(node) { |corrector| corrector.insert_before_multi(node, prefer) }
28
+ # add_offense(node) { |corrector| corrector.insert_after(node, prefer) }
29
+ # add_offense(node) { |corrector| corrector.insert_after_multi(node, prefer) }
30
+ # add_offense(node) { |corrector| corrector.swap(node, before, after) }
31
+ #
16
32
  class RedundantSourceRange < Base
17
33
  extend AutoCorrector
18
34
 
19
35
  MSG = 'Remove the redundant `source_range`.'
20
- RESTRICT_ON_SEND = %i[source].freeze
36
+ RESTRICT_ON_SEND = %i[
37
+ source
38
+ replace remove insert_before insert_before_multi insert_after insert_after_multi swap
39
+ ].freeze
21
40
 
22
41
  # @!method redundant_source_range(node)
23
42
  def_node_matcher :redundant_source_range, <<~PATTERN
24
- (send $(send _ :source_range) :source)
43
+ {
44
+ (send $(send _ :source_range) :source)
45
+ (send _ {
46
+ :replace :insert_before :insert_before_multi :insert_after :insert_after_multi
47
+ } $(send _ :source_range) _)
48
+ (send _ :remove $(send _ :source_range))
49
+ (send _ :swap $(send _ :source_range) _ _)
50
+ }
25
51
  PATTERN
26
52
 
27
53
  def on_send(node)
28
54
  return unless (source_range = redundant_source_range(node))
55
+ return if source_range.receiver.send_type? && source_range.receiver.method?(:buffer)
29
56
 
30
57
  selector = source_range.loc.selector
31
58
 
@@ -51,6 +51,8 @@ module RuboCop
51
51
  MSG_BEFORE_FOR_ONLY_BEFORE = 'Keep a blank line before `%<modifier>s`.'
52
52
  MSG_AFTER_FOR_ONLY_BEFORE = 'Remove a blank line after `%<modifier>s`.'
53
53
 
54
+ RESTRICT_ON_SEND = %i[public protected private module_function].freeze
55
+
54
56
  def initialize(config = nil, options = nil)
55
57
  super
56
58
 
@@ -109,6 +109,7 @@ module RuboCop
109
109
  check_other_alignment(node)
110
110
  end
111
111
  end
112
+ alias on_case_match on_case
112
113
 
113
114
  private
114
115
 
@@ -169,7 +170,10 @@ module RuboCop
169
170
  end
170
171
 
171
172
  def alignment_node_for_variable_style(node)
172
- return node.parent if node.case_type? && node.argument? && same_line?(node, node.parent)
173
+ if (node.case_type? || node.case_match_type?) && node.argument? &&
174
+ same_line?(node, node.parent)
175
+ return node.parent
176
+ end
173
177
 
174
178
  assignment = assignment_or_operator_method(node)
175
179
 
@@ -49,7 +49,7 @@ module RuboCop
49
49
 
50
50
  private
51
51
 
52
- def aligned_locations(locs)
52
+ def aligned_locations(locs) # rubocop:disable Metrics/AbcSize
53
53
  return [] if locs.empty?
54
54
 
55
55
  aligned = Set[locs.first.line, locs.last.line]
@@ -57,6 +57,11 @@ module RuboCop
57
57
  col = loc.column
58
58
  aligned << loc.line if col == before.column || col == after.column
59
59
  end
60
+
61
+ # if locs.size > 2 and the size of variable `aligned`
62
+ # has not increased from its initial value, there are not aligned lines.
63
+ return [] if locs.size > 2 && aligned.size == 2
64
+
60
65
  aligned
61
66
  end
62
67
 
@@ -153,9 +153,10 @@ module RuboCop
153
153
  MSG = 'Indent the first argument one step more than %<base>s.'
154
154
 
155
155
  def on_send(node)
156
+ return unless should_check?(node)
157
+ return if same_line?(node, node.first_argument)
156
158
  return if style != :consistent && enforce_first_argument_with_fixed_indentation? &&
157
159
  !enable_layout_first_method_argument_line_break?
158
- return if !node.arguments? || bare_operator?(node) || node.setter_method?
159
160
 
160
161
  indent = base_indentation(node) + configured_indentation_width
161
162
 
@@ -166,6 +167,10 @@ module RuboCop
166
167
 
167
168
  private
168
169
 
170
+ def should_check?(node)
171
+ node.arguments? && !bare_operator?(node) && !node.setter_method?
172
+ end
173
+
169
174
  def autocorrect(corrector, node)
170
175
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
171
176
  end
@@ -67,7 +67,7 @@ module RuboCop
67
67
 
68
68
  outermost_send = outermost_send_on_same_line(heredoc_arg)
69
69
  return unless outermost_send
70
- return if end_keyword_before_closing_parentesis?(node)
70
+ return if end_keyword_before_closing_parenthesis?(node)
71
71
  return if subsequent_closing_parentheses_in_same_line?(outermost_send)
72
72
  return if exist_argument_between_heredoc_end_and_closing_parentheses?(node)
73
73
 
@@ -159,7 +159,7 @@ module RuboCop
159
159
 
160
160
  # Closing parenthesis helpers.
161
161
 
162
- def end_keyword_before_closing_parentesis?(parenthesized_send_node)
162
+ def end_keyword_before_closing_parenthesis?(parenthesized_send_node)
163
163
  parenthesized_send_node.ancestors.any? do |ancestor|
164
164
  ancestor.loc.respond_to?(:end) && ancestor.loc.end&.source == 'end'
165
165
  end
@@ -50,9 +50,9 @@ module RuboCop
50
50
 
51
51
  def on_send(node)
52
52
  # Include "the whole expression".
53
- node = node.parent while convertible_block?(node) ||
54
- node.parent.is_a?(RuboCop::AST::BinaryOperatorNode) ||
55
- node.parent&.send_type?
53
+ node = node.parent while node.parent&.send_type? ||
54
+ convertible_block?(node) ||
55
+ node.parent.is_a?(RuboCop::AST::BinaryOperatorNode)
56
56
 
57
57
  return unless offense?(node) && !part_of_ignored_node?(node)
58
58
 
@@ -69,15 +69,14 @@ module RuboCop
69
69
 
70
70
  def register_offense(node)
71
71
  add_offense(node) do |corrector|
72
- corrector.replace(node.source_range, to_single_line(node.source).strip)
72
+ corrector.replace(node, to_single_line(node.source).strip)
73
73
  end
74
74
  ignore_node(node)
75
75
  end
76
76
 
77
77
  def offense?(node)
78
- return false if configured_to_not_be_inspected?(node)
79
-
80
- node.multiline? && !too_long?(node) && suitable_as_single_line?(node)
78
+ node.multiline? && !too_long?(node) && suitable_as_single_line?(node) &&
79
+ !configured_to_not_be_inspected?(node)
81
80
  end
82
81
 
83
82
  def configured_to_not_be_inspected?(node)
@@ -34,6 +34,7 @@ module RuboCop
34
34
 
35
35
  def on_send(node)
36
36
  return unless regular_method_call_with_arguments?(node)
37
+ return if node.parenthesized?
37
38
 
38
39
  first_arg = node.first_argument.source_range
39
40
  first_arg_with_space = range_with_surrounding_space(first_arg, side: :left)
@@ -52,7 +53,6 @@ module RuboCop
52
53
  end
53
54
 
54
55
  def expect_params_after_method_name?(node)
55
- return false if node.parenthesized?
56
56
  return true if no_space_between_method_name_and_first_argument?(node)
57
57
 
58
58
  first_arg = node.first_argument
@@ -91,7 +91,7 @@ module RuboCop
91
91
  if !left_parens?(token1, token2) && !right_parens?(token1, token2)
92
92
  correct_missing_space(token1, token2)
93
93
  else
94
- correct_extaneus_space_between_consecutive_parens(token1, token2)
94
+ correct_extraneous_space_between_consecutive_parens(token1, token2)
95
95
  end
96
96
  end
97
97
  end
@@ -112,7 +112,7 @@ module RuboCop
112
112
  end
113
113
  end
114
114
 
115
- def correct_extaneus_space_between_consecutive_parens(token1, token2)
115
+ def correct_extraneous_space_between_consecutive_parens(token1, token2)
116
116
  return if range_between(token1.end_pos, token2.begin_pos).source != ' '
117
117
 
118
118
  range = range_between(token1.end_pos, token2.begin_pos)
@@ -38,7 +38,7 @@ module RuboCop
38
38
  attr clone dup exists? freeze gethostbyaddr gethostbyname iterator?
39
39
  ].freeze
40
40
 
41
- PREFERRED_METHDOS = {
41
+ PREFERRED_METHODS = {
42
42
  clone: 'to_h',
43
43
  dup: 'to_h',
44
44
  exists?: 'exist?',
@@ -97,11 +97,11 @@ module RuboCop
97
97
 
98
98
  "#{preferred_attr_method} #{node.first_argument.source}"
99
99
  elsif dir_env_file_const?(node.receiver)
100
- prefer = PREFERRED_METHDOS[node.method_name]
100
+ prefer = PREFERRED_METHODS[node.method_name]
101
101
 
102
102
  prefer ? "#{node.receiver.source}.#{prefer}" : 'ENV'
103
103
  else
104
- PREFERRED_METHDOS[node.method_name]
104
+ PREFERRED_METHODS[node.method_name]
105
105
  end
106
106
  end
107
107
 
@@ -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|
@@ -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)
@@ -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)