class ExtractI18n::Adapters::Rewriter

Constants

PASTEL
PROMPT

Public Class Methods

new(file_key:, on_ask:) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 32
def initialize(file_key:, on_ask:)
  @file_key = file_key
  @on_ask = on_ask
end

Public Instance Methods

on_dstr(node) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 44
def on_dstr(node)
  if ignore?(node, parent: @nesting[-2])
    return
  end
  interpolate_arguments = {}
  out_string = ""
  node.children.each do |i|
    if i.type == :str
      out_string += i.children.first
    else
      inner_source = i.children[0].loc.expression.source.gsub(/^#\{|}$/, '')
      interpolate_key = ExtractI18n.key(inner_source)
      out_string += "%{#{interpolate_key}}"
      interpolate_arguments[interpolate_key] = inner_source
    end
  end

  i18n_key = ExtractI18n.key(node.children.select { |i| i.type == :str }.map { |i| i.children[0] }.join(' '))

  ask_and_continue(
    i18n_key: i18n_key, i18n_string: out_string, interpolate_arguments: interpolate_arguments, node: node,
  )
end
on_str(node) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 68
def on_str(node)
  string = node.children.first
  if ignore?(node, parent: @nesting[-2])
    return
  end
  ask_and_continue(i18n_key: ExtractI18n.key(string), i18n_string: string, node: node)
end
process(node) click to toggle source
Calls superclass method
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 37
def process(node)
  @nesting ||= []
  @nesting.push(node)
  super
  @nesting.pop
end

Private Instance Methods

ask_and_continue(i18n_key:, i18n_string:, interpolate_arguments: {}, node:) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 78
def ask_and_continue(i18n_key:, i18n_string:, interpolate_arguments: {}, node:)
  change = ExtractI18n::SourceChange.new(
    i18n_key: "#{@file_key}.#{i18n_key}",
    i18n_string: i18n_string,
    interpolate_arguments: interpolate_arguments,
    source_line: node.location.expression.source_line,
    remove: node.loc.expression.source
  )
  if @on_ask.call(change)
    replace_content(node, change.i18n_t)
  end
end
ignore?(node, parent: nil) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 103
def ignore?(node, parent: nil)
  unless node.respond_to?(:children)
    return false
  end
  if parent && ignore_parent?(parent)
    return true
  end
  if node.type == :str
    ExtractI18n.ignorelist.any? { |item| node.children[0][item] }
  else
    node.children.any? { |child|
      ignore?(child)
    }
  end
end
ignore_parent?(node) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 119
def ignore_parent?(node)
  node.children[1] == :require ||
    node.type == :regexp ||
    (node.type == :pair && ExtractI18n.ignore_hash_keys.include?(node.children[0].children[0].to_s)) ||
    (node.type == :send && ExtractI18n.ignore_functions.include?(node.children[1].to_s))
end
log(string) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 91
def log(string)
  puts string
end
replace_content(node, content) click to toggle source
# File lib/extract_i18n/adapters/ruby_adapter.rb, line 95
def replace_content(node, content)
  if node.loc.is_a?(Parser::Source::Map::Heredoc)
    replace(node.loc.expression.join(node.loc.heredoc_end), content)
  else
    replace(node.loc.expression, content)
  end
end