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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39e28428d694fdd8cf940d5e3494f62a0266b3ffd41e823cf50131d16b45cf2a
4
- data.tar.gz: 75eadde7a899742fcb5f08c4b3cd845658dd64087dd80c0dad1bda5fef30804a
3
+ metadata.gz: b29d7b756beef5910c678a948e4c5e6a852978e39b6d4436eadaf9087731a742
4
+ data.tar.gz: 139e8da95a5f9f5d84c6286bfccad4f41aa93790c7387b5e9737f5e8a3b0dd6b
5
5
  SHA512:
6
- metadata.gz: 21bba5ce4024ff5c66695c56e4261fbefdedc8b293396b8d0687f836d4a56d0f53ae307435cb714a3e4b059def785a1d9862504910f94fe1815223b6036b727b
7
- data.tar.gz: aec136f1e174b57ba2ae6307a3fdeda0257614bcaaf77a6d5c5eca9733cfd185970dd998ecc95a6691d9aa8310419cd69679ba9b021f5b8284b9d12ef675dcc0
6
+ metadata.gz: 4c642f1f939b5fd5c91669422f4cc901bc889b1999d0f44ab6e576a4b9aa84c5a0f77540a8b9ad613dd42d71c560699b7218fa5f5d9320721bb8f2ccf9b97aee
7
+ data.tar.gz: 7980c7461a8e5d7d7cb1e19bdcba86696490b3c016adaca99011c100373e1db5944fb20642d270db11f68aa5604dc9b26fcd25733f4285e777f5ab8a504b025f
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.50', 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
@@ -1016,8 +1016,6 @@ Layout/LineContinuationLeadingSpace:
1016
1016
  Layout/LineContinuationSpacing:
1017
1017
  Description: 'Checks the spacing in front of backslash in line continuations.'
1018
1018
  Enabled: pending
1019
- AutoCorrect: true
1020
- SafeAutoCorrect: true
1021
1019
  VersionAdded: '1.31'
1022
1020
  EnforcedStyle: space
1023
1021
  SupportedStyles:
@@ -1752,6 +1750,11 @@ Lint/DuplicateMagicComment:
1752
1750
  Enabled: pending
1753
1751
  VersionAdded: '1.37'
1754
1752
 
1753
+ Lint/DuplicateMatchPattern:
1754
+ Description: 'Do not repeat patterns in `in` keywords.'
1755
+ Enabled: pending
1756
+ VersionAdded: '1.50'
1757
+
1755
1758
  Lint/DuplicateMethods:
1756
1759
  Description: 'Check for duplicate method definitions.'
1757
1760
  Enabled: true
@@ -2779,7 +2782,7 @@ Naming/InclusiveLanguage:
2779
2782
  Description: 'Recommend the use of inclusive language instead of problematic terms.'
2780
2783
  Enabled: false
2781
2784
  VersionAdded: '1.18'
2782
- VersionChanged: '1.21'
2785
+ VersionChanged: '1.49'
2783
2786
  CheckIdentifiers: true
2784
2787
  CheckConstants: true
2785
2788
  CheckVariables: true
@@ -3277,7 +3280,7 @@ Style/ClassAndModuleChildren:
3277
3280
  StyleGuide: '#namespace-definition'
3278
3281
  # Moving from compact to nested children requires knowledge of whether the
3279
3282
  # 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
3283
+ # verification that the outer parent is defined elsewhere. RuboCop does not
3281
3284
  # have the knowledge to perform either operation safely and thus requires
3282
3285
  # manual oversight.
3283
3286
  SafeAutoCorrect: false
@@ -3508,6 +3511,12 @@ Style/Copyright:
3508
3511
  Notice: '^Copyright (\(c\) )?2[0-9]{3} .+'
3509
3512
  AutocorrectNotice: ''
3510
3513
 
3514
+ Style/DataInheritance:
3515
+ Description: 'Checks for inheritance from Data.define.'
3516
+ StyleGuide: '#no-extend-data-define'
3517
+ Enabled: pending
3518
+ VersionAdded: '1.49'
3519
+
3511
3520
  Style/DateTime:
3512
3521
  Description: 'Use Time over DateTime.'
3513
3522
  StyleGuide: '#date-time'
@@ -3737,7 +3746,6 @@ Style/FileEmpty:
3737
3746
  Prefer to use `File.empty?('path/to/file')` when checking if a file is empty.
3738
3747
  Enabled: pending
3739
3748
  Safe: false
3740
- SafeAutoCorrect: false
3741
3749
  VersionAdded: '1.48'
3742
3750
 
3743
3751
  Style/FileRead:
@@ -4565,11 +4573,10 @@ Style/NumericPredicate:
4565
4573
  Checks for the use of predicate- or comparison methods for
4566
4574
  numeric comparisons.
4567
4575
  StyleGuide: '#predicate-methods'
4568
- Safe: false
4569
4576
  # This will change to a new method call which isn't guaranteed to be on the
4570
4577
  # object. Switching these methods has to be done with knowledge of the types
4571
4578
  # of the variables which rubocop doesn't have.
4572
- SafeAutoCorrect: false
4579
+ Safe: false
4573
4580
  Enabled: true
4574
4581
  VersionAdded: '0.42'
4575
4582
  VersionChanged: '0.59'
@@ -4878,6 +4885,11 @@ Style/RedundantInterpolation:
4878
4885
  VersionAdded: '0.76'
4879
4886
  VersionChanged: '1.30'
4880
4887
 
4888
+ Style/RedundantLineContinuation:
4889
+ Description: 'Check for redundant line continuation.'
4890
+ Enabled: pending
4891
+ VersionAdded: '1.49'
4892
+
4881
4893
  Style/RedundantParentheses:
4882
4894
  Description: "Checks for parentheses that seem not to serve any purpose."
4883
4895
  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
@@ -23,6 +23,14 @@ module RuboCop
23
23
  class << self
24
24
  include FileFinder
25
25
 
26
+ PENDING_BANNER = <<~BANNER
27
+ The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
28
+
29
+ Please also note that you can opt-in to new cops by default by adding this to your config:
30
+ AllCops:
31
+ NewCops: enable
32
+ BANNER
33
+
26
34
  attr_accessor :debug, :ignore_parent_exclusion, :disable_pending_cops, :enable_pending_cops,
27
35
  :ignore_unrecognized_cops
28
36
  attr_writer :default_configuration
@@ -165,14 +173,6 @@ module RuboCop
165
173
  ConfigFinder.project_root
166
174
  end
167
175
 
168
- PENDING_BANNER = <<~BANNER
169
- The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
170
-
171
- Please also note that you can opt-in to new cops by default by adding this to your config:
172
- AllCops:
173
- NewCops: enable
174
- BANNER
175
-
176
176
  def warn_on_pending_cops(pending_cops)
177
177
  warn Rainbow(PENDING_BANNER).yellow
178
178
 
@@ -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
 
@@ -159,6 +159,7 @@ module RuboCop
159
159
  previous = index
160
160
  end
161
161
  end
162
+ alias on_sclass on_class
162
163
 
163
164
  private
164
165
 
@@ -137,7 +137,7 @@ module RuboCop
137
137
  end
138
138
 
139
139
  def current_token(comment)
140
- processed_source.find_token { |token| token.pos == comment.source_range }
140
+ processed_source.tokens.find { |token| token.pos == comment.source_range }
141
141
  end
142
142
 
143
143
  def previous_token(node)
@@ -31,7 +31,7 @@ module RuboCop
31
31
  return unless processed_source.raw_source.include?("\n\n\n")
32
32
 
33
33
  lines = Set.new
34
- processed_source.each_token { |token| lines << token.line }
34
+ processed_source.tokens.each { |token| lines << token.line }
35
35
 
36
36
  each_extra_empty_line(lines.sort) do |range|
37
37
  add_offense(range) do |corrector|
@@ -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
@@ -34,7 +34,7 @@ module RuboCop
34
34
  private
35
35
 
36
36
  def first_token
37
- processed_source.find_token { |t| !t.text.start_with?('#') }
37
+ processed_source.tokens.find { |t| !t.text.start_with?('#') }
38
38
  end
39
39
 
40
40
  def space_before(token)
@@ -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