-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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 for potential uses of `Enumberable#minmax`, e.g.: ``` [foo.min, foo.max] ``` can be changed to: ``` foo.minmax ```
- Loading branch information
Showing
7 changed files
with
196 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for potential uses of `Enumerable#minmax`. | ||
# | ||
# @example | ||
# | ||
# @bad | ||
# bar = [foo.min, foo.max] | ||
# return foo.min, foo.max | ||
# | ||
# @good | ||
# bar = foo.minmax | ||
# return foo.minmax | ||
class MinMax < Cop | ||
MSG = 'Use `%<receiver>s.minmax` instead of `%<offender>s`.'.freeze | ||
|
||
def on_array(node) | ||
min_max_candidate(node) do |receiver| | ||
offender = offending_range(node) | ||
|
||
add_offense(node, offender, message(offender, receiver)) | ||
end | ||
end | ||
alias on_return on_array | ||
|
||
private | ||
|
||
def_node_matcher :min_max_candidate, <<-PATTERN | ||
({array return} (send $_receiver :min) (send $_receiver :max)) | ||
PATTERN | ||
|
||
def message(offender, receiver) | ||
format(MSG, offender: offender.source, | ||
receiver: receiver.source) | ||
end | ||
|
||
def autocorrect(node) | ||
receiver = node.children.first.receiver | ||
|
||
lambda do |corrector| | ||
corrector.replace(offending_range(node), | ||
"#{receiver.source}.minmax") | ||
end | ||
end | ||
|
||
def offending_range(node) | ||
case node.type | ||
when :return | ||
argument_range(node) | ||
else | ||
node.loc.expression | ||
end | ||
end | ||
|
||
def argument_range(node) | ||
first_argument_range = node.children.first.loc.expression | ||
last_argument_range = node.children.last.loc.expression | ||
|
||
first_argument_range.join(last_argument_range) | ||
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
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,100 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::Style::MinMax, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'with an array literal containing calls to `#min` and `#max`' do | ||
context 'when the expression stands alone' do | ||
it 'registers an offense if the receivers match' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
[foo.min, foo.max] | ||
^^^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `[foo.min, foo.max]`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if the receivers do not match' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
[foo.min, bar.max] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if there are additional elements' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
[foo.min, foo.baz, foo.max] | ||
RUBY | ||
end | ||
|
||
it 'auto-corrects an offense to use `#minmax`' do | ||
corrected = autocorrect_source(<<-RUBY.strip_indent) | ||
[foo.bar.min, foo.bar.max] | ||
RUBY | ||
|
||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
foo.bar.minmax | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the expression is used in a parallel assignment' do | ||
it 'registers an offense if the receivers match' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
bar = foo.min, foo.max | ||
^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `foo.min, foo.max`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if the receivers do not match' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
baz = foo.min, bar.max | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if there are additional elements' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
bar = foo.min, foo.baz, foo.max | ||
RUBY | ||
end | ||
|
||
it 'auto-corrects an offense to use `#minmax`' do | ||
corrected = autocorrect_source(<<-RUBY.strip_indent) | ||
baz = foo.bar.min, foo.bar.max | ||
RUBY | ||
|
||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
baz = foo.bar.minmax | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the expression is used as a return value' do | ||
it 'registers an offense if the receivers match' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
return foo.min, foo.max | ||
^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `foo.min, foo.max`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if the receivers do not match' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
return foo.min, bar.max | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense if there are additional elements' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
return foo.min, foo.baz, foo.max | ||
RUBY | ||
end | ||
|
||
it 'auto-corrects an offense to use `#minmax`' do | ||
corrected = autocorrect_source(<<-RUBY.strip_indent) | ||
return foo.bar.min, foo.bar.max | ||
RUBY | ||
|
||
expect(corrected).to eq(<<-RUBY.strip_indent) | ||
return foo.bar.minmax | ||
RUBY | ||
end | ||
end | ||
end | ||
end |