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/OptionalBooleanParameter
cop
- Loading branch information
Showing
16 changed files
with
159 additions
and
16 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
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
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
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for places where keyword arguments can be used instead of | ||
# boolean arguments when defining methods. | ||
# | ||
# @example | ||
# # bad | ||
# def some_method(bar = false) | ||
# puts bar | ||
# end | ||
# | ||
# # bad - common hack before keyword args were introduced | ||
# def some_method(options = {}) | ||
# bar = options.fetch(:bar, false) | ||
# puts bar | ||
# end | ||
# | ||
# # good | ||
# def some_method(bar: false) | ||
# puts bar | ||
# end | ||
# | ||
class OptionalBooleanParameter < Base | ||
MSG = 'Use keyword arguments when defining method with boolean argument.' | ||
BOOLEAN_TYPES = %i[true false].freeze | ||
|
||
def on_def(node) | ||
node.arguments.each do |arg| | ||
next unless arg.optarg_type? | ||
|
||
_name, value = *arg | ||
add_offense(arg) if BOOLEAN_TYPES.include?(value.type) | ||
end | ||
end | ||
alias on_defs on_def | ||
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
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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::OptionalBooleanParameter do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when defining method with optional boolean arg' do | ||
expect_offense(<<~RUBY) | ||
def some_method(bar = false) | ||
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when defining class method with optional boolean arg' do | ||
expect_offense(<<~RUBY) | ||
def self.some_method(bar = false) | ||
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when defining method with multiple optional boolean args' do | ||
expect_offense(<<~RUBY) | ||
def some_method(foo = true, bar = 1, baz = false, quux: true) | ||
^^^^^^^^^^ Use keyword arguments when defining method with boolean argument. | ||
^^^^^^^^^^^ Use keyword arguments when defining method with boolean argument. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when defining method with keyword boolean arg' do | ||
expect_no_offenses(<<~RUBY) | ||
def some_method(bar: false) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when defining method without args' do | ||
expect_no_offenses(<<~RUBY) | ||
def some_method | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when defining method with optonal non-boolean arg' do | ||
expect_no_offenses(<<~RUBY) | ||
def some_method(bar = 'foo') | ||
end | ||
RUBY | ||
end | ||
end |