Skip to content

Commit

Permalink
💄 Add custom cop for include Hyrax::ArResource
Browse files Browse the repository at this point in the history
This commit will add a custom cop to RuboCop that will check for
`include Hyrax::ArResrouce` or `include ArRdResource` in the codebase.
The `ArResource` module is just for development ease of use and should
not be used within the codebase.

The custom cop leaves an example for additional custom cops that can be
used in the future and where to test them.  The key is to make sure the
`.rubocop.yml` file is updated to include the new cop.  The pattern for
it is adding something like Hyrax::RuboCop::CustomCops::SomeCop would be

```yaml
Hyrax/SomeCop:
  Enabled: true
```

From what I gather it is taking the top most namespace and then the
class name.
  • Loading branch information
Kirk Wang committed Apr 4, 2024
1 parent 54bb7b2 commit ef2ffa4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require: ./lib/hyrax/rubocop/custom_cops.rb

inherit_from:
- .rubocop_fixme.yml

Expand Down Expand Up @@ -125,3 +127,6 @@ RSpec/NestedGroups:

RSpec/LeadingSubject:
Enabled: false

Hyrax/ArResource:
Enabled: true
1 change: 1 addition & 0 deletions lib/hyrax/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Engine < ::Rails::Engine
require 'hyrax/transactions'
require 'hyrax/errors'
require 'hyrax/valkyrie_simple_path_generator'
require 'hyrax/rubocop/custom_cops'

# Force these models to be added to Legato's registry in development mode
config.eager_load_paths += %W[
Expand Down
30 changes: 30 additions & 0 deletions lib/hyrax/rubocop/custom_cops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rubocop'

module Hyrax
module RuboCop
module CustomCops
# This custom cop checks for mixins of Hyrax::ArResource
class ArResource < ::RuboCop::Cop::Cop
MSG = 'Do not `include Hyrax::ArResource`.'

# checks for `include Hyrax::ArResource`
def_node_search :includes_hyrax_ar_resource?, <<-PATTERN
(send nil? {:include :extend} (const (const nil? :Hyrax) :ArResource))
PATTERN

# checks for `include ArResource`
def_node_search :includes_ar_resource?, <<-PATTERN
(send nil? {:include :extend} (const nil? :ArResource))
PATTERN

def on_send(send_node)
add_offense(send_node, message: MSG) if includes_hyrax_ar_resource?(send_node) || includes_ar_resource?(send_node)
end
end

# class AdditionalCustomCops < ::RuboCop::Cop::Cop; end
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/hyrax/rubocop/custom_cops_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

RSpec.describe Hyrax::RuboCop::CustomCops::ArResource do
subject(:cop) { described_class.new }

it 'is not allowed to include Hyrax::ArResource' do
expect_offense(<<~RUBY)
include Hyrax::ArResource
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not `include Hyrax::ArResource`.
RUBY
end

it 'is not allowed to include ArResource' do
expect_offense(<<~RUBY)
include ArResource
^^^^^^^^^^^^^^^^^^ Do not `include Hyrax::ArResource`.
RUBY
end
end

# RSpec.describe Hyrax::RuboCop::CustomCops::AdditionalCustomCops do; end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def ci_build?
require 'hyrax/specs/clamav'
require 'hyrax/specs/engine_routes'

require 'rubocop'
require 'rubocop/rspec/support'

# ensure Hyrax::Schema gets loaded is resolvable for `support/` models
Hyrax::Schema # rubocop:disable Lint/Void

Expand Down Expand Up @@ -364,4 +367,6 @@ def clean_active_fedora_repository
.to receive(:storage_adapter)
.and_return(Valkyrie::StorageAdapter.find(adapter_name))
end

config.include RuboCop::RSpec::ExpectOffense
end

0 comments on commit ef2ffa4

Please sign in to comment.