Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Style/NumberedParameters cop
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* [#10100](https://github.com/rubocop/rubocop/pull/10100): Add new Style/NumberedParameters cop. ([@Hugo-Hache][]) | ||
|
||
[@Hugo-Hache]: https://github.com/Hugo-Hache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for numbered parameters. | ||
# | ||
# It can either restrict the use of numbered parameters to | ||
# single-lined blocks, or disallow completely numbered parameters. | ||
# | ||
# @example EnforcedStyle: allow_single_line (default) | ||
# # bad | ||
# collection.each do | ||
# puts _1 | ||
# end | ||
# | ||
# # good | ||
# collection.each { puts _1 } | ||
# | ||
# @example EnforcedStyle: disallow | ||
# # bad | ||
# collection.each { puts _1 } | ||
# | ||
# # good | ||
# collection.each { |item| puts item } | ||
# | ||
class NumberedParameters < Base | ||
include ConfigurableEnforcedStyle | ||
extend TargetRubyVersion | ||
|
||
MSG_DISALLOW = 'Avoid using numbered parameters.' | ||
MSG_MULTI_LINE = 'Avoid using numbered parameters for multi-line blocks.' | ||
|
||
minimum_target_ruby_version 2.7 | ||
|
||
def on_numblock(node) | ||
if style == :disallow | ||
add_offense(node, message: MSG_DISALLOW) | ||
elsif node.multiline? | ||
add_offense(node, message: MSG_MULTI_LINE) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::NumberedParameters, :config do | ||
context '>= Ruby 2.7', :ruby27 do | ||
context 'EnforcedStyle: allow_single_line' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'allow_single_line' } } | ||
|
||
it 'registers an offense when using numbered parameters with multi-line blocks' do | ||
expect_offense(<<~RUBY) | ||
collection.each do | ||
^^^^^^^^^^^^^^^^^^ Avoid using numbered parameters for multi-line blocks. | ||
puts _1 | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using numbered parameters with single-line blocks' do | ||
expect_no_offenses(<<~RUBY) | ||
collection.each { puts _1 } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'EnforcedStyle: disallow' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'disallow' } } | ||
|
||
it 'does an offense when using numbered parameters even with single-line blocks' do | ||
expect_offense(<<~RUBY) | ||
collection.each { puts _1 } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using numbered parameters. | ||
RUBY | ||
end | ||
end | ||
end | ||
end |