rubocop 1.66.0 → 1.66.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 694e2b2b4a391c38f0afd3667beaf252adcf2fe68374a544cf14748cfc97dd80
4
- data.tar.gz: ad3c46e7dbeaf3c94d16c7427458d2079e28ef58d08463af37ae2a1f6561b345
3
+ metadata.gz: 8a10568527197e80492a28c5b524fa524dbce965513daff4f47e260e03b49d98
4
+ data.tar.gz: 8ea447437cbd36aa805b3941cf51b07eecc9dc538ed6a4584f09a1335e21cb48
5
5
  SHA512:
6
- metadata.gz: 8df7fd7e62c55adac6725b1d60e071c0021eed0434a84c40b86b9a32b27b3a5e9cd3debe9d3edd21fec732c0065553a7a8299f7810ffffde2d010f5231b657e5
7
- data.tar.gz: 24192e071f881b02ac9c766b3d0847e8fa77d9e4ff5942c9a2cc72dd0f4d77412c13337a1b76bcd7e250e5a7b737ddf613764c0dccbb49b09fdc0f27b6a8c732
6
+ metadata.gz: 1f292c70ffac1cb186e5be899582c4849a29866722c87ab92a60a723dd16fe86fdf12073c8a7190bba197d246eae3b25ba3cdc6b586b28061cef39ac22b3b438
7
+ data.tar.gz: 2dbfc28779854fae0d28fd266ba269b92cb0e3b5c26200aa250df02ff9810804b7b1fcc289a43bac1913dc04c79f788487f9351444f7242cbcdad6854a821d69
@@ -4,6 +4,8 @@ module RuboCop
4
4
  # This class parses the special `rubocop:disable` comments in a source
5
5
  # and provides a way to check if each cop is enabled at arbitrary line.
6
6
  class CommentConfig
7
+ extend SimpleForwardable
8
+
7
9
  CONFIG_DISABLED_LINE_RANGE_MIN = -Float::INFINITY
8
10
 
9
11
  # This class provides an API compatible with RuboCop::DirectiveComment
@@ -27,19 +29,13 @@ module RuboCop
27
29
 
28
30
  attr_reader :processed_source
29
31
 
32
+ def_delegators :@processed_source, :config, :registry
33
+
30
34
  def initialize(processed_source)
31
35
  @processed_source = processed_source
32
36
  @no_directives = !processed_source.raw_source.include?('rubocop')
33
37
  end
34
38
 
35
- def config
36
- @processed_source.config
37
- end
38
-
39
- def registry
40
- @processed_source.registry
41
- end
42
-
43
39
  def cop_enabled_at_line?(cop, line_number)
44
40
  cop = cop.cop_name if cop.respond_to?(:cop_name)
45
41
  disabled_line_ranges = cop_disabled_line_ranges[cop]
@@ -12,6 +12,7 @@ module RuboCop
12
12
  class Config
13
13
  include PathUtil
14
14
  include FileFinder
15
+ extend SimpleForwardable
15
16
 
16
17
  CopConfig = Struct.new(:name, :metadata)
17
18
 
@@ -59,22 +60,9 @@ module RuboCop
59
60
  self
60
61
  end
61
62
 
62
- %i[[] []= delete dig each key? keys each_key fetch map merge replace to_h to_hash
63
- transform_values].each do |method|
64
- class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
65
- def #{method}(...) # def key?(...)
66
- @hash.#{method}(...) # @hash.key?(...)
67
- end # end
68
- RUBY
69
- end
70
-
71
- def validate(...)
72
- @validator.validate(...)
73
- end
74
-
75
- def target_ruby_version
76
- @validator.target_ruby_version
77
- end
63
+ def_delegators :@hash, :[], :[]=, :delete, :dig, :each, :key?, :keys, :each_key,
64
+ :fetch, :map, :merge, :replace, :to_h, :to_hash, :transform_values
65
+ def_delegators :@validator, :validate, :target_ruby_version
78
66
 
79
67
  def to_s
80
68
  @to_s ||= @hash.to_s
@@ -67,8 +67,8 @@ module RuboCop
67
67
  def load_yaml_configuration(absolute_path)
68
68
  file_contents = read_file(absolute_path)
69
69
  yaml_code = Dir.chdir(File.dirname(absolute_path)) { ERB.new(file_contents).result }
70
- check_duplication(yaml_code, absolute_path)
71
- hash = yaml_safe_load(yaml_code, absolute_path) || {}
70
+ yaml_tree = check_duplication(yaml_code, absolute_path)
71
+ hash = yaml_tree_to_hash(yaml_tree) || {}
72
72
 
73
73
  puts "configuration from #{absolute_path}" if debug?
74
74
 
@@ -235,8 +235,8 @@ module RuboCop
235
235
  raise ConfigNotFoundError, "Configuration file not found: #{absolute_path}"
236
236
  end
237
237
 
238
- def yaml_safe_load(yaml_code, filename)
239
- yaml_safe_load!(yaml_code, filename)
238
+ def yaml_tree_to_hash(yaml_tree)
239
+ yaml_tree_to_hash!(yaml_tree)
240
240
  rescue ::StandardError
241
241
  if defined?(::SafeYAML)
242
242
  raise 'SafeYAML is unmaintained, no longer needed and should be removed'
@@ -245,10 +245,16 @@ module RuboCop
245
245
  raise
246
246
  end
247
247
 
248
- def yaml_safe_load!(yaml_code, filename)
249
- YAML.safe_load(
250
- yaml_code, permitted_classes: [Regexp, Symbol], aliases: true, filename: filename
251
- )
248
+ def yaml_tree_to_hash!(yaml_tree)
249
+ return nil unless yaml_tree
250
+
251
+ # Optimization: Because we checked for duplicate keys, we already have the
252
+ # yaml tree and don't need to parse it again.
253
+ # Also see https://github.com/ruby/psych/blob/v5.1.2/lib/psych.rb#L322-L336
254
+ class_loader = YAML::ClassLoader::Restricted.new(%w[Regexp Symbol], [])
255
+ scanner = YAML::ScalarScanner.new(class_loader)
256
+ visitor = YAML::Visitors::ToRuby.new(scanner, class_loader)
257
+ visitor.accept(yaml_tree)
252
258
  end
253
259
  end
254
260
 
@@ -3,7 +3,9 @@
3
3
  module RuboCop
4
4
  # Handles validation of configuration, for example cop names, parameter
5
5
  # names, and Ruby versions.
6
- class ConfigValidator # rubocop:disable Metrics/ClassLength
6
+ class ConfigValidator
7
+ extend SimpleForwardable
8
+
7
9
  # @api private
8
10
  COMMON_PARAMS = %w[Exclude Include Severity inherit_mode AutoCorrect StyleGuide Details].freeze
9
11
  # @api private
@@ -19,20 +21,14 @@ module RuboCop
19
21
  CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
20
22
  private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
21
23
 
24
+ def_delegators :@config, :smart_loaded_path, :for_all_cops
25
+
22
26
  def initialize(config)
23
27
  @config = config
24
28
  @config_obsoletion = ConfigObsoletion.new(config)
25
29
  @target_ruby = TargetRuby.new(config)
26
30
  end
27
31
 
28
- def smart_loaded_path
29
- @config.smart_loaded_path
30
- end
31
-
32
- def for_all_cops
33
- @config.for_all_cops
34
- end
35
-
36
32
  def validate
37
33
  check_cop_config_value(@config)
38
34
  reject_conflicting_safe_settings
@@ -45,7 +45,7 @@ module RuboCop
45
45
  PATTERN
46
46
 
47
47
  def on_send(node)
48
- return if node.arguments.none?
48
+ return unless node.arguments.count == 2
49
49
  return unless valid_method_name?(node)
50
50
 
51
51
  actual_name = node.first_argument.value.to_s
@@ -179,6 +179,7 @@ module RuboCop
179
179
 
180
180
  def comment_in_else?(node)
181
181
  node = node.parent while node.if_type? && node.elsif?
182
+ return false unless node.else?
182
183
 
183
184
  processed_source.contains_comment?(node.loc.else.join(node.source_range.end))
184
185
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  def_node_matcher :array_node, '(send (const {nil? cbase} :Array) :new (array)?)'
35
35
 
36
36
  # @!method hash_node(node)
37
- def_node_matcher :hash_node, '(send (const {nil? cbase} :Hash) :new (array)?)'
37
+ def_node_matcher :hash_node, '(send (const {nil? cbase} :Hash) :new)'
38
38
 
39
39
  # @!method str_node(node)
40
40
  def_node_matcher :str_node, '(send (const {nil? cbase} :String) :new)'
@@ -36,10 +36,12 @@ module RuboCop
36
36
 
37
37
  private
38
38
 
39
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
39
40
  def message(node)
40
41
  template = if node.if_branch&.begin_type?
41
42
  MSG_NEWLINE
42
- elsif node.else_branch&.if_type? || node.else_branch&.begin_type?
43
+ elsif node.else_branch&.if_type? || node.else_branch&.begin_type? ||
44
+ use_block_in_branches?(node)
43
45
  MSG_IF_ELSE
44
46
  else
45
47
  MSG_TERNARY
@@ -47,15 +49,20 @@ module RuboCop
47
49
 
48
50
  format(template, expr: node.condition.source)
49
51
  end
52
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
50
53
 
51
54
  def autocorrect(corrector, node)
52
- if node.if_branch&.begin_type? || node.else_branch&.begin_type?
55
+ if node.branches.compact.any?(&:begin_type?) || use_block_in_branches?(node)
53
56
  corrector.replace(node.loc.begin, "\n")
54
57
  else
55
58
  corrector.replace(node, replacement(node))
56
59
  end
57
60
  end
58
61
 
62
+ def use_block_in_branches?(node)
63
+ node.branches.compact.any? { |branch| branch.block_type? || branch.numblock_type? }
64
+ end
65
+
59
66
  def replacement(node)
60
67
  return correct_elsif(node) if node.else_branch&.if_type?
61
68
 
@@ -105,26 +105,21 @@ module RuboCop
105
105
 
106
106
  # Value object to extract source ranges for the different parts of a magic comment
107
107
  class CommentRange
108
+ extend SimpleForwardable
109
+
108
110
  DIRECTIVE_REGEXP = Regexp.union(MagicComment::KEYWORDS.map do |_, v|
109
111
  Regexp.new(v, Regexp::IGNORECASE)
110
112
  end).freeze
111
113
 
112
114
  VALUE_REGEXP = Regexp.new("(?:#{DIRECTIVE_REGEXP}:\s*)(.*?)(?=;|$)")
113
115
 
116
+ def_delegators :@comment, :text, :loc
114
117
  attr_reader :comment
115
118
 
116
119
  def initialize(comment)
117
120
  @comment = comment
118
121
  end
119
122
 
120
- def text
121
- @comment.text
122
- end
123
-
124
- def loc
125
- @comment.loc
126
- end
127
-
128
123
  # A magic comment can contain one directive (normal style) or
129
124
  # multiple directives (emacs style)
130
125
  def directives
@@ -147,10 +147,8 @@ module RuboCop
147
147
  false
148
148
  when :begin, :kwbegin
149
149
  !node.right_sibling && return_value_used?(parent)
150
- when :block, :numblock
151
- !parent.void_context?
152
150
  else
153
- true
151
+ !parent.respond_to?(:void_context?) || !parent.void_context?
154
152
  end
155
153
  end
156
154
 
@@ -16,7 +16,7 @@ module RuboCop
16
16
  # "#{foo} bar".dup
17
17
  #
18
18
  # # good
19
- # "#{foo} bar"
19
+ # "#{foo} bar"
20
20
  #
21
21
  class RedundantInterpolationUnfreeze < Base
22
22
  include FrozenStringLiteral
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.66.0'
6
+ STRING = '1.66.1'
7
7
 
8
8
  MSG = '%<version>s (using %<parser_version>s, ' \
9
9
  'rubocop-ast %<rubocop_ast_version>s, ' \
@@ -16,6 +16,7 @@ module RuboCop
16
16
  return unless tree
17
17
 
18
18
  traverse(tree, &on_duplicated)
19
+ tree
19
20
  end
20
21
 
21
22
  def self.traverse(tree, &on_duplicated)
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.66.0
4
+ version: 1.66.1
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-08-31 00:00:00.000000000 Z
13
+ date: 2024-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -114,7 +114,7 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 1.32.1
117
+ version: 1.32.2
118
118
  - - "<"
119
119
  - !ruby/object:Gem::Version
120
120
  version: '2.0'
@@ -124,7 +124,7 @@ dependencies:
124
124
  requirements:
125
125
  - - ">="
126
126
  - !ruby/object:Gem::Version
127
- version: 1.32.1
127
+ version: 1.32.2
128
128
  - - "<"
129
129
  - !ruby/object:Gem::Version
130
130
  version: '2.0'
@@ -1017,7 +1017,7 @@ licenses:
1017
1017
  - MIT
1018
1018
  metadata:
1019
1019
  homepage_uri: https://rubocop.org/
1020
- changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.66.0
1020
+ changelog_uri: https://github.com/rubocop/rubocop/releases/tag/v1.66.1
1021
1021
  source_code_uri: https://github.com/rubocop/rubocop/
1022
1022
  documentation_uri: https://docs.rubocop.org/rubocop/1.66/
1023
1023
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues