rubocop 1.41.1 → 1.45.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +97 -31
  5. data/lib/rubocop/cli.rb +55 -9
  6. data/lib/rubocop/config.rb +7 -7
  7. data/lib/rubocop/config_loader.rb +12 -15
  8. data/lib/rubocop/config_loader_resolver.rb +8 -5
  9. data/lib/rubocop/cop/base.rb +89 -70
  10. data/lib/rubocop/cop/commissioner.rb +8 -2
  11. data/lib/rubocop/cop/cop.rb +51 -31
  12. data/lib/rubocop/cop/corrector.rb +30 -10
  13. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
  14. data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
  15. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  17. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  18. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
  19. data/lib/rubocop/cop/layout/class_structure.rb +31 -23
  20. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
  21. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  23. data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
  24. data/lib/rubocop/cop/layout/indentation_style.rb +4 -1
  25. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +6 -6
  26. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  27. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  28. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  29. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
  30. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  31. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  32. data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -2
  33. data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
  34. data/lib/rubocop/cop/lint/debugger.rb +8 -27
  35. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  36. data/lib/rubocop/cop/lint/else_layout.rb +2 -6
  37. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
  38. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
  39. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  40. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  41. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
  42. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
  43. data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
  44. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  45. data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
  46. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
  47. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
  48. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
  49. data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
  50. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
  51. data/lib/rubocop/cop/lint/void.rb +19 -10
  52. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  53. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  54. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  55. data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
  56. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  57. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
  58. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  59. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  60. data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
  61. data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
  62. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +57 -23
  63. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  64. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  65. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
  66. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  67. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  68. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
  69. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  70. data/lib/rubocop/cop/registry.rb +34 -29
  71. data/lib/rubocop/cop/security/compound_hash.rb +2 -1
  72. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
  73. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
  74. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  75. data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
  76. data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
  77. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  78. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  79. data/lib/rubocop/cop/style/concat_array_literals.rb +22 -2
  80. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
  81. data/lib/rubocop/cop/style/documentation.rb +1 -1
  82. data/lib/rubocop/cop/style/documentation_method.rb +6 -0
  83. data/lib/rubocop/cop/style/guard_clause.rb +11 -7
  84. data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
  85. data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
  86. data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
  87. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  88. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  89. data/lib/rubocop/cop/style/map_to_set.rb +61 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
  91. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  92. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
  93. data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
  94. data/lib/rubocop/cop/style/missing_else.rb +13 -1
  95. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  96. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  97. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  98. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
  99. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  100. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
  101. data/lib/rubocop/cop/style/operator_method_call.rb +16 -2
  102. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
  103. data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
  104. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  105. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  106. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  107. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  108. data/lib/rubocop/cop/style/redundant_string_escape.rb +4 -2
  109. data/lib/rubocop/cop/style/require_order.rb +6 -11
  110. data/lib/rubocop/cop/style/select_by_regexp.rb +6 -2
  111. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  112. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  113. data/lib/rubocop/cop/style/signal_exception.rb +8 -6
  114. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
  115. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  116. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
  117. data/lib/rubocop/cop/style/word_array.rb +42 -1
  118. data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
  119. data/lib/rubocop/cop/style/yoda_expression.rb +90 -0
  120. data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
  121. data/lib/rubocop/cop/team.rb +48 -43
  122. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  123. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
  124. data/lib/rubocop/cop/variable_force.rb +1 -4
  125. data/lib/rubocop/formatter.rb +0 -1
  126. data/lib/rubocop/options.rb +22 -1
  127. data/lib/rubocop/path_util.rb +17 -7
  128. data/lib/rubocop/result_cache.rb +1 -1
  129. data/lib/rubocop/rspec/expect_offense.rb +6 -4
  130. data/lib/rubocop/runner.rb +50 -7
  131. data/lib/rubocop/server/cache.rb +10 -3
  132. data/lib/rubocop/server/cli.rb +37 -18
  133. data/lib/rubocop/server/client_command/exec.rb +1 -1
  134. data/lib/rubocop/server/client_command/start.rb +6 -1
  135. data/lib/rubocop/server/core.rb +23 -8
  136. data/lib/rubocop/target_ruby.rb +0 -1
  137. data/lib/rubocop/version.rb +1 -1
  138. data/lib/rubocop.rb +8 -0
  139. metadata +21 -33
@@ -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]
@@ -97,8 +97,10 @@ module RuboCop
97
97
  scope_stack.reverse_each do |scope|
98
98
  variable = scope.variables[name]
99
99
  return variable if variable
100
+
100
101
  # Only block scope allows referencing outer scope variables.
101
- return nil unless scope.node.block_type?
102
+ node = scope.node
103
+ return nil unless node.block_type? || node.numblock_type?
102
104
  end
103
105
 
104
106
  nil
@@ -50,7 +50,7 @@ module RuboCop
50
50
 
51
51
  ZERO_ARITY_SUPER_TYPE = :zsuper
52
52
 
53
- TWISTED_SCOPE_TYPES = %i[block class sclass defs module].freeze
53
+ TWISTED_SCOPE_TYPES = %i[block numblock class sclass defs module].freeze
54
54
  SCOPE_TYPES = (TWISTED_SCOPE_TYPES + [:def]).freeze
55
55
 
56
56
  SEND_TYPE = :send
@@ -108,7 +108,6 @@ module RuboCop
108
108
  :skip_children
109
109
  end
110
110
 
111
- # rubocop:disable Layout/ClassStructure
112
111
  NODE_HANDLER_METHOD_NAMES = [
113
112
  [VARIABLE_ASSIGNMENT_TYPE, :process_variable_assignment],
114
113
  [REGEXP_NAMED_CAPTURE_TYPE, :process_regexp_named_captures],
@@ -123,8 +122,6 @@ module RuboCop
123
122
  *SCOPE_TYPES.product([:process_scope])
124
123
  ].to_h.freeze
125
124
  private_constant :NODE_HANDLER_METHOD_NAMES
126
- # rubocop:enable Layout/ClassStructure
127
-
128
125
  def node_handler_method_name(node)
129
126
  NODE_HANDLER_METHOD_NAMES[node.type]
130
127
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  # The bootstrap module for formatter.
5
- # @api private
6
5
  module Formatter
7
6
  require_relative 'formatter/text_util'
8
7
 
@@ -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
@@ -28,14 +28,19 @@ module RuboCop
28
28
  end
29
29
  end
30
30
 
31
+ SMART_PATH_CACHE = {} # rubocop:disable Style/MutableConstant
32
+ private_constant :SMART_PATH_CACHE
33
+
31
34
  def smart_path(path)
32
- # Ideally, we calculate this relative to the project root.
33
- base_dir = Dir.pwd
35
+ SMART_PATH_CACHE[path] ||= begin
36
+ # Ideally, we calculate this relative to the project root.
37
+ base_dir = Dir.pwd
34
38
 
35
- if path.start_with? base_dir
36
- relative_path(path, base_dir)
37
- else
38
- path
39
+ if path.start_with? base_dir
40
+ relative_path(path, base_dir)
41
+ else
42
+ path
43
+ end
39
44
  end
40
45
  end
41
46
 
@@ -46,7 +51,7 @@ module RuboCop
46
51
  matches =
47
52
  if pattern == path
48
53
  true
49
- elsif pattern.match?(/[*{\[?]/)
54
+ elsif glob?(pattern)
50
55
  File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
51
56
  end
52
57
 
@@ -68,6 +73,11 @@ module RuboCop
68
73
  %r{\A([A-Z]:)?/}i.match?(path)
69
74
  end
70
75
 
76
+ # Returns true for a glob
77
+ def glob?(path)
78
+ path.match?(/[*{\[?]/)
79
+ end
80
+
71
81
  def hidden_file_in_not_hidden_dir?(pattern, path)
72
82
  hidden_file?(path) &&
73
83
  File.fnmatch?(
@@ -53,7 +53,7 @@ module RuboCop
53
53
  def remove_oldest_files(files, dirs, cache_root, verbose)
54
54
  # Add 1 to half the number of files, so that we remove the file if
55
55
  # there's only 1 left.
56
- remove_count = 1 + (files.length / 2)
56
+ remove_count = (files.length / 2) + 1
57
57
  puts "Removing the #{remove_count} oldest files from #{cache_root}" if verbose
58
58
  sorted = files.sort_by { |path| File.mtime(path) }
59
59
  remove_files(sorted, dirs, remove_count)
@@ -6,7 +6,7 @@ module RuboCop
6
6
  #
7
7
  # This mixin makes it easier to specify strict offense expectations
8
8
  # in a declarative and visual fashion. Just type out the code that
9
- # should generate a offense, annotate code by writing '^'s
9
+ # should generate an offense, annotate code by writing '^'s
10
10
  # underneath each character that should be highlighted, and follow
11
11
  # the carets with a string (separated by a space) that is the
12
12
  # message of the offense. You can include multiple offenses in
@@ -126,7 +126,7 @@ module RuboCop
126
126
  @offenses
127
127
  end
128
128
 
129
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
129
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
130
130
  def expect_correction(correction, loop: true, source: nil)
131
131
  if source
132
132
  expected_annotations = parse_annotations(source, raise_error: false)
@@ -138,6 +138,8 @@ module RuboCop
138
138
 
139
139
  source = @processed_source.raw_source
140
140
 
141
+ raise 'Use `expect_no_corrections` if the code will not change' if correction == source
142
+
141
143
  iteration = 0
142
144
  new_source = loop do
143
145
  iteration += 1
@@ -157,11 +159,11 @@ module RuboCop
157
159
  _investigate(cop, @processed_source)
158
160
  end
159
161
 
160
- raise 'Use `expect_no_corrections` if the code will not change' if new_source == source
162
+ raise 'Expected correction but no corrections were made' if new_source == source
161
163
 
162
164
  expect(new_source).to eq(correction)
163
165
  end
164
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
166
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
165
167
 
166
168
  def expect_no_corrections
167
169
  raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source
@@ -24,9 +24,35 @@ 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
 
51
+ # @api private
52
+ REDUNDANT_COP_DISABLE_DIRECTIVE_RULES = %w[
53
+ Lint/RedundantCopDisableDirective RedundantCopDisableDirective Lint
54
+ ].freeze
55
+
30
56
  attr_reader :errors, :warnings
31
57
  attr_writer :aborting
32
58
 
@@ -194,7 +220,9 @@ module RuboCop
194
220
  end
195
221
 
196
222
  def check_for_redundant_disables?(source)
197
- !source.disabled_line_ranges.empty? && !filtered_run?
223
+ return false if source.disabled_line_ranges.empty? || except_redundant_cop_disable_directive?
224
+
225
+ !@options[:only]
198
226
  end
199
227
 
200
228
  def redundant_cop_disable_directive(file)
@@ -205,8 +233,8 @@ module RuboCop
205
233
  yield cop if cop.relevant_file?(file)
206
234
  end
207
235
 
208
- def filtered_run?
209
- @options[:except] || @options[:only]
236
+ def except_redundant_cop_disable_directive?
237
+ @options[:except] && (@options[:except] & REDUNDANT_COP_DISABLE_DIRECTIVE_RULES).any?
210
238
  end
211
239
 
212
240
  def file_started(file)
@@ -312,10 +340,25 @@ module RuboCop
312
340
  end
313
341
 
314
342
  def inspect_file(processed_source, team = mobilize_team(processed_source))
315
- report = team.investigate(processed_source)
316
- @errors.concat(team.errors)
317
- @warnings.concat(team.warnings)
318
- [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
319
362
  end
320
363
 
321
364
  def mobilize_team(processed_source)
@@ -57,18 +57,24 @@ 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
 
71
- config_yaml = YAML.safe_load(yaml_code, permitted_classes: [Regexp, Symbol])
74
+ require 'yaml'
75
+ config_yaml = YAML.safe_load(
76
+ yaml_code, permitted_classes: [Regexp, Symbol], aliases: true
77
+ )
72
78
 
73
79
  # For compatibility with Ruby 3.0 or lower.
74
80
  if Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0.0')
@@ -78,6 +84,7 @@ module RuboCop
78
84
  config_yaml&.dig('AllCops', 'CacheRootDirectory')
79
85
  end
80
86
  end
87
+ # rubocop:enable Metrics/MethodLength
81
88
 
82
89
  def port_path
83
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
@@ -244,7 +244,6 @@ module RuboCop
244
244
  ].freeze
245
245
 
246
246
  private_constant :SOURCES
247
-
248
247
  def initialize(config)
249
248
  @config = config
250
249
  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.41.1'
6
+ STRING = '1.45.1'
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
@@ -166,6 +166,7 @@ require_relative 'rubocop/cop/bundler/ordered_gems'
166
166
 
167
167
  require_relative 'rubocop/cop/gemspec/dependency_version'
168
168
  require_relative 'rubocop/cop/gemspec/deprecated_attribute_assignment'
169
+ require_relative 'rubocop/cop/gemspec/development_dependencies'
169
170
  require_relative 'rubocop/cop/gemspec/duplicated_assignment'
170
171
  require_relative 'rubocop/cop/gemspec/ordered_dependencies'
171
172
  require_relative 'rubocop/cop/gemspec/require_mfa'
@@ -399,6 +400,7 @@ require_relative 'rubocop/cop/lint/useless_access_modifier'
399
400
  require_relative 'rubocop/cop/lint/useless_assignment'
400
401
  require_relative 'rubocop/cop/lint/useless_else_without_rescue'
401
402
  require_relative 'rubocop/cop/lint/useless_method_definition'
403
+ require_relative 'rubocop/cop/lint/useless_rescue'
402
404
  require_relative 'rubocop/cop/lint/useless_ruby2_keywords'
403
405
  require_relative 'rubocop/cop/lint/useless_setter_call'
404
406
  require_relative 'rubocop/cop/lint/useless_times'
@@ -470,6 +472,7 @@ require_relative 'rubocop/cop/style/combinable_loops'
470
472
  require_relative 'rubocop/cop/style/command_literal'
471
473
  require_relative 'rubocop/cop/style/comment_annotation'
472
474
  require_relative 'rubocop/cop/style/commented_keyword'
475
+ require_relative 'rubocop/cop/style/comparable_clamp'
473
476
  require_relative 'rubocop/cop/style/concat_array_literals'
474
477
  require_relative 'rubocop/cop/style/conditional_assignment'
475
478
  require_relative 'rubocop/cop/style/constant_visibility'
@@ -531,6 +534,7 @@ require_relative 'rubocop/cop/style/in_pattern_then'
531
534
  require_relative 'rubocop/cop/style/infinite_loop'
532
535
  require_relative 'rubocop/cop/style/inverse_methods'
533
536
  require_relative 'rubocop/cop/style/inline_comment'
537
+ require_relative 'rubocop/cop/style/invertible_unless_condition'
534
538
  require_relative 'rubocop/cop/style/ip_addresses'
535
539
  require_relative 'rubocop/cop/style/keyword_parameters_order'
536
540
  require_relative 'rubocop/cop/style/lambda'
@@ -538,8 +542,10 @@ require_relative 'rubocop/cop/style/lambda_call'
538
542
  require_relative 'rubocop/cop/style/line_end_concatenation'
539
543
  require_relative 'rubocop/cop/style/magic_comment_format'
540
544
  require_relative 'rubocop/cop/style/map_to_hash'
545
+ require_relative 'rubocop/cop/style/map_to_set'
541
546
  require_relative 'rubocop/cop/style/method_call_without_args_parentheses'
542
547
  require_relative 'rubocop/cop/style/method_call_with_args_parentheses'
548
+ require_relative 'rubocop/cop/style/min_max_comparison'
543
549
  require_relative 'rubocop/cop/style/multiline_in_pattern_then'
544
550
  require_relative 'rubocop/cop/style/numbered_parameters'
545
551
  require_relative 'rubocop/cop/style/open_struct_use'
@@ -550,6 +556,7 @@ require_relative 'rubocop/cop/style/redundant_double_splat_hash_braces'
550
556
  require_relative 'rubocop/cop/style/redundant_each'
551
557
  require_relative 'rubocop/cop/style/redundant_fetch_block'
552
558
  require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
559
+ require_relative 'rubocop/cop/style/redundant_heredoc_delimiter_quotes'
553
560
  require_relative 'rubocop/cop/style/redundant_initialize'
554
561
  require_relative 'rubocop/cop/style/redundant_self_assignment'
555
562
  require_relative 'rubocop/cop/style/redundant_self_assignment_branch'
@@ -675,6 +682,7 @@ require_relative 'rubocop/cop/style/while_until_do'
675
682
  require_relative 'rubocop/cop/style/while_until_modifier'
676
683
  require_relative 'rubocop/cop/style/word_array'
677
684
  require_relative 'rubocop/cop/style/yoda_condition'
685
+ require_relative 'rubocop/cop/style/yoda_expression'
678
686
  require_relative 'rubocop/cop/style/zero_length_predicate'
679
687
 
680
688
  require_relative 'rubocop/cop/security/compound_hash'