Skip to content

Commit

Permalink
WIP: Add cop for DiscourseEvent.on
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Feb 7, 2024
1 parent 745caba commit 6717fd4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/default.yml
Expand Up @@ -67,3 +67,9 @@ Discourse/Plugins/CallRequiresPlugin:
Enabled: true
Include:
- 'app/controllers/**/*'

Discourse/Plugins/DiscourseEvent:
Enabled: true
Include:
- 'plugin.rb'
- 'plugins/*/plugin.rb'
24 changes: 24 additions & 0 deletions lib/rubocop/cop/discourse/plugins/discourse_event.rb
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Discourse
module Plugins
class DiscourseEvent < Base
MSG =
"Use `on` instead of `DiscourseEvent.on` as the latter will listen to events even if the plugin is disabled."
RESTRICT_ON_SEND = [:on].freeze

def_node_matcher :discourse_event_on?, <<~MATCHER
(send (const nil? :DiscourseEvent) :on _)
MATCHER

def on_send(node)
return unless discourse_event_on?(node)
add_offense(node, message: MSG)
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/lib/rubocop/cop/plugins/discourse_event_spec.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "spec_helper"

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

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

context "when `DiscourseEvent.on` is called" do
it "registers an offense" do
expect_offense(<<~RUBY)
DiscourseEvent.on(:topic_status_updated) { do_something }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/DiscourseEvent: Use `on` instead of `DiscourseEvent.on` [...]
RUBY
end
end

context "when `on` is called" do
it "does not register an offense" do
expect_no_offenses(<<~RUBY)
on(:topic_status_updated) { do_something }
RUBY
end
end
end

0 comments on commit 6717fd4

Please sign in to comment.