Sha256: 06657005cc9b2984e1c3aeb99cc43450b38e39bb919ef04fb87a73f6a8ab9ea3

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      module Plugins
        # Using `DiscourseEvent.on` leaves the handler enabled when the plugin is disabled.
        #
        # @example
        #   # bad
        #   DiscourseEvent.on(:event) { do_something }
        #
        #   # good
        #   on(:event) { do_something }
        #
        class UsePluginInstanceOn < Base
          MSG =
            "Use `on` instead of `DiscourseEvent.on` as the latter will listen to events even if the plugin is disabled."
          NOT_OUTSIDE_PLUGIN_RB =
            "Don’t call `DiscourseEvent.on` outside `plugin.rb`."
          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)
            return add_offense(node, message: MSG) if in_plugin_rb_file?
            add_offense(node, message: NOT_OUTSIDE_PLUGIN_RB)
          end

          private

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.8.1 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.7.1 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb
rubocop-discourse-3.7.0 lib/rubocop/cop/discourse/plugins/use_plugin_instance_on.rb