rubocop 1.62.1 → 1.63.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +22 -3
  4. data/lib/rubocop/cached_data.rb +11 -3
  5. data/lib/rubocop/config.rb +33 -10
  6. data/lib/rubocop/config_obsoletion.rb +1 -1
  7. data/lib/rubocop/cop/base.rb +40 -1
  8. data/lib/rubocop/cop/internal_affairs/example_description.rb +2 -1
  9. data/lib/rubocop/cop/lint/assignment_in_condition.rb +2 -2
  10. data/lib/rubocop/cop/lint/debugger.rb +27 -2
  11. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  12. data/lib/rubocop/cop/lint/empty_conditional_body.rb +1 -1
  13. data/lib/rubocop/cop/lint/mixed_case_range.rb +9 -4
  14. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +1 -1
  15. data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
  16. data/lib/rubocop/cop/lint/unreachable_code.rb +4 -2
  17. data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
  18. data/lib/rubocop/cop/mixin/code_length.rb +12 -1
  19. data/lib/rubocop/cop/mixin/method_complexity.rb +15 -6
  20. data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -1
  21. data/lib/rubocop/cop/naming/block_forwarding.rb +31 -12
  22. data/lib/rubocop/cop/naming/file_name.rb +2 -2
  23. data/lib/rubocop/cop/naming/inclusive_language.rb +1 -2
  24. data/lib/rubocop/cop/style/alias.rb +1 -0
  25. data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -1
  26. data/lib/rubocop/cop/style/collection_compact.rb +3 -3
  27. data/lib/rubocop/cop/style/copyright.rb +16 -11
  28. data/lib/rubocop/cop/style/eval_with_location.rb +3 -1
  29. data/lib/rubocop/cop/style/exact_regexp_match.rb +2 -1
  30. data/lib/rubocop/cop/style/format_string.rb +9 -9
  31. data/lib/rubocop/cop/style/map_into_array.rb +175 -0
  32. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  33. data/lib/rubocop/cop/style/map_to_set.rb +1 -1
  34. data/lib/rubocop/cop/style/redundant_argument.rb +24 -1
  35. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +1 -1
  36. data/lib/rubocop/cop/style/redundant_each.rb +1 -1
  37. data/lib/rubocop/cop/style/redundant_filter_chain.rb +1 -1
  38. data/lib/rubocop/cop/style/redundant_line_continuation.rb +11 -15
  39. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  40. data/lib/rubocop/cop/style/require_order.rb +1 -1
  41. data/lib/rubocop/cop/style/send.rb +4 -4
  42. data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
  43. data/lib/rubocop/cop/team.rb +3 -0
  44. data/lib/rubocop/formatter/clang_style_formatter.rb +3 -7
  45. data/lib/rubocop/formatter/tap_formatter.rb +3 -7
  46. data/lib/rubocop/lockfile.rb +56 -7
  47. data/lib/rubocop/lsp/routes.rb +1 -3
  48. data/lib/rubocop/lsp/server.rb +2 -0
  49. data/lib/rubocop/options.rb +3 -3
  50. data/lib/rubocop/rspec/expect_offense.rb +8 -0
  51. data/lib/rubocop/rspec/shared_contexts.rb +13 -1
  52. data/lib/rubocop/runner.rb +5 -1
  53. data/lib/rubocop/version.rb +5 -5
  54. data/lib/rubocop.rb +1 -0
  55. metadata +5 -4
@@ -1,18 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ begin
4
+ # We might not be running with `bundle exec`, so we need to pull in Bundler ourselves,
5
+ # in order to use `Bundler::LockfileParser`.
6
+ require 'bundler'
7
+ rescue LoadError
8
+ nil
9
+ end
10
+
3
11
  module RuboCop
4
12
  # Encapsulation of a lockfile for use when checking for gems.
5
13
  # Does not actually resolve gems, just parses the lockfile.
6
14
  # @api private
7
15
  class Lockfile
8
- # Gems that the bundle depends on
16
+ # @param [String, Pathname, nil] lockfile_path
17
+ def initialize(lockfile_path = nil)
18
+ lockfile_path ||= begin
19
+ ::Bundler.default_lockfile if bundler_lock_parser_defined?
20
+ rescue ::Bundler::GemfileNotFound
21
+ nil # We might not be a folder with a Gemfile, but that's okay.
22
+ end
23
+
24
+ @lockfile_path = lockfile_path
25
+ end
26
+
27
+ # Gems that the bundle directly depends on.
28
+ # @return [Array<Bundler::Dependency>, nil]
9
29
  def dependencies
10
30
  return [] unless parser
11
31
 
12
32
  parser.dependencies.values
13
33
  end
14
34
 
15
- # All activated gems, including transitive dependencies
35
+ # All activated gems, including transitive dependencies.
36
+ # @return [Array<Bundler::Dependency>, nil]
16
37
  def gems
17
38
  return [] unless parser
18
39
 
@@ -21,20 +42,48 @@ module RuboCop
21
42
  parser.dependencies.values.concat(parser.specs.flat_map(&:dependencies))
22
43
  end
23
44
 
45
+ # Returns the locked versions of gems from this lockfile.
46
+ # @param [Boolean] include_transitive_dependencies: When false, only direct dependencies
47
+ # are returned, i.e. those listed explicitly in the `Gemfile`.
48
+ # @returns [Hash{String => Gem::Version}] The locked gem versions, keyed by the gems' names.
49
+ def gem_versions(include_transitive_dependencies: true)
50
+ return {} unless parser
51
+
52
+ all_gem_versions = parser.specs.to_h { |spec| [spec.name, spec.version] }
53
+
54
+ if include_transitive_dependencies
55
+ all_gem_versions
56
+ else
57
+ direct_dep_names = parser.dependencies.keys
58
+ all_gem_versions.slice(*direct_dep_names)
59
+ end
60
+ end
61
+
62
+ # Whether this lockfile includes the named gem, directly or indirectly.
63
+ # @param [String] name
64
+ # @return [Boolean]
24
65
  def includes_gem?(name)
25
66
  gems.any? { |gem| gem.name == name }
26
67
  end
27
68
 
28
69
  private
29
70
 
71
+ # @return [Bundler::LockfileParser, nil]
30
72
  def parser
31
- return unless defined?(Bundler) && Bundler.default_lockfile
32
73
  return @parser if defined?(@parser)
33
74
 
34
- lockfile = Bundler.read_file(Bundler.default_lockfile)
35
- @parser = lockfile ? Bundler::LockfileParser.new(lockfile) : nil
36
- rescue Bundler::BundlerError
37
- nil
75
+ @parser = if @lockfile_path && bundler_lock_parser_defined?
76
+ begin
77
+ lockfile = ::Bundler.read_file(@lockfile_path)
78
+ ::Bundler::LockfileParser.new(lockfile) if lockfile
79
+ rescue ::Bundler::BundlerError
80
+ nil
81
+ end
82
+ end
83
+ end
84
+
85
+ def bundler_lock_parser_defined?
86
+ Object.const_defined?(:Bundler) && Bundler.const_defined?(:LockfileParser)
38
87
  end
39
88
  end
40
89
  end
@@ -73,9 +73,7 @@ module RuboCop
73
73
  end
74
74
 
75
75
  handle 'textDocument/diagnostic' do |request|
76
- doc = request[:params][:textDocument]
77
- result = diagnostic(doc[:uri], doc[:text])
78
- @server.write(result)
76
+ # no-op, diagnostics are handled in textDocument/didChange
79
77
  end
80
78
 
81
79
  handle 'textDocument/didChange' do |request|
@@ -21,6 +21,8 @@ module RuboCop
21
21
  # @api private
22
22
  class Server
23
23
  def initialize(config_store)
24
+ $PROGRAM_NAME = "rubocop --lsp #{ConfigFinder.project_root}"
25
+
24
26
  RuboCop::LSP.enable
25
27
 
26
28
  @reader = LanguageServer::Protocol::Transport::Io::Reader.new($stdin)
@@ -573,7 +573,7 @@ module RuboCop
573
573
  'cops. Only valid for --format junit.'],
574
574
  display_only_fail_level_offenses:
575
575
  ['Only output offense messages at',
576
- 'the specified --fail-level or above'],
576
+ 'the specified --fail-level or above.'],
577
577
  display_only_correctable: ['Only output correctable offense messages.'],
578
578
  display_only_safe_correctable: ['Only output safe-correctable offense messages',
579
579
  'when combined with --display-only-correctable.'],
@@ -636,8 +636,8 @@ module RuboCop
636
636
  raise_cop_error: ['Raise cop-related errors with cause and location.',
637
637
  'This is used to prevent cops from failing silently.',
638
638
  'Default is false.'],
639
- profile: 'Profile rubocop',
640
- memory: 'Profile rubocop memory usage'
639
+ profile: 'Profile rubocop.',
640
+ memory: 'Profile rubocop memory usage.'
641
641
  }.freeze
642
642
  end
643
643
  # rubocop:enable Metrics/ModuleLength
@@ -111,6 +111,7 @@ module RuboCop
111
111
  source
112
112
  end
113
113
 
114
+ # rubocop:disable Metrics/AbcSize
114
115
  def expect_offense(source, file = nil, severity: nil, chomp: false, **replacements)
115
116
  expected_annotations = parse_annotations(source, **replacements)
116
117
  source = expected_annotations.plain_source
@@ -123,8 +124,15 @@ module RuboCop
123
124
  expect(actual_annotations).to eq(expected_annotations), ''
124
125
  expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity
125
126
 
127
+ # Validate that all offenses have a range that formatters can display
128
+ expect do
129
+ @offenses.each { |offense| offense.location.source_line }
130
+ end.not_to raise_error, 'One of the offenses has a misconstructed range, for ' \
131
+ 'example if the offense is on line 1 and the source is empty'
132
+
126
133
  @offenses
127
134
  end
135
+ # rubocop:enable Metrics/AbcSize
128
136
 
129
137
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
130
138
  def expect_correction(correction, loop: true, source: nil)
@@ -110,7 +110,19 @@ RSpec.shared_context 'config' do # rubocop:disable Metrics/BlockLength
110
110
  let(:config) do
111
111
  hash = { 'AllCops' => all_cops_config, cop_class.cop_name => cur_cop_config }.merge!(other_cops)
112
112
 
113
- RuboCop::Config.new(hash, "#{Dir.pwd}/.rubocop.yml")
113
+ config = RuboCop::Config.new(hash, "#{Dir.pwd}/.rubocop.yml")
114
+
115
+ rails_version_in_gemfile = Gem::Version.new(
116
+ rails_version || RuboCop::Config::DEFAULT_RAILS_VERSION
117
+ )
118
+
119
+ allow(config).to receive(:gem_versions_in_target).and_return(
120
+ {
121
+ 'railties' => rails_version_in_gemfile
122
+ }
123
+ )
124
+
125
+ config
114
126
  end
115
127
 
116
128
  let(:cop) { cop_class.new(config, cop_options) }
@@ -20,7 +20,11 @@ module RuboCop
20
20
  message = 'Infinite loop detected'
21
21
  message += " in #{path}" if path
22
22
  message += " and caused by #{root_cause}" if root_cause
23
- super(message)
23
+ message += "\n"
24
+ hint = 'Hint: Please update to the latest RuboCop version if not already in use, ' \
25
+ "and report a bug if the issue still occurs on this version.\n" \
26
+ 'Please check the latest version at https://rubygems.org/gems/rubocop.'
27
+ super(Rainbow(message).red + Rainbow(hint).yellow)
24
28
  end
25
29
  end
26
30
 
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.62.1'
6
+ STRING = '1.63.4'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -11,7 +11,7 @@ module RuboCop
11
11
 
12
12
  CANONICAL_FEATURE_NAMES = {
13
13
  'Rspec' => 'RSpec', 'Graphql' => 'GraphQL', 'Md' => 'Markdown', 'Factory_bot' => 'FactoryBot',
14
- 'Thread_safety' => 'ThreadSafety'
14
+ 'Thread_safety' => 'ThreadSafety', 'Rspec_rails' => 'RSpecRails'
15
15
  }.freeze
16
16
  EXTENSION_PATH_NAMES = {
17
17
  'rubocop-md' => 'markdown', 'rubocop-factory_bot' => 'factory_bot'
@@ -42,9 +42,9 @@ module RuboCop
42
42
  # @api private
43
43
  def self.parser_version
44
44
  config_path = ConfigFinder.find_config_path(Dir.pwd)
45
- yaml = YAML.safe_load(
46
- File.read(config_path), permitted_classes: [Regexp, Symbol], aliases: true
47
- )
45
+ yaml = Util.silence_warnings do
46
+ ConfigLoader.load_yaml_configuration(config_path)
47
+ end
48
48
 
49
49
  if yaml.dig('AllCops', 'ParserEngine') == 'parser_prism'
50
50
  require 'prism'
data/lib/rubocop.rb CHANGED
@@ -557,6 +557,7 @@ require_relative 'rubocop/cop/style/lambda'
557
557
  require_relative 'rubocop/cop/style/lambda_call'
558
558
  require_relative 'rubocop/cop/style/line_end_concatenation'
559
559
  require_relative 'rubocop/cop/style/magic_comment_format'
560
+ require_relative 'rubocop/cop/style/map_into_array'
560
561
  require_relative 'rubocop/cop/style/map_to_hash'
561
562
  require_relative 'rubocop/cop/style/map_to_set'
562
563
  require_relative 'rubocop/cop/style/method_call_without_args_parentheses'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.62.1
4
+ version: 1.63.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2024-03-11 00:00:00.000000000 Z
13
+ date: 2024-04-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -778,6 +778,7 @@ files:
778
778
  - lib/rubocop/cop/style/line_end_concatenation.rb
779
779
  - lib/rubocop/cop/style/magic_comment_format.rb
780
780
  - lib/rubocop/cop/style/map_compact_with_conditional_block.rb
781
+ - lib/rubocop/cop/style/map_into_array.rb
781
782
  - lib/rubocop/cop/style/map_to_hash.rb
782
783
  - lib/rubocop/cop/style/map_to_set.rb
783
784
  - lib/rubocop/cop/style/method_call_with_args_parentheses.rb
@@ -1031,9 +1032,9 @@ licenses:
1031
1032
  - MIT
1032
1033
  metadata:
1033
1034
  homepage_uri: https://rubocop.org/
1034
- changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.62.1
1035
+ changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.63.4
1035
1036
  source_code_uri: https://github.com/rubocop/rubocop/
1036
- documentation_uri: https://docs.rubocop.org/rubocop/1.62/
1037
+ documentation_uri: https://docs.rubocop.org/rubocop/1.63/
1037
1038
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1038
1039
  rubygems_mfa_required: 'true'
1039
1040
  post_install_message: