Class: RuboCop::Cop::HGOOSTDD::NoSecureRandomInDomain

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/hgoostdd/no_secure_random_in_domain.rb

Overview

Domain code must generate IDs via an injected ‘id_gen:` collaborator, not via the `SecureRandom` constant. See HGOOSTDD §6.

Autocorrect rewrites ‘SecureRandom.<method>(args)` to `@id_gen.<method>(args)`, preserving the method name and args. Marked unsafe — assumes `@id_gen` exists and exposes a matching method (e.g. `IdGen#uuid` for `SecureRandom.uuid`).

Constant Summary collapse

MSG =
"Inject an `id_gen:` collaborator; do not call `%{call}` in domain code (§6)."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/hgoostdd/no_secure_random_in_domain.rb', line 20

def on_send(node)
  return unless (method_name = secure_random_send?(node))

  add_offense(node, message: format(MSG, call: node.source)) do |corrector|
    args = node.arguments.map(&:source).join(", ")
    replacement = args.empty? ? "@id_gen.#{method_name}" : "@id_gen.#{method_name}(#{args})"
    corrector.replace(node, replacement)
  end
end