Skip to content

Commit

Permalink
Snif cop (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx authored Feb 16, 2023
1 parent 7269a3b commit 4ebab6d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# main

* Added cop `Performance/Snif` ([#31](https://github.com/petalmd/rubocop-petal/pull/31))
* Updated gemspec file. ([#30](https://github.com/petalmd/rubocop-petal/pull/30))
* Added cop `RSpec/JsonParseResponseBody and RSpec/JsonResponse` ([#27](https://github.com/petalmd/rubocop-petal/pull/27))

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ RSpec/JsonResponse:
SafeAutoCorrect: true
Include:
- spec/**/*

Performance/Snif:
Description: 'Prevent snif in favor of detect'
Enabled: true
30 changes: 30 additions & 0 deletions lib/rubocop/cop/performance/snif.rb
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
15 changes: 15 additions & 0 deletions spec/rubocop/cop/performance/snif_spec.rb
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

0 comments on commit 4ebab6d

Please sign in to comment.