diff --git a/CHANGELOG.md b/CHANGELOG.md index 984959e..9e4d02a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ # main +* Added cop `Chewy/FieldType`. ([#64](https://github.com/petalmd/rubocop-petal/pull/64)) * 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)) diff --git a/config/default.yml b/config/default.yml index f3ba0b3..1b428df 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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 diff --git a/lib/rubocop/cop/chewy/field_type.rb b/lib/rubocop/cop/chewy/field_type.rb new file mode 100644 index 0000000..f198d06 --- /dev/null +++ b/lib/rubocop/cop/chewy/field_type.rb @@ -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 diff --git a/spec/rubocop/cop/chewy/field_type_spec.rb b/spec/rubocop/cop/chewy/field_type_spec.rb new file mode 100644 index 0000000..4cb4596 --- /dev/null +++ b/spec/rubocop/cop/chewy/field_type_spec.rb @@ -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