rubocop 1.86.1 → 1.87.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.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +15 -1
  3. data/lib/rubocop/cli/command/auto_generate_config.rb +27 -1
  4. data/lib/rubocop/cli/command/list_enabled_cops_for.rb +40 -0
  5. data/lib/rubocop/cli/command/show_docs_url.rb +3 -7
  6. data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
  7. data/lib/rubocop/cli.rb +6 -7
  8. data/lib/rubocop/comment_config.rb +12 -15
  9. data/lib/rubocop/config_loader.rb +17 -2
  10. data/lib/rubocop/config_loader_resolver.rb +11 -3
  11. data/lib/rubocop/config_store.rb +1 -1
  12. data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
  13. data/lib/rubocop/cop/base.rb +8 -2
  14. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -5
  15. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +33 -2
  16. data/lib/rubocop/cop/correctors.rb +28 -0
  17. data/lib/rubocop/cop/exclude_limit.rb +31 -5
  18. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
  19. data/lib/rubocop/cop/gemspec/require_mfa.rb +4 -4
  20. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
  21. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +1 -0
  22. data/lib/rubocop/cop/layout/begin_end_alignment.rb +1 -1
  23. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  24. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +14 -5
  25. data/lib/rubocop/cop/layout/end_alignment.rb +2 -2
  26. data/lib/rubocop/cop/layout/indentation_width.rb +13 -1
  27. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  28. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +26 -1
  29. data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -1
  30. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -1
  31. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  32. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
  33. data/lib/rubocop/cop/lint/constant_reassignment.rb +36 -4
  34. data/lib/rubocop/cop/lint/constant_resolution.rb +5 -5
  35. data/lib/rubocop/cop/lint/deprecated_constants.rb +1 -1
  36. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  37. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
  38. data/lib/rubocop/cop/lint/multiple_comparison.rb +2 -2
  39. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +1 -1
  40. data/lib/rubocop/cop/lint/number_conversion.rb +5 -5
  41. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
  42. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +3 -13
  43. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +2 -2
  44. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +3 -3
  45. data/lib/rubocop/cop/lint/require_relative_self_path.rb +3 -1
  46. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  47. data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +1 -1
  48. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -2
  49. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -8
  50. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
  51. data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +18 -7
  52. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  53. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  54. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +1 -1
  55. data/lib/rubocop/cop/mixin/configurable_max.rb +6 -5
  56. data/lib/rubocop/cop/mixin/project_index_help.rb +48 -0
  57. data/lib/rubocop/cop/mixin.rb +86 -0
  58. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  59. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  60. data/lib/rubocop/cop/naming/predicate_method.rb +2 -2
  61. data/lib/rubocop/cop/naming/predicate_prefix.rb +1 -1
  62. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  63. data/lib/rubocop/cop/offense.rb +8 -0
  64. data/lib/rubocop/cop/registry.rb +42 -25
  65. data/lib/rubocop/cop/security/io_methods.rb +1 -1
  66. data/lib/rubocop/cop/style/alias.rb +10 -1
  67. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  68. data/lib/rubocop/cop/style/class_and_module_children.rb +8 -0
  69. data/lib/rubocop/cop/style/copyright.rb +21 -10
  70. data/lib/rubocop/cop/style/date_time.rb +2 -2
  71. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  72. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +6 -1
  73. data/lib/rubocop/cop/style/file_write.rb +18 -16
  74. data/lib/rubocop/cop/style/format_string.rb +4 -3
  75. data/lib/rubocop/cop/style/hash_conversion.rb +1 -1
  76. data/lib/rubocop/cop/style/hash_lookup_method.rb +12 -7
  77. data/lib/rubocop/cop/style/if_inside_else.rb +15 -2
  78. data/lib/rubocop/cop/style/magic_comment_format.rb +1 -1
  79. data/lib/rubocop/cop/style/min_max_comparison.rb +1 -1
  80. data/lib/rubocop/cop/style/module_member_existence_check.rb +6 -3
  81. data/lib/rubocop/cop/style/reduce_to_hash.rb +16 -0
  82. data/lib/rubocop/cop/style/redundant_array_constructor.rb +2 -2
  83. data/lib/rubocop/cop/style/redundant_constant_base.rb +5 -5
  84. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +2 -2
  85. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  86. data/lib/rubocop/cop/style/regexp_literal.rb +31 -2
  87. data/lib/rubocop/cop/style/rescue_modifier.rb +3 -3
  88. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  89. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -2
  90. data/lib/rubocop/cop/style/struct_inheritance.rb +13 -0
  91. data/lib/rubocop/cop/style/symbol_proc.rb +3 -3
  92. data/lib/rubocop/cop/style/top_level_method_definition.rb +2 -2
  93. data/lib/rubocop/cop/style/unless_logical_operators.rb +3 -3
  94. data/lib/rubocop/cop/style/while_until_modifier.rb +16 -0
  95. data/lib/rubocop/cop/style/yoda_condition.rb +1 -1
  96. data/lib/rubocop/cop/team.rb +86 -35
  97. data/lib/rubocop/file_patterns.rb +9 -1
  98. data/lib/rubocop/formatter/disabled_config_formatter.rb +4 -1
  99. data/lib/rubocop/lsp/runtime.rb +1 -2
  100. data/lib/rubocop/options.rb +26 -4
  101. data/lib/rubocop/project_index_loader.rb +66 -0
  102. data/lib/rubocop/rspec/shared_contexts.rb +21 -0
  103. data/lib/rubocop/runner.rb +123 -57
  104. data/lib/rubocop/target_finder.rb +13 -6
  105. data/lib/rubocop/version.rb +20 -2
  106. data/lib/rubocop.rb +8 -96
  107. metadata +8 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7e3463c6020de496396b13ecf80f691f532af83ac8f8627286f3887f07b11b2
4
- data.tar.gz: 9e8c332d0f33ea078abcc71e0ddb7cd009497842a026dfdcae103a63fc909ba6
3
+ metadata.gz: 9386e7f8b3fc90e4096eb5436a4d35612586e27dd1ff46ba40e1051a7909fd52
4
+ data.tar.gz: 615ec2ec69759dce081efc1fddb8469a7ddf6a72223fea741368b501f333bce0
5
5
  SHA512:
6
- metadata.gz: 0f0d7cff4de9dc7d8f04cdea8f8f28d6e45d5c0676a40ca67daa4e3fd6e3397363d2c2f4b368b5bcbb165d7478bdfb33c5352c49d2fd52896f8aa6d488e017cc
7
- data.tar.gz: 357c3047ffea023a5c10162e4e2f2ee782796b5ca87d7c4413bde296d284c41362033ca9d997609ac6d9eceea7227b46e240150340a8a26cad8071bc5cce33c7
6
+ metadata.gz: c07cb70ee9d4c997a85c079361624d43574b78e6c364e8d0ff939b4d826c19b0e1ef2aad9569aea4f2f247b9b792ae9b21a776f69161da1c9a193e2f3de74d40
7
+ data.tar.gz: f6b44267bdfeba1237e06af52009143f13a7c0419a5ccaddf47dc9701c328f2b2ef6d87811ae5c8f05ece533ca236425e599c77a5a283ba44c641de09fc1b7e6
data/config/default.yml CHANGED
@@ -104,6 +104,12 @@ AllCops:
104
104
  # When `NewCops` is `disable`, pending cops are disabled in bulk. Can be overridden by
105
105
  # the `--disable-pending-cops` command-line option.
106
106
  NewCops: pending
107
+ # When `true`, RuboCop builds a project index (a project-wide map of declarations and references)
108
+ # once per run and makes it available to cops that opt in.
109
+ # The project index is implemented by the optional `rubydex` gem; when `rubydex` is not installed,
110
+ # a warning is shown and the flag has no effect.
111
+ # The default `false` preserves RuboCop's traditional file-local analysis.
112
+ UseProjectIndex: false
107
113
  # Enables the result cache if `true`. Can be overridden by the `--cache` command
108
114
  # line option.
109
115
  UseCache: true
@@ -529,6 +535,10 @@ Layout/ClosingParenthesisIndentation:
529
535
  Description: 'Checks the indentation of hanging closing parentheses.'
530
536
  Enabled: true
531
537
  VersionAdded: '0.49'
538
+ VersionChanged: '1.86'
539
+ # By default the indentation width from `Layout/IndentationWidth` is used,
540
+ # but it can be overridden by setting this parameter.
541
+ IndentationWidth: ~
532
542
 
533
543
  Layout/CommentIndentation:
534
544
  Description: 'Indentation of comments.'
@@ -537,7 +547,10 @@ Layout/CommentIndentation:
537
547
  # with a comment on the preceding line.
538
548
  AllowForAlignment: false
539
549
  VersionAdded: '0.49'
540
- VersionChanged: '1.24'
550
+ VersionChanged: '1.86'
551
+ # By default the indentation width from `Layout/IndentationWidth` is used,
552
+ # but it can be overridden by setting this parameter.
553
+ IndentationWidth: ~
541
554
 
542
555
  Layout/ConditionPosition:
543
556
  Description: >-
@@ -1696,6 +1709,7 @@ Lint/ConstantReassignment:
1696
1709
  Description: 'Checks for constant reassignments.'
1697
1710
  Enabled: pending
1698
1711
  VersionAdded: '1.70'
1712
+ VersionChanged: '1.87'
1699
1713
 
1700
1714
  Lint/ConstantResolution:
1701
1715
  Description: 'Checks that constants are fully qualified with `::`.'
@@ -24,13 +24,39 @@ module RuboCop
24
24
 
25
25
  def run
26
26
  add_formatter
27
+ use_temporary_cache
28
+ reset_auto_gen_tmp_dir
27
29
  reset_config_and_auto_gen_file
28
30
  line_length_contents = maybe_run_line_length_cop
29
- run_all_cops(line_length_contents)
31
+ result = run_all_cops(line_length_contents)
32
+ reset_auto_gen_tmp_dir
33
+ result
30
34
  end
31
35
 
32
36
  private
33
37
 
38
+ def use_temporary_cache
39
+ # Use a separate cache directory to ensure MinDigits and Max values are calculated.
40
+ # This allows parallel execution (which requires cache) while ensuring MinDigits
41
+ # and Max values are computed, since the cache starts empty.
42
+ @options[:cache_root] = auto_gen_tmp_dir.join('cache').to_s
43
+ end
44
+
45
+ def reset_auto_gen_tmp_dir
46
+ auto_gen_tmp_dir.rmtree if auto_gen_tmp_dir.exist?
47
+ auto_gen_tmp_dir.mkpath
48
+ end
49
+
50
+ def auto_gen_tmp_dir
51
+ @auto_gen_tmp_dir ||= Pathname.new(
52
+ RuboCop::CacheConfig.root_dir_from_toplevel_config
53
+ ).join('auto-gen-tmp').tap do |path|
54
+ path.mkpath
55
+ # Set the temp directory path for ExcludeLimit to use
56
+ RuboCop::ExcludeLimit.tmp_dir = path
57
+ end
58
+ end
59
+
34
60
  def maybe_run_line_length_cop
35
61
  if only_exclude?
36
62
  skip_line_length_cop(PHASE_1_SKIPPED_ONLY_EXCLUDE)
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ class CLI
5
+ module Command
6
+ # Lists the cops that will inspect the given file or directory.
7
+ # @api private
8
+ class ListEnabledCopsFor < Base
9
+ self.command_name = :list_enabled_cops_for
10
+
11
+ def initialize(env)
12
+ super
13
+
14
+ # Load the configs so the require()s are done for custom cops
15
+ @config = @config_store.for(@options[:list_enabled_cops_for])
16
+ end
17
+
18
+ def run
19
+ print_available_cops
20
+ end
21
+
22
+ private
23
+
24
+ def print_available_cops
25
+ registry = Cop::Registry.global
26
+
27
+ registry.departments.sort.each do |department|
28
+ puts cops_of_department(registry, department).sort
29
+ end
30
+ end
31
+
32
+ def cops_of_department(registry, department)
33
+ registry.with_department(department)
34
+ .map(&:cop_name)
35
+ .select { |cop_name| @config.cop_enabled?(cop_name) }
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -25,10 +25,10 @@ module RuboCop
25
25
  puts Cop::Documentation.default_base_url if cops_array.empty?
26
26
 
27
27
  cops_array.each do |cop_name|
28
- cop = registry_hash[cop_name]
29
- next if cop.empty?
28
+ cop = Cop::Registry.global.find_by_cop_name(cop_name)
29
+ next unless cop
30
30
 
31
- url = Cop::Documentation.url_for(cop.first, @config)
31
+ url = Cop::Documentation.url_for(cop, @config)
32
32
  puts url if url
33
33
  end
34
34
 
@@ -38,10 +38,6 @@ module RuboCop
38
38
  def cops_array
39
39
  @cops_array ||= @options[:show_docs_url]
40
40
  end
41
-
42
- def registry_hash
43
- @registry_hash ||= Cop::Registry.global.to_h
44
- end
45
41
  end
46
42
  end
47
43
  end
@@ -60,7 +60,7 @@ module RuboCop
60
60
  def print_opt_out_instruction
61
61
  puts
62
62
  puts 'You can opt out of this message by adding the following to your config ' \
63
- '(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions ' \
63
+ '(see https://docs.rubocop.org/rubocop/plugins.html#plugin-suggestions ' \
64
64
  'for more options):'
65
65
  puts ' AllCops:'
66
66
  puts ' SuggestExtensions: false'
data/lib/rubocop/cli.rb CHANGED
@@ -13,7 +13,7 @@ module RuboCop
13
13
  DEFAULT_PARALLEL_OPTIONS = %i[
14
14
  color config debug display_style_guide display_time display_only_fail_level_offenses
15
15
  display_only_failed editor_mode except extra_details fail_level fix_layout format formatters
16
- ignore_disable_comments lint only only_guide_cops require safe
16
+ ignore_disable_comments lint only only_guide_cops out require safe
17
17
  autocorrect safe_autocorrect autocorrect_all
18
18
  ].freeze
19
19
 
@@ -189,6 +189,8 @@ module RuboCop
189
189
  ConfigLoader.enable_pending_cops = @options[:enable_pending_cops]
190
190
  ConfigLoader.ignore_parent_exclusion = @options[:ignore_parent_exclusion]
191
191
  ConfigLoader.ignore_unrecognized_cops = @options[:ignore_unrecognized_cops]
192
+ ConfigLoader.enabled_by_default = @options[:enable_all_cops]
193
+ ConfigLoader.disabled_by_default = @options[:disable_all_cops]
192
194
  end
193
195
 
194
196
  def set_options_to_pending_cops_reporter
@@ -200,18 +202,15 @@ module RuboCop
200
202
  RuboCop::LSP.enable if @options[:editor_mode]
201
203
  end
202
204
 
203
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
204
205
  def handle_exiting_options
205
206
  return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o }
206
207
 
207
208
  run_command(:version) if @options[:version] || @options[:verbose_version]
208
- run_command(:show_cops) if @options[:show_cops]
209
- run_command(:show_docs_url) if @options[:show_docs_url]
210
- run_command(:lsp) if @options[:lsp]
211
- run_command(:mcp) if @options[:mcp]
209
+ %i[show_cops list_enabled_cops_for show_docs_url lsp mcp].each do |name|
210
+ run_command(name) if @options[name]
211
+ end
212
212
  raise Finished
213
213
  end
214
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
215
214
 
216
215
  def apply_default_formatter
217
216
  # This must be done after the options have already been processed,
@@ -95,7 +95,8 @@ module RuboCop
95
95
  end
96
96
  end
97
97
 
98
- def analyze # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
98
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
99
+ def analyze
99
100
  return {} if @no_directives
100
101
 
101
102
  analyses = Hash.new { |hash, key| hash[key] = CopAnalysis.new([], nil) }
@@ -103,9 +104,9 @@ module RuboCop
103
104
 
104
105
  each_directive do |directive|
105
106
  if directive.push?
106
- resolved = resolve_push_cops(directive)
107
- @stack.push(snapshot_cops(analyses, resolved.values.flatten))
108
- apply_push(analyses, resolved, directive.line_number)
107
+ restore_point = analyses.transform_values(&:dup)
108
+ @stack.push(restore_point)
109
+ apply_push(analyses, resolve_push_cops(directive), directive.line_number)
109
110
  elsif directive.pop?
110
111
  pop_state(analyses, directive.line_number) if @stack.any?
111
112
  else
@@ -121,10 +122,7 @@ module RuboCop
121
122
  hash[cop_name] = cop_line_ranges(analysis)
122
123
  end
123
124
  end
124
-
125
- def snapshot_cops(analyses, cop_names)
126
- cop_names.to_h { |name| [name, analyses[name].dup] }
127
- end
125
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
128
126
 
129
127
  def resolve_push_cops(directive)
130
128
  directive.push_args.transform_values do |names|
@@ -155,13 +153,12 @@ module RuboCop
155
153
  end
156
154
 
157
155
  def pop_state(analyses, line)
158
- saved = @stack.pop
159
- saved.each do |cop, old|
160
- cur = analyses[cop]
161
- new_range = cur.start_line_number ? [cur.start_line_number..(line - 1)] : []
162
- ranges = cur.line_ranges + new_range
163
- new_start = old.start_line_number ? line : nil
164
- analyses[cop] = CopAnalysis.new(ranges, new_start)
156
+ restore_point = @stack.pop
157
+ (restore_point.keys | analyses.keys).each do |cop|
158
+ current = analyses[cop]
159
+ new_range = current.start_line_number ? [current.start_line_number..(line - 1)] : []
160
+ new_start = restore_point[cop]&.start_line_number ? line : nil
161
+ analyses[cop] = CopAnalysis.new(current.line_ranges + new_range, new_start)
165
162
  end
166
163
  end
167
164
 
@@ -24,7 +24,7 @@ module RuboCop
24
24
  include FileFinder
25
25
 
26
26
  attr_accessor :debug, :ignore_parent_exclusion, :disable_pending_cops, :enable_pending_cops,
27
- :ignore_unrecognized_cops
27
+ :enabled_by_default, :disabled_by_default, :ignore_unrecognized_cops
28
28
  attr_writer :default_configuration, :cache_root
29
29
  attr_reader :loaded_plugins, :loaded_features
30
30
 
@@ -41,6 +41,8 @@ module RuboCop
41
41
  @loaded_features = Set.new
42
42
  @disable_pending_cops = nil
43
43
  @enable_pending_cops = nil
44
+ @enabled_by_default = nil
45
+ @disabled_by_default = nil
44
46
  @ignore_parent_exclusion = nil
45
47
  @ignore_unrecognized_cops = nil
46
48
  @cache_root = nil
@@ -121,7 +123,7 @@ module RuboCop
121
123
  end
122
124
 
123
125
  def configuration_from_file(config_file, check: true)
124
- return default_configuration if config_file == DEFAULT_FILE
126
+ return apply_default_overrides(default_configuration) if config_file == DEFAULT_FILE
125
127
 
126
128
  config = load_file(config_file, check: check)
127
129
  config.validate_after_resolution if check
@@ -190,6 +192,19 @@ module RuboCop
190
192
  resolver.merge_with_default(config, config_file, unset_nil: unset_nil)
191
193
  end
192
194
 
195
+ # Applies CLI overrides for `AllCops/EnabledByDefault` and
196
+ # `AllCops/DisabledByDefault` to the given configuration. Used when the
197
+ # configuration would otherwise be returned without going through
198
+ # `merge_with_default` (e.g. there is no user-supplied `.rubocop.yml`).
199
+ def apply_default_overrides(config)
200
+ return config if @enabled_by_default.nil? && @disabled_by_default.nil?
201
+
202
+ hash = config.transform_values do |params|
203
+ params.is_a?(Hash) ? params.merge('Enabled' => !@disabled_by_default) : params
204
+ end
205
+ Config.new(hash, config.loaded_path)
206
+ end
207
+
193
208
  # @api private
194
209
  # Used to add plugins that were required inside a config or from
195
210
  # the CLI using `--plugin`.
@@ -92,11 +92,11 @@ module RuboCop
92
92
  # only cops from user configuration are enabled. If
93
93
  # AllCops:EnabledByDefault is true, it changes the Enabled params so that
94
94
  # only cops explicitly disabled in user configuration are disabled.
95
+ # When the `--disable-all-cops` or `--enable-all-cops` CLI option is given,
96
+ # it takes precedence over the configuration values.
95
97
  def merge_with_default(config, config_file, unset_nil:)
96
98
  default_configuration = ConfigLoader.default_configuration
97
-
98
- disabled_by_default = config.for_all_cops['DisabledByDefault']
99
- enabled_by_default = config.for_all_cops['EnabledByDefault']
99
+ disabled_by_default, enabled_by_default = resolve_default_overrides(config)
100
100
 
101
101
  if disabled_by_default || enabled_by_default
102
102
  default_configuration = transform(default_configuration) do |params|
@@ -169,6 +169,14 @@ module RuboCop
169
169
 
170
170
  private
171
171
 
172
+ def resolve_default_overrides(config)
173
+ if ConfigLoader.disabled_by_default || ConfigLoader.enabled_by_default
174
+ [ConfigLoader.disabled_by_default, ConfigLoader.enabled_by_default]
175
+ else
176
+ [config.for_all_cops['DisabledByDefault'], config.for_all_cops['EnabledByDefault']]
177
+ end
178
+ end
179
+
172
180
  def disabled?(hash, department)
173
181
  hash[department].is_a?(Hash) && hash[department]['Enabled'] == false
174
182
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  end
37
37
 
38
38
  def force_default_config!
39
- @options_config = ConfigLoader.default_configuration
39
+ @options_config = ConfigLoader.apply_default_overrides(ConfigLoader.default_configuration)
40
40
  end
41
41
 
42
42
  def unvalidated
@@ -50,7 +50,8 @@ module RuboCop
50
50
 
51
51
  def disable_offense(offense_range)
52
52
  unbreakable_range = multiline_ranges(offense_range)&.find do |range|
53
- eol_comment_would_be_inside_literal?(offense_range, range)
53
+ offense_range.overlaps?(range) &&
54
+ eol_comment_would_be_inside_literal?(offense_range, range)
54
55
  end
55
56
 
56
57
  if unbreakable_range
@@ -41,6 +41,7 @@ module RuboCop
41
41
  include AutocorrectLogic
42
42
 
43
43
  attr_reader :config, :processed_source
44
+ attr_accessor :project_index
44
45
 
45
46
  # Reports of an investigation.
46
47
  # Immutable
@@ -306,7 +307,9 @@ module RuboCop
306
307
  def ready
307
308
  return self if self.class.support_multiple_source?
308
309
 
309
- self.class.new(@config, @options)
310
+ self.class.new(@config, @options).tap do |fresh|
311
+ fresh.project_index = @project_index
312
+ end
310
313
  end
311
314
 
312
315
  ### Reserved for Cop::Cop
@@ -416,7 +419,10 @@ module RuboCop
416
419
  ### Actually private methods
417
420
 
418
421
  def reset_investigation
419
- @currently_disabled_lines = @current_offenses = @processed_source = @current_corrector = nil
422
+ @currently_disabled_lines = nil
423
+ @current_offenses = nil
424
+ @processed_source = nil
425
+ @current_corrector = nil
420
426
  end
421
427
 
422
428
  # @return [Symbol, Corrector] offense status
@@ -59,11 +59,7 @@ module RuboCop
59
59
  end
60
60
 
61
61
  def content_if_comment_present(corrector, node)
62
- range = range_with_surrounding_space(
63
- children(node).last.source_range,
64
- side: :right
65
- ).end.resize(1)
66
- if range.source == '#'
62
+ if processed_source.comment_at_line(children(node).last.last_line)
67
63
  select_content_to_be_inserted_after_last_element(corrector, node)
68
64
  else
69
65
  node.loc.end.source
@@ -13,8 +13,7 @@ module RuboCop
13
13
  buffer = node.source_range.source_buffer
14
14
  corrector.remove(range_with_surrounding_space(range: node.loc.begin, buffer: buffer,
15
15
  side: :right, whitespace: true))
16
- corrector.remove(range_with_surrounding_space(range: node.loc.end, buffer: buffer,
17
- side: :left))
16
+ remove_close_paren(corrector, node, buffer)
18
17
  handle_orphaned_comma(corrector, node)
19
18
 
20
19
  return unless ternary_condition?(node) && next_char_is_question_mark?(node)
@@ -24,6 +23,38 @@ module RuboCop
24
23
 
25
24
  private
26
25
 
26
+ # When the line above `)` ends with a comment and a chained call follows `)`,
27
+ # crossing the newline would pull the chain into the comment. Preserve the newline.
28
+ def remove_close_paren(corrector, node, buffer)
29
+ newlines = !comment_above_close_paren_swallows_chain?(node, buffer)
30
+ corrector.remove(range_with_surrounding_space(range: node.loc.end, buffer: buffer,
31
+ side: :left, newlines: newlines))
32
+ end
33
+
34
+ def comment_above_close_paren_swallows_chain?(node, buffer)
35
+ last_child = node.children.last
36
+ return false unless last_child
37
+
38
+ body_end = last_child.source_range.end_pos
39
+ close_paren_begin = node.loc.end.begin_pos
40
+ return false if body_end >= close_paren_begin
41
+
42
+ source_between = buffer.source[body_end...close_paren_begin]
43
+ return false unless source_between.match?(/#[^\n]*\n/)
44
+
45
+ chained_after_close_paren?(node)
46
+ end
47
+
48
+ def chained_after_close_paren?(node)
49
+ close_paren = node.loc.end
50
+ line_text = close_paren.source_line
51
+ after_paren = line_text[(close_paren.column + 1)..]
52
+ return false if after_paren.nil?
53
+
54
+ trimmed = after_paren.lstrip
55
+ !trimmed.empty? && !trimmed.start_with?('#')
56
+ end
57
+
27
58
  def ternary_condition?(node)
28
59
  node.parent&.if_type? && node.parent.ternary?
29
60
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop # rubocop:disable Style/Documentation
5
+ # Autoloads corrector classes used by cops. Classes are autoloaded to reduce the number of
6
+ # required classes because they're referenced only when autocorrection is performed.
7
+
8
+ # rubocop:disable Layout/LineLength
9
+ autoload :AlignmentCorrector, 'rubocop/cop/correctors/alignment_corrector'
10
+ autoload :ConditionCorrector, 'rubocop/cop/correctors/condition_corrector'
11
+ autoload :EachToForCorrector, 'rubocop/cop/correctors/each_to_for_corrector'
12
+ autoload :EmptyLineCorrector, 'rubocop/cop/correctors/empty_line_corrector'
13
+ autoload :ForToEachCorrector, 'rubocop/cop/correctors/for_to_each_corrector'
14
+ autoload :IfThenCorrector, 'rubocop/cop/correctors/if_then_corrector'
15
+ autoload :LambdaLiteralToMethodCorrector, 'rubocop/cop/correctors/lambda_literal_to_method_corrector'
16
+ autoload :LineBreakCorrector, 'rubocop/cop/correctors/line_break_corrector'
17
+ autoload :MultilineLiteralBraceCorrector, 'rubocop/cop/correctors/multiline_literal_brace_corrector'
18
+ autoload :OrderedGemCorrector, 'rubocop/cop/correctors/ordered_gem_corrector'
19
+ autoload :ParenthesesCorrector, 'rubocop/cop/correctors/parentheses_corrector'
20
+ autoload :PercentLiteralCorrector, 'rubocop/cop/correctors/percent_literal_corrector'
21
+ autoload :PunctuationCorrector, 'rubocop/cop/correctors/punctuation_corrector'
22
+ autoload :RequireLibraryCorrector, 'rubocop/cop/correctors/require_library_corrector'
23
+ autoload :SpaceCorrector, 'rubocop/cop/correctors/space_corrector'
24
+ autoload :StringLiteralCorrector, 'rubocop/cop/correctors/string_literal_corrector'
25
+ autoload :UnusedArgCorrector, 'rubocop/cop/correctors/unused_arg_corrector'
26
+ # rubocop:enable Layout/LineLength
27
+ end
28
+ end
@@ -4,16 +4,42 @@ module RuboCop
4
4
  # Allows specified configuration options to have an exclude limit
5
5
  # ie. a maximum value tracked that it can be used by `--auto-gen-config`.
6
6
  module ExcludeLimit
7
+ class << self
8
+ attr_accessor :tmp_dir
9
+
10
+ # Reads the aggregated exclude limit values for a cop from tmp files.
11
+ # Returns a hash like { 'Max' => 81 } or an empty hash if no values were written.
12
+ def read_limits(cop_name)
13
+ cop_dir = cop_dir_for(cop_name)
14
+ return {} unless cop_dir&.directory?
15
+
16
+ limits = {}
17
+ cop_dir.children.each do |filepath|
18
+ next unless filepath.file?
19
+
20
+ values = filepath.readlines.map(&:to_i)
21
+ limits[filepath.basename.to_s] = values.max unless values.empty?
22
+ end
23
+ limits
24
+ end
25
+
26
+ # Returns the tmp directory path for a given cop, or nil if tmp_dir is not set.
27
+ def cop_dir_for(cop_name)
28
+ tmp_dir&.join(cop_name.tr('/', '-'))
29
+ end
30
+ end
31
+
7
32
  # Sets up a configuration option to have an exclude limit tracked.
8
33
  # The parameter name given is transformed into a method name (eg. `Max`
9
34
  # becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
10
35
  def exclude_limit(parameter_name, method_name: transform(parameter_name))
11
36
  define_method(:"#{method_name}=") do |value|
12
- cfg = config_to_allow_offenses
13
- cfg[:exclude_limit] ||= {}
14
- current_max = cfg[:exclude_limit][parameter_name]
15
- value = [current_max, value].max if current_max
16
- cfg[:exclude_limit][parameter_name] = value
37
+ cop_dir = RuboCop::ExcludeLimit.cop_dir_for(self.class.badge.to_s)
38
+ return unless cop_dir
39
+
40
+ cop_dir.mkpath
41
+ filepath = cop_dir.join(parameter_name)
42
+ filepath.write("#{value}\n", mode: 'a')
17
43
  end
18
44
  end
19
45
 
@@ -3,12 +3,12 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Gemspec
6
- # An attribute assignment method calls should be listed only once
6
+ # An attribute assignment method call should be listed only once
7
7
  # in a gemspec.
8
8
  #
9
9
  # Assigning to an attribute with the same name using `spec.foo =` or
10
10
  # `spec.attribute#[]=` will be an unintended usage. On the other hand,
11
- # duplication of methods such # as `spec.requirements`,
11
+ # duplication of methods such as `spec.requirements`,
12
12
  # `spec.add_runtime_dependency`, and others are permitted because it is
13
13
  # the intended use of appending values.
14
14
  #
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Requires a gemspec to have `rubygems_mfa_required` metadata set.
7
7
  #
8
8
  # This setting tells RubyGems that MFA (Multi-Factor Authentication) is
9
- # required for accounts to be able perform privileged operations, such as
9
+ # required for accounts to be able to perform privileged operations, such as
10
10
  # (see RubyGems' documentation for the full list of privileged
11
11
  # operations):
12
12
  #
@@ -70,7 +70,7 @@ module RuboCop
70
70
  def_node_matcher :metadata, <<~PATTERN
71
71
  `{
72
72
  (send _ :metadata= $_)
73
- (send (send _ :metadata) :[]= (str "rubygems_mfa_required") $_)
73
+ (send (send _ :metadata) :[]= {(str "rubygems_mfa_required") (sym :rubygems_mfa_required)} $_)
74
74
  }
75
75
  PATTERN
76
76
 
@@ -78,13 +78,13 @@ module RuboCop
78
78
  def_node_search :metadata_assignment, <<~PATTERN
79
79
  `{
80
80
  (send _ :metadata= _)
81
- (send (send _ :metadata) :[]= (str _) _)
81
+ (send (send _ :metadata) :[]= {str sym} _)
82
82
  }
83
83
  PATTERN
84
84
 
85
85
  # @!method rubygems_mfa_required(node)
86
86
  def_node_search :rubygems_mfa_required, <<~PATTERN
87
- (pair (str "rubygems_mfa_required") $_)
87
+ (pair {(str "rubygems_mfa_required") (sym :rubygems_mfa_required)} $_)
88
88
  PATTERN
89
89
 
90
90
  # @!method true_string?(node)
@@ -4,10 +4,10 @@ module RuboCop
4
4
  module Cop
5
5
  module Gemspec
6
6
  # Checks that `RUBY_VERSION` and `Ruby::VERSION` constants are not used in gemspec.
7
- # Using `RUBY_VERSION` and `Ruby::VERSION` are dangerous because value of the
7
+ # Using `RUBY_VERSION` and `Ruby::VERSION` is dangerous because the value of the
8
8
  # constant is determined by `rake release`.
9
- # It's possible to have dependency based on ruby version used
10
- # to execute `rake release` and not user's ruby version.
9
+ # It's possible to have a dependency based on the Ruby version used
10
+ # to execute `rake release` and not the user's Ruby version.
11
11
  #
12
12
  # @example
13
13
  #
@@ -25,6 +25,7 @@ module RuboCop
25
25
  def_node_matcher :line_send, <<~PATTERN
26
26
  {
27
27
  (send (send _ {:loc :source_range}) {:line :first_line})
28
+ (send (send (send _ :loc) {:begin :end}) :line)
28
29
  (send _ :first_line)
29
30
  }
30
31
  PATTERN
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # `Layout/EndAlignment` cop aligns with keywords (e.g. `if`, `while`, `case`)
15
15
  # by default. On the other hand, `||= begin` that this cop targets tends to
16
16
  # align with the start of the line, it defaults to `EnforcedStyleAlignWith: start_of_line`.
17
- # These style can be configured by each cop.
17
+ # These styles can be configured by each cop.
18
18
  #
19
19
  # @example EnforcedStyleAlignWith: start_of_line (default)
20
20
  # # bad
@@ -49,7 +49,7 @@ module RuboCop
49
49
  # - private_methods
50
50
  # ----
51
51
  #
52
- # Instead of putting all literals in the expected order, is also
52
+ # Instead of putting all literals in the expected order, it is also
53
53
  # possible to group categories of macros. Visibility levels are handled
54
54
  # automatically.
55
55
  #