rubocop 1.42.0 → 1.44.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +59 -2
  4. data/lib/rubocop/config_loader.rb +12 -15
  5. data/lib/rubocop/cop/corrector.rb +10 -2
  6. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
  7. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  8. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  9. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
  10. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -5
  11. data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
  12. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +0 -2
  13. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  14. data/lib/rubocop/cop/lint/else_layout.rb +2 -6
  15. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +10 -7
  16. data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
  17. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
  18. data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
  19. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +9 -1
  20. data/lib/rubocop/cop/lint/void.rb +17 -10
  21. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  22. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  23. data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
  24. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +2 -5
  25. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  26. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +46 -16
  27. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  28. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
  29. data/lib/rubocop/cop/registry.rb +12 -7
  30. data/lib/rubocop/cop/style/access_modifier_declarations.rb +18 -10
  31. data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
  32. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -9
  33. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  34. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
  35. data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
  36. data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
  37. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  38. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  39. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
  40. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  41. data/lib/rubocop/cop/style/min_max_comparison.rb +11 -1
  42. data/lib/rubocop/cop/style/missing_else.rb +13 -1
  43. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  44. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  45. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
  46. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -5
  47. data/lib/rubocop/cop/style/operator_method_call.rb +15 -1
  48. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  49. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  50. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  51. data/lib/rubocop/cop/style/require_order.rb +2 -9
  52. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  53. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
  54. data/lib/rubocop/cop/style/yoda_expression.rb +7 -0
  55. data/lib/rubocop/cop/variable_force.rb +1 -1
  56. data/lib/rubocop/formatter.rb +0 -1
  57. data/lib/rubocop/path_util.rb +11 -6
  58. data/lib/rubocop/rspec/expect_offense.rb +5 -3
  59. data/lib/rubocop/server/cache.rb +3 -1
  60. data/lib/rubocop/version.rb +1 -1
  61. data/lib/rubocop.rb +4 -0
  62. metadata +15 -11
@@ -77,7 +77,8 @@ module RuboCop
77
77
  # but it's not necessry to escape hyphen if it's the first or last character
78
78
  # within the character class. This method checks if that's the case.
79
79
  # e.g. "[0-9\\-]" or "[\\-0-9]" would return true
80
- node.source[index] == '[' || node.source[index + 3] == ']'
80
+ contents_range(node).source[index - 1] == '[' ||
81
+ contents_range(node).source[index + 2] == ']'
81
82
  end
82
83
 
83
84
  def delimiter?(node, char)
@@ -88,10 +88,9 @@ module RuboCop
88
88
  return unless previous_older_sibling
89
89
 
90
90
  add_offense(node, message: format(MSG, name: node.method_name)) do |corrector|
91
- swap(
91
+ corrector.swap(
92
92
  range_with_comments_and_lines(previous_older_sibling),
93
- range_with_comments_and_lines(node.parent.if_type? ? node.parent : node),
94
- corrector: corrector
93
+ range_with_comments_and_lines(node.parent.if_type? ? node.parent : node)
95
94
  )
96
95
  end
97
96
  end
@@ -130,12 +129,6 @@ module RuboCop
130
129
  end_pos: node2.location.expression.end_pos
131
130
  ).source.include?("\n\n")
132
131
  end
133
-
134
- def swap(range1, range2, corrector:)
135
- inserted = range2.source
136
- corrector.insert_before(range1, inserted)
137
- corrector.remove(range2)
138
- end
139
132
  end
140
133
  end
141
134
  end
@@ -72,8 +72,10 @@ module RuboCop
72
72
 
73
73
  def each_semicolon
74
74
  tokens_for_lines.each do |line, tokens|
75
- yield line, tokens.last.column, tokens[-2] if tokens.last.semicolon?
76
- yield line, tokens.first.column if tokens.first.semicolon?
75
+ semicolon_pos = semicolon_position(tokens)
76
+ after_expr_pos = semicolon_pos == -1 ? -2 : semicolon_pos
77
+
78
+ yield line, tokens[semicolon_pos].column, tokens[after_expr_pos] if semicolon_pos
77
79
  end
78
80
  end
79
81
 
@@ -81,6 +83,26 @@ module RuboCop
81
83
  processed_source.tokens.group_by(&:line)
82
84
  end
83
85
 
86
+ def semicolon_position(tokens)
87
+ if tokens.last.semicolon?
88
+ -1
89
+ elsif tokens.first.semicolon?
90
+ 0
91
+ elsif exist_semicolon_before_right_curly_brace?(tokens)
92
+ -3
93
+ elsif exist_semicolon_after_left_curly_brace?(tokens)
94
+ 2
95
+ end
96
+ end
97
+
98
+ def exist_semicolon_before_right_curly_brace?(tokens)
99
+ tokens[-2]&.right_curly_brace? && tokens[-3]&.semicolon?
100
+ end
101
+
102
+ def exist_semicolon_after_left_curly_brace?(tokens)
103
+ tokens[1]&.left_curly_brace? && tokens[2]&.semicolon?
104
+ end
105
+
84
106
  def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
85
107
  range = source_range(processed_source.buffer, line, column)
86
108
 
@@ -41,10 +41,13 @@ module RuboCop
41
41
 
42
42
  def on_pair(node)
43
43
  return unless string_hash_key?(node)
44
+
45
+ key_content = node.key.str_content
46
+ return unless key_content.valid_encoding?
44
47
  return if receive_environments_method?(node)
45
48
 
46
49
  add_offense(node.key) do |corrector|
47
- symbol_content = node.key.str_content.to_sym.inspect
50
+ symbol_content = key_content.to_sym.inspect
48
51
 
49
52
  corrector.replace(node.key, symbol_content)
50
53
  end
@@ -7,6 +7,13 @@ module RuboCop
7
7
  # and `^` operators) where the order of expression is reversed, eg. `1 + x`.
8
8
  # This cop complements `Style/YodaCondition` cop, which has a similar purpose.
9
9
  #
10
+ # This cop is disabled by default to respect user intentions such as:
11
+ #
12
+ # [source,ruby]
13
+ # ----
14
+ # config.server_port = 9000 + ENV["TEST_ENV_NUMBER"].to_i
15
+ # ----
16
+ #
10
17
  # @safety
11
18
  # This cop is unsafe because binary operators can be defined
12
19
  # differently on different classes, and are not guaranteed to
@@ -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
@@ -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
 
@@ -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
 
@@ -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
@@ -68,7 +68,9 @@ module RuboCop
68
68
  file_contents = File.read(config_path)
69
69
  yaml_code = ERB.new(file_contents).result
70
70
 
71
- config_yaml = YAML.safe_load(yaml_code, permitted_classes: [Regexp, Symbol])
71
+ config_yaml = YAML.safe_load(
72
+ yaml_code, permitted_classes: [Regexp, Symbol], aliases: true
73
+ )
72
74
 
73
75
  # For compatibility with Ruby 3.0 or lower.
74
76
  if Gem::Version.new(Psych::VERSION) < Gem::Version.new('4.0.0')
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '1.42.0'
6
+ STRING = '1.44.0'
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'
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.42.0
4
+ version: 1.44.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
8
8
  - Jonas Arvidsson
9
9
  - Yuji Nakayama
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2023-01-01 00:00:00.000000000 Z
13
+ date: 2023-01-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
@@ -46,14 +46,14 @@ dependencies:
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 3.1.2.1
49
+ version: 3.2.0.0
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 3.1.2.1
56
+ version: 3.2.0.0
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: rainbow
59
59
  requirement: !ruby/object:Gem::Requirement
@@ -154,7 +154,7 @@ dependencies:
154
154
  requirements:
155
155
  - - ">="
156
156
  - !ruby/object:Gem::Version
157
- version: 1.4.0
157
+ version: 2.4.0
158
158
  - - "<"
159
159
  - !ruby/object:Gem::Version
160
160
  version: '3.0'
@@ -164,7 +164,7 @@ dependencies:
164
164
  requirements:
165
165
  - - ">="
166
166
  - !ruby/object:Gem::Version
167
- version: 1.4.0
167
+ version: 2.4.0
168
168
  - - "<"
169
169
  - !ruby/object:Gem::Version
170
170
  version: '3.0'
@@ -275,6 +275,7 @@ files:
275
275
  - lib/rubocop/cop/force.rb
276
276
  - lib/rubocop/cop/gemspec/dependency_version.rb
277
277
  - lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb
278
+ - lib/rubocop/cop/gemspec/development_dependencies.rb
278
279
  - lib/rubocop/cop/gemspec/duplicated_assignment.rb
279
280
  - lib/rubocop/cop/gemspec/ordered_dependencies.rb
280
281
  - lib/rubocop/cop/gemspec/require_mfa.rb
@@ -539,6 +540,7 @@ files:
539
540
  - lib/rubocop/cop/lint/useless_assignment.rb
540
541
  - lib/rubocop/cop/lint/useless_else_without_rescue.rb
541
542
  - lib/rubocop/cop/lint/useless_method_definition.rb
543
+ - lib/rubocop/cop/lint/useless_rescue.rb
542
544
  - lib/rubocop/cop/lint/useless_ruby2_keywords.rb
543
545
  - lib/rubocop/cop/lint/useless_setter_call.rb
544
546
  - lib/rubocop/cop/lint/useless_times.rb
@@ -691,6 +693,7 @@ files:
691
693
  - lib/rubocop/cop/style/command_literal.rb
692
694
  - lib/rubocop/cop/style/comment_annotation.rb
693
695
  - lib/rubocop/cop/style/commented_keyword.rb
696
+ - lib/rubocop/cop/style/comparable_clamp.rb
694
697
  - lib/rubocop/cop/style/concat_array_literals.rb
695
698
  - lib/rubocop/cop/style/conditional_assignment.rb
696
699
  - lib/rubocop/cop/style/constant_visibility.rb
@@ -752,6 +755,7 @@ files:
752
755
  - lib/rubocop/cop/style/infinite_loop.rb
753
756
  - lib/rubocop/cop/style/inline_comment.rb
754
757
  - lib/rubocop/cop/style/inverse_methods.rb
758
+ - lib/rubocop/cop/style/invertible_unless_condition.rb
755
759
  - lib/rubocop/cop/style/ip_addresses.rb
756
760
  - lib/rubocop/cop/style/keyword_parameters_order.rb
757
761
  - lib/rubocop/cop/style/lambda.rb
@@ -995,10 +999,10 @@ metadata:
995
999
  homepage_uri: https://rubocop.org/
996
1000
  changelog_uri: https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md
997
1001
  source_code_uri: https://github.com/rubocop/rubocop/
998
- documentation_uri: https://docs.rubocop.org/rubocop/1.42/
1002
+ documentation_uri: https://docs.rubocop.org/rubocop/1.44/
999
1003
  bug_tracker_uri: https://github.com/rubocop/rubocop/issues
1000
1004
  rubygems_mfa_required: 'true'
1001
- post_install_message:
1005
+ post_install_message:
1002
1006
  rdoc_options: []
1003
1007
  require_paths:
1004
1008
  - lib
@@ -1013,8 +1017,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1013
1017
  - !ruby/object:Gem::Version
1014
1018
  version: '0'
1015
1019
  requirements: []
1016
- rubygems_version: 3.3.7
1017
- signing_key:
1020
+ rubygems_version: 3.1.2
1021
+ signing_key:
1018
1022
  specification_version: 4
1019
1023
  summary: Automatic Ruby code style checking tool.
1020
1024
  test_files: []