rubocop 1.78.0 → 1.81.6

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 (117) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -3
  3. data/config/default.yml +46 -21
  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/node_type_group.rb +3 -2
  14. data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
  15. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
  16. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  17. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  18. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +30 -12
  19. data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
  20. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
  21. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  22. data/lib/rubocop/cop/layout/hash_alignment.rb +0 -5
  23. data/lib/rubocop/cop/layout/line_length.rb +9 -1
  24. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +8 -4
  25. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +8 -0
  26. data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
  27. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -0
  28. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  29. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
  30. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
  31. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -1
  32. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
  33. data/lib/rubocop/cop/lint/empty_interpolation.rb +11 -0
  34. data/lib/rubocop/cop/lint/literal_as_condition.rb +12 -0
  35. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -2
  36. data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
  37. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
  38. data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
  39. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
  40. data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
  41. data/lib/rubocop/cop/lint/self_assignment.rb +6 -5
  42. data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
  43. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
  44. data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
  45. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
  46. data/lib/rubocop/cop/lint/void.rb +7 -0
  47. data/lib/rubocop/cop/message_annotator.rb +1 -1
  48. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  49. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
  50. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  51. data/lib/rubocop/cop/naming/method_name.rb +40 -1
  52. data/lib/rubocop/cop/naming/predicate_method.rb +19 -3
  53. data/lib/rubocop/cop/security/json_load.rb +33 -11
  54. data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -1
  55. data/lib/rubocop/cop/style/accessor_grouping.rb +13 -1
  56. data/lib/rubocop/cop/style/arguments_forwarding.rb +11 -17
  57. data/lib/rubocop/cop/style/array_intersect.rb +99 -35
  58. data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
  59. data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
  60. data/lib/rubocop/cop/style/block_delimiters.rb +1 -1
  61. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -4
  62. data/lib/rubocop/cop/style/dig_chain.rb +1 -1
  63. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  64. data/lib/rubocop/cop/style/endless_method.rb +15 -2
  65. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  66. data/lib/rubocop/cop/style/exponential_notation.rb +1 -0
  67. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  68. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  69. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  70. data/lib/rubocop/cop/style/it_assignment.rb +69 -12
  71. data/lib/rubocop/cop/style/it_block_parameter.rb +2 -0
  72. data/lib/rubocop/cop/style/map_to_hash.rb +1 -3
  73. data/lib/rubocop/cop/style/map_to_set.rb +1 -3
  74. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -4
  75. data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
  76. data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
  77. data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
  78. data/lib/rubocop/cop/style/redundant_begin.rb +34 -0
  79. data/lib/rubocop/cop/style/redundant_condition.rb +1 -1
  80. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  81. data/lib/rubocop/cop/style/redundant_format.rb +26 -5
  82. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  83. data/lib/rubocop/cop/style/redundant_interpolation.rb +11 -2
  84. data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -1
  85. data/lib/rubocop/cop/style/redundant_parentheses.rb +29 -11
  86. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
  87. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
  88. data/lib/rubocop/cop/style/safe_navigation.rb +20 -1
  89. data/lib/rubocop/cop/style/semicolon.rb +20 -5
  90. data/lib/rubocop/cop/style/single_line_methods.rb +3 -3
  91. data/lib/rubocop/cop/style/sole_nested_conditional.rb +30 -1
  92. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
  93. data/lib/rubocop/cop/style/string_concatenation.rb +17 -13
  94. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  95. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
  96. data/lib/rubocop/cop/style/unless_else.rb +10 -9
  97. data/lib/rubocop/cop/utils/format_string.rb +10 -0
  98. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  99. data/lib/rubocop/cop/variable_force.rb +25 -8
  100. data/lib/rubocop/cops_documentation_generator.rb +5 -4
  101. data/lib/rubocop/formatter/disabled_config_formatter.rb +18 -5
  102. data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
  103. data/lib/rubocop/formatter/pacman_formatter.rb +1 -0
  104. data/lib/rubocop/lsp/diagnostic.rb +21 -20
  105. data/lib/rubocop/lsp/routes.rb +65 -9
  106. data/lib/rubocop/lsp/runtime.rb +2 -2
  107. data/lib/rubocop/lsp/server.rb +2 -2
  108. data/lib/rubocop/lsp/stdin_runner.rb +0 -16
  109. data/lib/rubocop/result_cache.rb +14 -12
  110. data/lib/rubocop/runner.rb +6 -4
  111. data/lib/rubocop/target_finder.rb +9 -9
  112. data/lib/rubocop/target_ruby.rb +10 -1
  113. data/lib/rubocop/version.rb +1 -1
  114. data/lib/rubocop.rb +3 -0
  115. data/lib/ruby_lsp/rubocop/addon.rb +23 -8
  116. data/lib/ruby_lsp/rubocop/runtime_adapter.rb +49 -15
  117. metadata +9 -6
@@ -15,7 +15,7 @@ module RuboCop
15
15
  module LSP
16
16
  # Routes for Language Server Protocol of RuboCop.
17
17
  # @api private
18
- class Routes
18
+ class Routes # rubocop:disable Metrics/ClassLength
19
19
  CONFIGURATION_FILE_PATTERNS = [
20
20
  RuboCop::ConfigFinder::DOTFILE,
21
21
  RuboCop::CLI::Command::AutoGenerateConfig::AUTO_GENERATED_FILE
@@ -42,6 +42,7 @@ module RuboCop
42
42
 
43
43
  handle 'initialize' do |request|
44
44
  initialization_options = extract_initialization_options_from(request)
45
+ @position_encoding = initialization_options[:position_encoding]
45
46
 
46
47
  @server.configure(initialization_options)
47
48
 
@@ -51,9 +52,10 @@ module RuboCop
51
52
  capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new(
52
53
  document_formatting_provider: true,
53
54
  text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new(
54
- change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL,
55
+ change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::INCREMENTAL,
55
56
  open_close: true
56
- )
57
+ ),
58
+ position_encoding: @position_encoding
57
59
  )
58
60
  )
59
61
  )
@@ -76,7 +78,12 @@ module RuboCop
76
78
 
77
79
  handle 'textDocument/didChange' do |request|
78
80
  params = request[:params]
79
- result = diagnostic(params[:textDocument][:uri], params[:contentChanges][0][:text])
81
+ file_uri = params[:textDocument][:uri]
82
+ text = @text_cache[file_uri]
83
+ params[:contentChanges].each do |content|
84
+ text = change_text(text, content[:text], content[:range])
85
+ end
86
+ result = diagnostic(file_uri, text)
80
87
  @server.write(result)
81
88
  end
82
89
 
@@ -179,14 +186,26 @@ module RuboCop
179
186
 
180
187
  def extract_initialization_options_from(request)
181
188
  safe_autocorrect = request.dig(:params, :initializationOptions, :safeAutocorrect)
189
+ position_encodings = request.dig(:params, :capabilities, :general, :positionEncodings)
182
190
 
183
191
  {
184
192
  safe_autocorrect: safe_autocorrect.nil? || safe_autocorrect == true,
185
193
  lint_mode: request.dig(:params, :initializationOptions, :lintMode) == true,
186
- layout_mode: request.dig(:params, :initializationOptions, :layoutMode) == true
194
+ layout_mode: request.dig(:params, :initializationOptions, :layoutMode) == true,
195
+ position_encoding: position_encoding(position_encodings)
187
196
  }
188
197
  end
189
198
 
199
+ def position_encoding(position_encodings)
200
+ if position_encodings&.include?('utf-8')
201
+ 'utf-8'
202
+ elsif position_encodings&.include?('utf-32')
203
+ 'utf-32'
204
+ else
205
+ 'utf-16'
206
+ end
207
+ end
208
+
190
209
  def format_file(file_uri, command: nil)
191
210
  unless (text = @text_cache[file_uri])
192
211
  Logger.log("Format request arrived before text synchronized; skipping: `#{file_uri}'")
@@ -194,7 +213,7 @@ module RuboCop
194
213
  return []
195
214
  end
196
215
 
197
- new_text = @server.format(remove_file_protocol_from(file_uri), text, command: command)
216
+ new_text = @server.format(convert_file_uri_to_path(file_uri), text, command: command)
198
217
 
199
218
  return [] if new_text == text
200
219
 
@@ -214,13 +233,50 @@ module RuboCop
214
233
  method: 'textDocument/publishDiagnostics',
215
234
  params: {
216
235
  uri: file_uri,
217
- diagnostics: @server.offenses(remove_file_protocol_from(file_uri), text)
236
+ diagnostics: @server.offenses(convert_file_uri_to_path(file_uri),
237
+ text, @position_encoding)
218
238
  }
219
239
  }
220
240
  end
221
241
 
222
- def remove_file_protocol_from(uri)
223
- uri.delete_prefix('file://')
242
+ def change_text(orig_text, text, range)
243
+ return text unless range
244
+
245
+ start_pos = text_pos(orig_text, range[:start])
246
+ end_pos = text_pos(orig_text, range[:end])
247
+ orig_text[start_pos...end_pos] = text
248
+ orig_text
249
+ end
250
+
251
+ def text_pos(text, range)
252
+ line = range[:line]
253
+ char = range[:character]
254
+ pos = 0
255
+ text.each_line.with_index do |l, i|
256
+ if i == line
257
+ pos += line_pos(l, char)
258
+ return pos
259
+ end
260
+ pos += l.size
261
+ end
262
+ pos
263
+ end
264
+
265
+ def line_pos(line, char)
266
+ case @position_encoding
267
+ when 'utf-8'
268
+ line.byteslice(0, char).size
269
+ when 'utf-32'
270
+ char
271
+ else # 'utf-16'
272
+ # utf-16 is default position encoding on LSP
273
+ # https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/
274
+ line.encode('utf-16be').byteslice(0, char * 2).size
275
+ end
276
+ end
277
+
278
+ def convert_file_uri_to_path(uri)
279
+ URI.decode_www_form_component(uri.delete_prefix('file://'))
224
280
  end
225
281
  end
226
282
  end
@@ -44,14 +44,14 @@ module RuboCop
44
44
  @runner.formatted_source
45
45
  end
46
46
 
47
- def offenses(path, text, document_encoding = nil, prism_result: nil)
47
+ def offenses(path, text, position_encoding, prism_result: nil)
48
48
  diagnostic_options = {}
49
49
  diagnostic_options[:only] = config_only_options if @lint_mode || @layout_mode
50
50
 
51
51
  @runner.run(path, text, diagnostic_options, prism_result: prism_result)
52
52
  @runner.offenses.map do |offense|
53
53
  Diagnostic.new(
54
- document_encoding, offense, path, @cop_registry[offense.cop_name]&.first
54
+ position_encoding, offense, path, @cop_registry[offense.cop_name]&.first
55
55
  ).to_lsp_diagnostic(@runner.config_for_working_directory)
56
56
  end
57
57
  end
@@ -51,8 +51,8 @@ module RuboCop
51
51
  @runtime.format(path, text, command: command)
52
52
  end
53
53
 
54
- def offenses(path, text)
55
- @runtime.offenses(path, text)
54
+ def offenses(path, text, position_encoding)
55
+ @runtime.offenses(path, text, position_encoding)
56
56
  end
57
57
 
58
58
  def configure(options)
@@ -40,7 +40,6 @@ module RuboCop
40
40
  super(@options, config_store)
41
41
  end
42
42
 
43
- # rubocop:disable Metrics/MethodLength
44
43
  def run(path, contents, options, prism_result: nil)
45
44
  @options = options.merge(DEFAULT_RUBOCOP_OPTIONS)
46
45
  @options[:stdin] = contents
@@ -54,22 +53,7 @@ module RuboCop
54
53
  super([path])
55
54
 
56
55
  raise Interrupt if aborting?
57
- rescue RuboCop::Runner::InfiniteCorrectionLoop => e
58
- if defined?(::RubyLsp::Requests::Formatting::Error)
59
- raise ::RubyLsp::Requests::Formatting::Error, e.message
60
- end
61
-
62
- raise e
63
- rescue RuboCop::ValidationError => e
64
- raise ConfigurationError, e.message
65
- rescue StandardError => e
66
- if defined?(::RubyLsp::Requests::Formatting::Error)
67
- raise ::RubyLsp::Requests::Support::InternalRuboCopError, e
68
- end
69
-
70
- raise e
71
56
  end
72
- # rubocop:enable Metrics/MethodLength
73
57
 
74
58
  def formatted_source
75
59
  @options[:stdin]
@@ -9,7 +9,7 @@ module RuboCop
9
9
  # Provides functionality for caching RuboCop runs.
10
10
  # @api private
11
11
  class ResultCache
12
- NON_CHANGING = %i[color format formatters out debug fail_level
12
+ NON_CHANGING = %i[color format formatters out debug display_time fail_level
13
13
  fix_layout autocorrect safe_autocorrect autocorrect_all
14
14
  cache fail_fast stdin parallel].freeze
15
15
 
@@ -198,20 +198,22 @@ module RuboCop
198
198
  end
199
199
 
200
200
  def rubocop_extra_features
201
- lib_root = File.join(File.dirname(__FILE__), '..')
202
- exe_root = File.join(lib_root, '..', 'exe')
201
+ @rubocop_extra_features ||= begin
202
+ lib_root = File.join(File.dirname(__FILE__), '..')
203
+ exe_root = File.join(lib_root, '..', 'exe')
203
204
 
204
- # Make sure to use an absolute path to prevent errors on Windows
205
- # when traversing the relative paths with symlinks.
206
- exe_root = File.absolute_path(exe_root)
205
+ # Make sure to use an absolute path to prevent errors on Windows
206
+ # when traversing the relative paths with symlinks.
207
+ exe_root = File.absolute_path(exe_root)
207
208
 
208
- # These are all the files we have `require`d plus everything in the
209
- # exe directory. A change to any of them could affect the cop output
210
- # so we include them in the cache hash.
211
- source_files = $LOADED_FEATURES + Find.find(exe_root).to_a
212
- source_files -= ResultCache.rubocop_required_features # Rely on gem versions
209
+ # These are all the files we have `require`d plus everything in the
210
+ # exe directory. A change to any of them could affect the cop output
211
+ # so we include them in the cache hash.
212
+ source_files = $LOADED_FEATURES + Find.find(exe_root).to_a
213
+ source_files -= ResultCache.rubocop_required_features # Rely on gem versions
213
214
 
214
- source_files
215
+ source_files
216
+ end
215
217
  end
216
218
 
217
219
  # Return a hash of the options given at invocation, minus the ones that have
@@ -273,7 +273,8 @@ module RuboCop
273
273
  end
274
274
 
275
275
  def do_inspection_loop(file)
276
- processed_source = get_processed_source(file)
276
+ # We can reuse the prism result since the source did not change yet.
277
+ processed_source = get_processed_source(file, @prism_result)
277
278
  # This variable is 2d array used to track corrected offenses after each
278
279
  # inspection iteration. This is used to output meaningful infinite loop
279
280
  # error message.
@@ -295,7 +296,8 @@ module RuboCop
295
296
  # loop if we find any.
296
297
  break unless updated_source_file
297
298
 
298
- processed_source = get_processed_source(file)
299
+ # Autocorrect has happened, don't use the prism result since it is stale.
300
+ processed_source = get_processed_source(file, nil)
299
301
  end
300
302
 
301
303
  # Return summary of corrected offenses after all iterations
@@ -482,7 +484,7 @@ module RuboCop
482
484
  end
483
485
 
484
486
  # rubocop:disable Metrics/MethodLength
485
- def get_processed_source(file)
487
+ def get_processed_source(file, prism_result)
486
488
  config = @config_store.for_file(file)
487
489
  ruby_version = config.target_ruby_version
488
490
  parser_engine = config.parser_engine
@@ -493,7 +495,7 @@ module RuboCop
493
495
  ruby_version,
494
496
  file,
495
497
  parser_engine: parser_engine,
496
- prism_result: @prism_result
498
+ prism_result: prism_result
497
499
  )
498
500
  else
499
501
  begin
@@ -42,14 +42,12 @@ module RuboCop
42
42
  # Support Windows: Backslashes from command-line -> forward slashes
43
43
  base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
44
44
  all_files = find_files(base_dir, File::FNM_DOTMATCH)
45
- # use file.include? for performance optimization
46
- hidden_files = all_files.select { |file| file.include?(HIDDEN_PATH_SUBSTRING) }.sort
47
45
  base_dir_config = @config_store.for(base_dir)
48
46
 
49
- target_files = if base_dir.include?(HIDDEN_PATH_SUBSTRING)
47
+ target_files = if hidden_path?(base_dir)
50
48
  all_files.select { |file| ruby_file?(file) }
51
49
  else
52
- all_files.select { |file| to_inspect?(file, hidden_files, base_dir_config) }
50
+ all_files.select { |file| to_inspect?(file, base_dir_config) }
53
51
  end
54
52
 
55
53
  target_files.sort_by!(&order)
@@ -74,18 +72,20 @@ module RuboCop
74
72
 
75
73
  private
76
74
 
77
- def to_inspect?(file, hidden_files, base_dir_config)
75
+ def to_inspect?(file, base_dir_config)
78
76
  return false if base_dir_config.file_to_exclude?(file)
79
- return true if !hidden_files.bsearch do |hidden_file|
80
- file <=> hidden_file
81
- end && ruby_file?(file)
77
+ return true if !hidden_path?(file) && ruby_file?(file)
82
78
 
83
79
  base_dir_config.file_to_include?(file)
84
80
  end
85
81
 
82
+ def hidden_path?(path)
83
+ path.include?(HIDDEN_PATH_SUBSTRING)
84
+ end
85
+
86
86
  def wanted_dir_patterns(base_dir, exclude_pattern, flags)
87
87
  # Escape glob characters in base_dir to avoid unwanted behavior.
88
- base_dir = base_dir.gsub(/[\\\{\}\[\]\*\?]/) do |reserved_glob_character|
88
+ base_dir = base_dir.gsub(/[\\{}\[\]*?]/) do |reserved_glob_character|
89
89
  "\\#{reserved_glob_character}"
90
90
  end
91
91
 
@@ -110,8 +110,17 @@ module RuboCop
110
110
  end
111
111
 
112
112
  def version_from_gemspec_file(file)
113
+ # When using parser_prism, we need to use a Ruby version that Prism supports (3.3+)
114
+ # for parsing the gemspec file. This doesn't affect the detected Ruby version,
115
+ # it's just for the parsing step.
116
+ ruby_version_for_parsing = if @config.parser_engine == :parser_prism
117
+ 3.3
118
+ else
119
+ DEFAULT_VERSION
120
+ end
121
+
113
122
  processed_source = ProcessedSource.from_file(
114
- file, DEFAULT_VERSION, parser_engine: @config.parser_engine
123
+ file, ruby_version_for_parsing, parser_engine: @config.parser_engine
115
124
  )
116
125
  return unless processed_source.valid_syntax?
117
126
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.78.0'
6
+ STRING = '1.81.6'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
data/lib/rubocop.rb CHANGED
@@ -211,6 +211,7 @@ require_relative 'rubocop/cop/layout/empty_line_after_guard_clause'
211
211
  require_relative 'rubocop/cop/layout/empty_line_after_magic_comment'
212
212
  require_relative 'rubocop/cop/layout/empty_line_after_multiline_condition'
213
213
  require_relative 'rubocop/cop/layout/empty_line_between_defs'
214
+ require_relative 'rubocop/cop/layout/empty_lines_after_module_inclusion'
214
215
  require_relative 'rubocop/cop/layout/empty_lines_around_access_modifier'
215
216
  require_relative 'rubocop/cop/layout/empty_lines_around_arguments'
216
217
  require_relative 'rubocop/cop/layout/empty_lines_around_attribute_accessor'
@@ -290,6 +291,7 @@ require_relative 'rubocop/cop/layout/space_inside_string_interpolation'
290
291
  require_relative 'rubocop/cop/layout/trailing_empty_lines'
291
292
  require_relative 'rubocop/cop/layout/trailing_whitespace'
292
293
 
294
+ require_relative 'rubocop/cop/lint/utils/nil_receiver_checker'
293
295
  require_relative 'rubocop/cop/lint/ambiguous_assignment'
294
296
  require_relative 'rubocop/cop/lint/ambiguous_block_association'
295
297
  require_relative 'rubocop/cop/lint/ambiguous_operator'
@@ -487,6 +489,7 @@ require_relative 'rubocop/cop/style/arguments_forwarding'
487
489
  require_relative 'rubocop/cop/style/array_coercion'
488
490
  require_relative 'rubocop/cop/style/array_first_last'
489
491
  require_relative 'rubocop/cop/style/array_intersect'
492
+ require_relative 'rubocop/cop/style/array_intersect_with_single_element'
490
493
  require_relative 'rubocop/cop/style/array_join'
491
494
  require_relative 'rubocop/cop/style/ascii_comments'
492
495
  require_relative 'rubocop/cop/style/attr'
@@ -8,7 +8,10 @@ module RubyLsp
8
8
  module RuboCop
9
9
  # A Ruby LSP add-on for RuboCop.
10
10
  class Addon < RubyLsp::Addon
11
- def initializer
11
+ RESTART_WATCHERS = %w[.rubocop.yml .rubocop_todo.yml .rubocop].freeze
12
+
13
+ def initialize
14
+ super
12
15
  @runtime_adapter = nil
13
16
  end
14
17
 
@@ -16,12 +19,16 @@ module RubyLsp
16
19
  'RuboCop'
17
20
  end
18
21
 
22
+ def version
23
+ ::RuboCop::Version::STRING
24
+ end
25
+
19
26
  def activate(global_state, message_queue)
20
27
  ::RuboCop::LSP::Logger.log(
21
28
  "Activating RuboCop LSP addon #{::RuboCop::Version::STRING}.", prefix: '[RuboCop]'
22
29
  )
23
30
 
24
- @runtime_adapter = RuntimeAdapter.new
31
+ @runtime_adapter = RuntimeAdapter.new(message_queue)
25
32
  global_state.register_formatter('rubocop', @runtime_adapter)
26
33
  register_additional_file_watchers(global_state, message_queue)
27
34
 
@@ -49,7 +56,7 @@ module RubyLsp
49
56
  register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
50
57
  watchers: [
51
58
  Interface::FileSystemWatcher.new(
52
- glob_pattern: '**/.rubocop{,_todo}.yml',
59
+ glob_pattern: "**/{#{RESTART_WATCHERS.join(',')}}",
53
60
  kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE
54
61
  )
55
62
  ]
@@ -62,13 +69,21 @@ module RubyLsp
62
69
  # rubocop:enable Metrics/MethodLength
63
70
 
64
71
  def workspace_did_change_watched_files(changes)
65
- return unless changes.any? { |change| change[:uri].end_with?('.rubocop.yml') }
72
+ if (changed_config_file = changed_config_file(changes))
73
+ @runtime_adapter.reload_config
74
+
75
+ ::RuboCop::LSP::Logger.log(<<~MESSAGE, prefix: '[RuboCop]')
76
+ Re-initialized RuboCop LSP addon #{::RuboCop::Version::STRING} due to #{changed_config_file} change.
77
+ MESSAGE
78
+ end
79
+ end
66
80
 
67
- @runtime_adapter = RuntimeAdapter.new
81
+ private
68
82
 
69
- ::RuboCop::LSP::Logger.log(<<~MESSAGE, prefix: '[RuboCop]')
70
- Re-initialized RuboCop LSP addon #{::RuboCop::Version::STRING} due to .rubocop.yml file change.
71
- MESSAGE
83
+ def changed_config_file(changes)
84
+ RESTART_WATCHERS.find do |file_name|
85
+ changes.any? { |change| change[:uri].end_with?(file_name) }
86
+ end
72
87
  end
73
88
  end
74
89
  end
@@ -7,30 +7,45 @@ module RubyLsp
7
7
  # Provides an adapter to bridge RuboCop's built-in LSP runtime with Ruby LSP's add-on.
8
8
  # @api private
9
9
  class RuntimeAdapter
10
- include RubyLsp::Requests::Support::Formatter
10
+ def initialize(message_queue)
11
+ @message_queue = message_queue
12
+ reload_config
13
+ end
11
14
 
12
- def initialize
13
- config_store = ::RuboCop::ConfigStore.new
15
+ def reload_config
16
+ @runtime = nil
17
+ options, _paths = ::RuboCop::Options.new.parse([])
14
18
 
19
+ config_store = ::RuboCop::ConfigStore.new
20
+ config_store.apply_options!(options)
15
21
  @runtime = ::RuboCop::LSP::Runtime.new(config_store)
22
+ rescue ::RuboCop::Error => e
23
+ @message_queue << Notification.window_show_message(
24
+ "RuboCop configuration error: #{e.message}. Formatting will not be available.",
25
+ type: Constant::MessageType::ERROR
26
+ )
16
27
  end
17
28
 
18
29
  def run_diagnostic(uri, document)
19
- @runtime.offenses(
20
- uri_to_path(uri),
21
- document.source,
22
- document.encoding,
23
- prism_result: prism_result(document)
24
- )
30
+ with_error_handling do
31
+ @runtime.offenses(
32
+ uri_to_path(uri),
33
+ document.source,
34
+ document.encoding,
35
+ prism_result: prism_result(document)
36
+ )
37
+ end
25
38
  end
26
39
 
27
40
  def run_formatting(uri, document)
28
- @runtime.format(
29
- uri_to_path(uri),
30
- document.source,
31
- command: 'rubocop.formatAutocorrects',
32
- prism_result: prism_result(document)
33
- )
41
+ with_error_handling do
42
+ @runtime.format(
43
+ uri_to_path(uri),
44
+ document.source,
45
+ command: 'rubocop.formatAutocorrects',
46
+ prism_result: prism_result(document)
47
+ )
48
+ end
34
49
  end
35
50
 
36
51
  def run_range_formatting(_uri, _partial_source, _base_indentation)
@@ -43,6 +58,25 @@ module RubyLsp
43
58
 
44
59
  private
45
60
 
61
+ def with_error_handling
62
+ return unless @runtime
63
+
64
+ yield
65
+ rescue StandardError => e
66
+ ::RuboCop::LSP::Logger.log(e.full_message, prefix: '[RuboCop]')
67
+
68
+ message = if e.is_a?(::RuboCop::ErrorWithAnalyzedFileLocation)
69
+ "for the #{e.cop.name} cop"
70
+ else
71
+ "- #{e.message}"
72
+ end
73
+ raise Requests::Formatting::Error, <<~MSG
74
+ An internal error occurred #{message}.
75
+ Updating to a newer version of RuboCop may solve this.
76
+ For more details, run RuboCop on the command line.
77
+ MSG
78
+ end
79
+
46
80
  # duplicated from: lib/standard/lsp/routes.rb
47
81
  # modified to incorporate Ruby LSP's to_standardized_path method
48
82
  def uri_to_path(uri)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.78.0
4
+ version: 1.81.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -9,7 +9,7 @@ authors:
9
9
  - Yuji Nakayama
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-07-08 00:00:00.000000000 Z
12
+ date: 2025-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -127,7 +127,7 @@ dependencies:
127
127
  requirements:
128
128
  - - ">="
129
129
  - !ruby/object:Gem::Version
130
- version: 1.45.1
130
+ version: 1.47.1
131
131
  - - "<"
132
132
  - !ruby/object:Gem::Version
133
133
  version: '2.0'
@@ -137,7 +137,7 @@ dependencies:
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
- version: 1.45.1
140
+ version: 1.47.1
141
141
  - - "<"
142
142
  - !ruby/object:Gem::Version
143
143
  version: '2.0'
@@ -342,6 +342,7 @@ files:
342
342
  - lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb
343
343
  - lib/rubocop/cop/layout/empty_line_between_defs.rb
344
344
  - lib/rubocop/cop/layout/empty_lines.rb
345
+ - lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb
345
346
  - lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb
346
347
  - lib/rubocop/cop/layout/empty_lines_around_arguments.rb
347
348
  - lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb
@@ -572,6 +573,7 @@ files:
572
573
  - lib/rubocop/cop/lint/useless_ruby2_keywords.rb
573
574
  - lib/rubocop/cop/lint/useless_setter_call.rb
574
575
  - lib/rubocop/cop/lint/useless_times.rb
576
+ - lib/rubocop/cop/lint/utils/nil_receiver_checker.rb
575
577
  - lib/rubocop/cop/lint/void.rb
576
578
  - lib/rubocop/cop/message_annotator.rb
577
579
  - lib/rubocop/cop/metrics/abc_size.rb
@@ -706,6 +708,7 @@ files:
706
708
  - lib/rubocop/cop/style/array_coercion.rb
707
709
  - lib/rubocop/cop/style/array_first_last.rb
708
710
  - lib/rubocop/cop/style/array_intersect.rb
711
+ - lib/rubocop/cop/style/array_intersect_with_single_element.rb
709
712
  - lib/rubocop/cop/style/array_join.rb
710
713
  - lib/rubocop/cop/style/ascii_comments.rb
711
714
  - lib/rubocop/cop/style/attr.rb
@@ -1088,9 +1091,9 @@ licenses:
1088
1091
  - MIT
1089
1092
  metadata:
1090
1093
  homepage_uri: https://rubocop.org/
1091
- changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.78.0
1094
+ changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.81.6
1092
1095
  source_code_uri: https://github.com/rubocop/rubocop/
1093
- documentation_uri: https://docs.rubocop.org/rubocop/1.78/
1096
+ documentation_uri: https://docs.rubocop.org/rubocop/1.81/
1094
1097
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1095
1098
  rubygems_mfa_required: 'true'
1096
1099
  rdoc_options: []