Skip to content

Commit

Permalink
Added Chewy/FieldType
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Sep 19, 2023
1 parent 268fb76 commit 51743d7
Show file tree
Hide file tree
Showing 4 changed files with 84 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 `Chewy/FieldType`. ([#X](https://github.com/petalmd/rubocop-petal/pull/X))
* Fix `Migration/ChangeTableReferences` offense location (Fix [#61](https://github.com/petalmd/rubocop-petal/issues/61)) ([#62](https://github.com/petalmd/rubocop-petal/pull/62))
* Added cop `Sidekiq/NoEarlyNilReturn` ([#58](https://github.com/petalmd/rubocop-petal/pull/58))
* Added cop `Rails/EnumStartingValue` ([#57](https://github.com/petalmd/rubocop-petal/pull/57))
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Chewy/FieldType:
Description: 'Assure that a type is defined for a Chewy field.'
Enabled: true
Include:
- app/chewy/**/*

Grape/PreferNamespace:
Description: 'Prevent usage of namespace aliases.'
Enabled: true
Expand Down
31 changes: 31 additions & 0 deletions lib/rubocop/cop/chewy/field_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Chewy
# This cop force to specify a type for Chewy field
#
# # bad
# field :name
#
# # good
# field :name, type: 'text'
class FieldType < Base
MSG = 'Specify a `type` for Chewy field.'

RESTRICT_ON_SEND = %i[field].freeze

# @!method options_has_field?(node)
def_node_matcher :options_has_field?, <<~PATTERN
(send nil? :field (sym _)+ (hash <(pair (sym :type) {str sym}) ...>))
PATTERN

def on_send(node)
return if options_has_field?(node)

add_offense(node)
end
end
end
end
end
46 changes: 46 additions & 0 deletions spec/rubocop/cop/chewy/field_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Chewy::FieldType, :config do
it 'registers an offense when no specifying a field' do
expect_offense(<<~RUBY)
field :name
^^^^^^^^^^^ Specify a `type` for Chewy field.
RUBY

expect_offense(<<~RUBY)
field :name, value: 'text'
^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field.
RUBY

expect_offense(<<~RUBY)
field :name, value: lambda { |a| a.name.upcase }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field.
RUBY

expect_offense(<<~RUBY)
defined_type User do
field :name
^^^^^^^^^^^ Specify a `type` for Chewy field.
end
RUBY

expect_offense(<<~RUBY)
field :name, :email
^^^^^^^^^^^^^^^^^^^ Specify a `type` for Chewy field.
RUBY
end

it 'does not register an offense when a type is specified' do
expect_no_offenses(<<~RUBY)
field :name, type: 'text'
RUBY

expect_no_offenses(<<~RUBY)
field :name, type: :text
RUBY

expect_no_offenses(<<~RUBY)
field :name, :email, type: 'text'
RUBY
end
end

0 comments on commit 51743d7

Please sign in to comment.