Class: RuboCop::Cop::Legion::HelperMigration::DirectCrypt

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/helper_migration/direct_crypt.rb

Overview

Detects direct calls to ‘Legion::Crypt` methods and suggests using the `vault_*` helpers instead.

Examples:

# bad
Legion::Crypt.get('secret/path')
Legion::Crypt.exist?('secret/path')
Legion::Crypt.write('secret/path', data)

# good
vault_get('secret/path')
vault_exist?('secret/path')
vault_write('secret/path', data)

Constant Summary collapse

MSG =
'Use `%<helper>s` instead of `Legion::Crypt.%<method>s`. ' \
'Include the appropriate Vault/Crypt helper mixin.'
RESTRICT_ON_SEND =
%i[get exist? write].freeze
HELPER_MAP =
{
  get: 'vault_get',
  exist?: 'vault_exist?',
  write: 'vault_write'
}.freeze

Instance Method Summary collapse

Instance Method Details

#legion_crypt_call?(node) ⇒ Object



35
36
37
# File 'lib/rubocop/cop/legion/helper_migration/direct_crypt.rb', line 35

def_node_matcher :legion_crypt_call?, <<~PATTERN
  (send (const (const nil? :Legion) :Crypt) {:get :exist? :write} ...)
PATTERN

#on_send(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/legion/helper_migration/direct_crypt.rb', line 39

def on_send(node)
  return unless legion_crypt_call?(node)

  method_name = node.method_name
  helper = HELPER_MAP[method_name]
  message = format(MSG, helper: helper, method: method_name)

  add_offense(node, message: message) do |corrector|
    args_source = node.arguments.map(&:source).join(', ')
    corrector.replace(node, "#{helper}(#{args_source})")
  end
end