rubocop 1.48.0 → 1.49.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +14 -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 +3 -3
  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/gemspec/dependency_version.rb +1 -1
  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/block_end_newline.rb +7 -21
  17. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  18. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  19. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  20. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
  21. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  22. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  23. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  24. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  25. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
  26. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  27. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  28. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  29. data/lib/rubocop/cop/lint/syntax.rb +4 -0
  30. data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
  31. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  32. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  33. data/lib/rubocop/cop/lint/useless_rescue.rb +4 -1
  34. data/lib/rubocop/cop/lint/void.rb +7 -3
  35. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  36. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  37. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
  38. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  39. data/lib/rubocop/cop/naming/inclusive_language.rb +22 -3
  40. data/lib/rubocop/cop/naming/method_name.rb +2 -2
  41. data/lib/rubocop/cop/style/accessor_grouping.rb +4 -4
  42. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  43. data/lib/rubocop/cop/style/class_equality_comparison.rb +15 -2
  44. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  45. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  46. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  47. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  48. data/lib/rubocop/cop/style/hash_except.rb +4 -4
  49. data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
  50. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -12
  51. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  52. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  53. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -7
  54. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
  55. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  56. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  57. data/lib/rubocop/cop/style/redundant_line_continuation.rb +142 -0
  58. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  59. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  60. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  61. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  62. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
  63. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  64. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  65. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  66. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  67. data/lib/rubocop/ext/regexp_node.rb +1 -1
  68. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  69. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  70. data/lib/rubocop/options.rb +4 -1
  71. data/lib/rubocop/result_cache.rb +1 -1
  72. data/lib/rubocop/rspec/support.rb +1 -0
  73. data/lib/rubocop/server/cache.rb +1 -1
  74. data/lib/rubocop/server/helper.rb +1 -1
  75. data/lib/rubocop/server/server_command/exec.rb +1 -1
  76. data/lib/rubocop/version.rb +1 -1
  77. data/lib/rubocop.rb +2 -0
  78. metadata +11 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb25d50ecbd2f942993d245b3302d53cf3605fa362e0addf1ee088caaa22bfff
4
- data.tar.gz: 62df82b33ce3ae669b7fd23ca69f1a53f9dbbcd480d44cfff763df3c84c39585
3
+ metadata.gz: e52db814b1bd4755ea1be2740adc7513a40d731fcc2ff52e323a4d569dab3bb1
4
+ data.tar.gz: 06c85b14778fad98f2adb724df26b22b423bb65fdb45fcd159e6ed251a5ab678
5
5
  SHA512:
6
- metadata.gz: 563e301ae73973fd6ccb3679ce82fe15a02d6634c7993bfab9f0c15ae29687982ac63ec07f656be22273e6b5c63cd2686b678cdc3319536b51d086103ec60fcd
7
- data.tar.gz: eb24bb4636f91c76715a81896852d6620306ac45e52755cc5c2148ff0f85d1012ebe7b043076a802259bf1d130fc4296d55b70f4d99816aba39056fa05205b16
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
@@ -1659,6 +1659,7 @@ Lint/Debugger:
1659
1659
  - Kernel.binding.remote_pry
1660
1660
  - Kernel.binding.pry_remote
1661
1661
  - Pry.rescue
1662
+ - pry
1662
1663
  Rails:
1663
1664
  - debugger
1664
1665
  - Kernel.debugger
@@ -2778,7 +2779,7 @@ Naming/InclusiveLanguage:
2778
2779
  Description: 'Recommend the use of inclusive language instead of problematic terms.'
2779
2780
  Enabled: false
2780
2781
  VersionAdded: '1.18'
2781
- VersionChanged: '1.21'
2782
+ VersionChanged: '1.49'
2782
2783
  CheckIdentifiers: true
2783
2784
  CheckConstants: true
2784
2785
  CheckVariables: true
@@ -3276,7 +3277,7 @@ Style/ClassAndModuleChildren:
3276
3277
  StyleGuide: '#namespace-definition'
3277
3278
  # Moving from compact to nested children requires knowledge of whether the
3278
3279
  # outer parent is a module or a class. Moving from nested to compact requires
3279
- # verification that the outer parent is defined elsewhere. Rubocop does not
3280
+ # verification that the outer parent is defined elsewhere. RuboCop does not
3280
3281
  # have the knowledge to perform either operation safely and thus requires
3281
3282
  # manual oversight.
3282
3283
  SafeAutoCorrect: false
@@ -3507,6 +3508,12 @@ Style/Copyright:
3507
3508
  Notice: '^Copyright (\(c\) )?2[0-9]{3} .+'
3508
3509
  AutocorrectNotice: ''
3509
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
+
3510
3517
  Style/DateTime:
3511
3518
  Description: 'Use Time over DateTime.'
3512
3519
  StyleGuide: '#date-time'
@@ -4877,6 +4884,11 @@ Style/RedundantInterpolation:
4877
4884
  VersionAdded: '0.76'
4878
4885
  VersionChanged: '1.30'
4879
4886
 
4887
+ Style/RedundantLineContinuation:
4888
+ Description: 'Check for redundant line continuation.'
4889
+ Enabled: pending
4890
+ VersionAdded: '1.49'
4891
+
4880
4892
  Style/RedundantParentheses:
4881
4893
  Description: "Checks for parentheses that seem not to serve any purpose."
4882
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
@@ -304,8 +304,8 @@ module RuboCop
304
304
  end
305
305
 
306
306
  def enable_cop?(qualified_cop_name, cop_options)
307
- # If the cop is explicitly enabled, the other checks can be skipped.
308
- return true if cop_options['Enabled'] == true
307
+ # If the cop is explicitly enabled or `Lint/Syntax`, the other checks can be skipped.
308
+ return true if cop_options['Enabled'] == true || qualified_cop_name == 'Lint/Syntax'
309
309
 
310
310
  department = department_of(qualified_cop_name)
311
311
  cop_enabled = cop_options.fetch('Enabled') { !for_all_cops['DisabledByDefault'] }
@@ -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
@@ -94,7 +94,7 @@ module RuboCop
94
94
  private
95
95
 
96
96
  def allowed_gem?(node)
97
- allowed_gems.include?(node.first_argument.value)
97
+ allowed_gems.include?(node.first_argument.str_content)
98
98
  end
99
99
 
100
100
  def allowed_gems
@@ -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
 
@@ -36,17 +36,19 @@ module RuboCop
36
36
  # If the end is on its own line, there is no offense
37
37
  return if begins_its_line?(node.loc.end)
38
38
 
39
- register_offense(node)
39
+ offense_range = offense_range(node)
40
+ return if offense_range.source.lstrip.start_with?(';')
41
+
42
+ register_offense(node, offense_range)
40
43
  end
41
44
 
42
45
  alias on_numblock on_block
43
46
 
44
47
  private
45
48
 
46
- def register_offense(node)
49
+ def register_offense(node, offense_range)
47
50
  add_offense(node.loc.end, message: message(node)) do |corrector|
48
- offense_range = offense_range(node)
49
- replacement = replacement(node)
51
+ replacement = "\n#{offense_range.source.lstrip}"
50
52
 
51
53
  if (heredoc = last_heredoc_argument(node.body))
52
54
  corrector.remove(offense_range)
@@ -72,23 +74,7 @@ module RuboCop
72
74
  end
73
75
 
74
76
  def offense_range(node)
75
- Parser::Source::Range.new(
76
- node.source_range.source_buffer,
77
- node.children.compact.last.source_range.end_pos,
78
- end_of_method_chain(node).source_range.end_pos
79
- )
80
- end
81
-
82
- def replacement(node)
83
- end_with_method_chain = node.loc.end.join(end_of_method_chain(node).source_range.end)
84
-
85
- "\n#{end_with_method_chain.source.strip}"
86
- end
87
-
88
- def end_of_method_chain(node)
89
- return node unless node.parent&.call_type?
90
-
91
- end_of_method_chain(node.parent)
77
+ node.children.compact.last.source_range.end.join(node.loc.end)
92
78
  end
93
79
  end
94
80
  end
@@ -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|
@@ -33,6 +33,10 @@ module RuboCop
33
33
  message << '.' unless message.end_with?('.')
34
34
  message
35
35
  end
36
+
37
+ def find_severity(_range, _severity)
38
+ :fatal
39
+ end
36
40
  end
37
41
  end
38
42
  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