Class: RuboCop::Cop::Discourse::Services::MutableAttributeDefault
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Discourse::Services::MutableAttributeDefault
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/discourse/services/mutable_attribute_default.rb
Overview
Wrap mutable ‘attribute` defaults in a proc — `ActiveModel::Attributes` shares the literal across instances, so mutations leak between calls. `:array`-typed attributes are exempt; the type dups on read.
Constant Summary collapse
- MSG =
"Mutable `default: %<source>s` is shared across all instances; wrap it in a proc: `-> { %<source>s }`."- RESTRICT_ON_SEND =
%i[attribute].freeze
Instance Method Summary collapse
Instance Method Details
#on_class(node) ⇒ Object
49 50 51 |
# File 'lib/rubocop/cop/discourse/services/mutable_attribute_default.rb', line 49 def on_class(node) @service = true if service_include?(node) end |
#on_send(node) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/discourse/services/mutable_attribute_default.rb', line 53 def on_send(node) return unless @service return if array_typed?(node) default = mutable_default(node) return unless default source = default.source add_offense(default, message: format(MSG, source: source)) do |corrector| corrector.replace(default, "-> { #{source} }") end end |