-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7269a3b
commit 4ebab6d
Showing
4 changed files
with
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# Prevent using `snif`. | ||
# Consider using `detect`. | ||
# | ||
# @example | ||
# # bad | ||
# period.equity_packs.snif(:code, 'ONCALL') | ||
# | ||
# # good | ||
# period.equity_packs.detect { |equity_pack| equity_pack.code == 'ONCALL' } | ||
class Snif < Base | ||
MSG = 'Use `detect` instead.' | ||
|
||
def_node_matcher :snif?, <<~PATTERN | ||
(send _ :snif _ _) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless snif?(node) | ||
|
||
add_offense(node) | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::Snif, :config do | ||
it 'registers an offense when using `snif`', :aggregate_failures do | ||
expect_offense(<<~RUBY) | ||
test.snif(:attribute, value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `detect` instead. | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
test.snif(attribute, value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `detect` instead. | ||
RUBY | ||
end | ||
end |