Class: RuboCop::Cop::Discourse::Plugins::NoMonkeyPatching
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Discourse::Plugins::NoMonkeyPatching
- Defined in:
- lib/rubocop/cop/discourse/plugins/no_monkey_patching.rb
Overview
Don’t monkey-patch classes directly in ‘plugin.rb`. Instead, define additional methods in a dedicated mixin (an ActiveSupport concern for example) and use `prepend` (this allows calling `super` from the mixin).
If you’re just adding new methods to an existing serializer, then use ‘add_to_serializer` instead.
Constant Summary collapse
- MSG =
"Don’t reopen existing classes. Instead, create a mixin and use `prepend`."
- MSG_CLASS_EVAL =
"Don’t call `class_eval`. Instead, create a mixin and use `prepend`."
- MSG_CLASS_EVAL_SERIALIZERS =
"Don’t call `class_eval` on a serializer. If you’re adding new methods, use `add_to_serializer`. Otherwise, create a mixin and use `prepend`."
- MSG_SERIALIZERS =
"Don’t reopen serializers. Instead, use `add_to_serializer`."
- RESTRICT_ON_SEND =
[:class_eval].freeze
Instance Method Summary collapse
Instance Method Details
#on_class(node) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/rubocop/cop/discourse/plugins/no_monkey_patching.rb', line 74 def on_class(node) return unless in_plugin_rb_file? return unless existing_class?(node) if serializer?(node) return add_offense(node, message: MSG_SERIALIZERS) end add_offense(node, message: MSG) end |
#on_send(node) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/rubocop/cop/discourse/plugins/no_monkey_patching.rb', line 67 def on_send(node) if serializer?(node) return add_offense(node, message: MSG_CLASS_EVAL_SERIALIZERS) end add_offense(node, message: MSG_CLASS_EVAL) end |