-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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,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 |