-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 Add custom cop for
include Hyrax::ArResource
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
Showing
5 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters