Skip to content

Commit

Permalink
WIP: Add UseRequireRelative cop
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Feb 13, 2024
1 parent 569c4fb commit 9399744
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ Discourse/Plugins/NamespaceConstants:
- '**/spec/**/*'
- '**/tasks/**/*.rake'
- '**/db/fixtures/**/*'

Discourse/Plugins/UseRequireRelative:
Enabled: true
23 changes: 23 additions & 0 deletions lib/rubocop/cop/discourse/plugins/use_require_relative.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Discourse
module Plugins
class UseRequireRelative < Base
MSG = "Use `require_relative` instead of `load`."
RESTRICT_ON_SEND = [:load].freeze

def_node_matcher :load_called?, <<~MATCHER
(send nil? :load _)
MATCHER

def on_send(node)
return unless load_called?(node)
add_offense(node, message: MSG)
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/lib/rubocop/cop/plugins/use_require_relative_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe RuboCop::Cop::Discourse::Plugins::UseRequireRelative, :config do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

context "when using `load`" do
it "registers an offense" do
expect_offense(<<~RUBY)
load File.expand_path("../app/jobs/onceoff/voting_ensure_consistency.rb", __FILE__)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/UseRequireRelative: Use `require_relative` instead of `load`.
RUBY
end
end

context "when using `require_relative`" do
it "does not register an offense" do
expect_no_offenses(<<~RUBY)
require_relative "app/controllers/encrypt_controller.rb"
RUBY
end
end
end

0 comments on commit 9399744

Please sign in to comment.