Skip to content

Commit

Permalink
WIP: handle class_eval on serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Feb 19, 2024
1 parent 76ca3a1 commit be2937e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/rubocop/cop/discourse/plugins/no_monkey_patching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class NoMonkeyPatching < Base
"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
Expand All @@ -18,10 +20,13 @@ class NoMonkeyPatching < Base
MATCHER

def_node_matcher :serializer?, <<~MATCHER
(class (const _ /Serializer$/) ...)
({class send} (const _ /Serializer$/) ...)
MATCHER

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

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/rubocop/cop/plugins/no_monkey_patching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def a_new_method
it "registers an offense" do
expect_offense(<<~RUBY)
UserSerializer.class_eval do
^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/NoMonkeyPatching: Don’t call `class_eval`. [...]
^^^^^^^^^^^^^^^^^^^^^^^^^ Discourse/Plugins/NoMonkeyPatching: Don’t call `class_eval` on a serializer. [...]
def a_new_method
:new_value
end
Expand Down

0 comments on commit be2937e

Please sign in to comment.