Skip to content

Commit 84f3d43

Browse files
Earlopainbbatsov
authored andcommitted
Specify maximum_target_ruby_version for a handful of cops, document it
Followup to #13473 * `Lint/CircularArgumentReference`: Invalid syntax on modern rubies * `Lint/NonDeterministicRequireOrder`: Skipped in node handlers * `Lint/UselessElseWithoutRescue`: Invalid syntax on modern rubies * `Security/YAMLLoad`: Skipped in node handler I left `Lint/RefinementImportMethods` alone and just added the removal timeframe in the docs. Probably it could get an upper bound but docs currently don't handle both min/max at the same time.
1 parent ef8009c commit 84f3d43

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

docs/modules/ROOT/pages/development.adoc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ Some cops apply changes that only apply in particular contexts, such as if the u
378378

379379
==== Requiring a minimum Ruby version
380380

381-
If your cop uses new Ruby syntax or standard library APIs, it should only autocorrect if the user has the target Ruby version, which you set with https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/TargetRubyVersion#minimum_target_ruby_version-instance_metho[`TargetRubyVersion#minimum_target_ruby_version`].
381+
If your cop uses new Ruby syntax or standard library APIs, it should only register offenses if the user has the proper target Ruby version, which you can require with https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/TargetRubyVersion#minimum_target_ruby_version-instance_method[`TargetRubyVersion#minimum_target_ruby_version`].
382382

383383
For example, the `Performance/SelectMap` cop requires Ruby 2.7, which introduced `Enumerable#filter_map`:
384384

385385
```ruby
386-
module RuboCop::Cop::Performance::SelectMap < Base
386+
class RuboCop::Cop::Performance::SelectMap < Base
387387
extend TargetRubyVersion
388388

389389
minimum_target_ruby_version 2.7
@@ -392,7 +392,23 @@ module RuboCop::Cop::Performance::SelectMap < Base
392392
end
393393
```
394394

395-
This cop won't autocorrect on Ruby 2.6 or older, and it won't even report an offense (since there's no better alternative to recommend instead).
395+
This cop won't register offenses on Ruby 2.6 or older.
396+
397+
==== Requiring a maximum Ruby version
398+
399+
Mirroring `minimum_target_ruby_version`, you can also specify a maximum Ruby version your cop should analyze.
400+
401+
For example, the `Lint/CircularArgumentReference` cop only runs when analyzing code for Ruby before 2.7. The code it is looking for can never be written in more recent Rubies, it would be a syntax error:
402+
403+
```ruby
404+
class RuboCop::Cop::Lint::CircularArgumentReference < Base
405+
extend TargetRubyVersion
406+
407+
maximum_target_ruby_version 2.6
408+
409+
# ...
410+
end
411+
```
396412

397413
==== Requiring a gem
398414

lib/rubocop/cop/lint/circular_argument_reference.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ module Lint
3737
# dry_ingredients.combine
3838
# end
3939
class CircularArgumentReference < Base
40+
extend TargetRubyVersion
41+
4042
MSG = 'Circular argument reference - `%<arg_name>s`.'
4143

44+
maximum_target_ruby_version 2.6
45+
4246
def on_kwoptarg(node)
4347
check_for_circular_argument_references(*node)
4448
end

lib/rubocop/cop/lint/non_deterministic_require_order.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ module Lint
5959
#
6060
class NonDeterministicRequireOrder < Base
6161
extend AutoCorrector
62+
extend TargetRubyVersion
6263

6364
MSG = 'Sort files before requiring them.'
6465

66+
maximum_target_ruby_version 2.7
67+
6568
def on_block(node)
66-
return if target_ruby_version >= 3.0
6769
return unless node.body
6870
return unless unsorted_dir_loop?(node.send_node)
6971

@@ -75,7 +77,6 @@ def on_block(node)
7577
end
7678

7779
def on_numblock(node)
78-
return if target_ruby_version >= 3.0
7980
return unless node.body
8081
return unless unsorted_dir_loop?(node.send_node)
8182

@@ -87,7 +88,6 @@ def on_numblock(node)
8788
end
8889

8990
def on_block_pass(node)
90-
return if target_ruby_version >= 3.0
9191
return unless method_require?(node)
9292
return unless unsorted_dir_pass?(node.parent)
9393

lib/rubocop/cop/lint/refinement_import_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Lint
66
# Checks if `include` or `prepend` is called in `refine` block.
77
# These methods are deprecated and should be replaced with `Refinement#import_methods`.
88
#
9-
# It emulates deprecation warnings in Ruby 3.1.
9+
# It emulates deprecation warnings in Ruby 3.1. Functionality has been removed in Ruby 3.2.
1010
#
1111
# @safety
1212
# This cop's autocorrection is unsafe because `include M` will affect the included class

lib/rubocop/cop/lint/useless_else_without_rescue.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ module Lint
2525
# do_something_else
2626
# end
2727
class UselessElseWithoutRescue < Base
28+
extend TargetRubyVersion
29+
2830
MSG = '`else` without `rescue` is useless.'
2931

32+
maximum_target_ruby_version 2.5
33+
3034
def on_new_investigation
3135
processed_source.diagnostics.each do |diagnostic|
3236
next unless diagnostic.reason == :useless_else

lib/rubocop/cop/security/yaml_load.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ module Security
2525
#
2626
class YAMLLoad < Base
2727
extend AutoCorrector
28+
extend TargetRubyVersion
2829

2930
MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'
3031
RESTRICT_ON_SEND = %i[load].freeze
3132

33+
maximum_target_ruby_version 3.0
34+
3235
# @!method yaml_load(node)
3336
def_node_matcher :yaml_load, <<~PATTERN
3437
(send (const {nil? cbase} :YAML) :load ...)
3538
PATTERN
3639

3740
def on_send(node)
38-
return if target_ruby_version >= 3.1
39-
4041
yaml_load(node) do
4142
add_offense(node.loc.selector) do |corrector|
4243
corrector.replace(node.loc.selector, 'safe_load')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy