Skip to content

Commit

Permalink
WIP: add CheckPluginIsEnabled cop
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Feb 20, 2024
1 parent be2937e commit dde0c07
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/default.yml
Expand Up @@ -90,3 +90,6 @@ Discourse/Plugins/UseRequireRelative:

Discourse/Plugins/NoMonkeyPatching:
Enabled: true

Discourse/Plugins/CheckPluginIsEnabled:
Enabled: true
38 changes: 38 additions & 0 deletions lib/rubocop/cop/discourse/plugins/check_plugin_is_enabled.rb
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Discourse
module Plugins
class CheckPluginIsEnabled < Base
MSG = "Check the plugin is enabled."

def_node_matcher :existing_class?, <<~MATCHER
(class (const (cbase) _) ...)
MATCHER

def_node_matcher :plugin_enabled_check?, <<~MATCHER
(def _ _ (begin (if (send (const nil? :SiteSetting) /_enabled\\?$/) nil? (return)) ...))
MATCHER

def on_def(node)
return unless in_plugin_rb_file?
return unless in_existing_class?(node)
return if plugin_enabled_check?(node)
add_offense(node, message: MSG)
end

private

def in_plugin_rb_file?
processed_source.path.split("/").last == "plugin.rb"
end

def in_existing_class?(node)
node.each_ancestor(:class).detect(&method(:existing_class?))
end
end
end
end
end
end
68 changes: 68 additions & 0 deletions spec/lib/rubocop/cop/plugins/check_plugin_is_enabled_spec.rb
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require "spec_helper"

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

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

context "when outside `plugin.rb`" do
it "does not register an offense" do
expect_no_offenses(<<~RUBY, "my_class.rb")
class ::MyClass
def my_method
"my_value"
end
end
class AnotherClass
def my_method
"my_value"
end
end
RUBY
end
end

context "when inside `plugin.rb`" do
context "when opening an existing class" do
it "registers an offense" do
expect_offense(<<~RUBY, "plugin.rb")
after_initialize do
class ::Post
after_commit :generate_zendesk_ticket, on: [:create]
private
def generate_zendesk_ticket
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/CheckPluginIsEnabled: Check the plugin is enabled.
return unless DiscourseZendeskPlugin::Helper.autogeneration_category?(topic.category_id)
Jobs.enqueue_in(5.seconds, :zendesk_job, post_id: id)
end
end
end
RUBY
end

it "does not register an offense" do
expect_no_offenses(<<~RUBY, "plugin.rb")
after_initialize do
class ::Post
after_commit :generate_zendesk_ticket, on: [:create]
private
def generate_zendesk_ticket
return unless SiteSetting.zendesk_enabled?
return unless DiscourseZendeskPlugin::Helper.autogeneration_category?(topic.category_id)
Jobs.enqueue_in(5.seconds, :zendesk_job, post_id: id)
end
end
end
RUBY
end
end
end
end

0 comments on commit dde0c07

Please sign in to comment.