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

Add new Rails/Inquiry cop #282

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -10,6 +10,7 @@
* [#271](https://github.com/rubocop-hq/rubocop-rails/pull/271): Add new `Rails/RenderInline` cop. ([@fatkodima][])
* [#281](https://github.com/rubocop-hq/rubocop-rails/pull/281): Add new `Rails/MailerName` cop. ([@fatkodima][])
* [#280](https://github.com/rubocop-hq/rubocop-rails/pull/280): Add new `Rails/ShortI18n` cop. ([@fatkodima][])
* [#282](https://github.com/rubocop-hq/rubocop-rails/pull/282): Add new `Rails/Inquiry` cop. ([@fatkodima][])
* [#246](https://github.com/rubocop-hq/rubocop-rails/issues/246): Add new `Rails/PluckInWhere` cop. ([@fatkodima][])
* [#17](https://github.com/rubocop-hq/rubocop-rails/issues/17): Add new `Rails/NegateInclude` cop. ([@fatkodima][])
* [#278](https://github.com/rubocop-hq/rubocop-rails/pull/278): Add new `Rails/Pluck` cop. ([@eugeneius][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ Rails/IndexWith:
Enabled: true
VersionAdded: '2.5'

Rails/Inquiry:
Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
StyleGuide: 'https://rails.rubystyle.guide/#inquiry'
Enabled: 'pending'
VersionAdded: '2.7'

Rails/InverseOf:
Description: 'Checks for associations where the inverse cannot be determined automatically.'
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* xref:cops_rails.adoc#railsignoredskipactionfilteroption[Rails/IgnoredSkipActionFilterOption]
* xref:cops_rails.adoc#railsindexby[Rails/IndexBy]
* xref:cops_rails.adoc#railsindexwith[Rails/IndexWith]
* xref:cops_rails.adoc#railsinquiry[Rails/Inquiry]
* xref:cops_rails.adoc#railsinverseof[Rails/InverseOf]
* xref:cops_rails.adoc#railslexicallyscopedactionfilter[Rails/LexicallyScopedActionFilter]
* xref:cops_rails.adoc#railslinktoblank[Rails/LinkToBlank]
Expand Down
39 changes: 39 additions & 0 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,45 @@ Hash[[1, 2, 3].collect { |el| [el, foo(el)] }]
[1, 2, 3].index_with { |el| foo(el) }
----

== Rails/Inquiry

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Pending
| Yes
| No
| 2.7
| -
|===

This cop checks that Active Support's `inquiry` method is not used.

=== Examples

[source,ruby]
----
# bad - String#inquiry
ruby = 'two'.inquiry
ruby.two?

# good
ruby = 'two'
ruby == 'two'

# bad - Array#inquiry
pets = %w(cat dog).inquiry
pets.gopher?

# good
pets = %w(cat dog)
pets.include? 'cat'
----

=== References

* https://rails.rubystyle.guide/#inquiry

== Rails/InverseOf

|===
Expand Down
34 changes: 34 additions & 0 deletions lib/rubocop/cop/rails/inquiry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Rails
# This cop checks that Active Support's `inquiry` method is not used.
#
# @example
# # bad - String#inquiry
# ruby = 'two'.inquiry
# ruby.two?
#
# # good
# ruby = 'two'
# ruby == 'two'
#
# # bad - Array#inquiry
# pets = %w(cat dog).inquiry
# pets.gopher?
#
# # good
# pets = %w(cat dog)
# pets.include? 'cat'
#
class Inquiry < Cop
MSG = "Prefer Ruby's comparison operators over Active Support's `inquiry`."

def on_send(node)
add_offense(node, location: :selector) if node.method?(:inquiry) && node.arguments.empty?
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
require_relative 'rails/ignored_skip_action_filter_option'
require_relative 'rails/index_by'
require_relative 'rails/index_with'
require_relative 'rails/inquiry'
require_relative 'rails/inverse_of'
require_relative 'rails/lexically_scoped_action_filter'
require_relative 'rails/link_to_blank'
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/rails/inquiry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::Inquiry do
subject(:cop) { described_class.new }

it 'registers an offense when using `#inquiry`' do
expect_offense(<<~RUBY)
'two'.inquiry
^^^^^^^ Prefer Ruby's comparison operators over Active Support's `inquiry`.
RUBY
end

it 'does not register an offense when using `#inquiry` with arguments' do
expect_no_offenses(<<~RUBY)
foo.inquiry(bar)
RUBY
end
end