Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Style/NumberedParameters cop #10100

Merged
merged 1 commit into from Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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