Skip to content

Commit

Permalink
Add new Style/NumberedParameters cop
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Hache authored and bbatsov committed Sep 27, 2021
1 parent d372598 commit 8b5d234
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/new_add_new_stylenumberedparameters_cop.md
@@ -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
9 changes: 9 additions & 0 deletions config/default.yml
Expand Up @@ -4135,6 +4135,15 @@ Style/Not:
VersionAdded: '0.9'
VersionChanged: '0.20'

Style/NumberedParameters:
Description: 'Restrict the usage of numbered parameters.'
Enabled: pending
VersionAdded: '<<next>>'
EnforcedStyle: allow_single_line
SupportedStyles:
- allow_single_line
- disallow

Style/NumberedParametersLimit:
Description: 'Avoid excessive numbered params in a single block.'
Enabled: pending
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -513,6 +513,7 @@
require_relative 'rubocop/cop/style/method_call_without_args_parentheses'
require_relative 'rubocop/cop/style/method_call_with_args_parentheses'
require_relative 'rubocop/cop/style/multiline_in_pattern_then'
require_relative 'rubocop/cop/style/numbered_parameters'
require_relative 'rubocop/cop/style/redundant_assignment'
require_relative 'rubocop/cop/style/redundant_fetch_block'
require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
Expand Down
46 changes: 46 additions & 0 deletions lib/rubocop/cop/style/numbered_parameters.rb
@@ -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
35 changes: 35 additions & 0 deletions spec/rubocop/cop/style/numbered_parameters_spec.rb
@@ -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

0 comments on commit 8b5d234

Please sign in to comment.