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 options hash cop #2078

Merged
merged 1 commit into from
Jul 31, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#2060](https://github.com/bbatsov/rubocop/issues/2060): New cop `Style/RescueEnsureAlignment` checks for bad alignment of `rescue` and `ensure` keywords. ([@lumeet][])
* New cop `Style/OptionalArguments` checks for optional arguments that do not appear at the end of an argument list. ([@rrosenblum][])
* New cop `Lint/CircularArgumentReference` checks for "circular argument references" in keyword arguments, which Ruby 2.2 warns against. ([@maxjacobson][], [@sliuu][])
* [#2030](https://github.com/bbatsov/rubocop/issues/2030): New cop `Lint/OptionHash` checks for option hashes and encourages changing them to keyword arguments (disabled by default). ([@maxjacobson][])

### Changes

Expand Down
4 changes: 4 additions & 0 deletions config/disabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Style/MissingElse:
- case
- both

Style/OptionHash:
Description: "Don't use option hashes when you can use keyword arguments."
Enabled: false

Style/SymbolArray:
Description: 'Use %i or %I for arrays of symbols.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-i'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
require 'rubocop/cop/style/one_line_conditional'
require 'rubocop/cop/style/op_method'
require 'rubocop/cop/style/optional_arguments'
require 'rubocop/cop/style/option_hash'
require 'rubocop/cop/style/parallel_assignment'
require 'rubocop/cop/style/parentheses_around_condition'
require 'rubocop/cop/style/percent_literal_delimiters'
Expand Down
56 changes: 56 additions & 0 deletions lib/rubocop/cop/style/option_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# encoding: utf-8

module RuboCop
module Cop
module Style
# This cop checks for options hashes and discourages them if the
# current Ruby version supports keyword arguments.
#
# @example
# Instead of:
#
# def fry(options = {})
# temperature = options.fetch(:temperature, 300)
# ...
# end
#
# Prefer:
#
# def fry(temperature: 300)
# ...
# end
class OptionHash < Cop
MSG = 'Prefer keyword arguments to options hashes.'

def on_args(node)
return unless supports_keyword_arguments?

*_but_last, last_arg = *node

# asserting that there was an argument at all
return unless last_arg

# asserting last argument is an optional argument
return unless last_arg.optarg_type?

_, default_value = *last_arg

# asserting default value is a hash
return unless default_value.hash_type?

# asserting default value is empty hash
*key_value_pairs = *default_value
return unless key_value_pairs.empty?

add_offense(last_arg, :expression, MSG)
end

private

def supports_keyword_arguments?
RUBY_VERSION >= '2.0.0'
end
end
end
end
end
80 changes: 80 additions & 0 deletions spec/rubocop/cop/style/option_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# encoding: utf-8

require 'spec_helper'

describe RuboCop::Cop::Style::OptionHash do
subject(:cop) { described_class.new }
before(:each) do
inspect_source(cop, source)
end

let(:source) do
[
'def some_method(options = {})',
' puts some_arg',
'end'
]
end

context 'ruby < 2.0, which has no keyword arguments', ruby_less_than: 2.0 do
it 'does not register an offense' do
expect(cop.offenses).to be_empty
end
end

context 'ruby >= 2.0', ruby_greater_than_or_equal: 2.0 do
it 'registers an offense' do
expect(cop.offenses.size).to eq(1)
expect(cop.messages.first)
.to eq('Prefer keyword arguments to options hashes.')
expect(cop.highlights).to eq ['options = {}']
end

context 'when the last argument is an options hash named something else' do
let(:source) do
[
'def steep(flavor, duration, config={})',
' mug = config.fetch(:mug)',
' prep(flavor, duration, mug)',
'end'
]
end

it 'registers an offense' do
expect(cop.offenses.size).to eq(1)
expect(cop.messages.first)
.to eq('Prefer keyword arguments to options hashes.')
expect(cop.highlights).to eq ['config={}']
end
end

context 'when there are no arguments' do
let(:source) do
[
'def meditate',
' puts true',
' puts true',
'end'
]
end

it 'does not register an offense' do
expect(cop.offenses).to be_empty
end
end

context 'when the last argument is a non-options-hash optional hash' do
let(:source) do
[
'def cook(instructions, ingredients = { hot: [], cold: [] })',
' prep(ingredients)',
'end'
]
end

it 'does not register an offense' do
expect(cop.offenses).to be_empty
end
end
end
end