rubocop 1.71.2 → 1.72.2

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 (61) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +30 -0
  4. data/lib/rubocop/cli/command/suggest_extensions.rb +7 -1
  5. data/lib/rubocop/comment_config.rb +1 -1
  6. data/lib/rubocop/config.rb +4 -0
  7. data/lib/rubocop/config_loader.rb +44 -8
  8. data/lib/rubocop/config_loader_resolver.rb +21 -7
  9. data/lib/rubocop/cop/internal_affairs/example_description.rb +4 -2
  10. data/lib/rubocop/cop/internal_affairs/location_exists.rb +116 -0
  11. data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_walker.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/plugin.rb +33 -0
  13. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +7 -1
  14. data/lib/rubocop/cop/internal_affairs.rb +1 -16
  15. data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
  16. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  17. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  18. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +22 -2
  19. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  20. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  21. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
  22. data/lib/rubocop/cop/lint/cop_directive_syntax.rb +84 -0
  23. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
  24. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +231 -0
  25. data/lib/rubocop/cop/lint/suppressed_exception_in_number_conversion.rb +111 -0
  26. data/lib/rubocop/cop/lint/useless_constant_scoping.rb +80 -0
  27. data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +7 -7
  28. data/lib/rubocop/cop/mixin/alignment.rb +2 -2
  29. data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
  30. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  31. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +18 -18
  32. data/lib/rubocop/cop/mixin/hash_transform_method.rb +74 -74
  33. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  34. data/lib/rubocop/cop/mixin/range_help.rb +3 -3
  35. data/lib/rubocop/cop/mixin/string_help.rb +1 -1
  36. data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
  37. data/lib/rubocop/cop/naming/predicate_name.rb +44 -0
  38. data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -3
  39. data/lib/rubocop/cop/style/redundant_format.rb +238 -0
  40. data/lib/rubocop/cop/style/redundant_parentheses.rb +18 -4
  41. data/lib/rubocop/cop/util.rb +1 -1
  42. data/lib/rubocop/cop/utils/format_string.rb +7 -5
  43. data/lib/rubocop/directive_comment.rb +35 -2
  44. data/lib/rubocop/lsp/runtime.rb +2 -0
  45. data/lib/rubocop/lsp/server.rb +0 -2
  46. data/lib/rubocop/options.rb +26 -11
  47. data/lib/rubocop/path_util.rb +4 -0
  48. data/lib/rubocop/plugin/configuration_integrator.rb +143 -0
  49. data/lib/rubocop/plugin/load_error.rb +26 -0
  50. data/lib/rubocop/plugin/loader.rb +100 -0
  51. data/lib/rubocop/plugin/not_supported_error.rb +29 -0
  52. data/lib/rubocop/plugin.rb +39 -0
  53. data/lib/rubocop/rake_task.rb +4 -1
  54. data/lib/rubocop/rspec/cop_helper.rb +9 -0
  55. data/lib/rubocop/server/cache.rb +35 -2
  56. data/lib/rubocop/server/cli.rb +2 -2
  57. data/lib/rubocop/version.rb +17 -2
  58. data/lib/rubocop.rb +5 -0
  59. data/lib/ruby_lsp/rubocop/addon.rb +7 -10
  60. data/lib/ruby_lsp/rubocop/{wraps_built_in_lsp_runtime.rb → runtime_adapter.rb} +5 -8
  61. metadata +35 -9
@@ -18,10 +18,24 @@ module RuboCop
18
18
  # @api private
19
19
  COPS_PATTERN = "(all|#{COP_NAMES_PATTERN})"
20
20
  # @api private
21
+ AVAILABLE_MODES = %w[disable enable todo].freeze
22
+ # @api private
23
+ DIRECTIVE_MARKER_PATTERN = '# rubocop : '
24
+ # @api private
25
+ DIRECTIVE_MARKER_REGEXP = Regexp.new(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*'))
26
+ # @api private
27
+ DIRECTIVE_HEADER_PATTERN = "#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
28
+ # @api private
21
29
  DIRECTIVE_COMMENT_REGEXP = Regexp.new(
22
- "# rubocop : ((?:disable|enable|todo))\\b #{COPS_PATTERN}"
30
+ "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}"
23
31
  .gsub(' ', '\s*')
24
32
  )
33
+ # @api private
34
+ TRAILING_COMMENT_MARKER = '--'
35
+ # @api private
36
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP = Regexp.new(
37
+ "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*')
38
+ )
25
39
 
26
40
  def self.before_comment(line)
27
41
  line.split(DIRECTIVE_COMMENT_REGEXP).first
@@ -32,9 +46,28 @@ module RuboCop
32
46
  def initialize(comment, cop_registry = Cop::Registry.global)
33
47
  @comment = comment
34
48
  @cop_registry = cop_registry
49
+ @match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
35
50
  @mode, @cops = match_captures
36
51
  end
37
52
 
53
+ # Checks if the comment starts with `# rubocop:` marker
54
+ def start_with_marker?
55
+ comment.text.start_with?(DIRECTIVE_MARKER_REGEXP)
56
+ end
57
+
58
+ # Checks if the comment is malformed as a `# rubocop:` directive
59
+ def malformed?
60
+ return true if !start_with_marker? || @match_data.nil?
61
+
62
+ tail = @match_data.post_match.lstrip
63
+ !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER))
64
+ end
65
+
66
+ # Checks if the directive comment is missing a cop name
67
+ def missing_cop_name?
68
+ MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text)
69
+ end
70
+
38
71
  # Checks if this directive relates to single line
39
72
  def single_line?
40
73
  !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
@@ -55,7 +88,7 @@ module RuboCop
55
88
 
56
89
  # Returns match captures to directive comment pattern
57
90
  def match_captures
58
- @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
91
+ @match_captures ||= @match_data&.captures
59
92
  end
60
93
 
61
94
  # Checks if this directive disables cops
@@ -20,6 +20,8 @@ module RuboCop
20
20
  attr_writer :safe_autocorrect, :lint_mode, :layout_mode
21
21
 
22
22
  def initialize(config_store)
23
+ RuboCop::LSP.enable
24
+
23
25
  @runner = RuboCop::Lsp::StdinRunner.new(config_store)
24
26
  @cop_registry = RuboCop::Cop::Registry.global.to_h
25
27
 
@@ -22,8 +22,6 @@ module RuboCop
22
22
  def initialize(config_store)
23
23
  $PROGRAM_NAME = "rubocop --lsp #{ConfigFinder.project_root}"
24
24
 
25
- RuboCop::LSP.enable
26
-
27
25
  @reader = LanguageServer::Protocol::Transport::Io::Reader.new($stdin)
28
26
  @writer = LanguageServer::Protocol::Transport::Io::Writer.new($stdout)
29
27
  @runtime = RuboCop::LSP::Runtime.new(config_store)
@@ -243,6 +243,7 @@ module RuboCop
243
243
  option(opts, '--init')
244
244
  option(opts, '-c', '--config FILE')
245
245
  option(opts, '-d', '--debug')
246
+ option(opts, '--plugin FILE') { |f| plugin_feature(f) }
246
247
  option(opts, '-r', '--require FILE') { |f| require_feature(f) }
247
248
  option(opts, '--[no-]color')
248
249
  option(opts, '-v', '--version')
@@ -299,11 +300,25 @@ module RuboCop
299
300
  long_opt[2..].sub('[no-]', '').sub(/ .*/, '').tr('-', '_').gsub(/[\[\]]/, '').to_sym
300
301
  end
301
302
 
302
- def require_feature(file)
303
- # If any features were added on the CLI from `--require`,
303
+ def plugin_feature(file)
304
+ # If any features were added on the CLI from `--plugin`,
304
305
  # add them to the config.
305
- ConfigLoader.add_loaded_features(file)
306
- require file
306
+ ConfigLoaderResolver.new.resolve_plugins(Config.new, file)
307
+ end
308
+
309
+ def require_feature(file)
310
+ if Plugin.plugin_capable?(file)
311
+ # NOTE: Compatibility for before plugins style.
312
+ warn Rainbow(<<~MESSAGE).yellow
313
+ #{file} gem supports plugin, use `--plugin` instead of `--require`.
314
+ MESSAGE
315
+ plugin_feature(file)
316
+ else
317
+ # If any features were added on the CLI from `--require`,
318
+ # add them to the config.
319
+ require file
320
+ ConfigLoader.add_loaded_features(file)
321
+ end
307
322
  end
308
323
  end
309
324
 
@@ -397,7 +412,7 @@ module RuboCop
397
412
  return if @options[:format] == 'junit'
398
413
 
399
414
  raise OptionArgumentError,
400
- format('--display-only-failed can only be used together with --format junit.')
415
+ '--display-only-failed can only be used together with --format junit.'
401
416
  end
402
417
 
403
418
  def validate_display_only_correctable_and_autocorrect
@@ -415,14 +430,13 @@ module RuboCop
415
430
  !@options.key?(:display_only_safe_correctable)
416
431
 
417
432
  raise OptionArgumentError,
418
- format('--display-only-failed cannot be used together with other display options.')
433
+ '--display-only-failed cannot be used together with other display options.'
419
434
  end
420
435
 
421
436
  def validate_lsp_and_editor_mode
422
437
  return if !@options.key?(:lsp) || !@options.key?(:editor_mode)
423
438
 
424
- raise OptionArgumentError,
425
- format('Do not specify `--editor-mode` as it is redundant in `--lsp`.')
439
+ raise OptionArgumentError, 'Do not specify `--editor-mode` as it is redundant in `--lsp`.'
426
440
  end
427
441
 
428
442
  def validate_autocorrect
@@ -436,7 +450,7 @@ module RuboCop
436
450
  return unless @options.key?(:disable_uncorrectable)
437
451
 
438
452
  raise OptionArgumentError,
439
- format('--disable-uncorrectable can only be used together with --autocorrect.')
453
+ '--disable-uncorrectable can only be used together with --autocorrect.'
440
454
  end
441
455
 
442
456
  def disable_parallel_when_invalid_option_combo
@@ -504,6 +518,7 @@ module RuboCop
504
518
  only_guide_cops: ['Run only cops for rules that link to a',
505
519
  'style guide.'],
506
520
  except: 'Exclude the given cop(s).',
521
+ plugin: 'Load a RuboCop plugin.',
507
522
  require: 'Require Ruby file.',
508
523
  config: 'Specify configuration file.',
509
524
  auto_gen_config: ['Generate a configuration file acting as a',
@@ -542,8 +557,8 @@ module RuboCop
542
557
  only_recognized_file_types: ['Inspect files given on the command line only if',
543
558
  'they are listed in `AllCops/Include` parameters',
544
559
  'of user configuration or default configuration.'],
545
- ignore_disable_comments: ['Run cops even when they are disabled locally',
546
- 'by a `rubocop:disable` directive.'],
560
+ ignore_disable_comments: ['Report offenses even if they have been manually disabled',
561
+ 'with a `rubocop:disable` or `rubocop:todo` directive.'],
547
562
  ignore_parent_exclusion: ['Prevent from inheriting `AllCops/Exclude` from',
548
563
  'parent folders.'],
549
564
  ignore_unrecognized_cops: ['Ignore unrecognized cops or departments in the config.'],
@@ -28,6 +28,10 @@ module RuboCop
28
28
  end
29
29
  end
30
30
 
31
+ def remote_file?(uri)
32
+ uri.start_with?('http://', 'https://')
33
+ end
34
+
31
35
  SMART_PATH_CACHE = {} # rubocop:disable Style/MutableConstant
32
36
  private_constant :SMART_PATH_CACHE
33
37
 
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lint_roller/context'
4
+ require_relative 'not_supported_error'
5
+
6
+ module RuboCop
7
+ module Plugin
8
+ # A class for integrating plugin configurations into RuboCop.
9
+ # Handles configuration merging, validation, and compatibility for plugins.
10
+ # @api private
11
+ class ConfigurationIntegrator
12
+ class << self
13
+ def integrate_plugins_into_rubocop_config(rubocop_config, plugins)
14
+ default_config = ConfigLoader.default_configuration
15
+ runner_context = create_context(rubocop_config)
16
+
17
+ validate_plugins!(plugins, runner_context)
18
+
19
+ plugin_config = combine_rubocop_configs(default_config, runner_context, plugins).to_h
20
+
21
+ merge_plugin_config_into_all_cops!(default_config, plugin_config)
22
+ merge_plugin_config_into_default_config!(default_config, plugin_config)
23
+ end
24
+
25
+ private
26
+
27
+ def create_context(rubocop_config)
28
+ LintRoller::Context.new(
29
+ runner: :rubocop,
30
+ runner_version: Version.version,
31
+ engine: :rubocop,
32
+ engine_version: Version.version,
33
+ target_ruby_version: rubocop_config.target_ruby_version
34
+ )
35
+ end
36
+
37
+ def validate_plugins!(plugins, runner_context)
38
+ unsupported_plugins = plugins.reject { |plugin| plugin.supported?(runner_context) }
39
+ return if unsupported_plugins.none?
40
+
41
+ raise Plugin::NotSupportedError, unsupported_plugins
42
+ end
43
+
44
+ def combine_rubocop_configs(default_config, runner_context, plugins)
45
+ fake_out_rubocop_default_configuration(default_config) do |fake_config|
46
+ all_cop_keys_configured_by_plugins = []
47
+
48
+ plugins.reduce(fake_config) do |combined_config, plugin|
49
+ RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, combined_config)
50
+
51
+ print 'Plugin ' if ConfigLoader.debug
52
+
53
+ plugin_config, plugin_config_path = load_plugin_rubocop_config(plugin, runner_context)
54
+
55
+ plugin_config['AllCops'], all_cop_keys_configured_by_plugins = merge_all_cop_settings(
56
+ combined_config['AllCops'], plugin_config['AllCops'],
57
+ all_cop_keys_configured_by_plugins
58
+ )
59
+
60
+ plugin_config.make_excludes_absolute
61
+
62
+ ConfigLoader.merge_with_default(plugin_config, plugin_config_path)
63
+ end
64
+ end
65
+ end
66
+
67
+ def merge_plugin_config_into_all_cops!(rubocop_config, plugin_config)
68
+ rubocop_config['AllCops'].merge!(plugin_config['AllCops'])
69
+ end
70
+
71
+ def merge_plugin_config_into_default_config!(default_config, plugin_config)
72
+ plugin_config.each do |key, value|
73
+ default_config[key] = if default_config[key].is_a?(Hash)
74
+ resolver.merge(default_config[key], value)
75
+ else
76
+ value
77
+ end
78
+ end
79
+ end
80
+
81
+ def fake_out_rubocop_default_configuration(default_config)
82
+ orig_default_config = ConfigLoader.instance_variable_get(:@default_configuration)
83
+
84
+ result = yield default_config
85
+
86
+ ConfigLoader.instance_variable_set(:@default_configuration, orig_default_config)
87
+
88
+ result
89
+ end
90
+
91
+ # rubocop:disable Metrics/AbcSize
92
+ def load_plugin_rubocop_config(plugin, runner_context)
93
+ rules = plugin.rules(runner_context)
94
+
95
+ case rules.type
96
+ when :path
97
+ [ConfigLoader.load_file(rules.value, check: false), rules.value]
98
+ when :object
99
+ path = plugin.method(:rules).source_location[0]
100
+ [Config.create(rules.value, path, check: true), path]
101
+ when :error
102
+ plugin_name = plugin.about&.name || plugin.inspect
103
+ error_message = rules.value.respond_to?(:message) ? rules.value.message : rules.value
104
+
105
+ raise "Plugin `#{plugin_name}' failed to load with error: #{error_message}"
106
+ end
107
+ end
108
+ # rubocop:enable Metrics/AbcSize
109
+
110
+ # This is how we ensure "first-in wins": plugins can override AllCops settings that are
111
+ # set by RuboCop's default configuration, but once a plugin sets an AllCop setting, they
112
+ # have exclusive first-in-wins rights to that setting.
113
+ #
114
+ # The one exception to this are array fields, because we don't want to
115
+ # overwrite the AllCops defaults but rather munge the arrays (`existing |
116
+ # new`) to allow plugins to add to the array, for example Include and
117
+ # Exclude paths and patterns.
118
+ def merge_all_cop_settings(existing_all_cops, new_all_cops, already_configured_keys)
119
+ return [existing_all_cops, already_configured_keys] unless new_all_cops.is_a?(Hash)
120
+
121
+ combined_all_cops = existing_all_cops.dup
122
+ combined_configured_keys = already_configured_keys.dup
123
+
124
+ new_all_cops.each do |key, value|
125
+ if combined_all_cops[key].is_a?(Array) && value.is_a?(Array)
126
+ combined_all_cops[key] |= value
127
+ combined_configured_keys |= [key]
128
+ elsif !combined_configured_keys.include?(key)
129
+ combined_all_cops[key] = value
130
+ combined_configured_keys << key
131
+ end
132
+ end
133
+
134
+ [combined_all_cops, combined_configured_keys]
135
+ end
136
+
137
+ def resolver
138
+ @resolver ||= ConfigLoaderResolver.new
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Plugin
5
+ # An exception raised when a plugin fails to load.
6
+ # @api private
7
+ class LoadError < Error
8
+ def initialize(plugin_name)
9
+ super
10
+
11
+ @plugin_name = plugin_name
12
+ end
13
+
14
+ def message
15
+ <<~MESSAGE
16
+ Failed to load plugin `#{@plugin_name}` because the corresponding plugin class could not be determined for instantiation.
17
+ Try upgrading it first (e.g., `bundle update #{@plugin_name}`).
18
+ If `#{@plugin_name}` is not yet a plugin, use `require: #{@plugin_name}` instead of `plugins: `#{@plugin_name}` in your configuration.
19
+
20
+ For further assistance, check with the developer regarding the following points:
21
+ https://docs.rubocop.org/rubocop/plugin_migration_guide.html
22
+ MESSAGE
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../feature_loader'
4
+ require_relative 'load_error'
5
+
6
+ module RuboCop
7
+ module Plugin
8
+ # A class for loading and resolving plugins.
9
+ # @api private
10
+ class Loader
11
+ # rubocop:disable Layout/LineLength
12
+ DEFAULT_PLUGIN_CONFIG = {
13
+ 'enabled' => true,
14
+ 'require_path' => nil, # If not set, will be set to the plugin name
15
+ 'plugin_class_name' => nil # If not set, looks for gemspec `spec.metadata["default_lint_roller_plugin"]`
16
+ }.freeze
17
+
18
+ # rubocop:enable Layout/LineLength
19
+ class << self
20
+ def load(plugins)
21
+ normalized_plugin_configs = normalize(plugins)
22
+ normalized_plugin_configs.filter_map do |plugin_name, plugin_config|
23
+ next unless plugin_config['enabled']
24
+
25
+ plugin_class = constantize_plugin_from(plugin_name, plugin_config)
26
+
27
+ plugin_class.new(plugin_config)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # rubocop:disable Metrics/MethodLength
34
+ def normalize(plugin_configs)
35
+ plugin_configs.to_h do |plugin_config|
36
+ if plugin_config == Plugin::OBSOLETE_INTERNAL_AFFAIRS_PLUGIN_NAME
37
+ warn Rainbow(<<~MESSAGE).yellow
38
+ Specify `rubocop-internal_affairs` instead of `rubocop/cop/internal_affairs` in your configuration.
39
+ MESSAGE
40
+ plugin_config = Plugin::INTERNAL_AFFAIRS_PLUGIN_NAME
41
+ end
42
+
43
+ if plugin_config.is_a?(Hash)
44
+ plugin_name = plugin_config.keys.first
45
+
46
+ [
47
+ plugin_name, DEFAULT_PLUGIN_CONFIG.merge(
48
+ { 'require_path' => plugin_name }, plugin_config.values.first
49
+ )
50
+ ]
51
+ # NOTE: Compatibility is maintained when `require: rubocop/cop/internal_affairs` remains
52
+ # specified in `.rubocop.yml`.
53
+ elsif (builtin_plugin_config = Plugin::BUILTIN_INTERNAL_PLUGINS[plugin_config])
54
+ [plugin_config, builtin_plugin_config]
55
+ else
56
+ [plugin_config, DEFAULT_PLUGIN_CONFIG.merge('require_path' => plugin_config)]
57
+ end
58
+ end
59
+ end
60
+
61
+ def constantize_plugin_from(plugin_name, plugin_config)
62
+ if plugin_name.is_a?(String) || plugin_name.is_a?(Symbol)
63
+ constantize(plugin_name, plugin_config)
64
+ else
65
+ plugin_name
66
+ end
67
+ end
68
+
69
+ # rubocop:enable Metrics/MethodLength
70
+ def constantize(plugin_name, plugin_config)
71
+ require_plugin(plugin_config['require_path'])
72
+
73
+ if (constant_name = plugin_config['plugin_class_name'])
74
+ begin
75
+ Kernel.const_get(constant_name)
76
+ rescue StandardError
77
+ raise <<~MESSAGE
78
+ Failed while configuring plugin `#{plugin_name}': no constant with name `#{constant_name}' was found.
79
+ MESSAGE
80
+ end
81
+ else
82
+ constantize_plugin_from_gemspec_metadata(plugin_name)
83
+ end
84
+ end
85
+
86
+ def require_plugin(require_path)
87
+ FeatureLoader.load(config_directory_path: Dir.pwd, feature: require_path)
88
+ end
89
+
90
+ def constantize_plugin_from_gemspec_metadata(plugin_name)
91
+ plugin_class_name = Gem.loaded_specs[plugin_name].metadata['default_lint_roller_plugin']
92
+
93
+ Kernel.const_get(plugin_class_name)
94
+ rescue LoadError, StandardError
95
+ raise Plugin::LoadError, plugin_name
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Plugin
5
+ # An exception raised when a plugin is not supported by the RuboCop engine.
6
+ # @api private
7
+ class NotSupportedError < Error
8
+ def initialize(unsupported_plugins)
9
+ super
10
+
11
+ @unsupported_plugins = unsupported_plugins
12
+ end
13
+
14
+ def message
15
+ if @unsupported_plugins.one?
16
+ about = @unsupported_plugins.first.about
17
+
18
+ "#{about.name} #{about.version} is not a plugin supported by RuboCop engine."
19
+ else
20
+ unsupported_plugin_names = @unsupported_plugins.map do |plugin|
21
+ "#{plugin.about.name} #{plugin.about.version}"
22
+ end.join(', ')
23
+
24
+ "#{unsupported_plugin_names} are not plugins supported by RuboCop engine."
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'plugin/configuration_integrator'
4
+ require_relative 'plugin/loader'
5
+
6
+ module RuboCop
7
+ # Provides a plugin for RuboCop extensions that conform to lint_roller.
8
+ # https://github.com/standardrb/lint_roller
9
+ # @api private
10
+ module Plugin
11
+ BUILTIN_INTERNAL_PLUGINS = {
12
+ 'rubocop-internal_affairs' => {
13
+ 'enabled' => true,
14
+ 'require_path' => 'rubocop/cop/internal_affairs/plugin',
15
+ 'plugin_class_name' => 'RuboCop::InternalAffairs::Plugin'
16
+ }
17
+ }.freeze
18
+ INTERNAL_AFFAIRS_PLUGIN_NAME = Plugin::BUILTIN_INTERNAL_PLUGINS.keys.first
19
+ OBSOLETE_INTERNAL_AFFAIRS_PLUGIN_NAME = 'rubocop/cop/internal_affairs'
20
+
21
+ class << self
22
+ def plugin_capable?(feature_name)
23
+ return true if BUILTIN_INTERNAL_PLUGINS.key?(feature_name)
24
+ return true if feature_name == OBSOLETE_INTERNAL_AFFAIRS_PLUGIN_NAME
25
+ return false unless (gem = Gem.loaded_specs[feature_name])
26
+
27
+ !!gem.metadata['default_lint_roller_plugin']
28
+ end
29
+
30
+ def integrate_plugins(rubocop_config, plugins)
31
+ plugins = Plugin::Loader.load(plugins)
32
+
33
+ ConfigurationIntegrator.integrate_plugins_into_rubocop_config(rubocop_config, plugins)
34
+
35
+ plugins
36
+ end
37
+ end
38
+ end
39
+ end
@@ -12,7 +12,8 @@ module RuboCop
12
12
  # Use global Rake namespace here to avoid namespace issues with custom
13
13
  # rubocop-rake tasks
14
14
  class RakeTask < ::Rake::TaskLib
15
- attr_accessor :name, :verbose, :fail_on_error, :patterns, :formatters, :requires, :options
15
+ attr_accessor :name, :verbose, :fail_on_error, :patterns, :formatters, :plugins, :requires,
16
+ :options
16
17
 
17
18
  def initialize(name = :rubocop, *args, &task_block)
18
19
  super()
@@ -54,6 +55,7 @@ module RuboCop
54
55
 
55
56
  def full_options
56
57
  formatters.map { |f| ['--format', f] }.flatten
58
+ .concat(plugins.map { |plugin| ['--plugin', plugin] }.flatten)
57
59
  .concat(requires.map { |r| ['--require', r] }.flatten)
58
60
  .concat(options.flatten)
59
61
  .concat(patterns)
@@ -64,6 +66,7 @@ module RuboCop
64
66
  @verbose = true
65
67
  @fail_on_error = true
66
68
  @patterns = []
69
+ @plugins = []
67
70
  @requires = []
68
71
  @options = []
69
72
  @formatters = []
@@ -13,6 +13,15 @@ module CopHelper
13
13
  let(:parser_engine) { ENV.fetch('PARSER_ENGINE', :parser_whitequark).to_sym }
14
14
  let(:rails_version) { false }
15
15
 
16
+ before(:all) do
17
+ next if ENV['RUBOCOP_CORE_DEVELOPMENT']
18
+
19
+ plugins = Gem.loaded_specs.filter_map do |feature_name, feature_specification|
20
+ feature_name if feature_specification.metadata['default_lint_roller_plugin']
21
+ end
22
+ RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, plugins)
23
+ end
24
+
16
25
  def inspect_source(source, file = nil)
17
26
  RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
18
27
  RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
@@ -2,8 +2,10 @@
2
2
 
3
3
  require 'digest'
4
4
  require 'pathname'
5
+ require 'yaml'
5
6
  require_relative '../cache_config'
6
7
  require_relative '../config_finder'
8
+ require_relative '../path_util'
7
9
 
8
10
  #
9
11
  # This code is based on https://github.com/fohte/rubocop-daemon.
@@ -50,9 +52,11 @@ module RuboCop
50
52
  end.find(&:exist?)
51
53
  version_data = lockfile_path&.read || RuboCop::Version::STRING
52
54
  config_data = Pathname(ConfigFinder.find_config_path(Dir.pwd)).read
53
- todo_data = (rubocop_todo = Pathname('.rubocop_todo.yml')).exist? ? rubocop_todo.read : ''
55
+ yaml = YAML.safe_load(config_data, permitted_classes: [Regexp, Symbol], aliases: true)
56
+ inherit_from_data = inherit_from_data(yaml)
57
+ require_data = require_data(yaml)
54
58
 
55
- Digest::SHA1.hexdigest(version_data + config_data + todo_data)
59
+ Digest::SHA1.hexdigest(version_data + config_data + inherit_from_data + require_data)
56
60
  end
57
61
  # rubocop:enable Metrics/AbcSize
58
62
 
@@ -164,6 +168,35 @@ module RuboCop
164
168
  def write_version_file(version)
165
169
  version_path.write(version)
166
170
  end
171
+
172
+ def inherit_from_data(yaml)
173
+ return '' unless (inherit_from_paths = yaml['inherit_from'])
174
+
175
+ Array(inherit_from_paths).map do |path|
176
+ next if PathUtil.remote_file?(path)
177
+
178
+ path = Pathname(path)
179
+
180
+ path.exist? ? path.read : ''
181
+ end.join
182
+ end
183
+
184
+ def require_data(yaml)
185
+ return '' unless (require_paths = yaml['require'])
186
+
187
+ Array(require_paths).map do |path|
188
+ # NOTE: This targets only relative or absolute path specifications.
189
+ # For example, specifications like `require: rubocop-performance`,
190
+ # which can be loaded from `$LOAD_PATH`, are ignored.
191
+ next unless path.start_with?('.', '/')
192
+
193
+ # NOTE: `.so` files are not typically specified, so only `.rb` files are targeted.
194
+ path = "#{path}.rb" unless path.end_with?('.rb')
195
+ path = Pathname(path)
196
+
197
+ path.exist? ? path.read : ''
198
+ end.join
199
+ end
167
200
  end
168
201
  end
169
202
  end
@@ -86,7 +86,7 @@ module RuboCop
86
86
  end
87
87
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
88
88
 
89
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength:
89
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
90
90
  def run_command(server_command, detach:)
91
91
  case server_command
92
92
  when '--server'
@@ -107,7 +107,7 @@ module RuboCop
107
107
  Server::ClientCommand::Status.new.run
108
108
  end
109
109
  end
110
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength:
110
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength
111
111
 
112
112
  def fetch_cache_root_path_from(arguments)
113
113
  cache_root = arguments.detect { |argument| argument.start_with?('--cache-root') }