class I18n::Tasks::Scanners::AstMatchers::MessageReceiversMatcher

Public Class Methods

new(scanner:, receivers:, message:) click to toggle source
# File lib/i18n/tasks/scanners/ast_matchers/message_receivers_matcher.rb, line 8
def initialize(scanner:, receivers:, message:)
  super(scanner: scanner)
  @receivers = Array(receivers)
  @message = message
end

Public Instance Methods

convert_to_key_occurrences(send_node, method_name, location: send_node.loc) click to toggle source

@param send_node [Parser::AST::Node] @param method_name [Symbol, nil] @param location [Parser::Source::Map] @return [nil, [key, Occurrence]] full absolute key name and the occurrence.

# File lib/i18n/tasks/scanners/ast_matchers/message_receivers_matcher.rb, line 18
def convert_to_key_occurrences(send_node, method_name, location: send_node.loc)
  return unless node_match?(send_node)

  receiver = send_node.children[0]
  first_arg_node = send_node.children[2]
  second_arg_node = send_node.children[3]

  key = extract_string(first_arg_node)
  return if key.nil?

  key, default_arg = process_options(node: second_arg_node, key: key)

  return if key.nil?

  [
    full_key(receiver: receiver, key: key, location: location, calling_method: method_name),
    I18n::Tasks::Scanners::Results::Occurrence.from_range(
      raw_key: key,
      range: location.expression,
      default_arg: default_arg
    )
  ]
end

Private Instance Methods

full_key(receiver:, key:, location:, calling_method:) click to toggle source
# File lib/i18n/tasks/scanners/ast_matchers/message_receivers_matcher.rb, line 51
def full_key(receiver:, key:, location:, calling_method:)
  if receiver.nil?
    # Relative keys only work if called via `t()` but not `I18n.t()`:
    @scanner.absolute_key(
      key,
      location.expression.source_buffer.name,
      calling_method: calling_method
    )
  else
    key
  end
end
node_match?(node) click to toggle source
# File lib/i18n/tasks/scanners/ast_matchers/message_receivers_matcher.rb, line 44
def node_match?(node)
  receiver = node.children[0]
  message = node.children[1]

  @message == message && @receivers.any? { |r| r == receiver }
end
process_options(node:, key:) click to toggle source
# File lib/i18n/tasks/scanners/ast_matchers/message_receivers_matcher.rb, line 64
def process_options(node:, key:)
  return [key, nil] if node&.type != :hash

  scope_node = extract_hash_pair(node, 'scope')

  if scope_node
    scope = extract_string(
      scope_node.children[1],
      array_join_with: '.',
      array_flatten: true,
      array_reject_blank: true
    )
    return nil if scope.nil? && scope_node.type != :nil

    key = [scope, key].join('.') unless scope == ''
  end
  if default_arg_node = extract_hash_pair(node, 'default')
    default_arg = if default_arg_node.children[1]&.type == :hash
                    extract_hash(default_arg_node.children[1])
                  else
                    extract_string(default_arg_node.children[1])
                  end
  end

  [key, default_arg]
end