rubocop 1.75.1 → 1.75.5

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 (89) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +29 -18
  3. data/lib/rubocop/config_validator.rb +6 -6
  4. data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
  5. data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +1 -0
  6. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
  7. data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
  8. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  9. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  10. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
  11. data/lib/rubocop/cop/layout/hash_alignment.rb +2 -2
  12. data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
  13. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
  14. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -4
  15. data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
  16. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -1
  17. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
  18. data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
  19. data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
  20. data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
  21. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
  22. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -3
  23. data/lib/rubocop/cop/lint/literal_as_condition.rb +25 -11
  24. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
  25. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  26. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +2 -2
  27. data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
  28. data/lib/rubocop/cop/lint/redundant_type_conversion.rb +7 -4
  29. data/lib/rubocop/cop/lint/return_in_void_context.rb +7 -2
  30. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
  32. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  33. data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
  34. data/lib/rubocop/cop/lint/void.rb +2 -2
  35. data/lib/rubocop/cop/message_annotator.rb +7 -3
  36. data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
  37. data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
  38. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  39. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  40. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +0 -1
  41. data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
  42. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
  43. data/lib/rubocop/cop/mixin/trailing_comma.rb +9 -5
  44. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  45. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  46. data/lib/rubocop/cop/style/arguments_forwarding.rb +8 -5
  47. data/lib/rubocop/cop/style/class_and_module_children.rb +19 -3
  48. data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
  49. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  50. data/lib/rubocop/cop/style/comparable_between.rb +2 -2
  51. data/lib/rubocop/cop/style/conditional_assignment.rb +16 -4
  52. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  53. data/lib/rubocop/cop/style/empty_literal.rb +4 -0
  54. data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
  55. data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
  56. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
  57. data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
  58. data/lib/rubocop/cop/style/hash_fetch_chain.rb +0 -1
  59. data/lib/rubocop/cop/style/hash_syntax.rb +3 -0
  60. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  61. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  62. data/lib/rubocop/cop/style/identical_conditional_branches.rb +3 -3
  63. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
  64. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  65. data/lib/rubocop/cop/style/lambda_call.rb +7 -2
  66. data/lib/rubocop/cop/style/map_into_array.rb +3 -1
  67. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -1
  68. data/lib/rubocop/cop/style/redundant_condition.rb +13 -1
  69. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +1 -1
  70. data/lib/rubocop/cop/style/redundant_line_continuation.rb +0 -3
  71. data/lib/rubocop/cop/style/redundant_parentheses.rb +20 -3
  72. data/lib/rubocop/cop/style/return_nil.rb +2 -2
  73. data/lib/rubocop/cop/style/safe_navigation.rb +18 -3
  74. data/lib/rubocop/cop/style/super_arguments.rb +1 -2
  75. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +7 -1
  76. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
  77. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
  78. data/lib/rubocop/cop/util.rb +1 -1
  79. data/lib/rubocop/cop/variable_force/variable.rb +1 -1
  80. data/lib/rubocop/cops_documentation_generator.rb +6 -2
  81. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  82. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  83. data/lib/rubocop/magic_comment.rb +8 -0
  84. data/lib/rubocop/rspec/cop_helper.rb +2 -2
  85. data/lib/rubocop/rspec/shared_contexts.rb +1 -2
  86. data/lib/rubocop/server/cache.rb +13 -10
  87. data/lib/rubocop/target_finder.rb +6 -2
  88. data/lib/rubocop/version.rb +1 -1
  89. metadata +9 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a064330786019e0efee6b496e761b05bea0286f0afdcbb4fffad2f07433aef9d
4
- data.tar.gz: eb1ee00d5fe060ea49203635df3c7457df6f416de93c9801a687386aa02f4768
3
+ metadata.gz: 9421b54201da724554271e44969dd9b78ea86635ba622fd5d3f1807d9e3e3530
4
+ data.tar.gz: 6f76b18333faee3119a32957b72a5906a9a99cde620b95dc88014b1ebdbfa4f7
5
5
  SHA512:
6
- metadata.gz: 336c6e38cc6162a00ca4c9780dcc71fb4b03a785af0eb6abb6c30baf2e1c8512af12e921a2036564102290324e418278cb83ceb06d7a1ec44ddf314923275f89
7
- data.tar.gz: cbe9d5ab5685dbab70de2b281ecc807494631a294273260d87315d6875b753d9c2b60f6d7c14379eaf5b384894a4400c747ef91a57323c418c66a51076b33f06
6
+ metadata.gz: bfbad1a5c263aa4de3704954e0835a71d4d6b56bbf564dfec23dc1cc7b75913ae064ddf70a01c942a1ebfb3502fe881d840826a306ce24ed450747058f59541e
7
+ data.tar.gz: 88be3812e81036e0e8dde18c38548052f447ae76cf3195fc1b16189eb4c5d7e5b460f8425ecee663d96bfcb4c543f592f8471b7306906262426894fa76301da1
data/config/default.yml CHANGED
@@ -274,7 +274,8 @@ Bundler/OrderedGems:
274
274
  Gemspec/AddRuntimeDependency:
275
275
  Description: 'Prefer `add_dependency` over `add_runtime_dependency`.'
276
276
  StyleGuide: '#add_dependency_vs_add_runtime_dependency'
277
- Reference: https://github.com/rubygems/rubygems/issues/7799#issuecomment-2192720316
277
+ References:
278
+ - https://github.com/rubygems/rubygems/issues/7799#issuecomment-2192720316
278
279
  Enabled: pending
279
280
  VersionAdded: '1.65'
280
281
  Include:
@@ -343,7 +344,7 @@ Gemspec/RequireMFA:
343
344
  Severity: warning
344
345
  VersionAdded: '1.23'
345
346
  VersionChanged: '1.40'
346
- Reference:
347
+ References:
347
348
  - https://guides.rubygems.org/mfa-requirement-opt-in/
348
349
  Include:
349
350
  - '**/*.gemspec'
@@ -604,7 +605,7 @@ Layout/EmptyLineAfterMultilineCondition:
604
605
  # This is disabled, because this style is not very common in practice.
605
606
  Enabled: false
606
607
  VersionAdded: '0.90'
607
- Reference:
608
+ References:
608
609
  - https://github.com/airbnb/ruby#multiline-if-newline
609
610
 
610
611
  Layout/EmptyLineBetweenDefs:
@@ -639,7 +640,7 @@ Layout/EmptyLinesAroundAccessModifier:
639
640
  SupportedStyles:
640
641
  - around
641
642
  - only_before
642
- Reference:
643
+ References:
643
644
  # A reference to `EnforcedStyle: only_before`.
644
645
  - https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
645
646
 
@@ -996,7 +997,7 @@ Layout/IndentationConsistency:
996
997
  SupportedStyles:
997
998
  - normal
998
999
  - indented_internal_methods
999
- Reference:
1000
+ References:
1000
1001
  # A reference to `EnforcedStyle: indented_internal_methods`.
1001
1002
  - https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
1002
1003
 
@@ -2035,7 +2036,8 @@ Lint/InterpolationCheck:
2035
2036
 
2036
2037
  Lint/ItWithoutArgumentsInBlock:
2037
2038
  Description: 'Checks uses of `it` calls without arguments in block.'
2038
- Reference: 'https://bugs.ruby-lang.org/issues/18980'
2039
+ References:
2040
+ - 'https://bugs.ruby-lang.org/issues/18980'
2039
2041
  Enabled: pending
2040
2042
  VersionAdded: '1.59'
2041
2043
 
@@ -2673,7 +2675,7 @@ Metrics/AbcSize:
2673
2675
  Description: >-
2674
2676
  A calculated magnitude based on number of assignments,
2675
2677
  branches, and conditions.
2676
- Reference:
2678
+ References:
2677
2679
  - http://c2.com/cgi/wiki?AbcMetric
2678
2680
  - https://en.wikipedia.org/wiki/ABC_Software_Metric
2679
2681
  Enabled: true
@@ -3150,7 +3152,8 @@ Security/JSONLoad:
3150
3152
  Description: >-
3151
3153
  Prefer usage of `JSON.parse` over `JSON.load` due to potential
3152
3154
  security issues. See reference for more information.
3153
- Reference: 'https://ruby-doc.org/stdlib-2.7.0/libdoc/json/rdoc/JSON.html#method-i-load'
3155
+ References:
3156
+ - 'https://ruby-doc.org/stdlib-2.7.0/libdoc/json/rdoc/JSON.html#method-i-load'
3154
3157
  Enabled: true
3155
3158
  VersionAdded: '0.43'
3156
3159
  VersionChanged: '1.22'
@@ -3162,7 +3165,8 @@ Security/MarshalLoad:
3162
3165
  Description: >-
3163
3166
  Avoid using of `Marshal.load` or `Marshal.restore` due to potential
3164
3167
  security issues. See reference for more information.
3165
- Reference: 'https://ruby-doc.org/core-2.7.0/Marshal.html#module-Marshal-label-Security+considerations'
3168
+ References:
3169
+ - 'https://ruby-doc.org/core-2.7.0/Marshal.html#module-Marshal-label-Security+considerations'
3166
3170
  Enabled: true
3167
3171
  VersionAdded: '0.47'
3168
3172
 
@@ -3177,7 +3181,8 @@ Security/YAMLLoad:
3177
3181
  Description: >-
3178
3182
  Prefer usage of `YAML.safe_load` over `YAML.load` due to potential
3179
3183
  security issues. See reference for more information.
3180
- Reference: 'https://ruby-doc.org/stdlib-2.7.0/libdoc/yaml/rdoc/YAML.html#module-YAML-label-Security'
3184
+ References:
3185
+ - 'https://ruby-doc.org/stdlib-2.7.0/libdoc/yaml/rdoc/YAML.html#module-YAML-label-Security'
3181
3186
  Enabled: true
3182
3187
  VersionAdded: '0.47'
3183
3188
  SafeAutoCorrect: false
@@ -3271,7 +3276,8 @@ Style/ArrayCoercion:
3271
3276
 
3272
3277
  Style/ArrayFirstLast:
3273
3278
  Description: 'Use `arr.first` and `arr.last` instead of `arr[0]` and `arr[-1]`.'
3274
- Reference: '#first-and-last'
3279
+ References:
3280
+ - '#first-and-last'
3275
3281
  Enabled: false
3276
3282
  VersionAdded: '1.58'
3277
3283
  Safe: false
@@ -4009,7 +4015,7 @@ Style/ExponentialNotation:
4009
4015
  Style/FetchEnvVar:
4010
4016
  Description: >-
4011
4017
  Suggests `ENV.fetch` for the replacement of `ENV[]`.
4012
- Reference:
4018
+ References:
4013
4019
  - https://rubystyle.guide/#hash-fetch-defaults
4014
4020
  Enabled: pending
4015
4021
  VersionAdded: '1.28'
@@ -4050,7 +4056,8 @@ Style/FileWrite:
4050
4056
  Style/FloatDivision:
4051
4057
  Description: 'For performing float division, coerce one side only.'
4052
4058
  StyleGuide: '#float-division'
4053
- Reference: 'https://blog.rubystyle.guide/ruby/2019/06/21/float-division.html'
4059
+ References:
4060
+ - 'https://blog.rubystyle.guide/ruby/2019/06/21/float-division.html'
4054
4061
  Enabled: true
4055
4062
  VersionAdded: '0.72'
4056
4063
  VersionChanged: '1.9'
@@ -4147,7 +4154,8 @@ Style/GlobalStdStream:
4147
4154
  Style/GlobalVars:
4148
4155
  Description: 'Do not introduce global variables.'
4149
4156
  StyleGuide: '#instance-vars'
4150
- Reference: 'https://www.zenspider.com/ruby/quickref.html'
4157
+ References:
4158
+ - 'https://www.zenspider.com/ruby/quickref.html'
4151
4159
  Enabled: true
4152
4160
  VersionAdded: '0.13'
4153
4161
  # Built-in global variables are allowed by default.
@@ -4958,7 +4966,7 @@ Style/OpenStructUse:
4958
4966
  Description: >-
4959
4967
  Avoid using OpenStruct. As of Ruby 3.0, use is officially discouraged due to performance,
4960
4968
  version compatibility, and potential security issues.
4961
- Reference:
4969
+ References:
4962
4970
  - https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats
4963
4971
 
4964
4972
  Enabled: pending
@@ -5203,7 +5211,8 @@ Style/RedundantFetchBlock:
5203
5211
  Description: >-
5204
5212
  Use `fetch(key, value)` instead of `fetch(key) { value }`
5205
5213
  when value has Numeric, Rational, Complex, Symbol or String type, `false`, `true`, `nil` or is a constant.
5206
- Reference: 'https://github.com/fastruby/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
5214
+ References:
5215
+ - 'https://github.com/fastruby/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
5207
5216
  Enabled: true
5208
5217
  Safe: false
5209
5218
  # If enabled, this cop will autocorrect usages of
@@ -5445,7 +5454,8 @@ Style/Sample:
5445
5454
  Description: >-
5446
5455
  Use `sample` instead of `shuffle.first`,
5447
5456
  `shuffle.last`, and `shuffle[Integer]`.
5448
- Reference: 'https://github.com/fastruby/fast-ruby#arrayshufflefirst-vs-arraysample-code'
5457
+ References:
5458
+ - 'https://github.com/fastruby/fast-ruby#arrayshufflefirst-vs-arraysample-code'
5449
5459
  Enabled: true
5450
5460
  VersionAdded: '0.30'
5451
5461
 
@@ -5940,7 +5950,8 @@ Style/YAMLFileRead:
5940
5950
 
5941
5951
  Style/YodaCondition:
5942
5952
  Description: 'Forbid or enforce yoda conditions.'
5943
- Reference: 'https://en.wikipedia.org/wiki/Yoda_conditions'
5953
+ References:
5954
+ - 'https://en.wikipedia.org/wiki/Yoda_conditions'
5944
5955
  Enabled: true
5945
5956
  EnforcedStyle: forbid_for_all_comparison_operators
5946
5957
  SupportedStyles:
@@ -9,16 +9,16 @@ module RuboCop
9
9
 
10
10
  # @api private
11
11
  COMMON_PARAMS = %w[Exclude Include Severity inherit_mode AutoCorrect StyleGuide Details
12
- Enabled Reference].freeze
12
+ Enabled Reference References].freeze
13
13
  # @api private
14
14
  INTERNAL_PARAMS = %w[Description StyleGuide
15
15
  VersionAdded VersionChanged VersionRemoved
16
- Reference Safe SafeAutoCorrect].freeze
16
+ Reference References Safe SafeAutoCorrect].freeze
17
17
  # @api private
18
18
  NEW_COPS_VALUES = %w[pending disable enable].freeze
19
19
 
20
20
  # @api private
21
- CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect].to_set.freeze
21
+ CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect References].to_set.freeze
22
22
  CONFIG_CHECK_DEPARTMENTS = %w[pending override_department].freeze
23
23
  CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
24
24
  private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
@@ -260,8 +260,7 @@ module RuboCop
260
260
  end
261
261
  end
262
262
 
263
- # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
264
- def check_cop_config_value(hash, parent = nil)
263
+ def check_cop_config_value(hash, parent = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
265
264
  hash.each do |key, value|
266
265
  check_cop_config_value(value, key) if value.is_a?(Hash)
267
266
 
@@ -271,6 +270,8 @@ module RuboCop
271
270
  supposed_values = 'a boolean'
272
271
  elsif key == 'AutoCorrect' && !CONFIG_CHECK_AUTOCORRECTS.include?(value)
273
272
  supposed_values = '`always`, `contextual`, `disabled`, or a boolean'
273
+ elsif key == 'References'
274
+ supposed_values = 'an array of strings'
274
275
  else
275
276
  next
276
277
  end
@@ -278,7 +279,6 @@ module RuboCop
278
279
  raise ValidationError, param_error_message(parent, key, value, supposed_values)
279
280
  end
280
281
  end
281
- # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
282
282
 
283
283
  # FIXME: Handling colors in exception messages like this is ugly.
284
284
  def param_error_message(parent, key, value, supposed_values)
@@ -69,7 +69,7 @@ module RuboCop
69
69
  # @!method offense_example(node)
70
70
  def_node_matcher :offense_example, <<~PATTERN
71
71
  (block
72
- (send _ {:it :specify} $...)
72
+ (send _ {:it :specify :xit :fit} $...)
73
73
  _args
74
74
  `(send nil? %RESTRICT_ON_SEND ...)
75
75
  )
@@ -28,6 +28,7 @@ module RuboCop
28
28
  RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].freeze
29
29
  NODE_GROUPS = {
30
30
  any_block: %i[block numblock itblock],
31
+ any_def: %i[def defs],
31
32
  argument: %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg forward_arg shadowarg],
32
33
  boolean: %i[true false],
33
34
  call: %i[send csend],
@@ -10,7 +10,12 @@ module RuboCop
10
10
  extend FileFinder
11
11
 
12
12
  ALLOWED_CONFIGURATIONS = %w[
13
- Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude
13
+ Safe SafeAutoCorrect AutoCorrect
14
+ Severity
15
+ StyleGuide
16
+ Details
17
+ Reference References
18
+ Include Exclude
14
19
  ].freeze
15
20
  RESTRICT_ON_SEND = %i[[] fetch].freeze
16
21
  MSG = '`%<name>s` is not defined in the configuration for `%<cop>s` ' \
@@ -73,8 +73,7 @@ module RuboCop
73
73
  # @!method block_end_align_target?(node, child)
74
74
  def_node_matcher :block_end_align_target?, <<~PATTERN
75
75
  {assignment?
76
- def
77
- defs
76
+ any_def
78
77
  splat
79
78
  and
80
79
  or
@@ -48,7 +48,7 @@ module RuboCop
48
48
  def on_send(node)
49
49
  return unless node.def_modifier?
50
50
 
51
- method_def = node.each_descendant(:def, :defs).first
51
+ method_def = node.each_descendant(:any_def).first
52
52
  expr = node.source_range
53
53
 
54
54
  line_start = range_between(expr.begin_pos, method_def.loc.keyword.end_pos)
@@ -131,7 +131,7 @@ module RuboCop
131
131
 
132
132
  def next_sibling_parent_empty_or_else?(node)
133
133
  next_sibling = node.right_sibling
134
- return true if next_sibling.nil?
134
+ return true unless next_sibling.is_a?(AST::Node)
135
135
 
136
136
  parent = next_sibling.parent
137
137
 
@@ -180,7 +180,7 @@ module RuboCop
180
180
  end
181
181
 
182
182
  def method_candidate?(node)
183
- cop_config['EmptyLineBetweenMethodDefs'] && node.type?(:def, :defs)
183
+ cop_config['EmptyLineBetweenMethodDefs'] && node.any_def_type?
184
184
  end
185
185
 
186
186
  def class_candidate?(node)
@@ -252,7 +252,7 @@ module RuboCop
252
252
  end
253
253
 
254
254
  def end_loc(node)
255
- if node.type?(:def, :defs) && node.endless?
255
+ if node.any_def_type? && node.endless?
256
256
  node.source_range.end
257
257
  else
258
258
  node.loc.end
@@ -19,7 +19,7 @@ module RuboCop
19
19
  # * ignore_implicit (without curly braces)
20
20
  #
21
21
  # Alternatively you can specify multiple allowed styles. That's done by
22
- # passing a list of styles to EnforcedStyles.
22
+ # passing a list of styles to EnforcedHashRocketStyle and EnforcedColonStyle.
23
23
  #
24
24
  # @example EnforcedHashRocketStyle: key (default)
25
25
  # # bad
@@ -250,7 +250,7 @@ module RuboCop
250
250
  reset!
251
251
 
252
252
  alignment_for(first_pair).each do |alignment|
253
- delta = alignment.deltas_for_first_pair(first_pair, node)
253
+ delta = alignment.deltas_for_first_pair(first_pair)
254
254
  check_delta delta, node: first_pair, alignment: alignment
255
255
  end
256
256
 
@@ -58,6 +58,12 @@ module RuboCop
58
58
  # attr_reader :name #: String
59
59
  # attr_reader :age #: Integer?
60
60
  #
61
+ # #: (
62
+ # #| Integer,
63
+ # #| String
64
+ # #| ) -> void
65
+ # def foo; end
66
+ #
61
67
  # @example AllowRBSInlineAnnotation: true
62
68
  #
63
69
  # # good
@@ -67,6 +73,12 @@ module RuboCop
67
73
  # attr_reader :name #: String
68
74
  # attr_reader :age #: Integer?
69
75
  #
76
+ # #: (
77
+ # #| Integer,
78
+ # #| String
79
+ # #| ) -> void
80
+ # def foo; end
81
+ #
70
82
  # @example AllowSteepAnnotation: false (default)
71
83
  #
72
84
  # # bad
@@ -175,7 +187,7 @@ module RuboCop
175
187
  end
176
188
 
177
189
  def rbs_inline_annotation?(comment)
178
- allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]/)
190
+ allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]|#\|/)
179
191
  end
180
192
 
181
193
  def allow_steep_annotation?
@@ -118,7 +118,7 @@ module RuboCop
118
118
  end
119
119
 
120
120
  def right_hand_side(send_node)
121
- send_node.first_argument.source_range
121
+ send_node.first_argument&.source_range
122
122
  end
123
123
  end
124
124
  end
@@ -29,8 +29,7 @@ module RuboCop
29
29
  MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
30
30
  'aligned with `%<beginning>s` at ' \
31
31
  '%<begin_loc_line>d, %<begin_loc_column>d.'
32
- ANCESTOR_TYPES = %i[kwbegin def defs class module any_block].freeze
33
- ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
32
+ ANCESTOR_TYPES = %i[kwbegin any_def class module any_block].freeze
34
33
  ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method private_class_method].freeze
35
34
 
36
35
  def on_resbody(node)
@@ -162,8 +161,7 @@ module RuboCop
162
161
  end
163
162
 
164
163
  def access_modifier_node(node)
165
- return nil unless
166
- ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type)
164
+ return nil unless node.any_def_type?
167
165
 
168
166
  access_modifier_node = node.ancestors.first
169
167
  return nil unless access_modifier?(access_modifier_node)
@@ -23,6 +23,16 @@ module RuboCop
23
23
  def kind(token)
24
24
  'semicolon' if token.semicolon?
25
25
  end
26
+
27
+ def space_missing?(token1, token2)
28
+ super && !semicolon_sequence?(token1, token2)
29
+ end
30
+
31
+ private
32
+
33
+ def semicolon_sequence?(token, next_token)
34
+ token.semicolon? && next_token.semicolon?
35
+ end
26
36
  end
27
37
  end
28
38
  end
@@ -6,6 +6,8 @@ module RuboCop
6
6
  # Checks that brackets used for array literals have or don't have
7
7
  # surrounding space depending on configuration.
8
8
  #
9
+ # Array pattern matching is handled in the same way.
10
+ #
9
11
  # @example EnforcedStyle: no_space (default)
10
12
  # # The `no_space` style enforces that array literals have
11
13
  # # no surrounding space.
@@ -82,7 +84,7 @@ module RuboCop
82
84
  EMPTY_MSG = '%<command>s space inside empty array brackets.'
83
85
 
84
86
  def on_array(node)
85
- return unless node.square_brackets?
87
+ return if node.array_type? && !node.square_brackets?
86
88
 
87
89
  tokens, left, right = array_brackets(node)
88
90
 
@@ -95,6 +97,7 @@ module RuboCop
95
97
 
96
98
  issue_offenses(node, left, right, start_ok, end_ok)
97
99
  end
100
+ alias on_array_pattern on_array
98
101
 
99
102
  private
100
103
 
@@ -6,6 +6,8 @@ module RuboCop
6
6
  # Checks that braces used for hash literals have or don't have
7
7
  # surrounding space depending on configuration.
8
8
  #
9
+ # Hash pattern matching is handled in the same way.
10
+ #
9
11
  # @example EnforcedStyle: space (default)
10
12
  # # The `space` style enforces that hash literals have
11
13
  # # surrounding space.
@@ -87,6 +89,7 @@ module RuboCop
87
89
  check(tokens[-2], tokens[-1]) if tokens.size > 2
88
90
  check_whitespace_only_hash(node) if enforce_no_space_style_for_empty_braces?
89
91
  end
92
+ alias on_hash_pattern on_hash
90
93
 
91
94
  private
92
95
 
@@ -51,10 +51,9 @@ module RuboCop
51
51
  'in a regexp.'
52
52
 
53
53
  def on_interpolation(begin_node)
54
- final_node = begin_node.children.last
55
-
56
- return unless begin_node.parent.regexp_type?
54
+ return unless (final_node = begin_node.children.last)
57
55
  return unless final_node.array_type?
56
+ return unless begin_node.parent.regexp_type?
58
57
 
59
58
  if array_of_literal_values?(final_node)
60
59
  register_array_of_literal_values(begin_node, final_node)
@@ -48,7 +48,7 @@ module RuboCop
48
48
  def autocorrect(corrector, node)
49
49
  boolean_literal = node.source.delete(':')
50
50
  parent = node.parent
51
- if parent&.pair_type? && node.equal?(parent.children[0])
51
+ if parent&.pair_type? && parent.colon? && node.equal?(parent.children[0])
52
52
  corrector.remove(parent.loc.operator)
53
53
  boolean_literal = "#{node.source} =>"
54
54
  end
@@ -6,9 +6,8 @@ module RuboCop
6
6
  # Checks for circular argument references in optional keyword
7
7
  # arguments and optional ordinal arguments.
8
8
  #
9
- # This cop mirrors a warning produced by MRI since 2.2.
10
- #
11
- # NOTE: This syntax is no longer valid on Ruby 2.7 or higher.
9
+ # NOTE: This syntax was made invalid on Ruby 2.7 - Ruby 3.3 but is allowed
10
+ # again since Ruby 3.4.
12
11
  #
13
12
  # @example
14
13
  #
@@ -41,8 +40,6 @@ module RuboCop
41
40
 
42
41
  MSG = 'Circular argument reference - `%<arg_name>s`.'
43
42
 
44
- maximum_target_ruby_version 2.6
45
-
46
43
  def on_kwoptarg(node)
47
44
  check_for_circular_argument_references(*node)
48
45
  end
@@ -134,7 +134,7 @@ module RuboCop
134
134
  if NO_ARG_ALGORITHM.include?(algorithm_parts.first.upcase) && no_arguments
135
135
  "'#{algorithm_parts.first}'"
136
136
  else
137
- mode = 'cbc' unless size_and_mode == ['cbc']
137
+ mode = 'cbc' if size_and_mode.empty?
138
138
 
139
139
  "'#{(algorithm_parts + size_and_mode + [mode]).compact.take(3).join('-')}'"
140
140
  end
@@ -42,7 +42,6 @@ module RuboCop
42
42
  class DuplicateMethods < Base
43
43
  MSG = 'Method `%<method>s` is defined at both %<defined>s and %<current>s.'
44
44
  RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer attr_accessor attr].freeze
45
- DEF_TYPES = %i[def defs].freeze
46
45
 
47
46
  def initialize(config = nil, options = nil)
48
47
  super
@@ -162,7 +161,7 @@ module RuboCop
162
161
  end
163
162
 
164
163
  def method_key(node, method_name)
165
- if (ancestor_def = node.each_ancestor(*DEF_TYPES).first)
164
+ if (ancestor_def = node.each_ancestor(:any_def).first)
166
165
  "#{ancestor_def.method_name}.#{method_name}"
167
166
  else
168
167
  method_name
@@ -170,7 +169,7 @@ module RuboCop
170
169
  end
171
170
 
172
171
  def location(node)
173
- if DEF_TYPES.include?(node.type)
172
+ if node.any_def_type?
174
173
  node.loc.keyword.join(node.loc.name)
175
174
  else
176
175
  node.source_range
@@ -57,13 +57,9 @@ module RuboCop
57
57
  def on_if(node)
58
58
  cond = condition(node)
59
59
 
60
- if node.unless?
61
- correct_if_node(node, cond, true) if cond.falsey_literal?
62
- correct_if_node(node, cond, false) if cond.truthy_literal?
63
- else
64
- correct_if_node(node, cond, true) if cond.truthy_literal?
65
- correct_if_node(node, cond, false) if cond.falsey_literal?
66
- end
60
+ return unless cond.falsey_literal? || cond.truthy_literal?
61
+
62
+ correct_if_node(node, cond)
67
63
  end
68
64
 
69
65
  def on_while(node)
@@ -232,9 +228,27 @@ module RuboCop
232
228
  )
233
229
  end
234
230
 
235
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
236
- def correct_if_node(node, cond, result)
237
- if result
231
+ def condition_evaluation(node, cond)
232
+ if node.unless?
233
+ cond.falsey_literal?
234
+ else
235
+ cond.truthy_literal?
236
+ end
237
+ end
238
+
239
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
240
+ def correct_if_node(node, cond)
241
+ result = condition_evaluation(node, cond)
242
+
243
+ if node.elsif? && result
244
+ add_offense(cond) do |corrector|
245
+ corrector.replace(node, "else\n #{node.if_branch.source}")
246
+ end
247
+ elsif node.elsif? && !result
248
+ add_offense(cond) do |corrector|
249
+ corrector.replace(node, "else\n #{node.else_branch.source}")
250
+ end
251
+ elsif node.if_branch && result
238
252
  add_offense(cond) do |corrector|
239
253
  corrector.replace(node, node.if_branch.source)
240
254
  end
@@ -252,7 +266,7 @@ module RuboCop
252
266
  end
253
267
  end
254
268
  end
255
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
269
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
256
270
  end
257
271
  end
258
272
  end
@@ -52,7 +52,7 @@ module RuboCop
52
52
  each_missing_enable do |cop, line_range|
53
53
  next if acceptable_range?(cop, line_range)
54
54
 
55
- range = source_range(processed_source.buffer, line_range.min, (0..0))
55
+ range = source_range(processed_source.buffer, line_range.min, 0..0)
56
56
  comment = processed_source.comment_at_line(line_range.begin)
57
57
 
58
58
  add_offense(range, message: message(cop, comment))
@@ -98,7 +98,7 @@ module RuboCop
98
98
  subject, = *node # rubocop:disable InternalAffairs/NodeDestructuring
99
99
  return if node.defs_type? && allowed_subject_type?(subject)
100
100
 
101
- def_ancestor = node.each_ancestor(:def, :defs).first
101
+ def_ancestor = node.each_ancestor(:any_def).first
102
102
  return unless def_ancestor
103
103
 
104
104
  within_scoping_def =
@@ -46,7 +46,7 @@ module RuboCop
46
46
  def on_return(return_node)
47
47
  return if return_value?(return_node)
48
48
 
49
- return_node.each_ancestor(:any_block, :def, :defs) do |node|
49
+ return_node.each_ancestor(:any_block, :any_def) do |node|
50
50
  break if scoped_node?(node)
51
51
 
52
52
  # if a proc is passed to `Module#define_method` or
@@ -66,7 +66,7 @@ module RuboCop
66
66
  private
67
67
 
68
68
  def scoped_node?(node)
69
- node.type?(:def, :defs) || node.lambda?
69
+ node.any_def_type? || node.lambda?
70
70
  end
71
71
 
72
72
  def return_value?(return_node)
@@ -30,7 +30,7 @@ module RuboCop
30
30
  return unless node.lhs&.casgn_type?
31
31
 
32
32
  add_offense(node.loc.operator) do |corrector|
33
- next if node.each_ancestor(:def, :defs).any?
33
+ next if node.each_ancestor(:any_def).any?
34
34
 
35
35
  corrector.replace(node.loc.operator, '=')
36
36
  end