rubocop 1.79.2 → 1.81.1

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +10 -0
  4. data/exe/rubocop +1 -8
  5. data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
  6. data/lib/rubocop/cli.rb +6 -2
  7. data/lib/rubocop/config_loader.rb +3 -1
  8. data/lib/rubocop/config_store.rb +5 -0
  9. data/lib/rubocop/cop/autocorrect_logic.rb +4 -4
  10. data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
  11. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
  12. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +3 -1
  13. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  14. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  15. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  16. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +30 -12
  17. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +1 -1
  18. data/lib/rubocop/cop/layout/line_length.rb +9 -1
  19. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +8 -4
  20. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +8 -0
  21. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  22. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -1
  23. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
  24. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -2
  25. data/lib/rubocop/cop/lint/self_assignment.rb +5 -4
  26. data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
  27. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
  28. data/lib/rubocop/cop/lint/void.rb +7 -0
  29. data/lib/rubocop/cop/message_annotator.rb +1 -1
  30. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  31. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
  32. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  33. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  34. data/lib/rubocop/cop/naming/predicate_method.rb +15 -2
  35. data/lib/rubocop/cop/style/array_intersect.rb +45 -11
  36. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
  37. data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
  38. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  39. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  40. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  41. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  42. data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
  43. data/lib/rubocop/cop/style/redundant_begin.rb +34 -0
  44. data/lib/rubocop/cop/style/redundant_condition.rb +1 -1
  45. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  46. data/lib/rubocop/cop/style/redundant_format.rb +18 -3
  47. data/lib/rubocop/cop/style/redundant_parentheses.rb +14 -11
  48. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
  49. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
  50. data/lib/rubocop/cop/style/safe_navigation.rb +18 -1
  51. data/lib/rubocop/cop/style/string_concatenation.rb +17 -13
  52. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  53. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
  54. data/lib/rubocop/cop/style/unless_else.rb +10 -9
  55. data/lib/rubocop/cop/utils/format_string.rb +10 -0
  56. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  57. data/lib/rubocop/cop/variable_force.rb +9 -7
  58. data/lib/rubocop/formatter/disabled_config_formatter.rb +18 -5
  59. data/lib/rubocop/lsp/diagnostic.rb +21 -20
  60. data/lib/rubocop/lsp/routes.rb +62 -6
  61. data/lib/rubocop/lsp/runtime.rb +2 -2
  62. data/lib/rubocop/lsp/server.rb +2 -2
  63. data/lib/rubocop/lsp/stdin_runner.rb +0 -16
  64. data/lib/rubocop/result_cache.rb +1 -1
  65. data/lib/rubocop/runner.rb +6 -4
  66. data/lib/rubocop/target_finder.rb +9 -9
  67. data/lib/rubocop/target_ruby.rb +10 -1
  68. data/lib/rubocop/version.rb +1 -1
  69. data/lib/rubocop.rb +1 -0
  70. data/lib/ruby_lsp/rubocop/addon.rb +23 -8
  71. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +49 -15
  72. metadata +11 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f846b1bf660549b8197375e32c8219bff1e42888b925976bd3514cd9b32c1d3b
4
- data.tar.gz: 4e862ade4bbe0c24c7734427695733316c893b5278f9868aa88c44255d03461c
3
+ metadata.gz: 2e194323f7062efb4f5777f4039ff475256dfa86b6e95dd63f42f682b3e59a55
4
+ data.tar.gz: 533b2e1fe940252958b245c2d492e0e4b39e37babd9e918fbc3ee505faf030ac
5
5
  SHA512:
6
- metadata.gz: 05ff2343d4ddcdd26cf94325a5b9026fb7468f456b5714da5108030c39fb0fcfb7218848596d07b04817052af53bc225e66b1f81c53f18adb780c2b4bd417012
7
- data.tar.gz: 9c2ce30fbd9467d85bed0aa581beaa225f3e5b032f0e7a5b09130c8eb2918b25e452f3704a9774e8199c1b9e1ccb1b7d10e558a710c7a73d9f9bd86940475ebf
6
+ metadata.gz: c4bbe12c2a1c627b8f4938a3cc52cd3e4c51ee334b8f1d3b2c373dd109c20741721c090b9152ef2e3f8a908f95c90cc7d8021ba48a1354884361219880153581
7
+ data.tar.gz: 0ee2973781e011caf6069a9072f8f6a59196d0430fe3d90af740bc2795423d4ead9fc62ddbe11bfe2be8230b769446e734eebcf461c494bb2921a3fb16135c7c
data/README.md CHANGED
@@ -51,7 +51,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
51
51
  in your `Gemfile`:
52
52
 
53
53
  ```rb
54
- gem 'rubocop', '~> 1.79', require: false
54
+ gem 'rubocop', '~> 1.81', require: false
55
55
  ```
56
56
 
57
57
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -3341,6 +3341,12 @@ Style/ArrayIntersect:
3341
3341
  Safe: false
3342
3342
  VersionAdded: '1.40'
3343
3343
 
3344
+ Style/ArrayIntersectWithSingleElement:
3345
+ Description: 'Use `include?(element)` instead of `intersect?([element])`.'
3346
+ Enabled: 'pending'
3347
+ Safe: false
3348
+ VersionAdded: '1.81'
3349
+
3344
3350
  Style/ArrayJoin:
3345
3351
  Description: 'Use Array#join instead of Array#*.'
3346
3352
  StyleGuide: '#array-join'
@@ -5844,10 +5850,14 @@ Style/TrailingCommaInArguments:
5844
5850
  # parenthesized method calls where each argument is on its own line.
5845
5851
  # If `consistent_comma`, the cop requires a comma after the last argument,
5846
5852
  # for all parenthesized method calls with arguments.
5853
+ # If `diff_comma`, the cop requires a comma after the last argument, but only
5854
+ # when that argument is followed by an immediate newline, even if
5855
+ # there is an inline comment.
5847
5856
  EnforcedStyleForMultiline: no_comma
5848
5857
  SupportedStylesForMultiline:
5849
5858
  - comma
5850
5859
  - consistent_comma
5860
+ - diff_comma
5851
5861
  - no_comma
5852
5862
 
5853
5863
  Style/TrailingCommaInArrayLiteral:
data/exe/rubocop CHANGED
@@ -12,13 +12,6 @@ if RuboCop::Server.running?
12
12
  exit_status = RuboCop::Server::ClientCommand::Exec.new.run
13
13
  else
14
14
  require 'rubocop'
15
-
16
- cli = RuboCop::CLI.new
17
-
18
- time_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
19
- exit_status = cli.run
20
- elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - time_start
21
-
22
- puts "Finished in #{elapsed_time} seconds" if cli.options[:debug] || cli.options[:display_time]
15
+ exit_status = RuboCop::CLI.new.run
23
16
  end
24
17
  exit exit_status
@@ -83,7 +83,7 @@ module RuboCop
83
83
  execute_runner
84
84
  @options.delete(:only)
85
85
  @config_store = ConfigStore.new
86
- @config_store.options_config = @options[:config] if @options[:config]
86
+ @config_store.apply_options!(@options)
87
87
  # Save the todo configuration of the LineLength cop.
88
88
  File.read(AUTO_GENERATED_FILE).lines.drop_while { |line| line.start_with?('#') }.join
89
89
  end
@@ -99,7 +99,7 @@ module RuboCop
99
99
 
100
100
  def reset_config_and_auto_gen_file
101
101
  @config_store = ConfigStore.new
102
- @config_store.options_config = @options[:config] if @options[:config]
102
+ @config_store.apply_options!(@options)
103
103
  File.open(AUTO_GENERATED_FILE, 'w') {} # create or truncate if exists
104
104
  add_inheritance_from_auto_generated_file(@options[:config])
105
105
  end
data/lib/rubocop/cli.rb CHANGED
@@ -37,6 +37,8 @@ module RuboCop
37
37
  #
38
38
  # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
39
39
  def run(args = ARGV)
40
+ time_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
41
+
40
42
  @options, paths = Options.new.parse(args)
41
43
  @env = Environment.new(@options, @config_store, paths)
42
44
 
@@ -72,6 +74,9 @@ module RuboCop
72
74
  warn e.message
73
75
  warn e.backtrace
74
76
  STATUS_ERROR
77
+ ensure
78
+ elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - time_start
79
+ puts "Finished in #{elapsed_time} seconds" if @options[:debug] || @options[:display_time]
75
80
  end
76
81
  # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
77
82
 
@@ -159,8 +164,7 @@ module RuboCop
159
164
  set_options_to_pending_cops_reporter
160
165
  handle_editor_mode
161
166
 
162
- @config_store.options_config = @options[:config] if @options[:config]
163
- @config_store.force_default_config! if @options[:force_default_config]
167
+ @config_store.apply_options!(@options)
164
168
 
165
169
  handle_exiting_options
166
170
 
@@ -75,7 +75,9 @@ module RuboCop
75
75
 
76
76
  puts "configuration from #{absolute_path}" if debug?
77
77
 
78
- raise(TypeError, "Malformed configuration in #{absolute_path}") unless hash.is_a?(Hash)
78
+ unless hash.is_a?(Hash)
79
+ raise(ValidationError, "Malformed configuration in #{absolute_path}")
80
+ end
79
81
 
80
82
  hash
81
83
  end
@@ -25,6 +25,11 @@ module RuboCop
25
25
  @validated = true
26
26
  end
27
27
 
28
+ def apply_options!(options)
29
+ self.options_config = options[:config] if options[:config]
30
+ force_default_config! if options[:force_default_config]
31
+ end
32
+
28
33
  def options_config=(options_config)
29
34
  loaded_config = ConfigLoader.load_file(options_config)
30
35
  @options_config = ConfigLoader.merge_with_default(loaded_config, options_config)
@@ -35,8 +35,8 @@ module RuboCop
35
35
  # `false` is the same as `disabled` for backward compatibility.
36
36
  return false if ['disabled', false].include?(cop_config['AutoCorrect'])
37
37
 
38
- # When LSP is enabled, it is considered as editing source code,
39
- # and autocorrection with `AutoCorrect: contextual` will not be performed.
38
+ # When LSP is enabled or the `--editor-mode` option is on, it is considered as editing
39
+ # source code, and autocorrection with `AutoCorrect: contextual` will not be performed.
40
40
  return false if contextual_autocorrect? && LSP.enabled?
41
41
 
42
42
  # :safe_autocorrect is a derived option based on several command-line
@@ -94,7 +94,7 @@ module RuboCop
94
94
  end
95
95
 
96
96
  def surrounding_heredoc?(node)
97
- node.type?(:str, :dstr, :xstr) && node.heredoc?
97
+ node.any_str_type? && node.heredoc?
98
98
  end
99
99
 
100
100
  def heredoc_range(node)
@@ -106,7 +106,7 @@ module RuboCop
106
106
  end
107
107
 
108
108
  def string_continuation?(node)
109
- node.type?(:str, :dstr, :xstr) && node.source.match?(/\\\s*$/)
109
+ node.any_str_type? && node.source.match?(/\\\s*$/)
110
110
  end
111
111
 
112
112
  def multiline_string?(node)
@@ -29,10 +29,13 @@ module RuboCop
29
29
  def align_end(corrector, processed_source, node, align_to)
30
30
  @processed_source = processed_source
31
31
  whitespace = whitespace_range(node)
32
- return false unless whitespace.source.strip.empty?
33
-
34
32
  column = alignment_column(align_to)
35
- corrector.replace(whitespace, ' ' * column)
33
+
34
+ if whitespace.source.strip.empty?
35
+ corrector.replace(whitespace, ' ' * column)
36
+ else
37
+ corrector.insert_after(whitespace, "\n#{' ' * column}")
38
+ end
36
39
  end
37
40
 
38
41
  private
@@ -54,7 +57,7 @@ module RuboCop
54
57
  def inside_string_ranges(node)
55
58
  return [] unless node.is_a?(Parser::AST::Node)
56
59
 
57
- node.each_node(:str, :dstr, :xstr).filter_map { |n| inside_string_range(n) }
60
+ node.each_node(:any_str).filter_map { |n| inside_string_range(n) }
58
61
  end
59
62
 
60
63
  def inside_string_range(node)
@@ -6,7 +6,7 @@ module RuboCop
6
6
  class ForToEachCorrector
7
7
  extend NodePattern::Macros
8
8
 
9
- CORRECTION = '%<collection>s.each do |%<argument>s|'
9
+ CORRECTION = '%<collection>s%<dot>seach do |%<argument>s|'
10
10
 
11
11
  def initialize(for_node)
12
12
  @for_node = for_node
@@ -25,7 +25,12 @@ module RuboCop
25
25
  attr_reader :for_node, :variable_node, :collection_node
26
26
 
27
27
  def correction
28
- format(CORRECTION, collection: collection_source, argument: variable_node.source)
28
+ format(
29
+ CORRECTION,
30
+ collection: collection_source,
31
+ dot: collection_node.csend_type? ? '&.' : '.',
32
+ argument: variable_node.source
33
+ )
29
34
  end
30
35
 
31
36
  def collection_source
@@ -30,6 +30,8 @@ module RuboCop
30
30
  any_block: %i[block numblock itblock],
31
31
  any_def: %i[def defs],
32
32
  any_match_pattern: %i[match_pattern match_pattern_p],
33
+ any_str: %i[str dstr xstr],
34
+ any_sym: %i[sym dsym],
33
35
  argument: %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg forward_arg shadowarg],
34
36
  boolean: %i[true false],
35
37
  call: %i[send csend],
@@ -210,7 +212,7 @@ module RuboCop
210
212
  # A heredoc can be a `dstr` without interpolation, but if there is interpolation
211
213
  # there'll be a `begin` node, in which case, we cannot evaluate the pattern.
212
214
  def acceptable_heredoc?(node)
213
- node.type?(:str, :dstr) && node.heredoc? && node.each_child_node(:begin).none?
215
+ node.any_str_type? && node.heredoc? && node.each_child_node(:begin).none?
214
216
  end
215
217
 
216
218
  def process_pattern(pattern_node)
@@ -76,7 +76,7 @@ module RuboCop
76
76
  end
77
77
 
78
78
  def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend
79
- new_identifier = node.first_argument
79
+ return unless (new_identifier = node.first_argument)
80
80
  return unless new_identifier.basic_literal?
81
81
 
82
82
  new_identifier = new_identifier.value
@@ -357,7 +357,7 @@ module RuboCop
357
357
  end
358
358
 
359
359
  def find_heredoc(node)
360
- node.each_node(:str, :dstr, :xstr).find(&:heredoc?)
360
+ node.each_node(:any_str).find(&:heredoc?)
361
361
  end
362
362
 
363
363
  def buffer
@@ -120,7 +120,7 @@ module RuboCop
120
120
  end
121
121
 
122
122
  def heredoc?(node)
123
- node.type?(:str, :dstr) && node.heredoc?
123
+ node.any_str_type? && node.heredoc?
124
124
  end
125
125
 
126
126
  def end_range(node)
@@ -76,28 +76,40 @@ module RuboCop
76
76
  # # good
77
77
  # class ErrorA < BaseError; end
78
78
  # class ErrorB < BaseError; end
79
- # class ErrorC < BaseError; end
80
79
  #
81
80
  # # good
82
81
  # class ErrorA < BaseError; end
83
82
  #
84
83
  # class ErrorB < BaseError; end
85
84
  #
86
- # class ErrorC < BaseError; end
85
+ # # good - DefLikeMacros: [memoize]
86
+ # memoize :attribute_a
87
+ # memoize :attribute_b
88
+ #
89
+ # # good
90
+ # memoize :attribute_a
91
+ #
92
+ # memoize :attribute_b
87
93
  #
88
94
  # @example AllowAdjacentOneLineDefs: false
89
95
  #
90
96
  # # bad
91
97
  # class ErrorA < BaseError; end
92
98
  # class ErrorB < BaseError; end
93
- # class ErrorC < BaseError; end
94
99
  #
95
100
  # # good
96
101
  # class ErrorA < BaseError; end
97
102
  #
98
103
  # class ErrorB < BaseError; end
99
104
  #
100
- # class ErrorC < BaseError; end
105
+ # # bad - DefLikeMacros: [memoize]
106
+ # memoize :attribute_a
107
+ # memoize :attribute_b
108
+ #
109
+ # # good
110
+ # memoize :attribute_a
111
+ #
112
+ # memoize :attribute_b
101
113
  #
102
114
  class EmptyLineBetweenDefs < Base
103
115
  include RangeHelp
@@ -158,6 +170,8 @@ module RuboCop
158
170
  def def_location(correction_node)
159
171
  if correction_node.any_block_type?
160
172
  correction_node.source_range.join(correction_node.children.first.source_range)
173
+ elsif correction_node.send_type?
174
+ correction_node.source_range
161
175
  else
162
176
  correction_node.loc.keyword.join(correction_node.loc.name)
163
177
  end
@@ -175,8 +189,14 @@ module RuboCop
175
189
  end
176
190
 
177
191
  def macro_candidate?(node)
178
- node.any_block_type? && node.children.first.macro? &&
179
- empty_line_between_macros.include?(node.children.first.method_name)
192
+ macro_candidate = if node.any_block_type?
193
+ node.send_node
194
+ elsif node.send_type?
195
+ node
196
+ end
197
+ return false unless macro_candidate
198
+
199
+ macro_candidate.macro? && empty_line_between_macros.include?(macro_candidate.method_name)
180
200
  end
181
201
 
182
202
  def method_candidate?(node)
@@ -240,7 +260,9 @@ module RuboCop
240
260
  end
241
261
 
242
262
  def def_start(node)
243
- if node.any_block_type? && node.children.first.send_type?
263
+ node = node.send_node if node.any_block_type?
264
+
265
+ if node.send_type?
244
266
  node.source_range.line
245
267
  else
246
268
  node.loc.keyword.line
@@ -252,11 +274,7 @@ module RuboCop
252
274
  end
253
275
 
254
276
  def end_loc(node)
255
- if node.any_def_type? && node.endless?
256
- node.source_range.end
257
- else
258
- node.loc.end
259
- end
277
+ node.source_range.end
260
278
  end
261
279
 
262
280
  def autocorrect_remove_lines(corrector, newline_pos, count)
@@ -38,7 +38,7 @@ module RuboCop
38
38
  RESTRICT_ON_SEND = MODULE_INCLUSION_METHODS
39
39
 
40
40
  def on_send(node)
41
- return if node.receiver
41
+ return if node.receiver || node.arguments.empty?
42
42
  return if node.parent&.type?(:send, :any_block)
43
43
 
44
44
  return if next_line_empty_or_enable_directive_comment?(node.last_line)
@@ -134,6 +134,7 @@ module RuboCop
134
134
 
135
135
  def check_for_breakable_block(block_node)
136
136
  return unless block_node.single_line?
137
+ return if receiver_contains_heredoc?(block_node)
137
138
 
138
139
  line_index = block_node.loc.line - 1
139
140
  range = breakable_block_range(block_node)
@@ -321,7 +322,7 @@ module RuboCop
321
322
  def extract_heredocs(ast)
322
323
  return [] unless ast
323
324
 
324
- ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node|
325
+ ast.each_node(:any_str).select(&:heredoc?).map do |node|
325
326
  body = node.location.heredoc_body
326
327
  delimiter = node.location.heredoc_end.source.strip
327
328
  [body.first_line...body.last_line, delimiter]
@@ -341,6 +342,13 @@ module RuboCop
341
342
  heredocs.any? { |range, _delimiter| range.cover?(line_number) }
342
343
  end
343
344
 
345
+ def receiver_contains_heredoc?(node)
346
+ return false unless (receiver = node.receiver)
347
+ return true if receiver.any_str_type? && receiver.heredoc?
348
+
349
+ receiver.each_descendant(:any_str).any?(&:heredoc?)
350
+ end
351
+
344
352
  def check_directive_line(line, line_index)
345
353
  length_without_directive = line_length_without_directive(line)
346
354
  return if length_without_directive <= max
@@ -10,6 +10,8 @@ module RuboCop
10
10
  # condition, an explicit `return` statement, etc. In other contexts, the second operand should
11
11
  # be indented regardless of enforced style.
12
12
  #
13
+ # In both styles, operators should be aligned when an assignment begins on the next line.
14
+ #
13
15
  # @example EnforcedStyle: aligned (default)
14
16
  # # bad
15
17
  # if a +
@@ -100,10 +102,12 @@ module RuboCop
100
102
  return true if begins_its_line?(assignment_rhs.source_range)
101
103
  end
102
104
 
103
- given_style == :aligned &&
104
- (kw_node_with_special_indentation(node) ||
105
- assignment_node ||
106
- argument_in_method_call(node, :with_or_without_parentheses))
105
+ return false unless given_style == :aligned
106
+ return true if kw_node_with_special_indentation(node) || assignment_node
107
+
108
+ node = argument_in_method_call(node, :with_or_without_parentheses)
109
+
110
+ node.respond_to?(:def_modifier?) && !node.def_modifier?
107
111
  end
108
112
 
109
113
  def message(node, lhs, rhs)
@@ -194,6 +194,14 @@ module RuboCop
194
194
  def alignment_location(alignment_node)
195
195
  if begin_end_alignment_style == 'start_of_line'
196
196
  start_line_range(alignment_node)
197
+ elsif alignment_node.any_block_type?
198
+ # If the alignment node is a block, the `rescue`/`ensure` keyword should
199
+ # be aligned to the start of the block. It is possible that the block's
200
+ # `send_node` spans multiple lines, in which case it should align to the
201
+ # start of the last line.
202
+ send_node = alignment_node.send_node
203
+ range = processed_source.buffer.line_range(send_node.last_line)
204
+ range.adjust(begin_pos: range.source =~ /\S/)
197
205
  else
198
206
  alignment_node.source_range
199
207
  end
@@ -113,7 +113,7 @@ module RuboCop
113
113
  return [] unless ast
114
114
 
115
115
  heredocs = []
116
- ast.each_node(:str, :dstr, :xstr) do |node|
116
+ ast.each_node(:any_str) do |node|
117
117
  next unless node.heredoc?
118
118
 
119
119
  body = node.location.heredoc_body
@@ -118,8 +118,11 @@ module RuboCop
118
118
 
119
119
  def replacement_args(node)
120
120
  algorithm_constant, = algorithm_const(node)
121
- algorithm_name = algorithm_name(algorithm_constant)
121
+ if algorithm_constant.source == 'OpenSSL::Cipher::Cipher'
122
+ return node.first_argument.source
123
+ end
122
124
 
125
+ algorithm_name = algorithm_name(algorithm_constant)
123
126
  if openssl_class(algorithm_constant) == 'OpenSSL::Cipher'
124
127
  build_cipher_arguments(node, algorithm_name, node.arguments.empty?)
125
128
  else
@@ -24,8 +24,6 @@ module RuboCop
24
24
 
25
25
  MSG_REPEATED_ELEMENT = 'Duplicate element inside regexp character class'
26
26
 
27
- OCTAL_DIGITS_AFTER_ESCAPE = 2
28
-
29
27
  def on_regexp(node)
30
28
  each_repeated_character_class_element_loc(node) do |loc|
31
29
  add_offense(loc, message: MSG_REPEATED_ELEMENT) do |corrector|
@@ -40,9 +38,9 @@ module RuboCop
40
38
 
41
39
  seen = Set.new
42
40
  group_expressions(node, expr.expressions) do |group|
43
- group_source = group.map(&:to_s).join
41
+ group_source = group.to_s
44
42
 
45
- yield source_range(group) if seen.include?(group_source)
43
+ yield group.expression if seen.include?(group_source)
46
44
 
47
45
  seen << group_source
48
46
  end
@@ -52,40 +50,13 @@ module RuboCop
52
50
  private
53
51
 
54
52
  def group_expressions(node, expressions)
55
- # Create a mutable list to simplify state tracking while we iterate.
56
- expressions = expressions.to_a
57
-
58
- until expressions.empty?
59
- # With we may need to compose a group of multiple expressions.
60
- group = [expressions.shift]
61
- next if within_interpolation?(node, group.first)
62
-
63
- # With regexp_parser < 2.7 escaped octal sequences may be up to 3
64
- # separate expressions ("\\0", "0", "1").
65
- pop_octal_digits(group, expressions) if escaped_octal?(group.first.to_s)
66
-
67
- yield(group)
68
- end
69
- end
70
-
71
- def pop_octal_digits(current_child, expressions)
72
- OCTAL_DIGITS_AFTER_ESCAPE.times do
73
- next_child = expressions.first
74
- break unless octal?(next_child.to_s)
53
+ expressions.each do |expression|
54
+ next if within_interpolation?(node, expression)
75
55
 
76
- current_child << expressions.shift
56
+ yield(expression)
77
57
  end
78
58
  end
79
59
 
80
- def source_range(children)
81
- return children.first.expression if children.size == 1
82
-
83
- range_between(
84
- children.first.expression.begin_pos,
85
- children.last.expression.begin_pos + children.last.to_s.length
86
- )
87
- end
88
-
89
60
  def skip_expression?(expr)
90
61
  expr.type != :set || expr.token == :intersection
91
62
  end
@@ -99,14 +70,6 @@ module RuboCop
99
70
  interpolation_locs(node).any? { |il| il.overlaps?(parse_tree_child_loc) }
100
71
  end
101
72
 
102
- def escaped_octal?(string)
103
- string.length == 2 && string[0] == '\\' && octal?(string[1])
104
- end
105
-
106
- def octal?(char)
107
- ('0'..'7').cover?(char)
108
- end
109
-
110
73
  def interpolation_locs(node)
111
74
  @interpolation_locs ||= {}
112
75
 
@@ -52,10 +52,9 @@ module RuboCop
52
52
  each_missing_enable do |cop, line_range|
53
53
  next if acceptable_range?(cop, line_range)
54
54
 
55
- range = source_range(processed_source.buffer, line_range.min, 0..0)
56
55
  comment = processed_source.comment_at_line(line_range.begin)
57
56
 
58
- add_offense(range, message: message(cop, comment))
57
+ add_offense(comment, message: message(cop, comment))
59
58
  end
60
59
  end
61
60
 
@@ -45,7 +45,7 @@ module RuboCop
45
45
  return if allow_rbs_inline_annotation? && rbs_inline_annotation?(node.receiver)
46
46
 
47
47
  if node.method?(:[]=)
48
- handle_key_assignment(node) if node.arguments.size == 2
48
+ handle_key_assignment(node)
49
49
  elsif node.assignment_method?
50
50
  handle_attribute_assignment(node) if node.arguments.size == 1
51
51
  end
@@ -105,12 +105,13 @@ module RuboCop
105
105
  end
106
106
 
107
107
  def handle_key_assignment(node)
108
- value_node = node.arguments[1]
108
+ value_node = node.last_argument
109
+ node_arguments = node.arguments[0...-1]
109
110
 
110
111
  if value_node.send_type? && value_node.method?(:[]) &&
111
112
  node.receiver == value_node.receiver &&
112
- !node.first_argument.call_type? &&
113
- node.first_argument == value_node.first_argument
113
+ node_arguments.none?(&:call_type?) &&
114
+ node_arguments == value_node.arguments
114
115
  add_offense(node)
115
116
  end
116
117
  end
@@ -125,13 +125,13 @@ module RuboCop
125
125
  next false if assignment_node.shorthand_asgn?
126
126
  next false unless assignment_node.parent
127
127
 
128
- node_within_block_or_conditional =
129
- node_within_block_or_conditional?(assignment_node.parent, argument.scope.node)
128
+ conditional_assignment =
129
+ conditional_assignment?(assignment_node.parent, argument.scope.node)
130
130
 
131
131
  unless uses_var?(assignment_node, argument.name)
132
132
  # It's impossible to decide whether a branch or block is executed,
133
133
  # so the precise reassignment location is undecidable.
134
- next false if node_within_block_or_conditional
134
+ next false if conditional_assignment
135
135
 
136
136
  yield(assignment.node, location_known)
137
137
  break
@@ -147,13 +147,13 @@ module RuboCop
147
147
  node.source_range.begin_pos
148
148
  end
149
149
 
150
- # Check whether the given node is nested into block or conditional.
150
+ # Check whether the given node is always executed or not
151
151
  #
152
- def node_within_block_or_conditional?(node, stop_search_node)
152
+ def conditional_assignment?(node, stop_search_node)
153
153
  return false if node == stop_search_node
154
154
 
155
- node.conditional? || node.block_type? ||
156
- node_within_block_or_conditional?(node.parent, stop_search_node)
155
+ node.conditional? || node.type?(:block, :rescue) ||
156
+ conditional_assignment?(node.parent, stop_search_node)
157
157
  end
158
158
 
159
159
  # Get argument references without assignments' references
@@ -17,6 +17,7 @@ module RuboCop
17
17
  #
18
18
  # # good
19
19
  # CGI.escape('http://example.com')
20
+ # URI.encode_uri_component(uri) # Since Ruby 3.1
20
21
  # URI.encode_www_form([['example', 'param'], ['lang', 'en']])
21
22
  # URI.encode_www_form(page: 10, locale: 'en')
22
23
  # URI.encode_www_form_component('http://example.com')
@@ -27,6 +28,7 @@ module RuboCop
27
28
  #
28
29
  # # good
29
30
  # CGI.unescape(enc_uri)
31
+ # URI.decode_uri_component(uri) # Since Ruby 3.1
30
32
  # URI.decode_www_form(enc_uri)
31
33
  # URI.decode_www_form_component(enc_uri)
32
34
  class UriEscapeUnescape < Base