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

Snif cop #31

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -4,6 +4,7 @@

* 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))
* Added cop `Performance/Snif` ([#27](https://github.com/petalmd/rubocop-petal/pull/31))

# v0.7.0

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