-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This cop checks that `Hash#each_key`/`Hash#each_value` is used instead of `Hash#keys.each`/`Hash#values.each`/`Hash#each`. See https://github.com/bbatsov/ruby-style-guide#hash-each
- Loading branch information
Showing
6 changed files
with
172 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# encoding: utf-8 | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop checks for uses of `each_key` & `each_value` Hash methods. | ||
# | ||
# @example | ||
# # bad | ||
# hash.keys.each { |k| p k } | ||
# hash.values.each { |v| p v } | ||
# hash.each { |k, _v| p k } | ||
# hash.each { |_k, v| p v } | ||
# | ||
# # good | ||
# hash.each_key { |k| p k } | ||
# hash.each_value { |v| p v } | ||
class HashEachMethods < Cop | ||
include Lint::UnusedArgument | ||
|
||
MSG = 'Use `%s` instead of `%s`.'.freeze | ||
|
||
def_node_matcher :plain_each, <<-END | ||
(block $(send (send _ :hash) :each) (args (arg $_k) (arg $_v)) ...) | ||
END | ||
|
||
def_node_matcher :kv_each, <<-END | ||
(block $(send (send (send _ :hash) ${:keys :values}) :each) ...) | ||
END | ||
|
||
def on_block(node) | ||
plain_each(node) do |target, k, v| | ||
return if @args[k] && @args[v] | ||
used = @args[k] ? :key : :value | ||
add_offense(target, range(target), format(message, | ||
"each_#{used}", | ||
:each)) | ||
end | ||
kv_each(node) do |target, method| | ||
add_offense(target, range(target), format(message, | ||
"each_#{method[0..-2]}", | ||
"#{method}.each")) | ||
end | ||
end | ||
|
||
def check_argument(variable) | ||
return unless variable.block_argument? | ||
(@args ||= {})[variable.name] = variable.used? | ||
end | ||
|
||
def autocorrect(node) | ||
receiver, _second_method = *node | ||
caller, first_method = *receiver | ||
lambda do |corrector| | ||
if first_method == :hash | ||
method = @args.values.first ? :key : :value | ||
new_source = receiver.source + ".each_#{method}" | ||
corrector.replace(node.loc.expression, new_source) | ||
correct_args(node, corrector) | ||
else | ||
new_source = caller.source + ".each_#{first_method[0..-2]}" | ||
corrector.replace(node.loc.expression, new_source) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def correct_args(node, corrector) | ||
args = node.parent.children[1] | ||
used_arg = "|#{@args.detect { |_k, v| v }.first}|" | ||
args_range = Parser::Source::Range.new(node.parent.source, | ||
args.loc.begin.begin_pos, | ||
args.loc.end.end_pos) | ||
corrector.replace(args_range, used_arg) | ||
end | ||
|
||
def range(outer_node) | ||
inner_node = outer_node.children.first | ||
inner_node.loc.selector.join(outer_node.loc.selector) | ||
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,76 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Performance::HashEachMethods do | ||
subject(:cop) { described_class.new } | ||
let(:hash) { { key: 'value' } } | ||
|
||
it 'registers an offense for Hash#keys.each' do | ||
inspect_source(cop, 'hash.keys.each { |k| p k }') | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages) | ||
.to eq(['Use `each_key` instead of `keys.each`.']) | ||
end | ||
|
||
it 'registers an offense for Hash#values.each' do | ||
inspect_source(cop, 'hash.values.each { |v| p v }') | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages) | ||
.to eq(['Use `each_value` instead of `values.each`.']) | ||
end | ||
|
||
it 'registers an offense for Hash#each with unused value' do | ||
inspect_source(cop, 'hash.each { |k, _v| p k }') | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages) | ||
.to eq(['Use `each_key` instead of `each`.']) | ||
end | ||
|
||
it 'registers an offense for Hash#each with unused key' do | ||
inspect_source(cop, 'hash.each { |_k, v| p v }') | ||
expect(cop.offenses.size).to eq(1) | ||
expect(cop.messages) | ||
.to eq(['Use `each_value` instead of `each`.']) | ||
end | ||
|
||
it 'does not register an offense for Hash#each_key' do | ||
inspect_source(cop, 'hash.each_key { |k| p k }') | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'does not register an offense for Hash#each_value' do | ||
inspect_source(cop, 'hash.each_value { |v| p v }') | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'does not register an offense for Hash#each if both key/value are used' do | ||
inspect_source(cop, 'hash.each { |k, v| p "#{k}_#{v}" }') | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'does not register an offense for Hash#each if block takes only one arg' do | ||
inspect_source(cop, 'hash.each { |kv| p kv }') | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'auto-corrects Hash#keys.each with Hash#each_key' do | ||
new_source = autocorrect_source(cop, 'hash.keys.each { |k| p k }') | ||
expect(new_source).to eq('hash.each_key { |k| p k }') | ||
end | ||
|
||
it 'auto-corrects Hash#values.each with Hash#each_value' do | ||
new_source = autocorrect_source(cop, 'hash.values.each { |v| p v }') | ||
expect(new_source).to eq('hash.each_value { |v| p v }') | ||
end | ||
|
||
it 'auto-corrects Hash#each with unused value argument with Hash#each_key' do | ||
new_source = autocorrect_source(cop, 'hash.each { |k, _v| p k }') | ||
expect(new_source).to eq('hash.each_key { |k| p k }') | ||
end | ||
|
||
it 'auto-corrects Hash#each with unused key argument with Hash#each_value' do | ||
new_source = autocorrect_source(cop, 'hash.each { |_k, v| p v }') | ||
expect(new_source).to eq('hash.each_value { |v| p v }') | ||
end | ||
end |