rubocop 1.60.0 → 1.61.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.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/assets/output.css.erb +159 -0
  4. data/assets/output.html.erb +1 -160
  5. data/config/default.yml +35 -12
  6. data/lib/rubocop/cli/command/lsp.rb +2 -2
  7. data/lib/rubocop/cli.rb +6 -1
  8. data/lib/rubocop/config_finder.rb +12 -2
  9. data/lib/rubocop/config_validator.rb +14 -5
  10. data/lib/rubocop/cop/autocorrect_logic.rb +6 -1
  11. data/lib/rubocop/cop/base.rb +12 -7
  12. data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
  13. data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
  14. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
  15. data/lib/rubocop/cop/internal_affairs/example_description.rb +4 -4
  16. data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
  17. data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  19. data/lib/rubocop/cop/layout/end_alignment.rb +3 -1
  20. data/lib/rubocop/cop/layout/redundant_line_break.rb +7 -1
  21. data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
  22. data/lib/rubocop/cop/lint/empty_conditional_body.rb +1 -1
  23. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +14 -9
  24. data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
  25. data/lib/rubocop/cop/lint/script_permission.rb +3 -3
  26. data/lib/rubocop/cop/lint/syntax.rb +1 -1
  27. data/lib/rubocop/cop/lint/void.rb +6 -1
  28. data/lib/rubocop/cop/naming/predicate_name.rb +2 -2
  29. data/lib/rubocop/cop/registry.rb +5 -5
  30. data/lib/rubocop/cop/style/arguments_forwarding.rb +69 -13
  31. data/lib/rubocop/cop/style/case_like_if.rb +1 -1
  32. data/lib/rubocop/cop/style/collection_compact.rb +11 -2
  33. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  34. data/lib/rubocop/cop/style/conditional_assignment.rb +4 -5
  35. data/lib/rubocop/cop/style/hash_each_methods.rb +26 -5
  36. data/lib/rubocop/cop/style/hash_syntax.rb +6 -2
  37. data/lib/rubocop/cop/style/inverse_methods.rb +8 -8
  38. data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -5
  39. data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +5 -8
  40. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -1
  41. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +4 -0
  42. data/lib/rubocop/cop/style/object_then.rb +5 -3
  43. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -3
  44. data/lib/rubocop/cop/style/raise_args.rb +3 -0
  45. data/lib/rubocop/cop/style/redundant_argument.rb +2 -2
  46. data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
  47. data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -5
  48. data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -1
  49. data/lib/rubocop/cop/style/redundant_return.rb +6 -0
  50. data/lib/rubocop/cop/style/sample.rb +1 -3
  51. data/lib/rubocop/cops_documentation_generator.rb +4 -2
  52. data/lib/rubocop/formatter/html_formatter.rb +30 -10
  53. data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
  54. data/lib/rubocop/lsp/logger.rb +1 -1
  55. data/lib/rubocop/lsp/routes.rb +1 -1
  56. data/lib/rubocop/lsp/runtime.rb +1 -1
  57. data/lib/rubocop/lsp/server.rb +5 -2
  58. data/lib/rubocop/lsp/severity.rb +1 -1
  59. data/lib/rubocop/lsp.rb +29 -0
  60. data/lib/rubocop/options.rb +11 -0
  61. data/lib/rubocop/path_util.rb +6 -2
  62. data/lib/rubocop/rspec/shared_contexts.rb +10 -6
  63. data/lib/rubocop/rspec/support.rb +1 -1
  64. data/lib/rubocop/server/cache.rb +1 -1
  65. data/lib/rubocop/server/client_command/exec.rb +0 -1
  66. data/lib/rubocop/server/server_command/exec.rb +0 -1
  67. data/lib/rubocop/target_ruby.rb +79 -79
  68. data/lib/rubocop/version.rb +1 -1
  69. metadata +11 -8
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # The RuboCop's built-in LSP module.
5
+ module LSP
6
+ module_function
7
+
8
+ # Returns true when LSP is enabled, false when disabled.
9
+ #
10
+ # @return [Boolean]
11
+ def enabled?
12
+ @enabled ||= false
13
+ end
14
+
15
+ # Enable LSP.
16
+ #
17
+ # @return [void]
18
+ def enable
19
+ @enabled = true
20
+ end
21
+
22
+ # Disable LSP.
23
+ #
24
+ # @return [void]
25
+ def disable
26
+ @enabled = false
27
+ end
28
+ end
29
+ end
@@ -95,6 +95,7 @@ module RuboCop
95
95
  option(opts, '--ignore-unrecognized-cops')
96
96
  option(opts, '--force-default-config')
97
97
  option(opts, '-s', '--stdin FILE')
98
+ option(opts, '--editor-mode')
98
99
  option(opts, '-P', '--[no-]parallel')
99
100
  option(opts, '--raise-cop-error')
100
101
  add_severity_option(opts)
@@ -369,6 +370,7 @@ module RuboCop
369
370
  validate_display_only_failed
370
371
  validate_display_only_failed_and_display_only_correctable
371
372
  validate_display_only_correctable_and_autocorrect
373
+ validate_lsp_and_editor_mode
372
374
  disable_parallel_when_invalid_option_combo
373
375
 
374
376
  return if incompatible_options.size <= 1
@@ -416,6 +418,13 @@ module RuboCop
416
418
  format('--display-only-failed cannot be used together with other display options.')
417
419
  end
418
420
 
421
+ def validate_lsp_and_editor_mode
422
+ return if !@options.key?(:lsp) || !@options.key?(:editor_mode)
423
+
424
+ raise OptionArgumentError,
425
+ format('Do not specify `--editor-mode` as it is redundant in `--lsp`.')
426
+ end
427
+
419
428
  def validate_autocorrect
420
429
  if @options.key?(:safe_autocorrect) && @options.key?(:autocorrect_all)
421
430
  message = Rainbow(<<~MESSAGE).red
@@ -609,6 +618,8 @@ module RuboCop
609
618
  'parallel. Default is true.'],
610
619
  stdin: ['Pipe source from STDIN, using FILE in offense',
611
620
  'reports. This is useful for editor integration.'],
621
+ editor_mode: ['Optimize real-time feedback in editors,',
622
+ 'adjusting behaviors for editing experience.'],
612
623
  init: 'Generate a .rubocop.yml file in the current directory.',
613
624
  server: ['If a server process has not been started yet, start',
614
625
  'the server process and execute inspection with server.',
@@ -44,7 +44,7 @@ module RuboCop
44
44
  end
45
45
  end
46
46
 
47
- # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
47
+ # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
48
48
  def match_path?(pattern, path)
49
49
  case pattern
50
50
  when String
@@ -52,6 +52,10 @@ module RuboCop
52
52
  if pattern == path
53
53
  true
54
54
  elsif glob?(pattern)
55
+ # File name matching doesn't really work with relative patterns that start with "..". We
56
+ # get around that problem by converting the pattern to an absolute path.
57
+ pattern = File.expand_path(pattern) if pattern.start_with?('..')
58
+
55
59
  File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
56
60
  end
57
61
 
@@ -66,7 +70,7 @@ module RuboCop
66
70
  end
67
71
  end
68
72
  end
69
- # rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity
73
+ # rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
70
74
 
71
75
  # Returns true for an absolute Unix or Windows path.
72
76
  def absolute?(path)
@@ -100,10 +100,10 @@ RSpec.shared_context 'config' do # rubocop:disable Metrics/BlockLength
100
100
  let(:cur_cop_config) do
101
101
  RuboCop::ConfigLoader
102
102
  .default_configuration.for_cop(cop_class)
103
- .merge({
104
- 'Enabled' => true, # in case it is 'pending'
105
- 'AutoCorrect' => true # in case defaults set it to false
106
- })
103
+ .merge(
104
+ 'Enabled' => true, # in case it is 'pending'
105
+ 'AutoCorrect' => 'always' # in case defaults set it to 'disabled' or false
106
+ )
107
107
  .merge(cop_config)
108
108
  end
109
109
 
@@ -128,9 +128,13 @@ RSpec.shared_context 'mock console output' do
128
128
  end
129
129
  end
130
130
 
131
- RSpec.shared_context 'lsp mode' do
131
+ RSpec.shared_context 'lsp' do
132
132
  before do
133
- allow(cop).to receive(:lsp_mode?).and_return(true)
133
+ RuboCop::LSP.enable
134
+ end
135
+
136
+ after do
137
+ RuboCop::LSP.disable
134
138
  end
135
139
  end
136
140
 
@@ -13,7 +13,7 @@ RSpec.configure do |config|
13
13
  config.include HostEnvironmentSimulatorHelper
14
14
  config.include_context 'config', :config
15
15
  config.include_context 'isolated environment', :isolated_environment
16
- config.include_context 'lsp mode', :lsp_mode
16
+ config.include_context 'lsp', :lsp
17
17
  config.include_context 'maintain registry', :restore_registry
18
18
  config.include_context 'ruby 2.0', :ruby20
19
19
  config.include_context 'ruby 2.1', :ruby21
@@ -116,7 +116,7 @@ module RuboCop
116
116
 
117
117
  def pid_running?
118
118
  Process.kill(0, pid_path.read.to_i) == 1
119
- rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES
119
+ rescue Errno::ESRCH, Errno::ENOENT, Errno::EACCES, Errno::EROFS
120
120
  false
121
121
  end
122
122
 
@@ -17,7 +17,6 @@ module RuboCop
17
17
  class Exec < Base
18
18
  def run
19
19
  ensure_server!
20
- Cache.status_path.delete if Cache.status_path.file?
21
20
  read_stdin = ARGV.include?('-s') || ARGV.include?('--stdin')
22
21
  send_request(
23
22
  command: 'exec',
@@ -16,7 +16,6 @@ module RuboCop
16
16
  # @api private
17
17
  class Exec < Base
18
18
  def run
19
- Cache.status_path.delete if Cache.status_path.file?
20
19
  # RuboCop output is colorized by default where there is a TTY.
21
20
  # We must pass the --color option to preserve this behavior.
22
21
  @args.unshift('--color') unless %w[--color --no-color].any? { |f| @args.include?(f) }
@@ -48,6 +48,84 @@ module RuboCop
48
48
  end
49
49
  end
50
50
 
51
+ # The target ruby version may be found in a .gemspec file.
52
+ # @api private
53
+ class GemspecFile < Source
54
+ extend NodePattern::Macros
55
+
56
+ GEMSPEC_EXTENSION = '.gemspec'
57
+
58
+ # @!method required_ruby_version(node)
59
+ def_node_search :required_ruby_version, <<~PATTERN
60
+ (send _ :required_ruby_version= $_)
61
+ PATTERN
62
+
63
+ # @!method gem_requirement_versions(node)
64
+ def_node_matcher :gem_requirement_versions, <<~PATTERN
65
+ (send (const(const _ :Gem):Requirement) :new
66
+ {$str+ | (send $str :freeze)+ | (array $str+) | (array (send $str :freeze)+)}
67
+ )
68
+ PATTERN
69
+
70
+ def name
71
+ "`required_ruby_version` parameter (in #{gemspec_filename})"
72
+ end
73
+
74
+ private
75
+
76
+ def find_version
77
+ file = gemspec_filepath
78
+ return unless file && File.file?(file)
79
+
80
+ right_hand_side = version_from_gemspec_file(file)
81
+ return if right_hand_side.nil?
82
+
83
+ find_default_minimal_known_ruby(right_hand_side)
84
+ end
85
+
86
+ def gemspec_filename
87
+ @gemspec_filename ||= begin
88
+ basename = Pathname.new(@config.base_dir_for_path_parameters).basename.to_s
89
+ "#{basename}#{GEMSPEC_EXTENSION}"
90
+ end
91
+ end
92
+
93
+ def gemspec_filepath
94
+ @gemspec_filepath ||=
95
+ @config.find_file_upwards(gemspec_filename, @config.base_dir_for_path_parameters)
96
+ end
97
+
98
+ def version_from_gemspec_file(file)
99
+ processed_source = ProcessedSource.from_file(file, DEFAULT_VERSION)
100
+ required_ruby_version(processed_source.ast).first
101
+ end
102
+
103
+ def version_from_right_hand_side(right_hand_side)
104
+ gem_requirement_versions = gem_requirement_versions(right_hand_side)
105
+
106
+ if right_hand_side.array_type?
107
+ version_from_array(right_hand_side)
108
+ elsif gem_requirement_versions
109
+ gem_requirement_versions.map(&:value)
110
+ else
111
+ right_hand_side.value
112
+ end
113
+ end
114
+
115
+ def version_from_array(array)
116
+ array.children.map(&:value)
117
+ end
118
+
119
+ def find_default_minimal_known_ruby(right_hand_side)
120
+ version = version_from_right_hand_side(right_hand_side)
121
+ requirement = Gem::Requirement.new(version)
122
+
123
+ KNOWN_RUBIES.detect do |v|
124
+ v >= DEFAULT_VERSION && requirement.satisfied_by?(Gem::Version.new("#{v}.99"))
125
+ end
126
+ end
127
+ end
128
+
51
129
  # The target ruby version may be found in a .ruby-version file.
52
130
  # @api private
53
131
  class RubyVersionFile < Source
@@ -143,84 +221,6 @@ module RuboCop
143
221
  end
144
222
  end
145
223
 
146
- # The target ruby version may be found in a .gemspec file.
147
- # @api private
148
- class GemspecFile < Source
149
- extend NodePattern::Macros
150
-
151
- GEMSPEC_EXTENSION = '.gemspec'
152
-
153
- # @!method required_ruby_version(node)
154
- def_node_search :required_ruby_version, <<~PATTERN
155
- (send _ :required_ruby_version= $_)
156
- PATTERN
157
-
158
- # @!method gem_requirement_versions(node)
159
- def_node_matcher :gem_requirement_versions, <<~PATTERN
160
- (send (const(const _ :Gem):Requirement) :new
161
- {$str+ | (send $str :freeze)+ | (array $str+) | (array (send $str :freeze)+)}
162
- )
163
- PATTERN
164
-
165
- def name
166
- "`required_ruby_version` parameter (in #{gemspec_filename})"
167
- end
168
-
169
- private
170
-
171
- def find_version
172
- file = gemspec_filepath
173
- return unless file && File.file?(file)
174
-
175
- right_hand_side = version_from_gemspec_file(file)
176
- return if right_hand_side.nil?
177
-
178
- find_default_minimal_known_ruby(right_hand_side)
179
- end
180
-
181
- def gemspec_filename
182
- @gemspec_filename ||= begin
183
- basename = Pathname.new(@config.base_dir_for_path_parameters).basename.to_s
184
- "#{basename}#{GEMSPEC_EXTENSION}"
185
- end
186
- end
187
-
188
- def gemspec_filepath
189
- @gemspec_filepath ||=
190
- @config.find_file_upwards(gemspec_filename, @config.base_dir_for_path_parameters)
191
- end
192
-
193
- def version_from_gemspec_file(file)
194
- processed_source = ProcessedSource.from_file(file, DEFAULT_VERSION)
195
- required_ruby_version(processed_source.ast).first
196
- end
197
-
198
- def version_from_right_hand_side(right_hand_side)
199
- gem_requirement_versions = gem_requirement_versions(right_hand_side)
200
-
201
- if right_hand_side.array_type?
202
- version_from_array(right_hand_side)
203
- elsif gem_requirement_versions
204
- gem_requirement_versions.map(&:value)
205
- else
206
- right_hand_side.value
207
- end
208
- end
209
-
210
- def version_from_array(array)
211
- array.children.map(&:value)
212
- end
213
-
214
- def find_default_minimal_known_ruby(right_hand_side)
215
- version = version_from_right_hand_side(right_hand_side)
216
- requirement = Gem::Requirement.new(version)
217
-
218
- KNOWN_RUBIES.detect do |v|
219
- v >= DEFAULT_VERSION && requirement.satisfied_by?(Gem::Version.new("#{v}.99"))
220
- end
221
- end
222
- end
223
-
224
224
  # If all else fails, a default version will be picked.
225
225
  # @api private
226
226
  class Default < Source
@@ -241,10 +241,10 @@ module RuboCop
241
241
 
242
242
  SOURCES = [
243
243
  RuboCopConfig,
244
+ GemspecFile,
244
245
  RubyVersionFile,
245
246
  ToolVersionsFile,
246
247
  BundlerLockFile,
247
- GemspecFile,
248
248
  Default
249
249
  ].freeze
250
250
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.60.0'
6
+ STRING = '1.61.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.60.0
4
+ version: 1.61.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2024-01-15 00:00:00.000000000 Z
13
+ date: 2024-02-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -196,6 +196,7 @@ files:
196
196
  - LICENSE.txt
197
197
  - README.md
198
198
  - assets/logo.png
199
+ - assets/output.css.erb
199
200
  - assets/output.html.erb
200
201
  - config/default.yml
201
202
  - config/obsoletion.yml
@@ -302,6 +303,7 @@ files:
302
303
  - lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb
303
304
  - lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb
304
305
  - lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb
306
+ - lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb
305
307
  - lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb
306
308
  - lib/rubocop/cop/internal_affairs/redundant_location_argument.rb
307
309
  - lib/rubocop/cop/internal_affairs/redundant_message_argument.rb
@@ -978,6 +980,7 @@ files:
978
980
  - lib/rubocop/formatter/text_util.rb
979
981
  - lib/rubocop/formatter/worst_offenders_formatter.rb
980
982
  - lib/rubocop/lockfile.rb
983
+ - lib/rubocop/lsp.rb
981
984
  - lib/rubocop/lsp/logger.rb
982
985
  - lib/rubocop/lsp/routes.rb
983
986
  - lib/rubocop/lsp/runtime.rb
@@ -1028,12 +1031,12 @@ licenses:
1028
1031
  - MIT
1029
1032
  metadata:
1030
1033
  homepage_uri: https://rubocop.org/
1031
- changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.60.0
1034
+ changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.61.0
1032
1035
  source_code_uri: https://github.com/rubocop/rubocop/
1033
- documentation_uri: https://docs.rubocop.org/rubocop/1.60/
1036
+ documentation_uri: https://docs.rubocop.org/rubocop/1.61/
1034
1037
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1035
1038
  rubygems_mfa_required: 'true'
1036
- post_install_message:
1039
+ post_install_message:
1037
1040
  rdoc_options: []
1038
1041
  require_paths:
1039
1042
  - lib
@@ -1048,8 +1051,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1048
1051
  - !ruby/object:Gem::Version
1049
1052
  version: '0'
1050
1053
  requirements: []
1051
- rubygems_version: 3.4.22
1052
- signing_key:
1054
+ rubygems_version: 3.3.7
1055
+ signing_key:
1053
1056
  specification_version: 4
1054
1057
  summary: Automatic Ruby code style checking tool.
1055
1058
  test_files: []