rubocop 1.44.1 → 1.45.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +16 -31
  4. data/lib/rubocop/cli.rb +54 -8
  5. data/lib/rubocop/config_loader_resolver.rb +3 -4
  6. data/lib/rubocop/cop/base.rb +27 -9
  7. data/lib/rubocop/cop/commissioner.rb +8 -2
  8. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  9. data/lib/rubocop/cop/layout/class_structure.rb +2 -16
  10. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  11. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  12. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  13. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -11
  14. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  15. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  16. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -1
  17. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  18. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
  19. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
  20. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  21. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  22. data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
  23. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +13 -9
  24. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  25. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  26. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  27. data/lib/rubocop/cop/style/access_modifier_declarations.rb +8 -1
  28. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
  29. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  30. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  31. data/lib/rubocop/cop/style/documentation.rb +1 -1
  32. data/lib/rubocop/cop/style/documentation_method.rb +6 -0
  33. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  34. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  35. data/lib/rubocop/cop/style/operator_method_call.rb +1 -1
  36. data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
  37. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  38. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  39. data/lib/rubocop/cop/style/word_array.rb +1 -1
  40. data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
  41. data/lib/rubocop/cop/style/yoda_expression.rb +11 -2
  42. data/lib/rubocop/cop/team.rb +19 -14
  43. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  44. data/lib/rubocop/options.rb +22 -1
  45. data/lib/rubocop/runner.rb +40 -4
  46. data/lib/rubocop/server/cache.rb +7 -2
  47. data/lib/rubocop/server/cli.rb +37 -18
  48. data/lib/rubocop/server/client_command/exec.rb +1 -1
  49. data/lib/rubocop/server/client_command/start.rb +6 -1
  50. data/lib/rubocop/server/core.rb +23 -8
  51. data/lib/rubocop/version.rb +1 -1
  52. data/lib/rubocop.rb +1 -0
  53. metadata +8 -27
@@ -45,9 +45,9 @@ module RuboCop
45
45
  node
46
46
  else
47
47
  child_index = case node.type
48
- when :module, :sclass then 1
49
- when :def, :class, :block then 2
50
- when :defs then 3
48
+ when :module, :sclass then 1
49
+ when :def, :class, :block, :numblock then 2
50
+ when :defs then 3
51
51
  end
52
52
 
53
53
  node.children[child_index]
@@ -61,6 +61,9 @@ module RuboCop
61
61
  add_config_generation_options(opts)
62
62
  add_additional_modes(opts)
63
63
  add_general_options(opts)
64
+
65
+ # `stackprof` is not supported on JRuby and Windows.
66
+ add_profile_options(opts) if RUBY_ENGINE == 'ruby' && !Platform.windows?
64
67
  end
65
68
  end
66
69
 
@@ -206,6 +209,7 @@ module RuboCop
206
209
  option(opts, '--start-server')
207
210
  option(opts, '--stop-server')
208
211
  option(opts, '--server-status')
212
+ option(opts, '--no-detach')
209
213
  end
210
214
  end
211
215
 
@@ -233,6 +237,16 @@ module RuboCop
233
237
  end
234
238
  end
235
239
 
240
+ def add_profile_options(opts)
241
+ section(opts, 'Profiling Options') do
242
+ option(opts, '--profile') do
243
+ @options[:profile] = true
244
+ @options[:cache] = 'false' unless @options.key?(:cache)
245
+ end
246
+ option(opts, '--memory')
247
+ end
248
+ end
249
+
236
250
  def handle_deprecated_option(old_option, new_option)
237
251
  warn rainbow.wrap("#{old_option} is deprecated; use #{new_option} instead.").yellow
238
252
  @options[long_opt_symbol([new_option])] = @options.delete(long_opt_symbol([old_option]))
@@ -424,6 +438,8 @@ module RuboCop
424
438
  def invalid_arguments_for_parallel
425
439
  [('--auto-gen-config' if @options.key?(:auto_gen_config)),
426
440
  ('-F/--fail-fast' if @options.key?(:fail_fast)),
441
+ ('--profile' if @options[:profile]),
442
+ ('--memory' if @options[:memory]),
427
443
  ('--cache false' if @options > { cache: 'false' })].compact
428
444
  end
429
445
 
@@ -465,6 +481,7 @@ module RuboCop
465
481
 
466
482
  # This module contains help texts for command line options.
467
483
  # @api private
484
+ # rubocop:disable Metrics/ModuleLength
468
485
  module OptionsHelp
469
486
  MAX_EXCL = RuboCop::Options::DEFAULT_MAXIMUM_EXCLUSION_ITEMS.to_s
470
487
  FORMATTER_OPTION_LIST = RuboCop::Formatter::FormatterSet::BUILTIN_FORMATTERS_FOR_KEYS.keys
@@ -599,9 +616,13 @@ module RuboCop
599
616
  start_server: 'Start server process.',
600
617
  stop_server: 'Stop server process.',
601
618
  server_status: 'Show server status.',
619
+ no_detach: 'Run the server process in the foreground.',
602
620
  raise_cop_error: ['Raise cop-related errors with cause and location.',
603
621
  'This is used to prevent cops from failing silently.',
604
- 'Default is false.']
622
+ 'Default is false.'],
623
+ profile: 'Profile rubocop',
624
+ memory: 'Profile rubocop memory usage'
605
625
  }.freeze
606
626
  end
627
+ # rubocop:enable Metrics/ModuleLength
607
628
  end
@@ -24,6 +24,27 @@ module RuboCop
24
24
  end
25
25
  end
26
26
 
27
+ class << self
28
+ # @return [Array<#call>]
29
+ def ruby_extractors
30
+ @ruby_extractors ||= [default_ruby_extractor]
31
+ end
32
+
33
+ private
34
+
35
+ # @return [#call]
36
+ def default_ruby_extractor
37
+ lambda do |processed_source|
38
+ [
39
+ {
40
+ offset: 0,
41
+ processed_source: processed_source
42
+ }
43
+ ]
44
+ end
45
+ end
46
+ end
47
+
27
48
  # @api private
28
49
  MAX_ITERATIONS = 200
29
50
 
@@ -319,10 +340,25 @@ module RuboCop
319
340
  end
320
341
 
321
342
  def inspect_file(processed_source, team = mobilize_team(processed_source))
322
- report = team.investigate(processed_source)
323
- @errors.concat(team.errors)
324
- @warnings.concat(team.warnings)
325
- [report.offenses, team.updated_source_file?]
343
+ extracted_ruby_sources = extract_ruby_sources(processed_source)
344
+ offenses = extracted_ruby_sources.flat_map do |extracted_ruby_source|
345
+ report = team.investigate(
346
+ extracted_ruby_source[:processed_source],
347
+ offset: extracted_ruby_source[:offset],
348
+ original: processed_source
349
+ )
350
+ @errors.concat(team.errors)
351
+ @warnings.concat(team.warnings)
352
+ report.offenses
353
+ end
354
+ [offenses, team.updated_source_file?]
355
+ end
356
+
357
+ def extract_ruby_sources(processed_source)
358
+ self.class.ruby_extractors.find do |ruby_extractor|
359
+ result = ruby_extractor.call(processed_source)
360
+ break result if result
361
+ end
326
362
  end
327
363
 
328
364
  def mobilize_team(processed_source)
@@ -57,17 +57,21 @@ module RuboCop
57
57
  File.expand_path(File.join(cache_root_dir, 'server'))
58
58
  end
59
59
 
60
+ # rubocop:disable Metrics/MethodLength
60
61
  def cache_root_dir_from_config
61
62
  CacheConfig.root_dir do
62
63
  # `RuboCop::ConfigStore` has heavy dependencies, this is a lightweight implementation
63
64
  # so that only the necessary `CacheRootDirectory` can be obtained.
64
- require 'yaml'
65
65
  config_path = ConfigFinder.find_config_path(Dir.pwd)
66
+ file_contents = File.read(config_path)
67
+
68
+ # Returns early if `CacheRootDirectory` is not used before requiring `erb` or `yaml`.
69
+ next unless file_contents.include?('CacheRootDirectory')
66
70
 
67
71
  require 'erb'
68
- file_contents = File.read(config_path)
69
72
  yaml_code = ERB.new(file_contents).result
70
73
 
74
+ require 'yaml'
71
75
  config_yaml = YAML.safe_load(
72
76
  yaml_code, permitted_classes: [Regexp, Symbol], aliases: true
73
77
  )
@@ -80,6 +84,7 @@ module RuboCop
80
84
  config_yaml&.dig('AllCops', 'CacheRootDirectory')
81
85
  end
82
86
  end
87
+ # rubocop:enable Metrics/MethodLength
83
88
 
84
89
  def port_path
85
90
  dir.join('port')
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rainbow'
4
3
  require_relative '../arguments_env'
5
4
  require_relative '../arguments_file'
6
5
 
@@ -23,15 +22,21 @@ module RuboCop
23
22
  STATUS_ERROR = 2
24
23
 
25
24
  SERVER_OPTIONS = %w[
26
- --server --no-server --server-status --restart-server --start-server --stop-server
25
+ --server
26
+ --no-server
27
+ --server-status
28
+ --restart-server
29
+ --start-server
30
+ --stop-server
31
+ --no-detach
27
32
  ].freeze
28
33
  EXCLUSIVE_OPTIONS = (SERVER_OPTIONS - %w[--server --no-server]).freeze
34
+ NO_DETACH_OPTIONS = %w[--server --start-server --restart-server].freeze
29
35
 
30
36
  def initialize
31
37
  @exit = false
32
38
  end
33
39
 
34
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
35
40
  def run(argv = ARGV)
36
41
  unless Server.support_server?
37
42
  return error('RuboCop server is not supported by this Ruby.') if use_server_option?(argv)
@@ -40,16 +45,34 @@ module RuboCop
40
45
  end
41
46
 
42
47
  Cache.cache_root_path = fetch_cache_root_path_from(argv)
43
- deleted_server_arguments = delete_server_argument_from(argv)
44
48
 
45
- if deleted_server_arguments.size >= 2
46
- return error("#{deleted_server_arguments.join(', ')} cannot be specified together.")
49
+ process_arguments(argv)
50
+ end
51
+
52
+ def exit?
53
+ @exit
54
+ end
55
+
56
+ private
57
+
58
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
59
+ def process_arguments(argv)
60
+ server_arguments = delete_server_argument_from(argv)
61
+
62
+ detach = !server_arguments.delete('--no-detach')
63
+
64
+ if server_arguments.size >= 2
65
+ return error("#{server_arguments.join(', ')} cannot be specified together.")
47
66
  end
48
67
 
49
- server_command = deleted_server_arguments.first
68
+ server_command = server_arguments.first
69
+
70
+ unless detach || NO_DETACH_OPTIONS.include?(server_command)
71
+ return error("#{server_command} cannot be combined with --no-detach.")
72
+ end
50
73
 
51
74
  if EXCLUSIVE_OPTIONS.include?(server_command) && argv.count > allowed_option_count
52
- return error("#{server_command} cannot be combined with other options.")
75
+ return error("#{server_command} cannot be combined with #{argv[0]}.")
53
76
  end
54
77
 
55
78
  if server_command.nil?
@@ -57,23 +80,17 @@ module RuboCop
57
80
  ArgumentsFile.read_as_arguments.delete('--server')
58
81
  end
59
82
 
60
- run_command(server_command)
83
+ run_command(server_command, detach: detach)
61
84
 
62
85
  STATUS_SUCCESS
63
86
  end
64
87
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
65
88
 
66
- def exit?
67
- @exit
68
- end
69
-
70
- private
71
-
72
89
  # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength:
73
- def run_command(server_command)
90
+ def run_command(server_command, detach:)
74
91
  case server_command
75
92
  when '--server'
76
- Server::ClientCommand::Start.new.run unless Server.running?
93
+ Server::ClientCommand::Start.new(detach: detach).run unless Server.running?
77
94
  when '--no-server'
78
95
  Server::ClientCommand::Stop.new.run if Server.running?
79
96
  when '--restart-server'
@@ -81,7 +98,7 @@ module RuboCop
81
98
  Server::ClientCommand::Restart.new.run
82
99
  when '--start-server'
83
100
  @exit = true
84
- Server::ClientCommand::Start.new.run
101
+ Server::ClientCommand::Start.new(detach: detach).run
85
102
  when '--stop-server'
86
103
  @exit = true
87
104
  Server::ClientCommand::Stop.new.run
@@ -118,6 +135,8 @@ module RuboCop
118
135
  end
119
136
 
120
137
  def error(message)
138
+ require 'rainbow'
139
+
121
140
  @exit = true
122
141
  warn Rainbow(message).red
123
142
 
@@ -41,7 +41,7 @@ module RuboCop
41
41
  end
42
42
 
43
43
  def incompatible_version?
44
- RuboCop::Version::STRING != Cache.version_path.read
44
+ Cache.version_path.read != RuboCop::Version::STRING
45
45
  end
46
46
 
47
47
  def stderr
@@ -15,6 +15,11 @@ module RuboCop
15
15
  # This class is a client command to start server process.
16
16
  # @api private
17
17
  class Start < Base
18
+ def initialize(detach: true)
19
+ @detach = detach
20
+ super()
21
+ end
22
+
18
23
  def run
19
24
  if Server.running?
20
25
  warn "RuboCop server (#{Cache.pid_path.read}) is already running."
@@ -34,7 +39,7 @@ module RuboCop
34
39
  host = ENV.fetch('RUBOCOP_SERVER_HOST', '127.0.0.1')
35
40
  port = ENV.fetch('RUBOCOP_SERVER_PORT', 0)
36
41
 
37
- Server::Core.new.start(host, port)
42
+ Server::Core.new.start(host, port, detach: @detach)
38
43
  end
39
44
  end
40
45
  end
@@ -27,31 +27,46 @@ module RuboCop
27
27
  self.class.token
28
28
  end
29
29
 
30
- def start(host, port)
30
+ def start(host, port, detach: true)
31
31
  $PROGRAM_NAME = "rubocop --server #{Cache.project_dir}"
32
32
 
33
- require 'rubocop'
33
+ require_relative '../../rubocop'
34
34
  start_server(host, port)
35
35
 
36
- demonize if server_mode?
36
+ return unless server_mode?
37
+
38
+ detach ? detach_server : run_server
37
39
  end
38
40
 
39
41
  private
40
42
 
41
- def demonize
42
- Cache.write_port_and_token_files(port: @server.addr[1], token: token)
43
+ def detach_server
44
+ write_port_and_token_files
43
45
 
44
46
  pid = fork do
45
47
  Process.daemon(true)
46
48
  $stderr.reopen(Cache.stderr_path, 'w')
47
- Cache.write_pid_file do
48
- read_socket(@server.accept) until @server.closed?
49
- end
49
+ process_input
50
50
  end
51
51
 
52
52
  Process.waitpid(pid)
53
53
  end
54
54
 
55
+ def write_port_and_token_files
56
+ Cache.write_port_and_token_files(port: @server.addr[1], token: token)
57
+ end
58
+
59
+ def process_input
60
+ Cache.write_pid_file do
61
+ read_socket(@server.accept) until @server.closed?
62
+ end
63
+ end
64
+
65
+ def run_server
66
+ write_port_and_token_files
67
+ process_input
68
+ end
69
+
55
70
  def server_mode?
56
71
  true
57
72
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.44.1'
6
+ STRING = '1.45.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
data/lib/rubocop.rb CHANGED
@@ -556,6 +556,7 @@ require_relative 'rubocop/cop/style/redundant_double_splat_hash_braces'
556
556
  require_relative 'rubocop/cop/style/redundant_each'
557
557
  require_relative 'rubocop/cop/style/redundant_fetch_block'
558
558
  require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
559
+ require_relative 'rubocop/cop/style/redundant_heredoc_delimiter_quotes'
559
560
  require_relative 'rubocop/cop/style/redundant_initialize'
560
561
  require_relative 'rubocop/cop/style/redundant_self_assignment'
561
562
  require_relative 'rubocop/cop/style/redundant_self_assignment_branch'
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.44.1
4
+ version: 1.45.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: 2023-01-25 00:00:00.000000000 Z
13
+ date: 2023-02-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -168,26 +168,6 @@ dependencies:
168
168
  - - "<"
169
169
  - !ruby/object:Gem::Version
170
170
  version: '3.0'
171
- - !ruby/object:Gem::Dependency
172
- name: bundler
173
- requirement: !ruby/object:Gem::Requirement
174
- requirements:
175
- - - ">="
176
- - !ruby/object:Gem::Version
177
- version: 1.15.0
178
- - - "<"
179
- - !ruby/object:Gem::Version
180
- version: '3.0'
181
- type: :development
182
- prerelease: false
183
- version_requirements: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - ">="
186
- - !ruby/object:Gem::Version
187
- version: 1.15.0
188
- - - "<"
189
- - !ruby/object:Gem::Version
190
- version: '3.0'
191
171
  description: |2
192
172
  RuboCop is a Ruby code style checking and code formatting tool.
193
173
  It aims to enforce the community-driven Ruby Style Guide.
@@ -837,6 +817,7 @@ files:
837
817
  - lib/rubocop/cop/style/redundant_fetch_block.rb
838
818
  - lib/rubocop/cop/style/redundant_file_extension_in_require.rb
839
819
  - lib/rubocop/cop/style/redundant_freeze.rb
820
+ - lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb
840
821
  - lib/rubocop/cop/style/redundant_initialize.rb
841
822
  - lib/rubocop/cop/style/redundant_interpolation.rb
842
823
  - lib/rubocop/cop/style/redundant_parentheses.rb
@@ -999,10 +980,10 @@ metadata:
999
980
  homepage_uri: https://rubocop.org/
1000
981
  changelog_uri: https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md
1001
982
  source_code_uri: https://github.com/rubocop/rubocop/
1002
- documentation_uri: https://docs.rubocop.org/rubocop/1.44/
983
+ documentation_uri: https://docs.rubocop.org/rubocop/1.45/
1003
984
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1004
985
  rubygems_mfa_required: 'true'
1005
- post_install_message:
986
+ post_install_message:
1006
987
  rdoc_options: []
1007
988
  require_paths:
1008
989
  - lib
@@ -1017,8 +998,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1017
998
  - !ruby/object:Gem::Version
1018
999
  version: '0'
1019
1000
  requirements: []
1020
- rubygems_version: 3.1.2
1021
- signing_key:
1001
+ rubygems_version: 3.3.7
1002
+ signing_key:
1022
1003
  specification_version: 4
1023
1004
  summary: Automatic Ruby code style checking tool.
1024
1005
  test_files: []