Skip to content

Commit

Permalink
Add new Rails/Inquiry cop
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jun 29, 2020
1 parent 60ea152 commit decc820
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#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 @@ -300,6 +300,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 @@ -35,6 +35,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 @@ -1585,6 +1585,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 ActiveSupport `#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 ActiveSupport `#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 `ActiveSupport#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 @@ -37,6 +37,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 `ActiveSupport#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

0 comments on commit decc820

Please sign in to comment.